feat: Implement add employee to project route (SCRUM-2) #32

Merged
jank merged 8 commits from feature/add-employee-to-project into main 2024-10-23 08:39:26 +00:00
6 changed files with 30 additions and 32 deletions
Showing only changes of commit cda04fa8a2 - Show all commits

@ -6,10 +6,7 @@ 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.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.*;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
@ -37,7 +34,7 @@ class AddEmployeeToProjectActionIntegrationTest {
@Test
void addEmployeeToProjectTest() throws Exception {
var project = new ProjectEntity();
ProjectEntity project = new ProjectEntity();
project.setComment("comment");
project.setContractor(1);
project.setContractorName("contractorName");
ptran marked this conversation as resolved
Review

dont use var

dont use var
@ -54,7 +51,7 @@ class AddEmployeeToProjectActionIntegrationTest {
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isNoContent());
var updatedProject = projectRepository.findById(1L).get();
ProjectEntity updatedProject = projectRepository.findById(1L).get();
assert updatedProject.getEmployees().contains(312L);
}
ptran marked this conversation as resolved Outdated
Outdated
Review

also here

also here
@ -73,7 +70,7 @@ class AddEmployeeToProjectActionIntegrationTest {
HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<>(map, headers);
var response = this.restTemplate.exchange(url, HttpMethod.POST, request, Map.class);
ResponseEntity<Map> response = this.restTemplate.exchange(url, HttpMethod.POST, request, Map.class);
return Objects.requireNonNull(response.getBody()).get("access_token").toString();
}
ptran marked this conversation as resolved Outdated
Outdated
Review

and here

and here

@ -1,5 +1,6 @@
package de.szut.lf8_starter.integration.project;
import de.szut.lf8_starter.project.ProjectEntity;
import de.szut.lf8_starter.project.ProjectRepository;
import org.json.JSONObject;
import org.junit.jupiter.api.Test;
@ -12,10 +13,9 @@ import org.springframework.test.web.servlet.MockMvc;
import java.time.LocalDate;
import java.util.Arrays;
import static org.assertj.core.api.Assertions.assertThat;
import static org.hamcrest.Matchers.is;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.hamcrest.Matchers.is;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@SpringBootTest
@ -41,7 +41,7 @@ class CreateProjectActionTest {
}
""";
final var contentAsString = this.mockMvc.perform(
final String contentAsString = this.mockMvc.perform(
post("/projects").content(content).contentType(MediaType.APPLICATION_JSON)
)
.andExpect(status().isCreated())
@ -57,9 +57,9 @@ class CreateProjectActionTest {
.getResponse()
.getContentAsString();
final var id = Long.parseLong(new JSONObject(contentAsString).get("id").toString());
final long id = Long.parseLong(new JSONObject(contentAsString).get("id").toString());
final var project = this.projectRepository.findById(id);
final ProjectEntity project = this.projectRepository.findById(id);
assertThat(project.get().getName()).isEqualTo("name");
assertThat(project.get().getLeadingEmployee()).isEqualTo(1);
assertThat(project.get().getContractor()).isEqualTo(4);

@ -26,7 +26,7 @@ class GetProjectActionTest {
@Test
void getProjectTest() throws Exception {
var project = new ProjectEntity();
ProjectEntity project = new ProjectEntity();
project.setId(1);
project.setComment("comment");
project.setContractor(1);

@ -27,7 +27,7 @@ class ProjectFindAllSuccessTest {
@Test
void findAllProjectsTest() throws Exception {
var project = new ProjectEntity();
ProjectEntity project = new ProjectEntity();
project.setId(1);
project.setComment("comment");
project.setContractor(1);
@ -36,7 +36,7 @@ class ProjectFindAllSuccessTest {
project.setLeadingEmployee(1);
project.setName("name");
project.setStartDate(LocalDate.of(2021, 1, 1));
project.setEmployees(List.of(1L,2L,3L));
project.setEmployees(List.of(1L, 2L, 3L));
this.projectRepository.save(project);
this.mockMvc.perform(get("/projects"))

@ -25,7 +25,7 @@ class RemoveProjectActionTest {
@Test
void deleteProjectTest() throws Exception {
var project = new ProjectEntity();
ProjectEntity project = new ProjectEntity();
project.setComment("comment");
project.setContractor(1);
project.setContractorName("contractorName");

@ -13,6 +13,7 @@ import org.springframework.test.web.servlet.MockMvc;
import java.time.LocalDate;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import static org.assertj.core.api.Assertions.assertThat;
import static org.hamcrest.Matchers.is;
@ -55,7 +56,7 @@ class UpdateProjectActionTest {
}
""";
final var contentAsString = this.mockMvc.perform(
final String contentAsString = this.mockMvc.perform(
put("/projects/1").content(content).contentType(MediaType.APPLICATION_JSON)
)
.andExpect(status().isOk())
@ -71,9 +72,9 @@ class UpdateProjectActionTest {
.getResponse()
.getContentAsString();
final var id = Long.parseLong(new JSONObject(contentAsString).get("id").toString());
final long id = Long.parseLong(new JSONObject(contentAsString).get("id").toString());
final var existingProject = this.projectRepository.findById(id);
final Optional<ProjectEntity> existingProject = this.projectRepository.findById(id);
assertThat(existingProject.get().getName()).isEqualTo("updatedName");
assertThat(existingProject.get().getLeadingEmployee()).isEqualTo(2);
assertThat(existingProject.get().getContractor()).isEqualTo(6);
@ -103,13 +104,13 @@ class UpdateProjectActionTest {
{}
""";
final var contentAsString = this.mockMvc.perform(
final String contentAsString = this.mockMvc.perform(
put("/projects/1").content(content).contentType(MediaType.APPLICATION_JSON)
)
.andExpect(status().isOk())
.andExpect(jsonPath("name", is("name")))
.andExpect(jsonPath("leading_employee", is(1)))
.andExpect(jsonPath("employees", is(List.of(1,2,3))))
.andExpect(jsonPath("employees", is(List.of(1, 2, 3))))
.andExpect(jsonPath("contractor", is(1)))
.andExpect(jsonPath("contractor_name", is("contractorName")))
.andExpect(jsonPath("comment", is("comment")))
@ -120,9 +121,9 @@ class UpdateProjectActionTest {
.getResponse()
.getContentAsString();
final var id = Long.parseLong(new JSONObject(contentAsString).get("id").toString());
final long id = Long.parseLong(new JSONObject(contentAsString).get("id").toString());
final var existingProject = this.projectRepository.findById(id);
final Optional<ProjectEntity> existingProject = this.projectRepository.findById(id);
assertThat(existingProject.get().getName()).isEqualTo("name");
assertThat(existingProject.get().getLeadingEmployee()).isEqualTo(1);
assertThat(existingProject.get().getContractor()).isEqualTo(1);