Search in sources :

Example 1 with ValueCollection

use of com.ibm.watson.assistant.v1.model.ValueCollection 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 2 with ValueCollection

use of com.ibm.watson.assistant.v1.model.ValueCollection 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.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) ValueCollection(com.ibm.watson.developer_cloud.assistant.v1.model.ValueCollection) Test(org.junit.Test)

Example 3 with ValueCollection

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

the class Assistant method listValues.

/**
 * List entity values.
 *
 * <p>List the values for an entity.
 *
 * @param listValuesOptions the {@link ListValuesOptions} containing the options for the call
 * @return a {@link ServiceCall} with a result of type {@link ValueCollection}
 */
public ServiceCall<ValueCollection> listValues(ListValuesOptions listValuesOptions) {
    com.ibm.cloud.sdk.core.util.Validator.notNull(listValuesOptions, "listValuesOptions cannot be null");
    Map<String, String> pathParamsMap = new HashMap<String, String>();
    pathParamsMap.put("workspace_id", listValuesOptions.workspaceId());
    pathParamsMap.put("entity", listValuesOptions.entity());
    RequestBuilder builder = RequestBuilder.get(RequestBuilder.resolveRequestUrl(getServiceUrl(), "/v1/workspaces/{workspace_id}/entities/{entity}/values", pathParamsMap));
    Map<String, String> sdkHeaders = SdkCommon.getSdkHeaders("conversation", "v1", "listValues");
    for (Entry<String, String> header : sdkHeaders.entrySet()) {
        builder.header(header.getKey(), header.getValue());
    }
    builder.header("Accept", "application/json");
    builder.query("version", String.valueOf(this.version));
    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", String.valueOf(listValuesOptions.sort()));
    }
    if (listValuesOptions.cursor() != null) {
        builder.query("cursor", String.valueOf(listValuesOptions.cursor()));
    }
    if (listValuesOptions.includeAudit() != null) {
        builder.query("include_audit", String.valueOf(listValuesOptions.includeAudit()));
    }
    ResponseConverter<ValueCollection> responseConverter = ResponseConverterUtils.getValue(new com.google.gson.reflect.TypeToken<ValueCollection>() {
    }.getType());
    return createServiceCall(builder.build(), responseConverter);
}
Also used : RequestBuilder(com.ibm.cloud.sdk.core.http.RequestBuilder) HashMap(java.util.HashMap) ValueCollection(com.ibm.watson.assistant.v1.model.ValueCollection)

Example 4 with ValueCollection

use of com.ibm.watson.assistant.v1.model.ValueCollection 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 5 with ValueCollection

use of com.ibm.watson.assistant.v1.model.ValueCollection 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)

Aggregations

Test (org.junit.Test)6 ValueCollection (com.ibm.watson.assistant.v1.model.ValueCollection)4 NotFoundException (com.ibm.watson.developer_cloud.service.exception.NotFoundException)4 ListValuesOptions (com.ibm.watson.assistant.v1.model.ListValuesOptions)3 ValueCollection (com.ibm.watson.developer_cloud.assistant.v1.model.ValueCollection)3 ValueCollection (com.ibm.watson.developer_cloud.conversation.v1.model.ValueCollection)3 NotFoundException (com.ibm.cloud.sdk.core.service.exception.NotFoundException)2 CreateEntityOptions (com.ibm.watson.assistant.v1.model.CreateEntityOptions)2 CreateValueOptions (com.ibm.watson.assistant.v1.model.CreateValueOptions)2 DeleteValueOptions (com.ibm.watson.assistant.v1.model.DeleteValueOptions)2 CreateEntityOptions (com.ibm.watson.developer_cloud.assistant.v1.model.CreateEntityOptions)2 CreateValueOptions (com.ibm.watson.developer_cloud.assistant.v1.model.CreateValueOptions)2 DeleteValueOptions (com.ibm.watson.developer_cloud.assistant.v1.model.DeleteValueOptions)2 ListValuesOptions (com.ibm.watson.developer_cloud.assistant.v1.model.ListValuesOptions)2 CreateEntityOptions (com.ibm.watson.developer_cloud.conversation.v1.model.CreateEntityOptions)2 CreateValueOptions (com.ibm.watson.developer_cloud.conversation.v1.model.CreateValueOptions)2 DeleteValueOptions (com.ibm.watson.developer_cloud.conversation.v1.model.DeleteValueOptions)2 ListValuesOptions (com.ibm.watson.developer_cloud.conversation.v1.model.ListValuesOptions)2 RequestBuilder (com.ibm.watson.developer_cloud.http.RequestBuilder)2 RequestBuilder (com.ibm.cloud.sdk.core.http.RequestBuilder)1