Compare commits

...

27 commits

Author SHA1 Message Date
Rajbir Singh
fcf1371891
PMT-16: Integration Test for creating project added
All checks were successful
Quality Check / Validate OAS (push) Successful in 53s
Quality Check / Validate OAS (pull_request) Successful in 1m3s
Quality Check / Linting (push) Successful in 2m4s
Quality Check / Linting (pull_request) Successful in 2m7s
Quality Check / Testing (push) Successful in 2m19s
Quality Check / Static Analysis (push) Successful in 2m23s
Quality Check / Static Analysis (pull_request) Successful in 2m18s
Quality Check / Testing (pull_request) Successful in 2m16s
2024-10-11 21:02:58 +02:00
40df112186
PMT-16: Update Manual Validation Method of Projects to Include the builtin.
Signed-off-by: Dominik Säume <Dominik.Saeume@hmmh.de>
2024-10-11 21:02:58 +02:00
2153f022eb
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-11 21:02:58 +02:00
a8ead5e3cc
PMT-16: Add Validation for Time Range to Project Entity
Signed-off-by: Dominik Säume <Dominik.Saeume@hmmh.de>
2024-10-11 21:02:58 +02:00
5f88ac1df0
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-11 21:02:58 +02:00
Rajbir Singh
e42f92d6f1
PMT-16: Add Method to Check if Project with Name Exists to ProjectRepository 2024-10-11 21:02:58 +02:00
4940da7b40
PMT-16: Add More Errors to Endpoint Specification
Signed-off-by: Dominik Säume <Dominik.Saeume@hmmh.de>
2024-10-11 21:02:53 +02:00
Rajbir Singh
36c64b49a1
PMT-16: Add Endpoint Specification 2024-10-11 20:59:56 +02:00
1b171cb085 Merge pull request 'PMT-15: Projekte löschen' (!11) from story/PMT-15-projekte-loeschen into trunk
All checks were successful
Quality Check / Validate OAS (push) Successful in 33s
Quality Check / Linting (push) Successful in 1m9s
Quality Check / Static Analysis (push) Successful in 1m15s
Quality Check / Testing (push) Successful in 1m13s
Reviewed-on: #11
2024-10-09 13:14:40 +00:00
2cf79e3568
PMT-15: Implement Tests for Responses
All checks were successful
Quality Check / Validate OAS (push) Successful in 48s
Quality Check / Validate OAS (pull_request) Successful in 1m6s
Quality Check / Linting (push) Successful in 2m4s
Quality Check / Linting (pull_request) Successful in 2m7s
Quality Check / Testing (push) Successful in 2m12s
Quality Check / Static Analysis (push) Successful in 2m22s
Quality Check / Testing (pull_request) Successful in 2m12s
Quality Check / Static Analysis (pull_request) Successful in 2m16s
2024-10-09 11:48:07 +02:00
d725addef8
PMT-15: added api documentation 2024-10-09 11:36:56 +02:00
165aee2ece
PMT-15: Added a deleteProject by ID method 2024-10-09 11:36:56 +02:00
e9e58b821e Merge pull request 'Fix Testing Setup' (!10) from bugfix/testing into trunk
All checks were successful
Quality Check / Validate OAS (push) Successful in 32s
Quality Check / Linting (push) Successful in 1m6s
Quality Check / Testing (push) Successful in 1m10s
Quality Check / Static Analysis (push) Successful in 1m14s
Reviewed-on: #10
Reviewed-by: SZUT-Ole <ole.kueck@hmmh.de>
2024-10-09 09:18:14 +00:00
ca7ad3f19d
NOTICKET: Revert Limiting to One Parallel run
All checks were successful
Quality Check / Validate OAS (push) Successful in 51s
Quality Check / Validate OAS (pull_request) Successful in 1m2s
Quality Check / Linting (push) Successful in 1m59s
Quality Check / Testing (push) Successful in 2m9s
Quality Check / Linting (pull_request) Successful in 2m7s
Quality Check / Static Analysis (push) Successful in 2m18s
Quality Check / Testing (pull_request) Successful in 2m8s
Quality Check / Static Analysis (pull_request) Successful in 2m12s
Signed-off-by: Dominik Säume <Dominik.Saeume@hmmh.de>
2024-10-09 11:15:25 +02:00
d49d5233bf
NOTICKET: Update Intelij Run Config
All checks were successful
Quality Check / Validate OAS (push) Successful in 34s
Quality Check / Validate OAS (pull_request) Successful in 1m1s
Quality Check / Linting (push) Successful in 1m45s
Quality Check / Testing (push) Successful in 1m55s
Quality Check / Static Analysis (push) Successful in 2m5s
Quality Check / Linting (pull_request) Successful in 1m56s
Quality Check / Testing (pull_request) Successful in 1m54s
Quality Check / Static Analysis (pull_request) Successful in 1m58s
Signed-off-by: Dominik Säume <Dominik.Saeume@hmmh.de>
2024-10-09 11:12:20 +02:00
59e3bd1d53
NOTICKET: Update TestData Generation
Signed-off-by: Dominik Säume <Dominik.Saeume@hmmh.de>
2024-10-09 11:12:20 +02:00
0e4d58dc50
NOTICKET: Disable Parallel Unittests
All checks were successful
Quality Check / Validate OAS (push) Successful in 32s
Quality Check / Linting (push) Successful in 1m7s
Quality Check / Static Analysis (push) Successful in 1m11s
Quality Check / Testing (push) Successful in 1m10s
Signed-off-by: Dominik Säume <Dominik.Saeume@hmmh.de>
2024-10-09 11:10:59 +02:00
2e424484c3
NOTICKET: Add ObjectMapper and MockAPI to base IntegrationTest
Signed-off-by: Dominik Säume <Dominik.Saeume@hmmh.de>
2024-10-09 11:10:53 +02:00
cdb60f0d86
NOTICKET: Change Java Datetime Library to be follow Spring Context.
Signed-off-by: Dominik Säume <Dominik.Saeume@hmmh.de>
2024-10-09 11:10:46 +02:00
b31685b79d Merge pull request 'PMT-32: Alle Bestehende Projekte mit ihren Informationen abrufen' (!7) from story/PMT-32-alle-bestehende-projekte-mit into trunk
All checks were successful
Quality Check / Validate OAS (push) Successful in 31s
Quality Check / Linting (push) Successful in 1m8s
Quality Check / Testing (push) Successful in 1m9s
Quality Check / Static Analysis (push) Successful in 1m13s
Reviewed-on: #7
Reviewed-by: SZUT-Rajbir <rajbir2@schule.bremen.de>
2024-10-02 08:06:43 +00:00
524bd99fd5 PMT-32: Added Description to Valid Get All Projects Response
All checks were successful
Quality Check / Validate OAS (push) Successful in 44s
Quality Check / Validate OAS (pull_request) Successful in 57s
Quality Check / Linting (push) Successful in 1m43s
Quality Check / Linting (pull_request) Successful in 1m48s
Quality Check / Testing (pull_request) Successful in 1m46s
Quality Check / Static Analysis (push) Successful in 1m59s
Quality Check / Static Analysis (pull_request) Successful in 1m52s
Quality Check / Testing (push) Successful in 34s
2024-10-02 10:02:10 +02:00
a61791f3ef
PMT-32: Implement Tests for Responses
All checks were successful
Quality Check / Validate OAS (push) Successful in 31s
Quality Check / Linting (push) Successful in 1m5s
Quality Check / Testing (push) Successful in 1m7s
Quality Check / Static Analysis (push) Successful in 1m11s
Quality Check / Validate OAS (pull_request) Successful in 32s
Quality Check / Linting (pull_request) Successful in 1m5s
Quality Check / Testing (pull_request) Successful in 1m8s
Quality Check / Static Analysis (pull_request) Successful in 1m10s
2024-10-02 09:02:07 +02:00
5b1a759376
PMT-32: Add Test Data 2024-10-02 09:01:47 +02:00
2dacff69a2
PMT-32: Implement GetAllProjects Endpoint 2024-10-02 08:49:10 +02:00
df130b7d5c
PMT-32: Create Project Model and Repository 2024-10-02 08:48:47 +02:00
c70ca1d172
PMT-32: Define Endpoint Specification 2024-10-02 08:48:15 +02:00
dddc91bf4d Merge pull request 'NOTICKET: Fix Testing setup' (!6) from bugfix/testing into trunk
All checks were successful
Quality Check / Testing (push) Successful in 52s
Quality Check / Linting (push) Successful in 58s
Quality Check / Static Analysis (push) Successful in 1m0s
Quality Check / Validate OAS (push) Successful in 17s
Reviewed-on: #6
Reviewed-by: SZUT-Ole <ole.kueck@hmmh.de>
2024-10-02 06:12:18 +00:00
11 changed files with 564 additions and 88 deletions

