33import static org .junit .Assert .assertEquals ;
44import static org .junit .Assert .assertTrue ;
55
6+ import java .io .ByteArrayInputStream ;
67import java .io .IOException ;
8+ import java .io .InputStream ;
9+ import java .nio .charset .StandardCharsets ;
710import java .time .LocalDate ;
811import java .time .LocalDateTime ;
912
2124import ca .bc .gov .open .api .model .DocumentReceivedResponse ;
2225import ca .bc .gov .open .jrccaccess .autoconfigure .services .DocumentReadyHandler ;
2326import ca .bc .gov .open .jrccaccess .libs .services .exceptions .ServiceUnavailableException ;
27+ import org .springframework .web .multipart .MultipartFile ;
2428
2529public class DocumentControllerTester {
26-
30+
2731 private static final String SERVICE_UNAVAILABLE = "service_unavailable" ;
2832
2933 private static final String VALID = "valid" ;
3034
35+ private static final String FILENAME ="filename.txt" ;
36+
3137 private DocumentController sut ;
3238
3339 @ Mock
@@ -37,59 +43,66 @@ public class DocumentControllerTester {
3743 private TransactionInfo transactionInfoMock ;
3844
3945 @ Mock
40- private Resource resourceWithException ;
46+ private MultipartFile multipartFileWithException ;
4147
4248 @ Before
4349 public void init () throws Exception {
4450
4551 MockitoAnnotations .initMocks (this );
46- Mockito .doNothing ().when (this .documentReadyHandler ).handle ("message" , this .transactionInfoMock );
52+ this .transactionInfoMock .sender =VALID ;
53+ this .transactionInfoMock .fileName =FILENAME ;
54+ this .transactionInfoMock .receivedOn = LocalDateTime .now ();
55+ Mockito .doNothing ().when (this .documentReadyHandler ).handle (Mockito .anyString (), Mockito .eq (this .transactionInfoMock ));
4756 Mockito .doThrow (new ServiceUnavailableException (SERVICE_UNAVAILABLE )).when (this .documentReadyHandler ).handle (Mockito .eq (SERVICE_UNAVAILABLE ), Mockito .any ());
48- Mockito .doReturn ("filename.txt" ).when (this .resourceWithException ).getFilename ();
49- Mockito .when (this .resourceWithException .getInputStream ()).thenThrow (IOException .class );
57+
58+ Mockito .doReturn (FILENAME ).when (this .multipartFileWithException ).getOriginalFilename ();
59+ Mockito .when (this .multipartFileWithException .getInputStream ()).thenThrow (IOException .class );
5060 sut = new DocumentController (this .documentReadyHandler );
5161 }
5262
5363 @ Test
5464 public void post_with_valid_input_should_return_valid_response () {
55- ByteArrayResource bytes = new ByteArrayResource ("awesome content" .getBytes ()){
56- @ Override
57- public String getFilename (){
58- return "documentController.txt" ;
59- }
60- };
61- ResponseEntity <DocumentReceivedResponse > response = sut .postDocument (VALID , null , null , null , null , null , bytes );
62-
65+ MultipartFile multipartFile = Mockito .mock (MultipartFile .class );
66+ Mockito .doReturn (FILENAME ).when (multipartFile ).getOriginalFilename ();
67+ InputStream stream = new ByteArrayInputStream ("awesome_content" .getBytes (StandardCharsets .UTF_8 ));
68+ try {
69+ Mockito .doReturn (stream ).when (multipartFile ).getInputStream ();
70+ } catch (IOException e ) {
71+ e .printStackTrace ();
72+ }
73+ ResponseEntity <DocumentReceivedResponse > response = sut .postDocument (VALID , null , null , null , null , null , multipartFile );
6374 assertEquals (HttpStatus .OK , response .getStatusCode ());
6475 assertTrue (response .getBody ().getAcknowledge ());
6576 }
6677
6778 @ Test
6879 public void post_with_sevice_unavailable_input_should_return_503_response () {
69- ByteArrayResource bytes = new ByteArrayResource (SERVICE_UNAVAILABLE .getBytes ()){
70- @ Override
71- public String getFilename (){
72- return "documentController.txt" ;
73- }
74- };
7580 @ SuppressWarnings ("rawtypes" )
76- ResponseEntity response = sut .postDocument (SERVICE_UNAVAILABLE , null , null , null , null , null , bytes );
81+ MultipartFile multipartFile = Mockito .mock (MultipartFile .class );
82+ Mockito .doReturn (FILENAME ).when (multipartFile ).getOriginalFilename ();
83+ InputStream stream = new ByteArrayInputStream (SERVICE_UNAVAILABLE .getBytes (StandardCharsets .UTF_8 ));
84+ try {
85+ Mockito .doReturn (stream ).when (multipartFile ).getInputStream ();
86+ } catch (IOException e ) {
87+ e .printStackTrace ();
88+ }
89+ ResponseEntity response = sut .postDocument (SERVICE_UNAVAILABLE , null , null , null , null , null , multipartFile );
7790
7891 assertEquals (HttpStatus .SERVICE_UNAVAILABLE , response .getStatusCode ());
7992 assertEquals (SERVICE_UNAVAILABLE , ((ca .bc .gov .open .api .model .Error )response .getBody ()).getMessage ());
8093 assertEquals (Integer .toString (HttpStatus .SERVICE_UNAVAILABLE .value ()), ((ca .bc .gov .open .api .model .Error )response .getBody ()).getCode ());
81-
8294 }
83-
95+
8496 @ Test
8597 public void post_with_io_exception_input_should_return_500_response () {
8698 @ SuppressWarnings ("rawtypes" )
87- ResponseEntity response = sut .postDocument (VALID , null , null , null , null , null , this .resourceWithException );
88-
99+ ResponseEntity response = sut .postDocument (VALID , null , null , null , null , null , this .multipartFileWithException );
100+
89101 assertEquals (HttpStatus .INTERNAL_SERVER_ERROR , response .getStatusCode ());
90102 assertEquals (HttpStatus .INTERNAL_SERVER_ERROR .getReasonPhrase (), ((ca .bc .gov .open .api .model .Error )response .getBody ()).getMessage ());
91103 assertEquals (Integer .toString (HttpStatus .INTERNAL_SERVER_ERROR .value ()), ((ca .bc .gov .open .api .model .Error )response .getBody ()).getCode ());
92-
93104 }
94-
105+
106+ //@Test
107+ //public void post_with
95108}
0 commit comments