View Javadoc
1   /**
2    * This file Copyright (c) 2014-2017 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.for2_1;
35  
36  import static org.hamcrest.MatcherAssert.assertThat;
37  import static org.hamcrest.core.Is.is;
38  
39  import info.magnolia.context.MgnlContext;
40  import info.magnolia.jcr.util.NodeTypes;
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.module.googlesitemap.SiteMapNodeTypes.SiteMap;
47  import info.magnolia.test.hamcrest.NodeMatchers;
48  import info.magnolia.test.mock.MockContext;
49  import info.magnolia.test.mock.MockUtil;
50  import info.magnolia.test.mock.jcr.MockSession;
51  
52  import javax.jcr.Node;
53  import javax.jcr.RepositoryException;
54  
55  import org.junit.After;
56  import org.junit.Before;
57  import org.junit.Test;
58  
59  /**
60   * Main test class for {@link UpdatePropertyNamesAndNodeStructure}.
61   */
62  public class UpdatePropertyNamesAndNodeStructureTest {
63  
64      private Node googleSite;
65      private InstallContextImpl installContext;
66      private UpdatePropertyNamesAndNodeStructure task;
67      private MockSession session;
68  
69      @Before
70      public void setUp() throws Exception {
71          MockUtil.initMockContext();
72          session = new MockSession(GoogleSiteMapConfiguration.WORKSPACE);
73          Node root = session.getRootNode();
74          googleSite = root.addNode("siteMap", SiteMapNodeTypes.SiteMap.NAME);
75          ((MockContext) MgnlContext.getInstance()).addSession(GoogleSiteMapConfiguration.WORKSPACE, session);
76          ((MockContext) MgnlContext.getSystemContext()).addSession(GoogleSiteMapConfiguration.WORKSPACE, session);
77  
78          final ModuleRegistryImpl moduleRegistry = new ModuleRegistryImpl();
79          installContext = new InstallContextImpl(moduleRegistry);
80  
81          task = new UpdatePropertyNamesAndNodeStructure("taskName", "taskDescription");
82  
83      }
84  
85      @After
86      public void tearDown() {
87          MgnlContext.setInstance(null);
88      }
89  
90      @Test
91      public void testSitePagePropertyMigration() throws RepositoryException, TaskExecutionException {
92          // GIVEN
93          googleSite.setProperty("displayName", "displayName");
94          googleSite.setProperty("type", "type");
95          googleSite.setProperty("url", "url");
96          googleSite.setProperty("includeVirtualURIMappings", "includeVirtualURIMappings");
97          googleSite.setProperty(SiteMap.DEFAULT_PRIORITY, 0.6d);
98          Node sites = googleSite.addNode("sites", NodeTypes.ContentNode.NAME);
99          sites.setProperty("0", "/site1");
100         sites.setProperty("1", "/site2");
101 
102         // WHEN
103         task.execute(installContext);
104 
105         // THEN
106         googleSite = session.getNode("siteMap");
107         assertThat(googleSite, NodeMatchers.hasProperty(SiteMap.DISPLAY_NAME, "displayName"));
108         assertThat(googleSite, NodeMatchers.hasProperty(SiteMap.TYPE, "type"));
109         assertThat(googleSite, NodeMatchers.hasProperty(SiteMap.URL, "url"));
110         assertThat(googleSite, NodeMatchers.hasProperty(SiteMap.INCLUDE_VIRTUAL_URI, "includeVirtualURIMappings"));
111 
112         assertThat(googleSite, NodeMatchers.hasProperty(SiteMap.DEFAULT_CHANGEFREQ, GoogleSiteMapConfiguration.DEFAULT_CHANGE_FREQUENCY));
113         assertThat(googleSite, NodeMatchers.hasProperty(SiteMap.DEFAULT_PRIORITY, 0.6d));
114 
115         assertThat(googleSite, NodeMatchers.hasProperty(SiteMap.PAGES));
116         assertThat(googleSite.getProperty(SiteMap.PAGES).getValues()[0].getString(), is("/site1"));
117         assertThat(googleSite.getProperty(SiteMap.PAGES).getValues()[1].getString(), is("/site2"));
118     }
119 
120 }