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;
35  
36  import static org.hamcrest.CoreMatchers.is;
37  import static org.junit.Assert.assertThat;
38  import static org.mockito.Mockito.*;
39  
40  import info.magnolia.cms.beans.runtime.File;
41  import info.magnolia.context.MgnlContext;
42  import info.magnolia.imaging.ImagingSupport;
43  import info.magnolia.module.site.Site;
44  import info.magnolia.module.site.SiteManager;
45  import info.magnolia.module.site.theme.ThemeReference;
46  import info.magnolia.templating.imaging.variation.SimpleResizeVariation;
47  import info.magnolia.test.mock.MockWebContext;
48  
49  import javax.jcr.Node;
50  import javax.jcr.Property;
51  import javax.jcr.RepositoryException;
52  import javax.jcr.Session;
53  import javax.jcr.Workspace;
54  
55  import org.junit.After;
56  import org.junit.Before;
57  import org.junit.Test;
58  
59  /**
60   * Tests for {@link VariationAwareImagingSupport}.
61   */
62  public class VariationAwareImagingSupportTest {
63  
64      private static final String WORKSPACE_NAME = "workspace";
65      private static final String CONTEXT_PATH = "contextPath";
66      private static final String PATH_TO_NODE = "/path-to-node";
67      private static final String THEME_NAME = "theme-name";
68      private static final String FILENAME = "filename";
69      private static final String EXTENSION = "jpg";
70  
71      private VariationAwareImagingSupport variationAwareImagingSupport;
72      private SiteManager siteManager;
73      private Session session;
74  
75      private Node binaryNode;
76      private Property binaryProperty;
77  
78      @Before
79      public void setUp() throws RepositoryException {
80          MockWebContext ctx = new MockWebContext();
81          ctx.setContextPath(CONTEXT_PATH);
82          MgnlContext.setInstance(ctx);
83  
84          variationAwareImagingSupport = new VariationAwareImagingSupport();
85          siteManager = mock(SiteManager.class);
86          session = mock(Session.class);
87  
88          Workspace workspace = mock(Workspace.class);
89          when(workspace.getName()).thenReturn(WORKSPACE_NAME);
90          when(session.getWorkspace()).thenReturn(workspace);
91  
92          ThemeReference themeReference = new ThemeReference();
93          themeReference.setName(THEME_NAME);
94  
95          Site site = mock(Site.class);
96          when(site.getTheme()).thenReturn(themeReference);
97  
98          Property filenameProperty = mock(Property.class);
99          when(filenameProperty.getString()).thenReturn(FILENAME);
100         Property extensionProperty = mock(Property.class);
101         when(extensionProperty.getString()).thenReturn(EXTENSION);
102 
103         binaryNode = mock(Node.class);
104         binaryProperty = mock(Property.class);
105         when(binaryNode.getSession()).thenReturn(session);
106         when(binaryNode.getPath()).thenReturn(PATH_TO_NODE);
107         when(binaryNode.hasProperty(File.PROPERTY_FILENAME)).thenReturn(true);
108         when(binaryNode.getProperty(File.PROPERTY_FILENAME)).thenReturn(filenameProperty);
109         when(binaryNode.hasProperty(File.EXTENSION)).thenReturn(true);
110         when(binaryNode.getProperty(File.EXTENSION)).thenReturn(extensionProperty);
111         when(binaryProperty.getSession()).thenReturn(session);
112         when(binaryProperty.getParent()).thenReturn(binaryNode);
113         when(siteManager.getAssignedSite(binaryNode)).thenReturn(site);
114     }
115 
116     @After
117     public void tearDown() {
118         MgnlContext.setInstance(null);
119     }
120 
121     @Test
122     public void createLink() {
123         // GIVEN
124         String variationName = "small";
125         SimpleResizeVariation variation = new SimpleResizeVariation(siteManager);
126         variation.setName(variationName);
127 
128         variationAwareImagingSupport.addVariation(variationName, variation);
129 
130         // WHEN
131         String link = variationAwareImagingSupport.createLink(binaryProperty, variationName);
132 
133         // THEN
134         assertThat(link, is(String.format("%s/.imaging/%s/%s/%s/%s%s/%s.%s", CONTEXT_PATH, variation.getGeneratorName(), THEME_NAME, variationName, WORKSPACE_NAME, PATH_TO_NODE, FILENAME, EXTENSION)));
135     }
136 
137     @Test
138     public void createLinkWhenVariationIsDiabled() {
139         // GIVEN
140         String variationName = "small";
141         SimpleResizeVariation variation = new SimpleResizeVariation(siteManager);
142         variation.setName(variationName);
143 
144         variationAwareImagingSupport.addVariation(variationName, variation);
145         variationAwareImagingSupport.setEnabled(false);
146 
147         // WHEN
148         String link = variationAwareImagingSupport.createLink(binaryProperty, variationName);
149 
150         // THEN
151         assertThat(link, is(String.format("%s/.imaging/default/%s%s.%s", CONTEXT_PATH, WORKSPACE_NAME, PATH_TO_NODE, EXTENSION)));
152     }
153 
154     @Test
155     public void createLinkWithOriginalVariation() {
156         // GIVEN
157 
158         // WHEN
159         String link = variationAwareImagingSupport.createLink(binaryProperty, ImagingSupport.VARIATION_ORIGINAL);
160 
161         // THEN
162         assertThat(link, is(String.format("%s/.imaging/default/%s%s.%s", CONTEXT_PATH, WORKSPACE_NAME, PATH_TO_NODE, EXTENSION)));
163     }
164 
165     @Test
166     public void createLinkWithoutExistingVariation() {
167         // GIVEN
168         String variationName = "small";
169 
170         // WHEN
171         String link = variationAwareImagingSupport.createLink(binaryProperty, variationName);
172 
173         // THEN
174         assertThat(link, is(String.format("%s/.imaging/default/%s%s.%s", CONTEXT_PATH, WORKSPACE_NAME, PATH_TO_NODE, EXTENSION)));
175     }
176 
177 }