Clover icon

Magnolia Imaging Module 3.2.3

  1. Project Clover database Fri Nov 6 2015 14:54:03 CET
  2. Package info.magnolia.imaging.functions

File ImagingTemplatingFunctionsTest.java

 

Code metrics

0
17
3
1
241
125
3
0.18
5.67
3
1
73% of code in this file is excluded from these metrics.

Classes

Class Line # Actions
ImagingTemplatingFunctionsTest 63 17 73% 3 0
1.0100%
 

Contributing tests

This file is covered by 11 tests. .

Source view

1    /**
2    * This file Copyright (c) 2015 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.imaging.functions;
35   
36    import static org.hamcrest.CoreMatchers.*;
37    import static org.junit.Assert.assertThat;
38    import static org.mockito.Matchers.any;
39    import static org.mockito.Mockito.*;
40   
41    import info.magnolia.context.MgnlContext;
42    import info.magnolia.imaging.DefaultImagingSupport;
43    import info.magnolia.imaging.ImagingSupport;
44    import info.magnolia.jcr.util.ContentMap;
45    import info.magnolia.rendering.template.TemplateAvailability;
46    import info.magnolia.rendering.template.configured.ConfiguredTemplateDefinition;
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.apache.jackrabbit.JcrConstants;
56    import org.junit.After;
57    import org.junit.Before;
58    import org.junit.Test;
59   
60    /**
61    * Tests for {@link info.magnolia.imaging.functions.ImagingTemplatingFunctions}.
62    */
 
