Skip to content

Commit

Permalink
🆕 Provide a specific document Id when creating a document
Browse files Browse the repository at this point in the history
  • Loading branch information
Tibor Dumitriu committed Apr 5, 2017
1 parent f9899ba commit d581c3f
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -348,9 +348,19 @@ public ServiceCall<GetDocumentResponse> getDocument(GetDocumentRequest getReques
public ServiceCall<CreateDocumentResponse> createDocument(CreateDocumentRequest createRequest) {
Validator.notEmpty(createRequest.getEnvironmentId(), EnvironmentManager.ID + " cannot be empty");
Validator.notEmpty(createRequest.getCollectionId(), CollectionManager.ID + " cannot be empty");
RequestBuilder builder = RequestBuilder
.post(String
.format(PATH_DOCUMENTS, createRequest.getEnvironmentId(), createRequest.getCollectionId()));

String pathElements;
if (createRequest.getDocumentId() == null) {
pathElements = String.format(PATH_DOCUMENTS, createRequest.getEnvironmentId(),
createRequest.getCollectionId());
} else {
Validator.notEmpty(createRequest.getDocumentId(), DocumentManager.ID + " cannot be empty");
pathElements = String.format(PATH_DOCUMENT, createRequest.getEnvironmentId(),
createRequest.getCollectionId(), createRequest.getDocumentId());
}

RequestBuilder builder = RequestBuilder.post(pathElements);

if (createRequest.getConfigurationId() != null) {
builder.query(CollectionManager.CONFIGURATION_ID, createRequest.getConfigurationId());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public class CreateDocumentRequest extends GenericModel {
private final String environmentId;
private final String collectionId;
private String configurationId;
private String documentId;
private JsonObject metadata;
private InputStream file;
private String mediaType;
Expand All @@ -38,6 +39,7 @@ protected CreateDocumentRequest(Builder builder) {
this.environmentId = builder.environmentId;
this.collectionId = builder.collectionId;
this.configurationId = builder.configurationId;
this.documentId = builder.documentId;
this.metadata = builder.metadata;
this.file = builder.file;
this.mediaType = builder.mediaType;
Expand All @@ -52,6 +54,10 @@ public String getCollectionId() {
return collectionId;
}

public String getDocumentId() {
return documentId;
}

public String getConfigurationId() {
return configurationId;
}
Expand All @@ -76,6 +82,7 @@ public static class Builder {
private final String environmentId;
private final String collectionId;
private String configurationId;
private String documentId;
private JsonObject metadata;
private InputStream file;
private String mediaType;
Expand All @@ -87,6 +94,11 @@ public Builder(String environmentId, String collectionId) {
this.fileName = this.fileName == null || this.fileName.isEmpty() ? "file_name_not_provided" : this.fileName;
}

public Builder documentId(String documentId) {
this.documentId = documentId;
return this;
}

public Builder configurationId(String configurationId) {
this.configurationId = configurationId;
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -559,6 +559,35 @@ public void createDocumentFromFileIsSuccessful() throws InterruptedException {
}
}

@Test
public void createDocumentFromFileWithGivenIdIsSuccessful() 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("CreateDocTest3", ".json");
tempFile.deleteOnExit();
BufferedWriter out = new BufferedWriter(new FileWriter(tempFile));
out.write(myDocumentJson);
out.close();

builder.file(tempFile);
builder.metadata(myMetadata);
builder.documentId(documentId);
CreateDocumentResponse response = discoveryService.createDocument(builder.build()).execute();
RecordedRequest request = server.takeRequest();

assertEquals(DOCS2_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 d581c3f

Please sign in to comment.