Search in sources :

Example 16 with TrainingExample

use of com.ibm.watson.discovery.v1.model.TrainingExample 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 17 with TrainingExample

use of com.ibm.watson.discovery.v1.model.TrainingExample 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)

Example 18 with TrainingExample

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

the class DiscoveryServiceTest method updateTrainingExampleIsSuccessful.

@Test
public void updateTrainingExampleIsSuccessful() throws InterruptedException {
    server.enqueue(jsonResponse(updateTrainingExampleResp));
    UpdateTrainingExampleOptions.Builder builder = new UpdateTrainingExampleOptions.Builder(environmentId, collectionId, queryId, documentId);
    builder.relevance(100);
    TrainingExample response = discoveryService.updateTrainingExample(builder.build()).execute();
    RecordedRequest request = server.takeRequest();
    assertEquals(TRAINING4_PATH, request.getPath());
    assertEquals(PUT, request.getMethod());
    assertEquals(updateTrainingExampleResp, response);
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) UpdateTrainingExampleOptions(com.ibm.watson.developer_cloud.discovery.v1.model.UpdateTrainingExampleOptions) TrainingExample(com.ibm.watson.developer_cloud.discovery.v1.model.TrainingExample) WatsonServiceUnitTest(com.ibm.watson.developer_cloud.WatsonServiceUnitTest) Test(org.junit.Test)

Example 19 with TrainingExample

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

the class DiscoveryServiceIT method addTrainingDataIsSuccessful.

@Test
public void addTrainingDataIsSuccessful() {
    AddTrainingDataOptions.Builder builder = new AddTrainingDataOptions.Builder(environmentId, collectionId);
    String naturalLanguageQuery = "Example query" + UUID.randomUUID().toString();
    builder.naturalLanguageQuery(naturalLanguageQuery);
    TrainingExample example = new TrainingExample();
    String documentId = createTestDocument("test_document", collectionId).getDocumentId();
    example.setDocumentId(documentId);
    int relevance = 0;
    example.setRelevance(relevance);
    builder.addExamples(example);
    TrainingQuery response = discovery.addTrainingData(builder.build()).execute();
    assertFalse(response.getQueryId().isEmpty());
    assertEquals(response.getNaturalLanguageQuery(), naturalLanguageQuery);
    assertTrue(response.getFilter().isEmpty());
    assertEquals(response.getExamples().size(), 1);
    TrainingExample returnedExample = response.getExamples().get(0);
    assertEquals(returnedExample.getDocumentId(), documentId);
    assertTrue(returnedExample.getCrossReference().isEmpty());
    assertEquals(returnedExample.getRelevance(), new Long(relevance));
}
Also used : 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) Test(org.junit.Test) WatsonServiceTest(com.ibm.watson.developer_cloud.WatsonServiceTest)

Example 20 with TrainingExample

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

the class DiscoveryServiceIT method deleteTrainingDataExampleIsSuccessful.

@Test
public void deleteTrainingDataExampleIsSuccessful() {
    TrainingQuery newQuery = createTestQuery(collectionId, "Query" + UUID.randomUUID().toString());
    String queryId = newQuery.getQueryId();
    String documentId = "document_id";
    String crossReference = "cross_reference";
    int relevance = 50;
    CreateTrainingExampleOptions.Builder exampleBuilder = new CreateTrainingExampleOptions.Builder(environmentId, collectionId, queryId);
    exampleBuilder.documentId(documentId);
    exampleBuilder.crossReference(crossReference);
    exampleBuilder.relevance(relevance);
    TrainingExample createdExample = discovery.createTrainingExample(exampleBuilder.build()).execute();
    String exampleId = createdExample.getDocumentId();
    GetTrainingDataOptions.Builder queryBuilder = new GetTrainingDataOptions.Builder(environmentId, collectionId, queryId);
    TrainingQuery queryWithAddedExample = discovery.getTrainingData(queryBuilder.build()).execute();
    int startingCount = queryWithAddedExample.getExamples().size();
    DeleteTrainingExampleOptions.Builder deleteBuilder = new DeleteTrainingExampleOptions.Builder(environmentId, collectionId, queryId, exampleId);
    discovery.deleteTrainingExample(deleteBuilder.build()).execute();
    GetTrainingDataOptions.Builder newQueryBuilder = new GetTrainingDataOptions.Builder(environmentId, collectionId, queryId);
    TrainingQuery queryWithDeletedExample = discovery.getTrainingData(newQueryBuilder.build()).execute();
    assertTrue(startingCount > queryWithDeletedExample.getExamples().size());
}
Also used : CreateTrainingExampleOptions(com.ibm.watson.developer_cloud.discovery.v1.model.CreateTrainingExampleOptions) GetTrainingDataOptions(com.ibm.watson.developer_cloud.discovery.v1.model.GetTrainingDataOptions) TrainingExample(com.ibm.watson.developer_cloud.discovery.v1.model.TrainingExample) DeleteTrainingExampleOptions(com.ibm.watson.developer_cloud.discovery.v1.model.DeleteTrainingExampleOptions) TrainingQuery(com.ibm.watson.developer_cloud.discovery.v1.model.TrainingQuery) Test(org.junit.Test) WatsonServiceTest(com.ibm.watson.developer_cloud.WatsonServiceTest)

Aggregations

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