View Javadoc
1   /**
2    * This file Copyright (c) 2013 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.app.subapp.sitemapdetail.actions;
35  
36  import static org.junit.Assert.*;
37  import static org.mockito.Mockito.*;
38  
39  import info.magnolia.context.MgnlContext;
40  import info.magnolia.jcr.util.NodeTypes;
41  import info.magnolia.module.googlesitemap.GoogleSiteMapConfiguration;
42  import info.magnolia.module.googlesitemap.SiteMapNodeTypes;
43  import info.magnolia.module.googlesitemap.SiteMapTestUtil;
44  import info.magnolia.module.googlesitemap.bean.SiteMapEntry;
45  import info.magnolia.module.googlesitemap.service.SiteMapService;
46  import info.magnolia.repository.RepositoryConstants;
47  import info.magnolia.ui.api.action.ActionExecutionException;
48  
49  import javax.jcr.Node;
50  import javax.jcr.RepositoryException;
51  import javax.jcr.Session;
52  
53  import org.junit.Before;
54  import org.junit.Test;
55  
56  import com.vaadin.data.util.BeanItem;
57  
58  /**
59   * test class for {@link SaveSiteMapEntryDialogAction}.
60   */
61  public class SaveSiteMapEntryDialogActionTest extends SiteMapTestUtil {
62      private GoogleMapEditorCallbackTest callback;
63      private GoogleMapEditorValidatorTest validator;
64      private SaveSiteMapEntryDialogActionDefinition definition;
65      private SiteMapService service;
66  
67      private Node page;
68      private SiteMapEntry entry;
69  
70      @Override
71      @Before
72      public void setUp() throws Exception {
73          super.setUp();
74          callback = new GoogleMapEditorCallbackTest();
75          validator = new GoogleMapEditorValidatorTest();
76          definition = new SaveSiteMapEntryDialogActionDefinition();
77          definition.setName("save");
78          definition.setLabel("label");
79  
80          // Init page
81          Session webSiteSession = MgnlContext.getJCRSession(RepositoryConstants.WEBSITE);
82          this.page = webSiteSession.getRootNode().addNode("page", NodeTypes.Page.NAME);
83          NodeTypes.LastModified.update(page);
84          this.entry = new SiteMapEntry(new GoogleSiteMapConfiguration(), null, this.page, 1, "weekly", 0.5d);
85  
86          service = mock(SiteMapService.class);
87          when(service.updatePageNode(entry)).thenCallRealMethod();
88      }
89  
90      @Test
91      public void executeSaveEmptyNode() throws RepositoryException, ActionExecutionException {
92          // GIVEN
93          BeanItem<SiteMapEntry> item = new BeanItem<SiteMapEntry>(entry);
94          entry.setChangefreq("Daly");
95          entry.setPriority(0.2d);
96          entry.setHide(true);
97  
98          SaveSiteMapEntryDialogAction action = new SaveSiteMapEntryDialogAction(definition, item, validator, callback, service);
99          // WHEN
100         action.execute();
101 
102         // THEN
103         assertTrue(page.hasProperty(SiteMapNodeTypes.GoogleSiteMap.SITEMAP_CHANGEFREQ));
104         assertEquals("Daly", page.getProperty(SiteMapNodeTypes.GoogleSiteMap.SITEMAP_CHANGEFREQ).getString());
105         assertTrue(page.hasProperty(SiteMapNodeTypes.GoogleSiteMap.SITEMAP_PRIORITY));
106         assertEquals(0.2d, page.getProperty(SiteMapNodeTypes.GoogleSiteMap.SITEMAP_PRIORITY).getDouble(), 0);
107         assertTrue(page.hasProperty(SiteMapNodeTypes.GoogleSiteMap.SITEMAP_HIDE_IN_GOOGLESITEMAP));
108         assertEquals(true, page.getProperty(SiteMapNodeTypes.GoogleSiteMap.SITEMAP_HIDE_IN_GOOGLESITEMAP).getBoolean());
109         assertTrue(page.hasProperty(SiteMapNodeTypes.GoogleSiteMap.SITEMAP_HIDE_IN_GOOGLESITEMAP_CHILDREN));
110         assertEquals(false, page.getProperty(SiteMapNodeTypes.GoogleSiteMap.SITEMAP_HIDE_IN_GOOGLESITEMAP_CHILDREN).getBoolean());
111     }
112 
113     @Test
114     public void executeSaveExistingNode() throws RepositoryException, ActionExecutionException {
115         // GIVEN
116         page.setProperty(SiteMapNodeTypes.GoogleSiteMap.SITEMAP_CHANGEFREQ, "Monthly");
117         page.setProperty(SiteMapNodeTypes.GoogleSiteMap.SITEMAP_PRIORITY, 0.9d);
118         page.setProperty(SiteMapNodeTypes.GoogleSiteMap.SITEMAP_HIDE_IN_GOOGLESITEMAP_CHILDREN, true);
119         page.getSession().save();
120         BeanItem<SiteMapEntry> item = new BeanItem<SiteMapEntry>(entry);
121         entry.setChangefreq("Daly");
122         entry.setPriority(0.2d);
123         entry.setHide(true);
124 
125         SaveSiteMapEntryDialogAction action = new SaveSiteMapEntryDialogAction(definition, item, validator, callback, service);
126 
127         // WHEN
128         action.execute();
129 
130         // THEN
131         assertTrue(page.hasProperty(SiteMapNodeTypes.GoogleSiteMap.SITEMAP_CHANGEFREQ));
132         assertEquals("Daly", page.getProperty(SiteMapNodeTypes.GoogleSiteMap.SITEMAP_CHANGEFREQ).getString());
133         assertTrue(page.hasProperty(SiteMapNodeTypes.GoogleSiteMap.SITEMAP_PRIORITY));
134         assertEquals(0.2d, page.getProperty(SiteMapNodeTypes.GoogleSiteMap.SITEMAP_PRIORITY).getDouble(), 0);
135         assertTrue(page.hasProperty(SiteMapNodeTypes.GoogleSiteMap.SITEMAP_HIDE_IN_GOOGLESITEMAP));
136         assertEquals(true, page.getProperty(SiteMapNodeTypes.GoogleSiteMap.SITEMAP_HIDE_IN_GOOGLESITEMAP).getBoolean());
137         assertTrue(page.hasProperty(SiteMapNodeTypes.GoogleSiteMap.SITEMAP_HIDE_IN_GOOGLESITEMAP_CHILDREN));
138     }
139 
140     @Test
141     public void executeSaveDefaultValue() throws RepositoryException, ActionExecutionException {
142         // GIVEN
143         BeanItem<SiteMapEntry> item = new BeanItem<SiteMapEntry>(entry);
144 
145         SaveSiteMapEntryDialogAction action = new SaveSiteMapEntryDialogAction(definition, item, validator, callback, service);
146 
147         // WHEN
148         action.execute();
149 
150         // THEN
151         assertTrue(page.hasProperty(SiteMapNodeTypes.GoogleSiteMap.SITEMAP_CHANGEFREQ));
152         assertEquals("weekly", page.getProperty(SiteMapNodeTypes.GoogleSiteMap.SITEMAP_CHANGEFREQ).getString());
153         assertTrue(page.hasProperty(SiteMapNodeTypes.GoogleSiteMap.SITEMAP_PRIORITY));
154         assertEquals(0.5d, page.getProperty(SiteMapNodeTypes.GoogleSiteMap.SITEMAP_PRIORITY).getDouble(), 0);
155         assertTrue(page.hasProperty(SiteMapNodeTypes.GoogleSiteMap.SITEMAP_HIDE_IN_GOOGLESITEMAP));
156         assertEquals(false, page.getProperty(SiteMapNodeTypes.GoogleSiteMap.SITEMAP_HIDE_IN_GOOGLESITEMAP).getBoolean());
157         assertTrue(page.hasProperty(SiteMapNodeTypes.GoogleSiteMap.SITEMAP_HIDE_IN_GOOGLESITEMAP_CHILDREN));
158         assertEquals(false, page.getProperty(SiteMapNodeTypes.GoogleSiteMap.SITEMAP_HIDE_IN_GOOGLESITEMAP_CHILDREN).getBoolean());
159     }
160 }