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.File;
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 ImageOperationProvidingVariation imageOperationProvidingVariation;
67  
68      @Before
69      public void setUp() throws Exception {
70          MockWebContext ctx = new MockWebContext();
71          MgnlContext.setInstance(ctx);
72          MockSession session = new MockSession(RepositoryConstants.WEBSITE);
73          ctx.addSession(RepositoryConstants.WEBSITE, session);
74          ctx.setContextPath("http://localhost:9090/demoAuthor");
75  
76          String[] imageNodeName = { "image" };
77          imageNode = createChildNodes((MockNode) session.getRootNode(), imageNodeName, NodeTypes.Page.NAME);
78          imageNode.setProperty(File.PROPERTY_FILENAME, "fileName");
79          imageNode.setProperty(File.EXTENSION, "jpg");
80          imageProperty = imageNode.setProperty("document", "img");
81  
82          Site site = mock(Site.class);
83          ThemeReference themeRef = new ThemeReference();
84          themeRef.setName("myTheme");
85          when(site.getTheme()).thenReturn(themeRef);
86          SiteManager siteManager = mock(SiteManager.class);
87          when(siteManager.getAssignedSite(any(Node.class))).thenReturn(site);
88  
89          imageOperationProvidingVariation = new ImageOperationProvidingVariation(siteManager);
90          imageOperationProvidingVariation.setGeneratorName("generatorName");
91          imageOperationProvidingVariation.setName("name");
92      }
93  
94      @After
95      public void tearDown() {
96          ComponentsTestUtil.clear();
97          MgnlContext.setInstance(null);
98      }
99  
100     @Test
101     public void createImageOperationProvidingVariation() throws Exception {
102         // GIVEN
103         ImageOperationProvidingVariation imageOperationProvidingVariation = new ImageOperationProvidingVariation(null);
104 
105         // WHEN
106         String generatorName = imageOperationProvidingVariation.getGeneratorName();
107 
108         // THEN
109         assertEquals("mte", generatorName);
110     }
111 
112     @Test
113     public void createLink() throws RepositoryException {
114         // WHEN
115         String link = imageOperationProvidingVariation.createLink(imageProperty);
116 
117         // THEN
118         assertEquals("http://localhost:9090/demoAuthor/.imaging/generatorName/myTheme/name/website/image/fileName.jpg", link);
119     }
120 
121     @Test
122     public void createLinkWithoutBlankSpaces() throws Exception {
123         // GIVEN
124         imageNode.setProperty(File.PROPERTY_FILENAME, "name with blank spaces");
125 
126         // WHEN
127         String link = imageOperationProvidingVariation.createLink(imageProperty);
128 
129         // THEN
130         assertEquals("http://localhost:9090/demoAuthor/.imaging/generatorName/myTheme/name/website/image/name%20with%20blank%20spaces.jpg", link);
131     }
132 
133     @Test
134     public void createLinkDoNotDuplicateExtension() throws RepositoryException {
135         // GIVEN
136         imageNode.setProperty(File.PROPERTY_FILENAME, "fileName.jpg");
137 
138         // WHEN
139         String link = imageOperationProvidingVariation.createLink(imageProperty);
140 
141         // THEN
142         assertEquals("http://localhost:9090/demoAuthor/.imaging/generatorName/myTheme/name/website/image/fileName.jpg", link);
143     }
144 
145     @Test
146     public void createLinkWhenFilenameDoesNotHaveExtension() throws RepositoryException {
147         // GIVEN
148         imageNode.setProperty(File.PROPERTY_FILENAME, "a_jpg");
149         imageNode.setProperty(File.EXTENSION, "jpg");
150 
151         // WHEN
152         String link = imageOperationProvidingVariation.createLink(imageProperty);
153 
154         // THEN
155         assertEquals("http://localhost:9090/demoAuthor/.imaging/generatorName/myTheme/name/website/image/a_jpg.jpg", link);
156     }
157 
158     @Test
159     public void createLinkWhenExtensionIsEmpty() throws RepositoryException {
160         // GIVEN
161         imageNode.setProperty(File.EXTENSION, "");
162 
163         // WHEN
164         String link = imageOperationProvidingVariation.createLink(imageProperty);
165 
166         // THEN
167         assertEquals("http://localhost:9090/demoAuthor/.imaging/generatorName/myTheme/name/website/image/fileName", link);
168     }
169 
170     protected MockNode createChildNodes(MockNode parent, String[] childNodeNames, String nodeTypeName) throws RepositoryException {
171         for (String nodeName : childNodeNames) {
172             MockNode child = new MockNode(nodeName, nodeTypeName);
173             parent.addNode(child);
174         }
175         return childNodeNames.length == 0 ? null : (MockNode) parent.getNode(childNodeNames[0]);
176     }
177 }