Compare commits
14 commits
fcf1371891
...
bd8f92a9c6
Author | SHA1 | Date | |
---|---|---|---|
|
bd8f92a9c6 | ||
7913300fa3 | |||
280f22aa87 | |||
4cd4788010 | |||
fe39ea30f6 | |||
|
33cd8dae39 | ||
af3b3108e9 | |||
|
4a8eebc650 | ||
c02d95a12d | |||
99fdf967dc | |||
ed5c58ec6f | |||
34f2462984 | |||
a69fdfdda5 | |||
b936136b53 |
8 changed files with 500 additions and 62 deletions
141
api/pmt.yml
141
api/pmt.yml
|
@ -15,15 +15,76 @@ components:
|
||||||
scheme: bearer
|
scheme: bearer
|
||||||
bearerFormat: JWT
|
bearerFormat: JWT
|
||||||
schemas:
|
schemas:
|
||||||
HelloOut:
|
ProjectInfo:
|
||||||
description: "A Test Schema"
|
|
||||||
type: object
|
type: object
|
||||||
properties:
|
properties:
|
||||||
msg:
|
id:
|
||||||
|
type: integer
|
||||||
|
format: int64
|
||||||
|
name:
|
||||||
type: string
|
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:
|
responses:
|
||||||
OK:
|
Unauthorized:
|
||||||
description: "OK"
|
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:
|
content:
|
||||||
text/plain:
|
text/plain:
|
||||||
schema:
|
schema:
|
||||||
|
@ -34,17 +95,77 @@ components:
|
||||||
text/plain:
|
text/plain:
|
||||||
schema:
|
schema:
|
||||||
type: string
|
type: string
|
||||||
|
ServiceUnavailable:
|
||||||
|
description: "Service Unavailable"
|
||||||
|
content:
|
||||||
|
text/plain:
|
||||||
|
schema:
|
||||||
|
type: string
|
||||||
paths:
|
paths:
|
||||||
/hello:
|
/project:
|
||||||
get:
|
get:
|
||||||
operationId: "GetHello"
|
operationId: "getAllProjects"
|
||||||
description: "A Simple Hello World Endpoint"
|
description: "Get a List of all Projects"
|
||||||
responses:
|
responses:
|
||||||
200:
|
200:
|
||||||
description: "A Hello Response"
|
description: "Returns a List of all Projects"
|
||||||
content:
|
content:
|
||||||
application/json:
|
application/json:
|
||||||
schema:
|
schema:
|
||||||
$ref: "#/components/schemas/HelloOut"
|
$ref: "#/components/schemas/GetAllProjectsDTO"
|
||||||
|
401:
|
||||||
|
$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"
|
||||||
|
/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"
|
||||||
|
|
||||||
|
|
|
@ -1,23 +1,35 @@
|
||||||
package de.hmmh.pmt;
|
package de.hmmh.pmt;
|
||||||
|
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
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.ApiClientFactory;
|
||||||
import de.hmmh.pmt.employee.dtos.EmployeeResponseDTO;
|
|
||||||
import de.hmmh.pmt.oas.DefaultApi;
|
import de.hmmh.pmt.oas.DefaultApi;
|
||||||
import de.hmmh.pmt.dtos.HelloOut;
|
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
|
||||||
|
private ProjectRepository projectRepository;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Optional<ObjectMapper> getObjectMapper() {
|
public Optional<ObjectMapper> getObjectMapper() {
|
||||||
|
@ -30,15 +42,44 @@ public class ApiController implements DefaultApi {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ResponseEntity<HelloOut> getHello() {
|
public ResponseEntity<GetAllProjectsDTO> getAllProjects() {
|
||||||
|
GetAllProjectsDTO response = new GetAllProjectsDTO();
|
||||||
|
|
||||||
StringBuilder employees = new StringBuilder();
|
for (Project project : this.projectRepository.findAll()) {
|
||||||
for (EmployeeResponseDTO employeeResponseDTO : apiClientFactory.getEmployeeApi().findAll1()) {
|
ProjectInfo projectInfo = new ProjectInfo();
|
||||||
employees.append(employeeResponseDTO.toString());
|
projectInfo.setId(project.getId());
|
||||||
|
projectInfo.setName(project.getName());
|
||||||
|
response.add(projectInfo);
|
||||||
}
|
}
|
||||||
|
|
||||||
HelloOut hello = new HelloOut();
|
return ResponseEntity.ok(response);
|
||||||
hello.setMsg(employees.toString());
|
}
|
||||||
return ResponseEntity.ok(hello);
|
|
||||||
|
@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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
62
src/main/java/de/hmmh/pmt/db/Project.java
Normal file
62
src/main/java/de/hmmh/pmt/db/Project.java
Normal 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));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
7
src/main/java/de/hmmh/pmt/db/ProjectRepository.java
Normal file
7
src/main/java/de/hmmh/pmt/db/ProjectRepository.java
Normal 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);
|
||||||
|
}
|
32
src/main/java/de/hmmh/pmt/util/Mapper.java
Normal file
32
src/main/java/de/hmmh/pmt/util/Mapper.java
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,7 +1,7 @@
|
||||||
package de.hmmh.pmt;
|
package de.hmmh.pmt;
|
||||||
|
|
||||||
//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 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;
|
||||||
|
@ -11,6 +11,7 @@ import org.springframework.test.context.ActiveProfiles;
|
||||||
import org.springframework.test.web.servlet.MockMvc;
|
import org.springframework.test.web.servlet.MockMvc;
|
||||||
|
|
||||||
import java.time.LocalDateTime;
|
import java.time.LocalDateTime;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
@SpringBootTest
|
@SpringBootTest
|
||||||
@AutoConfigureMockMvc(addFilters = false)
|
@AutoConfigureMockMvc(addFilters = false)
|
||||||
|
@ -21,53 +22,83 @@ public abstract class IntegrationTest {
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
protected MockMvc mvc;
|
protected MockMvc mvc;
|
||||||
//@Autowired
|
@Autowired
|
||||||
//protected ProjectRepository projectRepository;
|
protected ProjectRepository projectRepository;
|
||||||
|
|
||||||
@BeforeEach
|
@BeforeEach
|
||||||
void setUp() {
|
void setUp() {
|
||||||
//projectRepository.deleteAll();
|
projectRepository.deleteAll();
|
||||||
}
|
}
|
||||||
|
|
||||||
@AfterEach
|
@AfterEach
|
||||||
void cleanUp() {
|
void cleanUp() {
|
||||||
//projectRepository.deleteAll();
|
projectRepository.deleteAll();
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void createTestData() {
|
protected List<Project> createTestProjectData() {
|
||||||
/*Project testProject1 = new Project(
|
Project project1 = new Project(
|
||||||
0L,
|
1L,
|
||||||
"testName1",
|
"Build a Dream Space Station",
|
||||||
"testGoaaaaaaaal!",
|
"Launch a self-sustaining space habitat!",
|
||||||
0L,
|
42L,
|
||||||
0L,
|
101L,
|
||||||
LocalDateTime.of(2001, 9, 11, 13, 34),
|
LocalDateTime.of(2024, 3, 1, 10, 0),
|
||||||
LocalDateTime.of(2002, 8, 13, 14, 34),
|
LocalDateTime.of(2028, 6, 30, 18, 0),
|
||||||
LocalDateTime.of(2003, 7, 12, 23, 34)
|
LocalDateTime.of(2029, 12, 15, 16, 30)
|
||||||
);
|
);
|
||||||
Project testProject2 = new Project(
|
Project project2 = new Project(
|
||||||
0L,
|
2L,
|
||||||
"testName2",
|
"Underwater Research Lab",
|
||||||
"testGoaaaaaaaal!",
|
"Discover new marine species!",
|
||||||
0L,
|
73L,
|
||||||
0L,
|
202L,
|
||||||
LocalDateTime.of(2009, 9, 11, 13, 34),
|
LocalDateTime.of(2025, 5, 22, 8, 45),
|
||||||
LocalDateTime.of(2009, 12, 13, 14, 34),
|
LocalDateTime.of(2027, 11, 5, 17, 0),
|
||||||
LocalDateTime.of(2010, 7, 12, 23, 34)
|
LocalDateTime.of(2027, 10, 20, 14, 0)
|
||||||
);
|
);
|
||||||
Project testProject3 = new Project(
|
Project project3 = new Project(
|
||||||
0L,
|
3L,
|
||||||
"testName3",
|
"AI-Powered Smart City",
|
||||||
"testGoaaaaaaaal!",
|
"Create the world's most advanced smart city!",
|
||||||
0L,
|
89L,
|
||||||
0L,
|
303L,
|
||||||
LocalDateTime.of(2010, 9, 11, 13, 34),
|
LocalDateTime.of(2026, 9, 14, 12, 0),
|
||||||
LocalDateTime.of(2012, 8, 13, 14, 34),
|
LocalDateTime.of(2030, 4, 1, 9, 30),
|
||||||
LocalDateTime.of(2013, 7, 12, 23, 34)
|
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);
|
|
||||||
*/
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
105
src/test/java/de/hmmh/pmt/project/CreateTest.java
Normal file
105
src/test/java/de/hmmh/pmt/project/CreateTest.java
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
39
src/test/java/de/hmmh/pmt/project/GetAllTest.java
Normal file
39
src/test/java/de/hmmh/pmt/project/GetAllTest.java
Normal 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.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<Project> allProjects = createTestProjectData();
|
||||||
|
|
||||||
|
mvc
|
||||||
|
.perform(get(baseUri + "/project"))
|
||||||
|
.andExpect(status().isOk())
|
||||||
|
.andExpect(jsonPath("$", hasSize(allProjects.size())))
|
||||||
|
.andExpect(jsonPath("$[*].id").exists())
|
||||||
|
.andExpect(jsonPath("$[*].name").exists())
|
||||||
|
;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in a new issue