View Javadoc

1   /**
2    * This file Copyright (c) 2013 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.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          // Register siteMap node type
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         // Import the sourrce definitions
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         // GIVEN
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         // WHEN
136         task.doExecute(installContext);
137 
138         // THEN
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         // assertFalse("No virtual URI mapping should be defined", siteMap.getProperty(GoogleSiteMapConfiguration.INCLUDE_VIRTUAL_URI_MAPPINGS_PROPERTIES).getBoolean());
158 
159         assertFalse(websiteSession.nodeExists("/demoFeaturesSiteMap"));
160 
161     }
162 
163     @Test
164     @Ignore
165     public void testDoExecuteSubFolderDefinition() throws TaskExecutionException, RepositoryException {
166         // GIVEN
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         // WHEN
171         task.doExecute(installContext);
172 
173         // THEN
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         // assertEquals("/demo-project/demoProjectSiteMap", siteMap.getProperty(GoogleSiteMapConfiguration.SITE_MAP_URL_PROPERTY_NAME).getString());
179         // assertEquals(SiteMapType.Standard.toString(), siteMap.getProperty(GoogleSiteMapConfiguration.SITE_MAP_TYPE_PROPERTY_NAME).getString());
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         // assertTrue(siteMap.hasNode(GoogleSiteMapConfiguration.SITE_DIALOG_CONFIGURATION_NAME));
185         // Node sites = siteMap.getNode(GoogleSiteMapConfiguration.SITE_DIALOG_CONFIGURATION_NAME);
186 
187         // Node filteredNode = new JCRMgnlPropertiesFilteringNodeWrapper(sites);
188         // PropertyIterator iterator = filteredNode.getProperties();
189         // while (iterator.hasNext()) {
190         // Property property = iterator.nextProperty();
191         // assertTrue("Migrated content should have this site mapp defined", siteDef.contains(property.getString()));
192         // }
193         //
194         // assertTrue("Virtual URI mapping should be defined", siteMap.getProperty(GoogleSiteMapConfiguration.INCLUDE_VIRTUAL_URI_MAPPINGS_PROPERTIES).getBoolean());
195         assertFalse(websiteSession.nodeExists("/demo-project/demoProjectSiteMap"));
196 
197     }
198 
199     @Test
200     @Ignore
201     public void testDoExecuteConfiguredSearchPath() throws TaskExecutionException, RepositoryException {
202         // GIVEN
203         SiteMapDefinitionMigrationTask task = new SiteMapDefinitionMigrationTask("name", "description", RepositoryConstants.WEBSITE, "/demo-project");
204 
205         // WHEN
206         task.doExecute(installContext);
207 
208         // THEN
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 }