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  
37  import static info.magnolia.test.hamcrest.ExecutionMatcher.throwsNothing;
38  import static org.junit.Assert.*;
39  
40  import info.magnolia.test.hamcrest.Execution;
41  
42  import java.io.StringReader;
43  
44  import javax.inject.Provider;
45  import javax.jcr.Node;
46  import javax.jcr.RepositoryException;
47  import javax.xml.XMLConstants;
48  import javax.xml.bind.JAXBException;
49  import javax.xml.transform.stream.StreamSource;
50  import javax.xml.validation.Schema;
51  import javax.xml.validation.SchemaFactory;
52  import javax.xml.validation.Validator;
53  
54  import org.apache.commons.lang3.StringUtils;
55  import org.junit.Test;
56  
57  
58  /**
59   * Main test class for {@link SiteMapXMLUtilImpl}.
60   */
61  public class SiteMapXMLUtilImplTest extends ServiceTestUtil {
62      private SiteMapXMLUtilImpl xmlUtil;
63      private Validator validator;
64  
65      @Override
66      public void setUp() throws Exception {
67          super.setUp();
68          xmlUtil = new SiteMapXMLUtilImpl(new Provider<SiteMapService>() {
69              @Override
70              public SiteMapService get() {
71                  return service;
72              }
73          });
74  
75          SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
76          Schema schema = schemaFactory.newSchema(getClass().getResource("sitemap.xsd"));
77          validator = schema.newValidator();
78      }
79  
80      @Test
81      public void generateAndValidateStandardSiteMapXML() throws Exception {
82          // GIVEN
83          Node siteMapNode = googleSiteMapSession.getNode("/test1");
84  
85          // WHEN
86          final String res = xmlUtil.generateSiteMapXML(siteMapNode);
87  
88          // THEN
89          assertNotNull(res);
90          assertTrue("Contains a url def for page", StringUtils.contains(res, "<url><loc>defaultBaseUrl/demo-site/article/articleSection</loc><lastmod>2014-01-08</lastmod><changefreq>always</changefreq><priority>0.8</priority></url>"));
91          assertTrue("Contains a url def for Uri", StringUtils.contains(res, "<url><loc>null/\\.magnolia/pages/messages\\.(.*)\\.js</loc><lastmod>2008-06-12</lastmod><changefreq>always</changefreq><priority>0.8</priority></url>"));
92          assertThat(new Execution() {
93              @Override
94              public void evaluate() throws Exception {
95                  validator.validate(new StreamSource(new StringReader(res)));
96              }
97          }, throwsNothing());
98      }
99  
100     @Test
101     public void generateMobileSiteMapXML() throws RepositoryException, JAXBException {
102         // GIVEN
103         Node siteMapNode = googleSiteMapSession.getNode("/test3");
104 
105         // WHEN
106         String res = xmlUtil.generateSiteMapXML(siteMapNode);
107 
108         // THEN
109         assertNotNull(res);
110         assertTrue("Contains a url def for Pages", StringUtils.contains(res, "<url><loc>defaultBaseUrl/demo-site/news/subNewsSection2/news2</loc><lastmod>2014-01-08</lastmod><changefreq>hourly</changefreq><priority>0.7</priority><mobile:mobile/></url>"));
111         assertTrue("Contains a url def for pages", StringUtils.contains(res, "<url><loc>defaultBaseUrl/demo-site/news/subNewsSection1/news11</loc><lastmod>2014-01-08</lastmod><changefreq>hourly</changefreq><priority>0.7</priority><mobile:mobile/></url>"));
112         assertTrue("Contains a url def for Uri", StringUtils.contains(res, "<url><loc>null/.magnolia/dialogs/fileThumbnail.jpg</loc><lastmod>2004-11-02</lastmod><changefreq>hourly</changefreq><priority>0.7</priority><mobile:mobile/></url>"));
113     }
114 
115     @Test
116     public void generateNonExistingSiteMapXML() throws RepositoryException, JAXBException {
117         // GIVEN
118 
119         // WHEN
120         String res = xmlUtil.generateSiteMapXML(websiteNode);
121 
122         // THEN
123         assertNotNull(res);
124         assertTrue("Contains an empty XML", StringUtils.contains(res, "<?xml version=\"1.0\" ?><urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\"></urlset>"));
125     }
126 }