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.setup.migration;
35
36 import static org.junit.Assert.*;
37
38 import info.magnolia.context.MgnlContext;
39 import info.magnolia.importexport.DataTransporter;
40 import info.magnolia.jcr.wrapper.JCRMgnlPropertiesFilteringNodeWrapper;
41 import info.magnolia.module.InstallContextImpl;
42 import info.magnolia.module.ModuleRegistryImpl;
43 import info.magnolia.module.delta.TaskExecutionException;
44 import info.magnolia.module.googlesitemap.GoogleSiteMapConfiguration;
45 import info.magnolia.module.googlesitemap.SiteMapNodeTypes;
46 import info.magnolia.objectfactory.Components;
47 import info.magnolia.repository.RepositoryConstants;
48 import info.magnolia.repository.RepositoryManager;
49 import info.magnolia.test.RepositoryTestCase;
50
51 import java.io.ByteArrayInputStream;
52 import java.io.File;
53 import java.io.FileInputStream;
54 import java.io.InputStream;
55 import java.util.Arrays;
56 import java.util.HashSet;
57 import java.util.Set;
58
59 import javax.jcr.ImportUUIDBehavior;
60 import javax.jcr.Node;
61 import javax.jcr.Property;
62 import javax.jcr.PropertyIterator;
63 import javax.jcr.RepositoryException;
64 import javax.jcr.Session;
65
66 import org.junit.Before;
67 import org.junit.Ignore;
68 import org.junit.Test;
69 import org.slf4j.Logger;
70 import org.slf4j.LoggerFactory;
71
72
73
74
75 public class SiteMapDefinitionMigrationTaskTest extends RepositoryTestCase {
76
77 private static final Logger log = LoggerFactory.getLogger(SiteMapDefinitionMigrationTaskTest.class);
78 private Session googleSiteMapSession;
79 private Session websiteSession;
80 protected InstallContextImpl installContext;
81
82 protected String siteMapNodeType = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + "<nodeTypes" + " xmlns:rep=\"internal\""
83 + " xmlns:nt=\"http://www.jcp.org/jcr/nt/1.0\"" + " xmlns:mix=\"http://www.jcp.org/jcr/mix/1.0\""
84 + " xmlns:mgnl=\"http://www.magnolia.info/jcr/mgnl\"" + " xmlns:jcr=\"http://www.jcp.org/jcr/1.0\">" + "<nodeType name=\"" + SiteMapNodeTypes.SiteMap.NAME + "\""
85 + " isMixin=\"false\" hasOrderableChildNodes=\"true\" primaryItemName=\"\">"
86 + "<supertypes>"
87 + "<supertype>mgnl:content</supertype>"
88 + "</supertypes>"
89 + "</nodeType>"
90 + "</nodeTypes>";
91
92 @Override
93 @Before
94 public void setUp() throws Exception {
95 super.setUp();
96
97 googleSiteMapSession = MgnlContext.getJCRSession(GoogleSiteMapConfiguration.WORKSPACE);
98 websiteSession = MgnlContext.getJCRSession(RepositoryConstants.WEBSITE);
99
100 RepositoryManager repositoryManager = Components.getComponent(RepositoryManager.class);
101 try {
102 repositoryManager.getRepositoryProvider("magnolia").registerNodeTypes(new ByteArrayInputStream(siteMapNodeType.getBytes()));
103 } catch (RepositoryException e) {
104 log.error("", e);
105 }
106
107
108 File inputFile1 = new File(getClass().getResource("/website.demo-project.xml").getFile());
109 InputStream inputStream1 = new FileInputStream(inputFile1);
110 DataTransporter.importXmlStream(inputStream1, RepositoryConstants.WEBSITE, "/", "test-stream", false, ImportUUIDBehavior.IMPORT_UUID_CREATE_NEW, true, false);
111
112 File inputFile2 = new File(getClass().getResource("/website.demoFeaturesSiteMap.xml").getFile());
113 InputStream inputStream2 = new FileInputStream(inputFile2);
114 DataTransporter.importXmlStream(inputStream2, RepositoryConstants.WEBSITE, "/", "test-stream", false, ImportUUIDBehavior.IMPORT_UUID_CREATE_NEW, true, false);
115
116 final ModuleRegistryImpl moduleRegistry = new ModuleRegistryImpl();
117 installContext = new InstallContextImpl(moduleRegistry);
118
119 }
120
121 @Override
122 protected String getRepositoryConfigFileName() {
123 String repositoryFileName = "test-siteMap-repositories.xml";
124 setRepositoryConfigFileName(repositoryFileName);
125 return repositoryFileName;
126 }
127
128 @Test
129 @Ignore
130 public void testDoExecuteRootDefinition() throws TaskExecutionException, RepositoryException {
131
132 Set<String> siteDef = new HashSet<String>(Arrays.asList("/demo-features/content-templates", "/demo-features/special-templates/glossary", "/demo-features/content-structure"));
133 SiteMapDefinitionMigrationTask task = new SiteMapDefinitionMigrationTask("name", "description", RepositoryConstants.WEBSITE, null);
134 assertTrue(websiteSession.nodeExists("/demoFeaturesSiteMap"));
135
136 task.doExecute(installContext);
137
138
139 assertTrue("Should have a migrated SiteMapDefinition", googleSiteMapSession.nodeExists("/demoFeaturesSiteMap"));
140 Node siteMap = googleSiteMapSession.getNode("/demoFeaturesSiteMap");
141 assertTrue("Property should have been copied", siteMap.hasProperty("mgnl:template"));
142 assertEquals("google-sitemap:pages/siteMapsConfiguration", siteMap.getProperty("mgnl:template").getString());
143 assertEquals("/demoFeaturesSiteMap", siteMap.getProperty(SiteMapNodeTypes.SiteMap.URL).getString());
144 assertTrue("Property should have been copied", siteMap.hasProperty("mgnl:activationStatus"));
145 assertEquals(true, siteMap.getProperty("mgnl:activationStatus").getBoolean());
146 assertFalse(siteMap.hasNode("content"));
147 assertTrue(siteMap.hasNode(SiteMapNodeTypes.SiteMap.PAGES));
148 Node sites = siteMap.getNode(SiteMapNodeTypes.SiteMap.PAGES);
149
150 Node filteredNode = new JCRMgnlPropertiesFilteringNodeWrapper(sites);
151 PropertyIterator iterator = filteredNode.getProperties();
152 while (iterator.hasNext()) {
153 Property property = iterator.nextProperty();
154 assertTrue("Migrated content should have this site mapp defined", siteDef.contains(property.getString()));
155 }
156
157
158
159 assertFalse(websiteSession.nodeExists("/demoFeaturesSiteMap"));
160
161 }
162
163 @Test
164 @Ignore
165 public void testDoExecuteSubFolderDefinition() throws TaskExecutionException, RepositoryException {
166
167 Set<String> siteDef = new HashSet<String>(Arrays.asList("/demo-project/about/subsection-articles", "/demo-project/news-and-events/events-overview"));
168 SiteMapDefinitionMigrationTask task = new SiteMapDefinitionMigrationTask("name", "description", RepositoryConstants.WEBSITE, null);
169 assertTrue(websiteSession.nodeExists("/demo-project/demoProjectSiteMap"));
170
171 task.doExecute(installContext);
172
173
174 assertTrue("Should have a migrated SiteMapDefinition", googleSiteMapSession.nodeExists("/demo-project/demoProjectSiteMap"));
175 Node siteMap = googleSiteMapSession.getNode("/demo-project/demoProjectSiteMap");
176 assertTrue("Property should have been copied", siteMap.hasProperty("mgnl:template"));
177 assertEquals("google-sitemap:pages/siteMapsConfiguration", siteMap.getProperty("mgnl:template").getString());
178
179
180 assertTrue("Property should have been copied", siteMap.hasProperty("mgnl:activationStatus"));
181 assertEquals(true, siteMap.getProperty("mgnl:activationStatus").getBoolean());
182
183 assertFalse(siteMap.hasNode("content"));
184
185
186
187
188
189
190
191
192
193
194
195 assertFalse(websiteSession.nodeExists("/demo-project/demoProjectSiteMap"));
196
197 }
198
199 @Test
200 @Ignore
201 public void testDoExecuteConfiguredSearchPath() throws TaskExecutionException, RepositoryException {
202
203 SiteMapDefinitionMigrationTask task = new SiteMapDefinitionMigrationTask("name", "description", RepositoryConstants.WEBSITE, "/demo-project");
204
205
206 task.doExecute(installContext);
207
208
209 assertTrue("Should have a migrated SiteMapDefinition", googleSiteMapSession.nodeExists("/demo-project/demoProjectSiteMap"));
210 assertFalse("Should not have a migrated SiteMapDefinition", googleSiteMapSession.nodeExists("/demoFeaturesSiteMap"));
211 assertFalse(websiteSession.nodeExists("/demo-project/demoProjectSiteMap"));
212 assertTrue(websiteSession.nodeExists("/demoFeaturesSiteMap"));
213 }
214
215 }