Search in sources :

Example 6 with DeleteEntityOptions

use of com.ibm.watson.assistant.v1.model.DeleteEntityOptions 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)

Example 7 with DeleteEntityOptions

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

the class EntitiesIT method testGetEntity.

/**
 * Test getEntity.
 */
@Test
public void testGetEntity() {
    // gotta be unique
    String entity = "Hello" + UUID.randomUUID().toString();
    String entityDescription = "Description of " + entity;
    String entityValue = "Value of " + entity;
    List<CreateValue> entityValues = new ArrayList<CreateValue>();
    entityValues.add(new CreateValue.Builder().value(entityValue).build());
    CreateEntityOptions.Builder optionsBuilder = new CreateEntityOptions.Builder();
    optionsBuilder.workspaceId(workspaceId);
    optionsBuilder.entity(entity);
    optionsBuilder.description(entityDescription);
    optionsBuilder.values(entityValues);
    service.createEntity(optionsBuilder.build()).execute().getResult();
    Date start = new Date();
    try {
        GetEntityOptions getOptions = new GetEntityOptions.Builder(workspaceId, entity).export(true).includeAudit(true).build();
        Entity response = service.getEntity(getOptions).execute().getResult();
        assertNotNull(response);
        assertNotNull(response.getEntity());
        assertEquals(response.getEntity(), entity);
        assertNotNull(response.getDescription());
        assertEquals(response.getDescription(), entityDescription);
        assertNotNull(response.getValues());
        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));
        List<Value> values = response.getValues();
        assertTrue(values.size() == 1);
        assertEquals(values.get(0).value(), entityValue);
        assertTrue(fuzzyBefore(values.get(0).created(), now));
        assertTrue(fuzzyAfter(values.get(0).created(), start));
        assertTrue(fuzzyBefore(values.get(0).updated(), now));
        assertTrue(fuzzyAfter(values.get(0).updated(), start));
    } 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) ArrayList(java.util.ArrayList) Date(java.util.Date) NotFoundException(com.ibm.cloud.sdk.core.service.exception.NotFoundException) CreateValue(com.ibm.watson.assistant.v1.model.CreateValue) Value(com.ibm.watson.assistant.v1.model.Value) DeleteEntityOptions(com.ibm.watson.assistant.v1.model.DeleteEntityOptions) GetEntityOptions(com.ibm.watson.assistant.v1.model.GetEntityOptions) Test(org.junit.Test)

Example 8 with DeleteEntityOptions

use of com.ibm.watson.assistant.v1.model.DeleteEntityOptions 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().getResult();
    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().getResult();
        assertNotNull(response);
        assertNotNull(response.getEntity());
        assertEquals(response.getEntity(), 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().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) HashMap(java.util.HashMap) NotFoundException(com.ibm.cloud.sdk.core.service.exception.NotFoundException) UpdateEntityOptions(com.ibm.watson.assistant.v1.model.UpdateEntityOptions) DeleteEntityOptions(com.ibm.watson.assistant.v1.model.DeleteEntityOptions) Test(org.junit.Test)

Example 9 with DeleteEntityOptions

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

the class EntitiesIT method testGetEntity.

/**
 * Test getEntity.
 */
