Search in sources :

Example 31 with Entity

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

the class ValuesIT method testGetValue.

/**
 * Test getValue.
 */
@Test
public void testGetValue() {
    String entity = "beverage";
    String entityValue = "coffee" + UUID.randomUUID().toString();
    String synonym1 = "java";
    String synonym2 = "joe";
    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, entityValue).synonyms(new ArrayList<String>(Arrays.asList(synonym1, synonym2))).build();
    service.createValue(createOptions).execute();
    Date start = new Date();
    try {
        GetValueOptions getOptions = new GetValueOptions.Builder(workspaceId, entity, entityValue).export(true).includeAudit(true).build();
        ValueExport response = service.getValue(getOptions).execute();
        assertNotNull(response);
        assertNotNull(response.getValueText());
        assertEquals(response.getValueText(), entityValue);
        assertNotNull(response.getCreated());
        assertNotNull(response.getUpdated());
        Date now = new Date();
        assertTrue(fuzzyBefore(response.getCreated(), now));
        assertTrue(fuzzyAfter(response.getCreated(), start));
        assertTrue(fuzzyBefore(response.getUpdated(), now));
        assertTrue(fuzzyAfter(response.getUpdated(), start));
        assertNotNull(response.getSynonyms());
        assertTrue(response.getSynonyms().size() == 2);
        assertTrue(response.getSynonyms().contains(synonym1));
        assertTrue(response.getSynonyms().contains(synonym2));
    } catch (Exception ex) {
        fail(ex.getMessage());
    } finally {
        DeleteValueOptions deleteOptions = new DeleteValueOptions.Builder(workspaceId, entity, entityValue).build();
        service.deleteValue(deleteOptions).execute();
    }
}
Also used : GetValueOptions(com.ibm.watson.developer_cloud.conversation.v1.model.GetValueOptions) CreateEntityOptions(com.ibm.watson.developer_cloud.conversation.v1.model.CreateEntityOptions) DeleteValueOptions(com.ibm.watson.developer_cloud.conversation.v1.model.DeleteValueOptions) CreateValueOptions(com.ibm.watson.developer_cloud.conversation.v1.model.CreateValueOptions) ArrayList(java.util.ArrayList) NotFoundException(com.ibm.watson.developer_cloud.service.exception.NotFoundException) Date(java.util.Date) ValueExport(com.ibm.watson.developer_cloud.conversation.v1.model.ValueExport) Test(org.junit.Test)

Example 32 with Entity

use of com.ibm.watson.developer_cloud.conversation.v1.model.Entity 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 33 with Entity

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

the class EntitiesIT method testUpdateEntity.

/**
 * Test updateEntity.
 */
@Test
public void testUpdateEntity() {
    // gotta be unique
    String entity = "Hello" + UUID.randomUUID().toString();
    // gotta be unique
    String entity2 = "Goodbye" + UUID.randomUUID().toString();
    String entityDescription = "Description of " + entity;
    CreateEntityOptions.Builder createOptionsBuilder = new CreateEntityOptions.Builder();
    createOptionsBuilder.workspaceId(workspaceId);
    createOptionsBuilder.entity(entity);
    createOptionsBuilder.description(entityDescription);
    service.createEntity(createOptionsBuilder.build()).execute();
    try {
        String entityDescription2 = "Description of " + entity2;
        String entityValue2 = "Value of " + entity2;
        Map<String, Object> entityMetadata2 = new HashMap<String, Object>();
        String metadataValue2 = "value for " + entity2;
        entityMetadata2.put("key", metadataValue2);
        UpdateEntityOptions.Builder updateOptionsBuilder = new UpdateEntityOptions.Builder(workspaceId, entity);
        updateOptionsBuilder.newEntity(entity2);
        updateOptionsBuilder.newDescription(entityDescription2);
        updateOptionsBuilder.addValue(new CreateValue.Builder().value(entityValue2).build());
        updateOptionsBuilder.newMetadata(entityMetadata2);
        updateOptionsBuilder.newFuzzyMatch(true);
        Entity response = service.updateEntity(updateOptionsBuilder.build()).execute();
        assertNotNull(response);
        assertNotNull(response.getEntityName());
        assertEquals(response.getEntityName(), entity2);
        assertNotNull(response.getDescription());
        assertEquals(response.getDescription(), entityDescription2);
        assertNotNull(response.getMetadata());
        assertNotNull(response.getMetadata().get("key"));
        assertEquals(response.getMetadata().get("key"), metadataValue2);
        assertNotNull(response.isFuzzyMatch());
        assertTrue(response.isFuzzyMatch());
    } catch (Exception ex) {
        fail(ex.getMessage());
    } finally {
        // Clean up
        DeleteEntityOptions deleteOptions = new DeleteEntityOptions.Builder(workspaceId, entity2).build();
        service.deleteEntity(deleteOptions).execute();
    }
}
Also used : Entity(com.ibm.watson.developer_cloud.assistant.v1.model.Entity) CreateValue(com.ibm.watson.developer_cloud.assistant.v1.model.CreateValue) CreateEntityOptions(com.ibm.watson.developer_cloud.assistant.v1.model.CreateEntityOptions) HashMap(java.util.HashMap) NotFoundException(com.ibm.watson.developer_cloud.service.exception.NotFoundException) UpdateEntityOptions(com.ibm.watson.developer_cloud.assistant.v1.model.UpdateEntityOptions) DeleteEntityOptions(com.ibm.watson.developer_cloud.assistant.v1.model.DeleteEntityOptions) Test(org.junit.Test)

