Search in sources :

Example 6 with Pagination

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

the class ConversationServiceIT method testListDialogNodesWithPaging.

/**
 * Test listDialogNodes with pagination.
 */
@Test
public void testListDialogNodesWithPaging() {
    String dialogNodeName1 = "First" + UUID.randomUUID().toString();
    String dialogNodeName2 = "Second" + UUID.randomUUID().toString();
    CreateDialogNodeOptions createOptions = new CreateDialogNodeOptions.Builder(workspaceId, dialogNodeName1).build();
    service.createDialogNode(createOptions).execute();
    service.createDialogNode(createOptions.newBuilder().dialogNode(dialogNodeName2).build()).execute();
    try {
        ListDialogNodesOptions listOptions = new ListDialogNodesOptions.Builder().workspaceId(workspaceId).pageLimit(1L).sort("dialog_node").build();
        DialogNodeCollection response = service.listDialogNodes(listOptions).execute();
        assertNotNull(response);
        assertNotNull(response.getDialogNodes());
        assertNotNull(response.getPagination());
        assertNotNull(response.getPagination().getRefreshUrl());
        assertNotNull(response.getPagination().getNextUrl());
        assertNotNull(response.getPagination().getCursor());
        boolean found1 = false, found2 = false;
        while (true) {
            assertNotNull(response.getDialogNodes());
            assertTrue(response.getDialogNodes().size() == 1);
            found1 |= response.getDialogNodes().get(0).getDialogNodeId().equals(dialogNodeName1);
            found2 |= response.getDialogNodes().get(0).getDialogNodeId().equals(dialogNodeName2);
            // verify sort
            assertTrue(found1 || !found2);
            if (response.getPagination().getCursor() == null) {
                break;
            }
            String cursor = response.getPagination().getCursor();
            response = service.listDialogNodes(listOptions.newBuilder().cursor(cursor).build()).execute();
        }
        assertTrue(found1 && found2);
    } catch (Exception ex) {
        fail(ex.getMessage());
    } finally {
        // Clean up
        DeleteDialogNodeOptions deleteOptions = new DeleteDialogNodeOptions.Builder(workspaceId, dialogNodeName1).build();
        service.deleteDialogNode(deleteOptions).execute();
        service.deleteDialogNode(deleteOptions.newBuilder().dialogNode(dialogNodeName2).build()).execute();
    }
}
Also used : DeleteDialogNodeOptions(com.ibm.watson.developer_cloud.conversation.v1.model.DeleteDialogNodeOptions) CreateDialogNodeOptions(com.ibm.watson.developer_cloud.conversation.v1.model.CreateDialogNodeOptions) ListDialogNodesOptions(com.ibm.watson.developer_cloud.conversation.v1.model.ListDialogNodesOptions) DialogNodeCollection(com.ibm.watson.developer_cloud.conversation.v1.model.DialogNodeCollection) UnauthorizedException(com.ibm.watson.developer_cloud.service.exception.UnauthorizedException) NotFoundException(com.ibm.watson.developer_cloud.service.exception.NotFoundException) Test(org.junit.Test)

Example 7 with Pagination

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

the class PaginationTypeAdapter method read.

/*
   * (non-Javadoc)
   * @see com.google.gson.TypeAdapter#read(com.google.gson.stream.JsonReader)
   */
@Override
public Pagination read(JsonReader reader) throws IOException {
    if (reader.peek() == JsonToken.NULL) {
        reader.nextNull();
        return null;
    }
    reader.beginObject();
    Pagination pagination = new Pagination();
    while (reader.hasNext()) {
        String name = reader.nextName();
        if (name.equals(REFRESH_URL)) {
            pagination.setRefreshUrl(reader.nextString());
        } else if (name.equals(NEXT_URL)) {
            String nextUrl = reader.nextString();
            HttpUrl url = HttpUrl.parse(DEFAULT_ENDPOINT + nextUrl);
            pagination.setCursor(url.queryParameter(CURSOR));
            pagination.setNextUrl(nextUrl);
        } else if (name.equals(TOTAL)) {
            pagination.setTotal(reader.nextLong());
        } else if (name.equals(MATCHED)) {
            pagination.setMatched(reader.nextLong());
        } else {
            reader.skipValue();
        }
    }
    reader.endObject();
    return pagination;
}
Also used : Pagination(com.ibm.watson.developer_cloud.assistant.v1.model.Pagination) HttpUrl(okhttp3.HttpUrl)

