View Javadoc

1   /**
2    * This file Copyright (c) 2012 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;
35  
36  import static org.mockito.Mockito.*;
37  
38  import info.magnolia.cms.beans.config.ServerConfiguration;
39  import info.magnolia.cms.i18n.DefaultI18nContentSupport;
40  import info.magnolia.cms.i18n.I18nContentSupport;
41  import info.magnolia.cms.security.MgnlUser;
42  import info.magnolia.cms.security.User;
43  import info.magnolia.context.MgnlContext;
44  import info.magnolia.jcr.util.NodeTypes;
45  import info.magnolia.module.googlesitemap.bean.SiteMapEntry;
46  import info.magnolia.module.googlesitemap.service.SiteMapService;
47  import info.magnolia.module.googlesitemap.service.query.QueryUtil;
48  import info.magnolia.module.templatingkit.sites.Site;
49  import info.magnolia.module.templatingkit.sites.SiteManager;
50  import info.magnolia.test.ComponentsTestUtil;
51  import info.magnolia.test.mock.MockWebContext;
52  import info.magnolia.test.mock.jcr.MockNode;
53  import info.magnolia.test.mock.jcr.MockSession;
54  
55  import java.util.ArrayList;
56  import java.util.Collections;
57  import java.util.HashMap;
58  import java.util.Iterator;
59  import java.util.List;
60  
61  import javax.jcr.Node;
62  import javax.jcr.RepositoryException;
63  
64  import org.apache.jackrabbit.JcrConstants;
65  import org.junit.After;
66  import org.junit.Before;
67  import org.mockito.Answers;
68  
69  
70  /**
71   * Utility test class.
72   * @version $Id$
73   *
74   */
75  public class SiteMapTestUtil {
76  
77      protected static final String CONTEXT_PATH = "/manual_set_context_path";
78      protected static final String DEFAULT_URL = "http://localhost:8080";
79      protected static final String ROOT_PAGE = "rootPage";
80      protected static final String PAGE_DEEP_1_1 = "page_1_1";
81      protected static final String PAGE_DEEP_1_1_1 = "page_1_1_1";
82      protected static final String PAGE_DEEP_1_2 = "page_1_2";
83      protected static final String PAGE_DEEP_1_2_1 = "page_1_2_1";
84      protected static final String VIRTUALURI_DEEP_2_1 = "virtualUri_2_1";
85      protected static final String VIRTUALURI_DEEP_2_2 = "virtualUri_2_2";
86  
87      protected MockNode rootWebsite;
88      protected Node rootPageWebsite;
89      protected Node page_1_1;
90      protected Node page_1_1_1;
91      protected Node page_1_2;
92      protected Node page_1_2_1;
93      protected MockNode rootConfig;
94      protected Node rootPageConfig;
95      protected Node virtualURIMapping;
96      protected Node virtualUri_2_1;
97      protected Node virtualUri_2_2;
98  
99  
100     protected MockWebContext ctx ;
101     protected SiteMapService service;
102 
103     @Before
104     public void setUp() throws RepositoryException{
105         // Init ctx
106         ctx = new MockWebContext();
107         ctx.setContextPath(CONTEXT_PATH);
108         User user = new MgnlUser("userName", "userRealName", new ArrayList<String>() , new ArrayList<String>(), new HashMap<String, String>());
109         ctx.setUser(user);
110         MgnlContext.setInstance(ctx);
111 
112 
113         ServerConfiguration sc = new ServerConfiguration();
114         sc.setDefaultBaseUrl(DEFAULT_URL);
115         ComponentsTestUtil.setInstance(ServerConfiguration.class, sc);
116 
117         // Init website Session
118         MockSession sessionWebsite = new MockSession("website");
119         ctx.addSession("website", sessionWebsite);
120         rootWebsite = (MockNode) sessionWebsite.getRootNode();
121         rootPageWebsite = rootWebsite.addNode(ROOT_PAGE, NodeTypes.Content.NAME);
122         page_1_1 = rootPageWebsite.addNode(PAGE_DEEP_1_1, NodeTypes.Content.NAME);
123         page_1_1_1 = page_1_1.addNode(PAGE_DEEP_1_1_1, NodeTypes.Content.NAME);
124         page_1_2 = rootPageWebsite.addNode(PAGE_DEEP_1_2, NodeTypes.Content.NAME);
125         page_1_2_1 = page_1_2.addNode(PAGE_DEEP_1_2_1, JcrConstants.NT_FILE);
126 
127         // Init config Session
128         MockSession sessionConfig = new MockSession("config");
129         ctx.addSession("config", sessionConfig);
130         rootConfig = (MockNode) sessionConfig.getRootNode();
131         rootPageConfig = rootConfig.addNode(ROOT_PAGE, NodeTypes.Content.NAME);
132         virtualURIMapping = rootPageConfig.addNode("virtualURIMapping", NodeTypes.Content.NAME);
133         virtualUri_2_1 = virtualURIMapping.addNode(VIRTUALURI_DEEP_2_1, NodeTypes.ContentNode.NAME);
134         virtualUri_2_2 = virtualURIMapping.addNode(VIRTUALURI_DEEP_2_2, NodeTypes.ContentNode.NAME);
135     }
136 
137 
138 
139     @After
140     public void tearDown() {
141         ComponentsTestUtil.clear();
142         MgnlContext.setInstance(null);
143         service = null;
144     }
145 
146 
147 
148     /**
149      * Init the SiteMapService.
150      */
151     public void initService() throws RepositoryException {
152         QueryUtil queryUtil =  mock(QueryUtil.class, Answers.RETURNS_SMART_NULLS.toString());
153         String xpath = "//virtualURIMapping//element(*," + NodeTypes.ContentNode.NAME + ")";
154         when(queryUtil.query("config", xpath, "xpath", NodeTypes.ContentNode.NAME)).thenReturn(virtualURIMapping.getNodes());
155         GoogleSiteMapConfiguration configuration = new GoogleSiteMapConfiguration();
156         // Init Site
157         SiteManager siteManager = mock(SiteManager.class, Answers.RETURNS_SMART_NULLS.toString());
158         Site site = new Site();
159         site.setEnabled(true);
160         when(siteManager.getAssignedSite(page_1_1)).thenReturn(site);
161         when(siteManager.getAssignedSite(page_1_2)).thenReturn(site);
162         when(siteManager.getAssignedSite(page_1_1_1)).thenReturn(site);
163         when(siteManager.getAssignedSite(page_1_2_1)).thenReturn(site);
164 
165         ComponentsTestUtil.setInstance(I18nContentSupport.class, new DefaultI18nContentSupport());
166 
167         service = new SiteMapService(siteManager, configuration, queryUtil);
168     }
169 
170 
171     /**
172      * Add a metaData to the Node.
173      */
174     public void setMetaData(Node node, String templateName) throws RepositoryException {
175         NodeTypes.Created.set(node);
176         NodeTypes.LastModified.update(node);
177         if(templateName != null) {
178             NodeTypes.Renderable.set(node, templateName);
179         }
180     }
181 
182 
183     public List<SiteMapEntry> fromIteratorToList(Iterator< SiteMapEntry> iterator) {
184         List<SiteMapEntry> res = new ArrayList<SiteMapEntry>();
185         while(iterator.hasNext()) {
186             res.add(iterator.next());
187         }
188         Collections.sort(res);
189         return res;
190     }
191 
192     public Node initSiteComponentNode(Node parent, String nodeName) throws RepositoryException {
193         Node res = parent.addNode(nodeName, NodeTypes.Component.NAME);
194         setMetaData(res, "google-sitemap:components/content/siteComponent");
195         return res;
196     }
197 
198     public Node initVirtualUriComponentNode(Node parent, String nodeName) throws RepositoryException {
199         Node res = parent.addNode(nodeName, NodeTypes.Component.NAME);
200         setMetaData(res, "google-sitemap:components/content/virtualUriComponent");
201         return res;
202     }
203 
204 }