feat: Write Integration test for Create Project Route and ensure using Snake Case in JSON Format (SCRUM-7) (!14)
All checks were successful
Quality Check / Tests (push) Successful in 53s
Quality Check / Checkstyle Main (push) Successful in 56s
Release / Release (push) Successful in 38s

Co-authored-by: Phan Huy Tran <p.tran@neusta.de>
Reviewed-on: #14
Reviewed-by: Jan Gleytenhoover <krisellp9@gmail.com>
This commit is contained in:
Phan Huy Tran 2024-09-25 12:27:34 +00:00
parent f53a317e72
commit 013e964be6
6 changed files with 89 additions and 31 deletions

@ -8,8 +8,8 @@ Content-Type: application/json
"leading_employee": 1, "leading_employee": 1,
"employees": [2, 3], "employees": [2, 3],
"contractor": 4, "contractor": 4,
"contractorName": "Peter File", "contractor_name": "Peter File",
"comment": "goal of project", "comment": "goal of project",
"startDate": "01.01.2000", "start_date": "01.01.2000",
"plannedEndDate": "01.01.2001" "planned_end_date": "01.01.2001"
} }

@ -8,10 +8,8 @@ import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse; import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses; import io.swagger.v3.oas.annotations.responses.ApiResponses;
import jakarta.validation.Valid; import jakarta.validation.Valid;
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController @RestController
@RequestMapping(value = "/projects") @RequestMapping(value = "/projects")
@ -30,6 +28,7 @@ public class CreateProjectAction {
@ApiResponse(responseCode = "400", description = "invalid JSON posted", content = @Content), @ApiResponse(responseCode = "400", description = "invalid JSON posted", content = @Content),
@ApiResponse(responseCode = "401", description = "not authorized", content = @Content)}) @ApiResponse(responseCode = "401", description = "not authorized", content = @Content)})
@PostMapping @PostMapping
@ResponseStatus(code = HttpStatus.CREATED)
public GetProjectDto create(@RequestBody @Valid CreateProjectDto createProjectDto) { public GetProjectDto create(@RequestBody @Valid CreateProjectDto createProjectDto) {
ProjectEntity projectEntity = this.projectMapper.mapCreateDtoToEntity(createProjectDto); ProjectEntity projectEntity = this.projectMapper.mapCreateDtoToEntity(createProjectDto);

@ -1,6 +1,8 @@
package de.szut.lf8_starter.project.dto; package de.szut.lf8_starter.project.dto;
import com.fasterxml.jackson.annotation.JsonFormat; import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
import com.fasterxml.jackson.databind.annotation.JsonNaming;
import jakarta.validation.constraints.NotBlank; import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull; import jakarta.validation.constraints.NotNull;
import lombok.Getter; import lombok.Getter;
@ -11,6 +13,7 @@ import java.util.List;
@Getter @Getter
@Setter @Setter
@JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class)
public class CreateProjectDto { public class CreateProjectDto {
@NotBlank @NotBlank
private String name; private String name;

@ -1,6 +1,8 @@
package de.szut.lf8_starter.project.dto; package de.szut.lf8_starter.project.dto;
import com.fasterxml.jackson.annotation.JsonFormat; import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
import com.fasterxml.jackson.databind.annotation.JsonNaming;
import jakarta.validation.constraints.NotBlank; import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull; import jakarta.validation.constraints.NotNull;
import lombok.Getter; import lombok.Getter;
@ -11,35 +13,22 @@ import java.util.List;
@Getter @Getter
@Setter @Setter
@JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class)
public class GetProjectDto { public class GetProjectDto {
private long id; private long id;
@NotBlank
private String name; private String name;
@NotNull
private long leadingEmployee; private long leadingEmployee;
private List<Long> employees; private List<Long> employees;
@NotNull
private long contractor; private long contractor;
@NotBlank
private String contractorName; private String contractorName;
@NotBlank
private String comment; private String comment;
@NotNull
@JsonFormat(pattern = "dd.MM.yyyy") @JsonFormat(pattern = "dd.MM.yyyy")
private LocalDate startDate; private LocalDate startDate;
@NotNull
@JsonFormat(pattern = "dd.MM.yyyy") @JsonFormat(pattern = "dd.MM.yyyy")
private LocalDate plannedEndDate; private LocalDate plannedEndDate;
@NotNull
@JsonFormat(pattern = "dd.MM.yyyy") @JsonFormat(pattern = "dd.MM.yyyy")
private LocalDate endDate; private LocalDate endDate;
} }

@ -0,0 +1,71 @@
package de.szut.lf8_starter.integration.project;
import de.szut.lf8_starter.project.ProjectRepository;
import org.json.JSONObject;
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.MediaType;
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.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
@AutoConfigureMockMvc(addFilters = false)
public class CreateProjectActionTest {
@Autowired
private MockMvc mockMvc;
@Autowired
private ProjectRepository projectRepository;
@Test
void createProjectTest() throws Exception {
String content = """
{
"name": "name",
"leading_employee": 1,
"employees": [2, 3],
"contractor": 4,
"contractor_name": "Peter File",
"comment": "goal of project",
"start_date": "01.01.2000",
"planned_end_date": "01.01.2001"
}
""";
final var contentAsString = this.mockMvc.perform(
post("/projects").content(content).contentType(MediaType.APPLICATION_JSON)
)
.andExpect(status().isCreated())
.andExpect(jsonPath("name", is("name")))
.andExpect(jsonPath("leading_employee", is(1)))
.andExpect(jsonPath("employees", is(Arrays.asList(2, 3))))
.andExpect(jsonPath("contractor", is(4)))
.andExpect(jsonPath("contractor_name", is("Peter File")))
.andExpect(jsonPath("comment", is("goal of project")))
.andExpect(jsonPath("start_date", is("01.01.2000")))
.andExpect(jsonPath("planned_end_date", is("01.01.2001")))
.andReturn()
.getResponse()
.getContentAsString();
final var id = Long.parseLong(new JSONObject(contentAsString).get("id").toString());
final var project = this.projectRepository.findById(id);
assertThat(project.get().getName()).isEqualTo("name");
assertThat(project.get().getLeadingEmployee()).isEqualTo(1);
assertThat(project.get().getContractor()).isEqualTo(4);
assertThat(project.get().getContractorName()).isEqualTo("Peter File");
assertThat(project.get().getComment()).isEqualTo("goal of project");
assertThat(project.get().getStartDate()).isEqualTo(LocalDate.of(2000, 1, 1));
assertThat(project.get().getPlannedEndDate()).isEqualTo(LocalDate.of(2001, 1, 1));
}
}

@ -6,7 +6,6 @@ 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.boot.test.web.client.TestRestTemplate;
import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.MockMvc;
import java.time.LocalDate; import java.time.LocalDate;
@ -19,18 +18,15 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@AutoConfigureMockMvc(addFilters = false) @AutoConfigureMockMvc(addFilters = false)
public class ProjectFindAllSuccess { public class ProjectFindAllSuccessTest {
@Autowired @Autowired
private ProjectRepository projectRepository; private ProjectRepository projectRepository;
@Autowired @Autowired
private MockMvc mockMvc; private MockMvc mockMvc;
@Autowired
private TestRestTemplate restTemplate;
@Test @Test
void findAllProjects() throws Exception { void findAllProjectsTest() throws Exception {
var project = new ProjectEntity(); var project = new ProjectEntity();
project.setId(1); project.setId(1);
project.setComment("comment"); project.setComment("comment");
@ -49,11 +45,11 @@ public class ProjectFindAllSuccess {
.andExpect(jsonPath("$[0].id").value(1)) .andExpect(jsonPath("$[0].id").value(1))
.andExpect(jsonPath("$[0].comment").value("comment")) .andExpect(jsonPath("$[0].comment").value("comment"))
.andExpect(jsonPath("$[0].contractor").value(1)) .andExpect(jsonPath("$[0].contractor").value(1))
.andExpect(jsonPath("$[0].contractorName").value("contractorName")) .andExpect(jsonPath("$[0].contractor_name").value("contractorName"))
.andExpect(jsonPath("$[0].endDate").value("01.01.2024")) .andExpect(jsonPath("$[0].end_date").value("01.01.2024"))
.andExpect(jsonPath("$[0].leadingEmployee").value(1)) .andExpect(jsonPath("$[0].leading_employee").value(1))
.andExpect(jsonPath("$[0].name").value("name")) .andExpect(jsonPath("$[0].name").value("name"))
.andExpect(jsonPath("$[0].startDate").value("01.01.2021")) .andExpect(jsonPath("$[0].start_date").value("01.01.2021"))
.andExpect(jsonPath("$[0].employees").isArray()) .andExpect(jsonPath("$[0].employees").isArray())
.andExpect(jsonPath("$[0].employees", hasSize(3))); .andExpect(jsonPath("$[0].employees", hasSize(3)));
} }