View Javadoc
1   /**
2    * This file Copyright (c) 2014-2016 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.service;
35  
36  import static org.junit.Assert.*;
37  
38  import info.magnolia.module.googlesitemap.SiteMapNodeTypes;
39  import info.magnolia.module.googlesitemap.bean.SiteMapEntry;
40  
41  import java.util.Collections;
42  import java.util.List;
43  
44  import javax.jcr.Node;
45  import javax.jcr.RepositoryException;
46  
47  import org.apache.commons.collections4.IteratorUtils;
48  import org.junit.Test;
49  
50  /**
51   * Main test class for {@link SiteMapService}.
52   */
53  public class SiteMapServiceTest extends ServiceTestUtil {
54  
55      @Test
56      public void testGetSiteMapBeansForPagesNotForEdit() throws RepositoryException {
57          // GIVEN
58          Node siteMapNode = googleSiteMapSession.getNode("/test1");
59          // WHEN
60          List<SiteMapEntry> res = service.getSiteMapBeans(siteMapNode, false, false);
61  
62          // THEN
63          assertNotNull(res);
64          // check amount of res
65          assertEquals(2, res.size());
66  
67          SiteMapEntry entry0 = res.get(0);
68          assertEquals("always", entry0.getChangefreq());
69          assertEquals("2014-01-08", entry0.getLastmod());
70          assertEquals("defaultBaseUrl/demo-site/article/article1", entry0.getLoc());
71          assertEquals("/demo-site/article/article1", entry0.getPath());
72          assertEquals(0.3d, entry0.getPriority(), 0);
73          assertEquals(1, entry0.getLevel());
74  
75          SiteMapEntry entry1 = res.get(1);
76          assertEquals("defaultBaseUrl/demo-site/article/articleSection", entry1.getLoc());
77          assertEquals("/demo-site/article/articleSection", entry1.getPath());
78  
79      }
80  
81      @Test
82      public void testGetSiteMapBeansForPagesForEdit() throws RepositoryException {
83          // GIVEN
84          Node siteMapNode = googleSiteMapSession.getNode("/test1");
85          // WHEN
86          List<SiteMapEntry> res = service.getSiteMapBeans(siteMapNode, false, true);
87  
88          // THEN
89          assertNotNull(res);
90          assertEquals(4, res.size());
91  
92          SiteMapEntry entry0 = res.get(0);
93          assertEquals("always", entry0.getChangefreq());
94          assertEquals("2014-01-08", entry0.getLastmod());
95          assertEquals("defaultBaseUrl/demo-site/article/article1", entry0.getLoc());
96          assertEquals("/demo-site/article/article1", entry0.getPath());
97          assertEquals(0.3d, entry0.getPriority(), 0);
98          assertEquals(1, entry0.getLevel());
99          assertFalse(entry0.isStyleAlert());
100 
101         SiteMapEntry entry1 = res.get(1); // Second article is hidden
102         assertEquals("defaultBaseUrl/demo-site/article/article2", entry1.getLoc());
103         assertTrue(entry1.isStyleAlert());
104         assertEquals(0.8d, entry1.getPriority(), 0);
105         assertEquals(1, entry1.getLevel());
106 
107         SiteMapEntry entry2 = res.get(2);
108         assertEquals("/demo-site/article/articleSection", entry2.getPath());
109         assertFalse(entry2.isStyleAlert());
110         assertEquals(1, entry2.getLevel());
111 
112         SiteMapEntry entry3 = res.get(3); // Section is required to hide child
113         assertEquals("defaultBaseUrl/demo-site/article/articleSection/article3", entry3.getLoc());
114         assertTrue(entry3.isStyleAlert());
115         assertEquals(2, entry3.getLevel());
116     }
117 
118     @Test
119     public void testGetSiteMapBeansForUirNotForEdit() throws RepositoryException {
120         // GIVEN
121         Node siteMapNode = googleSiteMapSession.getNode("/test1");
122         // WHEN
123         List<SiteMapEntry> res = service.getSiteMapBeans(siteMapNode, true, false);
124 
125         // THEN
126         assertNotNull(res);
127         assertEquals(2, res.size());
128 
129         SiteMapEntry entry0 = res.get(0);
130         assertEquals("always", entry0.getChangefreq());
131         assertEquals("2008-06-12", entry0.getLastmod());
132         assertEquals("null/\\.magnolia/pages/messages\\.(.*)\\.js", entry0.getLoc());
133         assertEquals("/modules/adminInterface/virtualURIMapping/messages", entry0.getPath());
134         assertEquals(0.8d, entry0.getPriority(), 0);
135         assertEquals(4, entry0.getLevel());
136         assertFalse(entry0.isStyleAlert());
137 
138         SiteMapEntry entry1 = res.get(1);
139         assertEquals("null/.magnolia/dialogs/fileThumbnail.jpg", entry1.getLoc());
140         assertEquals("/modules/adminInterface/virtualURIMapping/dialogsFileThumbnail", entry1.getPath());
141     }
142 
143     @Test
144     public void testGetSiteMapBeansForUriNotForEdit() throws RepositoryException {
145         // GIVEN
146         Node siteMapNode = googleSiteMapSession.getNode("/test1");
147         // WHEN
148         List<SiteMapEntry> res = service.getSiteMapBeans(siteMapNode, true, false);
149 
150         // THEN
151         assertNotNull(res);
152         assertEquals(2, res.size());
153 
154         SiteMapEntry entry0 = res.get(0);
155         assertEquals("null/\\.magnolia/pages/messages\\.(.*)\\.js", entry0.getLoc());
156         assertEquals("/modules/adminInterface/virtualURIMapping/messages", entry0.getPath());
157         assertEquals(4, entry0.getLevel());
158         assertFalse(entry0.isStyleAlert());
159 
160         SiteMapEntry entry1 = res.get(1);
161         assertEquals("null/.magnolia/dialogs/fileThumbnail.jpg", entry1.getLoc());
162         assertEquals("/modules/adminInterface/virtualURIMapping/dialogsFileThumbnail", entry1.getPath());
163     }
164 
165     @Test
166     public void testGetSiteMapBeansNotForEdit() throws RepositoryException {
167         // GIVEN
168         Node siteMapNode = googleSiteMapSession.getNode("/test1");
169         // WHEN
170         List<SiteMapEntry> res = IteratorUtils.toList(service.getSiteMapBeans(siteMapNode));
171         Collections.sort(res);
172 
173         // THEN
174         assertNotNull(res);
175         assertEquals(4, res.size());
176 
177         SiteMapEntry entry0 = res.get(0);
178         assertEquals("always", entry0.getChangefreq());
179         assertEquals("2008-06-12", entry0.getLastmod());
180         assertEquals("null/\\.magnolia/pages/messages\\.(.*)\\.js", entry0.getLoc());
181         assertEquals("/modules/adminInterface/virtualURIMapping/messages", entry0.getPath());
182         assertEquals(0.8d, entry0.getPriority(), 0);
183         assertEquals(4, entry0.getLevel());
184         assertFalse(entry0.isStyleAlert());
185 
186         SiteMapEntry entry1 = res.get(1);
187         assertEquals("null/.magnolia/dialogs/fileThumbnail.jpg", entry1.getLoc());
188         assertEquals("/modules/adminInterface/virtualURIMapping/dialogsFileThumbnail", entry1.getPath());
189 
190         SiteMapEntry entry2 = res.get(2);
191         assertEquals("defaultBaseUrl/demo-site/article/articleSection", entry2.getLoc());
192 
193         SiteMapEntry entry3 = res.get(3);
194         assertEquals("defaultBaseUrl/demo-site/article/article1", entry3.getLoc());
195     }
196 
197     @Test
198     public void testUpdatePageNode() throws RepositoryException {
199         // GIVEN
200         SiteMapEntry entry = new SiteMapEntry();
201         entry.setPath("/demo-site/article/article1");
202         entry.setChangefreq("changefreq");
203         entry.setPriority(0.5d);
204         entry.setHide(true);
205         entry.setHideChildren(true);
206 
207         // WHEN
208         service.updatePageNode(entry);
209 
210         // THEN
211         Node node = websiteSession.getNode("/demo-site/article/article1");
212         assertTrue(SiteMapNodeTypes.GoogleSiteMap.isHide(node));
213         assertEquals("changefreq", SiteMapNodeTypes.GoogleSiteMap.getChangeFreq(node));
214         assertEquals(0.5d, SiteMapNodeTypes.GoogleSiteMap.getPriority(node), 0);
215         assertTrue(SiteMapNodeTypes.GoogleSiteMap.isHideChildren(node));
216     }
217 
218 }