From 41b64e0094f219a86f79afcb3875756d50b63342 Mon Sep 17 00:00:00 2001 From: Phan Huy Tran Date: Wed, 2 Oct 2024 10:14:47 +0200 Subject: [PATCH] Tests --- .../lf8_starter/config/SampleDataCreator.java | 66 +++++++------- .../szut/lf8_starter/hello/HelloEntity.java | 54 ++++++------ .../szut/lf8_starter/hello/HelloMapper.java | 40 ++++----- .../lf8_starter/hello/HelloRepository.java | 24 ++--- .../szut/lf8_starter/hello/HelloService.java | 80 ++++++++--------- .../lf8_starter/hello/dto/HelloCreateDto.java | 40 ++++----- .../lf8_starter/hello/dto/HelloGetDto.java | 34 ++++---- .../project/action/UpdateProjectAction.java | 11 +++ .../project/UpdateProjectActionTest.java | 87 +++++++++++++++++++ 9 files changed, 267 insertions(+), 169 deletions(-) create mode 100644 src/test/java/de/szut/lf8_starter/integration/project/UpdateProjectActionTest.java diff --git a/src/main/java/de/szut/lf8_starter/config/SampleDataCreator.java b/src/main/java/de/szut/lf8_starter/config/SampleDataCreator.java index 9a7b889..373ea11 100644 --- a/src/main/java/de/szut/lf8_starter/config/SampleDataCreator.java +++ b/src/main/java/de/szut/lf8_starter/config/SampleDataCreator.java @@ -1,33 +1,33 @@ -package de.szut.lf8_starter.config; - - -import de.szut.lf8_starter.hello.HelloEntity; -import de.szut.lf8_starter.hello.HelloRepository; -import org.springframework.boot.ApplicationArguments; -import org.springframework.boot.ApplicationRunner; -import org.springframework.context.annotation.Bean; -import org.springframework.stereotype.Component; -import org.springframework.web.client.RestTemplate; - -@Component -public class SampleDataCreator implements ApplicationRunner { - - private HelloRepository repository; - - public SampleDataCreator(HelloRepository repository) { - this.repository = repository; - } - - public void run(ApplicationArguments args) { - repository.save(new HelloEntity("Hallo Welt!")); - repository.save(new HelloEntity("Schöner Tag heute")); - repository.save(new HelloEntity("FooBar")); - - } - - @Bean - public RestTemplate restTemplate() { - return new RestTemplate(); - } - -} +package de.szut.lf8_starter.config; + + +import de.szut.lf8_starter.hello.HelloEntity; +import de.szut.lf8_starter.hello.HelloRepository; +import org.springframework.boot.ApplicationArguments; +import org.springframework.boot.ApplicationRunner; +import org.springframework.context.annotation.Bean; +import org.springframework.stereotype.Component; +import org.springframework.web.client.RestTemplate; + +@Component +public class SampleDataCreator implements ApplicationRunner { + + private HelloRepository repository; + + public SampleDataCreator(HelloRepository repository) { + this.repository = repository; + } + + public void run(ApplicationArguments args) { + repository.save(new HelloEntity("Hallo Welt!")); + repository.save(new HelloEntity("Schöner Tag heute")); + repository.save(new HelloEntity("FooBar")); + + } + + @Bean + public RestTemplate restTemplate() { + return new RestTemplate(); + } + +} diff --git a/src/main/java/de/szut/lf8_starter/hello/HelloEntity.java b/src/main/java/de/szut/lf8_starter/hello/HelloEntity.java index 1b42cfc..f238e2f 100644 --- a/src/main/java/de/szut/lf8_starter/hello/HelloEntity.java +++ b/src/main/java/de/szut/lf8_starter/hello/HelloEntity.java @@ -1,27 +1,27 @@ -package de.szut.lf8_starter.hello; - -import jakarta.persistence.*; -import lombok.AllArgsConstructor; -import lombok.Getter; -import lombok.NoArgsConstructor; -import lombok.Setter; - -@NoArgsConstructor -@AllArgsConstructor -@Getter -@Setter -@Entity -@Table(name = "hello") -public class HelloEntity { - - @Id - @GeneratedValue(strategy = GenerationType.IDENTITY) - private long id; - - private String message; - - public HelloEntity(String message) { - this.message = message; - } -} - +package de.szut.lf8_starter.hello; + +import jakarta.persistence.*; +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; + +@NoArgsConstructor +@AllArgsConstructor +@Getter +@Setter +@Entity +@Table(name = "hello") +public class HelloEntity { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private long id; + + private String message; + + public HelloEntity(String message) { + this.message = message; + } +} + diff --git a/src/main/java/de/szut/lf8_starter/hello/HelloMapper.java b/src/main/java/de/szut/lf8_starter/hello/HelloMapper.java index d4feb7e..5639671 100644 --- a/src/main/java/de/szut/lf8_starter/hello/HelloMapper.java +++ b/src/main/java/de/szut/lf8_starter/hello/HelloMapper.java @@ -1,20 +1,20 @@ -package de.szut.lf8_starter.hello; - - -import de.szut.lf8_starter.hello.dto.HelloCreateDto; -import de.szut.lf8_starter.hello.dto.HelloGetDto; -import org.springframework.stereotype.Service; - -@Service -public class HelloMapper { - - public HelloGetDto mapToGetDto(HelloEntity entity) { - return new HelloGetDto(entity.getId(), entity.getMessage()); - } - - public HelloEntity mapCreateDtoToEntity(HelloCreateDto dto) { - var entity = new HelloEntity(); - entity.setMessage(dto.getMessage()); - return entity; - } -} +package de.szut.lf8_starter.hello; + + +import de.szut.lf8_starter.hello.dto.HelloCreateDto; +import de.szut.lf8_starter.hello.dto.HelloGetDto; +import org.springframework.stereotype.Service; + +@Service +public class HelloMapper { + + public HelloGetDto mapToGetDto(HelloEntity entity) { + return new HelloGetDto(entity.getId(), entity.getMessage()); + } + + public HelloEntity mapCreateDtoToEntity(HelloCreateDto dto) { + var entity = new HelloEntity(); + entity.setMessage(dto.getMessage()); + return entity; + } +} diff --git a/src/main/java/de/szut/lf8_starter/hello/HelloRepository.java b/src/main/java/de/szut/lf8_starter/hello/HelloRepository.java index 77c76e4..a030a27 100644 --- a/src/main/java/de/szut/lf8_starter/hello/HelloRepository.java +++ b/src/main/java/de/szut/lf8_starter/hello/HelloRepository.java @@ -1,12 +1,12 @@ -package de.szut.lf8_starter.hello; - - -import org.springframework.data.jpa.repository.JpaRepository; - -import java.util.List; - -public interface HelloRepository extends JpaRepository { - - - List findByMessage(String message); -} +package de.szut.lf8_starter.hello; + + +import org.springframework.data.jpa.repository.JpaRepository; + +import java.util.List; + +public interface HelloRepository extends JpaRepository { + + + List findByMessage(String message); +} diff --git a/src/main/java/de/szut/lf8_starter/hello/HelloService.java b/src/main/java/de/szut/lf8_starter/hello/HelloService.java index eb254e8..b608ee2 100644 --- a/src/main/java/de/szut/lf8_starter/hello/HelloService.java +++ b/src/main/java/de/szut/lf8_starter/hello/HelloService.java @@ -1,40 +1,40 @@ -package de.szut.lf8_starter.hello; - -import org.springframework.stereotype.Service; - -import java.util.List; -import java.util.Optional; - -@Service -public class HelloService { - private final HelloRepository repository; - - public HelloService(HelloRepository repository) { - this.repository = repository; - } - - public HelloEntity create(HelloEntity entity) { - return this.repository.save(entity); - } - - public List readAll() { - return this.repository.findAll(); - } - - public HelloEntity readById(long id) { - Optional optionalQualification = this.repository.findById(id); - if (optionalQualification.isEmpty()) { - return null; - } - return optionalQualification.get(); - } - - - public void delete(HelloEntity entity) { - this.repository.delete(entity); - } - - public List findByMessage(String message) { - return this.repository.findByMessage(message); - } -} +package de.szut.lf8_starter.hello; + +import org.springframework.stereotype.Service; + +import java.util.List; +import java.util.Optional; + +@Service +public class HelloService { + private final HelloRepository repository; + + public HelloService(HelloRepository repository) { + this.repository = repository; + } + + public HelloEntity create(HelloEntity entity) { + return this.repository.save(entity); + } + + public List readAll() { + return this.repository.findAll(); + } + + public HelloEntity readById(long id) { + Optional optionalQualification = this.repository.findById(id); + if (optionalQualification.isEmpty()) { + return null; + } + return optionalQualification.get(); + } + + + public void delete(HelloEntity entity) { + this.repository.delete(entity); + } + + public List findByMessage(String message) { + return this.repository.findByMessage(message); + } +} diff --git a/src/main/java/de/szut/lf8_starter/hello/dto/HelloCreateDto.java b/src/main/java/de/szut/lf8_starter/hello/dto/HelloCreateDto.java index fe47d1e..e0622d5 100644 --- a/src/main/java/de/szut/lf8_starter/hello/dto/HelloCreateDto.java +++ b/src/main/java/de/szut/lf8_starter/hello/dto/HelloCreateDto.java @@ -1,20 +1,20 @@ -package de.szut.lf8_starter.hello.dto; - -import com.fasterxml.jackson.annotation.JsonCreator; -import jakarta.validation.constraints.Size; -import lombok.Getter; -import lombok.Setter; - - -@Getter -@Setter -public class HelloCreateDto { - - @Size(min = 3, message = "at least length of 3") - private String message; - - @JsonCreator - public HelloCreateDto(String message) { - this.message = message; - } -} +package de.szut.lf8_starter.hello.dto; + +import com.fasterxml.jackson.annotation.JsonCreator; +import jakarta.validation.constraints.Size; +import lombok.Getter; +import lombok.Setter; + + +@Getter +@Setter +public class HelloCreateDto { + + @Size(min = 3, message = "at least length of 3") + private String message; + + @JsonCreator + public HelloCreateDto(String message) { + this.message = message; + } +} diff --git a/src/main/java/de/szut/lf8_starter/hello/dto/HelloGetDto.java b/src/main/java/de/szut/lf8_starter/hello/dto/HelloGetDto.java index babac4f..1cc51b2 100644 --- a/src/main/java/de/szut/lf8_starter/hello/dto/HelloGetDto.java +++ b/src/main/java/de/szut/lf8_starter/hello/dto/HelloGetDto.java @@ -1,17 +1,17 @@ -package de.szut.lf8_starter.hello.dto; - -import lombok.AllArgsConstructor; -import lombok.Getter; -import lombok.Setter; - -@AllArgsConstructor -@Getter -@Setter -public class HelloGetDto { - - private long id; - - private String message; - -} - +package de.szut.lf8_starter.hello.dto; + +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.Setter; + +@AllArgsConstructor +@Getter +@Setter +public class HelloGetDto { + + private long id; + + private String message; + +} + diff --git a/src/main/java/de/szut/lf8_starter/project/action/UpdateProjectAction.java b/src/main/java/de/szut/lf8_starter/project/action/UpdateProjectAction.java index 1e94754..e145837 100644 --- a/src/main/java/de/szut/lf8_starter/project/action/UpdateProjectAction.java +++ b/src/main/java/de/szut/lf8_starter/project/action/UpdateProjectAction.java @@ -5,6 +5,11 @@ import de.szut.lf8_starter.project.ProjectMapper; import de.szut.lf8_starter.project.ProjectService; import de.szut.lf8_starter.project.dto.GetProjectDto; import de.szut.lf8_starter.project.dto.UpdateProjectDto; +import io.swagger.v3.oas.annotations.Operation; +import io.swagger.v3.oas.annotations.media.Content; +import io.swagger.v3.oas.annotations.media.Schema; +import io.swagger.v3.oas.annotations.responses.ApiResponse; +import io.swagger.v3.oas.annotations.responses.ApiResponses; import jakarta.validation.Valid; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; @@ -23,6 +28,12 @@ public class UpdateProjectAction { this.projectMapper = mappingService; } + @Operation(summary = "Update a project by ID") + @ApiResponses(value = { + @ApiResponse(responseCode = "200", description = "Project updated successfully", + content = @Content(mediaType = "application/json", schema = @Schema(implementation = GetProjectDto.class))), + @ApiResponse(responseCode = "404", description = "Project not found", content = @Content) + }) @PutMapping("/{id}") public ResponseEntity updateSupplier(@PathVariable Long id, @Valid @RequestBody UpdateProjectDto updateProjectDto) { Optional project = this.projectService.findById(id); diff --git a/src/test/java/de/szut/lf8_starter/integration/project/UpdateProjectActionTest.java b/src/test/java/de/szut/lf8_starter/integration/project/UpdateProjectActionTest.java new file mode 100644 index 0000000..795d0bb --- /dev/null +++ b/src/test/java/de/szut/lf8_starter/integration/project/UpdateProjectActionTest.java @@ -0,0 +1,87 @@ +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; +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 java.util.List; + +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.put; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +@SpringBootTest +@AutoConfigureMockMvc(addFilters = false) +class UpdateProjectActionTest { + @Autowired + private MockMvc mockMvc; + @Autowired + private ProjectRepository projectRepository; + + @Test + void updateProjectTest() throws Exception { + ProjectEntity project = new ProjectEntity(); + project.setId(1); + project.setComment("comment"); + project.setContractor(1); + project.setContractorName("contractorName"); + project.setEndDate(LocalDate.of(2024, 1, 1)); + project.setLeadingEmployee(1); + project.setName("name"); + project.setStartDate(LocalDate.of(2021, 1, 1)); + project.setEmployees(List.of(1L, 2L, 3L)); + this.projectRepository.save(project); + + String content = """ + { + "name": "updatedName", + "leading_employee": 2, + "employees": [3, 4, 5], + "contractor": 6, + "contractor_name": "Updated Contractor name", + "comment": "new goal of project", + "start_date": "01.01.2021", + "planned_end_date": "01.01.2022" + } + """; + + final var contentAsString = this.mockMvc.perform( + put("/projects/1").content(content).contentType(MediaType.APPLICATION_JSON) + ) + .andExpect(status().isOk()) + .andExpect(jsonPath("name", is("updatedName"))) + .andExpect(jsonPath("leading_employee", is(2))) + .andExpect(jsonPath("employees", is(Arrays.asList(3, 4, 5)))) + .andExpect(jsonPath("contractor", is(6))) + .andExpect(jsonPath("contractor_name", is("Updated Contractor name"))) + .andExpect(jsonPath("comment", is("new goal of project"))) + .andExpect(jsonPath("start_date", is("01.01.2021"))) + .andExpect(jsonPath("planned_end_date", is("01.01.2022"))) + .andReturn() + .getResponse() + .getContentAsString(); + + final var id = Long.parseLong(new JSONObject(contentAsString).get("id").toString()); + + final var existingProject = this.projectRepository.findById(id); + assertThat(existingProject.get().getName()).isEqualTo("updatedName"); + assertThat(existingProject.get().getLeadingEmployee()).isEqualTo(2); + assertThat(existingProject.get().getContractor()).isEqualTo(6); + assertThat(existingProject.get().getContractorName()).isEqualTo("Updated Contractor name"); + assertThat(existingProject.get().getComment()).isEqualTo("new goal of project"); + assertThat(existingProject.get().getStartDate()).isEqualTo(LocalDate.of(2021, 1, 1)); + assertThat(existingProject.get().getPlannedEndDate()).isEqualTo(LocalDate.of(2022, 1, 1)); + + } +}