Example 8 with Pagination

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

the class ConversationServiceIT method testListLogsWithPaging.

/**
 * Test listLogs with pagination.
 */
@Test
@Ignore("To be run locally until we fix the Rate limitation issue")
public void testListLogsWithPaging() {
    try {
        ListLogsOptions.Builder listOptionsBuilder = new ListLogsOptions.Builder(workspaceId);
        listOptionsBuilder.sort("-request_timestamp");
        listOptionsBuilder.filter("request.intents:intent:off_topic");
        listOptionsBuilder.pageLimit(1L);
        LogCollection response = service.listLogs(listOptionsBuilder.build()).execute();
        assertNotNull(response);
        assertNotNull(response.getLogs());
        assertNotNull(response.getPagination());
        // Empirically -- no refresh_url in pagination of listLogs
        // assertNotNull(response.getPagination().getRefreshUrl());
        assertNotNull(response.getPagination().getNextUrl());
        assertNotNull(response.getPagination().getCursor());
        assertTrue(response.getLogs().size() == 1);
        LogExport logEntry1 = response.getLogs().get(0);
        String cursor = response.getPagination().getCursor();
        response = service.listLogs(listOptionsBuilder.cursor(cursor).build()).execute();
        assertNotNull(response.getLogs());
        assertTrue(response.getLogs().size() == 1);
        LogExport logEntry2 = response.getLogs().get(0);
        Date requestDate1 = isoDateFormat.parse(logEntry1.getRequestTimestamp());
        Date requestDate2 = isoDateFormat.parse(logEntry2.getRequestTimestamp());
        assertTrue(requestDate2.before(requestDate1));
    } catch (Exception ex) {
        fail(ex.getMessage());
    }
}
Also used : ListLogsOptions(com.ibm.watson.developer_cloud.conversation.v1.model.ListLogsOptions) LogCollection(com.ibm.watson.developer_cloud.conversation.v1.model.LogCollection) LogExport(com.ibm.watson.developer_cloud.conversation.v1.model.LogExport) Date(java.util.Date) UnauthorizedException(com.ibm.watson.developer_cloud.service.exception.UnauthorizedException) NotFoundException(com.ibm.watson.developer_cloud.service.exception.NotFoundException) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 9 with Pagination

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

the class SynonymsIT method testListSynonymsWithPaging.

/**
 * Test listSynonyms with pagination.
 */
@Test
public void testListSynonymsWithPaging() {
    String entity = "beverage";
    String entityValue = "coffee";
    String synonym1 = "java";
    String synonym2 = "joe";
    try {
        CreateEntityOptions createOptions = new CreateEntityOptions.Builder(workspaceId, entity).build();
        service.createEntity(createOptions).execute();
    } catch (Exception ex) {
        // Exception is okay if is for Unique Violation
        assertTrue(ex.getLocalizedMessage().startsWith("Unique Violation"));
    }
    try {
        CreateValueOptions createOptions = new CreateValueOptions.Builder(workspaceId, entity, entityValue).build();
        service.createValue(createOptions).execute();
    } catch (Exception ex) {
        // Exception is okay if is for Unique Violation
        assertTrue(ex.getLocalizedMessage().startsWith("Unique Violation"));
    }
    CreateSynonymOptions createOptions = new CreateSynonymOptions.Builder(workspaceId, entity, entityValue, synonym1).build();
    try {
        service.createSynonym(createOptions).execute();
    } catch (Exception ex) {
        // Exception is okay if is for Unique Violation
        assertTrue(ex.getLocalizedMessage().startsWith("Unique Violation"));
    }
    try {
        service.createSynonym(createOptions.newBuilder().synonym(synonym2).build()).execute();
    } catch (Exception ex) {
        // Exception is okay if is for Unique Violation
        assertTrue(ex.getLocalizedMessage().startsWith("Unique Violation"));
    }
    try {
        ListSynonymsOptions.Builder listOptionsBuilder = new ListSynonymsOptions.Builder(workspaceId, entity, entityValue);
        listOptionsBuilder.sort("modified");
        listOptionsBuilder.pageLimit(1L);
        SynonymCollection response = service.listSynonyms(listOptionsBuilder.build()).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.getSynonyms());
            assertTrue(response.getSynonyms().size() == 1);
            found1 |= response.getSynonyms().get(0).getSynonymText().equals(synonym1);
            found2 |= response.getSynonyms().get(0).getSynonymText().equals(synonym2);
            if (response.getPagination().getCursor() == null) {
                break;
            }
            String cursor = response.getPagination().getCursor();
            response = service.listSynonyms(listOptionsBuilder.cursor(cursor).build()).execute();
        }
        assertTrue(found1 && found2);
    } catch (Exception ex) {
        fail(ex.getMessage());
    } finally {
        DeleteSynonymOptions deleteOptions = new DeleteSynonymOptions.Builder(workspaceId, entity, entityValue, synonym1).build();
        service.deleteSynonym(deleteOptions).execute();
        service.deleteSynonym(deleteOptions.newBuilder().synonym(synonym2).build()).execute();
    }
}
Also used : DeleteSynonymOptions(com.ibm.watson.developer_cloud.conversation.v1.model.DeleteSynonymOptions) CreateEntityOptions(com.ibm.watson.developer_cloud.conversation.v1.model.CreateEntityOptions) CreateValueOptions(com.ibm.watson.developer_cloud.conversation.v1.model.CreateValueOptions) ListSynonymsOptions(com.ibm.watson.developer_cloud.conversation.v1.model.ListSynonymsOptions) NotFoundException(com.ibm.watson.developer_cloud.service.exception.NotFoundException) CreateSynonymOptions(com.ibm.watson.developer_cloud.conversation.v1.model.CreateSynonymOptions) SynonymCollection(com.ibm.watson.developer_cloud.conversation.v1.model.SynonymCollection) Test(org.junit.Test)

