Skip to content

Commit

Permalink
Merge pull request #630 from tdumitriu/fix_create_document
Browse files Browse the repository at this point in the history
🐛 create or update a document from an InputStream and optionally …
  • Loading branch information
germanattanasio authored Apr 2, 2017
2 parents 77f6a21 + 30dfb6a commit 8357064
Show file tree
Hide file tree
Showing 3 changed files with 172 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,6 @@ public ServiceCall<CreateDocumentResponse> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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() {
Expand All @@ -66,17 +68,23 @@ public String getMediaType() {
return mediaType;
}

public String getFileName() {
return fileName;
}

public static class Builder {
private final String environmentId;
private final String collectionId;
private String configurationId;
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) {
Expand All @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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));
Expand Down

0 comments on commit 8357064

Please sign in to comment.