Write tests
This commit is contained in:
parent
c3517d0517
commit
e28a3f010c
@ -30,7 +30,7 @@ public class GlobalExceptionHandler {
|
||||
@ExceptionHandler(Exception.class)
|
||||
public ResponseEntity<ErrorDetails> 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);
|
||||
}
|
||||
|
||||
|
82
src/test/java/de/szut/lf8_starter/integration/project/GetEmployeesFromProjectTest.java
Normal file
82
src/test/java/de/szut/lf8_starter/integration/project/GetEmployeesFromProjectTest.java
Normal file
@ -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<String, String> map = new LinkedMultiValueMap<>();
|
||||
map.add("grant_type", "password");
|
||||
map.add("client_id", "employee-management-service");
|
||||
map.add("username", "user");
|
||||
map.add("password", "test");
|
||||
|
||||
HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<>(map, headers);
|
||||
|
||||
ResponseEntity<Map> response = this.restTemplate.exchange(url, HttpMethod.POST, request, Map.class);
|
||||
|
||||
return Objects.requireNonNull(response.getBody()).get("access_token").toString();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user