Compare commits
10 commits
d9b9e2f815
...
d84647f9e6
Author | SHA1 | Date | |
---|---|---|---|
|
d84647f9e6 | ||
b31685b79d | |||
524bd99fd5 | |||
a61791f3ef | |||
5b1a759376 | |||
2dacff69a2 | |||
df130b7d5c | |||
c70ca1d172 | |||
dddc91bf4d | |||
0e9525cde4 |
10 changed files with 315 additions and 66 deletions
112
api/pmt.yml
112
api/pmt.yml
|
@ -14,37 +14,117 @@ 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"
|
||||
CreateProjectDTO:
|
||||
type: object
|
||||
properties:
|
||||
customerId:
|
||||
type: integer
|
||||
format: int64
|
||||
administratorId:
|
||||
type: integer
|
||||
format: int64
|
||||
title:
|
||||
type: string
|
||||
goal:
|
||||
type: string
|
||||
start:
|
||||
type: string
|
||||
format: date-time
|
||||
plannedEnd:
|
||||
type: string
|
||||
format: date-time
|
||||
CreatedProjectDTO:
|
||||
type: object
|
||||
properties:
|
||||
id:
|
||||
type: integer
|
||||
format: int64
|
||||
customerId:
|
||||
type: integer
|
||||
format: int64
|
||||
administratorId:
|
||||
type: integer
|
||||
format: int64
|
||||
title:
|
||||
type: string
|
||||
goal:
|
||||
type: string
|
||||
start:
|
||||
type: string
|
||||
format: date-time
|
||||
plannedEnd:
|
||||
type: string
|
||||
format: date-time
|
||||
responses:
|
||||
OK:
|
||||
description: "OK"
|
||||
content:
|
||||
text/plain:
|
||||
schema:
|
||||
type: string
|
||||
UnAuthorized:
|
||||
description: "Un Authorized"
|
||||
InternalError:
|
||||
description: "Internal Server Error"
|
||||
content:
|
||||
text/plain:
|
||||
schema:
|
||||
type: string
|
||||
NotFound:
|
||||
description: "Not Found"
|
||||
content:
|
||||
text/plain:
|
||||
schema:
|
||||
type: string
|
||||
Conflict:
|
||||
description: "Conflict"
|
||||
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/NotFound"
|
||||
500:
|
||||
$ref: "#/components/responses/InternalError"
|
|
@ -65,6 +65,7 @@ dependencies {
|
|||
testImplementation("org.springframework.security:spring-security-test")
|
||||
testImplementation("org.testcontainers:junit-jupiter")
|
||||
testImplementation("org.testcontainers:postgresql")
|
||||
testRuntimeOnly("com.h2database:h2")
|
||||
testRuntimeOnly("org.junit.platform:junit-platform-launcher")
|
||||
|
||||
//OAS
|
||||
|
@ -95,7 +96,6 @@ swaggerSources {
|
|||
}
|
||||
|
||||
tasks {
|
||||
withType()
|
||||
withType<Checkstyle> {
|
||||
reports {
|
||||
xml.required.set(true)
|
||||
|
|
|
@ -1,15 +1,21 @@
|
|||
package de.hmmh.pmt;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import de.hmmh.pmt.dtos.CreateProjectDTO;
|
||||
import de.hmmh.pmt.dtos.CreatedProjectDTO;
|
||||
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.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.client.RestClientException;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
|
@ -18,6 +24,8 @@ import java.util.Optional;
|
|||
public class ApiController implements DefaultApi {
|
||||
@Autowired
|
||||
private ApiClientFactory apiClientFactory;
|
||||
@Autowired
|
||||
private ProjectRepository projectRepository;
|
||||
|
||||
@Override
|
||||
public Optional<ObjectMapper> getObjectMapper() {
|
||||
|
@ -30,15 +38,32 @@ public class ApiController implements DefaultApi {
|
|||
}
|
||||
|
||||
@Override
|
||||
public ResponseEntity<HelloOut> getHello() {
|
||||
public ResponseEntity<GetAllProjectsDTO> 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);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ResponseEntity<CreatedProjectDTO> createProject(CreateProjectDTO body) {
|
||||
if (projectRepository.existsByName(body.getTitle())){
|
||||
return new ResponseEntity<>(HttpStatus.CONFLICT);
|
||||
}
|
||||
try {
|
||||
if (apiClientFactory.getEmployeeApi().findById(body.getAdministratorId()).getId() == body.getAdministratorId()){
|
||||
|
||||
}
|
||||
} catch (RestClientException exception){
|
||||
return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -11,7 +11,6 @@ import org.springframework.security.web.SecurityFilterChain;
|
|||
import org.springframework.security.web.authentication.session.RegisterSessionAuthenticationStrategy;
|
||||
import org.springframework.security.web.authentication.session.SessionAuthenticationStrategy;
|
||||
import org.springframework.security.web.session.HttpSessionEventPublisher;
|
||||
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
|
||||
|
||||
@Configuration
|
||||
@EnableWebSecurity
|
||||
|
|
47
src/main/java/de/hmmh/pmt/db/Project.java
Normal file
47
src/main/java/de/hmmh/pmt/db/Project.java
Normal file
|
@ -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
|
||||
}
|
||||
|
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);
|
||||
}
|
|
@ -1,26 +1,104 @@
|
|||
package de.hmmh.pmt;
|
||||
|
||||
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;
|
||||
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.ActiveProfiles;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
|
||||
@SpringBootTest
|
||||
@AutoConfigureMockMvc
|
||||
@AutoConfigureMockMvc(addFilters = false)
|
||||
@ActiveProfiles("test")
|
||||
@ContextConfiguration(initializers = PostgresContextInitializer.class)
|
||||
public abstract class IntegrationTest {
|
||||
|
||||
@Autowired
|
||||
protected MockMvc mockMvc;
|
||||
protected final static String baseUri = "/api/v1";
|
||||
|
||||
//protected Repository repository;
|
||||
@Autowired
|
||||
protected MockMvc mvc;
|
||||
@Autowired
|
||||
protected ProjectRepository projectRepository;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
//repository.deleteAll();
|
||||
projectRepository.deleteAll();
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
void cleanUp() {
|
||||
projectRepository.deleteAll();
|
||||
}
|
||||
|
||||
protected List<Project> 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 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 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
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,28 +0,0 @@
|
|||
package de.hmmh.pmt;
|
||||
|
||||
import org.springframework.boot.test.util.TestPropertyValues;
|
||||
import org.springframework.context.ApplicationContextInitializer;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
import org.testcontainers.containers.PostgreSQLContainer;
|
||||
import org.testcontainers.utility.DockerImageName;
|
||||
|
||||
public class PostgresContextInitializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
|
||||
|
||||
private static final PostgreSQLContainer<?> postgres = new PostgreSQLContainer<>(
|
||||
DockerImageName.parse("postgres:16")
|
||||
)
|
||||
.withDatabaseName("test_db")
|
||||
.withUsername("test-db-user")
|
||||
.withPassword("test-db-password")
|
||||
.withReuse(true);
|
||||
|
||||
public void initialize(ConfigurableApplicationContext configurableApplicationContext) {
|
||||
postgres.start();
|
||||
|
||||
TestPropertyValues.of(
|
||||
"spring.datasource.url=" + postgres.getJdbcUrl(),
|
||||
"spring.datasource.username=" + postgres.getUsername(),
|
||||
"spring.datasource.password=" + postgres.getPassword()
|
||||
).applyTo(configurableApplicationContext.getEnvironment());
|
||||
}
|
||||
}
|
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())
|
||||
;
|
||||
}
|
||||
|
||||
}
|
|
@ -1,3 +1,5 @@
|
|||
spring.datasource.url=set_by_test_containers
|
||||
spring.datasource.username=set_by_test_containers
|
||||
spring.datasource.password=set_by_test_containers
|
||||
spring.datasource.url=jdbc:h2:mem:test_db
|
||||
spring.datasource.username=test
|
||||
spring.datasource.password=test
|
||||
spring.datasource.driver-class-name=org.h2.Driver
|
||||
spring.jpa.hibernate.ddl-auto=create-drop
|
||||
|
|
Loading…
Reference in a new issue