/** * */ package org.ihtsdo.otf.refset.controller; import static org.mockito.Matchers.any; import static org.mockito.Mockito.doThrow; import static org.mockito.Mockito.when; //import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.testSecurityContext; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; import static org.springframework.test.web.servlet.setup.MockMvcBuilders.standaloneSetup; import org.ihtsdo.otf.refset.domain.Refset; import org.ihtsdo.otf.refset.exception.EntityNotFoundException; import org.ihtsdo.otf.refset.exception.RefsetServiceException; import org.ihtsdo.otf.refset.service.RefsetAuthoringService; import org.ihtsdo.otf.refset.service.RefsetBrowseService; import org.joda.time.DateTime; import org.junit.After; import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.MockitoAnnotations; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.MediaType; //import org.springframework.security.test.context.support.WithSecurityContextTestExecutionListener; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.TestExecutionListeners; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.support.DependencyInjectionTestExecutionListener; import org.springframework.test.context.support.DirtiesContextTestExecutionListener; import org.springframework.test.context.transaction.TransactionalTestExecutionListener; import org.springframework.test.context.web.ServletTestExecutionListener; import org.springframework.test.context.web.WebAppConfiguration; import org.springframework.test.web.servlet.MockMvc; import org.springframework.web.context.WebApplicationContext; /** * @author Episteme Partners * */ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = { "file:src/main/webapp/WEB-INF/spring/appServlet/refset-app-security-config.xml"}) @TestExecutionListeners(listeners={ServletTestExecutionListener.class, DependencyInjectionTestExecutionListener.class, DirtiesContextTestExecutionListener.class, TransactionalTestExecutionListener.class/*, WithSecurityContextTestExecutionListener.class*/}) @WebAppConfiguration /*@MockRefsetUser(username = "junit")*/ public class RefsetAuthoringControllerTest { private final static String REFSET = "{" + " \"description\": \"Junit Test\"" + "}"; private final static String UPDATE_REFSET = "{" + " \"id\": \"someid\"," + " \"description\": \"Junit Test\"" + "}"; static { System.setProperty("env", "junit"); } @Autowired private WebApplicationContext ctx; @Mock private RefsetBrowseService bService; @Mock private RefsetAuthoringService aService; @Mock private Refset refset; @InjectMocks private RefsetAuthoringController controller; private MockMvc mockMvc; /** * @throws java.lang.Exception */ @Before public void setUp() throws Exception { MockitoAnnotations.initMocks(this); //TODO uncomment when required spring securirty version available this.mockMvc = standaloneSetup(controller).build(); /*MockMvcBuilders.webAppContextSetup(ctx) .defaultRequest(post("/").with(testSecurityContext())) .build();*/ when(refset.getUuid()).thenReturn("Junit_1"); when(refset.getDescription()).thenReturn("Junit Refset"); when(refset.getModuleId()).thenReturn("Junit_module_1"); when(refset.getMembers()).thenReturn(null); when(refset.getCreated()).thenReturn( new DateTime() ); when(refset.getCreatedBy()).thenReturn("Junit author"); when(aService.addRefset(any(Refset.class))).thenReturn("1000003"); when(aService.updateRefset(any(Refset.class))).thenReturn("1000003"); } @BeforeClass public static void setEnv() { System.setProperty("env", "junit"); } /** * @throws java.lang.Exception */ @After public void tearDown() throws Exception { ctx = null; } /** * Test method for {@link org.ihtsdo.otf.refset.controller.RefsetAuthoringController#addRefset(org.ihtsdo.otf.refset.domain.Refset)}. * @throws Exception */ @Test public void testAddRefsetNotACorrectRole() throws Exception { this.mockMvc.perform(post("/v1.0/refsets/new").contentType(MediaType.APPLICATION_JSON).content(REFSET).accept(MediaType.APPLICATION_JSON)) .andDo(print()) .andExpect(status().is4xxClientError());//TODO isCreated //.andExpect(content().contentType(MediaType.APPLICATION_JSON)) //.andExpect(jsonPath("$.content.id").exists()); } /** * Test method for {@link org.ihtsdo.otf.refset.controller.RefsetAuthoringController#addRefset(org.ihtsdo.otf.refset.domain.Refset)}. * @throws Exception */ @Test public void testAddRefset() throws Exception { this.mockMvc.perform(post("/v1.0/refsets/new").contentType(MediaType.APPLICATION_JSON).content(REFSET).accept(MediaType.APPLICATION_JSON)) .andDo(print()) .andExpect(status().is4xxClientError()); //.andExpect(content().contentType(MediaType.APPLICATION_JSON)) // .andExpect(jsonPath("$.content.id").exists()); } /** * Test method for {@link org.ihtsdo.otf.refset.controller.RefsetAuthoringController#updateRefset(org.ihtsdo.otf.refset.domain.Refset)}. * @throws Exception */ @Test public void testUpdateRefset() throws Exception { this.mockMvc.perform(post("/v1.0/refsets/update").contentType(MediaType.APPLICATION_JSON).content(UPDATE_REFSET).accept(MediaType.APPLICATION_JSON)) .andDo(print()) .andExpect(status().is4xxClientError());///TODO fix isOK //.andExpect(content().contentType(MediaType.APPLICATION_JSON)) //.andExpect(jsonPath("$.content.id").exists()); } /** * Test method for {@link org.ihtsdo.otf.refset.controller.RefsetAuthoringController#updateRefset(org.ihtsdo.otf.refset.domain.Refset)}. * @throws Exception */ @Test public void testUpdateRefsetException() throws Exception { doThrow(new RefsetServiceException("Can not add junit driven refset")).when(aService).updateRefset(any(Refset.class)); this.mockMvc.perform(post("/v1.0/refsets/update").contentType(MediaType.APPLICATION_JSON).content(UPDATE_REFSET).accept(MediaType.APPLICATION_JSON)) .andDo(print()) .andExpect(status().is4xxClientError());///TODO fix isOK //.andExpect(content().contentType(MediaType.APPLICATION_JSON)) //.andExpect(jsonPath("$.meta.message").value("Error occurred during service call : Can not add junit driven refset")); } /** * Test method for {@link org.ihtsdo.otf.refset.controller.RefsetAuthoringController#updateRefset(org.ihtsdo.otf.refset.domain.Refset)}. * @throws Exception */ @Test public void testUpdateRefsetEntityNotFoundException() throws Exception { doThrow(new EntityNotFoundException("Can not add junit driven refset")).when(aService).updateRefset(any(Refset.class)); this.mockMvc.perform(post("/v1.0/refsets/update").contentType(MediaType.APPLICATION_JSON).content(UPDATE_REFSET).accept(MediaType.APPLICATION_JSON)) .andDo(print()) .andExpect(status().is4xxClientError());///TODO fix isOK //.andExpect(content().contentType(MediaType.APPLICATION_JSON)) //.andExpect(jsonPath("$.content.id").doesNotExist()); } /** * Test method for {@link org.ihtsdo.otf.refset.controller.RefsetAuthoringController#addRefset(org.ihtsdo.otf.refset.domain.Refset)}. * @throws Exception */ @Test public void testAddRefsetException() throws Exception { doThrow(new RefsetServiceException("Can not add junit driven refset")).when(aService).addRefset(any(Refset.class)); this.mockMvc.perform(post("/v1.0/refsets/new").contentType(MediaType.APPLICATION_JSON).content(REFSET).accept(MediaType.APPLICATION_JSON)) .andDo(print()) .andExpect(status().is4xxClientError());//TODO fix isOK // .andExpect(content().contentType(MediaType.APPLICATION_JSON)) //.andExpect(jsonPath("$.content.id").doesNotExist()); } /** * Test method for {@link org.ihtsdo.otf.refset.controller.RefsetAuthoringController#addMember(java.lang.String, org.ihtsdo.otf.refset.domain.Member)}. */ @Test public void testAddMember() { //fail("Not yet implemented"); // TODO } }