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