Search in sources :

Example 1 with UpdateTrainingQueryOptions

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

the class DiscoveryTest method testUpdateTrainingQueryWOptions.

@Test
public void testUpdateTrainingQueryWOptions() throws Throwable {
    // Schedule some responses.
    String mockResponseBody = "{\"query_id\": \"queryId\", \"natural_language_query\": \"naturalLanguageQuery\", \"filter\": \"filter\", \"created\": \"2019-01-01T12:00:00.000Z\", \"updated\": \"2019-01-01T12:00:00.000Z\", \"examples\": [{\"document_id\": \"documentId\", \"collection_id\": \"collectionId\", \"relevance\": 9, \"created\": \"2019-01-01T12:00:00.000Z\", \"updated\": \"2019-01-01T12:00:00.000Z\"}]}";
    String updateTrainingQueryPath = "/v2/projects/testString/training_data/queries/testString";
    server.enqueue(new MockResponse().setHeader("Content-type", "application/json").setResponseCode(201).setBody(mockResponseBody));
    constructClientService();
    // Construct an instance of the TrainingExample model
    TrainingExample trainingExampleModel = new TrainingExample.Builder().documentId("testString").collectionId("testString").relevance(Long.valueOf("26")).build();
    // Construct an instance of the UpdateTrainingQueryOptions model
    UpdateTrainingQueryOptions updateTrainingQueryOptionsModel = new UpdateTrainingQueryOptions.Builder().projectId("testString").queryId("testString").naturalLanguageQuery("testString").examples(new java.util.ArrayList<TrainingExample>(java.util.Arrays.asList(trainingExampleModel))).filter("testString").build();
    // Invoke operation with valid options model (positive test)
    Response<TrainingQuery> response = discoveryService.updateTrainingQuery(updateTrainingQueryOptionsModel).execute();
    assertNotNull(response);
    TrainingQuery 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, updateTrainingQueryPath);
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) UpdateTrainingQueryOptions(com.ibm.watson.discovery.v2.model.UpdateTrainingQueryOptions) TrainingExample(com.ibm.watson.discovery.v2.model.TrainingExample) TrainingQuery(com.ibm.watson.discovery.v2.model.TrainingQuery) Test(org.testng.annotations.Test)

Example 2 with UpdateTrainingQueryOptions

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

the class Discovery method updateTrainingQuery.

/**
 * Update a training query.
 *
 * <p>Updates an existing training query and it's examples.
 *
 * @param updateTrainingQueryOptions the {@link UpdateTrainingQueryOptions} containing the options
 *     for the call
 * @return a {@link ServiceCall} with a result of type {@link TrainingQuery}
 */
public ServiceCall<TrainingQuery> updateTrainingQuery(UpdateTrainingQueryOptions updateTrainingQueryOptions) {
    com.ibm.cloud.sdk.core.util.Validator.notNull(updateTrainingQueryOptions, "updateTrainingQueryOptions cannot be null");
    Map<String, String> pathParamsMap = new HashMap<String, String>();
    pathParamsMap.put("project_id", updateTrainingQueryOptions.projectId());
    pathParamsMap.put("query_id", updateTrainingQueryOptions.queryId());
    RequestBuilder builder = RequestBuilder.post(RequestBuilder.resolveRequestUrl(getServiceUrl(), "/v2/projects/{project_id}/training_data/queries/{query_id}", pathParamsMap));
    Map<String, String> sdkHeaders = SdkCommon.getSdkHeaders("discovery", "v2", "updateTrainingQuery");
    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();
    contentJson.addProperty("natural_language_query", updateTrainingQueryOptions.naturalLanguageQuery());
    contentJson.add("examples", com.ibm.cloud.sdk.core.util.GsonSingleton.getGson().toJsonTree(updateTrainingQueryOptions.examples()));
    if (updateTrainingQueryOptions.filter() != null) {
        contentJson.addProperty("filter", updateTrainingQueryOptions.filter());
    }
    builder.bodyJson(contentJson);
    ResponseConverter<TrainingQuery> responseConverter = ResponseConverterUtils.getValue(new com.google.gson.reflect.TypeToken<TrainingQuery>() {
    }.getType());
    return createServiceCall(builder.build(), responseConverter);
}
Also used : RequestBuilder(com.ibm.cloud.sdk.core.http.RequestBuilder) HashMap(java.util.HashMap) JsonObject(com.google.gson.JsonObject) TrainingQuery(com.ibm.watson.discovery.v2.model.TrainingQuery)

Aggregations

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