From e28a3f010c1a085ccede39d6b65dc7af472db001 Mon Sep 17 00:00:00 2001 From: Phan Huy Tran Date: Wed, 23 Oct 2024 12:10:25 +0200 Subject: [PATCH] Write tests --- .../GlobalExceptionHandler.java | 2 +- .../project/GetEmployeesFromProjectTest.java | 82 +++++++++++++++++++ 2 files changed, 83 insertions(+), 1 deletion(-) create mode 100644 src/test/java/de/szut/lf8_starter/integration/project/GetEmployeesFromProjectTest.java diff --git a/src/main/java/de/szut/lf8_starter/exceptionHandling/GlobalExceptionHandler.java b/src/main/java/de/szut/lf8_starter/exceptionHandling/GlobalExceptionHandler.java index f6415b5..dd3f97f 100644 --- a/src/main/java/de/szut/lf8_starter/exceptionHandling/GlobalExceptionHandler.java +++ b/src/main/java/de/szut/lf8_starter/exceptionHandling/GlobalExceptionHandler.java @@ -30,7 +30,7 @@ public class GlobalExceptionHandler { @ExceptionHandler(Exception.class) public ResponseEntity handleAllOtherExceptions(Exception ex, WebRequest request) { ErrorDetails errorDetails = new ErrorDetails(new Date(), ex.getClass() + " " + ex.getMessage(), request.getDescription(false)); - + System.out.println(ex); return new ResponseEntity<>(errorDetails, HttpStatus.INTERNAL_SERVER_ERROR); } diff --git a/src/test/java/de/szut/lf8_starter/integration/project/GetEmployeesFromProjectTest.java b/src/test/java/de/szut/lf8_starter/integration/project/GetEmployeesFromProjectTest.java new file mode 100644 index 0000000..1e8a630 --- /dev/null +++ b/src/test/java/de/szut/lf8_starter/integration/project/GetEmployeesFromProjectTest.java @@ -0,0 +1,82 @@ +package de.szut.lf8_starter.integration.project; + +import de.szut.lf8_starter.project.ProjectEntity; +import de.szut.lf8_starter.project.ProjectRepository; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.http.*; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.util.LinkedMultiValueMap; +import org.springframework.util.MultiValueMap; +import org.springframework.web.client.RestTemplate; + +import java.time.LocalDate; +import java.util.List; +import java.util.Map; +import java.util.Objects; + +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +@SpringBootTest +@AutoConfigureMockMvc(addFilters = false) +class GetEmployeesFromProjectTest { + @Autowired + private MockMvc mockMvc; + @Autowired + private ProjectRepository projectRepository; + @Autowired + private RestTemplate restTemplate; + + @Test + void getProjectTest() throws Exception { + ProjectEntity project = new ProjectEntity(); + project.setId(1); + project.setComment("comment"); + project.setContractor(1); + project.setContractorName("contractorName"); + project.setEndDate(LocalDate.of(2024, 1, 1)); + project.setLeadingEmployee(1); + project.setName("name"); + project.setStartDate(LocalDate.of(2021, 1, 1)); + project.setEmployees(List.of(315L)); + this.projectRepository.save(project); + + this.mockMvc.perform(get("/projects/1/employees") + .header(HttpHeaders.AUTHORIZATION, getBearerToken()) + ).andExpect(status().isOk()) + .andExpect(jsonPath("project_id").value(1)) + .andExpect(jsonPath("project_description").value("comment")) + .andExpect(jsonPath("employees[0].id").value(315)) + .andExpect(jsonPath("employees[0].skillSet[0].skill").value("Product Owner")); + } + + @Test + void getProjectShouldReturnNotFoundResponseWhenProjectIsNotFound() throws Exception { + this.mockMvc.perform(get("/projects/1111/employees") + .header(HttpHeaders.AUTHORIZATION, getBearerToken()) + ).andExpect(status().isNotFound()); + } + + private String getBearerToken() { + String url = "https://keycloak.szut.dev/auth/realms/szut/protocol/openid-connect/token"; + + HttpHeaders headers = new HttpHeaders(); + headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED); + + MultiValueMap map = new LinkedMultiValueMap<>(); + map.add("grant_type", "password"); + map.add("client_id", "employee-management-service"); + map.add("username", "user"); + map.add("password", "test"); + + HttpEntity> request = new HttpEntity<>(map, headers); + + ResponseEntity response = this.restTemplate.exchange(url, HttpMethod.POST, request, Map.class); + + return Objects.requireNonNull(response.getBody()).get("access_token").toString(); + } +}