From 2611135cbd792721ef293fb13f97637e22b83900 Mon Sep 17 00:00:00 2001 From: Dylan Hemsworth Date: Thu, 21 Nov 2024 16:36:56 -0800 Subject: [PATCH] Project Controller test stub --- .../gov/nrs/wfprev/ProjectControllerTest.java | 68 +++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 server/wfprev-api/src/test/java/ca/bc/gov/nrs/wfprev/ProjectControllerTest.java diff --git a/server/wfprev-api/src/test/java/ca/bc/gov/nrs/wfprev/ProjectControllerTest.java b/server/wfprev-api/src/test/java/ca/bc/gov/nrs/wfprev/ProjectControllerTest.java new file mode 100644 index 000000000..c1dc844f3 --- /dev/null +++ b/server/wfprev-api/src/test/java/ca/bc/gov/nrs/wfprev/ProjectControllerTest.java @@ -0,0 +1,68 @@ +package ca.bc.gov.nrs.wfprev; + +import java.util.Arrays; +import java.util.List; +import java.util.UUID; + +import org.junit.jupiter.api.Test; +import static org.mockito.Mockito.when; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.context.annotation.Import; +import org.springframework.hateoas.CollectionModel; +import org.springframework.http.MediaType; +import org.springframework.security.test.context.support.WithMockUser; +import org.springframework.test.web.servlet.MockMvc; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +import ca.bc.gov.nrs.wfprev.controllers.ProjectController; +import ca.bc.gov.nrs.wfprev.data.models.ProjectModel; +import ca.bc.gov.nrs.wfprev.services.ProjectService; + +@WebMvcTest(ProjectController.class) +@Import({SecurityConfig.class, TestcontainersConfiguration.class}) +class ProjectControllerTest { + @MockBean + private ProjectService projectService; + + @Autowired + private MockMvc mockMvc; + + @Test + @WithMockUser + void testGetProject() throws Exception { + String guid = UUID.randomUUID().toString(); + + ProjectModel project = new ProjectModel(); + project.setProjectGuid(guid); + + List projectList = Arrays.asList(project); + CollectionModel projectModel = CollectionModel.of(projectList); + + when(projectService.getAllProjects()).thenReturn(projectModel); + + mockMvc.perform(get("/projects") + .contentType(MediaType.APPLICATION_JSON)) + .andExpect(status().isOk()); + + when(projectService.getProjectById(guid)).thenReturn(project); + + mockMvc.perform(get("/projects/{id}", guid) + .contentType(MediaType.APPLICATION_JSON)) + .andExpect(status().isOk()); + } + + @Test + @WithMockUser + void testCreateProject() throws Exception { + ProjectModel project = new ProjectModel(); + when(projectService.createOrUpdateProject(project)).thenReturn(project); + + mockMvc.perform(post("/projects", project) + .contentType(MediaType.APPLICATION_JSON)) + .andExpect(status().isCreated()); + } +}