Search in sources :

Example 1 with CreateEntityOptions

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

the class ConversationTest method testCreateEntityOptionsBuilder.

/**
 * Test CreateEntityOptions builder.
 */
@Test
public void testCreateEntityOptionsBuilder() {
    String entity = "anEntity";
    CreateValue entityValue0 = new CreateValue.Builder().value("entityValue0").addPattern("pattern0").build();
    CreateValue entityValue1 = new CreateValue.Builder().value("entityValue1").addPattern("pattern1").build();
    CreateEntityOptions createOptions = new CreateEntityOptions.Builder().workspaceId(WORKSPACE_ID).entity(entity).addValue(entityValue0).addValue(entityValue1).build();
    assertEquals(createOptions.workspaceId(), WORKSPACE_ID);
    assertEquals(createOptions.entity(), entity);
    assertEquals(createOptions.values().size(), 2);
    assertEquals(createOptions.values().get(0), entityValue0);
    assertEquals(createOptions.values().get(1), entityValue1);
    CreateEntityOptions.Builder builder = createOptions.newBuilder();
    CreateValue entityValue2 = new CreateValue.Builder().value("entityValue2").addPattern("pattern2").build();
    builder.values(Arrays.asList(entityValue2));
    CreateEntityOptions options2 = builder.build();
    assertEquals(options2.workspaceId(), WORKSPACE_ID);
    assertEquals(options2.entity(), entity);
    assertEquals(options2.values().size(), 1);
    assertEquals(options2.values().get(0), entityValue2);
}
Also used : CreateValue(com.ibm.watson.developer_cloud.conversation.v1.model.CreateValue) CreateEntityOptions(com.ibm.watson.developer_cloud.conversation.v1.model.CreateEntityOptions) WatsonServiceUnitTest(com.ibm.watson.developer_cloud.WatsonServiceUnitTest) Test(org.junit.Test)

Example 2 with CreateEntityOptions

use of com.ibm.watson.developer_cloud.assistant.v1.model.CreateEntityOptions 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();
    service.createEntity(createOptions.newBuilder().entity(entity2).build()).execute();
    try {
        ListEntitiesOptions listOptions = new ListEntitiesOptions.Builder(workspaceId).sort("entity").pageLimit(1L).build();
        EntityCollection response = service.listEntities(listOptions).execute();
        assertNotNull(response);
        assertNotNull(response.getEntities());
        assertNotNull(response.getPagination());
        assertNotNull(response.getPagination().getRefreshUrl());
        assertNotNull(response.getPagination().getNextUrl());
        assertNotNull(response.getPagination().getCursor());
        assertTrue(!response.getEntities().get(0).getEntityName().equals(entity1));
        EntityExport ieResponse = null;
        while (response.getPagination().getCursor() != null) {
            assertNotNull(response.getEntities());
            assertTrue(response.getEntities().size() == 1);
            if (response.getEntities().get(0).getEntityName().equals(entity1)) {
                ieResponse = response.getEntities().get(0);
                break;
            }
            String cursor = response.getPagination().getCursor();
            response = service.listEntities(listOptions.newBuilder().cursor(cursor).build()).execute();
        }
        assertNotNull(ieResponse);
    } catch (Exception ex) {
        fail(ex.getMessage());
    } finally {
        // Clean up
        DeleteEntityOptions deleteOptions = new DeleteEntityOptions.Builder(workspaceId, entity1).build();
        service.deleteEntity(deleteOptions).execute();
        service.deleteEntity(deleteOptions.newBuilder().entity(entity2).build()).execute();
    }
}
Also used : EntityExport(com.ibm.watson.developer_cloud.conversation.v1.model.EntityExport) CreateEntityOptions(com.ibm.watson.developer_cloud.conversation.v1.model.CreateEntityOptions) EntityCollection(com.ibm.watson.developer_cloud.conversation.v1.model.EntityCollection) DeleteEntityOptions(com.ibm.watson.developer_cloud.conversation.v1.model.DeleteEntityOptions) ListEntitiesOptions(com.ibm.watson.developer_cloud.conversation.v1.model.ListEntitiesOptions) NotFoundException(com.ibm.watson.developer_cloud.service.exception.NotFoundException) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 3 with CreateEntityOptions

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

the class SynonymsIT method testUpdateSynonym.

/**
 * Test updateSynonym.
 */
@Test
public void testUpdateSynonym() {
    String entity = "beverage";
    String entityValue = "coffee";
    String synonym1 = "joe";
    String synonym2 = "mud";
    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, entityValue).build();
        service.createValue(createOptions).execute();
    } 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();
    } catch (Exception ex) {
        // Exception is okay if is for Unique Violation
        assertTrue(ex.getLocalizedMessage().startsWith("Unique Violation"));
    }
    UpdateSynonymOptions updateOptions = new UpdateSynonymOptions.Builder().workspaceId(workspaceId).entity(entity).value(entityValue).synonym(synonym1).newSynonym(synonym2).build();
    Synonym response = service.updateSynonym(updateOptions).execute();
    try {
        assertNotNull(response);
        assertNotNull(response.getSynonymText());
        assertEquals(response.getSynonymText(), synonym2);
    } catch (Exception ex) {
        fail(ex.getMessage());
    } finally {
        // Clean up
        DeleteSynonymOptions deleteOptions = new DeleteSynonymOptions.Builder(workspaceId, entity, entityValue, synonym2).build();
        service.deleteSynonym(deleteOptions).execute();
    }
}
Also used : DeleteSynonymOptions(com.ibm.watson.developer_cloud.conversation.v1.model.DeleteSynonymOptions) CreateEntityOptions(com.ibm.watson.developer_cloud.conversation.v1.model.CreateEntityOptions) CreateValueOptions(com.ibm.watson.developer_cloud.conversation.v1.model.CreateValueOptions) UpdateSynonymOptions(com.ibm.watson.developer_cloud.conversation.v1.model.UpdateSynonymOptions) NotFoundException(com.ibm.watson.developer_cloud.service.exception.NotFoundException) CreateSynonymOptions(com.ibm.watson.developer_cloud.conversation.v1.model.CreateSynonymOptions) Synonym(com.ibm.watson.developer_cloud.conversation.v1.model.Synonym) Test(org.junit.Test)

