Search in sources :

Example 31 with DocumentAccepted

use of com.ibm.watson.discovery.v1.model.DocumentAccepted in project java-sdk by watson-developer-cloud.

the class DiscoveryServiceTest method updateDocumentIsSuccessful.

// Deleted tests for (create)addDocument with file parameter as this is deprecated
/**
 * Update document is successful.
 *
 * @throws InterruptedException the interrupted exception
 */
@Test
public void updateDocumentIsSuccessful() throws InterruptedException {
    server.enqueue(jsonResponse(updateDocResp));
    UpdateDocumentOptions.Builder updateBuilder = new UpdateDocumentOptions.Builder(environmentId, collectionId, documentId);
    String myDocumentJson = "{\"field\":\"value2\"}";
    JsonObject myMetadata = new JsonObject();
    myMetadata.add("foo", new JsonPrimitive("bar"));
    InputStream documentStream = new ByteArrayInputStream(myDocumentJson.getBytes());
    updateBuilder.file(documentStream).fileContentType(HttpMediaType.APPLICATION_JSON);
    updateBuilder.filename("test_file");
    updateBuilder.metadata(myMetadata.toString());
    DocumentAccepted response = discoveryService.updateDocument(updateBuilder.build()).execute().getResult();
    RecordedRequest request = server.takeRequest();
    assertEquals(DOCS2_PATH, request.getPath());
    assertEquals(POST, request.getMethod());
    assertEquals(updateDocResp, response);
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) UpdateDocumentOptions(com.ibm.watson.discovery.v1.model.UpdateDocumentOptions) DocumentAccepted(com.ibm.watson.discovery.v1.model.DocumentAccepted) JsonPrimitive(com.google.gson.JsonPrimitive) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) JsonObject(com.google.gson.JsonObject) WatsonServiceUnitTest(com.ibm.watson.common.WatsonServiceUnitTest)

Example 32 with DocumentAccepted

use of com.ibm.watson.discovery.v1.model.DocumentAccepted in project java-sdk by watson-developer-cloud.

the class DiscoveryServiceTest method addDocumentIsSuccessful.

/**
 * Adds the document is successful.
 *
 * @throws InterruptedException the interrupted exception
 */
// Document tests
@Test
public void addDocumentIsSuccessful() 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());
    AddDocumentOptions.Builder builder = new AddDocumentOptions.Builder(environmentId, collectionId);
    builder.file(documentStream).fileContentType(HttpMediaType.APPLICATION_JSON);
    builder.filename("test_file");
    builder.metadata(myMetadata.toString());
    DocumentAccepted response = discoveryService.addDocument(builder.build()).execute().getResult();
    RecordedRequest request = server.takeRequest();
    assertEquals(DOCS1_PATH, request.getPath());
    assertEquals(POST, request.getMethod());
    assertEquals(createDocResp, response);
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) DocumentAccepted(com.ibm.watson.discovery.v1.model.DocumentAccepted) JsonPrimitive(com.google.gson.JsonPrimitive) ByteArrayInputStream(java.io.ByteArrayInputStream) AddDocumentOptions(com.ibm.watson.discovery.v1.model.AddDocumentOptions) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) JsonObject(com.google.gson.JsonObject) WatsonServiceUnitTest(com.ibm.watson.common.WatsonServiceUnitTest)

Example 33 with DocumentAccepted

use of com.ibm.watson.discovery.v1.model.DocumentAccepted in project java-sdk by watson-developer-cloud.

the class DiscoveryServiceTest method addDocumentFromInputStreamWithFileNameAndMediaTypeIsSuccessful.

/**
 * Adds the document from input stream with file name and media type is successful.
 *
 * @throws InterruptedException the interrupted exception
 */
@Test
public void addDocumentFromInputStreamWithFileNameAndMediaTypeIsSuccessful() 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());
    AddDocumentOptions.Builder builder = new AddDocumentOptions.Builder(environmentId, collectionId);
    builder.file(documentStream).fileContentType(HttpMediaType.APPLICATION_JSON);
    builder.filename("test_file");
    builder.metadata(myMetadata.toString());
    DocumentAccepted response = discoveryService.addDocument(builder.build()).execute().getResult();
    RecordedRequest request = server.takeRequest();
    assertEquals(DOCS1_PATH, request.getPath());
    assertEquals(POST, request.getMethod());
    assertEquals(createDocResp, response);
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) DocumentAccepted(com.ibm.watson.discovery.v1.model.DocumentAccepted) JsonPrimitive(com.google.gson.JsonPrimitive) ByteArrayInputStream(java.io.ByteArrayInputStream) AddDocumentOptions(com.ibm.watson.discovery.v1.model.AddDocumentOptions) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) JsonObject(com.google.gson.JsonObject) WatsonServiceUnitTest(com.ibm.watson.common.WatsonServiceUnitTest)

Example 34 with DocumentAccepted

use of com.ibm.watson.discovery.v1.model.DocumentAccepted 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");
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) UpdateDocumentOptions(com.ibm.watson.discovery.v1.model.UpdateDocumentOptions) DocumentAccepted(com.ibm.watson.discovery.v1.model.DocumentAccepted) Test(org.testng.annotations.Test)

Example 35 with DocumentAccepted

use of com.ibm.watson.discovery.v1.model.DocumentAccepted in project java-sdk by watson-developer-cloud.

the class Discovery method addDocument.

