/* Copyright 2006 - 2011 Under Dusken Licensed 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 no.dusken.aranea.spring; import no.dusken.aranea.admin.listener.PageChangeSectionListener; import no.dusken.aranea.model.*; import no.dusken.aranea.service.PageService; import no.dusken.aranea.service.SectionPageService; import no.dusken.common.event.PreSaveEvent; import org.junit.Before; import org.junit.Test; import org.mockito.invocation.InvocationOnMock; import org.mockito.stubbing.Answer; import static junit.framework.Assert.assertEquals; import static org.mockito.Mockito.*; /** * @author Marvin B. Lillehaug <lillehau@underdusken.no> */ public class PageChangeSectionListenerTest { private PageChangeSectionListener pageChangeSectionListener; private SectionPageService sectionPageService; private PageService pageService; @Before public void setUp() throws Exception { pageChangeSectionListener = new PageChangeSectionListener(); sectionPageService = mock(SectionPageService.class); pageService = mock(PageService.class); pageChangeSectionListener.setPageService(pageService); pageChangeSectionListener.setSectionPageService(sectionPageService); } @Test public void testNothingIsDoneIfNotPage(){ PreSaveEvent event = new PreSaveEvent(new Image()); pageChangeSectionListener.onApplicationEvent(event); verifyZeroInteractions(sectionPageService); } @Test public void testNothingIsDoneIfSectionNotChanged(){ Section originalSection = new Section(); final Section newSection = originalSection; Page p = new Article(); p.setParent(newSection); p.setID(1L); Page persistedPage = new Article(); persistedPage.setID(1L); persistedPage.setParent(originalSection); PreSaveEvent event = new PreSaveEvent(p); SectionPage sectionPage = new SectionPage(originalSection, persistedPage, 1, ""); when(pageService.getEntity(1L)).thenReturn(persistedPage); pageChangeSectionListener.onApplicationEvent(event); verifyZeroInteractions(sectionPageService); } @Test public void testSectionPageIsChangedWhenSectionIsChanged(){ Section originalSection = new Section(12L); final Section newSection = new Section(14L); Page p = new Article(); p.setParent(newSection); p.setID(1L); Page persistedPage = new Article(); persistedPage.setID(1L); persistedPage.setParent(originalSection); PreSaveEvent event = new PreSaveEvent(p); SectionPage sectionPage = new SectionPage(originalSection, persistedPage, 1, ""); when(pageService.getEntity(1L)).thenReturn(persistedPage); when(sectionPageService.getSectionPageBySectionAndPage(originalSection, persistedPage)).thenReturn(sectionPage); pageChangeSectionListener.onApplicationEvent(event); verify(sectionPageService).saveOrUpdate(any(SectionPage.class)); when(sectionPageService.saveOrUpdate(sectionPage)).thenAnswer(new Answer<SectionPage>() { @Override public SectionPage answer(InvocationOnMock invocation) throws Throwable { SectionPage sectionPage = (SectionPage) invocation.getArguments()[0]; assertEquals("Section was not changed", newSection, sectionPage.getSection()); return null; } }); } @Test public void testNoSectionPageExist(){ Section originalSection = new Section(12L); final Section newSection = new Section(14L); Page p = new Article(); p.setParent(newSection); p.setID(1L); Page persistedPage = new Article(); persistedPage.setID(1L); persistedPage.setParent(originalSection); PreSaveEvent event = new PreSaveEvent(p); when(pageService.getEntity(1L)).thenReturn(persistedPage); when(sectionPageService.getSectionPageBySectionAndPage(originalSection, persistedPage)).thenReturn(null); pageChangeSectionListener.onApplicationEvent(event); } }