Search in sources :

Example 16 with CreateEntityOptions

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

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

the class ValuesIT method testCreateValue.

/**
 * Test createValue.
 */
@Test
public void testCreateValue() {
    String entity = "beverage";
    String entityValue = "coffee" + UUID.randomUUID().toString();
    // metadata
    Map<String, Object> valueMetadata = new HashMap<String, Object>();
    String metadataValue = "value for " + entityValue;
    valueMetadata.put("key", metadataValue);
    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(workspaceId).entity(entity).value(entityValue).metadata(valueMetadata).build();
    Value response = service.createValue(createOptions).execute();
    try {
        assertNotNull(response);
        assertNotNull(response.getValueText());
        assertEquals(response.getValueText(), entityValue);
        assertNotNull(response.getMetadata());
        // metadata
        assertNotNull(response.getMetadata());
        assertNotNull(response.getMetadata().get("key"));
        assertEquals(response.getMetadata().get("key"), metadataValue);
    } catch (Exception ex) {
        fail(ex.getMessage());
    } finally {
        // Clean up
        DeleteValueOptions deleteOptions = new DeleteValueOptions.Builder(workspaceId, entity, entityValue).build();
        service.deleteValue(deleteOptions).execute();
    }
}
Also used : HashMap(java.util.HashMap) CreateEntityOptions(com.ibm.watson.developer_cloud.assistant.v1.model.CreateEntityOptions) DeleteValueOptions(com.ibm.watson.developer_cloud.assistant.v1.model.DeleteValueOptions) CreateValueOptions(com.ibm.watson.developer_cloud.assistant.v1.model.CreateValueOptions) Value(com.ibm.watson.developer_cloud.assistant.v1.model.Value) NotFoundException(com.ibm.watson.developer_cloud.service.exception.NotFoundException) Test(org.junit.Test)

Example 18 with CreateEntityOptions

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

the class AssistantTest method testCreateEntityOptionsBuilder.

/**
 * Test CreateEntityOptions builder.
 */
@Test
public void testCreateEntityOptionsBuilder() {
    String entity = "anEntity";
    CreateValue entityValue0 = new CreateValue.Builder().value("entityValue0").addPattern("pattern0").build();
    CreateValue entityValue1 = new CreateValue.Builder().value("entityValue1").addPattern("pattern1").build();
    CreateEntityOptions createOptions = new CreateEntityOptions.Builder().workspaceId(WORKSPACE_ID).entity(entity).addValue(entityValue0).addValue(entityValue1).build();
    assertEquals(createOptions.workspaceId(), WORKSPACE_ID);
    assertEquals(createOptions.entity(), entity);
    assertEquals(createOptions.values().size(), 2);
    assertEquals(createOptions.values().get(0), entityValue0);
    assertEquals(createOptions.values().get(1), entityValue1);
    CreateEntityOptions.Builder builder = createOptions.newBuilder();
    CreateValue entityValue2 = new CreateValue.Builder().value("entityValue2").addPattern("pattern2").build();
    builder.values(Arrays.asList(entityValue2));
    CreateEntityOptions options2 = builder.build();
    assertEquals(options2.workspaceId(), WORKSPACE_ID);
    assertEquals(options2.entity(), entity);
    assertEquals(options2.values().size(), 1);
    assertEquals(options2.values().get(0), entityValue2);
}
Also used : CreateValue(com.ibm.watson.developer_cloud.assistant.v1.model.CreateValue) CreateEntityOptions(com.ibm.watson.developer_cloud.assistant.v1.model.CreateEntityOptions) WatsonServiceUnitTest(com.ibm.watson.developer_cloud.WatsonServiceUnitTest) Test(org.junit.Test)

Example 19 with CreateEntityOptions

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

the class Assistant method createEntity.

/**
 * Create entity.
 *
 * <p>Create a new entity, or enable a system entity.
 *
 * <p>If you want to create multiple entities with a single API call, consider using the **[Update
 * workspace](#update-workspace)** method instead.
 *
 * @param createEntityOptions the {@link CreateEntityOptions} containing the options for the call
 * @return a {@link ServiceCall} with a result of type {@link Entity}
 */
public ServiceCall<Entity> createEntity(CreateEntityOptions createEntityOptions) {
    com.ibm.cloud.sdk.core.util.Validator.notNull(createEntityOptions, "createEntityOptions cannot be null");
    Map<String, String> pathParamsMap = new HashMap<String, String>();
    pathParamsMap.put("workspace_id", createEntityOptions.workspaceId());
    RequestBuilder builder = RequestBuilder.post(RequestBuilder.resolveRequestUrl(getServiceUrl(), "/v1/workspaces/{workspace_id}/entities", pathParamsMap));
    Map<String, String> sdkHeaders = SdkCommon.getSdkHeaders("conversation", "v1", "createEntity");
    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 (createEntityOptions.includeAudit() != null) {
        builder.query("include_audit", String.valueOf(createEntityOptions.includeAudit()));
    }
    final JsonObject contentJson = new JsonObject();
    contentJson.addProperty("entity", createEntityOptions.entity());
    if (createEntityOptions.description() != null) {
        contentJson.addProperty("description", createEntityOptions.description());
    }
    if (createEntityOptions.metadata() != null) {
        contentJson.add("metadata", com.ibm.cloud.sdk.core.util.GsonSingleton.getGson().toJsonTree(createEntityOptions.metadata()));
    }
    if (createEntityOptions.fuzzyMatch() != null) {
        contentJson.addProperty("fuzzy_match", createEntityOptions.fuzzyMatch());
    }
    if (createEntityOptions.values() != null) {
        contentJson.add("values", com.ibm.cloud.sdk.core.util.GsonSingleton.getGson().toJsonTree(createEntityOptions.values()));
    }
    builder.bodyJson(contentJson);
    ResponseConverter<Entity> responseConverter = ResponseConverterUtils.getValue(new com.google.gson.reflect.TypeToken<Entity>() {
    }.getType());
    return createServiceCall(builder.build(), responseConverter);
}
Also used : Entity(com.ibm.watson.assistant.v1.model.Entity) RequestBuilder(com.ibm.cloud.sdk.core.http.RequestBuilder) HashMap(java.util.HashMap) JsonObject(com.google.gson.JsonObject)

