package no.dusken.aranea.web.control; import no.dusken.aranea.model.Section; import no.dusken.aranea.service.SectionService; import org.junit.Before; import org.junit.Test; import org.springframework.ui.ExtendedModelMap; import org.springframework.ui.Model; import java.util.LinkedList; import java.util.List; import static junit.framework.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; /** * @author Marvin B. Lillehaug <lillehau@underdusken.no> */ public class HeaderControllerTest { private SectionService sectionService; private HeaderController headerController; @Before public void setUp() throws Exception { sectionService = mock(SectionService.class); headerController = new HeaderController(); headerController.setSectionService(sectionService); } @Test public void testHandleSection() { Section s = new Section(); List<Section> subSections = new LinkedList<Section>(); Section child1 = new Section(); child1.setActive(false); Section child2 = new Section(); child2.setActive(true); subSections.add(child1); subSections.add(child2); s.setChildren(subSections); when(sectionService.getSectionByUrl("section", null)).thenReturn(s); List<Section> toplevelSections = new LinkedList<Section>(); when(sectionService.getTopLevelSections(false)).thenReturn(toplevelSections); Model model = new ExtendedModelMap(); String view = headerController.handleHeaderMenu("section", "", model); assertEquals("Wrong view", "no/dusken/aranea/base/web/header/view", view); assertEquals("Selected section wrong", s, model.asMap().get("selectedSection")); assertTrue("Did not contain Calendar", model.containsAttribute("now")); assertEquals("Wrong toplevel sections", toplevelSections, model.asMap().get("sections")); assertEquals("Wrong children length", 1, ((List)model.asMap().get("children")).size()); } @Test public void testHandleSubSection(){ Section s = new Section(); List<Section> subSections = new LinkedList<Section>(); Section child1 = new Section(); child1.setActive(false); Section child2 = new Section(); child2.setActive(true); subSections.add(child1); subSections.add(child2); s.setChildren(subSections); Section subsection = new Section(); when(sectionService.getSectionByUrl("subsection", s)).thenReturn(subsection); when(sectionService.getSectionByUrl("section", null)).thenReturn(s); List<Section> toplevelSections = new LinkedList<Section>(); when(sectionService.getTopLevelSections(false)).thenReturn(toplevelSections); Model model = new ExtendedModelMap(); String view = headerController.handleHeaderMenu("section", "subsection", model); assertEquals("Wrong view", "no/dusken/aranea/base/web/header/view", view); assertEquals("Selected section wrong", s, model.asMap().get("selectedSection")); assertEquals("Selected subsection wrong", subsection, model.asMap().get("selectedSubsection")); } @Test public void testNoSection(){ List<Section> toplevelSections = new LinkedList<Section>(); when(sectionService.getTopLevelSections(false)).thenReturn(toplevelSections); Model model = new ExtendedModelMap(); String view = headerController.handleHeaderMenu("", "", model); assertEquals("Wrong view", "no/dusken/aranea/base/web/header/view", view); assertEquals("Sections wrong", model.asMap().get("sections"), toplevelSections); } }