View file

@ -1,24 +1,12 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="Test" type="GradleRunConfiguration" factoryName="Gradle">
<ExternalSystemSettings>
<option name="executionName" />
<option name="externalProjectPath" value="$PROJECT_DIR$" />
<option name="externalSystemIdString" value="GRADLE" />
<option name="scriptParameters" value="" />
<option name="taskDescriptions">
<list />
</option>
<option name="taskNames">
<list>
<option value="test" />
</list>
</option>
<option name="vmOptions" />
</ExternalSystemSettings>
<ExternalSystemDebugServerProcess>true</ExternalSystemDebugServerProcess>
<ExternalSystemReattachDebugProcess>true</ExternalSystemReattachDebugProcess>
<DebugAllEnabled>false</DebugAllEnabled>
<RunAsTest>false</RunAsTest>
<method v="2" />
<configuration default="false" name="Test" type="JUnit" factoryName="JUnit">
<module name="Project_Management_Tool.test" />
<option name="PACKAGE_NAME" value="" />
<option name="MAIN_CLASS_NAME" value="" />
<option name="METHOD_NAME" value="" />
<option name="TEST_OBJECT" value="package" />
<method v="2">
<option name="Make" enabled="true" />
</method>
</configuration>
</component>

View file

@ -15,15 +15,76 @@ components:
scheme: bearer
bearerFormat: JWT
schemas:
HelloOut:
description: "A Test Schema"
ProjectInfo:
type: object
properties:
msg:
id:
type: integer
format: int64
name:
type: string
GetAllProjectsDTO:
type: array
items:
$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:
OK:
description: "OK"
Unauthorized:
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:
@ -34,17 +95,79 @@ components:
text/plain:
schema:
type: string
ServiceUnavailable:
description: "Service Unavailable"
content:
text/plain:
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: "Returns a List of all Projects"
content:
application/json:
schema:
$ref: "#/components/schemas/HelloOut"
$ref: "#/components/schemas/GetAllProjectsDTO"
401:
$ref: "#/components/responses/Unauthorized"
500:
$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"
/project/{id}:
delete:
operationId: "deleteProject"
description: "Delete a specific Project"
parameters:
- in: path
name: id
schema:
type: integer
format: int64
required: true
responses:
204:
description: "Deletes a specific Project"
401:
$ref: "#/components/responses/Unauthorized"
404:
description: "Project not found"
content:
text/plain:
schema:
type: string
500:
$ref: "#/components/responses/InternalError"

