1. Project Clover database Fri Apr 29 2016 13:24:33 CEST
  2. Package info.magnolia.module.blossom.view

File UuidRedirectViewResolverTest.java

 

Code metrics

0
101
6
1
238
144
6
0.06
16.83
6
1

Classes

Class Line # Actions
UuidRedirectViewResolverTest 66 101 0% 6 0
1.0100%
 

Contributing tests

This file is covered by 5 tests. .

Source view

1    /**
2    * This file Copyright (c) 2010-2016 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.blossom.view;
35   
36    import org.junit.Test;
37    import org.springframework.mock.web.MockHttpServletRequest;
38    import org.springframework.mock.web.MockHttpServletResponse;
39    import org.springframework.ui.ModelMap;
40    import org.springframework.web.servlet.DispatcherServlet;
41    import org.springframework.web.servlet.support.SessionFlashMapManager;
42   
43    import static org.junit.Assert.assertEquals;
44   
45    import info.magnolia.cms.beans.config.ServerConfiguration;
46    import info.magnolia.cms.beans.config.URI2RepositoryManager;
47    import info.magnolia.cms.i18n.DefaultI18nContentSupport;
48    import info.magnolia.cms.i18n.I18nContentSupport;
49    import info.magnolia.context.MgnlContext;
50    import info.magnolia.link.AbsolutePathTransformer;
51    import info.magnolia.link.LinkTransformerManager;
52    import info.magnolia.repository.RepositoryConstants;
53    import info.magnolia.test.ComponentsTestUtil;
54    import info.magnolia.test.MgnlTestCase;
55    import info.magnolia.test.mock.MockUtil;
56    import info.magnolia.test.mock.MockWebContext;
57    import info.magnolia.test.mock.jcr.MockNode;
58    import info.magnolia.test.mock.jcr.MockSession;
59    import info.magnolia.test.mock.jcr.SessionTestUtil;
60   
61    /**
62    * TestCase for UuidRedirectViewResolver.
63    *
64    * @since 1.2.1
65    */
 
