Skip to content
Snippets Groups Projects
Commit bf51d0b7 authored by OZGCloud's avatar OZGCloud
Browse files

OZG-4717 Removed TestService

parent d2b9d60c
No related branches found
No related tags found
No related merge requests found
...@@ -3,12 +3,17 @@ package de.ozgcloud.admin.common.errorhandling; ...@@ -3,12 +3,17 @@ package de.ozgcloud.admin.common.errorhandling;
import static org.assertj.core.api.Assertions.*; import static org.assertj.core.api.Assertions.*;
import static org.mockito.ArgumentMatchers.*; import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.*; import static org.mockito.Mockito.*;
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
import java.util.Set;
import java.util.stream.Stream; import java.util.stream.Stream;
import jakarta.validation.ConstraintViolation;
import jakarta.validation.ConstraintViolationException;
import jakarta.validation.Validation;
import jakarta.validation.Validator;
import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.ParameterizedTest;
...@@ -18,7 +23,6 @@ import org.springframework.beans.factory.annotation.Autowired; ...@@ -18,7 +23,6 @@ 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;
import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.security.test.context.support.WithMockUser; import org.springframework.security.test.context.support.WithMockUser;
import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.ResultActions; import org.springframework.test.web.servlet.ResultActions;
...@@ -67,10 +71,6 @@ public class AdminExceptionHandlerITCase { ...@@ -67,10 +71,6 @@ public class AdminExceptionHandlerITCase {
return AdminExceptionHandler.STATUS_BY_EXCEPTION.entrySet().stream().map(kv -> Arguments.of(kv.getKey(), kv.getValue())); return AdminExceptionHandler.STATUS_BY_EXCEPTION.entrySet().stream().map(kv -> Arguments.of(kv.getKey(), kv.getValue()));
} }
@SneakyThrows
private ResultActions performGet() {
return mockMvc.perform(get(RootController.PATH));
}
} }
@Nested @Nested
...@@ -78,11 +78,26 @@ public class AdminExceptionHandlerITCase { ...@@ -78,11 +78,26 @@ public class AdminExceptionHandlerITCase {
@Test @Test
@SneakyThrows @SneakyThrows
void shouldHaveInfoInBody() { void shouldHaveInfoInBody() {
var result = mockMvc when(modelAssembler.toModel(any())).thenAnswer((a) -> {
.perform(post("/api/test-error").content("{ \"string\" : \"\" }").contentType(MediaType.APPLICATION_JSON).with(csrf())); var validationObject = ValidatedClass.builder().build();
throw new ConstraintViolationException(getConstraintViolations(validationObject));
});
var result = performGet();
assertThat(result.andReturn().getResponse().getContentAsString()).contains("validatedClass.string"); assertThat(result.andReturn().getResponse().getContentAsString()).contains("string: Empty field");
} }
private <T> Set<ConstraintViolation<T>> getConstraintViolations(T object) {
Validator validator = Validation.buildDefaultValidatorFactory().getValidator();
return validator.validate(object);
}
}
@SneakyThrows
private ResultActions performGet() {
return mockMvc.perform(get(RootController.PATH));
} }
} }
package de.ozgcloud.admin.common.errorhandling;
import jakarta.validation.Valid;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
@Service
@Validated
class HelperService {
ValidatedClass returnValidObject(@Valid ValidatedClass validatedClass) {
return validatedClass;
}
}
...@@ -26,24 +26,18 @@ import java.util.Map; ...@@ -26,24 +26,18 @@ import java.util.Map;
import jakarta.validation.ConstraintViolationException; import jakarta.validation.ConstraintViolationException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.rest.webmvc.ResourceNotFoundException; import org.springframework.data.rest.webmvc.ResourceNotFoundException;
import org.springframework.security.access.AccessDeniedException; import org.springframework.security.access.AccessDeniedException;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import de.ozgcloud.common.errorhandling.TechnicalException; import de.ozgcloud.common.errorhandling.TechnicalException;
import io.micrometer.common.lang.NonNullApi;
@RestController @RestController
@RequestMapping("/api/test-error") @RequestMapping("/api/test-error")
class TestErrorController { class TestErrorController {
@Autowired
private HelperService service;
@FunctionalInterface @FunctionalInterface
interface ExceptionProducer { interface ExceptionProducer {
...@@ -66,9 +60,4 @@ class TestErrorController { ...@@ -66,9 +60,4 @@ class TestErrorController {
Class.forName(errorClassName)).produceException(); Class.forName(errorClassName)).produceException();
} }
@PostMapping
public ValidatedClass postMethodName(@RequestBody ValidatedClass object) {
return service.returnValidObject(object);
}
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment