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.config;
35  
36  import static org.junit.Assert.*;
37  
38  import info.magnolia.cms.beans.config.VirtualURIMapping.MappingResult;
39  import info.magnolia.context.MgnlContext;
40  import info.magnolia.importexport.DataTransporter;
41  import info.magnolia.module.googlesitemap.GoogleSiteMapConfiguration;
42  import info.magnolia.module.googlesitemap.SiteMapTestUtil;
43  
44  import java.io.File;
45  import java.io.FileInputStream;
46  import java.io.InputStream;
47  
48  import javax.jcr.ImportUUIDBehavior;
49  
50  import org.junit.Before;
51  import org.junit.Test;
52  
53  /**
54   * Main test class for {@link SiteMapVirtualUriMapping}.
55   */
56  public class SiteMapVirtualUriMappingTest extends SiteMapTestUtil {
57  
58      private SiteMapVirtualUriMapping uriMapping;
59      private String prefix = "sitemaps";
60  
61      @Override
62      @Before
63      public void setUp() throws Exception {
64          super.setUp();
65          // Init SiteMap with Observation
66          uriMapping = new SiteMapVirtualUriMapping(MgnlContext.getSystemContext());
67          uriMapping.setPrefix(prefix);
68  
69          // Register SiteMap
70          File sitemapFile = new File(getClass().getResource("/googleSitemaps.xml").getFile());
71          InputStream inputStream = new FileInputStream(sitemapFile);
72          DataTransporter.importXmlStream(inputStream, GoogleSiteMapConfiguration.WORKSPACE, "/", "test-stream", false, ImportUUIDBehavior.IMPORT_UUID_CREATE_NEW, true, false);
73  
74          // Initial check
75          assertTrue(googleSiteMapSession.nodeExists("/test1"));
76          assertTrue(googleSiteMapSession.nodeExists("/test2"));
77      }
78  
79  
80      @Test
81      public void testSitemapPublished() throws InterruptedException {
82          // GIVEN
83          Thread.sleep(300);
84          // WHEN
85          MappingResult result = uriMapping.mapURI("/test1Uri");
86          MappingResult noResult = uriMapping.mapURI("/test1");
87  
88          // THEN
89          assertNotNull("A virtual URI was entered. This is now used to map", result);
90          assertEquals(prefix + "/test1.xml", result.getToURI());
91          assertNull("'test1' is the display name and 'test1Uri' is the virtual URI. No mapping has to be defined for the display name.", noResult);
92      }
93  
94      @Test
95      public void testSitemapNotPublished() throws InterruptedException {
96          // GIVEN
97          Thread.sleep(300);
98          // WHEN
99          MappingResult result = uriMapping.mapURI("/test2");
100 
101         // THEN
102         assertNull("'test2' is not published and should not be part of the URI map", result);
103     }
104 
105 }