-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
flawmop
committed
Aug 20, 2024
1 parent
4f3d0c7
commit 340e303
Showing
3 changed files
with
99 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 93 additions & 0 deletions
93
src/test/java/com/insilicosoft/portal/svc/rip/controller/FileAsyncUploadControllerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
|
||
} |