Search in sources :

Example 6 with ListEntitiesOptions

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

the class AssistantTest method testListEntitiesWOptions.

// Test the listEntities operation with a valid options model parameter
@Test
public void testListEntitiesWOptions() throws Throwable {
    // Register a mock response
    String mockResponseBody = "{\"entities\": [{\"entity\": \"entity\", \"description\": \"description\", \"metadata\": {\"mapKey\": \"anyValue\"}, \"fuzzy_match\": true, \"created\": \"2019-01-01T12:00:00.000Z\", \"updated\": \"2019-01-01T12:00:00.000Z\", \"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 listEntitiesPath = "/v1/workspaces/testString/entities";
    server.enqueue(new MockResponse().setHeader("Content-type", "application/json").setResponseCode(200).setBody(mockResponseBody));
    // Construct an instance of the ListEntitiesOptions model
    ListEntitiesOptions listEntitiesOptionsModel = new ListEntitiesOptions.Builder().workspaceId("testString").export(false).pageLimit(Long.valueOf("26")).includeCount(false).sort("entity").cursor("testString").includeAudit(false).build();
    // Invoke listEntities() with a valid options model and verify the result
    Response<EntityCollection> response = assistantService.listEntities(listEntitiesOptionsModel).execute();
    assertNotNull(response);
    EntityCollection 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, listEntitiesPath);
    // 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"), "entity");
    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) EntityCollection(com.ibm.watson.assistant.v1.model.EntityCollection) ListEntitiesOptions(com.ibm.watson.assistant.v1.model.ListEntitiesOptions) Test(org.testng.annotations.Test)

Example 7 with ListEntitiesOptions

use of com.ibm.watson.assistant.v1.model.ListEntitiesOptions 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().getResult();
    service.createEntity(createOptions.newBuilder().entity(entity2).build()).execute().getResult();
    try {
        ListEntitiesOptions listOptions = new ListEntitiesOptions.Builder(workspaceId).sort("entity").pageLimit(1L).build();
        EntityCollection response = service.listEntities(listOptions).execute().getResult();
        assertNotNull(response);
        assertNotNull(response.getEntities());
        assertNotNull(response.getPagination());
        assertNotNull(response.getPagination().getRefreshUrl());
        assertNotNull(response.getPagination().getNextUrl());
        assertNotNull(response.getPagination().getNextCursor());
        assertTrue(!response.getEntities().get(0).getEntity().equals(entity1));
        Entity ieResponse = null;
        while (response.getPagination().getNextCursor() != null) {
            assertNotNull(response.getEntities());
            assertTrue(response.getEntities().size() == 1);
            if (response.getEntities().get(0).getEntity().equals(entity1)) {
                ieResponse = response.getEntities().get(0);
                break;
            }
            String cursor = response.getPagination().getNextCursor();
            response = service.listEntities(listOptions.newBuilder().cursor(cursor).build()).execute().getResult();
        }
        assertNotNull(ieResponse);
    } catch (Exception ex) {
        fail(ex.getMessage());
    } finally {
        // Clean up
        DeleteEntityOptions deleteOptions = new DeleteEntityOptions.Builder(workspaceId, entity1).build();
        service.deleteEntity(deleteOptions).execute().getResult();
        service.deleteEntity(deleteOptions.newBuilder().entity(entity2).build()).execute().getResult();
    }
}
Also used : Entity(com.ibm.watson.assistant.v1.model.Entity) CreateEntityOptions(com.ibm.watson.assistant.v1.model.CreateEntityOptions) EntityCollection(com.ibm.watson.assistant.v1.model.EntityCollection) DeleteEntityOptions(com.ibm.watson.assistant.v1.model.DeleteEntityOptions) ListEntitiesOptions(com.ibm.watson.assistant.v1.model.ListEntitiesOptions) NotFoundException(com.ibm.cloud.sdk.core.service.exception.NotFoundException) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 8 with ListEntitiesOptions

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

the class Assistant method listEntities.

/**
 * List entities.
 *
 * <p>List the entities for a workspace.
 *
 * @param listEntitiesOptions the {@link ListEntitiesOptions} containing the options for the call
 * @return a {@link ServiceCall} with a result of type {@link EntityCollection}
 */
