View Javadoc
1   /**
2    * This file Copyright (c) 2015-2018 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
32   *
33   */
34  package info.magnolia.templating.imaging.variation;
35  
36  import static org.junit.Assert.assertEquals;
37  import static org.mockito.Mockito.*;
38  
39  import info.magnolia.cms.beans.runtime.FileProperties;
40  import info.magnolia.context.MgnlContext;
41  import info.magnolia.jcr.util.NodeTypes;
42  import info.magnolia.module.site.Site;
43  import info.magnolia.module.site.SiteManager;
44  import info.magnolia.module.site.theme.ThemeReference;
45  import info.magnolia.repository.RepositoryConstants;
46  import info.magnolia.test.ComponentsTestUtil;
47  import info.magnolia.test.mock.MockWebContext;
48  import info.magnolia.test.mock.jcr.MockNode;
49  import info.magnolia.test.mock.jcr.MockSession;
50  
51  import javax.jcr.Node;
52  import javax.jcr.Property;
53  import javax.jcr.RepositoryException;
54  
55  import org.junit.After;
56  import org.junit.Before;
57  import org.junit.Test;
58  
59  /**
60   * Tests for {@link info.magnolia.templating.imaging.variation.ImageOperationProvidingVariation}.
61   */
62  public class ImageOperationProvidingVariationTest {
63  
64      private Property imageProperty;
65      private MockNode imageNode;
66      private SiteManager siteManager;
67      private ImageOperationProvidingVariation imageOperationProvidingVariation;
68  
69      @Before
70      public void setUp() throws Exception {
71          MockWebContext ctx = new MockWebContext();
72          MgnlContext.setInstance(ctx);
73          MockSession session = new MockSession(RepositoryConstants.WEBSITE);
74          ctx.addSession(RepositoryConstants.WEBSITE, session);
75          ctx.setContextPath("http://localhost:9090/demoAuthor");
76  
77          String[] imageNodeName = { "image" };
78          imageNode = createChildNodes((MockNode) session.getRootNode(), imageNodeName, NodeTypes.Page.NAME);
79          imageNode.setProperty(FileProperties.PROPERTY_FILENAME, "fileName");
80          imageNode.setProperty(FileProperties.EXTENSION, "jpg");
81          imageProperty = imageNode.setProperty("document", "img");
82  
83          Site site = mock(Site.class);
84          ThemeReference themeRef = new ThemeReference();
85          themeRef.setName("myTheme");
86          when(site.getTheme()).thenReturn(themeRef);
87          siteManager = mock(SiteManager.class);
88          when(siteManager.getAssignedSite(any(Node.class))).thenReturn(site);
89  
90          imageOperationProvidingVariation = new ImageOperationProvidingVariation(siteManager);
91          imageOperationProvidingVariation.setGeneratorName("generatorName");
92          imageOperationProvidingVariation.setName("name");
93      }
94  
95      @After
96      public void tearDown() {
97          ComponentsTestUtil.clear();
98          MgnlContext.setInstance(null);
99      }
100 
101     @Test
102     public void createImageOperationProvidingVariation() throws Exception {
103         // GIVEN
104         ImageOperationProvidingVariation imageOperationProvidingVariation = new ImageOperationProvidingVariation(null);
105 
106         // WHEN
107         String generatorName = imageOperationProvidingVariation.getGeneratorName();
108 
109         // THEN
110         assertEquals("mte", generatorName);
111     }
112 
113     @Test
114     public void createLink() throws RepositoryException {
115         // WHEN
116         String link = imageOperationProvidingVariation.createLink(imageProperty);
117 
118         // THEN
119         assertEquals("http://localhost:9090/demoAuthor/.imaging/generatorName/myTheme/name/website/image/fileName.jpg", link);
120     }
121 
122     @Test
123     public void createLinkWithoutBlankSpaces() throws Exception {
124         // GIVEN
125         imageNode.setProperty(FileProperties.PROPERTY_FILENAME, "name with blank spaces");
126 
127         // WHEN
128         String link = imageOperationProvidingVariation.createLink(imageProperty);
129 
130         // THEN
131         assertEquals("http://localhost:9090/demoAuthor/.imaging/generatorName/myTheme/name/website/image/name%20with%20blank%20spaces.jpg", link);
132     }
133 
134     @Test
135     public void createLinkDoNotDuplicateExtension() throws RepositoryException {
136         // GIVEN
137         imageNode.setProperty(FileProperties.PROPERTY_FILENAME, "fileName.jpg");
138 
139         // WHEN
140         String link = imageOperationProvidingVariation.createLink(imageProperty);
141 
142         // THEN
143         assertEquals("http://localhost:9090/demoAuthor/.imaging/generatorName/myTheme/name/website/image/fileName.jpg", link);
144     }
145 
146     @Test
147     public void createLinkWhenFilenameDoesNotHaveExtension() throws RepositoryException {
148         // GIVEN
149         imageNode.setProperty(FileProperties.PROPERTY_FILENAME, "a_jpg");
150         imageNode.setProperty(FileProperties.EXTENSION, "jpg");
151 
152         // WHEN
153         String link = imageOperationProvidingVariation.createLink(imageProperty);
154 
155         // THEN
156         assertEquals("http://localhost:9090/demoAuthor/.imaging/generatorName/myTheme/name/website/image/a_jpg.jpg", link);
157     }
158 
159     @Test
160     public void createLinkWhenExtensionIsEmpty() throws RepositoryException {
161         // GIVEN
162         imageNode.setProperty(FileProperties.EXTENSION, "");
163 
164         // WHEN
165         String link = imageOperationProvidingVariation.createLink(imageProperty);
166 
167         // THEN
168         assertEquals("http://localhost:9090/demoAuthor/.imaging/generatorName/myTheme/name/website/image/fileName", link);
169     }
170 
171     protected MockNode createChildNodes(MockNode parent, String[] childNodeNames, String nodeTypeName) throws RepositoryException {
172         for (String nodeName : childNodeNames) {
173             MockNode child = new MockNode(nodeName, nodeTypeName);
174             parent.addNode(child);
175         }
176         return childNodeNames.length == 0 ? null : (MockNode) parent.getNode(childNodeNames[0]);
177     }
178 }