Search in sources :

Example 6 with ValueCollection

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

the class Conversation method listValues.

/**
 * List entity values.
 *
 * List the values for an entity.
 *
 * @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.conversation.v1.model.ValueCollection)

Example 7 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.
 *
 * 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)

Example 8 with ValueCollection

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

the class AssistantTest method testListValuesWOptions.

// Test the listValues operation with a valid options model parameter
@Test
public void testListValuesWOptions() throws Throwable {
    // Register a mock response
    String mockResponseBody = "{\"values\": [{\"value\": \"value\", \"metadata\": {\"mapKey\": \"anyValue\"}, \"type\": \"synonyms\", \"synonyms\": [\"synonym\"], \"patterns\": [\"pattern\"], \"created\": \"2019-01-01T12:00:00.000Z\", \"updated\": \"2019-01-01T12:00:00.000Z\"}], \"pagination\": {\"refresh_url\": \"refreshUrl\", \"next_url\": \"nextUrl\", \"total\": 5, \"matched\": 7, \"refresh_cursor\": \"refreshCursor\", \"next_cursor\": \"nextCursor\"}}";
    String listValuesPath = "/v1/workspaces/testString/entities/testString/values";
    server.enqueue(new MockResponse().setHeader("Content-type", "application/json").setResponseCode(200).setBody(mockResponseBody));
    // Construct an instance of the ListValuesOptions model
    ListValuesOptions listValuesOptionsModel = new ListValuesOptions.Builder().workspaceId("testString").entity("testString").export(false).pageLimit(Long.valueOf("26")).includeCount(false).sort("value").cursor("testString").includeAudit(false).build();
    // Invoke listValues() with a valid options model and verify the result
    Response<ValueCollection> response = assistantService.listValues(listValuesOptionsModel).execute();
    assertNotNull(response);
    ValueCollection responseObj = response.getResult();
    assertNotNull(responseObj);
    // Verify the contents of the request sent to the mock server
    RecordedRequest request = server.takeRequest();
    assertNotNull(request);
    assertEquals(request.getMethod(), "GET");
    // Verify request path
    String parsedPath = TestUtilities.parseReqPath(request);
    assertEquals(parsedPath, listValuesPath);
    // Verify query params
    Map<String, String> query = TestUtilities.parseQueryString(request);
    assertNotNull(query);
    assertEquals(query.get("version"), "testString");
    assertEquals(Boolean.valueOf(query.get("export")), Boolean.valueOf(false));
    assertEquals(Long.valueOf(query.get("page_limit")), Long.valueOf("26"));
    assertEquals(Boolean.valueOf(query.get("include_count")), Boolean.valueOf(false));
    assertEquals(query.get("sort"), "value");
    assertEquals(query.get("cursor"), "testString");
    assertEquals(Boolean.valueOf(query.get("include_audit")), Boolean.valueOf(false));
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) ListValuesOptions(com.ibm.watson.assistant.v1.model.ListValuesOptions) ValueCollection(com.ibm.watson.assistant.v1.model.ValueCollection) Test(org.testng.annotations.Test)

Example 9 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().getResult();
    } 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().getResult();
    } 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().getResult();
    } 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().getResult();
        assertNotNull(response);
        assertNotNull(response.getPagination());
        assertNotNull(response.getPagination().getRefreshUrl());
        assertNotNull(response.getPagination().getNextUrl());
        assertNotNull(response.getPagination().getNextCursor());
        boolean found1 = false;
        boolean found2 = false;
        while (true) {
            assertNotNull(response.getValues());
            assertTrue(response.getValues().size() == 1);
            found1 |= response.getValues().get(0).value().equals(entityValue1);
            found2 |= response.getValues().get(0).value().equals(entityValue2);
            if (response.getPagination().getNextCursor() == null) {
                break;
            }
            String cursor = response.getPagination().getNextCursor();
            response = service.listValues(listOptionsBuilder.cursor(cursor).build()).execute().getResult();
        }
        assertTrue(found1 && found2);
    } catch (Exception ex) {
        fail(ex.getMessage());
    } finally {
        DeleteValueOptions deleteOptions = new DeleteValueOptions.Builder(workspaceId, entity, entityValue1).build();
        service.deleteValue(deleteOptions).execute().getResult();
        service.deleteValue(deleteOptions.newBuilder().value(entityValue2).build()).execute().getResult();
    }
}
Also used : CreateEntityOptions(com.ibm.watson.assistant.v1.model.CreateEntityOptions) DeleteValueOptions(com.ibm.watson.assistant.v1.model.DeleteValueOptions) ListValuesOptions(com.ibm.watson.assistant.v1.model.ListValuesOptions) CreateValueOptions(com.ibm.watson.assistant.v1.model.CreateValueOptions) NotFoundException(com.ibm.cloud.sdk.core.service.exception.NotFoundException) ValueCollection(com.ibm.watson.assistant.v1.model.ValueCollection) Test(org.junit.Test)

Example 10 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().getResult();
    } 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().getResult();
    } 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().getResult();
    } 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().getResult();
        assertNotNull(response);
        assertNotNull(response.getValues());
        assertNotNull(response.getPagination());
        assertNotNull(response.getPagination().getRefreshUrl());
        // nextUrl may be null
        if (response.getPagination().getNextUrl() == null) {
            assertNull(response.getPagination().getNextCursor());
        } else {
            assertNotNull(response.getPagination().getNextCursor());
        }
        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;
            boolean found2 = false;
            for (Value valueResponse : response.getValues()) {
                found1 |= valueResponse.value().equals(entityValue1);
                found2 |= valueResponse.value().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().getResult();
        service.deleteValue(deleteOptions.newBuilder().value(entityValue2).build()).execute().getResult();
    }
}
Also used : CreateEntityOptions(com.ibm.watson.assistant.v1.model.CreateEntityOptions) DeleteValueOptions(com.ibm.watson.assistant.v1.model.DeleteValueOptions) ListValuesOptions(com.ibm.watson.assistant.v1.model.ListValuesOptions) CreateValueOptions(com.ibm.watson.assistant.v1.model.CreateValueOptions) NotFoundException(com.ibm.cloud.sdk.core.service.exception.NotFoundException) ValueCollection(com.ibm.watson.assistant.v1.model.ValueCollection) Value(com.ibm.watson.assistant.v1.model.Value) 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