public ServiceCall<EntityCollection> listEntities(ListEntitiesOptions listEntitiesOptions) {
    com.ibm.cloud.sdk.core.util.Validator.notNull(listEntitiesOptions, "listEntitiesOptions cannot be null");
    Map<String, String> pathParamsMap = new HashMap<String, String>();
    pathParamsMap.put("workspace_id", listEntitiesOptions.workspaceId());
    RequestBuilder builder = RequestBuilder.get(RequestBuilder.resolveRequestUrl(getServiceUrl(), "/v1/workspaces/{workspace_id}/entities", pathParamsMap));
    Map<String, String> sdkHeaders = SdkCommon.getSdkHeaders("conversation", "v1", "listEntities");
    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 (listEntitiesOptions.export() != null) {
        builder.query("export", String.valueOf(listEntitiesOptions.export()));
    }
    if (listEntitiesOptions.pageLimit() != null) {
        builder.query("page_limit", String.valueOf(listEntitiesOptions.pageLimit()));
    }
    if (listEntitiesOptions.includeCount() != null) {
        builder.query("include_count", String.valueOf(listEntitiesOptions.includeCount()));
    }
    if (listEntitiesOptions.sort() != null) {
        builder.query("sort", String.valueOf(listEntitiesOptions.sort()));
    }
    if (listEntitiesOptions.cursor() != null) {
        builder.query("cursor", String.valueOf(listEntitiesOptions.cursor()));
    }
    if (listEntitiesOptions.includeAudit() != null) {
        builder.query("include_audit", String.valueOf(listEntitiesOptions.includeAudit()));
    }
    ResponseConverter<EntityCollection> responseConverter = ResponseConverterUtils.getValue(new com.google.gson.reflect.TypeToken<EntityCollection>() {
    }.getType());
    return createServiceCall(builder.build(), responseConverter);
}
Also used : RequestBuilder(com.ibm.cloud.sdk.core.http.RequestBuilder) HashMap(java.util.HashMap) EntityCollection(com.ibm.watson.assistant.v1.model.EntityCollection)

Aggregations

Ignore (org.junit.Ignore)6 Test (org.junit.Test)6 EntityCollection (com.ibm.watson.assistant.v1.model.EntityCollection)4 NotFoundException (com.ibm.watson.developer_cloud.service.exception.NotFoundException)4 ListEntitiesOptions (com.ibm.watson.assistant.v1.model.ListEntitiesOptions)3 NotFoundException (com.ibm.cloud.sdk.core.service.exception.NotFoundException)2 CreateEntityOptions (com.ibm.watson.assistant.v1.model.CreateEntityOptions)2 DeleteEntityOptions (com.ibm.watson.assistant.v1.model.DeleteEntityOptions)2 Entity (com.ibm.watson.assistant.v1.model.Entity)2 CreateEntityOptions (com.ibm.watson.developer_cloud.assistant.v1.model.CreateEntityOptions)2 DeleteEntityOptions (com.ibm.watson.developer_cloud.assistant.v1.model.DeleteEntityOptions)2 EntityCollection (com.ibm.watson.developer_cloud.assistant.v1.model.EntityCollection)2 EntityExport (com.ibm.watson.developer_cloud.assistant.v1.model.EntityExport)2 ListEntitiesOptions (com.ibm.watson.developer_cloud.assistant.v1.model.ListEntitiesOptions)2 CreateEntityOptions (com.ibm.watson.developer_cloud.conversation.v1.model.CreateEntityOptions)2 DeleteEntityOptions (com.ibm.watson.developer_cloud.conversation.v1.model.DeleteEntityOptions)2 EntityCollection (com.ibm.watson.developer_cloud.conversation.v1.model.EntityCollection)2 EntityExport (com.ibm.watson.developer_cloud.conversation.v1.model.EntityExport)2 ListEntitiesOptions (com.ibm.watson.developer_cloud.conversation.v1.model.ListEntitiesOptions)2 RequestBuilder (com.ibm.cloud.sdk.core.http.RequestBuilder)1