/**
 * Add a document.
 *
 * <p>Add a document to a collection with optional metadata.
 *
 * <p>Returns immediately after the system has accepted the document for processing.
 *
 * <p>* The user must provide document content, metadata, or both. If the request is missing both
 * document content and metadata, it is rejected.
 *
 * <p>* You can set the **Content-Type** parameter on the **file** part to indicate the media type
 * of the document. If the **Content-Type** parameter is missing or is one of the generic media
 * types (for example, `application/octet-stream`), then the service attempts to automatically
 * detect the document's media type.
 *
 * <p>* The following field names are reserved and are filtered out if present after
 * normalization: `id`, `score`, `highlight`, and any field with the prefix of: `_`, `+`, or `-`
 *
 * <p>* Fields with empty name values after normalization are filtered out before indexing.
 *
 * <p>* Fields that contain the following characters after normalization are filtered out before
 * indexing: `#` and `,`
 *
 * <p>If the document is uploaded to a collection that shares its data with another collection,
 * the **X-Watson-Discovery-Force** header must be set to `true`.
 *
 * <p>**Note:** You can assign an ID to a document that you add by appending the ID to the
 * endpoint (`/v2/projects/{project_id}/collections/{collection_id}/documents/{document_id}`). If
 * a document already exists with the specified ID, it is replaced.
 *
 * <p>**Note:** This operation works with a file upload collection. It cannot be used to modify a
 * collection that crawls an external data source.
 *
 * @param addDocumentOptions the {@link AddDocumentOptions} containing the options for the call
 * @return a {@link ServiceCall} with a result of type {@link DocumentAccepted}
 */
public ServiceCall<DocumentAccepted> addDocument(AddDocumentOptions addDocumentOptions) {
    com.ibm.cloud.sdk.core.util.Validator.notNull(addDocumentOptions, "addDocumentOptions cannot be null");
    com.ibm.cloud.sdk.core.util.Validator.isTrue((addDocumentOptions.file() != null) || (addDocumentOptions.metadata() != null), "At least one of file or metadata must be supplied.");
    Map<String, String> pathParamsMap = new HashMap<String, String>();
    pathParamsMap.put("project_id", addDocumentOptions.projectId());
    pathParamsMap.put("collection_id", addDocumentOptions.collectionId());
    RequestBuilder builder = RequestBuilder.post(RequestBuilder.resolveRequestUrl(getServiceUrl(), "/v2/projects/{project_id}/collections/{collection_id}/documents", pathParamsMap));
    Map<String, String> sdkHeaders = SdkCommon.getSdkHeaders("discovery", "v2", "addDocument");
    for (Entry<String, String> header : sdkHeaders.entrySet()) {
        builder.header(header.getKey(), header.getValue());
    }
    builder.header("Accept", "application/json");
    if (addDocumentOptions.xWatsonDiscoveryForce() != null) {
        builder.header("X-Watson-Discovery-Force", addDocumentOptions.xWatsonDiscoveryForce());
    }
    builder.query("version", String.valueOf(this.version));
    MultipartBody.Builder multipartBuilder = new MultipartBody.Builder();
    multipartBuilder.setType(MultipartBody.FORM);
    if (addDocumentOptions.file() != null) {
        okhttp3.RequestBody fileBody = RequestUtils.inputStreamBody(addDocumentOptions.file(), addDocumentOptions.fileContentType());
        multipartBuilder.addFormDataPart("file", addDocumentOptions.filename(), fileBody);
    }
    if (addDocumentOptions.metadata() != null) {
        multipartBuilder.addFormDataPart("metadata", addDocumentOptions.metadata());
    }
    builder.body(multipartBuilder.build());
    ResponseConverter<DocumentAccepted> responseConverter = ResponseConverterUtils.getValue(new com.google.gson.reflect.TypeToken<DocumentAccepted>() {
    }.getType());
    return createServiceCall(builder.build(), responseConverter);
}
Also used : DocumentAccepted(com.ibm.watson.discovery.v2.model.DocumentAccepted) RequestBuilder(com.ibm.cloud.sdk.core.http.RequestBuilder) HashMap(java.util.HashMap) RequestBuilder(com.ibm.cloud.sdk.core.http.RequestBuilder) MultipartBody(okhttp3.MultipartBody)

Aggregations

DocumentAccepted (com.ibm.watson.developer_cloud.discovery.v1.model.DocumentAccepted)20 ByteArrayInputStream (java.io.ByteArrayInputStream)20 InputStream (java.io.InputStream)20 Test (org.junit.Test)16 JsonObject (com.google.gson.JsonObject)14 FileInputStream (java.io.FileInputStream)14 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)14 JsonPrimitive (com.google.gson.JsonPrimitive)13 AddDocumentOptions (com.ibm.watson.developer_cloud.discovery.v1.model.AddDocumentOptions)11 Collection (com.ibm.watson.developer_cloud.discovery.v1.model.Collection)10 DocumentAccepted (com.ibm.watson.discovery.v1.model.DocumentAccepted)10 WatsonServiceTest (com.ibm.watson.developer_cloud.WatsonServiceTest)9 WatsonServiceUnitTest (com.ibm.watson.developer_cloud.WatsonServiceUnitTest)7 AddDocumentOptions (com.ibm.watson.discovery.v1.model.AddDocumentOptions)6 DocumentAccepted (com.ibm.watson.discovery.v2.model.DocumentAccepted)6 MultipartBody (okhttp3.MultipartBody)6 WatsonServiceUnitTest (com.ibm.watson.common.WatsonServiceUnitTest)5 UpdateDocumentOptions (com.ibm.watson.developer_cloud.discovery.v1.model.UpdateDocumentOptions)5 RequestBuilder (com.ibm.cloud.sdk.core.http.RequestBuilder)4 DocumentStatus (com.ibm.watson.developer_cloud.discovery.v1.model.DocumentStatus)4