1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
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
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
121 ThemeAwareParameterProvider provider = new ThemeAwareParameterProvider(uri);
122
123
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
131 final String uri = "/pop/teaser-float3/dam/content/01/teaserImgBinary/index.jpeg";
132
133
134 ThemeAwareParameterProvider provider = new ThemeAwareParameterProvider(uri);
135
136
137 final String path = provider.getParameter().getCachePath();
138 assertEquals("/pop/teaser-float3/dam/demo-project/content/01/teaserImgBinary", path);
139 }
140 }