63    public class ImagingTemplatingFunctionsTest {
64   
65    private MockWebContext context;
66    private ImagingTemplatingFunctions imagingTemplatingFunctions;
67   
 
68  11 toggle @Before
69    public void setUp() {
70  11 context = new MockWebContext();
71  11 context.setContextPath("/ctx");
72  11 MgnlContext.setInstance(context);
73   
74  11 ImagingSupport imagingSupport = new DefaultImagingSupport();
75  11 imagingTemplatingFunctions = new ImagingTemplatingFunctions(imagingSupport);
76    }
77   
 
78  11 toggle @After
79    public void tearDown() {
80  11 MgnlContext.setInstance(null);
81    }
82   
 
83    toggle @Test
84    public void getImageVariationLinkFromBinaryWhenNull() {
85    // GIVEN
86    Node node = null;
87    String variation = null;
88    // WHEN
89    String link = imagingTemplatingFunctions.getImageVariationLinkFromBinary(node, variation);
90    // THEN
91    assertThat(link, nullValue());
92    }
93   
 
94    toggle @Test
95    public void getImageVariationLinkFromBinary() throws RepositoryException {
96    // GIVEN
97   
98    // WHEN
99    String link = imagingTemplatingFunctions.getImageVariationLinkFromBinary(mockNode(), ImagingSupport.VARIATION_ORIGINAL);
100   
101    // THEN
102    assertThat(link, is("/ctx/.imaging/default/workspace/path/to/this/parent/node.jpg"));
103    }
104   
 
105    toggle @Test
106    public void getImageVariationLinkFromBinaryContentMap() throws RepositoryException {
107    // GIVEN
108   
109    // WHEN
110    String link = imagingTemplatingFunctions.getImageVariationLinkFromBinary(new ContentMap(mockNode()), ImagingSupport.VARIATION_ORIGINAL);
111   
112    // THEN
113    assertThat(link, is("/ctx/.imaging/default/workspace/path/to/this/parent/node.jpg"));
114    }
115   
 
116    toggle @Test
117    public void getImageVariationLinkFromBinaryContentMapWhenNull() throws RepositoryException {
118    // GIVEN
119    final ContentMap contentMap = null;
120   
121    // WHEN
122    String link = imagingTemplatingFunctions.getImageVariationLinkFromBinary(contentMap, ImagingSupport.VARIATION_ORIGINAL);
123   
124    // THEN
125    assertThat(link, nullValue());
126    }
127   
 
128    toggle @Test
129    public void getImageVariationLinkFromBinaryAndTemplateDefinition() throws RepositoryException {
130    // GIVEN
131    TemplateAvailability templateAvailability = mock(TemplateAvailability.class);
132    ConfiguredTemplateDefinition templateDefinition = new ConfiguredTemplateDefinition(templateAvailability);
133    templateDefinition.addParameter(ImagingTemplatingFunctions.PROPERTY_IMAGE_VARIATION, ImagingSupport.VARIATION_ORIGINAL);
134   
135    // WHEN
136    String link = imagingTemplatingFunctions.getImageVariationLinkFromBinary(mockNode(), templateDefinition);
137   
138    // THEN
139    assertThat(link, is("/ctx/.imaging/default/workspace/path/to/this/parent/node.jpg"));
140    }
141   
 
142    toggle @Test
143    public void getImageVariationLinkFromBinaryAndTemplateDefinitionContentMap() throws RepositoryException {
144    // GIVEN
145    TemplateAvailability templateAvailability = mock(TemplateAvailability.class);
146    ConfiguredTemplateDefinition templateDefinition = new ConfiguredTemplateDefinition(templateAvailability);
147    templateDefinition.addParameter(ImagingTemplatingFunctions.PROPERTY_IMAGE_VARIATION, ImagingSupport.VARIATION_ORIGINAL);
148   
149    // WHEN
150    String link = imagingTemplatingFunctions.getImageVariationLinkFromBinary(new ContentMap(mockNode()), templateDefinition);
151   
152    // THEN
153    assertThat(link, is("/ctx/.imaging/default/workspace/path/to/this/parent/node.jpg"));
154    }
155   
 
156    toggle @Test
157    public void getImageVariationLinkFromBinaryAndTemplateDefinitionContentMapWhenNull() throws RepositoryException {
158    // GIVEN
159    ContentMap contentMap = null;
160    ConfiguredTemplateDefinition templateDefinition = null;
161   
162    // WHEN
163    String link = imagingTemplatingFunctions.getImageVariationLinkFromBinary(contentMap, templateDefinition);
164   
165    // THEN
166    assertThat(link, nullValue());
167    }
168   
 
169    toggle @Test
170    public void getImageVariationLinkFromBinaryAndTemplateDefinitionWithoutParameter() throws RepositoryException {
171    // GIVEN
172    TemplateAvailability templateAvailability = mock(TemplateAvailability.class);
173    ConfiguredTemplateDefinition templateDefinition = new ConfiguredTemplateDefinition(templateAvailability);
174   
175    // WHEN
176    String link = imagingTemplatingFunctions.getImageVariationLinkFromBinary(mockNode(), templateDefinition);
177   
178    // THEN
179    assertThat(link, is("/ctx/.imaging/default/workspace/path/to/this/parent/node.jpg"));
180    }
181   
 
182    toggle @Test
183    public void getImageVariationLinkFromBinaryAndTemplateDefinitionWhenNull() throws RepositoryException {
184    // GIVEN
185    ConfiguredTemplateDefinition templateDefinition = null;
186   
187    // WHEN
188    String link = imagingTemplatingFunctions.getImageVariationLinkFromBinary(mockNode(), templateDefinition);
189   
190    // THEN
191    assertThat(link, is("/ctx/.imaging/default/workspace/path/to/this/parent/node.jpg"));
192    }
193   
 
194    toggle @Test
195    public void getImageVariationLinkFromBinaryWhenException() throws RepositoryException {
196    // GIVEN
197    Node node = mock(Node.class);
198    when(node.getProperty(anyString())).thenThrow(new RepositoryException("This exception is thrown on purpose"));
199   
200    // WHEN
201    String encodedLink = imagingTemplatingFunctions.getImageVariationLinkFromBinary(node, ImagingSupport.VARIATION_ORIGINAL);
202   
203    // THEN
204    assertThat(encodedLink, nullValue());
205    }
206   
 
207    toggle @Test
208    public void getImageVariationLinkFromBinaryEscapedText() {
209    // GIVEN
210    String unEncodedString = "this string is not encoded";
211    Node node = mock(Node.class);
212   
213    ImagingSupport imagingSupport = mock(ImagingSupport.class);
214    ImagingTemplatingFunctions imagingTemplatingFunctions = new ImagingTemplatingFunctions(imagingSupport);
215   
216    when(imagingSupport.createLink(any(Property.class), anyString())).thenReturn(unEncodedString);
217   
218    // WHEN
219    String encodedLink = imagingTemplatingFunctions.getImageVariationLinkFromBinary(node, ImagingSupport.VARIATION_ORIGINAL);
220   
221    // THEN
222    assertThat(encodedLink, is("this%20string%20is%20not%20encoded"));
223    }
224   
 
225  6 toggle private Node mockNode() throws RepositoryException {
226  6 Session session = mock(Session.class);
227  6 Workspace workspace = mock(Workspace.class);
228  6 Node node = mock(Node.class);
229  6 Property binaryProperty = mock(Property.class);
230   
231  6 when(session.getWorkspace()).thenReturn(workspace);
232  6 when(workspace.getName()).thenReturn("workspace");
233  6 when(node.getProperty(JcrConstants.JCR_DATA)).thenReturn(binaryProperty);
234  6 when(node.getPath()).thenReturn("/path/to/this/parent/node");
235  6 when(binaryProperty.getParent()).thenReturn(node);
236  6 when(binaryProperty.getSession()).thenReturn(session);
237   
238  6 return node;
239    }
240   
241    }