Search in sources :

Example 1 with Pagination

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

the class EntitiesIT method testListEntitiesWithPaging.

/**
 * Test listEntities with pagination.
 */
@Test
@Ignore("To be run locally until we fix the Rate limitation issue")
public void testListEntitiesWithPaging() {
    // gotta be unique
    String entity1 = "Hello" + UUID.randomUUID().toString();
    // gotta be unique
    String entity2 = "Goodbye" + UUID.randomUUID().toString();
    CreateEntityOptions createOptions = new CreateEntityOptions.Builder(workspaceId, entity1).build();
    service.createEntity(createOptions).execute();
    service.createEntity(createOptions.newBuilder().entity(entity2).build()).execute();
    try {
        ListEntitiesOptions listOptions = new ListEntitiesOptions.Builder(workspaceId).sort("entity").pageLimit(1L).build();
        EntityCollection response = service.listEntities(listOptions).execute();
        assertNotNull(response);
        assertNotNull(response.getEntities());
        assertNotNull(response.getPagination());
        assertNotNull(response.getPagination().getRefreshUrl());
        assertNotNull(response.getPagination().getNextUrl());
        assertNotNull(response.getPagination().getCursor());
        assertTrue(!response.getEntities().get(0).getEntityName().equals(entity1));
        EntityExport ieResponse = null;
        while (response.getPagination().getCursor() != null) {
            assertNotNull(response.getEntities());
            assertTrue(response.getEntities().size() == 1);
            if (response.getEntities().get(0).getEntityName().equals(entity1)) {
                ieResponse = response.getEntities().get(0);
                break;
            }
            String cursor = response.getPagination().getCursor();
            response = service.listEntities(listOptions.newBuilder().cursor(cursor).build()).execute();
        }
        assertNotNull(ieResponse);
    } catch (Exception ex) {
        fail(ex.getMessage());
    } finally {
        // Clean up
        DeleteEntityOptions deleteOptions = new DeleteEntityOptions.Builder(workspaceId, entity1).build();
        service.deleteEntity(deleteOptions).execute();
        service.deleteEntity(deleteOptions.newBuilder().entity(entity2).build()).execute();
    }
}
Also used : EntityExport(com.ibm.watson.developer_cloud.conversation.v1.model.EntityExport) CreateEntityOptions(com.ibm.watson.developer_cloud.conversation.v1.model.CreateEntityOptions) EntityCollection(com.ibm.watson.developer_cloud.conversation.v1.model.EntityCollection) DeleteEntityOptions(com.ibm.watson.developer_cloud.conversation.v1.model.DeleteEntityOptions) ListEntitiesOptions(com.ibm.watson.developer_cloud.conversation.v1.model.ListEntitiesOptions) NotFoundException(com.ibm.watson.developer_cloud.service.exception.NotFoundException) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 2 with Pagination

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

the class ValuesIT method testListValuesWithPaging.

/**
 * Test listValues with pagination.
 */
@Test
public void testListValuesWithPaging() {
    String entity = "beverage";
    String entityValue1 = "coffee";
    String entityValue2 = "orange juice";
    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"));
    }
    CreateValueOptions createOptions = new CreateValueOptions.Builder(workspaceId, entity, entityValue1).build();
    try {
        service.createValue(createOptions).execute();
    } catch (Exception ex) {
        // Exception is okay if is for Unique Violation
        assertTrue(ex.getLocalizedMessage().startsWith("Unique Violation"));
    }
    try {
        service.createValue(createOptions.newBuilder().value(entityValue2).build()).execute();
    } catch (Exception ex) {
        // Exception is okay if is for Unique Violation
        assertTrue(ex.getLocalizedMessage().startsWith("Unique Violation"));
    }
    try {
        ListValuesOptions.Builder listOptionsBuilder = new ListValuesOptions.Builder(workspaceId, entity);
        listOptionsBuilder.sort("updated");
        listOptionsBuilder.pageLimit(1L);
        listOptionsBuilder.export(true);
        ValueCollection response = service.listValues(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.getValues());
            assertTrue(response.getValues().size() == 1);
            found1 |= response.getValues().get(0).getValueText().equals(entityValue1);
            found2 |= response.getValues().get(0).getValueText().equals(entityValue2);
            if (response.getPagination().getCursor() == null) {
                break;
            }
            String cursor = response.getPagination().getCursor();
            response = service.listValues(listOptionsBuilder.cursor(cursor).build()).execute();
        }
        assertTrue(found1 && found2);
    } catch (Exception ex) {
        fail(ex.getMessage());
    } finally {
        DeleteValueOptions deleteOptions = new DeleteValueOptions.Builder(workspaceId, entity, entityValue1).build();
        service.deleteValue(deleteOptions).execute();
        service.deleteValue(deleteOptions.newBuilder().value(entityValue2).build()).execute();
    }
}
Also used : CreateEntityOptions(com.ibm.watson.developer_cloud.conversation.v1.model.CreateEntityOptions) DeleteValueOptions(com.ibm.watson.developer_cloud.conversation.v1.model.DeleteValueOptions) ListValuesOptions(com.ibm.watson.developer_cloud.conversation.v1.model.ListValuesOptions) CreateValueOptions(com.ibm.watson.developer_cloud.conversation.v1.model.CreateValueOptions) NotFoundException(com.ibm.watson.developer_cloud.service.exception.NotFoundException) ValueCollection(com.ibm.watson.developer_cloud.conversation.v1.model.ValueCollection) Test(org.junit.Test)

Example 3 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 testParseTypeAdapter.

/**
 * Test parse type adapter.
 *
 * @throws FileNotFoundException the file not found exception
 */
@Test
public void testParseTypeAdapter() throws FileNotFoundException {
    Pagination pagination = loadFixture(FIXTURE, Pagination.class);
    assertEquals(pagination.getCursor(), "batman");
}
Also used : Pagination(com.ibm.watson.developer_cloud.conversation.v1.model.Pagination) WatsonServiceUnitTest(com.ibm.watson.developer_cloud.WatsonServiceUnitTest) Test(org.junit.Test)

Example 4 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.assistant.v1.model.Pagination) WatsonServiceUnitTest(com.ibm.watson.developer_cloud.WatsonServiceUnitTest) Test(org.junit.Test)

Example 5 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.conversation.v1.model.Pagination) HttpUrl(okhttp3.HttpUrl)

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