diff --git a/discovery/src/main/java/com/ibm/watson/developer_cloud/discovery/v1/Discovery.java b/discovery/src/main/java/com/ibm/watson/developer_cloud/discovery/v1/Discovery.java index 14fad62e703..49dc89a21c8 100644 --- a/discovery/src/main/java/com/ibm/watson/developer_cloud/discovery/v1/Discovery.java +++ b/discovery/src/main/java/com/ibm/watson/developer_cloud/discovery/v1/Discovery.java @@ -356,7 +356,6 @@ public ServiceCall createDocument(CreateDocumentRequest } Validator.notNull(createRequest.getFile(), "Document " + FILE + " cannot be null"); MediaType mediaType = supportedMediaTypes.get(createRequest.getMediaType()); - Validator.notNull(mediaType, String.format("Media Type '%s' not supported", createRequest.getMediaType())); RequestBody file = InputStreamRequestBody.create(mediaType, createRequest.getFile()); MultipartBody.Builder multipartBuilder = new MultipartBody.Builder(); multipartBuilder.setType(MultipartBody.FORM); diff --git a/discovery/src/main/java/com/ibm/watson/developer_cloud/discovery/v1/model/document/CreateDocumentRequest.java b/discovery/src/main/java/com/ibm/watson/developer_cloud/discovery/v1/model/document/CreateDocumentRequest.java index 14501086f30..cb9b6843cae 100644 --- a/discovery/src/main/java/com/ibm/watson/developer_cloud/discovery/v1/model/document/CreateDocumentRequest.java +++ b/discovery/src/main/java/com/ibm/watson/developer_cloud/discovery/v1/model/document/CreateDocumentRequest.java @@ -31,6 +31,7 @@ public class CreateDocumentRequest extends GenericModel { private JsonObject metadata; private InputStream file; private String mediaType; + private String fileName; //TODO add configuration protected CreateDocumentRequest(Builder builder) { @@ -40,6 +41,7 @@ protected CreateDocumentRequest(Builder builder) { this.metadata = builder.metadata; this.file = builder.file; this.mediaType = builder.mediaType; + this.fileName = builder.fileName; } public String getEnvironmentId() { @@ -66,6 +68,10 @@ public String getMediaType() { return mediaType; } + public String getFileName() { + return fileName; + } + public static class Builder { private final String environmentId; private final String collectionId; @@ -73,10 +79,12 @@ public static class Builder { private JsonObject metadata; private InputStream file; private String mediaType; + private String fileName; public Builder(String environmentId, String collectionId) { this.environmentId = environmentId; this.collectionId = collectionId; + this.fileName = this.fileName == null || this.fileName.isEmpty() ? "file_name_not_provided" : this.fileName; } public Builder configurationId(String configurationId) { @@ -89,17 +97,65 @@ public Builder metadata(JsonObject metadata) { return this; } + /** + * @deprecated + * Use instead file(InputStream content, String mediaType) + * + * @param file An input stream of bytes + * @param mediaType The media type + * @return A document builder + */ + @Deprecated public Builder inputStream(InputStream file, String mediaType) { this.file = file; this.mediaType = mediaType; return this; } + public Builder file(InputStream content) { + this.file = content; + return this; + } + + public Builder file(InputStream content, String mediaType) { + this.file = content; + this.mediaType = mediaType; + return this; + } + + /** + * Create a document builder with an input stream, file name, and media type. + * + * @param content An input stream of bytes + * @param fileName The file name + * @param mediaType The media type. If the media type is unknown then use null or empty string. + * @return A document builder + */ + public Builder file(InputStream content, String fileName, String mediaType) { + this.file = content; + this.mediaType = mediaType; + this.fileName = fileName; + return this; + } + public Builder file(File inputFile, String mediaType) { InputStream file; try { file = new FileInputStream(inputFile); this.mediaType = mediaType; + this.fileName = inputFile.getName(); + } catch (FileNotFoundException e) { + file = null; + } + this.file = file; + return this; + } + + public Builder file(File inputFile) { + InputStream file; + try { + file = new FileInputStream(inputFile); + this.fileName = inputFile.getName(); } catch (FileNotFoundException e) { file = null; } diff --git a/discovery/src/test/java/com/ibm/watson/developer_cloud/discovery/v1/DiscoveryServiceTest.java b/discovery/src/test/java/com/ibm/watson/developer_cloud/discovery/v1/DiscoveryServiceTest.java index fe7f3461057..7020cfc6e4b 100644 --- a/discovery/src/test/java/com/ibm/watson/developer_cloud/discovery/v1/DiscoveryServiceTest.java +++ b/discovery/src/test/java/com/ibm/watson/developer_cloud/discovery/v1/DiscoveryServiceTest.java @@ -73,10 +73,7 @@ import static org.junit.Assert.assertEquals; -import java.io.ByteArrayInputStream; -import java.io.FileNotFoundException; -import java.io.FileReader; -import java.io.InputStream; +import java.io.*; import java.util.HashSet; import java.util.Set; @@ -440,6 +437,121 @@ public void createDocumentIsSuccessful() throws InterruptedException { assertEquals(createDocResp, response); } + @Test + public void createDocumentFromInputStreamIsSuccessful() throws InterruptedException { + server.enqueue(jsonResponse(createDocResp)); + String myDocumentJson = "{\"field\":\"value\"}"; + JsonObject myMetadata = new JsonObject(); + myMetadata.add("foo", new JsonPrimitive("bar")); + InputStream documentStream = new ByteArrayInputStream(myDocumentJson.getBytes()); + + CreateDocumentRequest.Builder builder = new CreateDocumentRequest.Builder(environmentId, collectionId); + builder.file(documentStream); + builder.metadata(myMetadata); + CreateDocumentResponse response = discoveryService.createDocument(builder.build()).execute(); + RecordedRequest request = server.takeRequest(); + + assertEquals(DOCS1_PATH, request.getPath()); + assertEquals(POST, request.getMethod()); + assertEquals(createDocResp, response); + } + + @Test + public void createDocumentFromInputStreamWithMediaTypeIsSuccessful() throws InterruptedException { + server.enqueue(jsonResponse(createDocResp)); + String myDocumentJson = "{\"field\":\"value\"}"; + JsonObject myMetadata = new JsonObject(); + myMetadata.add("foo", new JsonPrimitive("bar")); + InputStream documentStream = new ByteArrayInputStream(myDocumentJson.getBytes()); + + CreateDocumentRequest.Builder builder = new CreateDocumentRequest.Builder(environmentId, collectionId); + builder.file(documentStream, HttpMediaType.APPLICATION_JSON); + builder.metadata(myMetadata); + CreateDocumentResponse response = discoveryService.createDocument(builder.build()).execute(); + RecordedRequest request = server.takeRequest(); + + assertEquals(DOCS1_PATH, request.getPath()); + assertEquals(POST, request.getMethod()); + assertEquals(createDocResp, response); + } + + @Test + public void createDocumentFromInputStreamWithFileNameAndMediaTypeIsSuccessful() throws InterruptedException { + server.enqueue(jsonResponse(createDocResp)); + String fileName = "MyFileName"; + String myDocumentJson = "{\"field\":\"value\"}"; + JsonObject myMetadata = new JsonObject(); + myMetadata.add("foo", new JsonPrimitive("bar")); + InputStream documentStream = new ByteArrayInputStream(myDocumentJson.getBytes()); + + CreateDocumentRequest.Builder builder = new CreateDocumentRequest.Builder(environmentId, collectionId); + builder.file(documentStream, fileName, HttpMediaType.APPLICATION_JSON); + builder.metadata(myMetadata); + CreateDocumentResponse response = discoveryService.createDocument(builder.build()).execute(); + RecordedRequest request = server.takeRequest(); + + assertEquals(DOCS1_PATH, request.getPath()); + assertEquals(POST, request.getMethod()); + assertEquals(createDocResp, response); + } + + @Test + public void createDocumentFromFileWithMediaTypeIsSuccessful() throws InterruptedException { + server.enqueue(jsonResponse(createDocResp)); + String myDocumentJson = "{\"field\":\"value\"}"; + JsonObject myMetadata = new JsonObject(); + myMetadata.add("foo", new JsonPrimitive("bar")); + + CreateDocumentRequest.Builder builder = new CreateDocumentRequest.Builder(environmentId, collectionId); + + try { + File tempFile = File.createTempFile("CreateDocTest1", ".json"); + tempFile.deleteOnExit(); + BufferedWriter out = new BufferedWriter(new FileWriter(tempFile)); + out.write(myDocumentJson); + out.close(); + + builder.file(tempFile, HttpMediaType.APPLICATION_JSON); + builder.metadata(myMetadata); + CreateDocumentResponse response = discoveryService.createDocument(builder.build()).execute(); + RecordedRequest request = server.takeRequest(); + + assertEquals(DOCS1_PATH, request.getPath()); + assertEquals(POST, request.getMethod()); + assertEquals(createDocResp, response); + } catch (final IOException e) { + e.printStackTrace(); + } + } + + @Test + public void createDocumentFromFileIsSuccessful() throws InterruptedException { + server.enqueue(jsonResponse(createDocResp)); + String myDocumentJson = "{\"field\":\"value\"}"; + JsonObject myMetadata = new JsonObject(); + myMetadata.add("foo", new JsonPrimitive("bar")); + + CreateDocumentRequest.Builder builder = new CreateDocumentRequest.Builder(environmentId, collectionId); + try { + File tempFile = File.createTempFile("CreateDocTest2", ".json"); + tempFile.deleteOnExit(); + BufferedWriter out = new BufferedWriter(new FileWriter(tempFile)); + out.write(myDocumentJson); + out.close(); + + builder.file(tempFile); + builder.metadata(myMetadata); + CreateDocumentResponse response = discoveryService.createDocument(builder.build()).execute(); + RecordedRequest request = server.takeRequest(); + + assertEquals(DOCS1_PATH, request.getPath()); + assertEquals(POST, request.getMethod()); + assertEquals(createDocResp, response); + } catch (final IOException e) { + e.printStackTrace(); + } + } + @Test public void updateDocumentIsSuccessful() throws InterruptedException { server.enqueue(jsonResponse(updateDocResp));