Search in sources :

Example 1 with CounterexampleCollection

use of com.ibm.watson.developer_cloud.assistant.v1.model.CounterexampleCollection in project java-sdk by watson-developer-cloud.

the class AssistantServiceIT method testListCounterexamplesWithPaging.

/**
 * Test listCounterexamples with paging.
 */
@Test
public void testListCounterexamplesWithPaging() {
    // gotta be unique
    String counterExampleText1 = "alpha" + UUID.randomUUID().toString();
    // gotta be unique
    String counterExampleText2 = "zeta" + UUID.randomUUID().toString();
    // Add two counterexamples
    CreateCounterexampleOptions createOptions = new CreateCounterexampleOptions.Builder(workspaceId, counterExampleText1).build();
    service.createCounterexample(createOptions).execute();
    service.createCounterexample(createOptions.newBuilder().text(counterExampleText2).build()).execute();
    try {
        ListCounterexamplesOptions listOptions = new ListCounterexamplesOptions.Builder(workspaceId).pageLimit(1L).sort("text").build();
        CounterexampleCollection response = service.listCounterexamples(listOptions).execute();
        assertNotNull(response);
        assertNotNull(response.getPagination());
        assertNotNull(response.getPagination().getRefreshUrl());
        assertNotNull(response.getPagination().getNextUrl());
        assertNotNull(response.getPagination().getCursor());
        boolean found1 = false, found2 = false;
        while (true) {
            assertNotNull(response.getCounterexamples());
            assertTrue(response.getCounterexamples().size() == 1);
            found1 |= response.getCounterexamples().get(0).getText().equals(counterExampleText1);
            found2 |= response.getCounterexamples().get(0).getText().equals(counterExampleText2);
            // verify sort
            assertTrue(found1 || !found2);
            if (response.getPagination().getCursor() == null) {
                break;
            }
            String cursor = response.getPagination().getCursor();
            response = service.listCounterexamples(listOptions.newBuilder().cursor(cursor).build()).execute();
        }
        assertTrue(found1 && found2);
    } catch (Exception ex) {
        fail(ex.getMessage());
    } finally {
        // Clean up
        DeleteCounterexampleOptions deleteOptions = new DeleteCounterexampleOptions.Builder(workspaceId, counterExampleText1).build();
        service.deleteCounterexample(deleteOptions).execute();
        service.deleteCounterexample(deleteOptions.newBuilder().text(counterExampleText2).build()).execute();
    }
}
Also used : ListCounterexamplesOptions(com.ibm.watson.developer_cloud.assistant.v1.model.ListCounterexamplesOptions) CreateCounterexampleOptions(com.ibm.watson.developer_cloud.assistant.v1.model.CreateCounterexampleOptions) DeleteCounterexampleOptions(com.ibm.watson.developer_cloud.assistant.v1.model.DeleteCounterexampleOptions) UnauthorizedException(com.ibm.watson.developer_cloud.service.exception.UnauthorizedException) NotFoundException(com.ibm.watson.developer_cloud.service.exception.NotFoundException) CounterexampleCollection(com.ibm.watson.developer_cloud.assistant.v1.model.CounterexampleCollection) Test(org.junit.Test)

Example 2 with CounterexampleCollection

use of com.ibm.watson.developer_cloud.assistant.v1.model.CounterexampleCollection in project java-sdk by watson-developer-cloud.

the class AssistantServiceIT method testListCounterexamples.

/**
 * Test listCounterexamples.
 */
