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.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.HttpEntity; import org.springframework.http.*;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.MockMvc;
import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap; import org.springframework.util.MultiValueMap;
@ -37,7 +34,7 @@ class AddEmployeeToProjectActionIntegrationTest {
@Test @Test
void addEmployeeToProjectTest() throws Exception { void addEmployeeToProjectTest() throws Exception {
var project = new ProjectEntity(); ProjectEntity project = new ProjectEntity();
project.setComment("comment"); project.setComment("comment");
project.setContractor(1); project.setContractor(1);
project.setContractorName("contractorName"); 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)) .contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isNoContent()); .andExpect(status().isNoContent());
var updatedProject = projectRepository.findById(1L).get(); ProjectEntity updatedProject = projectRepository.findById(1L).get();
assert updatedProject.getEmployees().contains(312L); assert updatedProject.getEmployees().contains(312L);
} }
@ -73,7 +70,7 @@ class AddEmployeeToProjectActionIntegrationTest {
HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<>(map, headers); 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(); return Objects.requireNonNull(response.getBody()).get("access_token").toString();
} }

@ -1,5 +1,6 @@
package de.szut.lf8_starter.integration.project; package de.szut.lf8_starter.integration.project;
import de.szut.lf8_starter.project.ProjectEntity;
import de.szut.lf8_starter.project.ProjectRepository; import de.szut.lf8_starter.project.ProjectRepository;
import org.json.JSONObject; import org.json.JSONObject;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
@ -12,10 +13,9 @@ import org.springframework.test.web.servlet.MockMvc;
import java.time.LocalDate; import java.time.LocalDate;
import java.util.Arrays; 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.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; 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; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@SpringBootTest @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) post("/projects").content(content).contentType(MediaType.APPLICATION_JSON)
) )
.andExpect(status().isCreated()) .andExpect(status().isCreated())
@ -57,9 +57,9 @@ class CreateProjectActionTest {
.getResponse() .getResponse()
.getContentAsString(); .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().getName()).isEqualTo("name");
assertThat(project.get().getLeadingEmployee()).isEqualTo(1); assertThat(project.get().getLeadingEmployee()).isEqualTo(1);
assertThat(project.get().getContractor()).isEqualTo(4); assertThat(project.get().getContractor()).isEqualTo(4);

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

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

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

@ -13,6 +13,7 @@ import org.springframework.test.web.servlet.MockMvc;
import java.time.LocalDate; import java.time.LocalDate;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.Optional;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.hamcrest.Matchers.is; 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) put("/projects/1").content(content).contentType(MediaType.APPLICATION_JSON)
) )
.andExpect(status().isOk()) .andExpect(status().isOk())
@ -71,9 +72,9 @@ class UpdateProjectActionTest {
.getResponse() .getResponse()
.getContentAsString(); .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().getName()).isEqualTo("updatedName");
assertThat(existingProject.get().getLeadingEmployee()).isEqualTo(2); assertThat(existingProject.get().getLeadingEmployee()).isEqualTo(2);
assertThat(existingProject.get().getContractor()).isEqualTo(6); assertThat(existingProject.get().getContractor()).isEqualTo(6);
@ -103,7 +104,7 @@ class UpdateProjectActionTest {
{} {}
"""; """;
final var contentAsString = this.mockMvc.perform( final String contentAsString = this.mockMvc.perform(
put("/projects/1").content(content).contentType(MediaType.APPLICATION_JSON) put("/projects/1").content(content).contentType(MediaType.APPLICATION_JSON)
) )
.andExpect(status().isOk()) .andExpect(status().isOk())
@ -120,9 +121,9 @@ class UpdateProjectActionTest {
.getResponse() .getResponse()
.getContentAsString(); .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().getName()).isEqualTo("name");
assertThat(existingProject.get().getLeadingEmployee()).isEqualTo(1); assertThat(existingProject.get().getLeadingEmployee()).isEqualTo(1);
assertThat(existingProject.get().getContractor()).isEqualTo(1); assertThat(existingProject.get().getContractor()).isEqualTo(1);