rave-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mfrank...@apache.org
Subject svn commit: r1087796 [23/37] - in /incubator/rave/donations/mitre-osec: ./ conf/ db/ db/data/ db/sequences/ db/tables/ lib/ lib/apache-commons/ lib/apache-taglibs/ lib/build/ lib/build/cobertura/ lib/eclipselink/ lib/freemarker/ lib/google-collections/...
Date Fri, 01 Apr 2011 16:42:42 GMT
Added: incubator/rave/donations/mitre-osec/test/unit/java/org/mitre/portal/web/controller/PageControllerTest.java
URL: http://svn.apache.org/viewvc/incubator/rave/donations/mitre-osec/test/unit/java/org/mitre/portal/web/controller/PageControllerTest.java?rev=1087796&view=auto
==============================================================================
--- incubator/rave/donations/mitre-osec/test/unit/java/org/mitre/portal/web/controller/PageControllerTest.java (added)
+++ incubator/rave/donations/mitre-osec/test/unit/java/org/mitre/portal/web/controller/PageControllerTest.java Fri Apr  1 16:42:22 2011
@@ -0,0 +1,1227 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in
+ * compliance with the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.mitre.portal.web.controller;
+
+import org.mitre.portal.service.util.MetricsLoggingKeys;
+import org.mitre.portal.service.MetricsLoggingService;
+import java.net.MalformedURLException;
+
+import org.mitre.portal.model.*;
+import org.apache.shindig.auth.SecurityToken;
+import org.mitre.portal.web.model.JsonPageExist;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.mitre.portal.security.SecurityTokenService;
+import org.mitre.portal.security.UserService;
+import org.mitre.portal.service.GadgetService;
+import org.mitre.portal.service.PageService;
+import org.mitre.portal.service.exception.DuplicatePageNameException;
+import org.mitre.portal.service.exception.GadgetNotFoundException;
+import org.mitre.portal.service.exception.PageException;
+import org.mitre.portal.service.exception.PageLayoutNotFoundException;
+import org.mitre.portal.service.exception.PageNotFoundException;
+import org.mitre.portal.service.exception.PageTemplateNotFoundException;
+import org.mitre.portal.service.exception.RegionGadgetNotFoundException;
+import org.mitre.portal.web.model.JsonRpcResult;
+import org.mitre.portal.web.util.ModelKeys;
+import org.mitre.portal.web.util.ViewNames;
+import org.springframework.mock.web.MockHttpServletRequest;
+import org.springframework.ui.ExtendedModelMap;
+import org.springframework.ui.Model;
+import static org.junit.Assert.*;
+import static org.easymock.EasyMock.*;
+
+/**
+ *
+ * @author ACARLUCCI
+ */
+public class PageControllerTest {
+    private PageService pageService;
+    private GadgetService gadgetService;
+    private UserService userService;
+    private SecurityTokenService securityTokenService;
+    private MetricsLoggingService metricsLoggingService;
+    private PageController pageController;
+
+    private Container container;
+    private Model model;
+    private Person user;
+    private Page page;
+    private Page page2;
+    private Page page3;
+    private Region region;
+    private Region region2;
+    private Gadget gadget;
+    private List<Region> regionList;
+    private List<Page> pageList;
+    private List<PageTemplateOwnerType> pageTemplateOwnerTypeList;
+    private List<PageLayout> pageLayoutList;
+    private List<PageTemplate> pageTemplateList;
+    private List<PageTemplateGadget> pageTemplateGadgetList;
+    private List<PageTemplateRegion> pageTemplateRegionList;
+    private List<GadgetRating> gadgetRatingList;
+    private MockHttpServletRequest request;
+    private PageTemplate pageTemplate;
+    private PageLayout pageLayout;
+    private PageTemplateOwnerType pageTemplateOwnerType;
+    private PageTemplateRegion pageTemplateRegion;
+    private PageTemplateGadget pageTemplateGadget;
+    private RegionGadget regionGadget;
+    private PersonGadget personGadget;
+    private SecurityToken securityToken;
+
+    // test data
+    private final Long VALID_CONTAINER_ID = 1L;
+    private final String VALID_USER_ID = "100";
+    private final Long VALID_PAGE_ID = 2L;
+    private final String VALID_LAYOUT_NAME = "layout_1";
+    private final String VALID_LAYOUT_DISPLAY_NAME = "Three Cols";
+    private final String VALID_PAGE_TEMPLATE_OWNER_TYPE_CODE = "ORG";
+    private final Long VALID_PAGE_LAYOUT_ID = 3L;
+    private final Long VALID_PAGE_TEMPLATE_ID = 4L;
+    private final String VALID_PAGE_TEMPLATE_VALUE = "R102";
+    private final String VALID_PAGE_NAME = "test page";
+    private final Long VALID_NUM_REGIONS = 1L;
+    private final Long VALID_LAYOUT_RENDER_SEQ = 1L;
+    private final Long VALID_PAGE_TEMPLATE_GADGET_ID = 5L;
+    private final String VALID_REGION_NAME = "region_1";
+    private final String VALID_REGION_NAME2 = "region_2";
+    private final Long VALID_GADGET_ID = 6L;
+    private final Long VALID_REGION_ID = 7L;
+    private final Long VALID_REGION_ID2 = 99L;
+    private final Long VALID_REGION_GADGET_ID = 7L;
+    private final Integer VALID_REGION_GADGET_RENDER_SEQ = 1;
+    private final Long VALID_PERSON_GADGET_ID = 8L;
+    private final String VALID_SECURITY_TOKEN = "ABC123XYZ";
+    private final Long VALID_GADGET_RATING = 5L;
+    private final String REDIRECT_VIEW = "redirect:view";
+    private final String REDIRECT_VIEW_FULL_PATH = "redirect:/app/page/view";
+
+    private final Long INVALID_PAGE_ID = -1L;
+    private final String INVALID_PAGE_TEMPLATE_OWNER_TYPE_CODE = "ZZZ";
+    private final Long INVALID_PAGE_LAYOUT_ID = -3L;
+    private final Long INVALID_PAGE_TEMPLATE_ID = -4L;
+    private final String INVALID_PAGE_NAME = "duplicate_page_name";
+    private final Long INVALID_GADGET_ID = -5L;
+    private final Long INVALID_REGION_GADGET_ID = -6L;
+    private final String INVALID_REGION_NAME = "ZZQQWW";
+
+    @Before
+    public void setup() throws MalformedURLException {
+        container = new Container(VALID_CONTAINER_ID);
+        pageService = createNiceMock(PageService.class);
+        gadgetService = createNiceMock(GadgetService.class);
+        userService = createNiceMock(UserService.class);
+        securityTokenService = createNiceMock(SecurityTokenService.class);
+        metricsLoggingService = createNiceMock(MetricsLoggingService.class);
+        request = new MockHttpServletRequest();
+
+        pageController = new PageController(pageService, gadgetService, userService, securityTokenService, metricsLoggingService, container);
+        model = new ExtendedModelMap();
+
+        user = new Person();
+        user.setUserId(VALID_USER_ID);
+
+        gadget = new Gadget(VALID_GADGET_ID);
+
+        pageLayout = new PageLayout();
+        pageLayout.setLayoutName(VALID_LAYOUT_NAME);
+        pageLayout.setPageLayoutId(VALID_PAGE_LAYOUT_ID);
+        pageLayout.setNumRegions(VALID_NUM_REGIONS);
+        pageLayout.setDisplayName(VALID_LAYOUT_DISPLAY_NAME);
+        pageLayout.setRenderSeq(VALID_LAYOUT_RENDER_SEQ);
+
+        region = new Region(VALID_REGION_ID);       
+        region.setName(VALID_REGION_NAME);
+        regionList = new ArrayList<Region>();
+        regionList.add(region);
+
+        region2 = new Region(VALID_REGION_ID2);
+        region2.setName(VALID_REGION_NAME2);
+        regionList.add(region2);
+
+        page = new Page(VALID_PAGE_ID);
+        page.setPageLayout(pageLayout);
+        page.setName(VALID_PAGE_NAME);
+        page.setRegionList(regionList);
+        page.setRenderSeq(1);
+        region.setPage(page);
+        region2.setPage(page);
+
+        pageList = new ArrayList<Page>();
+        pageList.add(page);
+        page2 = new Page(22L);
+        page2.setRenderSeq(2);
+        page2.setPageLayout(pageLayout);
+        pageList.add(page2);
+        page3 = new Page(33L);
+        page3.setPageLayout(pageLayout);
+        page3.setRenderSeq(3);
+        pageList.add(page3);
+
+        pageTemplateOwnerTypeList = new ArrayList<PageTemplateOwnerType>();
+        pageTemplateOwnerType = new PageTemplateOwnerType();
+        pageTemplateOwnerType.setCode(VALID_PAGE_TEMPLATE_OWNER_TYPE_CODE);
+        pageTemplateOwnerTypeList.add(pageTemplateOwnerType);
+
+        pageLayoutList = new ArrayList<PageLayout>();
+        pageLayoutList.add(new PageLayout(VALID_PAGE_LAYOUT_ID));
+
+        pageTemplate = new PageTemplate(VALID_PAGE_TEMPLATE_ID);
+        pageTemplate.setContainer(container);
+        pageTemplate.setPageLayout(pageLayout);        
+        pageTemplate.setPageTemplateOwnerType(pageTemplateOwnerType);
+
+        pageTemplateRegion = new PageTemplateRegion();
+        pageTemplateRegion.setPageTemplateRegionId(1L);
+        pageTemplateRegionList = new ArrayList<PageTemplateRegion>();
+        pageTemplateRegionList.add(pageTemplateRegion);
+
+        gadgetRatingList = new ArrayList<GadgetRating>();
+        gadgetRatingList.add(new GadgetRating(VALID_GADGET_ID, gadget, VALID_GADGET_RATING, VALID_USER_ID));
+
+        pageTemplateGadget = new PageTemplateGadget();
+        pageTemplateGadget.setPageTemplateGadgetId(VALID_PAGE_TEMPLATE_GADGET_ID);
+        pageTemplateGadget.setPageTemplateRegion(pageTemplateRegion);
+        pageTemplateGadget.setRenderSeq(1L);
+        pageTemplateGadget.setGadget(gadget);
+
+        pageTemplateGadgetList = new ArrayList<PageTemplateGadget>();
+        pageTemplateGadgetList.add(pageTemplateGadget);
+
+        pageTemplate.setPageTemplateRegionList(pageTemplateRegionList);
+
+        pageTemplateList = new ArrayList<PageTemplate>();
+        pageTemplateList.add(pageTemplate);
+
+        personGadget = new PersonGadget(VALID_PERSON_GADGET_ID);
+        personGadget.setGadget(gadget);
+        personGadget.setUserId(VALID_USER_ID);
+        personGadget.setSecurityToken(VALID_SECURITY_TOKEN);
+
+        regionGadget = new RegionGadget(VALID_REGION_GADGET_ID);
+        regionGadget.setCollapsed(false);
+        regionGadget.setPersonGadget(personGadget);
+        regionGadget.setRegion(region);
+        regionGadget.setRenderSeq(VALID_REGION_GADGET_RENDER_SEQ);
+    }
+
+    @Test
+    public void view_validPageId_validModel_validViewName() throws PageException, PageTemplateNotFoundException, PageLayoutNotFoundException {
+        expect(userService.getCurrentAuthenticatedUser()).andReturn(user);
+        replay(userService);
+        expect(pageService.getPage(VALID_PAGE_ID)).andReturn(page);
+        expect(pageService.findPages(container, user)).andReturn(pageList);
+        expect(pageService.getAllUserSelectablePageLayouts()).andReturn(pageLayoutList);
+        replay(pageService);
+        expect(gadgetService.getGadgetRatings(VALID_USER_ID)).andReturn(gadgetRatingList);
+        replay(gadgetService);
+
+        String viewName = pageController.view(VALID_PAGE_ID, model, request);
+        assertTrue(model.asMap().containsKey(ModelKeys.CONTAINER));
+        assertEquals(VALID_CONTAINER_ID, ((Container)model.asMap().get(ModelKeys.CONTAINER)).getContainerId());
+        assertTrue(model.asMap().containsKey(ModelKeys.PAGE));
+        assertEquals(VALID_PAGE_ID, ((Page)model.asMap().get(ModelKeys.PAGE)).getPageId());
+        assertTrue(model.asMap().containsKey(ModelKeys.PAGES));
+        assertEquals(pageList.size(), ((List<Page>)model.asMap().get(ModelKeys.PAGES)).size());
+        assertEquals(ViewNames.Page.getPageView(page.getPageLayout().getLayoutName()), viewName);
+        assertTrue(model.asMap().containsKey(ModelKeys.PAGE_LAYOUTS));
+        assertEquals(pageLayoutList.size(), ((List<PageLayout>)model.asMap().get(ModelKeys.PAGE_LAYOUTS)).size());
+        assertTrue(model.asMap().containsKey(ModelKeys.LAST_VISITED_PAGE_ID));
+        assertEquals(VALID_PAGE_ID, ((Long)model.asMap().get(ModelKeys.LAST_VISITED_PAGE_ID)));        
+        assertTrue(model.asMap().containsKey(ModelKeys.USER_GADGET_RATINGS));
+        assertEquals(gadgetRatingList.size(), ((Map<String, GadgetRating>)model.asMap().get(ModelKeys.USER_GADGET_RATINGS)).size());
+    }
+
+    @Test
+    public void view_defaultPageLayout_validModel_validViewName() throws Exception {
+        page.getPageLayout().setLayoutName("default");
+
+        expect(userService.getCurrentAuthenticatedUser()).andReturn(user);
+        replay(userService);
+        expect(securityTokenService.getSecurityTokenFromEncodedEncryptedSecurityToken(VALID_SECURITY_TOKEN)).andReturn(securityToken);
+        replay(securityTokenService);
+        expect(pageService.getPage(VALID_PAGE_ID)).andReturn(page);
+        expect(pageService.findPages(container, user)).andReturn(pageList);
+        expect(pageService.getAllUserSelectablePageLayouts()).andReturn(pageLayoutList);
+        replay(pageService);
+        expect(gadgetService.getGadgetRatings(VALID_USER_ID)).andReturn(gadgetRatingList);
+        replay(gadgetService);
+
+        pageController.view(VALID_PAGE_ID, model, request);
+    }
+
+    @Test
+    public void view_nullPageIdHasLastVisitedPageId_validModel_validViewName() throws PageException, PageTemplateNotFoundException, PageLayoutNotFoundException {
+        final Long LAST_VISITED_PAGE_ID = 99L;
+        Page lastVisitedPage = new Page();
+        lastVisitedPage.setPageId(LAST_VISITED_PAGE_ID);
+        lastVisitedPage.setPageLayout(pageLayout);
+        model.addAttribute(ModelKeys.LAST_VISITED_PAGE_ID, LAST_VISITED_PAGE_ID);
+
+        expect(userService.getCurrentAuthenticatedUser()).andReturn(user);
+        replay(userService);
+        expect(pageService.getPage(LAST_VISITED_PAGE_ID)).andReturn(lastVisitedPage);
+        expect(pageService.findPages(container, user)).andReturn(pageList);
+        replay(pageService);
+        expect(gadgetService.getGadgetRatings(VALID_USER_ID)).andReturn(gadgetRatingList);
+        replay(gadgetService);
+
+        String viewName = pageController.view(null, model, request);
+        assertTrue(model.asMap().containsKey(ModelKeys.CONTAINER));
+        assertEquals(VALID_CONTAINER_ID, ((Container)model.asMap().get(ModelKeys.CONTAINER)).getContainerId());
+        assertTrue(model.asMap().containsKey(ModelKeys.PAGE));
+        assertEquals(LAST_VISITED_PAGE_ID, ((Page)model.asMap().get(ModelKeys.PAGE)).getPageId());
+        assertTrue(model.asMap().containsKey(ModelKeys.PAGES));
+        assertEquals(pageList.size(), ((List<Page>)model.asMap().get(ModelKeys.PAGES)).size());
+        assertEquals(ViewNames.Page.getPageView(page.getPageLayout().getLayoutName()), viewName);
+        assertTrue(model.asMap().containsKey(ModelKeys.LAST_VISITED_PAGE_ID));
+        assertEquals(LAST_VISITED_PAGE_ID, ((Long)model.asMap().get(ModelKeys.LAST_VISITED_PAGE_ID)));
+        assertTrue(model.asMap().containsKey(ModelKeys.USER_GADGET_RATINGS));
+        assertEquals(gadgetRatingList.size(), ((Map<String, GadgetRating>)model.asMap().get(ModelKeys.USER_GADGET_RATINGS)).size());
+    }
+
+    @Test
+    public void view_nullPageIdHasDefaultPage_validModel_validViewName() throws PageException, PageTemplateNotFoundException, PageLayoutNotFoundException {
+        expect(userService.getCurrentAuthenticatedUser()).andReturn(user);
+        replay(userService);
+        expect(pageService.findDefaultPage(container, user)).andReturn(page);
+        expect(pageService.findPages(container, user)).andReturn(pageList);
+        replay(pageService);
+        expect(gadgetService.getGadgetRatings(VALID_USER_ID)).andReturn(gadgetRatingList);
+        replay(gadgetService);
+
+        String viewName = pageController.view(null, model, request);
+        assertTrue(model.asMap().containsKey(ModelKeys.CONTAINER));
+        assertEquals(VALID_CONTAINER_ID, ((Container)model.asMap().get(ModelKeys.CONTAINER)).getContainerId());
+        assertTrue(model.asMap().containsKey(ModelKeys.PAGE));
+        assertEquals(VALID_PAGE_ID, ((Page)model.asMap().get(ModelKeys.PAGE)).getPageId());
+        assertTrue(model.asMap().containsKey(ModelKeys.PAGES));
+        assertEquals(pageList.size(), ((List<Page>)model.asMap().get(ModelKeys.PAGES)).size());
+        assertEquals(ViewNames.Page.getPageView(page.getPageLayout().getLayoutName()), viewName);
+        assertTrue(model.asMap().containsKey(ModelKeys.LAST_VISITED_PAGE_ID));
+        assertEquals(VALID_PAGE_ID, ((Long)model.asMap().get(ModelKeys.LAST_VISITED_PAGE_ID)));
+        assertTrue(model.asMap().containsKey(ModelKeys.USER_GADGET_RATINGS));
+        assertEquals(gadgetRatingList.size(), ((Map<String, GadgetRating>)model.asMap().get(ModelKeys.USER_GADGET_RATINGS)).size());
+    }
+
+    @Test
+    public void view_nullPageIdNoDefaultPage_validModel_validViewName() throws PageException, PageTemplateNotFoundException, PageLayoutNotFoundException {
+        expect(userService.getCurrentAuthenticatedUser()).andReturn(user);
+        expect(pageService.findDefaultPage(container, user)).andReturn(null);
+        expect(pageService.findPages(container, user)).andReturn(null);
+        expect(pageService.getDefaultPageTemplate()).andReturn(pageTemplate);
+        expect(userService.getCurrentAuthenticatedUser()).andReturn(user);
+        expect(pageService.getPageTemplate(VALID_PAGE_TEMPLATE_ID)).andReturn(pageTemplate);
+        expect(pageService.getPageLayout(VALID_PAGE_LAYOUT_ID)).andReturn(pageLayout);
+        expect(pageService.convert(pageTemplate, user)).andReturn(page);
+        pageService.save(page);
+        expectLastCall();
+        expect(userService.getCurrentAuthenticatedUser()).andReturn(user);
+        expect(pageService.getPage(VALID_PAGE_ID)).andReturn(page);
+        expect(pageService.findPages(container, user)).andReturn(pageList);
+        replay(userService);
+        replay(pageService);
+
+        String viewName = pageController.view(null, model, request);       
+        assertEquals(REDIRECT_VIEW_FULL_PATH, viewName);
+    }
+
+    @Test(expected = PageNotFoundException.class)
+    public void view_invalidPageId_validModel_exception() throws PageException, PageTemplateNotFoundException, PageLayoutNotFoundException {
+        expect(userService.getCurrentAuthenticatedUser()).andReturn(user);
+        replay(userService);
+        expect(pageService.getPage(INVALID_PAGE_ID)).andThrow(new PageNotFoundException(VALID_PAGE_ID));
+        expect(pageService.findPages(container, user)).andReturn(pageList);
+        replay(pageService);
+
+        String viewName = pageController.view(INVALID_PAGE_ID, model, request);
+    }
+
+    @Test(expected = NullPointerException.class)
+    public void view_validPageId_nullModel_validViewName() throws PageException, PageTemplateNotFoundException, PageLayoutNotFoundException {
+        expect(userService.getCurrentAuthenticatedUser()).andReturn(user);
+        replay(userService);
+        expect(pageService.getPage(VALID_PAGE_ID)).andReturn(page);
+        expect(pageService.findPages(container, user)).andReturn(pageList);
+        replay(pageService);
+
+        String viewName = pageController.view(VALID_PAGE_ID, null, request);
+    }    
+
+    @Test
+    public void addPageGet_validModel() {
+        expect(pageService.getAllPageTemplates()).andReturn(pageTemplateList);
+        expect(pageService.getAllUserSelectablePageLayouts()).andReturn(pageLayoutList);
+        replay(pageService);
+        String viewName = pageController.addPageGet(model);
+
+        assertTrue(model.asMap().containsKey(ModelKeys.CONTAINER));
+        assertEquals(VALID_CONTAINER_ID, ((Container)model.asMap().get(ModelKeys.CONTAINER)).getContainerId());
+        assertTrue(model.asMap().containsKey(ModelKeys.PAGE_TEMPLATES));
+        assertEquals(pageTemplateList.size(), ((List<PageTemplate>)model.asMap().get(ModelKeys.PAGE_TEMPLATES)).size());
+        assertTrue(model.asMap().containsKey(ModelKeys.PAGE_LAYOUTS));
+        assertEquals(pageLayoutList.size(), ((List<PageLayout>)model.asMap().get(ModelKeys.PAGE_LAYOUTS)).size());
+        assertEquals(ViewNames.Page.ADD_PAGE, viewName);
+    }
+
+    @Test(expected = NullPointerException.class)
+    public void addPageGet_nullModel() {
+        expect(pageService.getAllPageTemplates()).andReturn(pageTemplateList);
+        expect(pageService.getAllUserSelectablePageLayouts()).andReturn(pageLayoutList);
+        replay(pageService);
+        String viewName = pageController.addPageGet(null);
+    }
+
+    @Test
+    public void addPredefinedPagePost_validPageTemplateId() throws PageTemplateNotFoundException, PageException {
+        expect(userService.getCurrentAuthenticatedUser()).andReturn(user);
+        expect(pageService.getPageTemplate(VALID_PAGE_TEMPLATE_ID)).andReturn(pageTemplate);
+        expect(pageService.convert(pageTemplate, user)).andReturn(page);
+        pageService.save(page);
+        expectLastCall();
+        expect(userService.getCurrentAuthenticatedUser()).andReturn(user);
+        expect(pageService.getPage(VALID_PAGE_ID)).andReturn(page);
+        expect(pageService.findPages(container, user)).andReturn(pageList);
+        replay(userService);
+        replay(pageService);
+
+        String viewName = pageController.addPredefinedPagePost(request, model, VALID_PAGE_TEMPLATE_ID);
+        assertEquals(REDIRECT_VIEW_FULL_PATH, viewName);
+    }
+
+    @Test(expected = PageTemplateNotFoundException.class)
+    public void addPredefinedPagePost_invalidPageTemplateId_exception() throws PageTemplateNotFoundException, PageException {
+        expect(userService.getCurrentAuthenticatedUser()).andReturn(user);
+        expect(pageService.getPageTemplate(INVALID_PAGE_TEMPLATE_ID)).andThrow(new PageTemplateNotFoundException(INVALID_PAGE_TEMPLATE_ID));
+        replay(userService);
+        replay(pageService);
+
+        String viewName = pageController.addPredefinedPagePost(request, model, INVALID_PAGE_TEMPLATE_ID);
+    }
+
+    @Test
+    public void addPredefinedPagePost_validPageTemplateId_duplicatePageName() throws PageTemplateNotFoundException, PageException {
+        final String NEW_UNIQUE_PAGE_NAME = page.getName() + "-2";
+        final String NEW_UNIQUE_PAGE_NAME2 = page.getName() + "-3";
+
+        expect(userService.getCurrentAuthenticatedUser()).andReturn(user);
+        expect(pageService.getPageTemplate(VALID_PAGE_TEMPLATE_ID)).andReturn(pageTemplate);
+        expect(pageService.convert(pageTemplate, user)).andReturn(page);
+        // simulate two failed attempts at saving due to duplicate page names
+        pageService.save(page);
+        expectLastCall().andThrow(new DuplicatePageNameException(page.getName(), user.getUserId()));
+        expect(pageService.findPage(container, NEW_UNIQUE_PAGE_NAME, user)).andReturn(new Page());
+        expect(pageService.findPage(container, NEW_UNIQUE_PAGE_NAME2, user)).andReturn(null);
+        pageService.save(page);
+        expectLastCall();
+        expect(userService.getCurrentAuthenticatedUser()).andReturn(user);
+        expect(pageService.getPage(VALID_PAGE_ID)).andReturn(page);
+        expect(pageService.findPages(container, user)).andReturn(pageList);
+        replay(userService);
+        replay(pageService);
+
+        String viewName = pageController.addPredefinedPagePost(request, model, VALID_PAGE_TEMPLATE_ID);
+        assertEquals(REDIRECT_VIEW_FULL_PATH, viewName);
+        assertEquals(page.getName(), NEW_UNIQUE_PAGE_NAME2);
+    }
+
+    @Test
+    public void addEmptyPagePost_validPageTemplateId_validPageName_validPageLayoutId_exception() throws Exception {
+        expect(userService.getCurrentAuthenticatedUser()).andReturn(user);
+        expect(pageService.getPageTemplate(VALID_PAGE_TEMPLATE_ID)).andReturn(pageTemplate);
+        expect(pageService.getPageLayout(VALID_PAGE_LAYOUT_ID)).andReturn(pageLayout);
+        expect(pageService.convert(pageTemplate, user)).andReturn(page);
+        pageService.save(page);
+        expectLastCall();
+        expect(userService.getCurrentAuthenticatedUser()).andReturn(user);
+        expect(pageService.getPage(VALID_PAGE_ID)).andReturn(page);
+        expect(pageService.findPages(container, user)).andReturn(pageList);
+        replay(userService);
+        replay(pageService);
+
+        String viewName = pageController.addEmptyPagePost(request, model, VALID_PAGE_TEMPLATE_ID, VALID_PAGE_NAME, VALID_PAGE_LAYOUT_ID);
+        assertEquals(REDIRECT_VIEW, viewName);
+    }
+
+    @Test(expected = PageTemplateNotFoundException.class)
+    public void addEmptyPagePost_invalidPageTemplateId_validPageName_validPageLayoutId() throws Exception {
+        expect(userService.getCurrentAuthenticatedUser()).andReturn(user);
+        expect(pageService.getPageTemplate(INVALID_PAGE_TEMPLATE_ID)).andThrow(new PageTemplateNotFoundException(INVALID_PAGE_TEMPLATE_ID));
+        replay(userService);
+        replay(pageService);
+
+        String viewName = pageController.addEmptyPagePost(request, model, INVALID_PAGE_TEMPLATE_ID, VALID_PAGE_NAME, VALID_PAGE_LAYOUT_ID);
+    }
+
+    @Test(expected = PageLayoutNotFoundException.class)
+    public void addEmptyPagePost_validPageTemplateId_validPageName_invalidPageLayoutId() throws Exception {
+        expect(userService.getCurrentAuthenticatedUser()).andReturn(user);
+        expect(pageService.getPageTemplate(VALID_PAGE_TEMPLATE_ID)).andReturn(pageTemplate);
+        expect(pageService.getPageLayout(INVALID_PAGE_LAYOUT_ID)).andThrow(new PageLayoutNotFoundException(INVALID_PAGE_LAYOUT_ID));
+        replay(userService);
+        replay(pageService);
+
+        String viewName = pageController.addEmptyPagePost(request, model, VALID_PAGE_TEMPLATE_ID, VALID_PAGE_NAME, INVALID_PAGE_LAYOUT_ID);
+    }
+
+    @Test
+    public void addEmptyPagePost_validPageTemplateId_duplicatePageName_validPageLayoutId_exception() throws Exception {
+        final String NEW_UNIQUE_PAGE_NAME = page.getName() + "-2";
+        final String NEW_UNIQUE_PAGE_NAME2 = NEW_UNIQUE_PAGE_NAME + "-2";
+
+        expect(userService.getCurrentAuthenticatedUser()).andReturn(user);
+        expect(pageService.getPageTemplate(VALID_PAGE_TEMPLATE_ID)).andReturn(pageTemplate);
+        expect(pageService.getPageLayout(VALID_PAGE_LAYOUT_ID)).andReturn(pageLayout);
+        expect(pageService.convert(pageTemplate, user)).andReturn(page);
+         // simulate two failed attempts at saving due to duplicate page names
+        pageService.save(page);
+        expectLastCall().andThrow(new DuplicatePageNameException(page.getName(), user.getUserId()));
+        expect(pageService.findPage(container, NEW_UNIQUE_PAGE_NAME, user)).andReturn(new Page());
+        expect(pageService.findPage(container, NEW_UNIQUE_PAGE_NAME2, user)).andReturn(null);
+        pageService.save(page);
+        expectLastCall();
+        expect(userService.getCurrentAuthenticatedUser()).andReturn(user);
+        expect(pageService.getPage(VALID_PAGE_ID)).andReturn(page);
+        expect(pageService.findPages(container, user)).andReturn(pageList);
+        replay(userService);
+        replay(pageService);
+
+        String viewName = pageController.addEmptyPagePost(request, model, VALID_PAGE_TEMPLATE_ID, VALID_PAGE_NAME, VALID_PAGE_LAYOUT_ID);
+        assertEquals(REDIRECT_VIEW, viewName);
+    }
+
+    @Test
+    public void deletePage_validModel_validPageId_validViewName() throws PageException, PageTemplateNotFoundException, PageLayoutNotFoundException {        
+        // simulate removed page
+        pageList.remove(0);
+        Page newDefaultPage = pageList.get(0);
+
+        expect(pageService.getPage(VALID_PAGE_ID)).andReturn(page);
+        pageService.delete(page);
+        expectLastCall();
+        expect(userService.getCurrentAuthenticatedUser()).andReturn(user);
+        expect(pageService.findDefaultPage(container, user)).andReturn(newDefaultPage);
+        expect(pageService.findPages(container, user)).andReturn(pageList);
+        replay(userService);
+        replay(pageService);
+
+        String viewName = pageController.deletePage(request, model, VALID_PAGE_ID);
+        assertEquals(REDIRECT_VIEW, viewName);
+    }
+
+    @Test(expected = NullPointerException.class)
+    public void deletePage_nullModel_validPageId_exception() throws PageException, PageTemplateNotFoundException, PageLayoutNotFoundException {
+        // simulate removed page
+        pageList.remove(0);
+        Page newDefaultPage = pageList.get(0);
+
+        expect(pageService.getPage(VALID_PAGE_ID)).andReturn(page);
+        pageService.delete(page);
+        expectLastCall();
+        expect(userService.getCurrentAuthenticatedUser()).andReturn(user);
+        expect(pageService.findDefaultPage(container, user)).andReturn(newDefaultPage);
+        expect(pageService.findPages(container, user)).andReturn(pageList);
+        replay(userService);
+        replay(pageService);
+
+        String viewName = pageController.deletePage(request, null, VALID_PAGE_ID);
+    }
+
+    @Test(expected = PageNotFoundException.class)
+    public void deletePage_validModel_invalidPageId_exception() throws PageException, PageTemplateNotFoundException, PageLayoutNotFoundException {
+        expect(pageService.getPage(INVALID_PAGE_ID)).andThrow(new PageNotFoundException(INVALID_PAGE_ID));
+        replay(pageService);
+
+        String viewName = pageController.deletePage(request, model, INVALID_PAGE_ID);
+    }
+
+    @Test
+    public void addGadgetToPage_validGadgetId_validPageId_validModel() throws GadgetNotFoundException, PageNotFoundException {
+        String METRICS_TEXT = "loggedtext";
+        expect(userService.getCurrentAuthenticatedUser()).andReturn(user);
+        expect(pageService.getPage(VALID_PAGE_ID)).andReturn(page);
+        expect(gadgetService.getGadget(VALID_GADGET_ID)).andReturn(gadget);
+        pageService.add(gadget, page);
+        expectLastCall();
+        expect(metricsLoggingService.logGadgetAction(MetricsLoggingKeys.GadgetActions.ADD, user, gadget, page, null)).andReturn(METRICS_TEXT);
+        replay(userService);
+        replay(pageService);
+        replay(metricsLoggingService);
+
+        String viewName = pageController.addGadgetToPage(VALID_GADGET_ID, VALID_PAGE_ID, model);
+
+        assertTrue(model.asMap().containsKey(ModelKeys.JSON_RESULT));
+        JsonRpcResult result = ((JsonRpcResult)model.asMap().get(ModelKeys.JSON_RESULT));
+        assertTrue(result.isResult());
+        assertEquals(ViewNames.JSON, viewName);
+    }
+
+    @Test
+    public void addGadgetToPage_invalidGadgetId_validPageId_validModel() throws GadgetNotFoundException, PageNotFoundException {
+        expect(pageService.getPage(VALID_PAGE_ID)).andReturn(page);
+        expect(gadgetService.getGadget(INVALID_GADGET_ID)).andThrow(new GadgetNotFoundException(INVALID_GADGET_ID));
+        replay(pageService);
+        replay(gadgetService);
+
+        String viewName = pageController.addGadgetToPage(INVALID_GADGET_ID, VALID_PAGE_ID, model);
+
+        assertTrue(model.asMap().containsKey(ModelKeys.JSON_RESULT));
+        JsonRpcResult result = ((JsonRpcResult)model.asMap().get(ModelKeys.JSON_RESULT));
+        assertFalse(result.isResult());
+        assertEquals(GadgetNotFoundException.class.getName(), result.getConsoleMessage());
+        assertEquals(ViewNames.JSON, viewName);
+    }
+
+    @Test
+    public void addGadgetToPage_validGadgetId_invalidPageId_validModel() throws GadgetNotFoundException, PageNotFoundException {
+        expect(pageService.getPage(INVALID_PAGE_ID)).andThrow(new PageNotFoundException(INVALID_PAGE_ID));
+        expect(gadgetService.getGadget(VALID_GADGET_ID)).andReturn(gadget);
+        replay(pageService);
+        replay(gadgetService);
+
+        String viewName = pageController.addGadgetToPage(VALID_GADGET_ID, INVALID_PAGE_ID, model);
+
+        assertTrue(model.asMap().containsKey(ModelKeys.JSON_RESULT));
+        JsonRpcResult result = ((JsonRpcResult)model.asMap().get(ModelKeys.JSON_RESULT));
+        assertFalse(result.isResult());
+        assertEquals(PageNotFoundException.class.getName(), result.getConsoleMessage());
+        assertEquals(ViewNames.JSON, viewName);
+    }
+
+    @Test(expected = NullPointerException.class)
+    public void addGadgetToPage_validGadgetId_validPageId_nullModel() throws GadgetNotFoundException, PageNotFoundException {
+        expect(pageService.getPage(VALID_PAGE_ID)).andReturn(page);
+        expect(gadgetService.getGadget(VALID_GADGET_ID)).andReturn(gadget);
+        pageService.add(gadget, page);
+        expectLastCall();
+        replay(pageService);
+
+        String viewName = pageController.addGadgetToPage(VALID_GADGET_ID, VALID_PAGE_ID, null);
+    }
+
+    @Test
+    public void addGadgetToPage_validModel() throws GadgetNotFoundException, PageNotFoundException {
+        expect(pageService.getPage(VALID_PAGE_ID)).andThrow(new RuntimeException("unknown exception occured"));
+        expect(gadgetService.getGadget(VALID_GADGET_ID)).andReturn(gadget);
+        replay(pageService);
+        replay(gadgetService);
+
+        String viewName = pageController.addGadgetToPage(VALID_GADGET_ID, INVALID_PAGE_ID, model);
+
+        assertTrue(model.asMap().containsKey(ModelKeys.JSON_RESULT));
+        JsonRpcResult result = ((JsonRpcResult)model.asMap().get(ModelKeys.JSON_RESULT));
+        assertFalse(result.isResult());        
+        assertEquals(ViewNames.JSON, viewName);
+    }
+
+    @Test
+    public void moveGadget_sameRegionSameSequence_validJson_validModel() throws RegionGadgetNotFoundException, PageNotFoundException {
+        String json = "{\"pageId\":" + VALID_PAGE_ID + "," +
+                      "\"gadgetId\":" + VALID_REGION_GADGET_ID + "," +
+                      "\"regionName\":\"" + VALID_REGION_NAME + "\"," +
+                      "\"position\":" + VALID_REGION_GADGET_RENDER_SEQ +
+                      "}";
+
+        String METRICS_TEXT = "loggedtext";
+
+        expect(userService.getCurrentAuthenticatedUser()).andReturn(user);
+        expect(pageService.getRegionGadget(VALID_REGION_GADGET_ID)).andReturn(regionGadget);
+        expect(pageService.getPage(VALID_PAGE_ID)).andReturn(page);
+        expect(pageService.findRegion(VALID_REGION_NAME, page)).andReturn(region);
+        pageService.move(regionGadget, VALID_REGION_GADGET_RENDER_SEQ);
+        expectLastCall();
+        pageService.move(regionGadget, VALID_REGION_GADGET_ID, VALID_REGION_GADGET_RENDER_SEQ);
+        expectLastCall();
+        expect(metricsLoggingService.logGadgetAction(MetricsLoggingKeys.GadgetActions.MOVE, user, gadget, page, null)).andReturn(METRICS_TEXT);
+        replay(userService);
+        replay(pageService);
+        replay(metricsLoggingService);
+        
+        String viewName = pageController.moveGadget(json, model);
+        
+        assertTrue(model.asMap().containsKey(ModelKeys.JSON_RESULT));
+        JsonRpcResult result = ((JsonRpcResult)model.asMap().get(ModelKeys.JSON_RESULT));
+        assertEquals(ViewNames.JSON, viewName);
+    }
+
+    @Test
+    public void moveGadget_sameRegionNewSequence_validJson_validModel() throws RegionGadgetNotFoundException, PageNotFoundException {
+        final Integer NEW_RENDER_SEQ = 2;
+        String json = "{\"pageId\":" + VALID_PAGE_ID + "," +
+                      "\"gadgetId\":" + VALID_REGION_GADGET_ID + "," +
+                      "\"regionName\":\"" + VALID_REGION_NAME + "\"," +
+                      "\"position\":" + NEW_RENDER_SEQ +
+                      "}";
+
+        expect(pageService.getRegionGadget(VALID_REGION_GADGET_ID)).andReturn(regionGadget);
+        expect(pageService.getPage(VALID_PAGE_ID)).andReturn(page);
+        expect(pageService.findRegion(VALID_REGION_NAME, page)).andReturn(region);
+        pageService.move(regionGadget, NEW_RENDER_SEQ);
+        expectLastCall();
+        pageService.move(regionGadget, VALID_REGION_GADGET_ID, NEW_RENDER_SEQ);
+        expectLastCall();
+        replay(pageService);
+
+        String viewName = pageController.moveGadget(json, model);
+
+        assertTrue(model.asMap().containsKey(ModelKeys.JSON_RESULT));
+        JsonRpcResult result = ((JsonRpcResult)model.asMap().get(ModelKeys.JSON_RESULT));
+        assertEquals(ViewNames.JSON, viewName);
+    }
+
+    @Test
+    public void moveGadget_newRegionSamePage_validJson_validModel() throws RegionGadgetNotFoundException, PageNotFoundException {
+        String json = "{\"pageId\":" + VALID_PAGE_ID + "," +
+                      "\"gadgetId\":" + VALID_REGION_GADGET_ID + "," +
+                      "\"regionName\":'" + VALID_REGION_NAME2 + "'," +
+                      "\"position\":" + VALID_REGION_GADGET_RENDER_SEQ +
+                      "}";
+
+        expect(pageService.getRegionGadget(VALID_REGION_GADGET_ID)).andReturn(regionGadget);
+        expect(pageService.getPage(VALID_PAGE_ID)).andReturn(page);
+        expect(pageService.findRegion(VALID_REGION_NAME2, page)).andReturn(region2);
+        pageService.move(regionGadget, VALID_REGION_GADGET_RENDER_SEQ);
+        expectLastCall();
+        pageService.move(regionGadget, VALID_REGION_GADGET_ID, VALID_REGION_GADGET_RENDER_SEQ);
+        expectLastCall();
+        replay(pageService);
+        
+        String viewName = pageController.moveGadget(json, model);
+        
+        assertTrue(model.asMap().containsKey(ModelKeys.JSON_RESULT));
+        JsonRpcResult result = ((JsonRpcResult)model.asMap().get(ModelKeys.JSON_RESULT));
+        //assertTrue(result.isResult());
+        assertEquals(ViewNames.JSON, viewName);
+    }
+
+    @Test
+    public void moveGadget_invalidPageIdJson_validModel() throws RegionGadgetNotFoundException, PageNotFoundException {
+        String json = "{\"pageId\":" + INVALID_PAGE_ID + "," +
+                      "\"gadgetId\":" + VALID_REGION_GADGET_ID + "," +
+                      "\"regionName\":\"" + VALID_REGION_NAME + "\"," +
+                      "\"position\":" + VALID_REGION_GADGET_RENDER_SEQ +
+                      "}";
+
+        expect(pageService.getRegionGadget(VALID_REGION_GADGET_ID)).andReturn(regionGadget);
+        expect(pageService.getPage(INVALID_PAGE_ID)).andThrow(new PageNotFoundException(INVALID_PAGE_ID));
+        expect(pageService.findRegion(VALID_REGION_NAME, page)).andReturn(region);
+        pageService.move(regionGadget, VALID_REGION_GADGET_RENDER_SEQ);
+        expectLastCall();
+        pageService.move(regionGadget, VALID_REGION_GADGET_ID, VALID_REGION_GADGET_RENDER_SEQ);
+        expectLastCall();
+        replay(pageService);
+        
+
+        String viewName = pageController.moveGadget(json, model);
+
+        assertTrue(model.asMap().containsKey(ModelKeys.JSON_RESULT));
+        JsonRpcResult result = ((JsonRpcResult)model.asMap().get(ModelKeys.JSON_RESULT));
+        assertFalse(result.isResult());
+        assertEquals(PageNotFoundException.class.getName(), result.getConsoleMessage());
+        assertEquals(ViewNames.JSON, viewName);
+    }
+
+    @Test
+    public void moveGadget_invalidGadgetIdJson_validModel() throws RegionGadgetNotFoundException, PageNotFoundException {
+        String json = "{\"pageId\":" + VALID_PAGE_ID + "," +
+                      "\"gadgetId\":" + INVALID_REGION_GADGET_ID + "," +
+                      "\"regionName\":\"" + VALID_REGION_NAME + "\"," +
+                      "\"position\":" + VALID_REGION_GADGET_RENDER_SEQ +
+                      "}";
+
+        expect(pageService.getRegionGadget(INVALID_REGION_GADGET_ID)).andThrow(new RegionGadgetNotFoundException(INVALID_REGION_GADGET_ID));
+        expect(pageService.getPage(VALID_PAGE_ID)).andReturn(page);
+        expect(pageService.findRegion(VALID_REGION_NAME, page)).andReturn(region);
+        pageService.move(regionGadget, VALID_REGION_GADGET_RENDER_SEQ);
+        expectLastCall();
+        pageService.move(regionGadget, VALID_REGION_GADGET_ID, VALID_REGION_GADGET_RENDER_SEQ);
+        expectLastCall();
+        replay(pageService);
+
+        String viewName = pageController.moveGadget(json, model);
+
+        assertTrue(model.asMap().containsKey(ModelKeys.JSON_RESULT));
+        JsonRpcResult result = ((JsonRpcResult)model.asMap().get(ModelKeys.JSON_RESULT));
+        assertFalse(result.isResult());
+        assertEquals(RegionGadgetNotFoundException.class.getName(), result.getConsoleMessage());
+        assertEquals(ViewNames.JSON, viewName);
+    }
+
+    @Test
+    public void moveGadget_invalidRegionNameJson_validModel() throws RegionGadgetNotFoundException, PageNotFoundException {
+        String json = "{\"pageId\":" + VALID_PAGE_ID + "," +
+                      "\"gadgetId\":" + VALID_REGION_GADGET_ID + "," +
+                      "\"regionName\":\"" + INVALID_REGION_NAME + "\"," +
+                      "\"position\":" + VALID_REGION_GADGET_RENDER_SEQ +
+                      "}";
+
+        expect(pageService.getRegionGadget(VALID_REGION_GADGET_ID)).andReturn(regionGadget);
+        expect(pageService.getPage(VALID_PAGE_ID)).andReturn(page);
+        expect(pageService.findRegion(INVALID_REGION_NAME, page)).andReturn(null);
+        pageService.move(regionGadget, VALID_REGION_GADGET_RENDER_SEQ);
+        expectLastCall();
+        pageService.move(regionGadget, VALID_REGION_GADGET_ID, VALID_REGION_GADGET_RENDER_SEQ);
+        expectLastCall();
+        replay(pageService);
+        
+
+        String viewName = pageController.moveGadget(json, model);
+
+        assertTrue(model.asMap().containsKey(ModelKeys.JSON_RESULT));
+        JsonRpcResult result = ((JsonRpcResult)model.asMap().get(ModelKeys.JSON_RESULT));
+        assertFalse(result.isResult());
+        //assertEquals(RegionNotFoundException.class.getName(), result.getConsoleMessage());
+        assertEquals(ViewNames.JSON, viewName);
+    }
+
+    @Test
+    public void moveGadget_invalidToRegion_exception() throws RegionGadgetNotFoundException, PageNotFoundException {
+        String json = "{\"pageId\":" + VALID_PAGE_ID + "," +
+                      "\"gadgetId\":" + VALID_REGION_GADGET_ID + "," +
+                      "\"regionName\":\"" + INVALID_REGION_NAME + "\"," +
+                      "\"position\":" + VALID_REGION_GADGET_RENDER_SEQ +
+                      "}";
+
+        Region nullRegion = null;
+
+        expect(pageService.getRegionGadget(VALID_REGION_GADGET_ID)).andReturn(regionGadget);
+        expect(pageService.getPage(INVALID_PAGE_ID)).andThrow(new PageNotFoundException(INVALID_PAGE_ID));
+        expect(pageService.findRegion(VALID_REGION_NAME, page)).andReturn(nullRegion);        
+        replay(pageService);
+
+        String viewName = pageController.moveGadget(json, model);
+        assertTrue(model.asMap().containsKey(ModelKeys.JSON_RESULT));
+        JsonRpcResult result = ((JsonRpcResult)model.asMap().get(ModelKeys.JSON_RESULT));
+        assertFalse(result.isResult());
+        assertEquals(ViewNames.JSON, viewName);
+    }
+
+    @Test
+    public void moveGadget_JsonParseException_validModel() throws RegionGadgetNotFoundException, PageNotFoundException {
+        String json = "INVALIDJSON";
+             
+        String viewName = pageController.moveGadget(json, model);
+        assertTrue(model.asMap().containsKey(ModelKeys.JSON_RESULT));
+        JsonRpcResult result = ((JsonRpcResult)model.asMap().get(ModelKeys.JSON_RESULT));
+        assertFalse(result.isResult());
+        assertEquals(ViewNames.JSON, viewName);
+    }
+
+    @Test
+    public void moveGadget_RuntimeException_validModel() throws RegionGadgetNotFoundException, PageNotFoundException {
+        String json = "{\"pageId\":" + VALID_PAGE_ID + "," +
+                      "\"gadgetId\":" + VALID_REGION_GADGET_ID + "," +
+                      "\"regionName\":\"" + INVALID_REGION_NAME + "\"," +
+                      "\"position\":" + VALID_REGION_GADGET_RENDER_SEQ +
+                      "}";
+
+        Region nullRegion = null;
+
+        expect(pageService.getRegionGadget(VALID_REGION_GADGET_ID)).andThrow(new RuntimeException("error"));
+        expect(pageService.getPage(INVALID_PAGE_ID)).andThrow(new PageNotFoundException(INVALID_PAGE_ID));
+        expect(pageService.findRegion(VALID_REGION_NAME, page)).andReturn(nullRegion);
+        replay(pageService);
+
+        String viewName = pageController.moveGadget(json, model);
+        assertTrue(model.asMap().containsKey(ModelKeys.JSON_RESULT));
+        JsonRpcResult result = ((JsonRpcResult)model.asMap().get(ModelKeys.JSON_RESULT));
+        assertFalse(result.isResult());
+        assertEquals(ViewNames.JSON, viewName);
+    }
+
+    @Test
+    public void moveGadget_differentPage_validJson_validModel() throws RegionGadgetNotFoundException, PageNotFoundException {
+        final long NEW_PAGE_ID = 33L;
+        Page page2 = new Page(NEW_PAGE_ID);
+        page2.setPageLayout(pageLayout);
+        page2.setName("page2");
+
+        final long NEW_REGION_ID = 99L;
+        Region newRegion = new Region(NEW_REGION_ID);
+        newRegion.setName(VALID_REGION_NAME);
+        newRegion.setPage(page2);
+        ArrayList<Region> newRegionList = new ArrayList<Region>();
+        newRegionList.add(newRegion);
+        page2.setRegionList(newRegionList);
+
+        String json = "{\"pageId\":" + NEW_PAGE_ID + "," +
+                      "\"gadgetId\":" + VALID_REGION_GADGET_ID + "," +
+                      "\"regionName\":\"" + VALID_REGION_NAME + "\"," +
+                      "\"position\":" + VALID_REGION_GADGET_RENDER_SEQ +
+                      "}";
+
+        String METRICS_TEXT = "loggedtext";
+
+        expect(userService.getCurrentAuthenticatedUser()).andReturn(user);
+        expect(pageService.getRegionGadget(VALID_REGION_GADGET_ID)).andReturn(regionGadget);
+        expect(pageService.getPage(NEW_PAGE_ID)).andReturn(page2);
+        expect(pageService.findRegion(VALID_REGION_NAME, page2)).andReturn(newRegion);
+        pageService.move(regionGadget, VALID_REGION_GADGET_ID, VALID_REGION_GADGET_RENDER_SEQ);
+        expectLastCall();
+        expect(metricsLoggingService.logGadgetAction(MetricsLoggingKeys.GadgetActions.MOVE, user, gadget, page2, page)).andReturn(METRICS_TEXT);
+        replay(userService);
+        replay(pageService);
+        replay(metricsLoggingService);
+
+        String viewName = pageController.moveGadget(json, model);
+
+        assertTrue(model.asMap().containsKey(ModelKeys.JSON_RESULT));
+        JsonRpcResult result = ((JsonRpcResult)model.asMap().get(ModelKeys.JSON_RESULT));
+        //assertTrue(result.isResult());
+        assertEquals(ViewNames.JSON, viewName);
+    }
+
+    @Test
+    public void deleteGadget_validJson_validModel() throws RegionGadgetNotFoundException {
+        String json = "{\"gadgetId\":" + VALID_REGION_GADGET_ID + "}";
+        String METRICS_TEXT = "loggedtext";
+
+        expect(userService.getCurrentAuthenticatedUser()).andReturn(user);
+        expect(pageService.getRegionGadget(VALID_REGION_GADGET_ID)).andReturn(regionGadget);
+        pageService.delete(regionGadget);
+        expectLastCall();
+        expect(metricsLoggingService.logGadgetAction(MetricsLoggingKeys.GadgetActions.DELETE, user, gadget, null, regionGadget.getRegion().getPage())).andReturn(METRICS_TEXT);
+        replay(userService);
+        replay(pageService);
+        replay(metricsLoggingService);
+
+        String viewName = pageController.deleteGadget(json, model);
+      
+        assertTrue(model.asMap().containsKey(ModelKeys.JSON_RESULT));
+        JsonRpcResult result = ((JsonRpcResult)model.asMap().get(ModelKeys.JSON_RESULT));
+        assertTrue(result.isResult());
+        assertEquals(ViewNames.JSON, viewName);
+    }
+
+    @Test
+    public void deleteGadget_invalidGadgetIdJson_validModel() throws RegionGadgetNotFoundException {
+        String json = "{\"gadgetId\":" + INVALID_REGION_GADGET_ID + "}";
+
+        expect(pageService.getRegionGadget(INVALID_REGION_GADGET_ID)).andThrow(new RegionGadgetNotFoundException(INVALID_REGION_GADGET_ID));
+        pageService.delete(regionGadget);
+        expectLastCall();
+        replay(pageService);
+
+        String viewName = pageController.deleteGadget(json, model);
+
+        assertTrue(model.asMap().containsKey(ModelKeys.JSON_RESULT));
+        JsonRpcResult result = ((JsonRpcResult)model.asMap().get(ModelKeys.JSON_RESULT));
+        assertFalse(result.isResult());
+        assertEquals(RegionGadgetNotFoundException.class.getName(), result.getConsoleMessage());
+        assertEquals(ViewNames.JSON, viewName);
+    }
+
+    @Test
+    public void deleteGadget_RuntimeException_validModel() throws RegionGadgetNotFoundException {
+        String json = "{\"gadgetId\":" + INVALID_REGION_GADGET_ID + "}";
+
+        expect(pageService.getRegionGadget(INVALID_REGION_GADGET_ID)).andThrow(new RuntimeException("error"));
+        pageService.delete(regionGadget);
+        expectLastCall();
+        replay(pageService);
+
+        String viewName = pageController.deleteGadget(json, model);
+
+        assertTrue(model.asMap().containsKey(ModelKeys.JSON_RESULT));
+        JsonRpcResult result = ((JsonRpcResult)model.asMap().get(ModelKeys.JSON_RESULT));
+        assertFalse(result.isResult());
+        assertEquals(ViewNames.JSON, viewName);
+    }
+
+    @Test
+    public void renamePage_validPageId_validPageName_validModel() throws PageException {
+        expect(pageService.getPage(VALID_PAGE_ID)).andReturn(page);
+        pageService.save(page);
+        expectLastCall();
+        replay(pageService);
+
+        String viewName = pageController.renamePage(VALID_PAGE_NAME, VALID_PAGE_ID, model);
+
+        assertTrue(model.asMap().containsKey(ModelKeys.JSON_RESULT));
+        JsonRpcResult result = ((JsonRpcResult)model.asMap().get(ModelKeys.JSON_RESULT));
+        assertTrue(result.isResult());
+        assertEquals(ViewNames.JSON, viewName);
+    }
+
+    @Test
+    public void renamePage_invalidPageId_validPageName_validModel() throws PageException {
+        expect(pageService.getPage(INVALID_PAGE_ID)).andThrow(new PageNotFoundException(INVALID_PAGE_ID));
+        pageService.save(page);
+        expectLastCall();
+        replay(pageService);
+
+        String viewName = pageController.renamePage(VALID_PAGE_NAME, INVALID_PAGE_ID, model);
+
+        assertTrue(model.asMap().containsKey(ModelKeys.JSON_RESULT));
+        JsonRpcResult result = ((JsonRpcResult)model.asMap().get(ModelKeys.JSON_RESULT));
+        assertFalse(result.isResult());
+        assertEquals(PageNotFoundException.class.getName(), result.getConsoleMessage());
+        assertEquals(ViewNames.JSON, viewName);
+    }
+
+    @Test
+    public void renamePage_validPageId_duplicatePageName_validModel() throws PageException {
+        expect(pageService.getPage(VALID_PAGE_ID)).andReturn(page);
+        pageService.save(page);
+        expectLastCall().andThrow(new DuplicatePageNameException(INVALID_PAGE_NAME, page.getUserId()));
+        replay(pageService);
+
+        String viewName = pageController.renamePage(INVALID_PAGE_NAME, VALID_PAGE_ID, model);
+
+        assertTrue(model.asMap().containsKey(ModelKeys.JSON_RESULT));
+        JsonRpcResult result = ((JsonRpcResult)model.asMap().get(ModelKeys.JSON_RESULT));
+        assertFalse(result.isResult());
+        assertEquals(DuplicatePageNameException.class.getName(), result.getConsoleMessage());
+        assertEquals(ViewNames.JSON, viewName);
+    }
+
+    @Test
+    public void renamePage_RuntimeException_validModel() throws PageException {
+        expect(pageService.getPage(VALID_PAGE_ID)).andReturn(page);
+        pageService.save(page);
+        expectLastCall().andThrow(new RuntimeException("error"));
+        replay(pageService);
+
+        String viewName = pageController.renamePage(INVALID_PAGE_NAME, VALID_PAGE_ID, model);
+
+        assertTrue(model.asMap().containsKey(ModelKeys.JSON_RESULT));
+        JsonRpcResult result = ((JsonRpcResult)model.asMap().get(ModelKeys.JSON_RESULT));
+        assertFalse(result.isResult());
+        assertEquals(RuntimeException.class.getName(), result.getConsoleMessage());
+        assertEquals(ViewNames.JSON, viewName);
+    }
+
+    @Test
+    public void editPageProperties_validPageId_validPageName_validPageLayout_validModel() throws Exception {
+        expect(pageService.getPage(VALID_PAGE_ID)).andReturn(page);
+        expect(pageService.getPageLayout(VALID_PAGE_LAYOUT_ID)).andReturn(pageLayout);
+        pageService.save(page);
+        expectLastCall();
+        replay(pageService);
+
+        String viewName = pageController.editPageProperties(VALID_PAGE_ID, VALID_PAGE_NAME, VALID_PAGE_LAYOUT_ID, model, request);
+
+        assertTrue(model.asMap().containsKey(ModelKeys.JSON_RESULT));
+        JsonRpcResult result = ((JsonRpcResult)model.asMap().get(ModelKeys.JSON_RESULT));
+        assertTrue(result.isResult());
+        assertEquals(ViewNames.JSON, viewName);
+    }
+
+    @Test
+    public void editPageProperties_fixedLayout_validPageId_validPageName_validPageLayout_validModel() throws Exception {
+        page.setFixedLayout(true);
+        
+        expect(pageService.getPage(VALID_PAGE_ID)).andReturn(page);
+        expect(pageService.getPageLayout(VALID_PAGE_LAYOUT_ID)).andReturn(pageLayout);
+        pageService.save(page);
+        expectLastCall();
+        replay(pageService);
+
+        String viewName = pageController.editPageProperties(VALID_PAGE_ID, VALID_PAGE_NAME, VALID_PAGE_LAYOUT_ID, model, request);
+
+        assertTrue(model.asMap().containsKey(ModelKeys.JSON_RESULT));
+        JsonRpcResult result = ((JsonRpcResult)model.asMap().get(ModelKeys.JSON_RESULT));
+        assertTrue(result.isResult());
+        assertEquals(ViewNames.JSON, viewName);
+    }
+
+    @Test
+    public void editPageProperties_validPageId_duplicatePageName_validPageLayout_validModel() throws Exception {
+        expect(pageService.getPage(VALID_PAGE_ID)).andReturn(page);
+        expect(pageService.getPageLayout(VALID_PAGE_LAYOUT_ID)).andReturn(pageLayout);
+        pageService.save(page);
+        expectLastCall().andThrow(new DuplicatePageNameException(INVALID_PAGE_NAME, page.getUserId()));
+        replay(pageService);
+
+        String viewName = pageController.editPageProperties(VALID_PAGE_ID, INVALID_PAGE_NAME, VALID_PAGE_LAYOUT_ID, model, request);
+
+        assertTrue(model.asMap().containsKey(ModelKeys.JSON_RESULT));
+        JsonRpcResult result = ((JsonRpcResult)model.asMap().get(ModelKeys.JSON_RESULT));
+        assertFalse(result.isResult());
+        assertEquals(DuplicatePageNameException.class.getName(), result.getConsoleMessage());
+        assertEquals(ViewNames.JSON, viewName);
+    }
+
+    @Test
+    public void editPageProperties_invalidPageId_validPageName_validPageLayout_validModel() throws PageException {
+        expect(pageService.getPage(INVALID_PAGE_ID)).andThrow(new PageNotFoundException(INVALID_PAGE_ID));
+        pageService.save(page);
+        expectLastCall();
+        replay(pageService);
+
+        String viewName = pageController.editPageProperties(INVALID_PAGE_ID, VALID_PAGE_NAME, VALID_PAGE_LAYOUT_ID, model, request);
+ 
+        assertTrue(model.asMap().containsKey(ModelKeys.JSON_RESULT));
+        JsonRpcResult result = ((JsonRpcResult)model.asMap().get(ModelKeys.JSON_RESULT));
+        assertFalse(result.isResult());
+        assertEquals(PageNotFoundException.class.getName(), result.getConsoleMessage());
+        assertEquals(ViewNames.JSON, viewName);
+    }
+
+    @Test
+    public void editPageProperties_validPageId_validPageName_invalidPageLayout_validModel() throws PageException, PageLayoutNotFoundException {
+        expect(pageService.getPage(VALID_PAGE_ID)).andReturn(page);
+        expect(pageService.getPageLayout(INVALID_PAGE_LAYOUT_ID)).andThrow(new PageLayoutNotFoundException(INVALID_PAGE_LAYOUT_ID));
+        pageService.save(page);
+        expectLastCall();
+        replay(pageService);
+
+        String viewName = pageController.editPageProperties(VALID_PAGE_ID, VALID_PAGE_NAME, INVALID_PAGE_LAYOUT_ID, model, request);
+
+        assertTrue(model.asMap().containsKey(ModelKeys.JSON_RESULT));
+        JsonRpcResult result = ((JsonRpcResult)model.asMap().get(ModelKeys.JSON_RESULT));
+        assertFalse(result.isResult());
+        assertEquals(PageLayoutNotFoundException.class.getName(), result.getConsoleMessage());
+        assertEquals(ViewNames.JSON, viewName);
+    }
+
+    @Test
+    public void editPageProperties_RuntimeException_validModel() throws PageException {
+        expect(pageService.getPage(INVALID_PAGE_ID)).andThrow(new RuntimeException("error"));
+        pageService.save(page);
+        expectLastCall();
+        replay(pageService);
+
+        String viewName = pageController.editPageProperties(INVALID_PAGE_ID, VALID_PAGE_NAME, VALID_PAGE_LAYOUT_ID, model, request);
+
+        assertTrue(model.asMap().containsKey(ModelKeys.JSON_RESULT));
+        JsonRpcResult result = ((JsonRpcResult)model.asMap().get(ModelKeys.JSON_RESULT));
+        assertFalse(result.isResult());
+        assertEquals(RuntimeException.class.getName(), result.getConsoleMessage());
+        assertEquals(ViewNames.JSON, viewName);
+    }
+
+    @Test
+    public void checkExistingPageName_newPageName_validModel() {
+        final String NEW_PAGE_NAME = "A NEW TEST PAGE THAT DOESNT EXIST";
+
+        expect(userService.getCurrentAuthenticatedUser()).andReturn(user);
+        replay(userService);
+        expect(pageService.findPage(container, NEW_PAGE_NAME, user)).andReturn(null);
+        replay(pageService);
+
+        String viewName = pageController.checkExistingPageName(NEW_PAGE_NAME, model);
+
+        assertTrue(model.asMap().containsKey(ModelKeys.JSON_RESULT));
+        JsonPageExist jsonPageExist = ((JsonPageExist)model.asMap().get(ModelKeys.JSON_RESULT));
+        assertFalse(jsonPageExist.isPageExists());
+        assertEquals(NEW_PAGE_NAME, jsonPageExist.getPageName());
+        assertEquals(VALID_USER_ID, jsonPageExist.getUserId());
+        assertEquals(ViewNames.JSON, viewName);
+    }
+
+    @Test
+    public void checkExistingPageName_existingPageName_validModel() {
+        expect(userService.getCurrentAuthenticatedUser()).andReturn(user);
+        replay(userService);
+        expect(pageService.findPage(container, VALID_PAGE_NAME, user)).andReturn(page);
+        replay(pageService);
+
+        String viewName = pageController.checkExistingPageName(VALID_PAGE_NAME, model);
+
+        assertTrue(model.asMap().containsKey(ModelKeys.JSON_RESULT));
+        JsonPageExist jsonPageExist = ((JsonPageExist)model.asMap().get(ModelKeys.JSON_RESULT));
+        assertTrue(jsonPageExist.isPageExists());
+        assertEquals(VALID_PAGE_NAME, jsonPageExist.getPageName());
+        assertEquals(VALID_USER_ID, jsonPageExist.getUserId());
+        assertEquals(ViewNames.JSON, viewName);
+    }
+
+    @Test
+    public void checkExistingPageName_RuntimeException_validModel() {
+        expect(userService.getCurrentAuthenticatedUser()).andThrow(new RuntimeException("error"));
+        replay(userService);
+        expect(pageService.findPage(container, VALID_PAGE_NAME, user)).andReturn(page);
+        replay(pageService);
+
+        String viewName = pageController.checkExistingPageName(VALID_PAGE_NAME, model);
+
+        assertTrue(model.asMap().containsKey(ModelKeys.JSON_RESULT));
+        JsonPageExist jsonPageExist = ((JsonPageExist)model.asMap().get(ModelKeys.JSON_RESULT));
+        assertFalse(jsonPageExist.isPageExists());
+        assertNotNull(jsonPageExist.getErrorMessage());
+        assertEquals(ViewNames.JSON, viewName);
+    }
+
+    @Test
+    public void movePage_validToMovePageId_validMoveAfterPageId_validViewName() throws Exception {
+        final Long toMovePageId = VALID_PAGE_ID;
+        final Long moveAfterPageId = 22L;
+
+        expect(userService.getCurrentAuthenticatedUser()).andReturn(user);        
+        expect(pageService.getPage(toMovePageId)).andReturn(page);
+        expect(pageService.getPage(moveAfterPageId)).andReturn(page2);
+        expect(pageService.findPages(container, user)).andReturn(pageList);
+        pageService.updatePageRenderSequences(pageList);
+        expectLastCall();
+        replay(userService);
+        replay(pageService);
+        
+        String viewName = pageController.movePage(request, model, toMovePageId, moveAfterPageId);
+        assertEquals(REDIRECT_VIEW, viewName);
+        assertTrue(model.asMap().containsKey(ModelKeys.LAST_VISITED_PAGE_ID));
+        assertEquals(model.asMap().get(ModelKeys.LAST_VISITED_PAGE_ID), toMovePageId);
+
+        // verify the list is in the right order
+        assertEquals(moveAfterPageId, pageList.get(0).getPageId());
+        assertEquals(toMovePageId, pageList.get(1).getPageId());       
+    }
+
+    @Test
+    public void movePage_validToMovePageId_validMoveAfterPageIdToBeginning_validViewName() throws Exception {
+        final Long toMovePageId = 33L;
+        final Long moveAfterPageId = -1L;
+
+        expect(userService.getCurrentAuthenticatedUser()).andReturn(user);
+        expect(pageService.getPage(toMovePageId)).andReturn(page3);
+        expect(pageService.findPages(container, user)).andReturn(pageList);
+        pageService.updatePageRenderSequences(pageList);
+        expectLastCall();
+        replay(userService);
+        replay(pageService);
+
+        String viewName = pageController.movePage(request, model, toMovePageId, moveAfterPageId);
+        assertEquals(REDIRECT_VIEW, viewName);
+        assertTrue(model.asMap().containsKey(ModelKeys.LAST_VISITED_PAGE_ID));
+        assertEquals(model.asMap().get(ModelKeys.LAST_VISITED_PAGE_ID), toMovePageId);
+
+        // verify the list is in the right order
+        assertEquals(toMovePageId, pageList.get(0).getPageId());
+    }
+
+    @Test(expected = PageNotFoundException.class)
+    public void movePage_pageNotFoundException() throws Exception {
+        final Long toMovePageId = 33L;
+        final Long moveAfterPageId = -1L;
+
+        expect(userService.getCurrentAuthenticatedUser()).andReturn(user);
+        expect(pageService.getPage(toMovePageId)).andReturn(page3);
+        expect(pageService.findPages(container, user)).andReturn(new ArrayList());
+        replay(userService);
+        replay(pageService);
+
+        String viewName = pageController.movePage(request, model, toMovePageId, moveAfterPageId);
+    }
+}
\ No newline at end of file

Propchange: incubator/rave/donations/mitre-osec/test/unit/java/org/mitre/portal/web/controller/PageControllerTest.java
------------------------------------------------------------------------------
    svn:executable = *



Mime
View raw message