diff --git a/test/org/zfin/ontology/presentation/OntologyControllerTest.java b/test/org/zfin/ontology/presentation/OntologyControllerTest.java index b9de08d755..72a0e9fe5a 100644 --- a/test/org/zfin/ontology/presentation/OntologyControllerTest.java +++ b/test/org/zfin/ontology/presentation/OntologyControllerTest.java @@ -1,117 +1,79 @@ package org.zfin.ontology.presentation; import org.junit.Before; -import org.junit.Ignore; import org.junit.Test; import org.junit.runner.RunWith; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.context.ApplicationContext; -import org.springframework.mock.web.MockHttpServletRequest; -import org.springframework.mock.web.MockHttpServletResponse; -import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; -import org.springframework.web.servlet.HandlerAdapter; -import org.springframework.web.servlet.ModelAndView; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.setup.MockMvcBuilders; import org.zfin.AbstractDatabaseTest; import org.zfin.TestConfiguration; import org.zfin.framework.presentation.LookupStrings; -import java.util.Map; - import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; + -/** - */ @RunWith(SpringJUnit4ClassRunner.class) -@ContextConfiguration(locations = "file:test/properties-test.xml") public class OntologyControllerTest extends AbstractDatabaseTest { static { TestConfiguration.configure(); } - @Autowired - private ApplicationContext applicationContext; - - private MockHttpServletRequest request; - private MockHttpServletResponse response; - private HandlerAdapter handlerAdapter; + private MockMvc mockMvc; private OntologyTermDetailController controller; - @Before public void setUp() { - request = new MockHttpServletRequest(); - response = new MockHttpServletResponse(); - handlerAdapter = applicationContext.getBean(HandlerAdapter.class); controller = new OntologyTermDetailController(); + mockMvc = MockMvcBuilders.standaloneSetup(controller).build(); } @Test public void retrieveTermByOboID() throws Exception { - request.setRequestURI("/ontology/term/GO:0032502"); - ModelAndView mav = handlerAdapter.handle(request, response, controller); - assertNotNull(mav); - assertEquals("ontology/term-view", mav.getViewName()); + mockMvc.perform(get("/ontology/term/GO:0032502")) + .andExpect(status().isOk()) + .andExpect(view().name("ontology/term-view")); } @Test public void retrieveTermByTermID() throws Exception { - // presumptive forebrain midbrain boundary - request.setRequestURI("/ontology/term/ZDB-TERM-100331-1323"); - ModelAndView mav = handlerAdapter.handle(request, response, controller); - assertNotNull(mav); - assertEquals("ontology/term-view", mav.getViewName()); - Map model = mav.getModel(); - assertNotNull(model); - Object formBean = model.get(LookupStrings.FORM_BEAN); - assertEquals(OntologyBean.class, formBean.getClass()); - OntologyBean ontologyBean = (OntologyBean) formBean; - assertEquals("presumptive forebrain midbrain boundary", ontologyBean.getTerm().getTermName()); + String termID = "ZDB-TERM-100331-1323"; + +// MockMvcResultHandlers.print() // Optional: prints the request and response for debugging + + mockMvc.perform(get("/ontology/term/{termID}", termID)) + .andExpect(status().isOk()) + .andExpect(view().name("ontology/term-view")) + .andExpect(model().attributeExists(LookupStrings.FORM_BEAN)) + .andDo(result -> { + Object formBean = result.getModelAndView().getModel().get(LookupStrings.FORM_BEAN); + assertNotNull(formBean); + assertEquals(OntologyBean.class, formBean.getClass()); + OntologyBean ontologyBean = (OntologyBean) formBean; + assertEquals("presumptive forebrain midbrain boundary", ontologyBean.getTerm().getTermName()); + }); } @Test public void retrieveTermByAnatomyID() throws Exception { - // Rohon-Beard neuron - request.setRequestURI("/ontology/term/ZDB-ANAT-010921-407"); - ModelAndView mav = handlerAdapter.handle(request, response, controller); - assertNotNull(mav); - // redirect to Rohon-Beard neurons - assertEquals("redirect:/action/ontology/term/ZDB-TERM-100331-2208", mav.getViewName()); - } + String anatomyID = "ZDB-ANAT-010921-407"; - // Todo: This is broken only within the test environment (must be a bug in spring) as it is working - // in the true servlet container. - @Test - @Ignore("broken only within the test environment") - public void retrieveTermByAnatomyName() throws Exception { - request.setRequestURI("/term-detail-by-name/term?name=liver&ontologyName=zebrafish_anatomy"); - ModelAndView mav = handlerAdapter.handle(request, response, controller); - assertNotNull(mav); - // redirect to 'liver' - assertEquals("redirect:/action/ontology/term/ZFA:0000123", mav.getViewName()); - } + mockMvc.perform(get("/ontology/term/{anatomyID}", anatomyID)) + .andDo(result -> { + int status = result.getResponse().getStatus(); - // Todo: This is broken only within the test environment (must be a bug in spring) as it is working - // in the true servlet container. - @Test - @Ignore("broken only within the test environment") - public void retrievePopupById() throws Exception { - request.setRequestURI("/term-detail-popup?termID=GO:0043231"); - ModelAndView mav = handlerAdapter.handle(request, response, controller); - assertNotNull(mav); - // redirect to 'liver' - assertEquals("redirect:/action/ontology/term/ZFA:0000123", mav.getViewName()); - } + // Assert the redirect status + assertEquals(302, status); - @Test - @Ignore("Requires to load AO ontology from serialized file.") - public void retrieveTermListByWildtype() throws Exception { - request.setRequestURI("/term-detail/emb*"); - ModelAndView mav = handlerAdapter.handle(request, response, controller); - assertNotNull(mav); - // redirect to 'liver' - assertEquals("redirect:/action/ontology/term/ZFA:0000123", mav.getViewName()); + // Assert the redirect location + String redirectUrl = result.getResponse().getHeader("Location"); + + assertNotNull(redirectUrl); + assertEquals("/action/ontology/term/ZDB-TERM-100331-2208", redirectUrl); + }); } } \ No newline at end of file