Skip to content

Commit

Permalink
Additional unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
flawmop committed Aug 20, 2024
1 parent 4f3d0c7 commit 340e303
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ public class FileAsyncUploadControllerE2E {
@Autowired
private TestRestTemplate restTemplate;

@Nested
@DisplayName("Test GET method(s)")
class getMethods {
@Nested
class GetMethods {
@DisplayName("Success")
@Test
void success() {
Expand All @@ -49,9 +49,9 @@ void success() {
}
}

@Nested
@DisplayName("Test POST method(s)")
class postMethods {
@Nested
class PostMethods {
@DisplayName("Fail on expected request param not supplied")
@Test
void failOnBadParamName() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ public class FileAsyncUploadControllerIT {
@MockBean
private InputProcessorService mockInputProcessorService;

@Nested
@DisplayName("Test GET method(s)")
class getMethods {
@Nested
class GetMethods {
@DisplayName("Success")
@Test
void testGet() throws Exception {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package com.insilicosoft.portal.svc.rip.controller;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import java.util.concurrent.ExecutionException;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.mock.web.MockMultipartFile;

import com.insilicosoft.portal.svc.rip.RipIdentifiers;
import com.insilicosoft.portal.svc.rip.exception.FileProcessingException;
import com.insilicosoft.portal.svc.rip.service.InputProcessorService;

@ExtendWith(MockitoExtension.class)
public class FileAsyncUploadControllerTest {

private static final String message = "The POST request must supply the parameter '" + RipIdentifiers.PARAM_NAME_SIMULATION_FILE + "'";

private FileAsyncUploadController controller;

@Captor
private ArgumentCaptor<byte[]> captorBytes;

@Mock
private InputProcessorService mockInputProcessorService;

@BeforeEach
void setUp() {
controller = new FileAsyncUploadController(mockInputProcessorService);
}

@DisplayName("Test GET method(s)")
@Nested
class GetMethods {
@DisplayName("Success")
@Test
void success() {
String message = "Test get message";

when(mockInputProcessorService.get()).thenReturn(message);

ResponseEntity<String> response = controller.get();

assertThat(response.getBody()).isEqualTo(message);
}
}

@DisplayName("Test POST method(s)")
@Nested
class PostMethods {
@DisplayName("Fail on null param (== bad parameter naming)")
@Test
void failOnBadParameterNaming() {
final FileProcessingException e = assertThrows(FileProcessingException.class, () -> {
controller.handleFileUpload(null);
});
assertThat(e.getMessage()).isEqualTo(message);
}

@DisplayName("Success on upload")
@Test
void failOn() throws FileProcessingException, InterruptedException, ExecutionException {
var bytes = "Hello, World!".getBytes();
var fileName = "request.json";
MockMultipartFile file = new MockMultipartFile("file", fileName, MediaType.TEXT_PLAIN_VALUE,
bytes);

doNothing().when(mockInputProcessorService).processAsync(captorBytes.capture());

ResponseEntity<String> response = controller.handleFileUpload(file).get();

assertThat(response.getBody()).isEqualTo(fileName);

verify(mockInputProcessorService).processAsync(captorBytes.capture());
assertThat(captorBytes.getValue()).isEqualTo(bytes);
}
}

}

0 comments on commit 340e303

Please sign in to comment.