Search in sources :

Example 66 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().getResult();
    try {
        assertNotNull(response);
        assertNotNull(response.getEntity());
        assertEquals(response.getEntity(), 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().getResult();
        fail(ex.getMessage());
    }
    DeleteEntityOptions deleteOptions = new DeleteEntityOptions.Builder(workspaceId, entity).build();
    service.deleteEntity(deleteOptions).execute().getResult();
    try {
        GetEntityOptions getOptions = new GetEntityOptions.Builder(workspaceId, entity).build();
        service.getEntity(getOptions).execute().getResult();
        fail("deleteEntity failed");
    } catch (Exception ex) {
        // Expected result
        assertTrue(ex instanceof NotFoundException);
    }
}
Also used : Entity(com.ibm.watson.assistant.v1.model.Entity) CreateEntityOptions(com.ibm.watson.assistant.v1.model.CreateEntityOptions) DeleteEntityOptions(com.ibm.watson.assistant.v1.model.DeleteEntityOptions) NotFoundException(com.ibm.cloud.sdk.core.service.exception.NotFoundException) NotFoundException(com.ibm.cloud.sdk.core.service.exception.NotFoundException) GetEntityOptions(com.ibm.watson.assistant.v1.model.GetEntityOptions) Test(org.junit.Test)

Example 67 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().getResult();
    try {
        assertNotNull(response);
        assertNotNull(response.getEntity());
        assertEquals(response.getEntity(), 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().getResult();
    }
}
Also used : Entity(com.ibm.watson.assistant.v1.model.Entity) HashMap(java.util.HashMap) CreateEntityOptions(com.ibm.watson.assistant.v1.model.CreateEntityOptions) DeleteEntityOptions(com.ibm.watson.assistant.v1.model.DeleteEntityOptions) NotFoundException(com.ibm.cloud.sdk.core.service.exception.NotFoundException) Test(org.junit.Test)

Example 68 with Entity

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

the class EntitiesIT method testListEntitiesWithPaging.

/**
 * Test listEntities with pagination.
 */
@Test
@Ignore("To be run locally until we fix the Rate limitation issue")
public void testListEntitiesWithPaging() {
    // gotta be unique
    String entity1 = "Hello" + UUID.randomUUID().toString();
    // gotta be unique
    String entity2 = "Goodbye" + UUID.randomUUID().toString();
    CreateEntityOptions createOptions = new CreateEntityOptions.Builder(workspaceId, entity1).build();
    service.createEntity(createOptions).execute().getResult();
    service.createEntity(createOptions.newBuilder().entity(entity2).build()).execute().getResult();
    try {
        ListEntitiesOptions listOptions = new ListEntitiesOptions.Builder(workspaceId).sort("entity").pageLimit(1L).build();
        EntityCollection response = service.listEntities(listOptions).execute().getResult();
        assertNotNull(response);
        assertNotNull(response.getEntities());
        assertNotNull(response.getPagination());
        assertNotNull(response.getPagination().getRefreshUrl());
        assertNotNull(response.getPagination().getNextUrl());
        assertNotNull(response.getPagination().getNextCursor());
        assertTrue(!response.getEntities().get(0).getEntity().equals(entity1));
        Entity ieResponse = null;
        while (response.getPagination().getNextCursor() != null) {
            assertNotNull(response.getEntities());
            assertTrue(response.getEntities().size() == 1);
            if (response.getEntities().get(0).getEntity().equals(entity1)) {
                ieResponse = response.getEntities().get(0);
                break;
            }
            String cursor = response.getPagination().getNextCursor();
            response = service.listEntities(listOptions.newBuilder().cursor(cursor).build()).execute().getResult();
        }
        assertNotNull(ieResponse);
    } catch (Exception ex) {
        fail(ex.getMessage());
    } finally {
        // Clean up
        DeleteEntityOptions deleteOptions = new DeleteEntityOptions.Builder(workspaceId, entity1).build();
        service.deleteEntity(deleteOptions).execute().getResult();
        service.deleteEntity(deleteOptions.newBuilder().entity(entity2).build()).execute().getResult();
    }
}
Also used : Entity(com.ibm.watson.assistant.v1.model.Entity) CreateEntityOptions(com.ibm.watson.assistant.v1.model.CreateEntityOptions) EntityCollection(com.ibm.watson.assistant.v1.model.EntityCollection) DeleteEntityOptions(com.ibm.watson.assistant.v1.model.DeleteEntityOptions) ListEntitiesOptions(com.ibm.watson.assistant.v1.model.ListEntitiesOptions) NotFoundException(com.ibm.cloud.sdk.core.service.exception.NotFoundException) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 69 with Entity

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

the class SynonymsIT method testListSynonyms.

/**
 * Test listSynonyms.
 */
