feat(project): Get all projects (SCRUM-15) #10

Merged
jank merged 13 commits from feature/get-all-projects into main 2024-09-25 11:38:31 +00:00
4 changed files with 10 additions and 39 deletions
Showing only changes of commit df8624c5ee - Show all commits

@ -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<ProjectGetDto> findAll() {
public List<GetProjectDto> findAll() {
return this.service
.readAll()
.stream()

@ -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<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.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))