View Javadoc
1   /**
2    * This file Copyright (c) 2017 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.mapping;
35  
36  import static org.junit.Assert.*;
37  
38  import info.magnolia.context.SystemContext;
39  import info.magnolia.importexport.DataTransporter;
40  import info.magnolia.module.googlesitemap.GoogleSiteMapConfiguration;
41  import info.magnolia.module.googlesitemap.SiteMapTestUtil;
42  import info.magnolia.objectfactory.Components;
43  import info.magnolia.virtualuri.VirtualUriMapping;
44  
45  import java.io.FileInputStream;
46  import java.net.URI;
47  import java.util.Optional;
48  
49  import javax.jcr.ImportUUIDBehavior;
50  
51  import org.junit.Before;
52  import org.junit.Test;
53  
54  public class SiteMapVirtualUriMappingTest extends SiteMapTestUtil {
55  
56      private String prefix = "sitemaps";
57      private SiteMapVirtualUriMapping mapping;
58  
59      @Before
60      @Override
61      public void setUp() throws Exception {
62          super.setUp();
63          mapping = new SiteMapVirtualUriMapping(Components.getComponent(SystemContext.class));
64          mapping.setPrefix(prefix);
65  
66          FileInputStream fis = new FileInputStream(getClass().getResource("/googleSitemaps.xml").getFile());
67          DataTransporter.importXmlStream(fis, GoogleSiteMapConfiguration.WORKSPACE, "/", "test-stream", false, ImportUUIDBehavior.IMPORT_UUID_CREATE_NEW, true, false);
68  
69          // Initial check
70          assertTrue(googleSiteMapSession.nodeExists("/test1"));
71          assertTrue(googleSiteMapSession.nodeExists("/test2"));
72      }
73  
74      @Test
75      public void sitemapCanPublish() throws InterruptedException {
76          // GIVEN
77          Thread.sleep(300);
78          // WHEN
79          Optional<VirtualUriMapping.Result> result = mapping.mapUri(URI.create("/test1Uri"));
80          Optional<VirtualUriMapping.Result> noResult = mapping.mapUri(URI.create("/test1"));
81  
82          // THEN
83          assertEquals(prefix + "/test1.xml", result.get().getToUri());
84          assertEquals("/test1Uri".length(), result.get().getWeight());
85          assertTrue(mapping.isValid());
86          assertFalse("'test1' is the display name and 'test1Uri' is the virtual URI. No mapping has to be defined for the display name.", noResult.isPresent());
87      }
88  
89      @Test
90      public void sitemapCanNotPublish() throws InterruptedException {
91          // GIVEN
92          Thread.sleep(300);
93          // WHEN
94          Optional<VirtualUriMapping.Result> result = mapping.mapUri(URI.create("/test2"));
95  
96          // THEN
97          assertFalse("'test2' is not published and should not be part of the URI map", result.isPresent());
98      }
99  
100 }