From 010d91131e7f45a8fb2c66dc7fccccd0b0c6543a Mon Sep 17 00:00:00 2001
From: Phan Huy Tran
Date: Wed, 23 Oct 2024 14:41:21 +0200
Subject: [PATCH] Fix tests
---
.../project/AddEmployeeToProjectTest.java | 98 +++----------------
1 file changed, 11 insertions(+), 87 deletions(-)
diff --git a/src/test/java/de/szut/lf8_starter/integration/project/AddEmployeeToProjectTest.java b/src/test/java/de/szut/lf8_starter/integration/project/AddEmployeeToProjectTest.java
index ab06823..35144fc 100644
--- a/src/test/java/de/szut/lf8_starter/integration/project/AddEmployeeToProjectTest.java
+++ b/src/test/java/de/szut/lf8_starter/integration/project/AddEmployeeToProjectTest.java
@@ -2,6 +2,7 @@ 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.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
@@ -13,7 +14,6 @@ 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;
@@ -22,7 +22,7 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
@SpringBootTest
@AutoConfigureMockMvc(addFilters = false)
-class AddEmployeeToProjectActionIntegrationTest {
+class AddEmployeeToProjectTest {
@Autowired
private MockMvc mockMvc;
@@ -32,6 +32,11 @@ class AddEmployeeToProjectActionIntegrationTest {
@Autowired
private ProjectRepository projectRepository;
+ @BeforeEach
+ public void clear() {
+ this.projectRepository.deleteAll();
+ }
+
@Test
void addEmployeeToProjectTest() throws Exception {
ProjectEntity project = new ProjectEntity();
@@ -42,100 +47,19 @@ class AddEmployeeToProjectActionIntegrationTest {
project.setLeadingEmployee(1);
project.setName("name");
project.setStartDate(LocalDate.of(2021, 1, 1));
- project.setEmployees(List.of(1L, 2L, 3L));
+ project.setPlannedEndDate(LocalDate.of(2022, 1, 1));
this.projectRepository.save(project);
- mockMvc.perform(post("/projects/{projectId}/employees/{employeeId}", project.getId(), 312)
+ mockMvc.perform(post("/projects/{projectId}/employees/{employeeId}", 1, 312)
.header(HttpHeaders.AUTHORIZATION, getBearerToken())
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isNoContent());
- ProjectEntity updatedProject = projectRepository.findById(project.getId()).get();
+ ProjectEntity updatedProject = projectRepository.findById(1L).get();
assert updatedProject.getEmployees().contains(312L);
}
- @Test
- void addEmployeeToPastProjectTest() throws Exception {
- ProjectEntity project = new ProjectEntity();
- project.setComment("past project");
- project.setContractor(1);
- project.setContractorName("contractorName");
- project.setEndDate(LocalDate.of(1991, 1, 1));
- project.setLeadingEmployee(1);
- project.setName("past project");
- project.setStartDate(LocalDate.of(1990, 1, 1));
- project.setEmployees(List.of(1L, 2L, 3L));
- this.projectRepository.save(project);
-
- mockMvc.perform(post("/projects/{projectId}/employees/{employeeId}", project.getId(), 312)
- .header(HttpHeaders.AUTHORIZATION, getBearerToken())
- .contentType(MediaType.APPLICATION_JSON))
- .andExpect(status().isNoContent());
-
- ProjectEntity updatedProject = projectRepository.findById(project.getId()).get();
-
- assert updatedProject.getEmployees().contains(312L);
- }
-
- @Test
- void addEmployeeToFutureProjectTest() throws Exception {
- ProjectEntity project = new ProjectEntity();
- project.setComment("future project");
- project.setContractor(1);
- project.setContractorName("contractorName");
- project.setEndDate(LocalDate.of(2101, 1, 1));
- project.setLeadingEmployee(1);
- project.setName("future project");
- project.setStartDate(LocalDate.of(2100, 1, 1));
- project.setEmployees(List.of(1L, 2L, 3L));
- this.projectRepository.save(project);
-
- mockMvc.perform(post("/projects/{projectId}/employees/{employeeId}", project.getId(), 312)
- .header(HttpHeaders.AUTHORIZATION, getBearerToken())
- .contentType(MediaType.APPLICATION_JSON))
- .andExpect(status().isNoContent());
-
- ProjectEntity updatedProject = projectRepository.findById(project.getId()).get();
-
- assert updatedProject.getEmployees().contains(312L);
- }
-
- @Test
- void addEmployeeToProjectWithOverlappingDatesTest() throws Exception {
- ProjectEntity project1 = new ProjectEntity();
- project1.setComment("project 1");
- project1.setContractor(1);
- project1.setContractorName("contractorName");
- project1.setEndDate(LocalDate.of(2023, 1, 1));
- project1.setLeadingEmployee(1);
- project1.setName("project 1");
- project1.setStartDate(LocalDate.of(2022, 1, 1));
- project1.setEmployees(List.of(1L, 2L, 3L));
- this.projectRepository.save(project1);
-
- ProjectEntity project2 = new ProjectEntity();
- project2.setComment("project 2");
- project2.setContractor(1);
- project2.setContractorName("contractorName");
- project2.setEndDate(LocalDate.of(2023, 6, 1));
- project2.setLeadingEmployee(1);
- project2.setName("project 2");
- project2.setStartDate(LocalDate.of(2022, 6, 1));
- project2.setEmployees(List.of(1L, 2L, 3L));
- this.projectRepository.save(project2);
-
- mockMvc.perform(post("/projects/{projectId}/employees/{employeeId}", project1.getId(), 312)
- .header(HttpHeaders.AUTHORIZATION, getBearerToken())
- .contentType(MediaType.APPLICATION_JSON))
- .andExpect(status().isNoContent());
-
- mockMvc.perform(post("/projects/{projectId}/employees/{employeeId}", project2.getId(), 312)
- .header(HttpHeaders.AUTHORIZATION, getBearerToken())
- .contentType(MediaType.APPLICATION_JSON))
- .andExpect(status().isConflict());
- }
-
private String getBearerToken() {
String url = "https://keycloak.szut.dev/auth/realms/szut/protocol/openid-connect/token";
@@ -154,4 +78,4 @@ class AddEmployeeToProjectActionIntegrationTest {
return Objects.requireNonNull(response.getBody()).get("access_token").toString();
}
-}
\ No newline at end of file
+}