Search in sources :

Example 46 with Entity

use of com.ibm.watson.assistant.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.conversation.v1.model.Entity) CreateEntityOptions(com.ibm.watson.developer_cloud.conversation.v1.model.CreateEntityOptions) DeleteEntityOptions(com.ibm.watson.developer_cloud.conversation.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.conversation.v1.model.GetEntityOptions) Test(org.junit.Test)

Example 47 with Entity

use of com.ibm.watson.assistant.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.conversation.v1.model.Entity) CreateValue(com.ibm.watson.developer_cloud.conversation.v1.model.CreateValue) CreateEntityOptions(com.ibm.watson.developer_cloud.conversation.v1.model.CreateEntityOptions) HashMap(java.util.HashMap) NotFoundException(com.ibm.watson.developer_cloud.service.exception.NotFoundException) UpdateEntityOptions(com.ibm.watson.developer_cloud.conversation.v1.model.UpdateEntityOptions) DeleteEntityOptions(com.ibm.watson.developer_cloud.conversation.v1.model.DeleteEntityOptions) Test(org.junit.Test)

Example 48 with Entity

use of com.ibm.watson.assistant.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.conversation.v1.model.Entity) HashMap(java.util.HashMap) CreateEntityOptions(com.ibm.watson.developer_cloud.conversation.v1.model.CreateEntityOptions) DeleteEntityOptions(com.ibm.watson.developer_cloud.conversation.v1.model.DeleteEntityOptions) NotFoundException(com.ibm.watson.developer_cloud.service.exception.NotFoundException) Test(org.junit.Test)

Example 49 with Entity

use of com.ibm.watson.assistant.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 50 with Entity

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

the class Conversation method updateEntity.

/**
 * Update entity.
 *
 * Update an existing entity with new or modified data.
 *
 * @param updateEntityOptions the {@link UpdateEntityOptions} containing the options for the call
 * @return a {@link ServiceCall} with a response type of {@link Entity}
 */
public ServiceCall<Entity> updateEntity(UpdateEntityOptions updateEntityOptions) {
    Validator.notNull(updateEntityOptions, "updateEntityOptions cannot be null");
    String[] pathSegments = { "v1/workspaces", "entities" };
    String[] pathParameters = { updateEntityOptions.workspaceId(), updateEntityOptions.entity() };
    RequestBuilder builder = RequestBuilder.post(RequestBuilder.constructHttpUrl(getEndPoint(), pathSegments, pathParameters));
    builder.query(VERSION, versionDate);
    final JsonObject contentJson = new JsonObject();
    if (updateEntityOptions.newFuzzyMatch() != null) {
        contentJson.addProperty("fuzzy_match", updateEntityOptions.newFuzzyMatch());
    }
    if (updateEntityOptions.newEntity() != null) {
        contentJson.addProperty("entity", updateEntityOptions.newEntity());
    }
    if (updateEntityOptions.newMetadata() != null) {
        contentJson.add("metadata", GsonSingleton.getGson().toJsonTree(updateEntityOptions.newMetadata()));
    }
    if (updateEntityOptions.newValues() != null) {
        contentJson.add("values", GsonSingleton.getGson().toJsonTree(updateEntityOptions.newValues()));
    }
    if (updateEntityOptions.newDescription() != null) {
        contentJson.addProperty("description", updateEntityOptions.newDescription());
    }
    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)

Aggregations

MockResponse (okhttp3.mockwebserver.MockResponse)32 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)32 Test (org.testng.annotations.Test)32 HashMap (java.util.HashMap)27 Test (org.junit.Test)24 CreateEntityOptions (com.ibm.watson.assistant.v1.model.CreateEntityOptions)19 NotFoundException (com.ibm.cloud.sdk.core.service.exception.NotFoundException)18 RequestBuilder (com.ibm.cloud.sdk.core.http.RequestBuilder)13 CreateValueOptions (com.ibm.watson.assistant.v1.model.CreateValueOptions)13 Entity (com.ibm.watson.assistant.v1.model.Entity)12 Value (com.ibm.watson.assistant.v1.model.Value)12 Synonym (com.ibm.watson.assistant.v1.model.Synonym)11 JsonObject (com.google.gson.JsonObject)10 CreateValue (com.ibm.watson.assistant.v1.model.CreateValue)10 Entity (model.Entity)10 CreateSynonymOptions (com.ibm.watson.assistant.v1.model.CreateSynonymOptions)7 DeleteEntityOptions (com.ibm.watson.assistant.v1.model.DeleteEntityOptions)7 DeleteSynonymOptions (com.ibm.watson.assistant.v1.model.DeleteSynonymOptions)7 DeleteValueOptions (com.ibm.watson.assistant.v1.model.DeleteValueOptions)7 Example (com.ibm.watson.assistant.v1.model.Example)7