From 25d7d30a7dac1e49fef4760259f2a9808daa238f Mon Sep 17 00:00:00 2001 From: Jan Gleytenhoover Date: Wed, 25 Sep 2024 11:38:31 +0000 Subject: [PATCH] feat(project): Get all projects (SCRUM-15) (!10) SCRUM-15 #close Reviewed-on: https://git.kjan.de/jank/LF8/pulls/10 Reviewed-by: Phan Huy Tran --- .gitea/workflows/qs.yml | 4 +- .../project/ProjectController.java | 38 ++++++++++++ .../lf8_starter/project/ProjectMapper.java | 1 + .../project/ProjectRepository.java | 2 + .../lf8_starter/project/ProjectService.java | 6 ++ .../project/dto/GetProjectDto.java | 2 + .../security/KeycloakSecurityConfig.java | 1 + .../ProjectFindAllNotAuthenticated.java | 37 ++++++++++++ .../project/ProjectFindAllSuccess.java | 60 +++++++++++++++++++ 9 files changed, 149 insertions(+), 2 deletions(-) create mode 100644 src/main/java/de/szut/lf8_starter/project/ProjectController.java create mode 100644 src/test/java/de/szut/lf8_starter/integration/project/ProjectFindAllNotAuthenticated.java create mode 100644 src/test/java/de/szut/lf8_starter/integration/project/ProjectFindAllSuccess.java diff --git a/.gitea/workflows/qs.yml b/.gitea/workflows/qs.yml index a3646cb..7c74bf8 100644 --- a/.gitea/workflows/qs.yml +++ b/.gitea/workflows/qs.yml @@ -23,7 +23,7 @@ jobs: - name: "Prepare Gradle" run: gradle clean - name: "Check" - run: gradle testClasses + run: gradle test - name: "Stop Gradle" run: gradle --stop checkstyle: @@ -45,6 +45,6 @@ jobs: - name: "Prepare Gradle" run: gradle clean - name: "Check" - run: gradle check + run: gradle checkstyleMain --scan - name: "Stop Gradle" run: gradle --stop diff --git a/src/main/java/de/szut/lf8_starter/project/ProjectController.java b/src/main/java/de/szut/lf8_starter/project/ProjectController.java new file mode 100644 index 0000000..61ee843 --- /dev/null +++ b/src/main/java/de/szut/lf8_starter/project/ProjectController.java @@ -0,0 +1,38 @@ +package de.szut.lf8_starter.project; + +import de.szut.lf8_starter.project.dto.GetProjectDto; +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 org.springframework.web.bind.annotation.*; + +import java.util.List; + +@RestController +@RequestMapping(value = "projects") +public class ProjectController { + private final ProjectService service; + private final ProjectMapper projectMapper; + public ProjectController(ProjectService service, ProjectMapper projectMapper) { + this.service = service; + this.projectMapper = projectMapper; + } + + @Operation(summary = "returns all projects") + @ApiResponses(value = { + @ApiResponse(responseCode = "200", description = "all projects", + content = {@Content(mediaType = "application/json", + schema = @Schema(implementation = GetProjectDto.class))}), + @ApiResponse(responseCode = "401", description = "not authorized", + content = @Content)}) + @GetMapping + public List findAll() { + return this.service + .readAll() + .stream() + .map(this.projectMapper::mapToGetDto) + .toList(); + } +} diff --git a/src/main/java/de/szut/lf8_starter/project/ProjectMapper.java b/src/main/java/de/szut/lf8_starter/project/ProjectMapper.java index 6567749..fa60491 100644 --- a/src/main/java/de/szut/lf8_starter/project/ProjectMapper.java +++ b/src/main/java/de/szut/lf8_starter/project/ProjectMapper.java @@ -25,6 +25,7 @@ public class ProjectMapper { public GetProjectDto mapToGetDto(ProjectEntity projectEntity) { GetProjectDto getProjectDto = new GetProjectDto(); + getProjectDto.setId(projectEntity.getId()); getProjectDto.setName(projectEntity.getName()); getProjectDto.setComment(projectEntity.getComment()); getProjectDto.setLeadingEmployee(projectEntity.getLeadingEmployee()); diff --git a/src/main/java/de/szut/lf8_starter/project/ProjectRepository.java b/src/main/java/de/szut/lf8_starter/project/ProjectRepository.java index 9798fc8..6f7e33f 100644 --- a/src/main/java/de/szut/lf8_starter/project/ProjectRepository.java +++ b/src/main/java/de/szut/lf8_starter/project/ProjectRepository.java @@ -1,6 +1,8 @@ package de.szut.lf8_starter.project; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; +@Repository public interface ProjectRepository extends JpaRepository { } diff --git a/src/main/java/de/szut/lf8_starter/project/ProjectService.java b/src/main/java/de/szut/lf8_starter/project/ProjectService.java index a12228f..4dc8688 100644 --- a/src/main/java/de/szut/lf8_starter/project/ProjectService.java +++ b/src/main/java/de/szut/lf8_starter/project/ProjectService.java @@ -2,6 +2,8 @@ package de.szut.lf8_starter.project; import org.springframework.stereotype.Service; +import java.util.List; + @Service public class ProjectService { private final ProjectRepository projectRepository; @@ -13,4 +15,8 @@ public class ProjectService { public ProjectEntity create(ProjectEntity projectEntity) { return this.projectRepository.save(projectEntity); } + + public List readAll() { + return this.projectRepository.findAll(); + } } diff --git a/src/main/java/de/szut/lf8_starter/project/dto/GetProjectDto.java b/src/main/java/de/szut/lf8_starter/project/dto/GetProjectDto.java index cc337c7..c90cd50 100644 --- a/src/main/java/de/szut/lf8_starter/project/dto/GetProjectDto.java +++ b/src/main/java/de/szut/lf8_starter/project/dto/GetProjectDto.java @@ -12,6 +12,8 @@ import java.util.List; @Getter @Setter public class GetProjectDto { + private long id; + @NotBlank private String name; diff --git a/src/main/java/de/szut/lf8_starter/security/KeycloakSecurityConfig.java b/src/main/java/de/szut/lf8_starter/security/KeycloakSecurityConfig.java index ea9e354..8ab3fd8 100644 --- a/src/main/java/de/szut/lf8_starter/security/KeycloakSecurityConfig.java +++ b/src/main/java/de/szut/lf8_starter/security/KeycloakSecurityConfig.java @@ -5,6 +5,7 @@ import java.util.stream.Collectors; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Profile; import org.springframework.http.HttpMethod; import org.springframework.security.config.Customizer; import org.springframework.security.config.annotation.web.builders.HttpSecurity; diff --git a/src/test/java/de/szut/lf8_starter/integration/project/ProjectFindAllNotAuthenticated.java b/src/test/java/de/szut/lf8_starter/integration/project/ProjectFindAllNotAuthenticated.java new file mode 100644 index 0000000..8c62848 --- /dev/null +++ b/src/test/java/de/szut/lf8_starter/integration/project/ProjectFindAllNotAuthenticated.java @@ -0,0 +1,37 @@ +package de.szut.lf8_starter.integration.project; + +import de.szut.lf8_starter.project.ProjectEntity; +import de.szut.lf8_starter.project.ProjectRepository; +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.boot.test.web.client.TestRestTemplate; +import org.springframework.test.web.servlet.MockMvc; + +import java.time.LocalDate; +import java.util.List; + +import static org.hamcrest.collection.IsCollectionWithSize.hasSize; +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.status; + +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) +@AutoConfigureMockMvc(addFilters = true) +public class ProjectFindAllNotAuthenticated { + @Autowired + private ProjectRepository projectRepository; + + @Autowired + private MockMvc mockMvc; + + @Autowired + private TestRestTemplate restTemplate; + + @Test + void findAllProjects() throws Exception { + this.mockMvc.perform(get("/projects")) + .andExpect(status().isUnauthorized()); + } +} diff --git a/src/test/java/de/szut/lf8_starter/integration/project/ProjectFindAllSuccess.java b/src/test/java/de/szut/lf8_starter/integration/project/ProjectFindAllSuccess.java new file mode 100644 index 0000000..906b63f --- /dev/null +++ b/src/test/java/de/szut/lf8_starter/integration/project/ProjectFindAllSuccess.java @@ -0,0 +1,60 @@ +package de.szut.lf8_starter.integration.project; + +import de.szut.lf8_starter.project.ProjectEntity; +import de.szut.lf8_starter.project.ProjectRepository; +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.boot.test.web.client.TestRestTemplate; +import org.springframework.test.web.servlet.MockMvc; + +import java.time.LocalDate; +import java.util.List; + +import static org.hamcrest.collection.IsCollectionWithSize.hasSize; +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.status; + +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) +@AutoConfigureMockMvc(addFilters = false) +public class ProjectFindAllSuccess { + @Autowired + private ProjectRepository projectRepository; + + @Autowired + private MockMvc mockMvc; + + @Autowired + private TestRestTemplate restTemplate; + + @Test + void findAllProjects() throws Exception { + var 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); + + this.mockMvc.perform(get("/projects")) + .andExpect(status().isOk()) + .andExpect(jsonPath("$", hasSize(1))) + .andExpect(jsonPath("$[0].id").value(1)) + .andExpect(jsonPath("$[0].comment").value("comment")) + .andExpect(jsonPath("$[0].contractor").value(1)) + .andExpect(jsonPath("$[0].contractorName").value("contractorName")) + .andExpect(jsonPath("$[0].endDate").value("01.01.2024")) + .andExpect(jsonPath("$[0].leadingEmployee").value(1)) + .andExpect(jsonPath("$[0].name").value("name")) + .andExpect(jsonPath("$[0].startDate").value("01.01.2021")) + .andExpect(jsonPath("$[0].employees").isArray()) + .andExpect(jsonPath("$[0].employees", hasSize(3))); + } +}