From c70ca1d172b3f072dd490a9670c517fd5d401fdf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20S=C3=A4ume?= Date: Wed, 2 Oct 2024 08:48:15 +0200 Subject: [PATCH 1/6] PMT-32: Define Endpoint Specification --- api/pmt.yml | 36 ++++++++++++++++++++---------------- 1 file changed, 20 insertions(+), 16 deletions(-) diff --git a/api/pmt.yml b/api/pmt.yml index 06e99b1..f3739be 100644 --- a/api/pmt.yml +++ b/api/pmt.yml @@ -14,20 +14,22 @@ components: type: http scheme: bearer bearerFormat: JWT - schemas: - HelloOut: - description: "A Test Schema" + schemas: + ProjectInfo: type: object - properties: - msg: + properties: + id: + type: integer + format: int64 + name: type: string + GetAllProjectsDTO: + type: array + items: + $ref: "#/components/schemas/ProjectInfo" responses: - OK: - description: "OK" - content: - text/plain: - schema: - type: string + UnAuthorized: + description: "Un Authorized" InternalError: description: "Internal Server Error" content: @@ -35,16 +37,18 @@ components: schema: type: string paths: - /hello: + /project: get: - operationId: "GetHello" - description: "A Simple Hello World Endpoint" + operationId: "getAllProjects" + description: "Get a List of all Projects" responses: 200: - description: "A Hello Response" + description: "" content: application/json: schema: - $ref: "#/components/schemas/HelloOut" + $ref: "#/components/schemas/GetAllProjectsDTO" + 401: + $ref: "#/components/responses/UnAuthorized" 500: $ref: "#/components/responses/InternalError" \ No newline at end of file From df130b7d5c31ac4aa13083571b2931fbdb5fd894 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20S=C3=A4ume?= Date: Wed, 2 Oct 2024 08:48:47 +0200 Subject: [PATCH 2/6] PMT-32: Create Project Model and Repository --- src/main/java/de/hmmh/pmt/db/Project.java | 47 +++++++++++++++++++ .../de/hmmh/pmt/db/ProjectRepository.java | 6 +++ 2 files changed, 53 insertions(+) create mode 100644 src/main/java/de/hmmh/pmt/db/Project.java create mode 100644 src/main/java/de/hmmh/pmt/db/ProjectRepository.java diff --git a/src/main/java/de/hmmh/pmt/db/Project.java b/src/main/java/de/hmmh/pmt/db/Project.java new file mode 100644 index 0000000..9c9891f --- /dev/null +++ b/src/main/java/de/hmmh/pmt/db/Project.java @@ -0,0 +1,47 @@ +package de.hmmh.pmt.db; + +import jakarta.persistence.*; +import jakarta.validation.constraints.NotBlank; +import jakarta.validation.constraints.NotNull; +import jakarta.validation.constraints.Size; +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; + +import java.time.LocalDateTime; + +@NoArgsConstructor +@AllArgsConstructor +@Getter +@Setter +@Entity +@Table(name = "project") +public class Project { + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + + @NotBlank + @Size(min = 3, max = 64) + private String name; + + @NotBlank + @Size(min = 10) + private String goal; + + @NotNull + private Long customerId; + + @NotNull + private Long administratorId; // Is an Employee + + @NotNull + private LocalDateTime start; + + @NotNull + private LocalDateTime plannedEnd; + + private LocalDateTime realEnd; // Cant be named just "end" because it's and SQL Keyword +} + diff --git a/src/main/java/de/hmmh/pmt/db/ProjectRepository.java b/src/main/java/de/hmmh/pmt/db/ProjectRepository.java new file mode 100644 index 0000000..ef501dc --- /dev/null +++ b/src/main/java/de/hmmh/pmt/db/ProjectRepository.java @@ -0,0 +1,6 @@ +package de.hmmh.pmt.db; + +import org.springframework.data.jpa.repository.JpaRepository; + +public interface ProjectRepository extends JpaRepository { +} From 2dacff69a273a2604ad212f4bcd034658b2c0ab4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20S=C3=A4ume?= Date: Wed, 2 Oct 2024 08:49:10 +0200 Subject: [PATCH 3/6] PMT-32: Implement GetAllProjects Endpoint --- src/main/java/de/hmmh/pmt/ApiController.java | 27 ++++++++++++-------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/src/main/java/de/hmmh/pmt/ApiController.java b/src/main/java/de/hmmh/pmt/ApiController.java index 8b56898..5031841 100644 --- a/src/main/java/de/hmmh/pmt/ApiController.java +++ b/src/main/java/de/hmmh/pmt/ApiController.java @@ -2,9 +2,11 @@ package de.hmmh.pmt; import com.fasterxml.jackson.databind.ObjectMapper; import de.hmmh.pmt.employee.ApiClientFactory; -import de.hmmh.pmt.employee.dtos.EmployeeResponseDTO; +import de.hmmh.pmt.db.Project; +import de.hmmh.pmt.db.ProjectRepository; import de.hmmh.pmt.oas.DefaultApi; -import de.hmmh.pmt.dtos.HelloOut; +import de.hmmh.pmt.dtos.GetAllProjectsDTO; +import de.hmmh.pmt.dtos.ProjectInfo; import jakarta.servlet.http.HttpServletRequest; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.ResponseEntity; @@ -18,6 +20,10 @@ import java.util.Optional; public class ApiController implements DefaultApi { @Autowired private ApiClientFactory apiClientFactory; + @Autowired + private ProjectRepository projectRepository; + + // apiClientFactory.getEmployeeApi().findAll1() @Override public Optional getObjectMapper() { @@ -30,15 +36,16 @@ public class ApiController implements DefaultApi { } @Override - public ResponseEntity getHello() { + public ResponseEntity getAllProjects() { + GetAllProjectsDTO response = new GetAllProjectsDTO(); - StringBuilder employees = new StringBuilder(); - for (EmployeeResponseDTO employeeResponseDTO : apiClientFactory.getEmployeeApi().findAll1()) { - employees.append(employeeResponseDTO.toString()); + for (Project project : this.projectRepository.findAll()){ + ProjectInfo projectInfo = new ProjectInfo(); + projectInfo.setId(project.getId()); + projectInfo.setName(project.getName()); + response.add(projectInfo); } - - HelloOut hello = new HelloOut(); - hello.setMsg(employees.toString()); - return ResponseEntity.ok(hello); + + return ResponseEntity.ok(response); } } From 5b1a759376626ec1fc053552d564e10d164e3e63 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20S=C3=A4ume?= Date: Wed, 2 Oct 2024 09:01:47 +0200 Subject: [PATCH 4/6] PMT-32: Add Test Data --- .../java/de/hmmh/pmt/IntegrationTest.java | 107 +++++++++++------- 1 file changed, 69 insertions(+), 38 deletions(-) diff --git a/src/test/java/de/hmmh/pmt/IntegrationTest.java b/src/test/java/de/hmmh/pmt/IntegrationTest.java index f25bb51..1bc1a0c 100644 --- a/src/test/java/de/hmmh/pmt/IntegrationTest.java +++ b/src/test/java/de/hmmh/pmt/IntegrationTest.java @@ -1,7 +1,7 @@ package de.hmmh.pmt; -//import de.hmmh.pmt.db.Project; -//import de.hmmh.pmt.db.ProjectRepository; +import de.hmmh.pmt.db.Project; +import de.hmmh.pmt.db.ProjectRepository; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.springframework.beans.factory.annotation.Autowired; @@ -11,6 +11,7 @@ import org.springframework.test.context.ActiveProfiles; import org.springframework.test.web.servlet.MockMvc; import java.time.LocalDateTime; +import java.util.List; @SpringBootTest @AutoConfigureMockMvc(addFilters = false) @@ -21,53 +22,83 @@ public abstract class IntegrationTest { @Autowired protected MockMvc mvc; - //@Autowired - //protected ProjectRepository projectRepository; + @Autowired + protected ProjectRepository projectRepository; @BeforeEach void setUp() { - //projectRepository.deleteAll(); + projectRepository.deleteAll(); } @AfterEach void cleanUp() { - //projectRepository.deleteAll(); + projectRepository.deleteAll(); } - protected void createTestData() { - /*Project testProject1 = new Project( - 0L, - "testName1", - "testGoaaaaaaaal!", - 0L, - 0L, - LocalDateTime.of(2001, 9, 11, 13, 34), - LocalDateTime.of(2002, 8, 13, 14, 34), - LocalDateTime.of(2003, 7, 12, 23, 34) + protected List createTestProjectData() { + Project project1 = new Project( + 1L, + "Build a Dream Space Station", + "Launch a self-sustaining space habitat!", + 42L, + 101L, + LocalDateTime.of(2024, 3, 1, 10, 0), + LocalDateTime.of(2028, 6, 30, 18, 0), + LocalDateTime.of(2029, 12, 15, 16, 30) ); - Project testProject2 = new Project( - 0L, - "testName2", - "testGoaaaaaaaal!", - 0L, - 0L, - LocalDateTime.of(2009, 9, 11, 13, 34), - LocalDateTime.of(2009, 12, 13, 14, 34), - LocalDateTime.of(2010, 7, 12, 23, 34) + Project project2 = new Project( + 2L, + "Underwater Research Lab", + "Discover new marine species!", + 73L, + 202L, + LocalDateTime.of(2025, 5, 22, 8, 45), + LocalDateTime.of(2027, 11, 5, 17, 0), + LocalDateTime.of(2027, 10, 20, 14, 0) ); - Project testProject3 = new Project( - 0L, - "testName3", - "testGoaaaaaaaal!", - 0L, - 0L, - LocalDateTime.of(2010, 9, 11, 13, 34), - LocalDateTime.of(2012, 8, 13, 14, 34), - LocalDateTime.of(2013, 7, 12, 23, 34) + Project project3 = new Project( + 3L, + "AI-Powered Smart City", + "Create the world's most advanced smart city!", + 89L, + 303L, + LocalDateTime.of(2026, 9, 14, 12, 0), + LocalDateTime.of(2030, 4, 1, 9, 30), + LocalDateTime.of(2030, 5, 2, 15, 0) + ); + Project project4 = new Project( + 4L, + "Renewable Energy Revolution", + "Replace all fossil fuels with renewables!", + 56L, + 404L, + LocalDateTime.of(2023, 7, 19, 11, 30), + LocalDateTime.of(2029, 12, 31, 20, 0), + LocalDateTime.of(2029, 10, 5, 18, 45) + ); + Project project5 = new Project( + 5L, + "Virtual Reality Theme Park", + "Build a fully immersive VR theme park!", + 99L, + 505L, + LocalDateTime.of(2024, 2, 28, 9, 15), + LocalDateTime.of(2026, 9, 30, 17, 0), + LocalDateTime.of(2026, 8, 15, 13, 45) + ); + + projectRepository.save(project1); + projectRepository.save(project2); + projectRepository.save(project3); + projectRepository.save(project4); + projectRepository.save(project5); + + return List.of( + project1, + project2, + project3, + project4, + project5 ); - projectRepository.save(testProject1); - projectRepository.save(testProject2); - projectRepository.save(testProject3); - */ } } From a61791f3efd264f186c57ae95fb1db8e62723b38 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20S=C3=A4ume?= Date: Wed, 2 Oct 2024 09:02:07 +0200 Subject: [PATCH 5/6] PMT-32: Implement Tests for Responses --- .../java/de/hmmh/pmt/project/GetAllTest.java | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 src/test/java/de/hmmh/pmt/project/GetAllTest.java diff --git a/src/test/java/de/hmmh/pmt/project/GetAllTest.java b/src/test/java/de/hmmh/pmt/project/GetAllTest.java new file mode 100644 index 0000000..ea8fb84 --- /dev/null +++ b/src/test/java/de/hmmh/pmt/project/GetAllTest.java @@ -0,0 +1,39 @@ +package de.hmmh.pmt.project; + +import de.hmmh.pmt.IntegrationTest; + +import de.hmmh.pmt.db.Project; +import org.junit.jupiter.api.Test; + +import java.util.List; + +import static org.hamcrest.Matchers.empty; +import static org.hamcrest.Matchers.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; + +public class GetAllTest extends IntegrationTest { + @Test + void noProjects() throws Exception { + mvc + .perform(get(baseUri + "/project")) + .andExpect(status().isOk()) + .andExpect(jsonPath("$", empty())) + ; + } + + @Test + void multipleProjects() throws Exception { + List allProjects = createTestProjectData(); + + mvc + .perform(get(baseUri + "/project")) + .andExpect(status().isOk()) + .andExpect(jsonPath("$", hasSize(allProjects.size()))) + .andExpect(jsonPath("$[*].id").exists()) + .andExpect(jsonPath("$[*].name").exists()) + ; + } + +} From 524bd99fd5c881318f44db67b9bda3c3af1fcc95 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ole=20K=C3=BCck?= Date: Wed, 2 Oct 2024 10:02:10 +0200 Subject: [PATCH 6/6] PMT-32: Added Description to Valid Get All Projects Response --- api/pmt.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/api/pmt.yml b/api/pmt.yml index f3739be..fbcab1e 100644 --- a/api/pmt.yml +++ b/api/pmt.yml @@ -43,7 +43,7 @@ paths: description: "Get a List of all Projects" responses: 200: - description: "" + description: "Returns a List of all Projects" content: application/json: schema: