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;
35  
36  import static org.junit.Assert.*;
37  import static org.mockito.Mockito.mock;
38  
39  import info.magnolia.cms.exchange.ActivationManager;
40  import info.magnolia.cms.security.RoleManager;
41  import info.magnolia.cms.security.SecuritySupport;
42  import info.magnolia.cms.security.SecuritySupportImpl;
43  import info.magnolia.context.MgnlContext;
44  import info.magnolia.jcr.util.NodeTypes;
45  import info.magnolia.jcr.util.NodeUtil;
46  import info.magnolia.module.model.Version;
47  import info.magnolia.module.rssaggregator.setup.RSSAggregatorVersionHandler;
48  import info.magnolia.repository.RepositoryConstants;
49  import info.magnolia.test.ComponentsTestUtil;
50  
51  import java.util.Arrays;
52  import java.util.List;
53  
54  import javax.jcr.Node;
55  import javax.jcr.NodeIterator;
56  import javax.jcr.RepositoryException;
57  import javax.jcr.Session;
58  
59  import org.junit.Before;
60  import org.junit.Test;
61  
62  /**
63   * Tests for {@link info.magnolia.module.rssaggregator.setup.RSSAggregatorVersionHandler}.
64   */
65  public class RSSAggregatorVersionHandlerTest extends ModuleVersionHandlerTestCase {
66  
67      private Session config;
68  
69      @Override
70      @Before
71      public void setUp() throws Exception {
72          super.setUp();
73          ComponentsTestUtil.setInstance(ActivationManager.class, mock(ActivationManager.class));
74  
75          SecuritySupportImpl impl = new SecuritySupportImpl();
76          ComponentsTestUtil.setInstance(SecuritySupport.class, impl);
77          RoleManager roleManager = mock(RoleManager.class);
78          impl.setRoleManager(roleManager);
79  
80          config = MgnlContext.getJCRSession(RepositoryConstants.CONFIG);
81  
82          this.setupConfigNode("/modules/data/config/importers/rssaggregator");
83          this.setupConfigNode("/modules/data/trees/RssAggregator");
84          this.setupConfigNode("/modules/data/config/types/RssAggregator");
85          this.setupConfigNode("/modules/adminInterface/config/menu/data/RssAggregator");
86      }
87  
88      @Override
89      protected String[] getExtraWorkspaces() {
90          return new String[] { "dam" };
91      }
92  
93      @Override
94      public String getModuleDescriptorPath() {
95          return "/META-INF/magnolia/rssaggregator.xml";
96      }
97  
98      @Override
99      protected List<String> getModuleDescriptorPathsForTests() {
100         return Arrays.asList(
101                 "/META-INF/magnolia/core.xml"
102         );
103     }
104 
105     @Override
106     public ModuleVersionHandler newModuleVersionHandlerForTests() {
107         return new RSSAggregatorVersionHandler();
108     }
109 
110     @Test
111     public void testUpdateTo22ReordersRSSBeforeConfigApp() throws Exception {
112         // GIVEN
113         Session session = MgnlContext.getJCRSession(RepositoryConstants.CONFIG);
114         Node manageApps = NodeUtil.createPath(session.getRootNode(), "modules/ui-admincentral/config/appLauncherLayout/groups/manage/apps", NodeTypes.ContentNode.NAME);
115         NodeUtil.createPath(manageApps, "security", NodeTypes.ContentNode.NAME);
116         NodeUtil.createPath(manageApps, "configuration", NodeTypes.ContentNode.NAME);
117 
118         // WHEN
119         executeUpdatesAsIfTheCurrentlyInstalledVersionWas(Version.parseVersion("2.1"));
120 
121         // THEN
122         NodeIterator it = manageApps.getNodes();
123         assertTrue(manageApps.hasNode("rssAggregator"));
124         assertEquals("security", it.nextNode().getName());
125         assertEquals("rssAggregator", it.nextNode().getName());
126         assertEquals("configuration", it.nextNode().getName());
127     }
128 
129     @Test
130     public void testFreshInstallReordersRSSBeforeConfigApp() throws Exception {
131         // GIVEN
132         Session session = MgnlContext.getJCRSession(RepositoryConstants.CONFIG);
133         Node manageApps = NodeUtil.createPath(session.getRootNode(), "modules/ui-admincentral/config/appLauncherLayout/groups/manage/apps", NodeTypes.ContentNode.NAME);
134         NodeUtil.createPath(manageApps, "security", NodeTypes.ContentNode.NAME);
135         NodeUtil.createPath(manageApps, "configuration", NodeTypes.ContentNode.NAME);
136         // we have to create the rssAggregator node artificially before bootstrapping, otherwise test would fail in maven
137         NodeUtil.createPath(manageApps, "rssAggregator", NodeTypes.ContentNode.NAME);
138 
139         // WHEN
140         executeUpdatesAsIfTheCurrentlyInstalledVersionWas(null);
141 
142         // THEN
143         NodeIterator it = manageApps.getNodes();
144         assertTrue(manageApps.hasNode("rssAggregator"));
145         assertEquals("security", it.nextNode().getName());
146         assertEquals("rssAggregator", it.nextNode().getName());
147         assertEquals("configuration", it.nextNode().getName());
148     }
149 
150     @Test
151     public void testUpdateFrom21() throws RepositoryException, ModuleManagementException {
152         // GIVEN
153         assertTrue(config.itemExists("/modules/data/trees/RssAggregator"));
154         assertTrue(config.itemExists("/modules/data/config/types/RssAggregator"));
155         assertTrue(config.itemExists("/modules/adminInterface/config/menu/data/RssAggregator"));
156 
157         // WHEN
158         executeUpdatesAsIfTheCurrentlyInstalledVersionWas(Version.parseVersion("2.1"));
159 
160         // THEN
161         assertFalse(config.itemExists("/modules/data/trees/RssAggregator"));
162         assertFalse(config.itemExists("/modules/data/config/types/RssAggregator"));
163         assertFalse(config.itemExists("/modules/adminInterface/config/menu/data/RssAggregator"));
164     }
165 
166     @Test
167     public void testUpdateFromMagnolia45x() throws RepositoryException, ModuleManagementException {
168         // GIVEN
169         this.setupConfigNode("/modules/adminInterface/pages/rssaggregator");
170         this.setupConfigNode("/modules/adminInterface/config/menu/coonfiguration/rssaggregator");
171         this.setupConfigNode("/modules/adminInterface/virtualURIMapping/rssFeeds");
172         this.setupConfigProperty("/modules/data/config/importers/rssaggregator", "repository", "data");
173 
174         assertTrue(config.itemExists("/modules/adminInterface/pages/rssaggregator"));
175         assertTrue(config.itemExists("/modules/adminInterface/config/menu/coonfiguration/rssaggregator"));
176 
177         // WHEN
178         executeUpdatesAsIfTheCurrentlyInstalledVersionWas(Version.parseVersion("1.3"));
179 
180         // THEN
181         assertFalse(config.itemExists("/modules/adminInterface/pages/rssaggregator"));
182         assertFalse(config.itemExists("/modules/adminInterface/config/menu/coonfiguration/rssaggregator"));
183     }
184 }