@Test
public void testListCounterexamples() {
    // gotta be unique
    String counterExampleText = "Make me a " + UUID.randomUUID().toString() + " sandwich";
    try {
        ListCounterexamplesOptions listOptions = new ListCounterexamplesOptions.Builder(workspaceId).build();
        CounterexampleCollection ccResponse = service.listCounterexamples(listOptions).execute();
        assertNotNull(ccResponse);
        assertNotNull(ccResponse.getCounterexamples());
        assertNotNull(ccResponse.getPagination());
        assertNotNull(ccResponse.getPagination().getRefreshUrl());
        // nextUrl may be null
        Date start = new Date();
        // Now add a counterexample and make sure we get it back
        CreateCounterexampleOptions createOptions = new CreateCounterexampleOptions.Builder(workspaceId, counterExampleText).build();
        service.createCounterexample(createOptions).execute();
        long count = ccResponse.getCounterexamples().size();
        CounterexampleCollection ccResponse2 = service.listCounterexamples(listOptions.newBuilder().pageLimit(count + 1).includeAudit(true).build()).execute();
        assertNotNull(ccResponse2);
        assertNotNull(ccResponse2.getCounterexamples());
        List<Counterexample> counterexamples = ccResponse2.getCounterexamples();
        assertTrue(counterexamples.size() > count);
        Counterexample exResponse = null;
        for (Counterexample resp : counterexamples) {
            if (resp.getText().equals(counterExampleText)) {
                exResponse = resp;
                break;
            }
        }
        assertNotNull(exResponse);
        Date now = new Date();
        assertTrue(fuzzyBefore(exResponse.getCreated(), now));
        assertTrue(fuzzyAfter(exResponse.getCreated(), start));
        assertTrue(fuzzyBefore(exResponse.getUpdated(), now));
        assertTrue(fuzzyAfter(exResponse.getUpdated(), start));
    } catch (Exception ex) {
        fail(ex.getMessage());
    } finally {
        // Clean up
        try {
            DeleteCounterexampleOptions deleteOptions = new DeleteCounterexampleOptions.Builder(workspaceId, counterExampleText).build();
            service.deleteCounterexample(deleteOptions).execute();
        } catch (NotFoundException ex) {
        // Okay
        }
    }
}
Also used : ListCounterexamplesOptions(com.ibm.watson.developer_cloud.assistant.v1.model.ListCounterexamplesOptions) NotFoundException(com.ibm.watson.developer_cloud.service.exception.NotFoundException) DeleteCounterexampleOptions(com.ibm.watson.developer_cloud.assistant.v1.model.DeleteCounterexampleOptions) Counterexample(com.ibm.watson.developer_cloud.assistant.v1.model.Counterexample) CreateCounterexample(com.ibm.watson.developer_cloud.assistant.v1.model.CreateCounterexample) Date(java.util.Date) UnauthorizedException(com.ibm.watson.developer_cloud.service.exception.UnauthorizedException) NotFoundException(com.ibm.watson.developer_cloud.service.exception.NotFoundException) CounterexampleCollection(com.ibm.watson.developer_cloud.assistant.v1.model.CounterexampleCollection) CreateCounterexampleOptions(com.ibm.watson.developer_cloud.assistant.v1.model.CreateCounterexampleOptions) Test(org.junit.Test)

Example 3 with CounterexampleCollection

use of com.ibm.watson.developer_cloud.assistant.v1.model.CounterexampleCollection in project java-sdk by watson-developer-cloud.

the class Conversation method listCounterexamples.

/**
 * List counterexamples.
 *
 * List the counterexamples for a workspace. Counterexamples are examples that have been marked as irrelevant input.
 *
 * @param listCounterexamplesOptions the {@link ListCounterexamplesOptions} containing the options for the call
 * @return a {@link ServiceCall} with a response type of {@link CounterexampleCollection}
 */
