Search in sources :

Example 6 with AddDocumentOptions

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

the class DiscoveryTest method testAddDocumentWOptions.

@Test
public void testAddDocumentWOptions() throws Throwable {
    // Schedule some responses.
    String mockResponseBody = "{\"document_id\": \"documentId\", \"status\": \"processing\"}";
    String addDocumentPath = "/v2/projects/testString/collections/testString/documents";
    server.enqueue(new MockResponse().setHeader("Content-type", "application/json").setResponseCode(202).setBody(mockResponseBody));
    constructClientService();
    // Construct an instance of the AddDocumentOptions model
    AddDocumentOptions addDocumentOptionsModel = new AddDocumentOptions.Builder().projectId("testString").collectionId("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.addDocument(addDocumentOptionsModel).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, addDocumentPath);
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) DocumentAccepted(com.ibm.watson.discovery.v2.model.DocumentAccepted) AddDocumentOptions(com.ibm.watson.discovery.v2.model.AddDocumentOptions) Test(org.testng.annotations.Test)

Example 7 with AddDocumentOptions

use of com.ibm.watson.discovery.v1.model.AddDocumentOptions in project nlp4j by oyahiroki.

the class WD2DocumentImporter method importDocument.

@Override
public void importDocument(Document doc) throws IOException {
    String fileName;
    if (doc.getAttribute("filename") != null) {
        fileName = doc.getAttributeAsString("filename");
    } else {
        fileName = "hello" + System.currentTimeMillis() + ".json";
    }
    JsonObject jsonObj = DocumentUtil.toJsonObject(doc);
    jsonObj.remove("keywords");
    {
        IamAuthenticator authenticator = new IamAuthenticator(this.DISCOVERY_APIKEY);
        Discovery v2Discovery = new Discovery("2020-08-30", authenticator);
        v2Discovery.setServiceUrl(this.DISCOVERY_URL);
        AddDocumentOptions options = // 
        new AddDocumentOptions.Builder().projectId(// 
        this.projectId).collectionId(// 
        this.collectionId).file(// 
        new ByteArrayInputStream(jsonObj.toString().getBytes(StandardCharsets.UTF_8))).filename(// 
        fileName).fileContentType(// 
        "application/json").build();
        DocumentAccepted response = v2Discovery.addDocument(options).execute().getResult();
        System.err.println(response.getStatus());
        System.err.println(response);
    }
}
Also used : DocumentAccepted(com.ibm.watson.discovery.v2.model.DocumentAccepted) AddDocumentOptions(com.ibm.watson.discovery.v2.model.AddDocumentOptions) ByteArrayInputStream(java.io.ByteArrayInputStream) IamAuthenticator(com.ibm.cloud.sdk.core.security.IamAuthenticator) Discovery(com.ibm.watson.discovery.v2.Discovery) JsonObject(com.google.gson.JsonObject)

Aggregations

AddDocumentOptions (com.ibm.watson.discovery.v2.model.AddDocumentOptions)3 DocumentAccepted (com.ibm.watson.discovery.v2.model.DocumentAccepted)3 AddDocumentOptions (com.ibm.watson.discovery.v1.model.AddDocumentOptions)2 DocumentAccepted (com.ibm.watson.discovery.v1.model.DocumentAccepted)2 MockResponse (okhttp3.mockwebserver.MockResponse)2 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)2 Test (org.testng.annotations.Test)2 JsonObject (com.google.gson.JsonObject)1 RequestBuilder (com.ibm.cloud.sdk.core.http.RequestBuilder)1 Authenticator (com.ibm.cloud.sdk.core.security.Authenticator)1 BearerTokenAuthenticator (com.ibm.cloud.sdk.core.security.BearerTokenAuthenticator)1 IamAuthenticator (com.ibm.cloud.sdk.core.security.IamAuthenticator)1 WatsonServiceUnitTest (com.ibm.watson.common.WatsonServiceUnitTest)1 WatsonServiceUnitTest (com.ibm.watson.developer_cloud.WatsonServiceUnitTest)1 AddDocumentOptions (com.ibm.watson.developer_cloud.discovery.v1.model.AddDocumentOptions)1 DocumentAccepted (com.ibm.watson.developer_cloud.discovery.v1.model.DocumentAccepted)1 Discovery (com.ibm.watson.discovery.v2.Discovery)1 DeleteDocumentOptions (com.ibm.watson.discovery.v2.model.DeleteDocumentOptions)1 QueryOptions (com.ibm.watson.discovery.v2.model.QueryOptions)1 QueryResponse (com.ibm.watson.discovery.v2.model.QueryResponse)1