diff --git a/src/main/java/de/szut/lf8_starter/project/ProjectController.java b/src/main/java/de/szut/lf8_starter/project/ProjectController.java index e39c274..fe0991b 100644 --- a/src/main/java/de/szut/lf8_starter/project/ProjectController.java +++ b/src/main/java/de/szut/lf8_starter/project/ProjectController.java @@ -1,5 +1,6 @@ package de.szut.lf8_starter.project; +import de.szut.lf8_starter.project.dto.GetProjectDto; import de.szut.lf8_starter.project.dto.ProjectGetDto; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.media.Content; @@ -28,7 +29,7 @@ public class ProjectController { @ApiResponse(responseCode = "401", description = "not authorized", content = @Content)}) @GetMapping - public List findAll() { + public List findAll() { return this.service .readAll() .stream() diff --git a/src/main/java/de/szut/lf8_starter/project/ProjectService.java b/src/main/java/de/szut/lf8_starter/project/ProjectService.java index a12228f..4dc8688 100644 --- a/src/main/java/de/szut/lf8_starter/project/ProjectService.java +++ b/src/main/java/de/szut/lf8_starter/project/ProjectService.java @@ -2,6 +2,8 @@ package de.szut.lf8_starter.project; import org.springframework.stereotype.Service; +import java.util.List; + @Service public class ProjectService { private final ProjectRepository projectRepository; @@ -13,4 +15,8 @@ public class ProjectService { public ProjectEntity create(ProjectEntity projectEntity) { return this.projectRepository.save(projectEntity); } + + public List readAll() { + return this.projectRepository.findAll(); + } } diff --git a/src/main/java/de/szut/lf8_starter/project/utils/AuthenticationHelper.java b/src/main/java/de/szut/lf8_starter/project/utils/AuthenticationHelper.java deleted file mode 100644 index 2a9d397..0000000 --- a/src/main/java/de/szut/lf8_starter/project/utils/AuthenticationHelper.java +++ /dev/null @@ -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 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> request = new HttpEntity<>(authParams, headers); - ResponseEntity 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)); - } -} \ No newline at end of file diff --git a/src/test/java/de/szut/lf8_starter/integration/project/ProjectFindAll.java b/src/test/java/de/szut/lf8_starter/integration/project/ProjectFindAll.java index ab87527..0409f36 100644 --- a/src/test/java/de/szut/lf8_starter/integration/project/ProjectFindAll.java +++ b/src/test/java/de/szut/lf8_starter/integration/project/ProjectFindAll.java @@ -2,7 +2,6 @@ package de.szut.lf8_starter.integration.project; import de.szut.lf8_starter.project.ProjectEntity; import de.szut.lf8_starter.project.ProjectRepository; -import de.szut.lf8_starter.project.utils.AuthenticationHelper; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; 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) @AutoConfigureMockMvc -public class ProjectFindAll implements AuthenticationHelper { +public class ProjectFindAll { @Autowired private ProjectRepository projectRepository; @@ -44,7 +43,7 @@ public class ProjectFindAll implements AuthenticationHelper { project.setEmployees(List.of(1L,2L,3L)); this.projectRepository.save(project); - this.mockMvc.perform(get("/projects").header("Authorization", getAuthenticationToken(restTemplate))) + this.mockMvc.perform(get("/projects")) .andExpect(status().isOk()) .andExpect(jsonPath("$", hasSize(1))) .andExpect(jsonPath("$[0].id").value(1))