From 49a121820dce325abba46e6ddae16b7edebdad97 Mon Sep 17 00:00:00 2001 From: ecs90 Date: Thu, 27 Jun 2024 11:06:24 +0200 Subject: [PATCH] Added addSolutionRequestBodyTooLarge_test changed spring.servlet and server.tomcat to take a max payload = 2MB added the exception handler to this too --- .../exception/GlobalExceptionHandler.java | 6 +++++ .../src/main/resources/application.yml | 6 +++++ .../user/controller/UserControllerTest.java | 27 ++++++++++++++++--- 3 files changed, 36 insertions(+), 3 deletions(-) diff --git a/itachallenge-user/src/main/java/com/itachallenge/user/exception/GlobalExceptionHandler.java b/itachallenge-user/src/main/java/com/itachallenge/user/exception/GlobalExceptionHandler.java index 111e02536..0448dc4b4 100755 --- a/itachallenge-user/src/main/java/com/itachallenge/user/exception/GlobalExceptionHandler.java +++ b/itachallenge-user/src/main/java/com/itachallenge/user/exception/GlobalExceptionHandler.java @@ -9,6 +9,7 @@ import org.springframework.web.bind.MethodArgumentNotValidException; import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.multipart.MaxUploadSizeExceededException; import java.util.stream.Collectors; @@ -44,4 +45,9 @@ public ResponseEntity handleUnmodifiableSolutionException(Unmo return ResponseEntity.status(HttpStatus.CONFLICT).body(new ErrorResponseDto(e.getMessage())); } + @ExceptionHandler(MaxUploadSizeExceededException.class) + public ResponseEntity handleMaxSizeException(MaxUploadSizeExceededException exc) { + return ResponseEntity.status(HttpStatus.PAYLOAD_TOO_LARGE).body("Payload too large!"); + } + } \ No newline at end of file diff --git a/itachallenge-user/src/main/resources/application.yml b/itachallenge-user/src/main/resources/application.yml index a5cf43fcb..088b548b9 100755 --- a/itachallenge-user/src/main/resources/application.yml +++ b/itachallenge-user/src/main/resources/application.yml @@ -11,6 +11,10 @@ spring: mongodb: uri: mongodb://admin_user:yxRG4sYBDjPFzbh5@localhost:27017/users?authSource=admin uuid-representation: standard + servlet: + multipart: + max-file-size: 2MB + max-request-size: 2MB springdoc: swagger-ui: @@ -22,6 +26,8 @@ springdoc: server: port: 8764 + tomcat: + max-http-form-post-size: 2MB management: endpoints: diff --git a/itachallenge-user/src/test/java/com/itachallenge/user/controller/UserControllerTest.java b/itachallenge-user/src/test/java/com/itachallenge/user/controller/UserControllerTest.java index 372c4da49..019060365 100755 --- a/itachallenge-user/src/test/java/com/itachallenge/user/controller/UserControllerTest.java +++ b/itachallenge-user/src/test/java/com/itachallenge/user/controller/UserControllerTest.java @@ -3,16 +3,13 @@ import com.itachallenge.user.document.UserSolutionDocument; import com.itachallenge.user.dtos.*; import com.itachallenge.user.exception.UnmodifiableSolutionException; -import com.itachallenge.user.dtos.*; import com.itachallenge.user.service.IServiceChallengeStatistics; import com.itachallenge.user.service.IUserSolutionService; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; -import org.mockito.Mock; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.test.autoconfigure.web.reactive.AutoConfigureWebTestClient; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.mock.mockito.MockBean; @@ -23,6 +20,7 @@ import org.springframework.test.context.ActiveProfiles; import org.springframework.test.context.junit.jupiter.SpringExtension; import org.springframework.test.web.reactive.server.WebTestClient; +import org.springframework.web.multipart.MaxUploadSizeExceededException; import reactor.core.publisher.Mono; import java.util.Arrays; @@ -312,6 +310,29 @@ void getVersionTest() { .jsonPath("$.version").isEqualTo("1.0-SNAPSHOT"); } + @Test + void addSolutionRequestBodyTooLarge_test() { + String URI_TEST = "/solution"; + + String largeSolutionText = "aLongText"; // 3MB de datos + UserSolutionDto userSolutionDto = new UserSolutionDto(); + userSolutionDto.setUserId("550e8400-e29b-41d4-a716-446655440001"); + userSolutionDto.setChallengeId("550e8400-e29b-41d4-a716-446655440002"); + userSolutionDto.setLanguageId("550e8400-e29b-41d4-a716-446655440003"); + userSolutionDto.setSolutionText(largeSolutionText); + + when(userSolutionService.addSolution(any())) + .thenReturn(Mono.error(new MaxUploadSizeExceededException(2 * 1024 * 1024))); + + webTestClient.put() + .uri(CONTROLLER_URL + URI_TEST) + .contentType(MediaType.APPLICATION_JSON) + .bodyValue(userSolutionDto) + .exchange() + .expectStatus().isEqualTo(HttpStatus.PAYLOAD_TOO_LARGE); + + verify(userSolutionService).addSolution(userSolutionDto); + } }