Search in sources :

Example 1 with CreateTrainingExampleOptions

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

the class DiscoveryTest method testCreateTrainingExampleWOptions.

// Test the createTrainingExample operation with a valid options model parameter
@Test
public void testCreateTrainingExampleWOptions() throws Throwable {
    // Register a mock response
    String mockResponseBody = "{\"document_id\": \"documentId\", \"cross_reference\": \"crossReference\", \"relevance\": 9}";
    String createTrainingExamplePath = "/v1/environments/testString/collections/testString/training_data/testString/examples";
    server.enqueue(new MockResponse().setHeader("Content-type", "application/json").setResponseCode(201).setBody(mockResponseBody));
    // Construct an instance of the CreateTrainingExampleOptions model
    CreateTrainingExampleOptions createTrainingExampleOptionsModel = new CreateTrainingExampleOptions.Builder().environmentId("testString").collectionId("testString").queryId("testString").documentId("testString").crossReference("testString").relevance(Long.valueOf("26")).build();
    // Invoke createTrainingExample() with a valid options model and verify the result
    Response<TrainingExample> response = discoveryService.createTrainingExample(createTrainingExampleOptionsModel).execute();
    assertNotNull(response);
    TrainingExample 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, createTrainingExamplePath);
    // 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) CreateTrainingExampleOptions(com.ibm.watson.discovery.v1.model.CreateTrainingExampleOptions) TrainingExample(com.ibm.watson.discovery.v1.model.TrainingExample) Test(org.testng.annotations.Test)

Example 2 with CreateTrainingExampleOptions

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

the class Discovery method createTrainingExample.

/**
 * Add example to training data query.
 *
 * <p>Adds a example to this training data query.
 *
 * @param createTrainingExampleOptions the {@link CreateTrainingExampleOptions} containing the
 *     options for the call
 * @return a {@link ServiceCall} with a result of type {@link TrainingExample}
 */
public ServiceCall<TrainingExample> createTrainingExample(CreateTrainingExampleOptions createTrainingExampleOptions) {
    com.ibm.cloud.sdk.core.util.Validator.notNull(createTrainingExampleOptions, "createTrainingExampleOptions cannot be null");
    Map<String, String> pathParamsMap = new HashMap<String, String>();
    pathParamsMap.put("environment_id", createTrainingExampleOptions.environmentId());
    pathParamsMap.put("collection_id", createTrainingExampleOptions.collectionId());
    pathParamsMap.put("query_id", createTrainingExampleOptions.queryId());
    RequestBuilder builder = RequestBuilder.post(RequestBuilder.resolveRequestUrl(getServiceUrl(), "/v1/environments/{environment_id}/collections/{collection_id}/training_data/{query_id}/examples", pathParamsMap));
    Map<String, String> sdkHeaders = SdkCommon.getSdkHeaders("discovery", "v1", "createTrainingExample");
    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));
    final JsonObject contentJson = new JsonObject();
    if (createTrainingExampleOptions.documentId() != null) {
        contentJson.addProperty("document_id", createTrainingExampleOptions.documentId());
    }
    if (createTrainingExampleOptions.crossReference() != null) {
        contentJson.addProperty("cross_reference", createTrainingExampleOptions.crossReference());
    }
    if (createTrainingExampleOptions.relevance() != null) {
        contentJson.addProperty("relevance", createTrainingExampleOptions.relevance());
    }
    builder.bodyJson(contentJson);
    ResponseConverter<TrainingExample> responseConverter = ResponseConverterUtils.getValue(new com.google.gson.reflect.TypeToken<TrainingExample>() {
    }.getType());
    return createServiceCall(builder.build(), responseConverter);
}
Also used : RequestBuilder(com.ibm.cloud.sdk.core.http.RequestBuilder) HashMap(java.util.HashMap) TrainingExample(com.ibm.watson.discovery.v1.model.TrainingExample) JsonObject(com.google.gson.JsonObject)

Aggregations

TrainingExample (com.ibm.watson.discovery.v1.model.TrainingExample)2 JsonObject (com.google.gson.JsonObject)1 RequestBuilder (com.ibm.cloud.sdk.core.http.RequestBuilder)1 CreateTrainingExampleOptions (com.ibm.watson.discovery.v1.model.CreateTrainingExampleOptions)1 HashMap (java.util.HashMap)1 MockResponse (okhttp3.mockwebserver.MockResponse)1 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)1 Test (org.testng.annotations.Test)1