use of com.ibm.watson.discovery.v2.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);
}
use of com.ibm.watson.discovery.v2.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);
}
use of com.ibm.watson.discovery.v2.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);
}
use of com.ibm.watson.discovery.v2.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));
}
use of com.ibm.watson.discovery.v2.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());
}
Aggregations