3
3
import static org .junit .Assert .assertEquals ;
4
4
import static org .junit .Assert .assertTrue ;
5
5
6
+ import java .io .ByteArrayInputStream ;
6
7
import java .io .IOException ;
8
+ import java .io .InputStream ;
9
+ import java .nio .charset .StandardCharsets ;
7
10
import java .time .LocalDate ;
8
11
import java .time .LocalDateTime ;
9
12
21
24
import ca .bc .gov .open .api .model .DocumentReceivedResponse ;
22
25
import ca .bc .gov .open .jrccaccess .autoconfigure .services .DocumentReadyHandler ;
23
26
import ca .bc .gov .open .jrccaccess .libs .services .exceptions .ServiceUnavailableException ;
27
+ import org .springframework .web .multipart .MultipartFile ;
24
28
25
29
public class DocumentControllerTester {
26
-
30
+
27
31
private static final String SERVICE_UNAVAILABLE = "service_unavailable" ;
28
32
29
33
private static final String VALID = "valid" ;
30
34
35
+ private static final String FILENAME ="filename.txt" ;
36
+
31
37
private DocumentController sut ;
32
38
33
39
@ Mock
@@ -37,59 +43,66 @@ public class DocumentControllerTester {
37
43
private TransactionInfo transactionInfoMock ;
38
44
39
45
@ Mock
40
- private Resource resourceWithException ;
46
+ private MultipartFile multipartFileWithException ;
41
47
42
48
@ Before
43
49
public void init () throws Exception {
44
50
45
51
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 ));
47
56
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 );
50
60
sut = new DocumentController (this .documentReadyHandler );
51
61
}
52
62
53
63
@ Test
54
64
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 );
63
74
assertEquals (HttpStatus .OK , response .getStatusCode ());
64
75
assertTrue (response .getBody ().getAcknowledge ());
65
76
}
66
77
67
78
@ Test
68
79
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
- };
75
80
@ 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 );
77
90
78
91
assertEquals (HttpStatus .SERVICE_UNAVAILABLE , response .getStatusCode ());
79
92
assertEquals (SERVICE_UNAVAILABLE , ((ca .bc .gov .open .api .model .Error )response .getBody ()).getMessage ());
80
93
assertEquals (Integer .toString (HttpStatus .SERVICE_UNAVAILABLE .value ()), ((ca .bc .gov .open .api .model .Error )response .getBody ()).getCode ());
81
-
82
94
}
83
-
95
+
84
96
@ Test
85
97
public void post_with_io_exception_input_should_return_500_response () {
86
98
@ 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
+
89
101
assertEquals (HttpStatus .INTERNAL_SERVER_ERROR , response .getStatusCode ());
90
102
assertEquals (HttpStatus .INTERNAL_SERVER_ERROR .getReasonPhrase (), ((ca .bc .gov .open .api .model .Error )response .getBody ()).getMessage ());
91
103
assertEquals (Integer .toString (HttpStatus .INTERNAL_SERVER_ERROR .value ()), ((ca .bc .gov .open .api .model .Error )response .getBody ()).getCode ());
92
-
93
104
}
94
-
105
+
106
+ //@Test
107
+ //public void post_with
95
108
}
0 commit comments