Clover icon

Google Sitemap Module 2.4.3

  1. Project Clover database Thu May 11 2017 16:52:32 CEST
  2. Package info.magnolia.module.googlesitemap.app.subapp.sitemapdetail.contentconnector

File SitemapContentConnectorTest.java

 

Code metrics

0
64
15
1
258
149
15
0.23
4.27
15
1

Classes

Class Line # Actions
SitemapContentConnectorTest 69 64 0% 15 0
1.0100%
 

Contributing tests

This file is covered by 6 tests. .

Source view

1    /**
2    * This file Copyright (c) 2014-2017 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.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    * Tests for {@link SitemapContentConnector}.
68    */
 
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   
 
95  6 toggle @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   
 
110  6 toggle @After
111    public void tearDown() throws Exception {
112  6 MgnlContext.setInstance(null);
113    }
114   
 
115  1 toggle @Test
116    public void testUrlFragmentToSitemapEntryConversion() throws Exception {
117    // GIVEN
118  1 initContentConnectorWithExistingSitemap(false);
119   
120    // WHEN
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    // THEN
126  1 assertThat(pageEntry, equalTo(pageId));
127  1 assertThat(subPageEntry, equalTo(subPageId));
128  1 assertThat(subSubPageEntry, equalTo(subSubPageFragmentId));
129    }
130   
 
131  1 toggle @Test
132    public void tesSitemapEntryTotUrlFragmentConversion() throws Exception {
133    // GIVEN
134  1 initContentConnectorWithExistingSitemap(false);
135   
136    // WHEN
137  1 String pageFragment = contentConnector.getItemUrlFragment(pageEntry);
138  1 String subPageFragment = contentConnector.getItemUrlFragment(subPageEntry);
139  1 String subSubPageFragment = contentConnector.getItemUrlFragment(subSubPageEntry);
140   
141    // THEN
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   
 
147  1 toggle @Test
148    public void testSitemapContentConnectorHandlingCapabilities() throws Exception {
149    // GIVEN
150  1 initContentConnectorWithExistingSitemap(false);
151   
152    // WHEN
153  1 boolean isSupportsSitemapEntry = contentConnector.canHandleItem(new SiteMapEntry());
154  1 boolean isSupportsArbitraryObject = contentConnector.canHandleItem(new Object());
155   
156    // THEN
157  1 assertThat(isSupportsArbitraryObject, is(false));
158  1 assertThat(isSupportsSitemapEntry, is(true));
159    }
160   
 
161  1 toggle @Test
162    public void testSitemapEntryToBeanItemConversion() throws Exception {
163    // GIVEN
164  1 initContentConnectorWithExistingSitemap(false);
165   
166    // WHEN
167  1 final Item vaadinItem = contentConnector.getItem(pageEntry);
168   
169    // THEN
170  1 assertThat(vaadinItem, instanceOf(BeanItem.class));
171  1 assertThat(pageEntry, equalTo(((BeanItem) vaadinItem).getBean()));
172    }
173   
 
174  1 toggle @Test
175    public void testDoesNotFailAndEmptyForNonExistingSitemap() throws Exception {
176    // GIVEN
177  1 initContentConnectorWithNonExistingSitemap(false);
178   
179    // WHEN
180  1 final Container c = contentConnector.getContainer();
181   
182    // THEN
183  1 assertThat(c.size(), equalTo(0));
184    }
185   
 
186  1 toggle @Test
187    public void testContainsVirtualUrisWhenConfiguredAccordingly() throws Exception {
188    // GIVEN
189  1 initContentConnectorWithExistingSitemap(true);
190   
191    // WHEN
192  1 Object pageId = contentConnector.getItemIdByUrlFragment("/sitemap@/page");
193  1 Container.Indexed c = contentConnector.getContainer();
194   
195    // THEN
196  1 assertThat(virtualUriEntry, equalTo(pageId));
197  1 assertThat(c.size(), equalTo(1));
198  1 assertThat(virtualUriEntry, equalTo(c.firstItemId()));
199    }
200   
 
201  6 toggle 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() {
 
218  4 toggle @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() {
 
229  1 toggle @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   
 
236  18 toggle 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   
 
242  5 toggle private void initContentConnectorWithExistingSitemap(boolean isVirtualUris) {
243  5 doReturn(new DetailLocation("app", "subapp", "/sitemap")).when(subAppContext).getLocation();
244  5 doCreateSitemapContentConnector(isVirtualUris);
245    }
246   
 
247  1 toggle private void initContentConnectorWithNonExistingSitemap(boolean isVirtualUris) {
248  1 doReturn(new DetailLocation("app", "subapp", "/not-a-sitemap")).when(subAppContext).getLocation();
249  1 doCreateSitemapContentConnector(isVirtualUris);
250    }
251   
 
252  6 toggle private void doCreateSitemapContentConnector(boolean isVirtualUris) {
253  6 this.contentConnectorDefinition.setVirtualUris(isVirtualUris);
254  6 this.contentConnector = new SitemapContentConnector(contentConnectorDefinition, siteMapService, subAppContext);
255    }
256   
257   
258    }