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.parameters;
35  
36  import static org.junit.Assert.assertEquals;
37  import static org.mockito.Matchers.*;
38  import static org.mockito.Mockito.*;
39  
40  import info.magnolia.context.Context;
41  import info.magnolia.context.MgnlContext;
42  import info.magnolia.jcr.util.VersionUtil;
43  import info.magnolia.objectfactory.ComponentProvider;
44  import info.magnolia.test.ComponentsTestUtil;
45  import info.magnolia.test.mock.MockComponentProvider;
46  
47  import javax.jcr.Node;
48  import javax.jcr.RepositoryException;
49  import javax.jcr.Session;
50  import javax.jcr.Workspace;
51  import javax.jcr.version.Version;
52  
53  import org.junit.After;
54  import org.junit.Before;
55  import org.junit.Test;
56  import org.junit.runner.RunWith;
57  import org.powermock.api.mockito.PowerMockito;
58  import org.powermock.core.classloader.annotations.PrepareForTest;
59  import org.powermock.modules.junit4.PowerMockRunner;
60  
61  /**
62   * Tests for {@link info.magnolia.templating.imaging.parameters.ThemeAwareParameterProvider}.
63   */
64  @RunWith(PowerMockRunner.class)
65  @PrepareForTest(VersionUtil.class)
66  public class ThemeAwareParameterProviderTest {
67  
68      private final String id = "1be12547-ad82-4c83-8396-213466ceb003";
69      private final String version = "1.2";
70      private final String path = "/demo-project/content/01/teaserImgBinary";
71  
72      @Before
73      public void setUp() throws RepositoryException {
74          ComponentsTestUtil.setInstance(ComponentProvider.class, new MockComponentProvider());
75  
76          Version versionedNode = mock(Version.class);
77          Node damNode = mock(Node.class);
78  
79          Session mgnlVersionSession = mock(Session.class);
80  
81          Session damSession = mock(Session.class);
82          when(damSession.getNode("/content/01/teaserImgBinary")).thenReturn(damNode);
83  
84          when(versionedNode.getSession()).thenReturn(mgnlVersionSession);
85          when(versionedNode.getNode(anyString())).thenReturn(versionedNode);
86          when(versionedNode.getPath()).thenReturn(path);
87  
88          when(damNode.getSession()).thenReturn(damSession);
89          when(damNode.getPath()).thenReturn(path);
90  
91          Workspace mgnlVersionWorkspace = mock(Workspace.class);
92          when(mgnlVersionWorkspace.getName()).thenReturn("mgnlVersion");
93          when(mgnlVersionSession.getWorkspace()).thenReturn(mgnlVersionWorkspace);
94  
95          Workspace damWorkspace = mock(Workspace.class);
96          when(damWorkspace.getName()).thenReturn("dam");
97          when(damSession.getWorkspace()).thenReturn(damWorkspace);
98  
99          PowerMockito.mockStatic(VersionUtil.class);
100         when(VersionUtil.getVersion(any(Node.class), eq(version))).thenReturn(versionedNode);
101 
102         Context ctx = mock(Context.class);
103         when(ctx.getJCRSession("mgnlVersion")).thenReturn(mgnlVersionSession);
104         when(ctx.getJCRSession("dam")).thenReturn(damSession);
105         MgnlContext.setInstance(ctx);
106     }
107 
108     @After
109     public void tearDown() {
110         ComponentsTestUtil.clear();
111         MgnlContext.setInstance(null);
112     }
113 
114     @Test
115     public void versionedNode() throws RepositoryException {
116         // GIVEN
117         final String uri = "/pop/teaser-float3/mgnlVersion/jcr:system/jcr:versionStorage/1b/e1/25/" + id + "/" + version +
118                 "/jcr:frozenNode/content/01/teaserImgBinary/index.jpeg";
119 
120         // WHEN
121         ThemeAwareParameterProvider provider = new ThemeAwareParameterProvider(uri);
122 
123         // THEN
124         final String path = provider.getParameter().getCachePath();
125         assertEquals("/pop/teaser-float3/mgnlVersion/demo-project/content/01/teaserImgBinary", path);
126     }
127 
128     @Test
129     public void notVersionedNode() throws RepositoryException {
130         // GIVEN
131         final String uri = "/pop/teaser-float3/dam/content/01/teaserImgBinary/index.jpeg";
132 
133         // WHEN
134         ThemeAwareParameterProvider provider = new ThemeAwareParameterProvider(uri);
135 
136         // THEN
137         final String path = provider.getParameter().getCachePath();
138         assertEquals("/pop/teaser-float3/dam/demo-project/content/01/teaserImgBinary", path);
139     }
140 }