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.module.googlesitemap.app.subapp.sitemapdetail.contentconnector; |
35 |
|
|
36 |
|
import static org.hamcrest.CoreMatchers.*; |
37 |
|
import static org.hamcrest.MatcherAssert.assertThat; |
38 |
|
import static org.mockito.Mockito.*; |
39 |
|
|
40 |
|
import info.magnolia.context.MgnlContext; |
41 |
|
import info.magnolia.jcr.util.NodeTypes; |
42 |
|
import info.magnolia.module.googlesitemap.GoogleSiteMapConfiguration; |
43 |
|
import info.magnolia.module.googlesitemap.bean.SiteMapEntry; |
44 |
|
import info.magnolia.module.googlesitemap.service.SiteMapService; |
45 |
|
import info.magnolia.test.mock.MockWebContext; |
46 |
|
import info.magnolia.test.mock.jcr.MockNode; |
47 |
|
import info.magnolia.test.mock.jcr.MockSession; |
48 |
|
import info.magnolia.ui.api.app.SubAppContext; |
49 |
|
import info.magnolia.ui.contentapp.detail.DetailLocation; |
50 |
|
|
51 |
|
import java.util.Arrays; |
52 |
|
import java.util.GregorianCalendar; |
53 |
|
|
54 |
|
import javax.jcr.RepositoryException; |
55 |
|
|
56 |
|
import org.junit.After; |
57 |
|
import org.junit.Before; |
58 |
|
import org.junit.Test; |
59 |
|
import org.mockito.invocation.InvocationOnMock; |
60 |
|
import org.mockito.stubbing.Answer; |
61 |
|
|
62 |
|
import com.vaadin.data.Container; |
63 |
|
import com.vaadin.data.Item; |
64 |
|
import com.vaadin.data.util.BeanItem; |
65 |
|
|
66 |
|
|
67 |
|
@link |
68 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (79) |
Complexity: 15 |
Complexity Density: 0.23 |
|
69 |
|
public class SitemapContentConnectorTest { |
70 |
|
|
71 |
|
private SitemapContentConnectorDefinition contentConnectorDefinition = new SitemapContentConnectorDefinition(); |
72 |
|
|
73 |
|
private SitemapContentConnector contentConnector; |
74 |
|
|
75 |
|
private SiteMapService siteMapService; |
76 |
|
|
77 |
|
private MockNode existingSitemapNode = new MockNode("sitemap"); |
78 |
|
|
79 |
|
private MockSession session = new MockSession(GoogleSiteMapConfiguration.WORKSPACE); |
80 |
|
|
81 |
|
private MockWebContext ctx = new MockWebContext(); |
82 |
|
|
83 |
|
private SubAppContext subAppContext; |
84 |
|
|
85 |
|
private GoogleSiteMapConfiguration configuration = new GoogleSiteMapConfiguration(); |
86 |
|
|
87 |
|
private SiteMapEntry pageEntry; |
88 |
|
|
89 |
|
private SiteMapEntry subPageEntry; |
90 |
|
|
91 |
|
private SiteMapEntry subSubPageEntry; |
92 |
|
|
93 |
|
private SiteMapEntry virtualUriEntry; |
94 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 1 |
Complexity Density: 0.14 |
|
95 |
6 |
@Before... |
96 |
|
public void setUp() throws Exception { |
97 |
6 |
this.ctx.addSession(GoogleSiteMapConfiguration.WORKSPACE, session); |
98 |
|
|
99 |
6 |
this.siteMapService = mock(SiteMapService.class); |
100 |
6 |
this.subAppContext = mock(SubAppContext.class); |
101 |
|
|
102 |
6 |
final MockNode rootNode = (MockNode) this.session.getRootNode(); |
103 |
6 |
rootNode.addNode(existingSitemapNode); |
104 |
|
|
105 |
6 |
MgnlContext.setInstance(ctx); |
106 |
|
|
107 |
6 |
mockPageEntries(); |
108 |
|
} |
109 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
110 |
6 |
@After... |
111 |
|
public void tearDown() throws Exception { |
112 |
6 |
MgnlContext.setInstance(null); |
113 |
|
} |
114 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 1 |
Complexity Density: 0.14 |
1PASS
|
|
115 |
1 |
@Test... |
116 |
|
public void testUrlFragmentToSitemapEntryConversion() throws Exception { |
117 |
|
|
118 |
1 |
initContentConnectorWithExistingSitemap(false); |
119 |
|
|
120 |
|
|
121 |
1 |
Object pageId = contentConnector.getItemIdByUrlFragment("/sitemap@/page"); |
122 |
1 |
Object subPageId = contentConnector.getItemIdByUrlFragment("/sitemap@/page/subPage"); |
123 |
1 |
Object subSubPageFragmentId = contentConnector.getItemIdByUrlFragment("/sitemap@/page/subPage/subSubPage"); |
124 |
|
|
125 |
|
|
126 |
1 |
assertThat(pageEntry, equalTo(pageId)); |
127 |
1 |
assertThat(subPageEntry, equalTo(subPageId)); |
128 |
1 |
assertThat(subSubPageEntry, equalTo(subSubPageFragmentId)); |
129 |
|
} |
130 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 1 |
Complexity Density: 0.14 |
1PASS
|
|
131 |
1 |
@Test... |
132 |
|
public void tesSitemapEntryTotUrlFragmentConversion() throws Exception { |
133 |
|
|
134 |
1 |
initContentConnectorWithExistingSitemap(false); |
135 |
|
|
136 |
|
|
137 |
1 |
String pageFragment = contentConnector.getItemUrlFragment(pageEntry); |
138 |
1 |
String subPageFragment = contentConnector.getItemUrlFragment(subPageEntry); |
139 |
1 |
String subSubPageFragment = contentConnector.getItemUrlFragment(subSubPageEntry); |
140 |
|
|
141 |
|
|
142 |
1 |
assertThat(pageFragment, equalTo("/sitemap@/page")); |
143 |
1 |
assertThat(subPageFragment, equalTo("/sitemap@/page/subPage")); |
144 |
1 |
assertThat(subSubPageFragment, equalTo("/sitemap@/page/subPage/subSubPage")); |
145 |
|
} |
146 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
1PASS
|
|
147 |
1 |
@Test... |
148 |
|
public void testSitemapContentConnectorHandlingCapabilities() throws Exception { |
149 |
|
|
150 |
1 |
initContentConnectorWithExistingSitemap(false); |
151 |
|
|
152 |
|
|
153 |
1 |
boolean isSupportsSitemapEntry = contentConnector.canHandleItem(new SiteMapEntry()); |
154 |
1 |
boolean isSupportsArbitraryObject = contentConnector.canHandleItem(new Object()); |
155 |
|
|
156 |
|
|
157 |
1 |
assertThat(isSupportsArbitraryObject, is(false)); |
158 |
1 |
assertThat(isSupportsSitemapEntry, is(true)); |
159 |
|
} |
160 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
1PASS
|
|
161 |
1 |
@Test... |
162 |
|
public void testSitemapEntryToBeanItemConversion() throws Exception { |
163 |
|
|
164 |
1 |
initContentConnectorWithExistingSitemap(false); |
165 |
|
|
166 |
|
|
167 |
1 |
final Item vaadinItem = contentConnector.getItem(pageEntry); |
168 |
|
|
169 |
|
|
170 |
1 |
assertThat(vaadinItem, instanceOf(BeanItem.class)); |
171 |
1 |
assertThat(pageEntry, equalTo(((BeanItem) vaadinItem).getBean())); |
172 |
|
} |
173 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
1PASS
|
|
174 |
1 |
@Test... |
175 |
|
public void testDoesNotFailAndEmptyForNonExistingSitemap() throws Exception { |
176 |
|
|
177 |
1 |
initContentConnectorWithNonExistingSitemap(false); |
178 |
|
|
179 |
|
|
180 |
1 |
final Container c = contentConnector.getContainer(); |
181 |
|
|
182 |
|
|
183 |
1 |
assertThat(c.size(), equalTo(0)); |
184 |
|
} |
185 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 1 |
Complexity Density: 0.17 |
1PASS
|
|
186 |
1 |
@Test... |
187 |
|
public void testContainsVirtualUrisWhenConfiguredAccordingly() throws Exception { |
188 |
|
|
189 |
1 |
initContentConnectorWithExistingSitemap(true); |
190 |
|
|
191 |
|
|
192 |
1 |
Object pageId = contentConnector.getItemIdByUrlFragment("/sitemap@/page"); |
193 |
1 |
Container.Indexed c = contentConnector.getContainer(); |
194 |
|
|
195 |
|
|
196 |
1 |
assertThat(virtualUriEntry, equalTo(pageId)); |
197 |
1 |
assertThat(c.size(), equalTo(1)); |
198 |
1 |
assertThat(virtualUriEntry, equalTo(c.firstItemId())); |
199 |
|
} |
200 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (13) |
Complexity: 1 |
Complexity Density: 0.08 |
|
201 |
6 |
private void mockPageEntries() throws RepositoryException {... |
202 |
6 |
MockNode page = getMockPage("page"); |
203 |
6 |
MockNode subPage = getMockPage("subPage"); |
204 |
6 |
MockNode subSubPage = getMockPage("subSubPage"); |
205 |
|
|
206 |
6 |
page.addNode(subPage); |
207 |
6 |
subPage.addNode(subSubPage); |
208 |
|
|
209 |
6 |
pageEntry = new SiteMapEntry(configuration, "/page", page, 0, "daily", 0.9); |
210 |
6 |
subPageEntry = new SiteMapEntry(configuration, "/subPage", subPage, 0, "daily", 0.9); |
211 |
6 |
subSubPageEntry = new SiteMapEntry(configuration, "/subSubPage", subSubPage, 0, "daily", 0.9); |
212 |
|
|
213 |
6 |
virtualUriEntry = new SiteMapEntry(configuration, "/page", page, 0, "daily", 0.9); |
214 |
6 |
virtualUriEntry.setTo("/to"); |
215 |
6 |
virtualUriEntry.setFrom("/from"); |
216 |
|
|
217 |
6 |
doAnswer(new Answer() { |
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
218 |
4 |
@Override... |
219 |
|
public Object answer(InvocationOnMock invocation) throws Throwable { |
220 |
4 |
return Arrays.asList( |
221 |
|
pageEntry, |
222 |
|
subPageEntry, |
223 |
|
subSubPageEntry |
224 |
|
); |
225 |
|
} |
226 |
|
}).when(siteMapService).getSiteMapBeans(eq(existingSitemapNode), eq(false), eq(true)); |
227 |
|
|
228 |
6 |
doAnswer(new Answer() { |
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
229 |
1 |
@Override... |
230 |
|
public Object answer(InvocationOnMock invocation) throws Throwable { |
231 |
1 |
return Arrays.asList(virtualUriEntry); |
232 |
|
} |
233 |
|
}).when(siteMapService).getSiteMapBeans(eq(existingSitemapNode), eq(true), eq(true)); |
234 |
|
} |
235 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
236 |
18 |
private MockNode getMockPage(String name) throws RepositoryException {... |
237 |
18 |
MockNode page = new MockNode(name); |
238 |
18 |
page.setProperty(NodeTypes.LastModified.NAME, GregorianCalendar.getInstance()); |
239 |
18 |
return page; |
240 |
|
} |
241 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
242 |
5 |
private void initContentConnectorWithExistingSitemap(boolean isVirtualUris) {... |
243 |
5 |
doReturn(new DetailLocation("app", "subapp", "/sitemap")).when(subAppContext).getLocation(); |
244 |
5 |
doCreateSitemapContentConnector(isVirtualUris); |
245 |
|
} |
246 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
247 |
1 |
private void initContentConnectorWithNonExistingSitemap(boolean isVirtualUris) {... |
248 |
1 |
doReturn(new DetailLocation("app", "subapp", "/not-a-sitemap")).when(subAppContext).getLocation(); |
249 |
1 |
doCreateSitemapContentConnector(isVirtualUris); |
250 |
|
} |
251 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
252 |
6 |
private void doCreateSitemapContentConnector(boolean isVirtualUris) {... |
253 |
6 |
this.contentConnectorDefinition.setVirtualUris(isVirtualUris); |
254 |
6 |
this.contentConnector = new SitemapContentConnector(contentConnectorDefinition, siteMapService, subAppContext); |
255 |
|
} |
256 |
|
|
257 |
|
|
258 |
|
} |