Search in sources :

Example 11 with DocumentAccepted

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

the class DiscoveryTest method testAddDocumentWOptions.

// Test the addDocument operation with a valid options model parameter
@Test
public void testAddDocumentWOptions() 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 addDocumentPath = "/v1/environments/testString/collections/testString/documents";
    server.enqueue(new MockResponse().setHeader("Content-type", "application/json").setResponseCode(202).setBody(mockResponseBody));
    // Construct an instance of the AddDocumentOptions model
    AddDocumentOptions addDocumentOptionsModel = new AddDocumentOptions.Builder().environmentId("testString").collectionId("testString").file(TestUtilities.createMockStream("This is a mock file.")).filename("testString").fileContentType("application/json").metadata("testString").build();
    // Invoke addDocument() with a valid options model and verify the result
    Response<DocumentAccepted> response = discoveryService.addDocument(addDocumentOptionsModel).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, addDocumentPath);
    // 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) DocumentAccepted(com.ibm.watson.discovery.v1.model.DocumentAccepted) AddDocumentOptions(com.ibm.watson.discovery.v1.model.AddDocumentOptions) Test(org.testng.annotations.Test)

Example 12 with DocumentAccepted

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

the class DiscoveryServiceTest method addDocumentFromInputStreamWithMediaTypeIsSuccessful.

/**
 * Adds the document from input stream with media type is successful.
 *
 * @throws InterruptedException the interrupted exception
 */
@Test
public void addDocumentFromInputStreamWithMediaTypeIsSuccessful() 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 13 with DocumentAccepted

use of com.ibm.watson.discovery.v1.model.DocumentAccepted 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);
}
Also used : DocumentAccepted(com.ibm.watson.discovery.v1.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)

Example 14 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>* The **version** query parameter is still required.
 *
 * <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>* The user 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 will be 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 containing the following characters after normalization are filtered out before
 * indexing: `#` and `,`
 *
 * <p>**Note:** Documents can be added with a specific **document_id** by using the
 * **_/v1/environments/{environment_id}/collections/{collection_id}/documents** method.
 *
 * @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("environment_id", addDocumentOptions.environmentId());
    pathParamsMap.put("collection_id", addDocumentOptions.collectionId());
    RequestBuilder builder = RequestBuilder.post(RequestBuilder.resolveRequestUrl(getServiceUrl(), "/v1/environments/{environment_id}/collections/{collection_id}/documents", pathParamsMap));
    Map<String, String> sdkHeaders = SdkCommon.getSdkHeaders("discovery", "v1", "addDocument");
    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 (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.v1.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)

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

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