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