public ServiceCall<CounterexampleCollection> listCounterexamples(ListCounterexamplesOptions listCounterexamplesOptions) {
    Validator.notNull(listCounterexamplesOptions, "listCounterexamplesOptions cannot be null");
    String[] pathSegments = { "v1/workspaces", "counterexamples" };
    String[] pathParameters = { listCounterexamplesOptions.workspaceId() };
    RequestBuilder builder = RequestBuilder.get(RequestBuilder.constructHttpUrl(getEndPoint(), pathSegments, pathParameters));
    builder.query(VERSION, versionDate);
    if (listCounterexamplesOptions.pageLimit() != null) {
        builder.query("page_limit", String.valueOf(listCounterexamplesOptions.pageLimit()));
    }
    if (listCounterexamplesOptions.includeCount() != null) {
        builder.query("include_count", String.valueOf(listCounterexamplesOptions.includeCount()));
    }
    if (listCounterexamplesOptions.sort() != null) {
        builder.query("sort", listCounterexamplesOptions.sort());
    }
    if (listCounterexamplesOptions.cursor() != null) {
        builder.query("cursor", listCounterexamplesOptions.cursor());
    }
    if (listCounterexamplesOptions.includeAudit() != null) {
        builder.query("include_audit", String.valueOf(listCounterexamplesOptions.includeAudit()));
    }
    return createServiceCall(builder.build(), ResponseConverterUtils.getObject(CounterexampleCollection.class));
}
Also used : RequestBuilder(com.ibm.watson.developer_cloud.http.RequestBuilder) CounterexampleCollection(com.ibm.watson.developer_cloud.conversation.v1.model.CounterexampleCollection)

Example 4 with CounterexampleCollection

use of com.ibm.watson.developer_cloud.assistant.v1.model.CounterexampleCollection in project java-sdk by watson-developer-cloud.

the class Assistant method listCounterexamples.

/**
 * List counterexamples.
 *
 * List the counterexamples for a workspace. Counterexamples are examples that have been marked as irrelevant input.
 * This operation is limited to 2500 requests per 30 minutes. For more information, see **Rate limiting**.
 *
 * @param listCounterexamplesOptions the {@link ListCounterexamplesOptions} containing the options for the call
 * @return a {@link ServiceCall} with a response type of {@link CounterexampleCollection}
 */
public ServiceCall<CounterexampleCollection> listCounterexamples(ListCounterexamplesOptions listCounterexamplesOptions) {
    Validator.notNull(listCounterexamplesOptions, "listCounterexamplesOptions cannot be null");
    String[] pathSegments = { "v1/workspaces", "counterexamples" };
    String[] pathParameters = { listCounterexamplesOptions.workspaceId() };
    RequestBuilder builder = RequestBuilder.get(RequestBuilder.constructHttpUrl(getEndPoint(), pathSegments, pathParameters));
    builder.query(VERSION, versionDate);
    if (listCounterexamplesOptions.pageLimit() != null) {
        builder.query("page_limit", String.valueOf(listCounterexamplesOptions.pageLimit()));
    }
    if (listCounterexamplesOptions.includeCount() != null) {
        builder.query("include_count", String.valueOf(listCounterexamplesOptions.includeCount()));
    }
    if (listCounterexamplesOptions.sort() != null) {
        builder.query("sort", listCounterexamplesOptions.sort());
    }
    if (listCounterexamplesOptions.cursor() != null) {
        builder.query("cursor", listCounterexamplesOptions.cursor());
    }
    if (listCounterexamplesOptions.includeAudit() != null) {
        builder.query("include_audit", String.valueOf(listCounterexamplesOptions.includeAudit()));
    }
    return createServiceCall(builder.build(), ResponseConverterUtils.getObject(CounterexampleCollection.class));
}
Also used : RequestBuilder(com.ibm.watson.developer_cloud.http.RequestBuilder) CounterexampleCollection(com.ibm.watson.developer_cloud.assistant.v1.model.CounterexampleCollection)

Example 5 with CounterexampleCollection

use of com.ibm.watson.developer_cloud.assistant.v1.model.CounterexampleCollection in project java-sdk by watson-developer-cloud.

the class ConversationServiceIT method testListCounterexamplesWithPaging.

/**
 * Test listCounterexamples with paging.
 */
