feat(project): update findAll to use GetProjectDto type
Some checks failed
Quality Check / Tests (pull_request) Successful in 39s
Quality Check / Checkstyle Main (pull_request) Failing after 52s

This commit is contained in:
Jan Gleytenhoover 2024-09-25 13:13:40 +02:00
parent dddeab0224
commit df8624c5ee
Signed by: jank
GPG Key ID: B267751B8AE29EFE
4 changed files with 10 additions and 39 deletions

@ -1,5 +1,6 @@
package de.szut.lf8_starter.project; package de.szut.lf8_starter.project;
import de.szut.lf8_starter.project.dto.GetProjectDto;
import de.szut.lf8_starter.project.dto.ProjectGetDto; import de.szut.lf8_starter.project.dto.ProjectGetDto;
import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content; import io.swagger.v3.oas.annotations.media.Content;
@ -28,7 +29,7 @@ public class ProjectController {
@ApiResponse(responseCode = "401", description = "not authorized", @ApiResponse(responseCode = "401", description = "not authorized",
content = @Content)}) content = @Content)})
@GetMapping @GetMapping
public List<ProjectGetDto> findAll() { public List<GetProjectDto> findAll() {
return this.service return this.service
.readAll() .readAll()
.stream() .stream()

@ -2,6 +2,8 @@ package de.szut.lf8_starter.project;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.util.List;
@Service @Service
public class ProjectService { public class ProjectService {
private final ProjectRepository projectRepository; private final ProjectRepository projectRepository;
@ -13,4 +15,8 @@ public class ProjectService {
public ProjectEntity create(ProjectEntity projectEntity) { public ProjectEntity create(ProjectEntity projectEntity) {
return this.projectRepository.save(projectEntity); return this.projectRepository.save(projectEntity);
} }
public List<ProjectEntity> readAll() {
return this.projectRepository.findAll();
}
} }

@ -1,35 +0,0 @@
package de.szut.lf8_starter.project.utils;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
public interface AuthenticationHelper {
default String getAuthenticationToken(TestRestTemplate restTemplate) {
MultiValueMap<String, String> authParams = new LinkedMultiValueMap<>();
authParams.add("grant_type", "password");
authParams.add("client_id", "employee-management-service");
authParams.add("username", "user");
authParams.add("password", "test");
HttpHeaders headers = new HttpHeaders();
headers.set("Content-Type", "application/x-www-form-urlencoded");
HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<>(authParams, headers);
ResponseEntity<String> authResponse = restTemplate.postForEntity("https://keycloak.szut.dev/auth/realms/szut/protocol/openid-connect/token", request, String.class);
// Extract the token from the response
String responseBody = authResponse.getBody();
assert responseBody != null;
return "Bearer " + extractToken(responseBody);
}
private String extractToken(String responseBody) {
return responseBody.substring(responseBody.indexOf("access_token\":\"") + 15, responseBody.indexOf("\"", responseBody.indexOf("access_token\":\"") + 15));
}
}

@ -2,7 +2,6 @@ package de.szut.lf8_starter.integration.project;
import de.szut.lf8_starter.project.ProjectEntity; import de.szut.lf8_starter.project.ProjectEntity;
import de.szut.lf8_starter.project.ProjectRepository; import de.szut.lf8_starter.project.ProjectRepository;
import de.szut.lf8_starter.project.utils.AuthenticationHelper;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
@ -20,7 +19,7 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@AutoConfigureMockMvc @AutoConfigureMockMvc
public class ProjectFindAll implements AuthenticationHelper { public class ProjectFindAll {
@Autowired @Autowired
private ProjectRepository projectRepository; private ProjectRepository projectRepository;
@ -44,7 +43,7 @@ public class ProjectFindAll implements AuthenticationHelper {
project.setEmployees(List.of(1L,2L,3L)); project.setEmployees(List.of(1L,2L,3L));
this.projectRepository.save(project); this.projectRepository.save(project);
this.mockMvc.perform(get("/projects").header("Authorization", getAuthenticationToken(restTemplate))) this.mockMvc.perform(get("/projects"))
.andExpect(status().isOk()) .andExpect(status().isOk())
.andExpect(jsonPath("$", hasSize(1))) .andExpect(jsonPath("$", hasSize(1)))
.andExpect(jsonPath("$[0].id").value(1)) .andExpect(jsonPath("$[0].id").value(1))