View Javadoc
1   /**
2    * This file Copyright (c) 2014-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.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.v7.data.Container;
63  import com.vaadin.v7.data.Item;
64  import com.vaadin.v7.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      @Before
96      public void setUp() throws Exception {
97          this.ctx.addSession(GoogleSiteMapConfiguration.WORKSPACE, session);
98  
99          this.siteMapService = mock(SiteMapService.class);
100         this.subAppContext = mock(SubAppContext.class);
101 
102         final MockNode rootNode = (MockNode) this.session.getRootNode();
103         rootNode.addNode(existingSitemapNode);
104 
105         MgnlContext.setInstance(ctx);
106 
107         mockPageEntries();
108     }
109 
110     @After
111     public void tearDown() throws Exception {
112          MgnlContext.setInstance(null);
113     }
114 
115     @Test
116     public void testUrlFragmentToSitemapEntryConversion() throws Exception {
117         // GIVEN
118         initContentConnectorWithExistingSitemap(false);
119 
120         // WHEN
121         Object pageId = contentConnector.getItemIdByUrlFragment("/sitemap@/page");
122         Object subPageId = contentConnector.getItemIdByUrlFragment("/sitemap@/page/subPage");
123         Object subSubPageFragmentId = contentConnector.getItemIdByUrlFragment("/sitemap@/page/subPage/subSubPage");
124 
125         // THEN
126         assertThat(pageEntry, equalTo(pageId));
127         assertThat(subPageEntry, equalTo(subPageId));
128         assertThat(subSubPageEntry, equalTo(subSubPageFragmentId));
129     }
130 
131     @Test
132     public void tesSitemapEntryTotUrlFragmentConversion() throws Exception {
133         // GIVEN
134         initContentConnectorWithExistingSitemap(false);
135 
136         // WHEN
137         String pageFragment = contentConnector.getItemUrlFragment(pageEntry);
138         String subPageFragment = contentConnector.getItemUrlFragment(subPageEntry);
139         String subSubPageFragment = contentConnector.getItemUrlFragment(subSubPageEntry);
140 
141         // THEN
142         assertThat(pageFragment, equalTo("/sitemap@/page"));
143         assertThat(subPageFragment, equalTo("/sitemap@/page/subPage"));
144         assertThat(subSubPageFragment, equalTo("/sitemap@/page/subPage/subSubPage"));
145     }
146 
147     @Test
148     public void testSitemapContentConnectorHandlingCapabilities() throws Exception {
149         // GIVEN
150         initContentConnectorWithExistingSitemap(false);
151 
152         // WHEN
153         boolean isSupportsSitemapEntry = contentConnector.canHandleItem(new SiteMapEntry());
154         boolean isSupportsArbitraryObject = contentConnector.canHandleItem(new Object());
155 
156         // THEN
157         assertThat(isSupportsArbitraryObject, is(false));
158         assertThat(isSupportsSitemapEntry, is(true));
159     }
160 
161     @Test
162     public void testSitemapEntryToBeanItemConversion() throws Exception {
163         // GIVEN
164         initContentConnectorWithExistingSitemap(false);
165 
166         // WHEN
167         final Item vaadinItem = contentConnector.getItem(pageEntry);
168 
169         // THEN
170         assertThat(vaadinItem, instanceOf(BeanItem.class));
171         assertThat(pageEntry, equalTo(((BeanItem) vaadinItem).getBean()));
172     }
173 
174     @Test
175     public void testDoesNotFailAndEmptyForNonExistingSitemap() throws Exception {
176         // GIVEN
177         initContentConnectorWithNonExistingSitemap(false);
178 
179         // WHEN
180         final Container c = contentConnector.getContainer();
181 
182         // THEN
183         assertThat(c.size(), equalTo(0));
184     }
185 
186     @Test
187     public void testContainsVirtualUrisWhenConfiguredAccordingly() throws Exception {
188         // GIVEN
189         initContentConnectorWithExistingSitemap(true);
190 
191         // WHEN
192         Object pageId = contentConnector.getItemIdByUrlFragment("/sitemap@/page");
193         Container.Indexed c = contentConnector.getContainer();
194 
195         // THEN
196         assertThat(virtualUriEntry, equalTo(pageId));
197         assertThat(c.size(), equalTo(1));
198         assertThat(virtualUriEntry, equalTo(c.firstItemId()));
199     }
200 
201     private void mockPageEntries() throws RepositoryException {
202         MockNode page = getMockPage("page");
203         MockNode subPage = getMockPage("subPage");
204         MockNode subSubPage = getMockPage("subSubPage");
205 
206         page.addNode(subPage);
207         subPage.addNode(subSubPage);
208 
209         pageEntry = new SiteMapEntry(configuration, "/page", page, 0, "daily", 0.9);
210         subPageEntry = new SiteMapEntry(configuration, "/subPage", subPage, 0, "daily", 0.9);
211         subSubPageEntry = new SiteMapEntry(configuration, "/subSubPage", subSubPage, 0, "daily", 0.9);
212 
213         virtualUriEntry = new SiteMapEntry(configuration, "/page", page, 0, "daily", 0.9);
214         virtualUriEntry.setTo("/to");
215         virtualUriEntry.setFrom("/from");
216 
217         doAnswer(new Answer() {
218             @Override
219             public Object answer(InvocationOnMock invocation) throws Throwable {
220                 return Arrays.asList(
221                         pageEntry,
222                         subPageEntry,
223                         subSubPageEntry
224                 );
225             }
226         }).when(siteMapService).getSiteMapBeans(eq(existingSitemapNode), eq(false), eq(true));
227 
228         doAnswer(new Answer() {
229             @Override
230             public Object answer(InvocationOnMock invocation) throws Throwable {
231                 return Arrays.asList(virtualUriEntry);
232             }
233         }).when(siteMapService).getSiteMapBeans(eq(existingSitemapNode), eq(true), eq(true));
234     }
235 
236     private MockNode getMockPage(String name) throws RepositoryException {
237         MockNode page = new MockNode(name);
238         page.setProperty(NodeTypes.LastModified.NAME, GregorianCalendar.getInstance());
239         return page;
240     }
241 
242     private void initContentConnectorWithExistingSitemap(boolean isVirtualUris) {
243         doReturn(new DetailLocation("app", "subapp", "/sitemap")).when(subAppContext).getLocation();
244         doCreateSitemapContentConnector(isVirtualUris);
245     }
246 
247     private void initContentConnectorWithNonExistingSitemap(boolean isVirtualUris) {
248         doReturn(new DetailLocation("app", "subapp", "/not-a-sitemap")).when(subAppContext).getLocation();
249         doCreateSitemapContentConnector(isVirtualUris);
250     }
251 
252     private void doCreateSitemapContentConnector(boolean isVirtualUris) {
253         this.contentConnectorDefinition.setVirtualUris(isVirtualUris);
254         this.contentConnector = new SitemapContentConnector(contentConnectorDefinition, siteMapService, subAppContext);
255     }
256 
257 
258 }