Search in sources :

Example 11 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 testUpdateValue.

/**
 * Test updateValue.
 */
@Test
public void testUpdateValue() {
    String entity = "beverage";
    String entityValue1 = "coffee" + UUID.randomUUID().toString();
    String entityValue2 = "coffee" + UUID.randomUUID().toString();
    String synonym1 = "java";
    String synonym2 = "joe";
    // metadata
    Map<String, Object> valueMetadata = new HashMap<String, Object>();
    String metadataValue = "value for " + entityValue2;
    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"));
    }
    try {
        CreateValueOptions createOptions = new CreateValueOptions.Builder(workspaceId, entity, entityValue1).build();
        service.createValue(createOptions).execute();
    } catch (Exception ex) {
        // Exception is okay if is for Unique Violation
        assertTrue(ex.getLocalizedMessage().startsWith("Unique Violation"));
    }
    UpdateValueOptions updateOptions = new UpdateValueOptions.Builder().workspaceId(workspaceId).entity(entity).value(entityValue1).newValue(entityValue2).newSynonyms(new ArrayList<String>(Arrays.asList(synonym1, synonym2))).newMetadata(valueMetadata).build();
    Value response = service.updateValue(updateOptions).execute();
    try {
        assertNotNull(response);
        assertNotNull(response.getValueText());
        assertEquals(response.getValueText(), entityValue2);
        GetValueOptions getOptions = new GetValueOptions.Builder(workspaceId, entity, entityValue2).export(true).includeAudit(true).build();
        ValueExport vResponse = service.getValue(getOptions).execute();
        assertNotNull(vResponse);
        assertNotNull(vResponse.getValueText());
        assertEquals(vResponse.getValueText(), entityValue2);
        assertNotNull(vResponse.getCreated());
        assertNotNull(vResponse.getUpdated());
        assertNotNull(vResponse.getSynonyms());
        assertTrue(vResponse.getSynonyms().size() == 2);
        assertTrue(vResponse.getSynonyms().contains(synonym1));
        assertTrue(vResponse.getSynonyms().contains(synonym2));
        // 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, entityValue2).build();
        service.deleteValue(deleteOptions).execute();
    }
}
Also used : GetValueOptions(com.ibm.watson.developer_cloud.conversation.v1.model.GetValueOptions) HashMap(java.util.HashMap) 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) UpdateValueOptions(com.ibm.watson.developer_cloud.conversation.v1.model.UpdateValueOptions) NotFoundException(com.ibm.watson.developer_cloud.service.exception.NotFoundException) ValueExport(com.ibm.watson.developer_cloud.conversation.v1.model.ValueExport) Value(com.ibm.watson.developer_cloud.conversation.v1.model.Value) Test(org.junit.Test)

Example 12 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 testCreateEntity.

/**
 * Test createEntity.
 */
@Test
public void testCreateEntity() {
    // gotta be unique
    String entity = "Hello" + UUID.randomUUID().toString();
    String entityDescription = "Description of " + entity;
    Map<String, Object> entityMetadata = new HashMap<String, Object>();
    String metadataValue = "value for " + entity;
    entityMetadata.put("key", metadataValue);
    CreateEntityOptions.Builder optionsBuilder = new CreateEntityOptions.Builder();
    optionsBuilder.workspaceId(workspaceId);
    optionsBuilder.entity(entity);
    optionsBuilder.description(entityDescription);
    optionsBuilder.metadata(entityMetadata);
    // default is false
    optionsBuilder.fuzzyMatch(true);
    Entity response = service.createEntity(optionsBuilder.build()).execute();
    try {
        assertNotNull(response);
        assertNotNull(response.getEntityName());
        assertEquals(response.getEntityName(), entity);
        assertNotNull(response.getDescription());
        assertEquals(response.getDescription(), entityDescription);
        assertNotNull(response.getMetadata());
        assertNotNull(response.getMetadata().get("key"));
        assertEquals(response.getMetadata().get("key"), metadataValue);
        assertNotNull(response.isFuzzyMatch());
        assertTrue(response.isFuzzyMatch());
    } catch (Exception ex) {
        fail(ex.getMessage());
    } finally {
        // Clean up
        DeleteEntityOptions deleteOptions = new DeleteEntityOptions.Builder(workspaceId, entity).build();
        service.deleteEntity(deleteOptions).execute();
    }
}
Also used : Entity(com.ibm.watson.developer_cloud.assistant.v1.model.Entity) HashMap(java.util.HashMap) CreateEntityOptions(com.ibm.watson.developer_cloud.assistant.v1.model.CreateEntityOptions) DeleteEntityOptions(com.ibm.watson.developer_cloud.assistant.v1.model.DeleteEntityOptions) NotFoundException(com.ibm.watson.developer_cloud.service.exception.NotFoundException) Test(org.junit.Test)