@Test
public void testGetEntity() {
    // gotta be unique
    String entity = "Hello" + UUID.randomUUID().toString();
    String entityDescription = "Description of " + entity;
    String entityValue = "Value of " + entity;
    List<CreateValue> entityValues = new ArrayList<CreateValue>();
    entityValues.add(new CreateValue.Builder().value(entityValue).build());
    CreateEntityOptions.Builder optionsBuilder = new CreateEntityOptions.Builder();
    optionsBuilder.workspaceId(workspaceId);
    optionsBuilder.entity(entity);
    optionsBuilder.description(entityDescription);
    optionsBuilder.values(entityValues);
    service.createEntity(optionsBuilder.build()).execute();
    Date start = new Date();
    try {
        GetEntityOptions getOptions = new GetEntityOptions.Builder(workspaceId, entity).export(true).includeAudit(true).build();
        EntityExport response = service.getEntity(getOptions).execute();
        assertNotNull(response);
        assertNotNull(response.getEntityName());
        assertEquals(response.getEntityName(), entity);
        assertNotNull(response.getDescription());
        assertEquals(response.getDescription(), entityDescription);
        assertNotNull(response.getValues());
        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));
        List<ValueExport> values = response.getValues();
        assertTrue(values.size() == 1);
        assertEquals(values.get(0).getValueText(), entityValue);
        assertTrue(fuzzyBefore(values.get(0).getCreated(), now));
        assertTrue(fuzzyAfter(values.get(0).getCreated(), start));
        assertTrue(fuzzyBefore(values.get(0).getUpdated(), now));
        assertTrue(fuzzyAfter(values.get(0).getUpdated(), start));
    } catch (Exception ex) {
        fail(ex.getMessage());
    } finally {
        // Clean up
        DeleteEntityOptions deleteOptions = new DeleteEntityOptions.Builder(workspaceId, entity).build();
        service.deleteEntity(deleteOptions).execute();
    }
}
Also used : CreateValue(com.ibm.watson.developer_cloud.conversation.v1.model.CreateValue) CreateEntityOptions(com.ibm.watson.developer_cloud.conversation.v1.model.CreateEntityOptions) ArrayList(java.util.ArrayList) Date(java.util.Date) NotFoundException(com.ibm.watson.developer_cloud.service.exception.NotFoundException) EntityExport(com.ibm.watson.developer_cloud.conversation.v1.model.EntityExport) ValueExport(com.ibm.watson.developer_cloud.conversation.v1.model.ValueExport) DeleteEntityOptions(com.ibm.watson.developer_cloud.conversation.v1.model.DeleteEntityOptions) GetEntityOptions(com.ibm.watson.developer_cloud.conversation.v1.model.GetEntityOptions) Test(org.junit.Test)

Example 10 with DeleteEntityOptions

use of com.ibm.watson.assistant.v1.model.DeleteEntityOptions 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)

Aggregations

Test (org.junit.Test)18 NotFoundException (com.ibm.watson.developer_cloud.service.exception.NotFoundException)12 DeleteEntityOptions (com.ibm.watson.assistant.v1.model.DeleteEntityOptions)7 NotFoundException (com.ibm.cloud.sdk.core.service.exception.NotFoundException)6 CreateEntityOptions (com.ibm.watson.assistant.v1.model.CreateEntityOptions)6 Entity (com.ibm.watson.assistant.v1.model.Entity)6 CreateEntityOptions (com.ibm.watson.developer_cloud.assistant.v1.model.CreateEntityOptions)6 DeleteEntityOptions (com.ibm.watson.developer_cloud.assistant.v1.model.DeleteEntityOptions)6 CreateEntityOptions (com.ibm.watson.developer_cloud.conversation.v1.model.CreateEntityOptions)6 DeleteEntityOptions (com.ibm.watson.developer_cloud.conversation.v1.model.DeleteEntityOptions)6 HashMap (java.util.HashMap)6 Ignore (org.junit.Ignore)6 CreateValue (com.ibm.watson.assistant.v1.model.CreateValue)3 CreateValue (com.ibm.watson.developer_cloud.assistant.v1.model.CreateValue)3 Entity (com.ibm.watson.developer_cloud.assistant.v1.model.Entity)3 EntityExport (com.ibm.watson.developer_cloud.assistant.v1.model.EntityExport)3 CreateValue (com.ibm.watson.developer_cloud.conversation.v1.model.CreateValue)3 Entity (com.ibm.watson.developer_cloud.conversation.v1.model.Entity)3 EntityExport (com.ibm.watson.developer_cloud.conversation.v1.model.EntityExport)3 ArrayList (java.util.ArrayList)3