Example 20 with CreateEntityOptions

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

the class EntitiesIT method testListEntities.

/**
 * Test listEntities.
 */
@Test
@Ignore("To be run locally until we fix the Rate limitation issue")
public void testListEntities() {
    // gotta be unique
    String entity = "Hello" + UUID.randomUUID().toString();
    try {
        ListEntitiesOptions listOptions = new ListEntitiesOptions.Builder(workspaceId).build();
        EntityCollection response = service.listEntities(listOptions).execute().getResult();
        assertNotNull(response);
        assertNotNull(response.getEntities());
        assertNotNull(response.getPagination());
        assertNotNull(response.getPagination().getRefreshUrl());
        // nextUrl may be null
        // Now add an entity and make sure we get it back
        String entityDescription = "Description of " + entity;
        String entityValue = "Value of " + entity;
        CreateEntityOptions options = new CreateEntityOptions.Builder(workspaceId, entity).description(entityDescription).addValues(new CreateValue.Builder(entityValue).build()).build();
        service.createEntity(options).execute().getResult();
        ListEntitiesOptions listOptions2 = listOptions.newBuilder().sort("-updated").pageLimit(5L).export(true).build();
        EntityCollection response2 = service.listEntities(listOptions2).execute().getResult();
        assertNotNull(response2);
        assertNotNull(response2.getEntities());
        List<Entity> entities = response2.getEntities();
        Entity ieResponse = null;
        for (Entity resp : entities) {
            if (resp.getEntity().equals(entity)) {
                ieResponse = resp;
                break;
            }
        }
        assertNotNull(ieResponse);
        assertNotNull(ieResponse.getDescription());
        assertEquals(ieResponse.getDescription(), entityDescription);
        assertNotNull(ieResponse.getValues());
        assertTrue(ieResponse.getValues().size() == 1);
        assertTrue(ieResponse.getValues().get(0).value().equals(entityValue));
    } catch (Exception ex) {
        fail(ex.getMessage());
    } finally {
        // Clean up
        DeleteEntityOptions deleteOptions = new DeleteEntityOptions.Builder(workspaceId, entity).build();
        service.deleteEntity(deleteOptions).execute().getResult();
    }
}
Also used : Entity(com.ibm.watson.assistant.v1.model.Entity) CreateValue(com.ibm.watson.assistant.v1.model.CreateValue) CreateEntityOptions(com.ibm.watson.assistant.v1.model.CreateEntityOptions) NotFoundException(com.ibm.cloud.sdk.core.service.exception.NotFoundException) EntityCollection(com.ibm.watson.assistant.v1.model.EntityCollection) DeleteEntityOptions(com.ibm.watson.assistant.v1.model.DeleteEntityOptions) ListEntitiesOptions(com.ibm.watson.assistant.v1.model.ListEntitiesOptions) Ignore(org.junit.Ignore) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)47 NotFoundException (com.ibm.watson.developer_cloud.service.exception.NotFoundException)30 CreateEntityOptions (com.ibm.watson.assistant.v1.model.CreateEntityOptions)16 CreateEntityOptions (com.ibm.watson.developer_cloud.assistant.v1.model.CreateEntityOptions)16 CreateEntityOptions (com.ibm.watson.developer_cloud.conversation.v1.model.CreateEntityOptions)16 NotFoundException (com.ibm.cloud.sdk.core.service.exception.NotFoundException)15 CreateValueOptions (com.ibm.watson.assistant.v1.model.CreateValueOptions)12 CreateValueOptions (com.ibm.watson.developer_cloud.assistant.v1.model.CreateValueOptions)12 CreateValueOptions (com.ibm.watson.developer_cloud.conversation.v1.model.CreateValueOptions)12 HashMap (java.util.HashMap)7 CreateSynonymOptions (com.ibm.watson.assistant.v1.model.CreateSynonymOptions)6 DeleteSynonymOptions (com.ibm.watson.assistant.v1.model.DeleteSynonymOptions)6 DeleteValueOptions (com.ibm.watson.assistant.v1.model.DeleteValueOptions)6 CreateSynonymOptions (com.ibm.watson.developer_cloud.assistant.v1.model.CreateSynonymOptions)6 DeleteSynonymOptions (com.ibm.watson.developer_cloud.assistant.v1.model.DeleteSynonymOptions)6 DeleteValueOptions (com.ibm.watson.developer_cloud.assistant.v1.model.DeleteValueOptions)6 CreateSynonymOptions (com.ibm.watson.developer_cloud.conversation.v1.model.CreateSynonymOptions)6 DeleteSynonymOptions (com.ibm.watson.developer_cloud.conversation.v1.model.DeleteSynonymOptions)6 DeleteValueOptions (com.ibm.watson.developer_cloud.conversation.v1.model.DeleteValueOptions)6 Date (java.util.Date)6