View Javadoc
1   /**
2    * This file Copyright (c) 2013-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.googlesitemap.app.actions;
35  
36  import static org.junit.Assert.*;
37  
38  import info.magnolia.module.googlesitemap.SiteMapNodeTypes;
39  import info.magnolia.module.googlesitemap.SiteMapTestUtil;
40  import info.magnolia.ui.api.action.ActionExecutionException;
41  import info.magnolia.ui.vaadin.integration.jcr.JcrNewNodeAdapter;
42  import info.magnolia.ui.vaadin.integration.jcr.JcrNodeAdapter;
43  
44  import javax.jcr.Node;
45  import javax.jcr.RepositoryException;
46  
47  import org.junit.Before;
48  import org.junit.Test;
49  
50  import com.vaadin.data.util.ObjectProperty;
51  
52  /**
53   * Test class for {@link SaveSiteMapAction}.
54   */
55  public class SaveSiteMapActionTest extends SiteMapTestUtil {
56  
57      private GoogleMapEditorCallbackTest callback;
58      private GoogleMapEditorValidatorTest validator;
59      private SaveSiteMapActionDefinition definition;
60  
61      @Override
62      @Before
63      public void setUp() throws Exception {
64          super.setUp();
65          callback = new GoogleMapEditorCallbackTest();
66          validator = new GoogleMapEditorValidatorTest();
67          definition = new SaveSiteMapActionDefinition();
68          definition.setName("save");
69          definition.setLabel("label");
70      }
71  
72      @Test
73      public void executeSaveNewSiteMap() throws RepositoryException, ActionExecutionException {
74          // GIVEN
75          Node rootNode = googleSiteMapSession.getRootNode();
76          JcrNewNodeAdapter item = new JcrNewNodeAdapter(rootNode, SiteMapNodeTypes.SiteMap.NAME);
77          item.addItemProperty(SiteMapNodeTypes.SiteMap.DISPLAY_NAME, new ObjectProperty<String>("displayName"));
78          SaveSiteMapAction action = new SaveSiteMapAction(definition, item, validator, callback);
79  
80          // WHEN
81          action.execute();
82  
83          // THEN
84          assertEquals("onSuccess(save)", this.callback.getCallbackActionCalled());
85          assertTrue(googleSiteMapSession.getRootNode().hasNode("displayName"));
86      }
87  
88      @Test
89      public void executeSaveExistingSiteMap() throws RepositoryException, ActionExecutionException {
90          // GIVEN
91          Node rootNode = googleSiteMapSession.getRootNode();
92          Node displayName = rootNode.addNode("display Name", SiteMapNodeTypes.SiteMap.NAME);
93          displayName.setProperty(SiteMapNodeTypes.SiteMap.URL, "initial_url");
94          displayName.setProperty(SiteMapNodeTypes.SiteMap.DISPLAY_NAME, "display Name");
95          JcrNodeAdapter item = new JcrNodeAdapter(displayName);
96          item.addItemProperty(SiteMapNodeTypes.SiteMap.URL, new ObjectProperty<String>("changed_url"));
97          SaveSiteMapAction action = new SaveSiteMapAction(definition, item, validator, callback);
98  
99          // WHEN
100         action.execute();
101 
102         // THEN
103         assertEquals("onSuccess(save)", this.callback.getCallbackActionCalled());
104         assertTrue(googleSiteMapSession.getRootNode().hasNode("display Name"));
105         assertEquals("changed_url", displayName.getProperty(SiteMapNodeTypes.SiteMap.URL).getString());
106     }
107 
108     @Test
109     public void executeSaveExistingSiteMapWithNewName() throws RepositoryException, ActionExecutionException {
110         // GIVEN
111         Node rootNode = googleSiteMapSession.getRootNode();
112         Node displayName = rootNode.addNode("displayName", SiteMapNodeTypes.SiteMap.NAME);
113         displayName.setProperty(SiteMapNodeTypes.SiteMap.URL, "initial_url");
114         displayName.setProperty(SiteMapNodeTypes.SiteMap.TYPE, "initial_type");
115         JcrNodeAdapter item = new JcrNodeAdapter(displayName);
116         item.addItemProperty(SiteMapNodeTypes.SiteMap.URL, new ObjectProperty<String>("changed_url"));
117         item.addItemProperty(SiteMapNodeTypes.SiteMap.DISPLAY_NAME, new ObjectProperty<String>("new_displayName"));
118         SaveSiteMapAction action = new SaveSiteMapAction(definition, item, validator, callback);
119 
120         // WHEN
121         action.execute();
122 
123         // THEN
124         assertEquals("onSuccess(save)", this.callback.getCallbackActionCalled());
125         assertFalse(googleSiteMapSession.getRootNode().hasNode("displayName"));
126         assertTrue(googleSiteMapSession.getRootNode().hasNode("new_displayName"));
127         assertEquals("changed_url", displayName.getProperty(SiteMapNodeTypes.SiteMap.URL).getString());
128         assertEquals("initial_type", displayName.getProperty(SiteMapNodeTypes.SiteMap.TYPE).getString());
129     }
130 
131     @Test
132     public void executeSaveExistingSiteMapWithExistingNewName() throws RepositoryException, ActionExecutionException {
133         // GIVEN
134         Node rootNode = googleSiteMapSession.getRootNode();
135         Node displayName = rootNode.addNode("displayName", SiteMapNodeTypes.SiteMap.NAME);
136         Node existingDisplayName = rootNode.addNode("new_displayName", SiteMapNodeTypes.SiteMap.NAME);
137         displayName.setProperty(SiteMapNodeTypes.SiteMap.URL, "initial_url");
138         displayName.setProperty(SiteMapNodeTypes.SiteMap.TYPE, "initial_type");
139         JcrNodeAdapter item = new JcrNodeAdapter(displayName);
140         item.addItemProperty(SiteMapNodeTypes.SiteMap.URL, new ObjectProperty<String>("changed_url"));
141         item.addItemProperty(SiteMapNodeTypes.SiteMap.DISPLAY_NAME, new ObjectProperty<String>("new_displayName"));
142         SaveSiteMapAction action = new SaveSiteMapAction(definition, item, validator, callback);
143 
144         // WHEN
145         action.execute();
146 
147         // THEN
148         assertEquals("onSuccess(save)", this.callback.getCallbackActionCalled());
149         assertFalse(googleSiteMapSession.getRootNode().hasNode("displayName"));
150         assertFalse(existingDisplayName.hasProperty(SiteMapNodeTypes.SiteMap.URL));
151         assertTrue(googleSiteMapSession.getRootNode().hasNode("new_displayName0"));
152         assertEquals("new_displayName0", displayName.getName());
153         assertEquals("changed_url", displayName.getProperty(SiteMapNodeTypes.SiteMap.URL).getString());
154         assertEquals("initial_type", displayName.getProperty(SiteMapNodeTypes.SiteMap.TYPE).getString());
155     }
156 }