Search in sources :

Example 11 with TrainingQuery

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

the class DiscoveryTest method testCreateTrainingQueryWOptions.

@Test
public void testCreateTrainingQueryWOptions() 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 createTrainingQueryPath = "/v2/projects/testString/training_data/queries";
    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 CreateTrainingQueryOptions model
    CreateTrainingQueryOptions createTrainingQueryOptionsModel = new CreateTrainingQueryOptions.Builder().projectId("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.createTrainingQuery(createTrainingQueryOptionsModel).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, createTrainingQueryPath);
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) TrainingExample(com.ibm.watson.discovery.v2.model.TrainingExample) CreateTrainingQueryOptions(com.ibm.watson.discovery.v2.model.CreateTrainingQueryOptions) TrainingQuery(com.ibm.watson.discovery.v2.model.TrainingQuery) Test(org.testng.annotations.Test)

Example 12 with TrainingQuery

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

the class Discovery method getTrainingQuery.

/**
 * Get a training data query.
 *
 * <p>Get details for a specific training data query, including the query string and all examples.
 *
 * @param getTrainingQueryOptions the {@link GetTrainingQueryOptions} containing the options for
 *     the call
 * @return a {@link ServiceCall} with a result of type {@link TrainingQuery}
 */
public ServiceCall<TrainingQuery> getTrainingQuery(GetTrainingQueryOptions getTrainingQueryOptions) {
    com.ibm.cloud.sdk.core.util.Validator.notNull(getTrainingQueryOptions, "getTrainingQueryOptions cannot be null");
    Map<String, String> pathParamsMap = new HashMap<String, String>();
    pathParamsMap.put("project_id", getTrainingQueryOptions.projectId());
    pathParamsMap.put("query_id", getTrainingQueryOptions.queryId());
    RequestBuilder builder = RequestBuilder.get(RequestBuilder.resolveRequestUrl(getServiceUrl(), "/v2/projects/{project_id}/training_data/queries/{query_id}", pathParamsMap));
    Map<String, String> sdkHeaders = SdkCommon.getSdkHeaders("discovery", "v2", "getTrainingQuery");
    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));
    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) TrainingQuery(com.ibm.watson.discovery.v2.model.TrainingQuery)

Example 13 with TrainingQuery

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

the class Discovery method createTrainingQuery.

/**
 * Create training query.
 *
 * <p>Add a query to the training data for this project. The query can contain a filter and
 * natural language query.
 *
 * @param createTrainingQueryOptions the {@link CreateTrainingQueryOptions} containing the options
 *     for the call
 * @return a {@link ServiceCall} with a result of type {@link TrainingQuery}
 */