@Test
public void testListCounterexamplesWithPaging() {
    // gotta be unique
    String counterExampleText1 = "alpha" + UUID.randomUUID().toString();
    // gotta be unique
    String counterExampleText2 = "zeta" + UUID.randomUUID().toString();
    // Add two counterexamples
    CreateCounterexampleOptions createOptions = new CreateCounterexampleOptions.Builder(workspaceId, counterExampleText1).build();
    service.createCounterexample(createOptions).execute();
    service.createCounterexample(createOptions.newBuilder().text(counterExampleText2).build()).execute();
    try {
        ListCounterexamplesOptions listOptions = new ListCounterexamplesOptions.Builder(workspaceId).pageLimit(1L).sort("text").build();
        CounterexampleCollection response = service.listCounterexamples(listOptions).execute();
        assertNotNull(response);
        assertNotNull(response.getPagination());
        assertNotNull(response.getPagination().getRefreshUrl());
        assertNotNull(response.getPagination().getNextUrl());
        assertNotNull(response.getPagination().getCursor());
        boolean found1 = false, found2 = false;
        while (true) {
            assertNotNull(response.getCounterexamples());
            assertTrue(response.getCounterexamples().size() == 1);
            found1 |= response.getCounterexamples().get(0).getText().equals(counterExampleText1);
            found2 |= response.getCounterexamples().get(0).getText().equals(counterExampleText2);
            // verify sort
            assertTrue(found1 || !found2);
            if (response.getPagination().getCursor() == null) {
                break;
            }
            String cursor = response.getPagination().getCursor();
            response = service.listCounterexamples(listOptions.newBuilder().cursor(cursor).build()).execute();
        }
        assertTrue(found1 && found2);
    } catch (Exception ex) {
        fail(ex.getMessage());
    } finally {
        // Clean up
        DeleteCounterexampleOptions deleteOptions = new DeleteCounterexampleOptions.Builder(workspaceId, counterExampleText1).build();
        service.deleteCounterexample(deleteOptions).execute();
        service.deleteCounterexample(deleteOptions.newBuilder().text(counterExampleText2).build()).execute();
    }
}
Also used : ListCounterexamplesOptions(com.ibm.watson.developer_cloud.conversation.v1.model.ListCounterexamplesOptions) CreateCounterexampleOptions(com.ibm.watson.developer_cloud.conversation.v1.model.CreateCounterexampleOptions) DeleteCounterexampleOptions(com.ibm.watson.developer_cloud.conversation.v1.model.DeleteCounterexampleOptions) UnauthorizedException(com.ibm.watson.developer_cloud.service.exception.UnauthorizedException) NotFoundException(com.ibm.watson.developer_cloud.service.exception.NotFoundException) CounterexampleCollection(com.ibm.watson.developer_cloud.conversation.v1.model.CounterexampleCollection) Test(org.junit.Test)

Aggregations

NotFoundException (com.ibm.watson.developer_cloud.service.exception.NotFoundException)4 UnauthorizedException (com.ibm.watson.developer_cloud.service.exception.UnauthorizedException)4 Test (org.junit.Test)4 CounterexampleCollection (com.ibm.watson.developer_cloud.assistant.v1.model.CounterexampleCollection)3 CounterexampleCollection (com.ibm.watson.developer_cloud.conversation.v1.model.CounterexampleCollection)3 CreateCounterexampleOptions (com.ibm.watson.developer_cloud.assistant.v1.model.CreateCounterexampleOptions)2 DeleteCounterexampleOptions (com.ibm.watson.developer_cloud.assistant.v1.model.DeleteCounterexampleOptions)2 ListCounterexamplesOptions (com.ibm.watson.developer_cloud.assistant.v1.model.ListCounterexamplesOptions)2 CreateCounterexampleOptions (com.ibm.watson.developer_cloud.conversation.v1.model.CreateCounterexampleOptions)2 DeleteCounterexampleOptions (com.ibm.watson.developer_cloud.conversation.v1.model.DeleteCounterexampleOptions)2 ListCounterexamplesOptions (com.ibm.watson.developer_cloud.conversation.v1.model.ListCounterexamplesOptions)2 RequestBuilder (com.ibm.watson.developer_cloud.http.RequestBuilder)2 Date (java.util.Date)2 Counterexample (com.ibm.watson.developer_cloud.assistant.v1.model.Counterexample)1 CreateCounterexample (com.ibm.watson.developer_cloud.assistant.v1.model.CreateCounterexample)1 Counterexample (com.ibm.watson.developer_cloud.conversation.v1.model.Counterexample)1 CreateCounterexample (com.ibm.watson.developer_cloud.conversation.v1.model.CreateCounterexample)1