1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
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
83 Node siteMapNode = googleSiteMapSession.getNode("/test1");
84
85
86 final String res = xmlUtil.generateSiteMapXML(siteMapNode);
87
88
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
103 Node siteMapNode = googleSiteMapSession.getNode("/test3");
104
105
106 String res = xmlUtil.generateSiteMapXML(siteMapNode);
107
108
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
118
119
120 String res = xmlUtil.generateSiteMapXML(websiteNode);
121
122
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 }