View file

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

View file

@ -1,23 +1,35 @@
package de.hmmh.pmt;
import com.fasterxml.jackson.databind.ObjectMapper;
import de.hmmh.pmt.db.Project;
import de.hmmh.pmt.db.ProjectRepository;
import de.hmmh.pmt.dtos.CreateProjectDTO;
import de.hmmh.pmt.dtos.CreatedProjectDTO;
import de.hmmh.pmt.dtos.GetAllProjectsDTO;
import de.hmmh.pmt.dtos.ProjectInfo;
import de.hmmh.pmt.employee.ApiClientFactory;
import de.hmmh.pmt.employee.dtos.EmployeeResponseDTO;
import de.hmmh.pmt.oas.DefaultApi;
import de.hmmh.pmt.dtos.HelloOut;
import de.hmmh.pmt.util.Mapper;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.RestClientException;
import java.util.Optional;
@Controller
@RequestMapping("${openapi.projectManagement.base-path:/api/v1}")
public class ApiController implements DefaultApi {
@Autowired
private Mapper mapper;
@Autowired
private ApiClientFactory apiClientFactory;
@Autowired
private ProjectRepository projectRepository;
@Override
public Optional<ObjectMapper> getObjectMapper() {
@ -30,15 +42,54 @@ public class ApiController implements DefaultApi {
}
@Override
public ResponseEntity<HelloOut> getHello() {
StringBuilder employees = new StringBuilder();
for (EmployeeResponseDTO employeeResponseDTO : apiClientFactory.getEmployeeApi().findAll1()) {
employees.append(employeeResponseDTO.toString());
public ResponseEntity<Void> deleteProject(Long id) {
if (!projectRepository.existsById(id)) {
return ResponseEntity.notFound().build();
}
HelloOut hello = new HelloOut();
hello.setMsg(employees.toString());
return ResponseEntity.ok(hello);
projectRepository.deleteById(id);
return ResponseEntity.noContent().build();
}
@Override
public ResponseEntity<GetAllProjectsDTO> getAllProjects() {
GetAllProjectsDTO response = new GetAllProjectsDTO();
for (Project project : this.projectRepository.findAll()) {
ProjectInfo projectInfo = new ProjectInfo();
projectInfo.setId(project.getId());
projectInfo.setName(project.getName());
response.add(projectInfo);
}
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

@ -0,0 +1,62 @@
package de.hmmh.pmt.db;
import jakarta.persistence.*;
import jakarta.validation.ConstraintViolation;
import jakarta.validation.Validation;
import jakarta.validation.Validator;
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;
import java.util.Set;
@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
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

@ -0,0 +1,7 @@
package de.hmmh.pmt.db;
import org.springframework.data.jpa.repository.JpaRepository;
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,16 +1,22 @@
package de.hmmh.pmt;
//import de.hmmh.pmt.db.Project;
//import de.hmmh.pmt.db.ProjectRepository;
import com.fasterxml.jackson.databind.ObjectMapper;
import de.hmmh.pmt.db.Project;
import de.hmmh.pmt.db.ProjectRepository;
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.BeforeEach;
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.mock.mockito.MockBean;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.web.servlet.MockMvc;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;
@SpringBootTest
@AutoConfigureMockMvc(addFilters = false)
@ -21,53 +27,85 @@ public abstract class IntegrationTest {
@Autowired
protected MockMvc mvc;
//@Autowired
//protected ProjectRepository projectRepository;
@Autowired
protected ObjectMapper objectMapper;
@Autowired
protected ProjectRepository projectRepository;
@MockBean
protected EmployeeControllerApi mockEmployeeApi;
@MockBean
protected QualificationControllerApi mockQualificationApi;
@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)
);
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 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)
);
projectRepository.save(testProject1);
projectRepository.save(testProject2);
projectRepository.save(testProject3);
*/
protected Map<String, Project> createTestProjectData() {
Map<String, Project> projects = new HashMap<>();
Project researchLabProject = new Project();
researchLabProject.setName("Underwater Research Lab");
researchLabProject.setGoal( "Discover new marine species!");
researchLabProject.setCustomerId(73L);
researchLabProject.setAdministratorId(202L);
researchLabProject.setStart(LocalDateTime.of(2025, 5, 22, 8, 45));
researchLabProject.setPlannedEnd(LocalDateTime.of(2026, 5, 22, 8, 45));
projects.put("research-lab", researchLabProject);
Project spaceStationProject = new Project();
spaceStationProject.setName("International Space Station Expansion");
spaceStationProject.setGoal("Build new modules for extended research.");
spaceStationProject.setCustomerId(85L);
spaceStationProject.setAdministratorId(150L);
spaceStationProject.setStart(LocalDateTime.of(2024, 10, 15, 10, 0));
spaceStationProject.setPlannedEnd(LocalDateTime.of(2025, 12, 15, 10, 0));
projects.put("space-station", spaceStationProject);
Project aiResearchProject = new Project();
aiResearchProject.setName("AI Human Interaction Study");
aiResearchProject.setGoal("Study AI interactions in healthcare.");
aiResearchProject.setCustomerId(63L);
aiResearchProject.setAdministratorId(180L);
aiResearchProject.setStart(LocalDateTime.of(2023, 11, 3, 9, 30));
aiResearchProject.setPlannedEnd(LocalDateTime.of(2024, 6, 3, 9, 30));
projects.put("ai-research", aiResearchProject);
Project renewableEnergyProject = new Project();
renewableEnergyProject.setName("Renewable Energy Storage System");
renewableEnergyProject.setGoal("Develop new battery tech for solar power.");
renewableEnergyProject.setCustomerId(90L);
renewableEnergyProject.setAdministratorId(175L);
renewableEnergyProject.setStart(LocalDateTime.of(2024, 1, 10, 11, 0));
renewableEnergyProject.setPlannedEnd(LocalDateTime.of(2025, 1, 10, 11, 0));
projects.put("renewable-energy", renewableEnergyProject);
Project climateChangeProject = new Project();
climateChangeProject.setName("Climate Change Impact Analysis");
climateChangeProject.setGoal("Study global warming effects on ecosystems.");
climateChangeProject.setCustomerId(72L);
climateChangeProject.setAdministratorId(190L);
climateChangeProject.setStart(LocalDateTime.of(2025, 3, 12, 8, 0));
climateChangeProject.setPlannedEnd(LocalDateTime.of(2026, 3, 12, 8, 0));
projects.put("climate change", climateChangeProject);
Project medicalResearchProject = new Project();
medicalResearchProject.setName("Cancer Treatment Innovation");
medicalResearchProject.setGoal("Develop new gene therapy techniques.");
medicalResearchProject.setCustomerId(95L);
medicalResearchProject.setAdministratorId(155L);
medicalResearchProject.setStart(LocalDateTime.of(2024, 8, 20, 9, 15));
medicalResearchProject.setPlannedEnd(LocalDateTime.of(2026, 8, 20, 9, 15));
projects.put("medical-research", medicalResearchProject);
projectRepository.saveAllAndFlush(projects.values());
return projects;
}
}

View file

@ -0,0 +1,105 @@
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.HttpStatusCode;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.RequestBuilder;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.web.client.HttpClientErrorException;
import java.time.LocalDateTime;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
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 {
when(this.mockEmployeeApi.findById(Mockito.anyLong()))
.thenReturn(new EmployeeResponseDTO());
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));
RequestBuilder requestBuilder = MockMvcRequestBuilders
.post(baseUri + "/project")
.accept(MediaType.APPLICATION_JSON)
.content(this.objectMapper.writeValueAsString(createDTO))
.contentType(MediaType.APPLICATION_JSON);
this.mvc
.perform(requestBuilder)
.andExpect(status().isCreated())
.andExpect(jsonPath("$.id").exists());
}
@Test
void shouldNotCreateProjectWithSameName() throws Exception {
CreateProjectDTO createDTO = new CreateProjectDTO();
createDTO.setName("Test");
createDTO.setGoal("A Test Goal");
createDTO.setCustomerId(10L);
createDTO.setAdministratorId(1L);
createDTO.setStart(LocalDateTime.of(2000, 1, 13, 12, 51));
createDTO.setPlannedEnd(LocalDateTime.of(2002, 3, 21, 11, 42));
RequestBuilder firstProjectRequestBuilder = createProjectRequestBuilder(createDTO);
this.mvc
.perform(firstProjectRequestBuilder)
.andExpect(status().isCreated())
.andExpect(jsonPath("$.id").exists());
RequestBuilder secondProjectRequestBuilder = createProjectRequestBuilder(createDTO);
this.mvc
.perform(secondProjectRequestBuilder)
.andExpect(status().isConflict());
}
@Test
void shouldNotCreateProjectWhenAdministratorDoesNotExist() throws Exception {
HttpClientErrorException httpClientErrorException = mock(HttpClientErrorException.class);
when(httpClientErrorException.getStatusCode()).thenReturn(HttpStatusCode.valueOf(404));
when(this.mockEmployeeApi.findById(1L))
.thenThrow(httpClientErrorException);
CreateProjectDTO createDTO = new CreateProjectDTO();
createDTO.setName("Test");
createDTO.setGoal("A Test Goal");
createDTO.setCustomerId(10L);
createDTO.setAdministratorId(1L);
createDTO.setStart(LocalDateTime.of(2000, 1, 13, 12, 51));
createDTO.setPlannedEnd(LocalDateTime.of(2002, 3, 21, 11, 42));
RequestBuilder projectRequestBuilder = createProjectRequestBuilder(createDTO);
this.mvc
.perform(projectRequestBuilder)
.andExpect(status().isNotFound());
}
private RequestBuilder createProjectRequestBuilder(CreateProjectDTO createDTO) throws Exception {
return MockMvcRequestBuilders
.post(baseUri + "/project")
.accept(MediaType.APPLICATION_JSON)
.content(this.objectMapper.writeValueAsString(createDTO))
.contentType(MediaType.APPLICATION_JSON);
}
}

View file

@ -0,0 +1,31 @@
package de.hmmh.pmt.project;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import java.util.List;
import java.util.Map;
import org.junit.jupiter.api.Test;
import de.hmmh.pmt.IntegrationTest;
import de.hmmh.pmt.db.Project;
public class DeleteTest extends IntegrationTest {
@Test
void projectNotFound() throws Exception {
mvc
.perform(delete(baseUri + "/project/1"))
.andExpect(status().isNotFound())
;
}
@Test
void deletedSuccessfully() throws Exception {
Map<String,Project> allProjects = createTestProjectData();
mvc
.perform(delete(baseUri + "/project/" + allProjects.get("space-station").getId()))
.andExpect(status().isNoContent())
;
}
}

View file

@ -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.Map;
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 {
Map<String, Project> allProjects = createTestProjectData();
mvc
.perform(get(baseUri + "/project"))
.andExpect(status().isOk())
.andExpect(jsonPath("$", hasSize(allProjects.size())))
.andExpect(jsonPath("$[*].id").exists())
.andExpect(jsonPath("$[*].name").exists())
;
}
}