View Javadoc

1   /**
2    * This file Copyright (c) 2012 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.model;
35  
36  import static org.junit.Assert.*;
37  import static org.mockito.Mockito.mock;
38  
39  import info.magnolia.jcr.util.NodeTypes;
40  import info.magnolia.module.googlesitemap.GoogleSiteMapConfiguration;
41  import info.magnolia.module.googlesitemap.SiteMapTestUtil;
42  import info.magnolia.module.googlesitemap.bean.SiteMapEntry;
43  import info.magnolia.module.googlesitemap.service.SiteMapXMLUtil;
44  import info.magnolia.rendering.model.RenderingModel;
45  import info.magnolia.rendering.template.RenderableDefinition;
46  
47  import java.util.Iterator;
48  import java.util.List;
49  
50  import javax.jcr.Node;
51  import javax.jcr.RepositoryException;
52  
53  import org.junit.After;
54  import org.junit.Before;
55  import org.junit.Test;
56  
57  
58  /**
59   * Main SiteMapModel Test class.
60   * @version $Id$
61   *
62   */
63  public class SiteMapModelTest extends SiteMapTestUtil {
64  
65      private SiteMapModel<RenderableDefinition> model;
66      private String SITEMAPNODE = "siteMapNode";
67      private Node siteMapNode;
68  
69      @Override
70      @Before
71      public void setUp() throws RepositoryException{
72          super.setUp();
73          initService();
74  
75          siteMapNode = rootWebsite.addNode(SITEMAPNODE, GoogleSiteMapConfiguration.NODE_TYPE);
76          setMetaData(siteMapNode, "google-sitemap:pages/siteMapsConfiguration");
77  
78          initModel(siteMapNode) ;
79  
80      }
81  
82  
83      @Override
84      @After
85      public void tearDown() {
86          super.tearDown();
87          model = null;
88      }
89  
90  
91      private void initModel(Node content) {
92          RenderingModel<RenderableDefinition> parent = null;
93          RenderableDefinition definition = null;
94          model = new SiteMapModel<RenderableDefinition>(content, definition, parent, service, mock(SiteMapXMLUtil.class));
95      }
96  
97  
98      @Test
99      public void testGetSiteMapBeans_NoSite_NoVirtualUri() throws RepositoryException {
100         // GIVEN
101 
102         // WHEN
103         Iterator<SiteMapEntry> res =  model.getSiteMapBeans();
104 
105         // THEN
106         assertNotNull(res);
107         assertEquals("res should have no result", false, res.hasNext());
108 
109     }
110 
111     @Test
112     public void testGetSiteMapBeans_OneSite_NoVirtualUri() throws RepositoryException {
113         // GIVEN
114         Node site = siteMapNode.addNode("sites", NodeTypes.Content.NAME);
115         site.setProperty("0", rootPageWebsite.getPath());
116 
117         setMetaData(page_1_1, null);
118         setMetaData(page_1_1_1, null);
119 
120         // WHEN
121         Iterator<SiteMapEntry> res =  model.getSiteMapBeans();
122 
123         // THEN
124         assertNotNull(res);
125         List<SiteMapEntry> listRes = fromIteratorToList(res);
126         assertEquals("res should have 2 result", 2, listRes.size());
127         assertEquals("",DEFAULT_URL+page_1_1_1.getPath(), listRes.get(0).getLoc());
128         assertEquals("",DEFAULT_URL+page_1_1.getPath(), listRes.get(1).getLoc());
129 
130 
131     }
132 
133     @Test
134     public void testGetSiteMapBeans_TwoSite_NoVirtualUri_NoDuplicates() throws RepositoryException {
135         // GIVEN
136         Node site = siteMapNode.addNode("sites", NodeTypes.Content.NAME);
137         site.setProperty("0", page_1_1.getPath());
138         site.setProperty("1", page_1_2.getPath());
139 
140         setMetaData(page_1_1, null);
141         setMetaData(page_1_1_1, null);
142         setMetaData(page_1_2, null);
143         setMetaData(page_1_2_1, null);
144         page_1_2_1.setPrimaryType(NodeTypes.Content.NAME);
145 
146         // WHEN
147         Iterator<SiteMapEntry> res =  model.getSiteMapBeans();
148 
149         // THEN
150         assertNotNull(res);
151         List<SiteMapEntry> listRes = fromIteratorToList(res);
152         assertEquals("res should have 2 result", 2, listRes.size());
153         assertEquals("",DEFAULT_URL+page_1_2_1.getPath(), listRes.get(0).getLoc());
154         assertEquals("",DEFAULT_URL+page_1_1_1.getPath(), listRes.get(1).getLoc());
155     }
156 
157     @Test
158     public void testGetSiteMapBeans_TwoSite_NoVirtualUri_Duplicates_SameNode() throws RepositoryException {
159         // GIVEN
160         // Create a site node (containing the selected sites)
161         Node site = siteMapNode.addNode("sites", NodeTypes.Content.NAME);
162         site.setProperty("0", page_1_1.getPath());
163         site.setProperty("1", page_1_2.getPath());
164         //Duplicates
165         site.setProperty("2", page_1_1.getPath());
166         site.setProperty("3", page_1_2.getPath());
167 
168         setMetaData(page_1_1, null);
169         setMetaData(page_1_1_1, null);
170         setMetaData(page_1_2, null);
171         setMetaData(page_1_2_1, null);
172         page_1_2_1.setPrimaryType(NodeTypes.Content.NAME);
173 
174         // WHEN
175         Iterator<SiteMapEntry> res =  model.getSiteMapBeans();
176 
177         // THEN
178         assertNotNull(res);
179         List<SiteMapEntry> listRes = fromIteratorToList(res);
180         assertEquals("res should have 2 result", 2, listRes.size());
181         assertEquals("",DEFAULT_URL+page_1_2_1.getPath(), listRes.get(0).getLoc());
182         assertEquals("",DEFAULT_URL+page_1_1_1.getPath(), listRes.get(1).getLoc());
183     }
184 
185     @Test
186     public void testGetSiteMapBeans_TwoSite_NoVirtualUri_Duplicates_TwoNode() throws RepositoryException {
187         // GIVEN
188         Node site = siteMapNode.addNode("sites", NodeTypes.Content.NAME);
189         site.setProperty("0", page_1_1.getPath());
190         site.setProperty("1", page_1_2.getPath());
191 
192         //Duplicates
193         site = siteMapNode.addNode("sites", NodeTypes.Content.NAME);
194         site.setProperty("0", page_1_1.getPath());
195         site.setProperty("1", page_1_2.getPath());
196 
197 
198         setMetaData(page_1_1, null);
199         setMetaData(page_1_1_1, null);
200         setMetaData(page_1_2, null);
201         setMetaData(page_1_2_1, null);
202         page_1_2_1.setPrimaryType(NodeTypes.Content.NAME);
203 
204         // WHEN
205         Iterator<SiteMapEntry> res =  model.getSiteMapBeans();
206 
207         // THEN
208         assertNotNull(res);
209         List<SiteMapEntry> listRes = fromIteratorToList(res);
210         assertEquals("res should have 2 result", 2, listRes.size());
211         assertEquals("",DEFAULT_URL+page_1_2_1.getPath(), listRes.get(0).getLoc());
212         assertEquals("",DEFAULT_URL+page_1_1_1.getPath(), listRes.get(1).getLoc());
213     }
214 
215 
216     @Test
217     public void testGetSiteMapBeans_NoSite_VirtualUri() throws RepositoryException {
218         //WHEN
219         siteMapNode.setProperty(GoogleSiteMapConfiguration.INCLUDE_VIRTUAL_URI_MAPPINGS_PROPERTIES, true);
220 
221         //THEN
222         // Create root content node
223         Node content = siteMapNode.addNode("content", NodeTypes.Area.NAME);
224         // Create a virtual node conponent
225         initVirtualUriComponentNode(content, "00");
226         setMetaData(virtualUri_2_1, null);
227         virtualUri_2_1.setProperty("fromURI", "/fromURI*");
228         virtualUri_2_1.setProperty("toURI", "toURI");
229         setMetaData(virtualUri_2_2, null);
230         virtualUri_2_2.setProperty("fromURI", "/fromURI22*");
231         virtualUri_2_2.setProperty("toURI", "toURI22");
232 
233         // WHEN
234         Iterator<SiteMapEntry> res =  model.getSiteMapBeans();
235 
236         // THEN
237         assertNotNull(res);
238         List<SiteMapEntry> listRes = fromIteratorToList(res);
239         assertEquals("res should have 2 result", 2, listRes.size());
240         assertEquals("",CONTEXT_PATH+"/fromURI22*", listRes.get(0).getLoc());
241         assertEquals("",CONTEXT_PATH+"/fromURI*", listRes.get(1).getLoc());
242     }
243 
244     @Test
245     public void testGetSiteMapBeans_TwoSite_VirtualUri() throws RepositoryException {
246         // WHEN
247         siteMapNode.setProperty(GoogleSiteMapConfiguration.INCLUDE_VIRTUAL_URI_MAPPINGS_PROPERTIES, true);
248 
249         //THEN
250         // Create root content node
251         Node content = siteMapNode.addNode("content", NodeTypes.Area.NAME);
252         // Create a site node (containing the selected sites)
253         Node siteDef = initSiteComponentNode(content, "0");
254         Node site = siteDef.addNode("sites", NodeTypes.Content.NAME);
255         site.setProperty("0", page_1_1.getPath());
256         site.setProperty("1", page_1_2.getPath());
257         setMetaData(page_1_1, null);
258         setMetaData(page_1_1_1, null);
259         setMetaData(page_1_2, null);
260         setMetaData(page_1_2_1, null);
261         page_1_2_1.setPrimaryType(NodeTypes.Content.NAME);
262 
263         // Create a virtual node conponent
264         initVirtualUriComponentNode(content, "00");
265         setMetaData(virtualUri_2_1, null);
266         virtualUri_2_1.setProperty("fromURI", "/fromURI*");
267         virtualUri_2_1.setProperty("toURI", "toURI");
268         setMetaData(virtualUri_2_2, null);
269         virtualUri_2_2.setProperty("fromURI", "/fromURI22*");
270         virtualUri_2_2.setProperty("toURI", "toURI22");
271 
272         // WHEN
273         Iterator<SiteMapEntry> res =  model.getSiteMapBeans();
274 
275         // THEN
276         assertNotNull(res);
277         List<SiteMapEntry> listRes = fromIteratorToList(res);
278         assertEquals("res should have 4 result", 4, listRes.size());
279         assertEquals("",DEFAULT_URL+page_1_2_1.getPath(), listRes.get(0).getLoc());
280         assertEquals("",DEFAULT_URL+page_1_1_1.getPath(), listRes.get(1).getLoc());
281         assertEquals("",CONTEXT_PATH+"/fromURI22*", listRes.get(2).getLoc());
282         assertEquals("",CONTEXT_PATH+"/fromURI*", listRes.get(3).getLoc());
283     }
284 }