public ServiceCall<TrainingQuery> createTrainingQuery(CreateTrainingQueryOptions createTrainingQueryOptions) {
    com.ibm.cloud.sdk.core.util.Validator.notNull(createTrainingQueryOptions, "createTrainingQueryOptions cannot be null");
    Map<String, String> pathParamsMap = new HashMap<String, String>();
    pathParamsMap.put("project_id", createTrainingQueryOptions.projectId());
    RequestBuilder builder = RequestBuilder.post(RequestBuilder.resolveRequestUrl(getServiceUrl(), "/v2/projects/{project_id}/training_data/queries", pathParamsMap));
    Map<String, String> sdkHeaders = SdkCommon.getSdkHeaders("discovery", "v2", "createTrainingQuery");
    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", createTrainingQueryOptions.naturalLanguageQuery());
    contentJson.add("examples", com.ibm.cloud.sdk.core.util.GsonSingleton.getGson().toJsonTree(createTrainingQueryOptions.examples()));
    if (createTrainingQueryOptions.filter() != null) {
        contentJson.addProperty("filter", createTrainingQueryOptions.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)

Example 14 with TrainingQuery

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

the class DiscoveryServiceTest method getTrainingDataIsSuccessful.

@Test
public void getTrainingDataIsSuccessful() throws InterruptedException {
    server.enqueue(jsonResponse(getTrainingDataResp));
    GetTrainingDataOptions.Builder builder = new GetTrainingDataOptions.Builder(environmentId, collectionId, queryId);
    TrainingQuery response = discoveryService.getTrainingData(builder.build()).execute();
    RecordedRequest request = server.takeRequest();
    assertEquals(TRAINING3_PATH, request.getPath());
    assertEquals(GET, request.getMethod());
    assertEquals(getTrainingDataResp, response);
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) GetTrainingDataOptions(com.ibm.watson.developer_cloud.discovery.v1.model.GetTrainingDataOptions) TrainingQuery(com.ibm.watson.developer_cloud.discovery.v1.model.TrainingQuery) WatsonServiceUnitTest(com.ibm.watson.developer_cloud.WatsonServiceUnitTest) Test(org.junit.Test)

Example 15 with TrainingQuery

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

the class DiscoveryServiceTest method addTrainingDataIsSuccessful.

// Training data tests
@Test
public void addTrainingDataIsSuccessful() throws InterruptedException {
    server.enqueue(jsonResponse(addTrainingQueryResp));
    AddTrainingDataOptions.Builder builder = new AddTrainingDataOptions.Builder(environmentId, collectionId);
    builder.naturalLanguageQuery("Example query");
    TrainingExample example = new TrainingExample();
    example.setDocumentId(documentId);
    example.setRelevance(0);
    builder.addExamples(example);
    TrainingQuery response = discoveryService.addTrainingData(builder.build()).execute();
    RecordedRequest request = server.takeRequest();
    assertEquals(TRAINING1_PATH, request.getPath());
    assertEquals(POST, request.getMethod());
    assertEquals(addTrainingQueryResp, response);
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) AddTrainingDataOptions(com.ibm.watson.developer_cloud.discovery.v1.model.AddTrainingDataOptions) TrainingExample(com.ibm.watson.developer_cloud.discovery.v1.model.TrainingExample) TrainingQuery(com.ibm.watson.developer_cloud.discovery.v1.model.TrainingQuery) WatsonServiceUnitTest(com.ibm.watson.developer_cloud.WatsonServiceUnitTest) Test(org.junit.Test)

Aggregations

TrainingQuery (com.ibm.watson.developer_cloud.discovery.v1.model.TrainingQuery)12 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)9 Test (org.junit.Test)9 WatsonServiceTest (com.ibm.watson.developer_cloud.WatsonServiceTest)7 TrainingExample (com.ibm.watson.developer_cloud.discovery.v1.model.TrainingExample)6 TrainingQuery (com.ibm.watson.discovery.v1.model.TrainingQuery)6 TrainingQuery (com.ibm.watson.discovery.v2.model.TrainingQuery)6 RequestBuilder (com.ibm.cloud.sdk.core.http.RequestBuilder)5 AddTrainingDataOptions (com.ibm.watson.developer_cloud.discovery.v1.model.AddTrainingDataOptions)5 HashMap (java.util.HashMap)5 MockResponse (okhttp3.mockwebserver.MockResponse)5 Test (org.testng.annotations.Test)5 JsonObject (com.google.gson.JsonObject)4 GetTrainingDataOptions (com.ibm.watson.developer_cloud.discovery.v1.model.GetTrainingDataOptions)4 WatsonServiceUnitTest (com.ibm.watson.common.WatsonServiceUnitTest)2 WatsonServiceUnitTest (com.ibm.watson.developer_cloud.WatsonServiceUnitTest)2 CreateTrainingExampleOptions (com.ibm.watson.developer_cloud.discovery.v1.model.CreateTrainingExampleOptions)2 RequestBuilder (com.ibm.watson.developer_cloud.http.RequestBuilder)2 AddTrainingDataOptions (com.ibm.watson.discovery.v1.model.AddTrainingDataOptions)2 GetTrainingDataOptions (com.ibm.watson.discovery.v1.model.GetTrainingDataOptions)2