Compare commits

...

12 commits

Author SHA1 Message Date
ec7b3b5e40
testing....... (Comit needs to be edited later)
Some checks failed
Quality Check / Validate OAS (push) Successful in 30s
Quality Check / Linting (push) Successful in 1m6s
Quality Check / Static Analysis (push) Successful in 1m12s
Quality Check / Testing (push) Failing after 34s
Co-authored-by: Rajbir Singh <rajbir.singh@hmmh.de>
Signed-off-by: Dominik Säume <Dominik.Saeume@hmmh.de>
2024-10-09 10:32:23 +02:00
20a22001c2
PMT-16: Update Manual Validation Method of Projects to Include the builtin.
Signed-off-by: Dominik Säume <Dominik.Saeume@hmmh.de>
2024-10-09 10:32:23 +02:00
21df04fbc6
PMT-16: Implement New Endpoint Logic
Co-authored-by: Rajbir Singh <rajbir.singh@hmmh.de>
Signed-off-by: Dominik Säume <Dominik.Saeume@hmmh.de>
2024-10-09 10:32:23 +02:00
ec9b930386
PMT-16: Add Validation for Time Range to Project Entity
Signed-off-by: Dominik Säume <Dominik.Saeume@hmmh.de>
2024-10-09 10:32:23 +02:00
cde8556d9c
PMT-16: Add Mapper for Project Creation/Created DTO to/from Project Entity
Signed-off-by: Dominik Säume <Dominik.Saeume@hmmh.de>
2024-10-09 10:32:23 +02:00
Rajbir Singh
264595ca58
PMT-16: Add Method to Check if Project with Name Exists to ProjectRepository 2024-10-09 10:32:23 +02:00
f866eaade6
PMT-16: Add More Errors to Endpoint Specification
Signed-off-by: Dominik Säume <Dominik.Saeume@hmmh.de>
2024-10-09 10:32:23 +02:00
Rajbir Singh
f774450986
PMT-16: Add Endpoint Specification 2024-10-09 10:32:23 +02:00
5c06b052af Merge pull request 'bugfix/testing' (!8) from bugfix/testing into trunk
Some checks failed
Quality Check / Validate OAS (push) Successful in 29s
Quality Check / Linting (push) Has been cancelled
Quality Check / Static Analysis (push) Has been cancelled
Quality Check / Testing (push) Has been cancelled
Reviewed-on: #8
Reviewed-by: SZUT-Rajbir <rajbir2@schule.bremen.de>
Reviewed-by: SZUT-Ole <ole.kueck@hmmh.de>
2024-10-09 08:26:02 +00:00
137095814f
PMT-16: Disable Parallel Unittests
All checks were successful
Quality Check / Validate OAS (push) Successful in 3m53s
Quality Check / Linting (push) Successful in 4m26s
Quality Check / Static Analysis (push) Successful in 4m32s
Quality Check / Testing (push) Successful in 4m31s
Quality Check / Validate OAS (pull_request) Successful in 30s
Quality Check / Linting (pull_request) Successful in 1m7s
Quality Check / Testing (pull_request) Successful in 1m8s
Quality Check / Static Analysis (pull_request) Successful in 1m11s
Signed-off-by: Dominik Säume <Dominik.Saeume@hmmh.de>
2024-10-09 10:13:59 +02:00
13d5283b67
PMT-16: Add ObjectMapper and MockAPI to base IntegrationTest
Signed-off-by: Dominik Säume <Dominik.Saeume@hmmh.de>
2024-10-09 10:13:56 +02:00
dc018471b2
PMT-16: Change Java Datetime Library to be follow Spring Context.
Signed-off-by: Dominik Säume <Dominik.Saeume@hmmh.de>
2024-10-09 10:13:33 +02:00
9 changed files with 243 additions and 11 deletions

View file

@ -27,15 +27,80 @@ components:
type: array type: array
items: items:
$ref: "#/components/schemas/ProjectInfo" $ref: "#/components/schemas/ProjectInfo"
CreateProjectDTO:
type: object
properties:
name:
type: string
goal:
type: string
customerId:
type: integer
format: int64
administratorId:
type: integer
format: int64
start:
type: string
format: date-time
plannedEnd:
type: string
format: date-time
CreatedProjectDTO:
type: object
properties:
id:
type: integer
format: int64
name:
type: string
goal:
type: string
customerId:
type: integer
format: int64
administratorId:
type: integer
format: int64
start:
type: string
format: date-time
plannedEnd:
type: string
format: date-time
responses: responses:
UnAuthorized: Unauthorized:
description: "Un Authorized" description: "Unauthorized"
NotFound:
description: "Not Found"
content:
text/plain:
schema:
type: string
Conflict:
description: "Conflict"
content:
text/plain:
schema:
type: string
UnprocessableContent:
description: "Unprocessable Content"
content:
text/plain:
schema:
type: string
InternalError: InternalError:
description: "Internal Server Error" description: "Internal Server Error"
content: content:
text/plain: text/plain:
schema: schema:
type: string type: string
ServiceUnavailable:
description: "Service Unavailable"
content:
text/plain:
schema:
type: string
paths: paths:
/project: /project:
get: get:
@ -49,6 +114,33 @@ paths:
schema: schema:
$ref: "#/components/schemas/GetAllProjectsDTO" $ref: "#/components/schemas/GetAllProjectsDTO"
401: 401:
$ref: "#/components/responses/UnAuthorized" $ref: "#/components/responses/Unauthorized"
500: 500:
$ref: "#/components/responses/InternalError" $ref: "#/components/responses/InternalError"
post:
operationId: "CreateProject"
description: "Creates a new Project"
requestBody:
content:
application/json:
schema:
$ref: "#/components/schemas/CreateProjectDTO"
responses:
201:
description: "Project created successfully"
content:
application/json:
schema:
$ref: "#/components/schemas/CreatedProjectDTO"
401:
$ref: "#/components/responses/Unauthorized"
404:
$ref: "#/components/responses/NotFound"
409:
$ref: "#/components/responses/Conflict"
422:
$ref: "#/components/responses/UnprocessableContent"
500:
$ref: "#/components/responses/InternalError"
503:
$ref: "#/components/responses/ServiceUnavailable"

