refactor: Add http requests #23

Closed
ptran wants to merge 10 commits from feature/create-project into main
8 changed files with 260 additions and 260 deletions
Showing only changes of commit 273202842c - Show all commits

@ -59,7 +59,7 @@ docker compose up
# Keycloak # Keycloak
### Keycloak Token ### Keycloak Token
1. Auf der Projektebene [GetBearerToken.http](GetBearerToken.http) öffnen. 1. Auf der Projektebene [GetBearerToken.http](requests/GetBearerToken.http) öffnen.
2. Neben der Request auf den grünen Pfeil drücken 2. Neben der Request auf den grünen Pfeil drücken
3. Aus dem Reponse das access_token kopieren 3. Aus dem Reponse das access_token kopieren

@ -1,15 +1,15 @@
### GET request to example server ### GET request to example server
POST http://localhost:8080/projects POST http://localhost:8080/projects
Authorization: Bearer {{auth_token}} Authorization: Bearer {{auth_token}}
Content-Type: application/json Content-Type: application/json
{ {
"name": "name", "name": "name",
"leading_employee": 1, "leading_employee": 1,
"employees": [2, 3], "employees": [2, 3],
"contractor": 4, "contractor": 4,
"contractor_name": "Peter File", "contractor_name": "Peter File",
"comment": "goal of project", "comment": "goal of project",
"start_date": "01.01.2000", "start_date": "01.01.2000",
"planned_end_date": "01.01.2001" "planned_end_date": "01.01.2001"
} }

@ -1,39 +1,39 @@
package de.szut.lf8_starter.project; package de.szut.lf8_starter.project;
import de.szut.lf8_starter.project.dto.CreateProjectDto; import de.szut.lf8_starter.project.dto.CreateProjectDto;
import de.szut.lf8_starter.project.dto.GetProjectDto; import de.szut.lf8_starter.project.dto.GetProjectDto;
import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content; import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema; 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.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
@RestController @RestController
@RequestMapping(value = "/projects") @RequestMapping(value = "/projects")
public class CreateProjectAction { public class CreateProjectAction {
private final ProjectService projectService; private final ProjectService projectService;
private final ProjectMapper projectMapper; private final ProjectMapper projectMapper;
public CreateProjectAction(ProjectService projectService, ProjectMapper mappingService) { public CreateProjectAction(ProjectService projectService, ProjectMapper mappingService) {
this.projectService = projectService; this.projectService = projectService;
this.projectMapper = mappingService; this.projectMapper = mappingService;
} }
@Operation(summary = "Creates a new Project") @Operation(summary = "Creates a new Project")
@ApiResponses(value = { @ApiResponses(value = {
@ApiResponse(responseCode = "201", description = "created project", content = {@Content(mediaType = "application/json", schema = @Schema(implementation = GetProjectDto.class))}), @ApiResponse(responseCode = "201", description = "created project", content = {@Content(mediaType = "application/json", schema = @Schema(implementation = GetProjectDto.class))}),
@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) @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);
projectEntity = this.projectService.create(projectEntity); projectEntity = this.projectService.create(projectEntity);
return this.projectMapper.mapToGetDto(projectEntity); return this.projectMapper.mapToGetDto(projectEntity);
} }
} }

@ -1,45 +1,45 @@
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.PropertyNamingStrategies;
import com.fasterxml.jackson.databind.annotation.JsonNaming; 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;
import lombok.Setter; import lombok.Setter;
import java.time.LocalDate; import java.time.LocalDate;
import java.util.List; import java.util.List;
@Getter @Getter
@Setter @Setter
@JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class) @JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class)
public class CreateProjectDto { public class CreateProjectDto {
@NotBlank @NotBlank
private String name; private String name;
@NotNull @NotNull
private long leadingEmployee; private long leadingEmployee;
private List<Long> employees; private List<Long> employees;
@NotNull @NotNull
private long contractor; private long contractor;
@NotBlank @NotBlank
private String contractorName; private String contractorName;
@NotBlank @NotBlank
private String comment; private String comment;
@JsonFormat(pattern = "dd.MM.yyyy") @JsonFormat(pattern = "dd.MM.yyyy")
@NotNull @NotNull
private LocalDate startDate; private LocalDate startDate;
@JsonFormat(pattern = "dd.MM.yyyy") @JsonFormat(pattern = "dd.MM.yyyy")
@NotNull @NotNull
private LocalDate plannedEndDate; private LocalDate plannedEndDate;
@JsonFormat(pattern = "dd.MM.yyyy") @JsonFormat(pattern = "dd.MM.yyyy")
private LocalDate endDate; private LocalDate endDate;
} }

