View Javadoc

1   /**
2    * This file Copyright (c) 2014 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.service;
35  
36  import static org.junit.Assert.assertTrue;
37  import static org.mockito.Matchers.any;
38  import static org.mockito.Mockito.*;
39  
40  import info.magnolia.cms.beans.config.ServerConfiguration;
41  import info.magnolia.cms.i18n.DefaultI18nContentSupport;
42  import info.magnolia.cms.i18n.I18nContentSupport;
43  import info.magnolia.context.MgnlContext;
44  import info.magnolia.importexport.DataTransporter;
45  import info.magnolia.jcr.util.NodeTypes;
46  import info.magnolia.jcr.util.NodeUtil;
47  import info.magnolia.module.googlesitemap.GoogleSiteMapConfiguration;
48  import info.magnolia.module.googlesitemap.SiteMapTestUtil;
49  import info.magnolia.module.googlesitemap.service.query.QueryUtil;
50  import info.magnolia.module.templatingkit.sites.Site;
51  import info.magnolia.module.templatingkit.sites.SiteManager;
52  import info.magnolia.repository.RepositoryConstants;
53  import info.magnolia.test.ComponentsTestUtil;
54  
55  import java.io.File;
56  import java.io.FileInputStream;
57  import java.io.InputStream;
58  
59  import javax.jcr.ImportUUIDBehavior;
60  import javax.jcr.Node;
61  import javax.jcr.RepositoryException;
62  import javax.jcr.Session;
63  
64  import org.apache.commons.io.IOUtils;
65  
66  /**
67   * Util test class used for initialization of a service.
68   */
69  public class ServiceTestUtil extends SiteMapTestUtil {
70  
71      protected Session websiteSession;
72      protected Session configSession;
73      protected SiteMapService service;
74      protected Node websiteNode;
75  
76      @Override
77      public void setUp() throws Exception {
78          super.setUp();
79          websiteSession = MgnlContext.getJCRSession(RepositoryConstants.WEBSITE);
80          // Register SiteMap
81          File file = new File(getClass().getResource("/googleSitemaps.xml").getFile());
82          InputStream inputStream = new FileInputStream(file);
83          DataTransporter.importXmlStream(inputStream, GoogleSiteMapConfiguration.WORKSPACE, "/", "test-stream", false, ImportUUIDBehavior.IMPORT_UUID_CREATE_NEW, true, false);
84  
85          // Register Pages
86          file = new File(getClass().getResource("/website.demo-site.xml").getFile());
87          inputStream = new FileInputStream(file);
88          DataTransporter.importXmlStream(inputStream, RepositoryConstants.WEBSITE, "/", "test-stream", false, ImportUUIDBehavior.IMPORT_UUID_CREATE_NEW, true, false);
89          websiteNode = websiteSession.getNode("/demo-site");
90  
91          // Register Config
92          // Initialize config tree
93          configSession = MgnlContext.getJCRSession(RepositoryConstants.CONFIG);
94          NodeUtil.createPath(configSession.getRootNode(), "/modules/adminInterface", NodeTypes.ContentNode.NAME);
95          file = new File(getClass().getResource("/config.modules.adminInterface.virtualURIMapping.xml").getFile());
96          inputStream = new FileInputStream(file);
97          DataTransporter.importXmlStream(inputStream, RepositoryConstants.CONFIG, "/modules/adminInterface", "test-stream", false, ImportUUIDBehavior.IMPORT_UUID_CREATE_NEW, true, false);
98  
99          IOUtils.closeQuietly(inputStream);
100         // Initial check
101         assertTrue(googleSiteMapSession.nodeExists("/test1"));
102         assertTrue(googleSiteMapSession.nodeExists("/test2"));
103         assertTrue(websiteSession.nodeExists("/demo-site"));
104 
105         // Init service
106         initService();
107     }
108 
109     protected void initService() throws RepositoryException {
110         // Init Site
111         Site site = new Site();
112         site.setEnabled(true);
113         SiteManager siteManager = mock(SiteManager.class);
114         when(siteManager.getAssignedSite((Node) any())).thenReturn(site);
115         ComponentsTestUtil.setInstance(I18nContentSupport.class, new DefaultI18nContentSupport());
116         ServerConfiguration.getInstance().setDefaultBaseUrl("defaultBaseUrl");
117         service = new SiteMapService(siteManager, new GoogleSiteMapConfiguration(), new QueryUtil());
118     }
119 }