View file

@ -110,6 +110,7 @@ tasks {
} }
withType<Test> { withType<Test> {
useJUnitPlatform() useJUnitPlatform()
maxParallelForks = 1
} }
named("compileJava").configure { named("compileJava").configure {
dependsOn(swaggerSources.getByName("pmt").code) dependsOn(swaggerSources.getByName("pmt").code)

View file

@ -4,7 +4,7 @@
"invokerPackage": "de.hmmh.pmt", "invokerPackage": "de.hmmh.pmt",
"java8": false, "java8": false,
"java11": true, "java11": true,
"dateLibrary": "java11", "dateLibrary": "java8-localdatetime",
"library": "spring-boot3", "library": "spring-boot3",
"defaultInterfaces": false, "defaultInterfaces": false,
"serializableModel": true "serializableModel": true

View file

@ -1,30 +1,36 @@
package de.hmmh.pmt; package de.hmmh.pmt;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import de.hmmh.pmt.employee.ApiClientFactory;
import de.hmmh.pmt.db.Project; import de.hmmh.pmt.db.Project;
import de.hmmh.pmt.db.ProjectRepository; import de.hmmh.pmt.db.ProjectRepository;
import de.hmmh.pmt.oas.DefaultApi; import de.hmmh.pmt.dtos.CreateProjectDTO;
import de.hmmh.pmt.dtos.CreatedProjectDTO;
import de.hmmh.pmt.dtos.GetAllProjectsDTO; import de.hmmh.pmt.dtos.GetAllProjectsDTO;
import de.hmmh.pmt.dtos.ProjectInfo; import de.hmmh.pmt.dtos.ProjectInfo;
import de.hmmh.pmt.employee.ApiClientFactory;
import de.hmmh.pmt.oas.DefaultApi;
import de.hmmh.pmt.util.Mapper;
import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.RestClientException;
import java.util.Optional; import java.util.Optional;
@Controller @Controller
@RequestMapping("${openapi.projectManagement.base-path:/api/v1}") @RequestMapping("${openapi.projectManagement.base-path:/api/v1}")
public class ApiController implements DefaultApi { public class ApiController implements DefaultApi {
@Autowired
private Mapper mapper;
@Autowired @Autowired
private ApiClientFactory apiClientFactory; private ApiClientFactory apiClientFactory;
@Autowired @Autowired
private ProjectRepository projectRepository; private ProjectRepository projectRepository;
// apiClientFactory.getEmployeeApi().findAll1()
@Override @Override
public Optional<ObjectMapper> getObjectMapper() { public Optional<ObjectMapper> getObjectMapper() {
return Optional.empty(); return Optional.empty();
@ -48,4 +54,32 @@ public class ApiController implements DefaultApi {
return ResponseEntity.ok(response); return ResponseEntity.ok(response);
} }
@Override
public ResponseEntity<CreatedProjectDTO> createProject(CreateProjectDTO body) {
if (projectRepository.existsByName(body.getName())) {
return new ResponseEntity<>(HttpStatus.CONFLICT);
}
try {
apiClientFactory.getEmployeeApi().findById(body.getAdministratorId());
} catch (HttpClientErrorException exception) {
return new ResponseEntity<>(
exception.getStatusCode().equals(HttpStatus.NOT_FOUND)
? HttpStatus.NOT_FOUND
: HttpStatus.SERVICE_UNAVAILABLE
);
} catch (RestClientException exception) {
return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
}
Project project = mapper.map(body);
if (!project.isValid()) {
return new ResponseEntity<>(HttpStatus.UNPROCESSABLE_ENTITY);
}
projectRepository.save(project);
CreatedProjectDTO response = mapper.map(project);
return new ResponseEntity<>(response, HttpStatus.CREATED);
}
} }

View file

@ -1,6 +1,9 @@
package de.hmmh.pmt.db; package de.hmmh.pmt.db;
import jakarta.persistence.*; import jakarta.persistence.*;
import jakarta.validation.ConstraintViolation;
import jakarta.validation.Validation;
import jakarta.validation.Validator;
import jakarta.validation.constraints.NotBlank; import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull; import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size; import jakarta.validation.constraints.Size;
@ -10,6 +13,7 @@ import lombok.NoArgsConstructor;
import lombok.Setter; import lombok.Setter;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.util.Set;
@NoArgsConstructor @NoArgsConstructor
@AllArgsConstructor @AllArgsConstructor
@ -43,5 +47,16 @@ public class Project {
private LocalDateTime plannedEnd; private LocalDateTime plannedEnd;
private LocalDateTime realEnd; // Cant be named just "end" because it's and SQL Keyword private LocalDateTime realEnd; // Cant be named just "end" because it's and SQL Keyword
public boolean isValid() {
Validator validator = Validation.buildDefaultValidatorFactory().getValidator();
Set<ConstraintViolation<Project>> violations = validator.validate(this);
return violations.isEmpty() &&
plannedEnd.isAfter(start) &&
(realEnd == null || realEnd.isAfter(start));
}
} }

View file

@ -3,4 +3,5 @@ package de.hmmh.pmt.db;
import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaRepository;
public interface ProjectRepository extends JpaRepository<Project, Long> { public interface ProjectRepository extends JpaRepository<Project, Long> {
boolean existsByName(String name);
} }

View file

@ -0,0 +1,32 @@
package de.hmmh.pmt.util;
import de.hmmh.pmt.db.Project;
import de.hmmh.pmt.dtos.CreateProjectDTO;
import de.hmmh.pmt.dtos.CreatedProjectDTO;
import org.springframework.stereotype.Component;
@Component
public class Mapper {
public Project map(CreateProjectDTO dto) {
Project project = new Project();
project.setName(dto.getName());
project.setGoal(dto.getGoal());
project.setCustomerId(dto.getCustomerId());
project.setAdministratorId(dto.getAdministratorId());
project.setStart(dto.getStart());
project.setPlannedEnd(dto.getPlannedEnd());
return project;
}
public CreatedProjectDTO map(Project project) {
CreatedProjectDTO dto = new CreatedProjectDTO();
dto.setId(project.getId());
dto.setName(project.getName());
dto.setGoal(project.getGoal());
dto.setCustomerId(project.getCustomerId());
dto.setAdministratorId(project.getAdministratorId());
dto.setStart(project.getStart());
dto.setPlannedEnd(project.getPlannedEnd());
return dto;
}
}

View file

@ -1,12 +1,17 @@
package de.hmmh.pmt; package de.hmmh.pmt;
import com.fasterxml.jackson.databind.ObjectMapper;
import de.hmmh.pmt.db.Project; import de.hmmh.pmt.db.Project;
import de.hmmh.pmt.db.ProjectRepository; import de.hmmh.pmt.db.ProjectRepository;
import de.hmmh.pmt.employee.ApiClientFactory;
import de.hmmh.pmt.employee.api.EmployeeControllerApi;
import de.hmmh.pmt.employee.api.QualificationControllerApi;
import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
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.mock.mockito.MockBean;
import org.springframework.test.context.ActiveProfiles; import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.MockMvc;
@ -23,8 +28,15 @@ public abstract class IntegrationTest {
@Autowired @Autowired
protected MockMvc mvc; protected MockMvc mvc;
@Autowired @Autowired
protected ObjectMapper objectMapper;
@Autowired
protected ProjectRepository projectRepository; protected ProjectRepository projectRepository;
@MockBean
protected EmployeeControllerApi mockEmployeeApi;
@MockBean
protected QualificationControllerApi mockQualificationApi;
@BeforeEach @BeforeEach
void setUp() { void setUp() {
projectRepository.deleteAll(); projectRepository.deleteAll();

View file

@ -0,0 +1,45 @@
package de.hmmh.pmt.project;
import de.hmmh.pmt.IntegrationTest;
import de.hmmh.pmt.dtos.CreateProjectDTO;
import de.hmmh.pmt.employee.dtos.EmployeeResponseDTO;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.RequestBuilder;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import java.time.LocalDateTime;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
public class CreateTest extends IntegrationTest {
@Test
void successfullyCreate() throws Exception {
CreateProjectDTO createDTO = new CreateProjectDTO();
createDTO.setName("Test");
createDTO.setGoal("A Test Goal");
createDTO.setCustomerId(10L);
createDTO.setAdministratorId(10L);
createDTO.setStart(LocalDateTime.of(2000, 1, 13, 12, 51));
createDTO.setPlannedEnd(LocalDateTime.of(2002, 3, 21, 11, 42));
String body = objectMapper.writeValueAsString(createDTO);
RequestBuilder requestBuilder = MockMvcRequestBuilders
.post(baseUri + "/project")
.accept(MediaType.APPLICATION_JSON)
.content(body)
.contentType(MediaType.APPLICATION_JSON);
Mockito.when(mockEmployeeApi.findById(Mockito.anyLong())).thenReturn(new EmployeeResponseDTO());
mvc
.perform(requestBuilder)
.andExpect(status().isCreated())
.andExpect(jsonPath("$.id").exists())
;
}
}