Example 4 with CreateEntityOptions

use of com.ibm.watson.developer_cloud.assistant.v1.model.CreateEntityOptions 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();
    } 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();
    } 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();
    try {
        GetSynonymOptions getOptions = new GetSynonymOptions.Builder(workspaceId, entity, entityValue, synonym).includeAudit(true).build();
        Synonym response = service.getSynonym(getOptions).execute();
        assertNotNull(response);
        assertNotNull(response.getSynonymText());
        assertEquals(response.getSynonymText(), synonym);
        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));
    } catch (Exception ex) {
        fail(ex.getMessage());
    } finally {
        DeleteSynonymOptions deleteOptions = new DeleteSynonymOptions.Builder(workspaceId, entity, entityValue, synonym).build();
        service.deleteSynonym(deleteOptions).execute();
    }
}
Also used : DeleteSynonymOptions(com.ibm.watson.developer_cloud.conversation.v1.model.DeleteSynonymOptions) CreateEntityOptions(com.ibm.watson.developer_cloud.conversation.v1.model.CreateEntityOptions) CreateValueOptions(com.ibm.watson.developer_cloud.conversation.v1.model.CreateValueOptions) GetSynonymOptions(com.ibm.watson.developer_cloud.conversation.v1.model.GetSynonymOptions) NotFoundException(com.ibm.watson.developer_cloud.service.exception.NotFoundException) Date(java.util.Date) CreateSynonymOptions(com.ibm.watson.developer_cloud.conversation.v1.model.CreateSynonymOptions) Synonym(com.ibm.watson.developer_cloud.conversation.v1.model.Synonym) Test(org.junit.Test)

Example 5 with CreateEntityOptions

use of com.ibm.watson.developer_cloud.assistant.v1.model.CreateEntityOptions 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();
    } 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();
    } 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();
        service.createSynonym(createOptions.newBuilder().synonym(synonym2).build()).execute();
    } 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();
        assertNotNull(response);
        assertNotNull(response.getSynonyms());
        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.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, found2 = false;
            for (Synonym synonymResponse : response.getSynonyms()) {
                found1 |= synonymResponse.getSynonymText().equals(synonym1);
                found2 |= synonymResponse.getSynonymText().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();
        service.deleteSynonym(deleteOptions.newBuilder().synonym(synonym2).build()).execute();
    }
}
Also used : DeleteSynonymOptions(com.ibm.watson.developer_cloud.conversation.v1.model.DeleteSynonymOptions) CreateEntityOptions(com.ibm.watson.developer_cloud.conversation.v1.model.CreateEntityOptions) CreateValueOptions(com.ibm.watson.developer_cloud.conversation.v1.model.CreateValueOptions) ListSynonymsOptions(com.ibm.watson.developer_cloud.conversation.v1.model.ListSynonymsOptions) NotFoundException(com.ibm.watson.developer_cloud.service.exception.NotFoundException) CreateSynonymOptions(com.ibm.watson.developer_cloud.conversation.v1.model.CreateSynonymOptions) Synonym(com.ibm.watson.developer_cloud.conversation.v1.model.Synonym) SynonymCollection(com.ibm.watson.developer_cloud.conversation.v1.model.SynonymCollection) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)32 NotFoundException (com.ibm.watson.developer_cloud.service.exception.NotFoundException)30 CreateEntityOptions (com.ibm.watson.developer_cloud.assistant.v1.model.CreateEntityOptions)16 CreateEntityOptions (com.ibm.watson.developer_cloud.conversation.v1.model.CreateEntityOptions)16 CreateValueOptions (com.ibm.watson.developer_cloud.assistant.v1.model.CreateValueOptions)12 CreateValueOptions (com.ibm.watson.developer_cloud.conversation.v1.model.CreateValueOptions)12 CreateSynonymOptions (com.ibm.watson.developer_cloud.assistant.v1.model.CreateSynonymOptions)6 DeleteSynonymOptions (com.ibm.watson.developer_cloud.assistant.v1.model.DeleteSynonymOptions)6 DeleteValueOptions (com.ibm.watson.developer_cloud.assistant.v1.model.DeleteValueOptions)6 CreateSynonymOptions (com.ibm.watson.developer_cloud.conversation.v1.model.CreateSynonymOptions)6 DeleteSynonymOptions (com.ibm.watson.developer_cloud.conversation.v1.model.DeleteSynonymOptions)6 DeleteValueOptions (com.ibm.watson.developer_cloud.conversation.v1.model.DeleteValueOptions)6 Synonym (com.ibm.watson.developer_cloud.assistant.v1.model.Synonym)5 Synonym (com.ibm.watson.developer_cloud.conversation.v1.model.Synonym)5 Ignore (org.junit.Ignore)4 DeleteEntityOptions (com.ibm.watson.developer_cloud.assistant.v1.model.DeleteEntityOptions)3 GetValueOptions (com.ibm.watson.developer_cloud.assistant.v1.model.GetValueOptions)3 Value (com.ibm.watson.developer_cloud.assistant.v1.model.Value)3 ValueExport (com.ibm.watson.developer_cloud.assistant.v1.model.ValueExport)3 DeleteEntityOptions (com.ibm.watson.developer_cloud.conversation.v1.model.DeleteEntityOptions)3