package no.dusken.aranea.web.control; import no.dusken.aranea.model.Article; import no.dusken.aranea.service.PageService; import no.dusken.common.exception.PageNotFoundException; import org.junit.Before; import org.junit.Test; import org.springframework.mock.web.MockHttpServletRequest; import org.springframework.mock.web.MockHttpServletResponse; import org.springframework.web.servlet.ModelAndView; import java.util.GregorianCalendar; import static org.junit.Assert.assertNotNull; 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 PageControllerTest { private PageController pageController; private PageService pageService; @Before public void setup() { pageController = new PageController(); pageService = mock(PageService.class); pageController.setPageService(pageService); pageController.setMAX_PARAGRAPHS(8); } /** * Test that the controller doesn't return a page that has published = false * and visiblefrom < now < visibleto * * @throws PageNotFoundException when page is not found */ @Test(expected = PageNotFoundException.class) public void testTryGetNotPublishedPage() throws Exception { Article a = new Article(); a.setTitle("tittel"); a.setText("tittel"); a.setPublished(false); a.setVisibleFrom(new GregorianCalendar(1900, 1, 1)); a.setVisibleTo(new GregorianCalendar(3900, 1, 1)); when(pageService.getEntity(1L)).thenReturn(a); MockHttpServletRequest request = new MockHttpServletRequest(); MockHttpServletResponse response = new MockHttpServletResponse(); request.addParameter("ID", "1"); ModelAndView modelAndView; modelAndView = pageController.handleArticle(request, response); assertNotNull(modelAndView); assertTrue(!modelAndView.getModel().isEmpty()); } /** * Test that the controller return a page that has published = true * and visiblefrom < now < visibleto * * @throws PageNotFoundException when page is now found */ @Test() public void testTryGetPublishedPage() throws Exception { Article a = new Article(); a.setTitle("tittel"); a.setText("tittel"); a.setPublished(true); a.setVisibleFrom(new GregorianCalendar(1900, 1, 1)); a.setVisibleTo(new GregorianCalendar(3900, 1, 1)); when(pageService.getEntity(1L)).thenReturn(a); MockHttpServletRequest request = new MockHttpServletRequest(); MockHttpServletResponse response = new MockHttpServletResponse(); request.addParameter("ID", "1"); ModelAndView modelAndView; modelAndView = pageController.handleArticle(request, response); assertNotNull(modelAndView); assertTrue(!modelAndView.getModel().isEmpty()); } /** * Test that the controller doesn't return a page that has published = false * and visiblefrom, visibleto < now * * @throws PageNotFoundException when page is not found */ @Test(expected = PageNotFoundException.class) public void testTryGetNotPublishedPagePast() throws Exception { Article a = new Article(); a.setTitle("tittel"); a.setText("tittel"); a.setPublished(true); a.setVisibleFrom(new GregorianCalendar(1900, 1, 1)); a.setVisibleTo(new GregorianCalendar(1950, 1, 1)); when(pageService.getEntity(1L)).thenReturn(a); MockHttpServletRequest request = new MockHttpServletRequest(); MockHttpServletResponse response = new MockHttpServletResponse(); request.addParameter("ID", "1"); ModelAndView modelAndView; modelAndView = pageController.handleArticle(request, response); assertNotNull(modelAndView); assertTrue(!modelAndView.getModel().isEmpty()); } /** * Test that the controller doesn't return a page that has published = false * and now < visiblefrom, visibleto * * @throws PageNotFoundException when page is not found */ @Test(expected = PageNotFoundException.class) public void testTryGetNotPublishedPageFuture() throws Exception { Article a = new Article(); a.setTitle("tittel"); a.setText("tittel"); a.setPublished(true); a.setVisibleFrom(new GregorianCalendar(2900, 1, 1)); a.setVisibleTo(new GregorianCalendar(3900, 1, 1)); when(pageService.getEntity(1L)).thenReturn(a); MockHttpServletRequest request = new MockHttpServletRequest(); MockHttpServletResponse response = new MockHttpServletResponse(); request.addParameter("ID", "1"); ModelAndView modelAndView; modelAndView = pageController.handleArticle(request, response); assertNotNull(modelAndView); assertTrue(!modelAndView.getModel().isEmpty()); } /** * */ @Test public void testShowPublishedTrue() throws Exception { Article a = new Article(); a.setTitle("tittel"); a.setText("tittel"); a.setPublished(false); pageController.setShowUnpublished(true); a.setVisibleFrom(new GregorianCalendar(2000, 1, 1)); a.setVisibleTo(new GregorianCalendar(3900, 1, 1)); when(pageService.getEntity(1L)).thenReturn(a); MockHttpServletRequest request = new MockHttpServletRequest(); MockHttpServletResponse response = new MockHttpServletResponse(); request.addParameter("ID", "1"); ModelAndView modelAndView; modelAndView = pageController.handleArticle(request, response); assertNotNull(modelAndView); assertNotNull(modelAndView.getModel().get("article")); } }