Search in sources :

Example 1 with ListValuesOptions

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

the class ValuesIT method testListValues.

/**
 * Test listValues.
 */
@Test
public void testListValues() {
    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 listOptions = new ListValuesOptions.Builder().workspaceId(workspaceId).entity(entity).build();
        final ValueCollection response = service.listValues(listOptions).execute();
        assertNotNull(response);
        assertNotNull(response.getValues());
        assertNotNull(response.getPagination());
        assertNotNull(response.getPagination().getRefreshUrl());
        // nextUrl may be null
        if (response.getPagination().getNextUrl() == null) {
            assertNull(response.getPagination().getCursor());
        } else {
            assertNotNull(response.getPagination().getCursor());
        }
        assertTrue(response.getValues().size() >= 2);
        // Should not be paginated, but just to be sure
        if (response.getPagination().getNextUrl() == null) {
            // assertTrue(response.getValues().stream().filter(r -> r.getValue().equals(synonym1)).count() == 1);
            boolean found1 = false, found2 = false;
            for (ValueExport valueResponse : response.getValues()) {
                found1 |= valueResponse.getValueText().equals(entityValue1);
                found2 |= valueResponse.getValueText().equals(entityValue2);
            }
            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) ValueExport(com.ibm.watson.developer_cloud.conversation.v1.model.ValueExport) ValueCollection(com.ibm.watson.developer_cloud.conversation.v1.model.ValueCollection) Test(org.junit.Test)

Example 2 with ListValuesOptions

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

the class ValuesIT method testListValues.

/**
 * Test listValues.
 */
@Test
public void testListValues() {
    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 listOptions = new ListValuesOptions.Builder().workspaceId(workspaceId).entity(entity).build();
        final ValueCollection response = service.listValues(listOptions).execute();
        assertNotNull(response);
        assertNotNull(response.getValues());
        assertNotNull(response.getPagination());
        assertNotNull(response.getPagination().getRefreshUrl());
        // nextUrl may be null
        if (response.getPagination().getNextUrl() == null) {
            assertNull(response.getPagination().getCursor());
        } else {
            assertNotNull(response.getPagination().getCursor());
        }
        assertTrue(response.getValues().size() >= 2);
        // Should not be paginated, but just to be sure
        if (response.getPagination().getNextUrl() == null) {
            // assertTrue(response.getValues().stream().filter(r -> r.getValue().equals(synonym1)).count() == 1);
            boolean found1 = false, found2 = false;
            for (ValueExport valueResponse : response.getValues()) {
                found1 |= valueResponse.getValueText().equals(entityValue1);
                found2 |= valueResponse.getValueText().equals(entityValue2);
            }
            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.assistant.v1.model.CreateEntityOptions) DeleteValueOptions(com.ibm.watson.developer_cloud.assistant.v1.model.DeleteValueOptions) ListValuesOptions(com.ibm.watson.developer_cloud.assistant.v1.model.ListValuesOptions) CreateValueOptions(com.ibm.watson.developer_cloud.assistant.v1.model.CreateValueOptions) NotFoundException(com.ibm.watson.developer_cloud.service.exception.NotFoundException) ValueExport(com.ibm.watson.developer_cloud.assistant.v1.model.ValueExport) ValueCollection(com.ibm.watson.developer_cloud.assistant.v1.model.ValueCollection) Test(org.junit.Test)

Example 3 with ListValuesOptions

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

the class Assistant method listValues.

/**
 * List entity values.
 *
 * List the values for an entity. This operation is limited to 2500 requests per 30 minutes. For more information, see
 * **Rate limiting**.
 *
 * @param listValuesOptions the {@link ListValuesOptions} containing the options for the call
 * @return a {@link ServiceCall} with a response type of {@link ValueCollection}
 */
public ServiceCall<ValueCollection> listValues(ListValuesOptions listValuesOptions) {
    Validator.notNull(listValuesOptions, "listValuesOptions cannot be null");
    String[] pathSegments = { "v1/workspaces", "entities", "values" };
    String[] pathParameters = { listValuesOptions.workspaceId(), listValuesOptions.entity() };
    RequestBuilder builder = RequestBuilder.get(RequestBuilder.constructHttpUrl(getEndPoint(), pathSegments, pathParameters));
    builder.query(VERSION, versionDate);
    if (listValuesOptions.export() != null) {
        builder.query("export", String.valueOf(listValuesOptions.export()));
    }
    if (listValuesOptions.pageLimit() != null) {
        builder.query("page_limit", String.valueOf(listValuesOptions.pageLimit()));
    }
    if (listValuesOptions.includeCount() != null) {
        builder.query("include_count", String.valueOf(listValuesOptions.includeCount()));
    }
    if (listValuesOptions.sort() != null) {
        builder.query("sort", listValuesOptions.sort());
    }
    if (listValuesOptions.cursor() != null) {
        builder.query("cursor", listValuesOptions.cursor());
    }
    if (listValuesOptions.includeAudit() != null) {
        builder.query("include_audit", String.valueOf(listValuesOptions.includeAudit()));
    }
    return createServiceCall(builder.build(), ResponseConverterUtils.getObject(ValueCollection.class));
}
Also used : RequestBuilder(com.ibm.watson.developer_cloud.http.RequestBuilder) ValueCollection(com.ibm.watson.developer_cloud.assistant.v1.model.ValueCollection)

Aggregations

ValueCollection (com.ibm.watson.developer_cloud.assistant.v1.model.ValueCollection)2 NotFoundException (com.ibm.watson.developer_cloud.service.exception.NotFoundException)2 Test (org.junit.Test)2 CreateEntityOptions (com.ibm.watson.developer_cloud.assistant.v1.model.CreateEntityOptions)1 CreateValueOptions (com.ibm.watson.developer_cloud.assistant.v1.model.CreateValueOptions)1 DeleteValueOptions (com.ibm.watson.developer_cloud.assistant.v1.model.DeleteValueOptions)1 ListValuesOptions (com.ibm.watson.developer_cloud.assistant.v1.model.ListValuesOptions)1 ValueExport (com.ibm.watson.developer_cloud.assistant.v1.model.ValueExport)1 CreateEntityOptions (com.ibm.watson.developer_cloud.conversation.v1.model.CreateEntityOptions)1 CreateValueOptions (com.ibm.watson.developer_cloud.conversation.v1.model.CreateValueOptions)1 DeleteValueOptions (com.ibm.watson.developer_cloud.conversation.v1.model.DeleteValueOptions)1 ListValuesOptions (com.ibm.watson.developer_cloud.conversation.v1.model.ListValuesOptions)1 ValueCollection (com.ibm.watson.developer_cloud.conversation.v1.model.ValueCollection)1 ValueExport (com.ibm.watson.developer_cloud.conversation.v1.model.ValueExport)1 RequestBuilder (com.ibm.watson.developer_cloud.http.RequestBuilder)1