Skip to content

Commit

Permalink
Added addSolutionRequestBodyTooLarge_test
Browse files Browse the repository at this point in the history
changed spring.servlet and server.tomcat to take a max payload = 2MB
added the exception handler to this too
  • Loading branch information
ecs90 committed Jun 27, 2024
1 parent 5f5101c commit 49a1218
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -44,4 +45,9 @@ public ResponseEntity<ErrorResponseDto> handleUnmodifiableSolutionException(Unmo
return ResponseEntity.status(HttpStatus.CONFLICT).body(new ErrorResponseDto(e.getMessage()));
}

@ExceptionHandler(MaxUploadSizeExceededException.class)
public ResponseEntity<String> handleMaxSizeException(MaxUploadSizeExceededException exc) {
return ResponseEntity.status(HttpStatus.PAYLOAD_TOO_LARGE).body("Payload too large!");
}

}
6 changes: 6 additions & 0 deletions itachallenge-user/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -22,6 +26,8 @@ springdoc:

server:
port: 8764
tomcat:
max-http-form-post-size: 2MB

management:
endpoints:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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);
}
}


Expand Down

0 comments on commit 49a1218

Please sign in to comment.