Example 10 with Pagination

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

the class PaginationTypeAdapterTest method testWriteTypeAdapter.

/**
 * Test write type adapter.
 *
 * @throws FileNotFoundException the file not found exception
 */
@Test
public void testWriteTypeAdapter() throws FileNotFoundException {
    Pagination pagination = loadFixture(FIXTURE, Pagination.class);
    assertNotNull(GsonSingleton.getGson().toJson(pagination), pagination.toString());
}
Also used : Pagination(com.ibm.watson.developer_cloud.conversation.v1.model.Pagination) WatsonServiceUnitTest(com.ibm.watson.developer_cloud.WatsonServiceUnitTest) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)9 NotFoundException (com.ibm.watson.developer_cloud.service.exception.NotFoundException)5 WatsonServiceUnitTest (com.ibm.watson.developer_cloud.WatsonServiceUnitTest)4 Pagination (com.ibm.watson.developer_cloud.assistant.v1.model.Pagination)3 CreateEntityOptions (com.ibm.watson.developer_cloud.conversation.v1.model.CreateEntityOptions)3 Pagination (com.ibm.watson.developer_cloud.conversation.v1.model.Pagination)3 HttpUrl (okhttp3.HttpUrl)3 CreateValueOptions (com.ibm.watson.developer_cloud.conversation.v1.model.CreateValueOptions)2 UnauthorizedException (com.ibm.watson.developer_cloud.service.exception.UnauthorizedException)2 Ignore (org.junit.Ignore)2 CreateDialogNodeOptions (com.ibm.watson.developer_cloud.conversation.v1.model.CreateDialogNodeOptions)1 CreateSynonymOptions (com.ibm.watson.developer_cloud.conversation.v1.model.CreateSynonymOptions)1 DeleteDialogNodeOptions (com.ibm.watson.developer_cloud.conversation.v1.model.DeleteDialogNodeOptions)1 DeleteEntityOptions (com.ibm.watson.developer_cloud.conversation.v1.model.DeleteEntityOptions)1 DeleteSynonymOptions (com.ibm.watson.developer_cloud.conversation.v1.model.DeleteSynonymOptions)1 DeleteValueOptions (com.ibm.watson.developer_cloud.conversation.v1.model.DeleteValueOptions)1 DialogNodeCollection (com.ibm.watson.developer_cloud.conversation.v1.model.DialogNodeCollection)1 EntityCollection (com.ibm.watson.developer_cloud.conversation.v1.model.EntityCollection)1 EntityExport (com.ibm.watson.developer_cloud.conversation.v1.model.EntityExport)1 ListDialogNodesOptions (com.ibm.watson.developer_cloud.conversation.v1.model.ListDialogNodesOptions)1