View Javadoc
1   /**
2    * This file Copyright (c) 2014 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  
38  import info.magnolia.module.googlesitemap.bean.SiteMapEntry;
39  import info.magnolia.module.googlesitemap.service.ServiceTestUtil;
40  import info.magnolia.module.googlesitemap.service.SiteMapService;
41  import info.magnolia.module.googlesitemap.service.SiteMapXMLUtilImpl;
42  import info.magnolia.rendering.model.RenderingModel;
43  import info.magnolia.rendering.template.RenderableDefinition;
44  
45  import java.util.Collections;
46  import java.util.List;
47  
48  import javax.inject.Provider;
49  import javax.jcr.Node;
50  import javax.jcr.RepositoryException;
51  
52  import org.apache.commons.collections.IteratorUtils;
53  import org.apache.commons.lang.StringUtils;
54  import org.junit.Test;
55  
56  /**
57   * Main test class for {@link SiteMapModel}.
58   */
59  public class SiteMapModelTest extends ServiceTestUtil {
60  
61      private SiteMapModel<RenderableDefinition> model;
62      private SiteMapXMLUtilImpl xmlUtil;
63  
64      @Override
65      public void setUp() throws Exception {
66          super.setUp();
67          xmlUtil = new SiteMapXMLUtilImpl(new Provider<SiteMapService>() {
68              @Override
69              public SiteMapService get() {
70                  return service;
71              }
72          });
73      }
74  
75      private void initModel(Node contentNode) {
76          RenderingModel<RenderableDefinition> parent = null;
77          RenderableDefinition definition = null;
78          model = new SiteMapModel<RenderableDefinition>(contentNode, definition, parent, service, xmlUtil);
79      }
80  
81      @Test
82      public void getSiteMapBeans() throws RepositoryException {
83          // GIVEN
84          Node siteMapNode = googleSiteMapSession.getNode("/test1");
85          initModel(siteMapNode);
86  
87          // WHEN
88          List<SiteMapEntry> res = IteratorUtils.toList(model.getSiteMapBeans());
89          Collections.sort(res);
90  
91          // THEN
92          assertNotNull(res);
93          assertEquals(4, res.size());
94          SiteMapEntry entry0 = res.get(0);
95          assertEquals("/modules/adminInterface/virtualURIMapping/messages", entry0.getPath());
96  
97          SiteMapEntry entry1 = res.get(1);
98          assertEquals("/modules/adminInterface/virtualURIMapping/dialogsFileThumbnail", entry1.getPath());
99  
100         SiteMapEntry entry2 = res.get(2);
101         assertEquals("defaultBaseUrl/demo-site/article/articleSection", entry2.getLoc());
102 
103         SiteMapEntry entry3 = res.get(3);
104         assertEquals("defaultBaseUrl/demo-site/article/article1", entry3.getLoc());
105     }
106 
107     @Test
108     public void getXmlBeans() throws RepositoryException {
109         // GIVEN
110         Node siteMapNode = googleSiteMapSession.getNode("/test3");
111         initModel(siteMapNode);
112 
113         // WHEN
114         String res = model.getXML();
115 
116         // THEN
117         assertNotNull(res);
118         assertTrue("Contains a url def for Pages", StringUtils.contains(res, "<url><mobile:mobile/><changefreq>hourly</changefreq><lastmod>2014-01-08</lastmod><loc>defaultBaseUrl/demo-site/news/subNewsSection2/news2</loc><priority>0.7</priority></url>"));
119 
120     }
121 }