Skip to content

Commit c6cd855

Browse files
committed
fix http upload filename issue. update readme.md. update unit test for DocumentController.
1 parent cb51180 commit c6cd855

File tree

5 files changed

+65
-50
lines changed

5 files changed

+65
-50
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -381,8 +381,8 @@ bcgov:
381381

382382
you can use this [Postman collection](jrcc-access-api/jrcc-document-api.postman_collection.json) to interact with the server.
383383

384-
For body, select binary and click select file
385-
set the http header to `Content-Type:application/octet-stream`
384+
For body, select form-data and input key value as "file" and select file.
385+
set the http header to `Content-Type: multipart/form-data`.
386386

387387
![Postman config](docs\postman.body.png)
388388

docs/postman.body.png

-9.99 KB
Loading

jrcc-access-api/jrcc.swagger.yml

+8-5
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ info:
55
description: JRCC ACCESS API enables document to be submited of HTTP REST API.
66
servers:
77
- url: 'http://localhost/8080'
8-
tags:
8+
tags:
99
- name: document
1010
description: Document API
1111
paths:
@@ -29,10 +29,13 @@ paths:
2929
type: string
3030
requestBody:
3131
content:
32-
application/octet-stream:
32+
multipart/form-data:
3333
schema:
34-
type: string
35-
format: binary
34+
type: object
35+
properties:
36+
fileInfo:
37+
type: string
38+
format: binary
3639
responses:
3740
'200':
3841
description: File successfully received
@@ -114,4 +117,4 @@ components:
114117
code:
115118
type: string
116119
message:
117-
type: string
120+
type: string

jrcc-access-spring-boot-autoconfigure/src/main/java/ca/bc/gov/open/jrccaccess/autoconfigure/plugins/http/DocumentController.java

+16-17
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import ca.bc.gov.open.api.model.Error;
2525
import ca.bc.gov.open.jrccaccess.libs.services.exceptions.DocumentMessageException;
2626
import ca.bc.gov.open.jrccaccess.libs.services.exceptions.ServiceUnavailableException;
27+
import org.springframework.web.multipart.MultipartFile;
2728

2829
/**
2930
* The document controller provides an endpoint to submit a document.
@@ -49,41 +50,39 @@ public DocumentController(DocumentReadyHandler documentReadyHandler) {
4950
this.documentReadyHandler = documentReadyHandler;
5051

5152
}
52-
53-
/**
54-
* POST /document?sender={sender}
55-
*/
56-
@SuppressWarnings({ "unchecked", "rawtypes" })
57-
@Override
58-
public ResponseEntity<DocumentReceivedResponse> postDocument(@NotNull @Valid String sender, UUID xRequestID,
59-
UUID xB3TraceId, UUID xB3ParentSpanId, UUID xB3SpanId, String xB3Sampled, @Valid Resource body) {
60-
53+
54+
public ResponseEntity<DocumentReceivedResponse> postDocument(@NotNull @Valid String sender,
55+
UUID xRequestID,
56+
UUID xB3TraceId,
57+
UUID xB3ParentSpanId,
58+
UUID xB3SpanId,
59+
String xB3Sampled,
60+
@Valid MultipartFile fileInfo) {
61+
6162
DocumentReceivedResponse response = new DocumentReceivedResponse();
6263
response.setAcknowledge(true);
6364

64-
TransactionInfo transactionInfo = new TransactionInfo(body.getFilename(), sender, LocalDateTime.now());
6565
try {
66-
documentReadyHandler.handle(getContent(body.getInputStream()), transactionInfo);
66+
TransactionInfo transactionInfo = new TransactionInfo(fileInfo.getOriginalFilename(), sender, LocalDateTime.now());
67+
documentReadyHandler.handle(getContent(fileInfo.getInputStream()), transactionInfo);
6768
} catch (ServiceUnavailableException e) {
6869
Error error = new Error();
6970
error.setCode(Integer.toString(HttpStatus.SERVICE_UNAVAILABLE.value()));
7071
error.setMessage(e.getMessage());
7172
return new ResponseEntity(error, HttpStatus.SERVICE_UNAVAILABLE);
72-
73+
7374
} catch (IOException | DocumentMessageException e) {
7475
// TODO Auto-generated catch block
75-
7676
Error error = new Error();
7777
error.setCode(Integer.toString(HttpStatus.INTERNAL_SERVER_ERROR.value()));
7878
error.setMessage(HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase());
7979
return new ResponseEntity(error, HttpStatus.INTERNAL_SERVER_ERROR);
80-
8180
}
82-
81+
8382
return ResponseEntity.ok(response);
84-
83+
8584
}
86-
85+
8786
private String getContent(InputStream inputStream) throws IOException {
8887

8988
StringBuilder stringBuilder = new StringBuilder();

jrcc-access-spring-boot-autoconfigure/src/test/java/ca/bc/gov/open/jrccaccess/autoconfigure/plugins/http/DocumentControllerTester.java

+39-26
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33
import static org.junit.Assert.assertEquals;
44
import static org.junit.Assert.assertTrue;
55

6+
import java.io.ByteArrayInputStream;
67
import java.io.IOException;
8+
import java.io.InputStream;
9+
import java.nio.charset.StandardCharsets;
710
import java.time.LocalDate;
811
import java.time.LocalDateTime;
912

@@ -21,13 +24,16 @@
2124
import ca.bc.gov.open.api.model.DocumentReceivedResponse;
2225
import ca.bc.gov.open.jrccaccess.autoconfigure.services.DocumentReadyHandler;
2326
import ca.bc.gov.open.jrccaccess.libs.services.exceptions.ServiceUnavailableException;
27+
import org.springframework.web.multipart.MultipartFile;
2428

2529
public 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

Comments
 (0)