@ -1,34 +1,34 @@
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.PropertyNamingStrategies;
import com.fasterxml.jackson.databind.annotation.JsonNaming; 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;
import lombok.Setter; import lombok.Setter;
import java.time.LocalDate; import java.time.LocalDate;
import java.util.List; import java.util.List;
@Getter @Getter
@Setter @Setter
@JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class) @JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class)
public class GetProjectDto { public class GetProjectDto {
private long id; private long id;
private String name; private String name;
private long leadingEmployee; private long leadingEmployee;
private List<Long> employees; private List<Long> employees;
private long contractor; private long contractor;
private String contractorName; private String contractorName;
private String comment; private String comment;
@JsonFormat(pattern = "dd.MM.yyyy") @JsonFormat(pattern = "dd.MM.yyyy")
private LocalDate startDate; private LocalDate startDate;
@JsonFormat(pattern = "dd.MM.yyyy") @JsonFormat(pattern = "dd.MM.yyyy")
private LocalDate plannedEndDate; private LocalDate plannedEndDate;
@JsonFormat(pattern = "dd.MM.yyyy") @JsonFormat(pattern = "dd.MM.yyyy")
private LocalDate endDate; private LocalDate endDate;
} }

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

@ -1,56 +1,56 @@
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.ProjectEntity;
import de.szut.lf8_starter.project.ProjectRepository; import de.szut.lf8_starter.project.ProjectRepository;
import org.junit.jupiter.api.Test; 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.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.MockMvc;
import java.time.LocalDate; import java.time.LocalDate;
import java.util.List; import java.util.List;
import static org.hamcrest.collection.IsCollectionWithSize.hasSize; import static org.hamcrest.collection.IsCollectionWithSize.hasSize;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; 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.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@AutoConfigureMockMvc(addFilters = false) @AutoConfigureMockMvc(addFilters = false)
public class ProjectFindAllSuccessTest { public class ProjectFindAllSuccessTest {
@Autowired @Autowired
private ProjectRepository projectRepository; private ProjectRepository projectRepository;
@Autowired @Autowired
private MockMvc mockMvc; private MockMvc mockMvc;
@Test @Test
void findAllProjectsTest() 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");
project.setContractor(1); project.setContractor(1);
project.setContractorName("contractorName"); project.setContractorName("contractorName");
project.setEndDate(LocalDate.of(2024, 1, 1)); project.setEndDate(LocalDate.of(2024, 1, 1));
project.setLeadingEmployee(1); project.setLeadingEmployee(1);
project.setName("name"); project.setName("name");
project.setStartDate(LocalDate.of(2021, 1, 1)); 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.projectRepository.save(project);
this.mockMvc.perform(get("/projects")) this.mockMvc.perform(get("/projects"))
.andExpect(status().isOk()) .andExpect(status().isOk())
.andExpect(jsonPath("$", hasSize(1))) .andExpect(jsonPath("$", hasSize(1)))
.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].contractor_name").value("contractorName")) .andExpect(jsonPath("$[0].contractor_name").value("contractorName"))
.andExpect(jsonPath("$[0].end_date").value("01.01.2024")) .andExpect(jsonPath("$[0].end_date").value("01.01.2024"))
.andExpect(jsonPath("$[0].leading_employee").value(1)) .andExpect(jsonPath("$[0].leading_employee").value(1))
.andExpect(jsonPath("$[0].name").value("name")) .andExpect(jsonPath("$[0].name").value("name"))
.andExpect(jsonPath("$[0].start_date").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)));
} }
} }