use of com.ibm.watson.discovery.v1.model.UpdateDocumentOptions in project java-sdk by watson-developer-cloud.
the class DiscoveryServiceTest method updateDocumentWithoutRequiredParametersFails.
@Test(expected = IllegalArgumentException.class)
public void updateDocumentWithoutRequiredParametersFails() {
UpdateDocumentOptions options = new UpdateDocumentOptions.Builder(environmentId, collectionId, documentId).build();
DocumentAccepted response = discoveryService.updateDocument(options).execute();
}
use of com.ibm.watson.discovery.v1.model.UpdateDocumentOptions in project java-sdk by watson-developer-cloud.
the class DiscoveryServiceTest method updateDocumentWithoutRequiredParametersFails.
/**
* Update document without required parameters fails.
*/
@Test(expected = IllegalArgumentException.class)
public void updateDocumentWithoutRequiredParametersFails() {
UpdateDocumentOptions options = new UpdateDocumentOptions.Builder(environmentId, collectionId, documentId).build();
discoveryService.updateDocument(options).execute().getResult();
}
use of com.ibm.watson.discovery.v1.model.UpdateDocumentOptions in project java-sdk by watson-developer-cloud.
the class Discovery method updateDocument.
/**
* Update a document.
*
* <p>Replace an existing document or add a document with a specified **document_id**. Starts
* ingesting a document with optional metadata.
*
* <p>**Note:** When uploading a new document with this method it automatically replaces any
* document stored with the same **document_id** if it exists.
*
* @param updateDocumentOptions the {@link UpdateDocumentOptions} containing the options for the
* call
* @return a {@link ServiceCall} with a result of type {@link DocumentAccepted}
*/
public ServiceCall<DocumentAccepted> updateDocument(UpdateDocumentOptions updateDocumentOptions) {
com.ibm.cloud.sdk.core.util.Validator.notNull(updateDocumentOptions, "updateDocumentOptions cannot be null");
com.ibm.cloud.sdk.core.util.Validator.isTrue((updateDocumentOptions.file() != null) || (updateDocumentOptions.metadata() != null), "At least one of file or metadata must be supplied.");
Map<String, String> pathParamsMap = new HashMap<String, String>();
pathParamsMap.put("environment_id", updateDocumentOptions.environmentId());
pathParamsMap.put("collection_id", updateDocumentOptions.collectionId());
pathParamsMap.put("document_id", updateDocumentOptions.documentId());
RequestBuilder builder = RequestBuilder.post(RequestBuilder.resolveRequestUrl(getServiceUrl(), "/v1/environments/{environment_id}/collections/{collection_id}/documents/{document_id}", pathParamsMap));
Map<String, String> sdkHeaders = SdkCommon.getSdkHeaders("discovery", "v1", "updateDocument");
for (Entry<String, String> header : sdkHeaders.entrySet()) {
builder.header(header.getKey(), header.getValue());
}
builder.header("Accept", "application/json");
builder.query("version", String.valueOf(this.version));
MultipartBody.Builder multipartBuilder = new MultipartBody.Builder();
multipartBuilder.setType(MultipartBody.FORM);
if (updateDocumentOptions.file() != null) {
okhttp3.RequestBody fileBody = RequestUtils.inputStreamBody(updateDocumentOptions.file(), updateDocumentOptions.fileContentType());
multipartBuilder.addFormDataPart("file", updateDocumentOptions.filename(), fileBody);
}
if (updateDocumentOptions.metadata() != null) {
multipartBuilder.addFormDataPart("metadata", updateDocumentOptions.metadata());
}
builder.body(multipartBuilder.build());
ResponseConverter<DocumentAccepted> responseConverter = ResponseConverterUtils.getValue(new com.google.gson.reflect.TypeToken<DocumentAccepted>() {
}.getType());
return createServiceCall(builder.build(), responseConverter);
}
use of com.ibm.watson.discovery.v1.model.UpdateDocumentOptions in project java-sdk by watson-developer-cloud.
the class DiscoveryTest method testUpdateDocumentWOptions.
@Test
public void testUpdateDocumentWOptions() throws Throwable {
// Schedule some responses.
String mockResponseBody = "{\"document_id\": \"documentId\", \"status\": \"processing\"}";
String updateDocumentPath = "/v2/projects/testString/collections/testString/documents/testString";
server.enqueue(new MockResponse().setHeader("Content-type", "application/json").setResponseCode(202).setBody(mockResponseBody));
constructClientService();
// Construct an instance of the UpdateDocumentOptions model
UpdateDocumentOptions updateDocumentOptionsModel = new UpdateDocumentOptions.Builder().projectId("testString").collectionId("testString").documentId("testString").file(TestUtilities.createMockStream("This is a mock file.")).filename("testString").fileContentType("application/json").metadata("testString").xWatsonDiscoveryForce(false).build();
// Invoke operation with valid options model (positive test)
Response<DocumentAccepted> response = discoveryService.updateDocument(updateDocumentOptionsModel).execute();
assertNotNull(response);
DocumentAccepted responseObj = response.getResult();
assertNotNull(responseObj);
// Verify the contents of the request
RecordedRequest request = server.takeRequest();
assertNotNull(request);
assertEquals(request.getMethod(), "POST");
// Check query
Map<String, String> query = TestUtilities.parseQueryString(request);
assertNotNull(query);
// Get query params
assertEquals(query.get("version"), "testString");
// Check request path
String parsedPath = TestUtilities.parseReqPath(request);
assertEquals(parsedPath, updateDocumentPath);
}
use of com.ibm.watson.discovery.v1.model.UpdateDocumentOptions in project java-sdk by watson-developer-cloud.
the class DiscoveryTest method testUpdateDocumentWOptions.
// Test the updateDocument operation with a valid options model parameter
@Test
public void testUpdateDocumentWOptions() throws Throwable {
// Register a mock response
String mockResponseBody = "{\"document_id\": \"documentId\", \"status\": \"processing\", \"notices\": [{\"notice_id\": \"noticeId\", \"created\": \"2019-01-01T12:00:00.000Z\", \"document_id\": \"documentId\", \"query_id\": \"queryId\", \"severity\": \"warning\", \"step\": \"step\", \"description\": \"description\"}]}";
String updateDocumentPath = "/v1/environments/testString/collections/testString/documents/testString";
server.enqueue(new MockResponse().setHeader("Content-type", "application/json").setResponseCode(202).setBody(mockResponseBody));
// Construct an instance of the UpdateDocumentOptions model
UpdateDocumentOptions updateDocumentOptionsModel = new UpdateDocumentOptions.Builder().environmentId("testString").collectionId("testString").documentId("testString").file(TestUtilities.createMockStream("This is a mock file.")).filename("testString").fileContentType("application/json").metadata("testString").build();
// Invoke updateDocument() with a valid options model and verify the result
Response<DocumentAccepted> response = discoveryService.updateDocument(updateDocumentOptionsModel).execute();
assertNotNull(response);
DocumentAccepted responseObj = response.getResult();
assertNotNull(responseObj);
// Verify the contents of the request sent to the mock server
RecordedRequest request = server.takeRequest();
assertNotNull(request);
assertEquals(request.getMethod(), "POST");
// Verify request path
String parsedPath = TestUtilities.parseReqPath(request);
assertEquals(parsedPath, updateDocumentPath);
// Verify query params
Map<String, String> query = TestUtilities.parseQueryString(request);
assertNotNull(query);
assertEquals(query.get("version"), "testString");
}
Aggregations