@Test
public void testListSynonyms() {
    String entity = "beverage";
    String entityValue = "coffee";
    String synonym1 = "java";
    String synonym2 = "joe";
    try {
        CreateEntityOptions createOptions = new CreateEntityOptions.Builder(workspaceId, entity).build();
        service.createEntity(createOptions).execute().getResult();
    } 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, entityValue).build();
        service.createValue(createOptions).execute().getResult();
    } catch (Exception ex) {
        // Exception is okay if is for Unique Violation
        assertTrue(ex.getLocalizedMessage().startsWith("Unique Violation"));
    }
    try {
        CreateSynonymOptions createOptions = new CreateSynonymOptions.Builder(workspaceId, entity, entityValue, synonym1).build();
        service.createSynonym(createOptions).execute().getResult();
        service.createSynonym(createOptions.newBuilder().synonym(synonym2).build()).execute().getResult();
    } catch (Exception ex) {
        // Exception is okay if is for Unique Violation
        assertTrue(ex.getLocalizedMessage().startsWith("Unique Violation"));
    }
    try {
        ListSynonymsOptions listOptions = new ListSynonymsOptions.Builder().workspaceId(workspaceId).entity(entity).value(entityValue).build();
        final SynonymCollection response = service.listSynonyms(listOptions).execute().getResult();
        assertNotNull(response);
        assertNotNull(response.getSynonyms());
        assertNotNull(response.getPagination());
        assertNotNull(response.getPagination().getRefreshUrl());
        // nextUrl may be null
        if (response.getPagination().getNextUrl() == null) {
            assertNull(response.getPagination().getNextCursor());
        } else {
            assertNotNull(response.getPagination().getNextCursor());
        }
        assertTrue(response.getSynonyms().size() >= 2);
        // Should not be paginated, but just to be sure
        if (response.getPagination().getNextUrl() == null) {
            // assertTrue(response.getSynonyms().stream().filter(r ->
            // r.getSynonym().equals(synonym1)).count() == 1);
            boolean found1 = false;
            boolean found2 = false;
            for (Synonym synonymResponse : response.getSynonyms()) {
                found1 |= synonymResponse.synonym().equals(synonym1);
                found2 |= synonymResponse.synonym().equals(synonym2);
            }
            assertTrue(found1 && found2);
        }
    } catch (Exception ex) {
        fail(ex.getMessage());
    } finally {
        DeleteSynonymOptions deleteOptions = new DeleteSynonymOptions.Builder(workspaceId, entity, entityValue, synonym1).build();
        service.deleteSynonym(deleteOptions).execute().getResult();
        service.deleteSynonym(deleteOptions.newBuilder().synonym(synonym2).build()).execute().getResult();
    }
}
Also used : DeleteSynonymOptions(com.ibm.watson.assistant.v1.model.DeleteSynonymOptions) CreateEntityOptions(com.ibm.watson.assistant.v1.model.CreateEntityOptions) CreateValueOptions(com.ibm.watson.assistant.v1.model.CreateValueOptions) ListSynonymsOptions(com.ibm.watson.assistant.v1.model.ListSynonymsOptions) NotFoundException(com.ibm.cloud.sdk.core.service.exception.NotFoundException) CreateSynonymOptions(com.ibm.watson.assistant.v1.model.CreateSynonymOptions) Synonym(com.ibm.watson.assistant.v1.model.Synonym) SynonymCollection(com.ibm.watson.assistant.v1.model.SynonymCollection) Test(org.junit.Test)

Example 70 with Entity

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

the class SynonymsIT method testGetSynonym.

/**
 * Test getSynonym.
 */
@Test
public void testGetSynonym() {
    String entity = "beverage";
    String entityValue = "orange juice";
    String synonym = "OJ";
    try {
        CreateEntityOptions createOptions = new CreateEntityOptions.Builder(workspaceId, entity).build();
        service.createEntity(createOptions).execute().getResult();
    } 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, entityValue).build();
        service.createValue(createOptions).execute().getResult();
    } catch (Exception ex) {
        // Exception is okay if is for Unique Violation
        assertTrue(ex.getLocalizedMessage().startsWith("Unique Violation"));
    }
    Date start = new Date();
    CreateSynonymOptions createOptions = new CreateSynonymOptions.Builder(workspaceId, entity, entityValue, synonym).build();
    service.createSynonym(createOptions).execute().getResult();
    try {
        GetSynonymOptions getOptions = new GetSynonymOptions.Builder(workspaceId, entity, entityValue, synonym).includeAudit(true).build();
        Synonym response = service.getSynonym(getOptions).execute().getResult();
        assertNotNull(response);
        assertNotNull(response.synonym());
        assertEquals(response.synonym(), synonym);
        assertNotNull(response.created());
        assertNotNull(response.updated());
        Date now = new Date();
        assertTrue(fuzzyBefore(response.created(), now));
        assertTrue(fuzzyAfter(response.created(), start));
        assertTrue(fuzzyBefore(response.updated(), now));
        assertTrue(fuzzyAfter(response.updated(), start));
    } catch (Exception ex) {
        fail(ex.getMessage());
    } finally {
        DeleteSynonymOptions deleteOptions = new DeleteSynonymOptions.Builder(workspaceId, entity, entityValue, synonym).build();
        service.deleteSynonym(deleteOptions).execute().getResult();
    }
}
Also used : DeleteSynonymOptions(com.ibm.watson.assistant.v1.model.DeleteSynonymOptions) CreateEntityOptions(com.ibm.watson.assistant.v1.model.CreateEntityOptions) CreateValueOptions(com.ibm.watson.assistant.v1.model.CreateValueOptions) GetSynonymOptions(com.ibm.watson.assistant.v1.model.GetSynonymOptions) NotFoundException(com.ibm.cloud.sdk.core.service.exception.NotFoundException) Date(java.util.Date) CreateSynonymOptions(com.ibm.watson.assistant.v1.model.CreateSynonymOptions) Synonym(com.ibm.watson.assistant.v1.model.Synonym) Test(org.junit.Test)

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