Example 34 with Entity

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

the class Conversation method createValue.

/**
 * Add entity value.
 *
 * Create a new value for an entity.
 *
 * @param createValueOptions the {@link CreateValueOptions} containing the options for the call
 * @return a {@link ServiceCall} with a response type of {@link Value}
 */
public ServiceCall<Value> createValue(CreateValueOptions createValueOptions) {
    Validator.notNull(createValueOptions, "createValueOptions cannot be null");
    String[] pathSegments = { "v1/workspaces", "entities", "values" };
    String[] pathParameters = { createValueOptions.workspaceId(), createValueOptions.entity() };
    RequestBuilder builder = RequestBuilder.post(RequestBuilder.constructHttpUrl(getEndPoint(), pathSegments, pathParameters));
    builder.query(VERSION, versionDate);
    final JsonObject contentJson = new JsonObject();
    contentJson.addProperty("value", createValueOptions.value());
    if (createValueOptions.metadata() != null) {
        contentJson.add("metadata", GsonSingleton.getGson().toJsonTree(createValueOptions.metadata()));
    }
    if (createValueOptions.synonyms() != null) {
        contentJson.add("synonyms", GsonSingleton.getGson().toJsonTree(createValueOptions.synonyms()));
    }
    if (createValueOptions.patterns() != null) {
        contentJson.add("patterns", GsonSingleton.getGson().toJsonTree(createValueOptions.patterns()));
    }
    if (createValueOptions.valueType() != null) {
        contentJson.addProperty("type", createValueOptions.valueType());
    }
    builder.bodyJson(contentJson);
    return createServiceCall(builder.build(), ResponseConverterUtils.getObject(Value.class));
}
Also used : RequestBuilder(com.ibm.watson.developer_cloud.http.RequestBuilder) Value(com.ibm.watson.developer_cloud.conversation.v1.model.Value) JsonObject(com.google.gson.JsonObject)

Example 35 with Entity

use of com.ibm.watson.developer_cloud.conversation.v1.model.Entity 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)

Aggregations

Test (org.junit.Test)28 NotFoundException (com.ibm.watson.developer_cloud.service.exception.NotFoundException)22 CreateEntityOptions (com.ibm.watson.developer_cloud.conversation.v1.model.CreateEntityOptions)19 CreateValueOptions (com.ibm.watson.developer_cloud.conversation.v1.model.CreateValueOptions)13 RequestBuilder (com.ibm.watson.developer_cloud.http.RequestBuilder)13 JsonObject (com.google.gson.JsonObject)9 Synonym (com.ibm.watson.developer_cloud.conversation.v1.model.Synonym)8 HashMap (java.util.HashMap)7 CreateSynonymOptions (com.ibm.watson.developer_cloud.conversation.v1.model.CreateSynonymOptions)6 CreateValue (com.ibm.watson.developer_cloud.conversation.v1.model.CreateValue)6 DeleteEntityOptions (com.ibm.watson.developer_cloud.conversation.v1.model.DeleteEntityOptions)6 DeleteSynonymOptions (com.ibm.watson.developer_cloud.conversation.v1.model.DeleteSynonymOptions)6 DeleteValueOptions (com.ibm.watson.developer_cloud.conversation.v1.model.DeleteValueOptions)6 WatsonServiceUnitTest (com.ibm.watson.developer_cloud.WatsonServiceUnitTest)5 Entity (com.ibm.watson.developer_cloud.assistant.v1.model.Entity)5 Entity (com.ibm.watson.developer_cloud.conversation.v1.model.Entity)5 Value (com.ibm.watson.developer_cloud.conversation.v1.model.Value)5 ValueExport (com.ibm.watson.developer_cloud.conversation.v1.model.ValueExport)5 EntityExport (com.ibm.watson.developer_cloud.conversation.v1.model.EntityExport)4 CreateEntityOptions (com.ibm.watson.developer_cloud.assistant.v1.model.CreateEntityOptions)3