66    public class UuidRedirectViewResolverTest extends MgnlTestCase {
67   
 
68  5 toggle @Override
69    public void setUp() throws Exception {
70  5 super.setUp();
71  5 ((MockWebContext) MockUtil.getMockContext()).setContextPath("");
72  5 LinkTransformerManager linkTransformerManager = new LinkTransformerManager();
73  5 linkTransformerManager.addTransformer("absolute", new AbsolutePathTransformer());
74  5 linkTransformerManager.setAddContextPathToBrowserLinks(true);
75  5 ComponentsTestUtil.setInstance(LinkTransformerManager.class, linkTransformerManager);
76  5 ComponentsTestUtil.setInstance(URI2RepositoryManager.class, new URI2RepositoryManager());
77  5 ServerConfiguration serverConfiguration = new ServerConfiguration();
78  5 serverConfiguration.setDefaultExtension("html");
79  5 ComponentsTestUtil.setInstance(ServerConfiguration.class, serverConfiguration);
80  5 ComponentsTestUtil.setInstance(I18nContentSupport.class, new DefaultI18nContentSupport());
81    }
82   
 
83  1 toggle @Test
84    public void testRedirect() throws Exception {
85   
86  1 MockSession session = SessionTestUtil.createSession(RepositoryConstants.WEBSITE, "/foo/bar");
87  1 MockUtil.setSessionAndHierarchyManager(session);
88   
89  1 UuidRedirectViewResolver viewResolver = new UuidRedirectViewResolver();
90   
91  1 viewResolver.afterPropertiesSet();
92   
93  1 MockNode content = (MockNode) session.getNode("/foo/bar");
94  1 content.setIdentifier("abcd-1234");
95   
96  1 UuidRedirectView view = (UuidRedirectView) viewResolver.resolveViewName("website:" + content.getIdentifier(), null);
97   
98  1 assertEquals("website", view.getWorkspace());
99  1 assertEquals("abcd-1234", view.getUuid());
100   
101  1 MockHttpServletResponse response = new MockHttpServletResponse();
102  1 MockHttpServletRequest request = new MockHttpServletRequest();
103  1 request.setAttribute(DispatcherServlet.FLASH_MAP_MANAGER_ATTRIBUTE, new SessionFlashMapManager());
104    // view.render(new ModelMap(), request, response);
105    // assertEquals("/foo/bar.html", response.getRedirectedUrl());
106   
107   
108  1 request.setContextPath("/contextpath");
109  1 ((MockWebContext) MockUtil.getMockContext()).setContextPath(request.getContextPath());
110  1 response = new MockHttpServletResponse();
111   
112  1 view.render(new ModelMap(), request, response);
113  1 assertEquals("/contextpath/foo/bar.html", response.getRedirectedUrl());
114    }
115   
 
116  1 toggle @Test
117    public void testRedirectMainContent() throws Exception {
118   
119  1 MockSession session = SessionTestUtil.createSession(RepositoryConstants.WEBSITE, "/foo/bar");
120  1 MockUtil.setSessionAndHierarchyManager(session);
121   
122  1 UuidRedirectViewResolver viewResolver = new UuidRedirectViewResolver();
123   
124  1 viewResolver.afterPropertiesSet();
125   
126  1 MockNode content = (MockNode) session.getNode("/foo/bar");
127  1 content.setIdentifier("abcd-1234");
128   
129  1 MgnlContext.getAggregationState().setMainContentNode(content);
130   
131  1 UuidRedirectView view = (UuidRedirectView) viewResolver.resolveViewName("magnolia-redirect:main-content", null);
132   
133  1 assertEquals("website", view.getWorkspace());
134  1 assertEquals("abcd-1234", view.getUuid());
135   
136  1 MockHttpServletResponse response = new MockHttpServletResponse();
137  1 MockHttpServletRequest request = new MockHttpServletRequest();
138  1 request.setAttribute(DispatcherServlet.FLASH_MAP_MANAGER_ATTRIBUTE, new SessionFlashMapManager());
139  1 view.render(new ModelMap(), request, response);
140  1 assertEquals("/foo/bar.html", response.getRedirectedUrl());
141   
142   
143  1 request.setContextPath("/contextpath");
144  1 ((MockWebContext) MockUtil.getMockContext()).setContextPath(request.getContextPath());
145  1 response = new MockHttpServletResponse();
146   
147  1 view.render(new ModelMap(), request, response);
148  1 assertEquals("/contextpath/foo/bar.html", response.getRedirectedUrl());
149    }
150   
 
151  1 toggle @Test
152    public void testRedirectCurrentContent() throws Exception {
153   
154  1 MockSession session = SessionTestUtil.createSession(RepositoryConstants.WEBSITE, "/foo/bar");
155  1 MockUtil.setSessionAndHierarchyManager(session);
156   
157  1 UuidRedirectViewResolver viewResolver = new UuidRedirectViewResolver();
158   
159  1 viewResolver.afterPropertiesSet();
160   
161  1 MockNode content = (MockNode) session.getNode("/foo/bar");
162  1 content.setIdentifier("abcd-1234");
163   
164  1 MgnlContext.getAggregationState().setCurrentContentNode(content);
165   
166  1 UuidRedirectView view = (UuidRedirectView) viewResolver.resolveViewName("magnolia-redirect:current-content", null);
167   
168  1 assertEquals("website", view.getWorkspace());
169  1 assertEquals("abcd-1234", view.getUuid());
170   
171  1 MockHttpServletResponse response = new MockHttpServletResponse();
172  1 MockHttpServletRequest request = new MockHttpServletRequest();
173  1 request.setAttribute(DispatcherServlet.FLASH_MAP_MANAGER_ATTRIBUTE, new SessionFlashMapManager());
174  1 view.render(new ModelMap(), request, response);
175  1 assertEquals("/foo/bar.html", response.getRedirectedUrl());
176   
177   
178  1 request.setContextPath("/contextpath");
179  1 ((MockWebContext) MockUtil.getMockContext()).setContextPath(request.getContextPath());
180  1 response = new MockHttpServletResponse();
181   
182  1 view.render(new ModelMap(), request, response);
183  1 assertEquals("/contextpath/foo/bar.html", response.getRedirectedUrl());
184    }
185   
 
186  1 toggle @Test
187    public void testRedirectWithModelAttributesExposed() throws Exception {
188   
189  1 UuidRedirectViewResolver viewResolver = new UuidRedirectViewResolver();
190  1 viewResolver.afterPropertiesSet();
191   
192  1 MockSession session = SessionTestUtil.createSession(RepositoryConstants.WEBSITE, "/foo/bar");
193  1 MockUtil.setSessionAndHierarchyManager(session);
194  1 MockNode content = (MockNode) session.getNode("/foo/bar");
195  1 content.setIdentifier("abcd-1234");
196   
197  1 UuidRedirectView view = (UuidRedirectView) viewResolver.resolveViewName("website:" + content.getIdentifier(), null);
198   
199  1 assertEquals("website", view.getWorkspace());
200  1 assertEquals("abcd-1234", view.getUuid());
201   
202  1 MockHttpServletResponse response = new MockHttpServletResponse();
203  1 MockHttpServletRequest request = new MockHttpServletRequest();
204  1 request.setAttribute(DispatcherServlet.FLASH_MAP_MANAGER_ATTRIBUTE, new SessionFlashMapManager());
205  1 ModelMap model = new ModelMap();
206  1 model.put("attribute", "attributeValue");
207  1 view.render(model, request, response);
208   
209  1 assertEquals("/foo/bar.html?attribute=attributeValue", response.getRedirectedUrl());
210    }
211   
 
212  1 toggle @Test
213    public void testRedirectWithoutModelAttributesExposed() throws Exception {
214   
215  1 UuidRedirectViewResolver viewResolver = new UuidRedirectViewResolver();
216  1 viewResolver.setExposeModelAttributes(false);
217  1 viewResolver.afterPropertiesSet();
218   
219  1 MockSession session = SessionTestUtil.createSession(RepositoryConstants.WEBSITE, "/foo/bar");
220  1 MockUtil.setSessionAndHierarchyManager(session);
221  1 MockNode content = (MockNode) session.getNode("/foo/bar");
222  1 content.setIdentifier("abcd-1234");
223   
224  1 UuidRedirectView view = (UuidRedirectView) viewResolver.resolveViewName("website:" + content.getIdentifier(), null);
225   
226  1 assertEquals("website", view.getWorkspace());
227  1 assertEquals("abcd-1234", view.getUuid());
228   
229  1 MockHttpServletResponse response = new MockHttpServletResponse();
230  1 MockHttpServletRequest request = new MockHttpServletRequest();
231  1 request.setAttribute(DispatcherServlet.FLASH_MAP_MANAGER_ATTRIBUTE, new SessionFlashMapManager());
232  1 ModelMap model = new ModelMap();
233  1 model.put("attribute", "attributeValue");
234  1 view.render(model, request, response);
235   
236  1 assertEquals("/foo/bar.html", response.getRedirectedUrl());
237    }
238    }