Example 13 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 testDeleteEntity.

/**
 * Test deleteEntity.
 */
@Test
public void testDeleteEntity() {
    // gotta be unique
    String entity = "Hello" + UUID.randomUUID().toString();
    CreateEntityOptions options = new CreateEntityOptions.Builder(workspaceId, entity).build();
    Entity response = service.createEntity(options).execute();
    try {
        assertNotNull(response);
        assertNotNull(response.getEntityName());
        assertEquals(response.getEntityName(), entity);
        assertNull(response.getDescription());
        assertNull(response.getMetadata());
        assertTrue(response.isFuzzyMatch() == null || response.isFuzzyMatch().equals(Boolean.FALSE));
    } catch (Exception ex) {
        DeleteEntityOptions deleteOptions = new DeleteEntityOptions.Builder(workspaceId, entity).build();
        service.deleteEntity(deleteOptions).execute();
        fail(ex.getMessage());
    }
    DeleteEntityOptions deleteOptions = new DeleteEntityOptions.Builder(workspaceId, entity).build();
    service.deleteEntity(deleteOptions).execute();
    try {
        GetEntityOptions getOptions = new GetEntityOptions.Builder(workspaceId, entity).build();
        service.getEntity(getOptions).execute();
        fail("deleteEntity failed");
    } catch (Exception ex) {
        // Expected result
        assertTrue(ex instanceof NotFoundException);
    }
}
Also used : Entity(com.ibm.watson.developer_cloud.assistant.v1.model.Entity) CreateEntityOptions(com.ibm.watson.developer_cloud.assistant.v1.model.CreateEntityOptions) DeleteEntityOptions(com.ibm.watson.developer_cloud.assistant.v1.model.DeleteEntityOptions) NotFoundException(com.ibm.watson.developer_cloud.service.exception.NotFoundException) NotFoundException(com.ibm.watson.developer_cloud.service.exception.NotFoundException) GetEntityOptions(com.ibm.watson.developer_cloud.assistant.v1.model.GetEntityOptions) Test(org.junit.Test)

Example 14 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 createEntity.

/**
 * Create entity.
 *
 * Create a new entity.
 *
 * @param createEntityOptions the {@link CreateEntityOptions} containing the options for the call
 * @return a {@link ServiceCall} with a response type of {@link Entity}
 */
public ServiceCall<Entity> createEntity(CreateEntityOptions createEntityOptions) {
    Validator.notNull(createEntityOptions, "createEntityOptions cannot be null");
    String[] pathSegments = { "v1/workspaces", "entities" };
    String[] pathParameters = { createEntityOptions.workspaceId() };
    RequestBuilder builder = RequestBuilder.post(RequestBuilder.constructHttpUrl(getEndPoint(), pathSegments, pathParameters));
    builder.query(VERSION, versionDate);
    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", GsonSingleton.getGson().toJsonTree(createEntityOptions.metadata()));
    }
    if (createEntityOptions.values() != null) {
        contentJson.add("values", GsonSingleton.getGson().toJsonTree(createEntityOptions.values()));
    }
    if (createEntityOptions.fuzzyMatch() != null) {
        contentJson.addProperty("fuzzy_match", createEntityOptions.fuzzyMatch());
    }
    builder.bodyJson(contentJson);
    return createServiceCall(builder.build(), ResponseConverterUtils.getObject(Entity.class));
}
Also used : Entity(com.ibm.watson.developer_cloud.conversation.v1.model.Entity) RequestBuilder(com.ibm.watson.developer_cloud.http.RequestBuilder) JsonObject(com.google.gson.JsonObject)

Example 15 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 createSynonym.

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

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