package no.dusken.aranea.web.control;
import no.dusken.aranea.model.ExtendedPage;
import no.dusken.aranea.model.Page;
import no.dusken.aranea.model.Section;
import no.dusken.aranea.service.PageService;
import no.dusken.aranea.service.SectionService;
import no.dusken.common.exception.PageNotFoundException;
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 junit.framework.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
/**
* @author Marvin B. Lillehaug <lillehau@underdusken.no>
*/
public class SectionControllerTest {
private SectionController sectionController;
private SectionService sectionService;
private PageService pageService;
private int number = 12;
@Before
public void onSetUp() throws Exception {
sectionService = mock(SectionService.class);
pageService = mock(PageService.class);
sectionController = new SectionController();
sectionController.setPageService(pageService);
sectionController.setSectionService(sectionService);
sectionController.setArticlesPerPage(number);
}
/**
* Test that only pages that are published and visiblefrom < now < visibleto appear
*/
@Test
public void testSectionOnlyPublished() throws PageNotFoundException {
Model model = new ExtendedModelMap();
Section s = new Section();
s.setActive(true);
List<Page> publishedPages = new LinkedList<Page>();
when(sectionService.getSectionByUrl("s", null)).thenReturn(s);
when(pageService.getSectionPagePublished(number, s)).thenReturn(publishedPages);
String view = sectionController.getPagesBySection("s", model);
assertEquals("Wrong page list returned", publishedPages, model.asMap().get("pages"));
assertEquals(s, model.asMap().get("section"));
}
@Test(expected = PageNotFoundException.class)
public void testHiddenSection() throws PageNotFoundException {
Model model = new ExtendedModelMap();
Section s = new Section();
s.setActive(false);
List<Page> publishedPages = new LinkedList<Page>();
when(sectionService.getSectionByUrl("s", null)).thenReturn(s);
when(pageService.getSectionPagePublished(number, s)).thenReturn(publishedPages);
String view = sectionController.getPagesBySection("s", model);
}
/**
* Test that only pages that are published and visiblefrom < now < visibleto appear
*/
@Test
public void testSubSectionOnlyPublished() throws PageNotFoundException {
Model model = new ExtendedModelMap();
Section s = new Section();
Section sub = new Section();
s.setActive(true);
sub.setActive(true);
List<Page> publishedPages = new LinkedList<Page>();
when(sectionService.getSectionByUrl("s", null)).thenReturn(s);
when(sectionService.getSectionByUrl("sub", s)).thenReturn(sub);
when(pageService.getSectionPagePublished(number,s )).thenReturn(publishedPages);
String view = sectionController.getPagesBySectionAndSubSection("sub", "s", model);
assertEquals("Wrong page list returned", publishedPages, model.asMap().get("pages"));
assertEquals(s, model.asMap().get("section"));
}
/**
* Test that a exception is thrown when a non exisiting section is requested.
*/
@Test(expected = PageNotFoundException.class)
public void testNonExistingSection() throws PageNotFoundException {
Model model = new ExtendedModelMap();
String view = sectionController.getPagesBySection("noneexisting", model);
}
@Test(expected = PageNotFoundException.class)
public void testGetnonactiveSection() throws PageNotFoundException {
Section s = new Section();
s.setActive(false);
when(sectionService.getSectionByUrl("nonactive", null)).thenReturn(s);
Model model = new ExtendedModelMap();
String view = sectionController.getPagesBySection("nonactive", model);
}
@Test(expected = PageNotFoundException.class)
public void getSubSectionWithnonactiveParent() throws PageNotFoundException {
Section s = new Section();
s.setActive(false);
when(sectionService.getSectionByUrl("nonactive", null)).thenReturn(s);
Section sub = new Section();
sub.setActive(true);
when(sectionService.getSectionByUrl("sub", s)).thenReturn(sub);
Model model = new ExtendedModelMap();
String view = sectionController.getPagesBySectionAndSubSection("sub", "nonactive", model);
}
@Test(expected = PageNotFoundException.class)
public void getSubSectionnonactive() throws PageNotFoundException {
Section s = new Section();
s.setActive(true);
when(sectionService.getSectionByUrl("nonactive", null)).thenReturn(s);
Section sub = new Section();
sub.setActive(false);
when(sectionService.getSectionByUrl("sub", s)).thenReturn(sub);
Model model = new ExtendedModelMap();
String view = sectionController.getPagesBySectionAndSubSection("sub", "nonactive", model);
}
@Test
public void testGetFeedFromTopLevelSection() throws PageNotFoundException {
Section s = new Section();
s.setActive(true);
when(sectionService.getSectionByUrl("sec", null)).thenReturn(s);
List<Page> pages = new LinkedList<Page>();
pages.add(new ExtendedPage());
when(pageService.getSectionPagePublished(number, s)).thenReturn(pages);
Model model = new ExtendedModelMap();
String view = sectionController.handleSectionFeed("sec", "", model);
assertTrue("Model should contain section", model.asMap().containsValue(s));
assertTrue("Model should contain pages", model.asMap().containsValue(pages));
}
@Test
public void testGetFeedFromSubSection() throws PageNotFoundException {
Section s = new Section();
s.setActive(true);
when(sectionService.getSectionByUrl("sec", null)).thenReturn(s);
Section sub = new Section();
sub.setActive(true);
when(sectionService.getSectionByUrl("sub", s)).thenReturn(sub);
List<Page> pages = new LinkedList<Page>();
pages.add(new ExtendedPage());
when(pageService.getSectionPagePublished(number, sub)).thenReturn(pages);
Model model = new ExtendedModelMap();
String view = sectionController.handleSectionFeed("sec", "sub", model);
assertTrue("Model should contain section", model.asMap().containsValue(s));
assertTrue("Model should contain pages", model.asMap().containsValue(pages));
}
@Test(expected = PageNotFoundException.class)
public void testGetFeedFromHiddenTopLevelSection() throws PageNotFoundException {
Section s = new Section();
s.setActive(false);
when(sectionService.getSectionByUrl("sec", null)).thenReturn(s);
Model model = new ExtendedModelMap();
String view = sectionController.handleSectionFeed("sec", "", model);
}
@Test(expected = PageNotFoundException.class)
public void testGetFeedFromHiddenSubSection() throws PageNotFoundException {
Section s = new Section();
s.setActive(true);
when(sectionService.getSectionByUrl("sec", null)).thenReturn(s);
Section sub = new Section();
sub.setActive(false);
when(sectionService.getSectionByUrl("sub", s)).thenReturn(sub);
Model model = new ExtendedModelMap();
String view = sectionController.handleSectionFeed("sec", "sub", model);
}
@Test(expected = PageNotFoundException.class)
public void testGetFeedFromNonExistingSection() throws PageNotFoundException {
Model model = new ExtendedModelMap();
String view = sectionController.handleSectionFeed("sec", "sub", model);
}
}