Compare commits
No commits in common. "v1.5.0" and "v1.4.0" have entirely different histories.
23 changed files with 804 additions and 1065 deletions
|
@ -58,25 +58,20 @@ repositories {
|
|||
mavenCentral()
|
||||
}
|
||||
|
||||
val springDocVersion = "2.6.0"
|
||||
val oauth2Version = "3.3.4"
|
||||
|
||||
dependencies {
|
||||
implementation("org.springframework.boot:spring-boot-starter-data-jpa")
|
||||
implementation("org.springframework.boot:spring-boot-starter-web")
|
||||
implementation("org.springframework.boot:spring-boot-starter-validation")
|
||||
implementation("org.springdoc:springdoc-openapi-starter-webmvc-ui:$springDocVersion")
|
||||
implementation("org.springframework.boot:spring-boot-starter-security")
|
||||
implementation("org.springframework.boot:spring-boot-starter-oauth2-resource-server:$oauth2Version")
|
||||
implementation("org.springframework.boot:spring-boot-starter-oauth2-client:$oauth2Version")
|
||||
|
||||
testImplementation("com.h2database:h2")
|
||||
testImplementation("org.springframework.boot:spring-boot-starter-test")
|
||||
|
||||
compileOnly("org.projectlombok:lombok")
|
||||
annotationProcessor("org.projectlombok:lombok")
|
||||
testImplementation("org.springframework.boot:spring-boot-starter-test")
|
||||
testRuntimeOnly("org.junit.platform:junit-platform-launcher")
|
||||
implementation("org.springframework.boot:spring-boot-starter-security")
|
||||
implementation("org.springframework.boot:spring-boot-starter-oauth2-resource-server:3.3.4")
|
||||
implementation("org.springframework.boot:spring-boot-starter-oauth2-client:3.3.4")
|
||||
runtimeOnly("org.postgresql:postgresql")
|
||||
implementation("org.springdoc:springdoc-openapi-starter-webmvc-ui:2.6.0")
|
||||
testImplementation("com.h2database:h2")
|
||||
}
|
||||
|
||||
tasks.withType<Test> {
|
||||
|
|
|
@ -1,3 +1,3 @@
|
|||
### GET request to example server
|
||||
GET http://localhost:8080/projects/1
|
||||
GET http://localhost:8080/projects/2
|
||||
Authorization: Bearer {{auth_token}}
|
||||
|
|
|
@ -1,15 +0,0 @@
|
|||
### GET request to example server
|
||||
PUT http://localhost:8080/projects/1
|
||||
Authorization: Bearer {{auth_token}}
|
||||
Content-Type: application/json
|
||||
|
||||
{
|
||||
"name": "newName",
|
||||
"leading_employee": 2,
|
||||
"employees": [],
|
||||
"contractor": 9,
|
||||
"contractor_name": "New Contractor name",
|
||||
"comment": "new goal of project",
|
||||
"start_date": "01.01.2010",
|
||||
"planned_end_date": "01.01.2021"
|
||||
}
|
|
@ -1,6 +1,7 @@
|
|||
package de.szut.lf8_starter.config;
|
||||
|
||||
|
||||
|
||||
import io.swagger.v3.oas.models.Components;
|
||||
import io.swagger.v3.oas.models.OpenAPI;
|
||||
import io.swagger.v3.oas.models.info.Info;
|
||||
|
@ -32,33 +33,15 @@ public class OpenAPIConfiguration {
|
|||
.addServersItem(new Server().url(this.context.getContextPath()))
|
||||
.info(new Info()
|
||||
.title("LF8 project starter")
|
||||
.description("""
|
||||
## Auth
|
||||
.description("\n## Auth\n" +
|
||||
"\n## Authentication\n" + "\nThis Hello service uses JWTs to authenticate requests. You will receive a bearer token by making a POST-Request in IntelliJ on:\n\n" +
|
||||
"\n" +
|
||||
"```\nPOST http://keycloak.szut.dev/auth/realms/szut/protocol/openid-connect/token\nContent-Type: application/x-www-form-urlencoded\ngrant_type=password&client_id=employee-management-service&username=user&password=test\n```\n" +
|
||||
"\n" +
|
||||
"\nor by CURL\n" +
|
||||
"```\ncurl -X POST 'http://keycloak.szut.dev/auth/realms/szut/protocol/openid-connect/token'\n--header 'Content-Type: application/x-www-form-urlencoded'\n--data-urlencode 'grant_type=password'\n--data-urlencode 'client_id=employee-management-service'\n--data-urlencode 'username=user'\n--data-urlencode 'password=test'\n```\n" +
|
||||
"\nTo get a bearer-token in Postman, you have to follow the instructions in \n [Postman-Documentation](https://documenter.getpostman.com/view/7294517/SzmfZHnd).")
|
||||
|
||||
## Authentication
|
||||
|
||||
This Hello service uses JWTs to authenticate requests. You will receive a bearer token by making a POST-Request in IntelliJ on:
|
||||
|
||||
```
|
||||
POST http://keycloak.szut.dev/auth/realms/szut/protocol/openid-connect/token
|
||||
Content-Type: application/x-www-form-urlencoded
|
||||
grant_type=password&client_id=employee-management-service&username=user&password=test
|
||||
```
|
||||
|
||||
or by CURL:
|
||||
|
||||
```
|
||||
curl -X POST 'http://keycloak.szut.dev/auth/realms/szut/protocol/openid-connect/token'
|
||||
--header 'Content-Type: application/x-www-form-urlencoded'
|
||||
--data-urlencode 'grant_type=password'
|
||||
--data-urlencode 'client_id=employee-management-service'
|
||||
--data-urlencode 'username=user'
|
||||
--data-urlencode 'password=test'
|
||||
```
|
||||
|
||||
To get a bearer-token in Postman, you have to follow the instructions in
|
||||
[Postman-Documentation](https://documenter.getpostman.com/view/7294517/SzmfZHnd).
|
||||
""")
|
||||
.version("0.1"))
|
||||
.addSecurityItem(new SecurityRequirement().addList(securitySchemeName))
|
||||
.components(
|
||||
|
|
|
@ -6,7 +6,6 @@ import io.swagger.v3.oas.annotations.responses.ApiResponses;
|
|||
import jakarta.validation.ConstraintViolationException;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.http.converter.HttpMessageNotReadableException;
|
||||
import org.springframework.web.bind.MethodArgumentNotValidException;
|
||||
import org.springframework.web.bind.annotation.ControllerAdvice;
|
||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||
|
@ -22,7 +21,7 @@ import java.util.Date;
|
|||
public class GlobalExceptionHandler {
|
||||
|
||||
@ExceptionHandler(ResourceNotFoundException.class)
|
||||
public ResponseEntity<ErrorDetails> handleHelloEntityNotFoundException(ResourceNotFoundException ex, WebRequest request) {
|
||||
public ResponseEntity<?> handleHelloEntityNotFoundException(ResourceNotFoundException ex, WebRequest request) {
|
||||
ErrorDetails errorDetails = new ErrorDetails(new Date(), ex.getMessage(), request.getDescription(false));
|
||||
return new ResponseEntity<>(errorDetails, HttpStatus.NOT_FOUND);
|
||||
}
|
||||
|
@ -41,13 +40,6 @@ public class GlobalExceptionHandler {
|
|||
return new ResponseEntity<>(errorDetails, HttpStatus.BAD_REQUEST);
|
||||
}
|
||||
|
||||
@ExceptionHandler(HttpMessageNotReadableException.class)
|
||||
public ResponseEntity<ErrorDetails> handleHttpMessageNotReadableException(HttpMessageNotReadableException ex, WebRequest request) {
|
||||
ErrorDetails errorDetails = new ErrorDetails(new Date(), ex.getMessage(), request.getDescription(false));
|
||||
|
||||
return new ResponseEntity<>(errorDetails, HttpStatus.BAD_REQUEST);
|
||||
}
|
||||
|
||||
@ExceptionHandler(ConstraintViolationException.class)
|
||||
public ResponseEntity<ErrorDetails> handleConstraintViolationException(ConstraintViolationException ex, WebRequest request) {
|
||||
String errorMessage = ex.getConstraintViolations().stream().findFirst().get().getMessage();
|
||||
|
|
|
@ -45,6 +45,22 @@ public class HelloController {
|
|||
return this.helloMapper.mapToGetDto(helloEntity);
|
||||
}
|
||||
|
||||
@Operation(summary = "delivers a list of hellos")
|
||||
@ApiResponses(value = {
|
||||
@ApiResponse(responseCode = "200", description = "list of hellos",
|
||||
content = {@Content(mediaType = "application/json",
|
||||
schema = @Schema(implementation = HelloGetDto.class))}),
|
||||
@ApiResponse(responseCode = "401", description = "not authorized",
|
||||
content = @Content)})
|
||||
@GetMapping
|
||||
public List<HelloGetDto> findAll() {
|
||||
return this.service
|
||||
.readAll()
|
||||
.stream()
|
||||
.map(e -> this.helloMapper.mapToGetDto(e))
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
@Operation(summary = "deletes a Hello by id")
|
||||
@ApiResponses(value = {
|
||||
@ApiResponse(responseCode = "204", description = "delete successful"),
|
||||
|
@ -62,4 +78,22 @@ public class HelloController {
|
|||
this.service.delete(entity);
|
||||
}
|
||||
}
|
||||
|
||||
@Operation(summary = "find hellos by message")
|
||||
@ApiResponses(value = {
|
||||
@ApiResponse(responseCode = "200", description = "List of hellos who have the given message",
|
||||
content = {@Content(mediaType = "application/json",
|
||||
schema = @Schema(implementation = HelloGetDto.class))}),
|
||||
@ApiResponse(responseCode = "404", description = "qualification description does not exist",
|
||||
content = @Content),
|
||||
@ApiResponse(responseCode = "401", description = "not authorized",
|
||||
content = @Content)})
|
||||
@GetMapping("/findByMessage")
|
||||
public List<HelloGetDto> findAllEmployeesByQualification(@RequestParam String message) {
|
||||
return this.service
|
||||
.findByMessage(message)
|
||||
.stream()
|
||||
.map(e -> this.helloMapper.mapToGetDto(e))
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,7 +2,6 @@ package de.szut.lf8_starter.project;
|
|||
|
||||
import de.szut.lf8_starter.project.dto.CreateProjectDto;
|
||||
import de.szut.lf8_starter.project.dto.GetProjectDto;
|
||||
import de.szut.lf8_starter.project.dto.UpdateProjectDto;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
|
@ -39,23 +38,4 @@ public class ProjectMapper {
|
|||
|
||||
return getProjectDto;
|
||||
}
|
||||
|
||||
public ProjectEntity mapUpdateDtoToEntity(UpdateProjectDto updateProjectDto, ProjectEntity projectEntity) {
|
||||
projectEntity.setName(updateProjectDto.getName() != null ? updateProjectDto.getName() : projectEntity.getName());
|
||||
projectEntity.setLeadingEmployee(updateProjectDto.getLeadingEmployee() != null ? updateProjectDto.getLeadingEmployee() : projectEntity.getLeadingEmployee());
|
||||
projectEntity.setContractor(updateProjectDto.getContractor() != null ? updateProjectDto.getContractor() : projectEntity.getContractor());
|
||||
projectEntity.setContractorName(updateProjectDto.getContractorName() != null ? updateProjectDto.getContractorName() : projectEntity.getContractorName());
|
||||
projectEntity.setComment(updateProjectDto.getComment() != null ? updateProjectDto.getComment() : projectEntity.getComment());
|
||||
projectEntity.setStartDate(updateProjectDto.getStartDate() != null ? updateProjectDto.getStartDate() : projectEntity.getStartDate());
|
||||
projectEntity.setPlannedEndDate(updateProjectDto.getPlannedEndDate() != null ? updateProjectDto.getPlannedEndDate() : projectEntity.getPlannedEndDate());
|
||||
projectEntity.setEndDate(updateProjectDto.getEndDate() != null ? updateProjectDto.getEndDate() : projectEntity.getEndDate());
|
||||
|
||||
if (updateProjectDto.getEmployees() != null) {
|
||||
projectEntity.getEmployees().clear();
|
||||
|
||||
projectEntity.setEmployees(updateProjectDto.getEmployees());
|
||||
}
|
||||
|
||||
return projectEntity;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
package de.szut.lf8_starter.project;
|
||||
|
||||
import de.szut.lf8_starter.exceptionHandling.ResourceNotFoundException;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.List;
|
||||
|
@ -21,13 +22,13 @@ public class ProjectService {
|
|||
return this.projectRepository.findAll();
|
||||
}
|
||||
|
||||
public Optional<ProjectEntity> findById(Long id) {
|
||||
return projectRepository.findById(id);
|
||||
public ProjectEntity findById(Long id) {
|
||||
Optional<ProjectEntity> articleEntity = projectRepository.findById(id);
|
||||
|
||||
if (articleEntity.isEmpty()) {
|
||||
throw new ResourceNotFoundException("Project with id " + id + " not found");
|
||||
}
|
||||
|
||||
public ProjectEntity update(ProjectEntity project) {
|
||||
this.projectRepository.save(project);
|
||||
|
||||
return project;
|
||||
return articleEntity.get();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -13,8 +13,6 @@ import org.springframework.http.HttpStatus;
|
|||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
@RestController
|
||||
@RequestMapping(value = "projects")
|
||||
public class GetProjectAction {
|
||||
|
@ -35,12 +33,8 @@ public class GetProjectAction {
|
|||
})
|
||||
@GetMapping("/{id}")
|
||||
public ResponseEntity<GetProjectDto> findArticleById(@PathVariable Long id) {
|
||||
Optional<ProjectEntity> project = this.projectService.findById(id);
|
||||
ProjectEntity project = this.projectService.findById(id);
|
||||
|
||||
if (project.isEmpty()) {
|
||||
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
|
||||
}
|
||||
|
||||
return new ResponseEntity<>(this.projectMapper.mapToGetDto(project.get()), HttpStatus.OK);
|
||||
return new ResponseEntity<>(this.projectMapper.mapToGetDto(project), HttpStatus.OK);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,50 +0,0 @@
|
|||
package de.szut.lf8_starter.project.action;
|
||||
|
||||
import de.szut.lf8_starter.project.ProjectEntity;
|
||||
import de.szut.lf8_starter.project.ProjectMapper;
|
||||
import de.szut.lf8_starter.project.ProjectService;
|
||||
import de.szut.lf8_starter.project.dto.GetProjectDto;
|
||||
import de.szut.lf8_starter.project.dto.UpdateProjectDto;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.media.Content;
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import io.swagger.v3.oas.annotations.responses.ApiResponse;
|
||||
import io.swagger.v3.oas.annotations.responses.ApiResponses;
|
||||
import jakarta.validation.Valid;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
@RestController
|
||||
@RequestMapping(value = "/projects")
|
||||
public class UpdateProjectAction {
|
||||
private final ProjectService projectService;
|
||||
private final ProjectMapper projectMapper;
|
||||
|
||||
public UpdateProjectAction(ProjectService projectService, ProjectMapper mappingService) {
|
||||
this.projectService = projectService;
|
||||
this.projectMapper = mappingService;
|
||||
}
|
||||
|
||||
@Operation(summary = "Update a project by ID")
|
||||
@ApiResponses(value = {
|
||||
@ApiResponse(responseCode = "200", description = "Project updated successfully",
|
||||
content = @Content(mediaType = "application/json", schema = @Schema(implementation = GetProjectDto.class))),
|
||||
@ApiResponse(responseCode = "404", description = "Project not found", content = @Content)
|
||||
})
|
||||
@PutMapping("/{id}")
|
||||
public ResponseEntity<GetProjectDto> updateSupplier(@PathVariable Long id, @Valid @RequestBody UpdateProjectDto updateProjectDto) {
|
||||
Optional<ProjectEntity> project = this.projectService.findById(id);
|
||||
|
||||
if (project.isEmpty()) {
|
||||
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
|
||||
}
|
||||
|
||||
ProjectEntity updatedProject = this.projectMapper.mapUpdateDtoToEntity(updateProjectDto, project.get());
|
||||
this.projectService.update(updatedProject);
|
||||
|
||||
return new ResponseEntity<>(this.projectMapper.mapToGetDto(updatedProject), HttpStatus.OK);
|
||||
}
|
||||
}
|
|
@ -1,36 +0,0 @@
|
|||
package de.szut.lf8_starter.project.dto;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonFormat;
|
||||
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
|
||||
import com.fasterxml.jackson.databind.annotation.JsonNaming;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.util.List;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class)
|
||||
public class UpdateProjectDto {
|
||||
private String name;
|
||||
|
||||
private Long leadingEmployee;
|
||||
|
||||
private List<Long> employees;
|
||||
|
||||
private Long contractor;
|
||||
|
||||
private String contractorName;
|
||||
|
||||
private String comment;
|
||||
|
||||
@JsonFormat(pattern = "dd.MM.yyyy")
|
||||
private LocalDate startDate;
|
||||
|
||||
@JsonFormat(pattern = "dd.MM.yyyy")
|
||||
private LocalDate plannedEndDate;
|
||||
|
||||
@JsonFormat(pattern = "dd.MM.yyyy")
|
||||
private LocalDate endDate;
|
||||
}
|
|
@ -25,10 +25,10 @@ public class KeycloakLogoutHandler implements LogoutHandler {
|
|||
|
||||
@Override
|
||||
public void logout(HttpServletRequest request, HttpServletResponse response, Authentication auth) {
|
||||
logout(auth);
|
||||
logout(request, auth);
|
||||
}
|
||||
|
||||
public void logout(Authentication auth) {
|
||||
public void logout(HttpServletRequest request, Authentication auth) {
|
||||
logoutFromKeycloak((OidcUser) auth.getPrincipal());
|
||||
}
|
||||
|
||||
|
|
|
@ -29,10 +29,14 @@ import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
|
|||
@EnableWebSecurity
|
||||
class KeycloakSecurityConfig {
|
||||
|
||||
private static final String GROUPS = "groups";
|
||||
private static final String REALM_ACCESS_CLAIM = "realm_access";
|
||||
private static final String ROLES_CLAIM = "roles";
|
||||
|
||||
KeycloakSecurityConfig() {
|
||||
private final KeycloakLogoutHandler keycloakLogoutHandler;
|
||||
|
||||
KeycloakSecurityConfig(KeycloakLogoutHandler keycloakLogoutHandler) {
|
||||
this.keycloakLogoutHandler = keycloakLogoutHandler;
|
||||
}
|
||||
|
||||
@Bean
|
||||
|
@ -79,9 +83,9 @@ class KeycloakSecurityConfig {
|
|||
jwtAuthenticationConverter.setJwtGrantedAuthoritiesConverter(jwt -> {
|
||||
List<GrantedAuthority> grantedAuthorities = new ArrayList<>();
|
||||
|
||||
Map<String, Object> realmAccess = jwt.getClaim(REALM_ACCESS_CLAIM);
|
||||
if (realmAccess != null && realmAccess.containsKey(ROLES_CLAIM)) {
|
||||
List<String> roles = (List<String>) realmAccess.get(ROLES_CLAIM);
|
||||
Map<String, Object> realmAccess = jwt.getClaim("realm_access");
|
||||
if (realmAccess != null && realmAccess.containsKey("roles")) {
|
||||
List<String> roles = (List<String>) realmAccess.get("roles");
|
||||
for (String role : roles) {
|
||||
grantedAuthorities.add(new SimpleGrantedAuthority("ROLE_" + role));
|
||||
}
|
||||
|
|
|
@ -1,12 +1,14 @@
|
|||
package de.szut.lf8_starter.welcome;
|
||||
|
||||
|
||||
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.security.access.prepost.PreAuthorize;
|
||||
import org.springframework.security.core.Authentication;
|
||||
import org.springframework.security.core.GrantedAuthority;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.security.Principal;
|
||||
|
||||
@RestController
|
||||
public class WelcomeController {
|
||||
|
@ -17,8 +19,8 @@ public class WelcomeController {
|
|||
}
|
||||
|
||||
@GetMapping("/roles")
|
||||
public ResponseEntity<Collection<GrantedAuthority>> getRoles(Authentication authentication) {
|
||||
return ResponseEntity.ok((Collection<GrantedAuthority>) authentication.getAuthorities());
|
||||
public ResponseEntity<?> getRoles(Authentication authentication) {
|
||||
return ResponseEntity.ok(authentication.getAuthorities());
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -18,14 +18,14 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
|
|||
|
||||
@SpringBootTest
|
||||
@AutoConfigureMockMvc(addFilters = false)
|
||||
class GetProjectActionTest {
|
||||
class FindProjectActionTest {
|
||||
@Autowired
|
||||
private MockMvc mockMvc;
|
||||
@Autowired
|
||||
private ProjectRepository projectRepository;
|
||||
|
||||
@Test
|
||||
void getProjectTest() throws Exception {
|
||||
void createProjectTest() throws Exception {
|
||||
var project = new ProjectEntity();
|
||||
project.setId(1);
|
||||
project.setComment("comment");
|
||||
|
@ -51,9 +51,4 @@ class GetProjectActionTest {
|
|||
.andExpect(jsonPath("employees").isArray())
|
||||
.andExpect(jsonPath("employees", hasSize(3)));
|
||||
}
|
||||
|
||||
@Test
|
||||
void getProjectShouldReturnNotFoundResponseWhenProjectIsNotFound() throws Exception {
|
||||
this.mockMvc.perform(get("/projects/2")).andExpect(status().isNotFound());
|
||||
}
|
||||
}
|
|
@ -1,140 +0,0 @@
|
|||
package de.szut.lf8_starter.integration.project;
|
||||
|
||||
import de.szut.lf8_starter.project.ProjectEntity;
|
||||
import de.szut.lf8_starter.project.ProjectRepository;
|
||||
import org.json.JSONObject;
|
||||
import org.junit.jupiter.api.Test;
|
||||
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.http.MediaType;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.hamcrest.Matchers.is;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
|
||||
@SpringBootTest
|
||||
@AutoConfigureMockMvc(addFilters = false)
|
||||
class UpdateProjectActionTest {
|
||||
@Autowired
|
||||
private MockMvc mockMvc;
|
||||
@Autowired
|
||||
private ProjectRepository projectRepository;
|
||||
|
||||
@Test
|
||||
void updateProjectShouldUpdateProject() throws Exception {
|
||||
ProjectEntity project = new ProjectEntity();
|
||||
project.setId(1);
|
||||
project.setComment("comment");
|
||||
project.setContractor(1);
|
||||
project.setContractorName("contractorName");
|
||||
project.setEndDate(LocalDate.of(2024, 1, 1));
|
||||
project.setLeadingEmployee(1);
|
||||
project.setName("name");
|
||||
project.setStartDate(LocalDate.of(2021, 1, 1));
|
||||
project.setEmployees(List.of(1L, 2L, 3L));
|
||||
this.projectRepository.save(project);
|
||||
|
||||
String content = """
|
||||
{
|
||||
"name": "updatedName",
|
||||
"leading_employee": 2,
|
||||
"employees": [3, 4, 5],
|
||||
"contractor": 6,
|
||||
"contractor_name": "Updated Contractor name",
|
||||
"comment": "new goal of project",
|
||||
"start_date": "01.01.2021",
|
||||
"planned_end_date": "01.01.2022"
|
||||
}
|
||||
""";
|
||||
|
||||
final var contentAsString = this.mockMvc.perform(
|
||||
put("/projects/1").content(content).contentType(MediaType.APPLICATION_JSON)
|
||||
)
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(jsonPath("name", is("updatedName")))
|
||||
.andExpect(jsonPath("leading_employee", is(2)))
|
||||
.andExpect(jsonPath("employees", is(Arrays.asList(3, 4, 5))))
|
||||
.andExpect(jsonPath("contractor", is(6)))
|
||||
.andExpect(jsonPath("contractor_name", is("Updated Contractor name")))
|
||||
.andExpect(jsonPath("comment", is("new goal of project")))
|
||||
.andExpect(jsonPath("start_date", is("01.01.2021")))
|
||||
.andExpect(jsonPath("planned_end_date", is("01.01.2022")))
|
||||
.andReturn()
|
||||
.getResponse()
|
||||
.getContentAsString();
|
||||
|
||||
final var id = Long.parseLong(new JSONObject(contentAsString).get("id").toString());
|
||||
|
||||
final var existingProject = this.projectRepository.findById(id);
|
||||
assertThat(existingProject.get().getName()).isEqualTo("updatedName");
|
||||
assertThat(existingProject.get().getLeadingEmployee()).isEqualTo(2);
|
||||
assertThat(existingProject.get().getContractor()).isEqualTo(6);
|
||||
assertThat(existingProject.get().getContractorName()).isEqualTo("Updated Contractor name");
|
||||
assertThat(existingProject.get().getComment()).isEqualTo("new goal of project");
|
||||
assertThat(existingProject.get().getStartDate()).isEqualTo(LocalDate.of(2021, 1, 1));
|
||||
assertThat(existingProject.get().getPlannedEndDate()).isEqualTo(LocalDate.of(2022, 1, 1));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
void updateProjectShouldUpdateProjectPartially() throws Exception {
|
||||
ProjectEntity project = new ProjectEntity();
|
||||
project.setId(1);
|
||||
project.setName("name");
|
||||
project.setLeadingEmployee(1);
|
||||
project.setContractor(1);
|
||||
project.setComment("comment");
|
||||
project.setEmployees(List.of(1L, 2L, 3L));
|
||||
project.setContractorName("contractorName");
|
||||
project.setStartDate(LocalDate.of(2021, 1, 1));
|
||||
project.setPlannedEndDate(LocalDate.of(2023, 1, 1));
|
||||
project.setEndDate(LocalDate.of(2024, 1, 1));
|
||||
this.projectRepository.save(project);
|
||||
|
||||
String content = """
|
||||
{}
|
||||
""";
|
||||
|
||||
final var contentAsString = this.mockMvc.perform(
|
||||
put("/projects/1").content(content).contentType(MediaType.APPLICATION_JSON)
|
||||
)
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(jsonPath("name", is("name")))
|
||||
.andExpect(jsonPath("leading_employee", is(1)))
|
||||
.andExpect(jsonPath("employees", is(List.of(1,2,3))))
|
||||
.andExpect(jsonPath("contractor", is(1)))
|
||||
.andExpect(jsonPath("contractor_name", is("contractorName")))
|
||||
.andExpect(jsonPath("comment", is("comment")))
|
||||
.andExpect(jsonPath("start_date", is("01.01.2021")))
|
||||
.andExpect(jsonPath("planned_end_date", is("01.01.2023")))
|
||||
.andExpect(jsonPath("end_date", is("01.01.2024")))
|
||||
.andReturn()
|
||||
.getResponse()
|
||||
.getContentAsString();
|
||||
|
||||
final var id = Long.parseLong(new JSONObject(contentAsString).get("id").toString());
|
||||
|
||||
final var existingProject = this.projectRepository.findById(id);
|
||||
assertThat(existingProject.get().getName()).isEqualTo("name");
|
||||
assertThat(existingProject.get().getLeadingEmployee()).isEqualTo(1);
|
||||
assertThat(existingProject.get().getContractor()).isEqualTo(1);
|
||||
assertThat(existingProject.get().getContractorName()).isEqualTo("contractorName");
|
||||
assertThat(existingProject.get().getComment()).isEqualTo("comment");
|
||||
assertThat(existingProject.get().getStartDate()).isEqualTo(LocalDate.of(2021, 1, 1));
|
||||
assertThat(existingProject.get().getPlannedEndDate()).isEqualTo(LocalDate.of(2023, 1, 1));
|
||||
assertThat(existingProject.get().getEndDate()).isEqualTo(LocalDate.of(2024, 1, 1));
|
||||
}
|
||||
|
||||
@Test
|
||||
void updateProjectShouldReturnNotFoundResponseWhenProjectIsNotFound() throws Exception {
|
||||
this.mockMvc.perform(put("/projects/2").content("{}").contentType(MediaType.APPLICATION_JSON)).andExpect(status().isNotFound());
|
||||
}
|
||||
}
|
Reference in a new issue