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.support;
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.context.MgnlContext;
41 import info.magnolia.module.site.ConfiguredSite;
42 import info.magnolia.module.site.DefaultSiteManager;
43 import info.magnolia.module.site.SiteManager;
44 import info.magnolia.module.site.SiteModule;
45 import info.magnolia.module.site.theme.ConfiguredTheme;
46 import info.magnolia.module.site.theme.ThemeReference;
47 import info.magnolia.templating.functions.TemplatingFunctions;
48 import info.magnolia.templating.imaging.VariationAwareImagingSupport;
49 import info.magnolia.test.mock.MockWebContext;
50
51 import javax.inject.Provider;
52 import javax.jcr.Node;
53 import javax.jcr.Property;
54 import javax.jcr.Session;
55 import javax.jcr.Workspace;
56
57 import org.junit.Before;
58 import org.junit.Test;
59
60
61
62
63 public class ThemeDelegatingImagingSupportTest {
64
65 private SiteModule siteModule;
66 private SiteManager siteManager;
67 private ThemeDelegatingImagingSupport themeDelegatingImagingSupport;
68 private ConfiguredSite site;
69
70 @Before
71 public void setUp() {
72 final MockWebContext context = new MockWebContext();
73 context.setContextPath("ctx");
74 MgnlContext.setInstance(context);
75
76 siteModule = mock(SiteModule.class);
77 Provider<SiteModule> siteModuleProvider = new Provider<SiteModule>() {
78 @Override
79 public SiteModule get() {
80 return siteModule;
81 }
82 };
83 siteManager = new DefaultSiteManager(siteModuleProvider, mock(TemplatingFunctions.class));
84
85 themeDelegatingImagingSupport = new ThemeDelegatingImagingSupport(siteManager, siteModuleProvider);
86
87 site = new ConfiguredSite();
88 }
89
90 @Test
91 public void checkThatImagingSupportDelegatesToThemesImagingSupport() throws Exception {
92
93 final String variationName = "variationName";
94 final String themeName = "test";
95
96 final VariationAwareImagingSupport variationAwareImagingSupport = mock(VariationAwareImagingSupport.class);
97 final ConfiguredTheme theme = new ConfiguredTheme(variationAwareImagingSupport);
98 theme.setName(themeName);
99
100 final ThemeReference themeReference = new ThemeReference();
101 themeReference.setName(themeName);
102
103 site.setTheme(themeReference);
104
105 final Node node = mock(Node.class);
106 final Property property = mock(Property.class);
107 when(property.getParent()).thenReturn(node);
108
109 when(siteModule.getSite()).thenReturn(site);
110 when(siteModule.getTheme(anyString())).thenReturn(theme);
111
112
113 themeDelegatingImagingSupport.createLink(property);
114
115
116 verify(variationAwareImagingSupport, times(1)).createLink(property);
117
118
119 themeDelegatingImagingSupport.createLink(property, variationName);
120
121
122 verify(variationAwareImagingSupport, times(1)).createLink(property, variationName);
123 }
124
125 @Test
126 public void checkFallback() throws Exception {
127
128 final Session session = mock(Session.class);
129 final Workspace workspace = mock(Workspace.class);
130 final Node node = mock(Node.class);
131 final Property property = mock(Property.class);
132 when(property.getNode()).thenReturn(node);
133 when(property.getParent()).thenReturn(node);
134 when(node.getPath()).thenReturn("/path/to/binary/node");
135 when(property.getSession()).thenReturn(session);
136 when(session.getWorkspace()).thenReturn(workspace);
137 when(workspace.getName()).thenReturn("dummy-workspace");
138
139 when(siteModule.getSite()).thenReturn(site);
140
141
142 String link = themeDelegatingImagingSupport.createLink(property);
143
144
145 assertThat(link, is("ctx/.imaging/default/dummy-workspace/path/to/binary/node.jpg"));
146
147
148 String linkWithVariation = themeDelegatingImagingSupport.createLink(property, "variationName");
149
150
151 assertThat(linkWithVariation, is("ctx/.imaging/default/dummy-workspace/path/to/binary/node.jpg"));
152 }
153
154 }