package no.dusken.aranea.admin.control; import no.dusken.aranea.model.ExtendedPage; import no.dusken.aranea.model.Page; import no.dusken.aranea.model.Section; import no.dusken.aranea.model.SectionPage; import no.dusken.aranea.service.PageService; import no.dusken.aranea.service.SectionPageService; import no.dusken.aranea.service.SectionService; import no.dusken.common.exception.PageNotFoundException; import org.junit.Before; import org.junit.Test; import org.mockito.ArgumentCaptor; import org.springframework.ui.ExtendedModelMap; import org.springframework.ui.Model; import java.util.LinkedList; import java.util.List; import java.util.Set; import static junit.framework.Assert.*; import static org.mockito.Matchers.any; import static org.mockito.Mockito.*; /** * @author Marvin B. Lillehaug <lillehau@underdusken.no> */ public class TestSectionPageController { private SectionPageController controller; private SectionPageService sectionPageService; private SectionService sectionService; private PageService pageService; @Before public void onSetup(){ sectionPageService = mock(SectionPageService.class); sectionService = mock(SectionService.class); pageService = mock(PageService.class); controller = new SectionPageController(); controller.setSectionPageService(sectionPageService); controller.setSectionService(sectionService); controller.setPageService(pageService); } @Test public void testPrioritizeUp() throws PageNotFoundException { Section s = new Section(); SectionPage sp1 = new SectionPage(s, null, 2, ""); SectionPage sp2 = new SectionPage(s, null, 1, ""); when(sectionPageService.getEntity(1L)).thenReturn(sp1); when(sectionPageService.getSectionPageBySectionAndOrdering(s, 1)).thenReturn(sp2); controller.prioritizeUp(1L); assertEquals("SectionPage did not get reprioritized", new Integer(1), sp1.getOrdering()); assertEquals("SectionPage did not get reprioritized", new Integer(2), sp2.getOrdering()); verify(sectionPageService).saveOrUpdateSectionPages((List<SectionPage>) any()); } @Test public void testPrioritizeUpWhenHasHighestPri() throws PageNotFoundException { Section s = new Section(); SectionPage sp2 = new SectionPage(s, null, 1, ""); when(sectionPageService.getEntity(1L)).thenReturn(sp2); controller.prioritizeUp(1L); assertEquals("SectionPage should not get reprioritized", new Integer(1), sp2.getOrdering()); verify(sectionPageService, times(0)).saveOrUpdateSectionPages((List<SectionPage>) any()); verify(sectionPageService, times(0)).saveOrUpdate((SectionPage) any()); } @Test public void testPrioritizeUpWhenHasLowestPri() throws PageNotFoundException { Section s = new Section(); SectionPage sp2 = new SectionPage(s, null, 1, ""); when(sectionPageService.getEntity(1L)).thenReturn(sp2); controller.prioritizeDown(1L); assertEquals("SectionPage should not get reprioritized", new Integer(1), sp2.getOrdering()); verify(sectionPageService, times(0)).saveOrUpdateSectionPages((List<SectionPage>) any()); verify(sectionPageService, times(0)).saveOrUpdate((SectionPage) any()); } @Test public void testPrioritizedown() throws PageNotFoundException { Section s = new Section(); SectionPage sp1 = new SectionPage(s, null, 2, ""); SectionPage sp2 = new SectionPage(s, null, new Integer(1), ""); when(sectionPageService.getEntity(2L)).thenReturn(sp2); when(sectionPageService.getSectionPageBySectionAndOrdering(s, 2)).thenReturn(sp1); controller.prioritizeDown(2L); assertEquals("SectionPage did not get reprioritized", new Integer(1), sp1.getOrdering()); assertEquals("SectionPage did not get reprioritized", new Integer(2), sp2.getOrdering()); verify(sectionPageService).saveOrUpdateSectionPages((List<SectionPage>) any()); } @Test public void testSetPriority(){ Section s = new Section(); SectionPage sp1 = new SectionPage(s, null, 1, "1"); SectionPage sp2 = new SectionPage(s, null, 2, "2"); SectionPage sp3 = new SectionPage(s, null, 3, "3"); SectionPage sp4 = new SectionPage(s, null, 4, "4"); sp1.setID(1L); sp2.setID(2L); sp3.setID(3L); sp4.setID(4L); List<SectionPage> sps = new LinkedList<SectionPage>(); sps.add(sp1); sps.add(sp2); sps.add(sp3); sps.add(sp4); when(sectionPageService.getSectionPageByOwner(s)).thenReturn(sps); when(sectionPageService.getEntity(4L)).thenReturn(sp4); controller.setPriority(4L, 1); ArgumentCaptor<List> argument = ArgumentCaptor.forClass(List.class); verify(sectionPageService).cascadeAndSaveOrUpdateSectionPages((List<SectionPage>) argument.capture()); SectionPage actual = (SectionPage) argument.getValue().get(0); assertEquals("Wrong page as 1", sp4, actual); assertEquals("Wrong ordering", 1, actual.getOrdering().intValue()); } @Test public void testSetPriority2(){ Section s = new Section(); SectionPage sp1 = new SectionPage(s, null, 1, ""); SectionPage sp2 = new SectionPage(s, null, 2, ""); SectionPage sp3 = new SectionPage(s, null, 3, ""); SectionPage sp4 = new SectionPage(s, null, 4, ""); sp1.setID(1L); sp2.setID(2L); sp3.setID(3L); sp4.setID(4L); List<SectionPage> sps = new LinkedList<SectionPage>(); sps.add(sp1); sps.add(sp2); sps.add(sp3); sps.add(sp4); when(sectionPageService.getSectionPageByOwner(s)).thenReturn(sps); when(sectionPageService.getEntity(1L)).thenReturn(sp1); controller.setPriority(1L, 4); ArgumentCaptor<List> argument = ArgumentCaptor.forClass(List.class); verify(sectionPageService).cascadeAndSaveOrUpdateSectionPages((List<SectionPage>) argument.capture()); SectionPage actual = (SectionPage) argument.getValue().get(3); assertEquals("Wrong page as 4", sp1, actual); assertEquals("Wrong ordering", 4, actual.getOrdering().intValue()); } @Test public void testSetPriority3(){ Section s = new Section(); SectionPage sp1 = new SectionPage(s, new ExtendedPage(), 1, ""); SectionPage sp2 = new SectionPage(s, new ExtendedPage(), 2, ""); SectionPage sp3 = new SectionPage(s, new ExtendedPage(), 3, ""); SectionPage sp4 = new SectionPage(s, new ExtendedPage(), 4, ""); sp1.setID(1L); sp2.setID(2L); sp3.setID(3L); sp4.setID(4L); List<SectionPage> sps = new LinkedList<SectionPage>(); sps.add(sp1); sps.add(sp2); sps.add(sp3); sps.add(sp4); when(sectionPageService.getSectionPageByOwner(s)).thenReturn(sps); when(sectionPageService.getEntity(3L)).thenReturn(sp3); controller.setPriority(3L, 2); ArgumentCaptor<List> argument = ArgumentCaptor.forClass(List.class); verify(sectionPageService).cascadeAndSaveOrUpdateSectionPages((List<SectionPage>) argument.capture()); SectionPage actual = (SectionPage) argument.getValue().get(1); assertEquals("Wrong page as 2", sp3, actual); assertEquals("Wrong ordering", 2, actual.getOrdering().intValue()); } @Test public void testSetPriority4(){ Section s = new Section(); SectionPage sp1 = new SectionPage(s, null, 1, ""); SectionPage sp2 = new SectionPage(s, null, 2, ""); SectionPage sp3 = new SectionPage(s, null, 3, ""); SectionPage sp4 = new SectionPage(s, null, 4, ""); sp1.setID(1L); sp2.setID(2L); sp3.setID(3L); sp4.setID(4L); List<SectionPage> sps = new LinkedList<SectionPage>(); sps.add(sp1); sps.add(sp2); sps.add(sp3); sps.add(sp4); when(sectionPageService.getSectionPageByOwner(s)).thenReturn(sps); when(sectionPageService.getEntity(3L)).thenReturn(sp3); controller.setPriority(3L, 1); ArgumentCaptor<List> argument = ArgumentCaptor.forClass(List.class); verify(sectionPageService).cascadeAndSaveOrUpdateSectionPages((List<SectionPage>) argument.capture()); SectionPage actual = (SectionPage) argument.getValue().get(0); assertEquals("Wrong page as 1", sp3, actual); assertEquals("Wrong ordering", 1, actual.getOrdering().intValue()); } @Test public void testSetPriorityNegative(){ Section s = new Section(); SectionPage sp1 = new SectionPage(s, null, 1, ""); SectionPage sp2 = new SectionPage(s, null, 2, ""); SectionPage sp3 = new SectionPage(s, null, 3, ""); SectionPage sp4 = new SectionPage(s, null, 4, ""); sp1.setID(1L); sp2.setID(2L); sp3.setID(3L); sp4.setID(4L); List<SectionPage> sps = new LinkedList<SectionPage>(); sps.add(sp1); sps.add(sp2); sps.add(sp3); sps.add(sp4); when(sectionPageService.getSectionPageByOwner(s)).thenReturn(sps); when(sectionPageService.getEntity(3L)).thenReturn(sp3); controller.setPriority(3L, -11); assertEquals("SectionPage has wrong ordering", new Integer(1), sp1.getOrdering()); assertEquals("SectionPage has wrong ordering", new Integer(2), sp2.getOrdering()); assertEquals("SectionPage has wrong ordering", new Integer(3), sp3.getOrdering()); assertEquals("SectionPage has wrong ordering", new Integer(4), sp4.getOrdering()); } @Test public void testSetpriorityLowerThanLowestSectionPage(){ Section s = new Section(); SectionPage sp1 = new SectionPage(s, null, 1, ""); SectionPage sp2 = new SectionPage(s, null, 2, ""); SectionPage sp3 = new SectionPage(s, null, 3, ""); SectionPage sp4 = new SectionPage(s, null, 4, ""); sp1.setID(1L); sp2.setID(2L); sp3.setID(3L); sp4.setID(4L); List<SectionPage> sps = new LinkedList<SectionPage>(); sps.add(sp1); sps.add(sp2); sps.add(sp3); sps.add(sp4); when(sectionPageService.getSectionPageByOwner(s)).thenReturn(sps); when(sectionPageService.getEntity(3L)).thenReturn(sp3); controller.setPriority(3L, 5); assertEquals("SectionPage has wrong ordering", new Integer(1), sp1.getOrdering()); assertEquals("SectionPage has wrong ordering", new Integer(2), sp2.getOrdering()); assertEquals("SectionPage has wrong ordering", new Integer(3), sp3.getOrdering()); assertEquals("SectionPage has wrong ordering", new Integer(4), sp4.getOrdering()); } @Test public void testGetAllowedSectionsForPage(){ Page p = new ExtendedPage(); Section s = new Section(3L); p.setParent(s); Section s2 = new Section(14L); s.setParent(s2); when(pageService.getEntity(1L)).thenReturn(p); when(sectionService.getSectionByUrl("frontpage", null)).thenReturn(new Section(15L)); Model model = new ExtendedModelMap(); controller.addSectionPage(1L, model); Set<Section> allowedSections = (Set<Section>) model.asMap().get("sections"); assertNotNull("Section list was null", allowedSections); assertTrue("Did not contain s", allowedSections.contains(s)); assertTrue("Did not contain s2", allowedSections.contains(s2)); assertEquals("Wrong size", 3, allowedSections.size()); } @Test public void testAddSectionPage(){ Section s = new Section(); List<SectionPage> sps = new LinkedList<SectionPage>(); sps.add(new SectionPage()); sps.add(new SectionPage()); s.setPages(sps); Page p = new ExtendedPage(); p.setParent(s); when(sectionService.getEntity(1L)).thenReturn(s); when(pageService.getEntity(1L)).thenReturn(p); when(sectionPageService.getSectionPageByOwner(s)).thenReturn(sps); ArgumentCaptor<SectionPage> saved = ArgumentCaptor.forClass(SectionPage.class); controller.handleAddSectionPage(1L, 1L); verify(sectionPageService).saveOrUpdate(saved.capture()); assertNotNull("No object saved", saved); assertEquals("Wrong section", s, saved.getValue().getSection()); assertEquals("Wrong page", p, saved.getValue().getPage()); assertEquals("Wrong ordering", new Integer(3), saved.getValue().getOrdering()); } @Test public void testAddIllegalSectionPage(){ Page p = new ExtendedPage(); Section s = new Section(1L); p.setParent(s); Section s2 = new Section(2L); s.setParent(s2); when(pageService.getEntity(1L)).thenReturn(p); when(sectionService.getEntity(1L)).thenReturn(new Section(3L)); controller.handleAddSectionPage(1L, 1L); verify(sectionPageService, times(0)).saveOrUpdate(any(SectionPage.class)); } @Test /** * Hvis det ligger to kultursaker på topp to, og jeg synkroniserer nummer to (etter å synkronisert nummer en) vil jeg altså at den skal være nummer to også på seksjonen. * Når en sak ligger som nr to på forsiden skal den, når den blir synkronister, legge seg som nr to i seksjonen sin. Saken som var nr én skal fortsatt være nr én og * sakene som lå som andre og utover skal forskyves. */ public void testSynchronizeFrontPageWithSection(){ Section frontPage = new Section(1L); Section s = new Section(2L); s.setParent(frontPage); Page p = new ExtendedPage(); p.setParent(s); SectionPage sp = new SectionPage(frontPage, p, 1, ""); sp.setID(5L); when(sectionPageService.getEntity(1L)).thenReturn(sp); ArgumentCaptor<List> argument = ArgumentCaptor.forClass(List.class); List<SectionPage> pages = new LinkedList<SectionPage>(); pages.add(new SectionPage(s, new ExtendedPage(), 1, "1")); pages.add(new SectionPage(s, new ExtendedPage(), 2, "2")); pages.add(new SectionPage(s, new ExtendedPage(), 3, "3")); pages.add(new SectionPage(s, new ExtendedPage(), 4, "4")); when(sectionPageService.getSectionPageByOwner(s)).thenReturn(pages); s.setPages(pages); pages.get(0).setID(1L); pages.get(1).setID(2L); pages.get(2).setID(3L); pages.get(3).setID(4L); controller.synchronizeSection(1L); verify(sectionPageService).cascadeAndSaveOrUpdateSectionPages(argument.capture()); List<SectionPage> arg = argument.getValue(); assertEquals("Wrong length", 5, arg.size()); assertEquals("Wrong ordering", new Integer(1), arg.get(0).getOrdering()); assertEquals("Wrong ordering", new Integer(1), arg.get(1).getOrdering()); assertEquals("Wrong ordering", new Integer(2), arg.get(2).getOrdering()); assertEquals("Wrong ordering", new Integer(3), arg.get(3).getOrdering()); assertEquals("Wrong ordering", new Integer(4), arg.get(4).getOrdering()); assertEquals("Wrong SectionPage", p, arg.get(0).getPage()); assertEquals("Wrong SectionPage", s, arg.get(0).getSection()); } @Test public void testSyncWhenSectionPageExists(){ Section frontPage = new Section(1L); Section s = new Section(2L); s.setParent(frontPage); Page p = new ExtendedPage(); p.setParent(s); SectionPage sp = new SectionPage(frontPage, p, 1, ""); when(sectionPageService.getEntity(1L)).thenReturn(sp); SectionPage existing = new SectionPage(s, p, 3, ""); when(sectionPageService.getSectionPageBySectionAndPage(s, p)).thenReturn(existing); ArgumentCaptor<List> argument = ArgumentCaptor.forClass(List.class); List<SectionPage> pages = new LinkedList<SectionPage>(); pages.add(new SectionPage(s, new ExtendedPage(), 1, "1")); pages.add(new SectionPage(s, new ExtendedPage(), 2, "2")); pages.add(existing); pages.add(new SectionPage(s, new ExtendedPage(), 4, "4")); when(sectionPageService.getSectionPageByOwner(s)).thenReturn(pages); pages.get(0).setID(1L); pages.get(1).setID(2L); pages.get(2).setID(3L); pages.get(3).setID(4L); s.setPages(pages); controller.synchronizeSection(1L); verify(sectionPageService).cascadeAndSaveOrUpdateSectionPages(argument.capture()); List<SectionPage> arg = argument.getValue(); assertEquals("Wrong length", 4, arg.size()); assertEquals("Wrong ordering", new Integer(1), arg.get(0).getOrdering()); assertEquals("Wrong ordering", new Integer(1), arg.get(1).getOrdering()); assertEquals("Wrong ordering", new Integer(2), arg.get(2).getOrdering()); assertEquals("Wrong ordering", new Integer(4), arg.get(3).getOrdering()); assertEquals("Wrong SectionPage", existing, arg.get(0)); assertEquals("Wrong SectionPage", "1", arg.get(1).getDescription()); assertEquals("Wrong SectionPage", "2", arg.get(2).getDescription()); assertEquals("Wrong SectionPage", "4", arg.get(3).getDescription()); } @Test public void testCleanSection(){ Section s = new Section(2L); List<SectionPage> pages = new LinkedList<SectionPage>(); pages.add(new SectionPage(s, new ExtendedPage(), 1, "1")); pages.add(new SectionPage(s, new ExtendedPage(), 2, "2")); SectionPage sp3 = new SectionPage(s, new ExtendedPage(), 3, "3"); pages.add(sp3); SectionPage sp4 = new SectionPage(s, new ExtendedPage(), 4, "4"); pages.add(sp4); s.setPages(pages); pages.get(0).setID(1L); pages.get(1).setID(2L); pages.get(2).setID(3L); pages.get(3).setID(4L); when(sectionService.getEntity(2L)).thenReturn(s); controller.setCleanLimit(2); controller.cleanSection(2L); verify(sectionPageService, times(2)).remove(any(SectionPage.class)); verify(sectionPageService).remove(sp3); verify(sectionPageService).remove(sp4); } @Test public void testCleanSectionWithUnpublishedPage(){ Section s = new Section(2L); List<SectionPage> pages = new LinkedList<SectionPage>(); pages.add(new SectionPage(s, new ExtendedPage(), 1, "1")); ExtendedPage page = new ExtendedPage(); page.setPublished(false); pages.add(new SectionPage(s, page, 2, "2")); SectionPage sp3 = new SectionPage(s, new ExtendedPage(), 3, "3"); pages.add(sp3); SectionPage sp4 = new SectionPage(s, new ExtendedPage(), 4, "4"); pages.add(sp4); s.setPages(pages); pages.get(0).setID(1L); pages.get(1).setID(2L); pages.get(2).setID(3L); pages.get(3).setID(4L); when(sectionService.getEntity(2L)).thenReturn(s); controller.setCleanLimit(2); controller.cleanSection(2L); verify(sectionPageService, times(1)).remove(any(SectionPage.class)); verify(sectionPageService).remove(sp4); } @Test public void testSectionByName() throws PageNotFoundException { Section s = new Section(); when(sectionService.getEntity(1L)).thenReturn(s); LinkedList<Section> toplevel = new LinkedList<Section>(); toplevel.add(new Section()); when(sectionService.getTopLevelSections(false)).thenReturn(toplevel); List<SectionPage> sps = new LinkedList<SectionPage>(); when(sectionPageService.getSectionPageByOwner(s)).thenReturn(sps); Model model = new ExtendedModelMap(); String view = controller.handleSectionPages(1L, model); assertEquals("Wrong view", "no/dusken/aranea/base/admin/sectionpage/prioritize", view); assertEquals("Model did not contain section", s, model.asMap().get("section")); assertEquals("Model did not contain sectionpages", sps, model.asMap().get("sectionpages")); } @Test public void testSubSectionsAreListed() throws PageNotFoundException { Section s = new Section(2L); when(sectionService.getSectionByUrl("hiddensection", null)).thenReturn(s); LinkedList<Section> toplevel = new LinkedList<Section>(); toplevel.add(new Section(1L)); when(sectionService.getTopLevelSections(false)).thenReturn(toplevel); List<SectionPage> sps = new LinkedList<SectionPage>(); when(sectionPageService.getSectionPageByOwner(s)).thenReturn(sps); when(sectionService.getEntity(2L)).thenReturn(s); Model model = new ExtendedModelMap(); String view = controller.handleSectionPages("hiddensection", model); assertEquals("Wrong view", "no/dusken/aranea/base/admin/sectionpage/prioritize", view); assertEquals("Model did not contain section", s, model.asMap().get("section")); assertEquals("Model did not contain sectionpages", sps, model.asMap().get("sectionpages")); } @Test public void testTopLevelSectionInMapWhenNoSectionSpecified() throws PageNotFoundException { Section s = new Section(); List<Section> sections = new LinkedList<Section>(); sections.add(s); when(sectionService.getTopLevelSections(false)).thenReturn(sections); List<SectionPage> sps = new LinkedList<SectionPage>(); when(sectionPageService.getSectionPageByOwner(s)).thenReturn(sps); when(sectionService.getSectionByUrl("frontpage", null)).thenReturn(s); Model model = new ExtendedModelMap(); String view = controller.handleSectionPages(0L, model); assertEquals("Wrong view", "no/dusken/aranea/base/admin/sectionpage/prioritize", view); assertTrue("Model did not contain section", model.asMap().containsValue(s)); } @Test(expected = PageNotFoundException.class) public void testExceptionWhenSectionDoesNotExistByID() throws PageNotFoundException { Model model = new ExtendedModelMap(); String view = controller.handleSectionPages(666L, model); } @Test(expected = PageNotFoundException.class) public void testExceptionWhenSectionDoesNotExistByName() throws PageNotFoundException { Model model = new ExtendedModelMap(); String view = controller.handleSectionPages("666", model); } @Test public void testOrderingIsCascadedWhenPageIsRemoved(){ Section s = new Section(); SectionPage sp1 = new SectionPage(s, null, 1, ""); SectionPage sp2 = new SectionPage(s, null, 2, ""); SectionPage sp3 = new SectionPage(s, null, 3, ""); SectionPage sp4 = new SectionPage(s, null, 4, ""); sp1.setID(1L); sp2.setID(2L); sp3.setID(3L); sp4.setID(4L); List<SectionPage> sps = new LinkedList<SectionPage>(); sps.add(sp1); sps.add(sp2); sps.add(sp4); when(sectionPageService.getSectionPageByOwner(s)).thenReturn(sps); when(sectionPageService.getEntity(3L)).thenReturn(sp3); controller.deleteSectionPage(3L); assertEquals("SectionPage has wrong ordering", new Integer(1), sp1.getOrdering()); assertEquals("SectionPage has wrong ordering", new Integer(2), sp2.getOrdering()); assertEquals("SectionPage has wrong ordering", new Integer(4), sp4.getOrdering()); assertFalse("Should not contain sp3", sps.contains(sp3)); verify(sectionPageService).cascadeAndSaveOrUpdateSectionPages(sps); verify(sectionPageService).remove(any(SectionPage.class)); } }