Search in sources :

Example 31 with CreateEntityOptions

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

the class SynonymsIT method testDeleteSynonym.

/**
 * Test deleteSynonym.
 */
@Test
public void testDeleteSynonym() {
    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"));
    }
    CreateSynonymOptions createOptions = new CreateSynonymOptions.Builder(workspaceId, entity, entityValue, synonym).build();
    Synonym response = service.createSynonym(createOptions).execute();
    try {
        assertNotNull(response);
        assertNotNull(response.getSynonymText());
        assertEquals(response.getSynonymText(), synonym);
    } catch (Exception ex) {
        DeleteSynonymOptions deleteOptions = new DeleteSynonymOptions.Builder(workspaceId, entity, entityValue, synonym).build();
        service.deleteSynonym(deleteOptions).execute();
        fail(ex.getMessage());
    }
    DeleteSynonymOptions deleteOptions = new DeleteSynonymOptions.Builder().workspaceId(workspaceId).entity(entity).value(entityValue).synonym(synonym).build();
    service.deleteSynonym(deleteOptions).execute();
    try {
        GetSynonymOptions getOptions = new GetSynonymOptions.Builder(workspaceId, entity, entityValue, synonym).build();
        service.getSynonym(getOptions).execute();
        fail("deleteSynonym failed");
    } catch (Exception ex) {
        // Expected result
        assertTrue(ex instanceof NotFoundException);
    }
}
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) 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 32 with CreateEntityOptions

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

the class ValuesIT method testGetValue.

/**
 * Test getValue.
 */
@Test
public void testGetValue() {
    String entity = "beverage";
    String entityValue = "coffee" + UUID.randomUUID().toString();
    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"));
    }
    CreateValueOptions createOptions = new CreateValueOptions.Builder(workspaceId, entity, entityValue).synonyms(new ArrayList<String>(Arrays.asList(synonym1, synonym2))).build();
    service.createValue(createOptions).execute();
    Date start = new Date();
    try {
        GetValueOptions getOptions = new GetValueOptions.Builder(workspaceId, entity, entityValue).export(true).includeAudit(true).build();
        ValueExport response = service.getValue(getOptions).execute();
        assertNotNull(response);
        assertNotNull(response.getValueText());
        assertEquals(response.getValueText(), entityValue);
        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));
        assertNotNull(response.getSynonyms());
        assertTrue(response.getSynonyms().size() == 2);
        assertTrue(response.getSynonyms().contains(synonym1));
        assertTrue(response.getSynonyms().contains(synonym2));
    } catch (Exception ex) {
        fail(ex.getMessage());
    } finally {
        DeleteValueOptions deleteOptions = new DeleteValueOptions.Builder(workspaceId, entity, entityValue).build();
        service.deleteValue(deleteOptions).execute();
    }
}
Also used : GetValueOptions(com.ibm.watson.developer_cloud.conversation.v1.model.GetValueOptions) 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) ArrayList(java.util.ArrayList) NotFoundException(com.ibm.watson.developer_cloud.service.exception.NotFoundException) Date(java.util.Date) ValueExport(com.ibm.watson.developer_cloud.conversation.v1.model.ValueExport) Test(org.junit.Test)

Example 33 with CreateEntityOptions

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

the class ValuesIT method testListValues.

/**
 * Test listValues.
 */
@Test
public void testListValues() {
    String entity = "beverage";
    String entityValue1 = "coffee";
    String entityValue2 = "orange juice";
    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"));
    }
    CreateValueOptions createOptions = new CreateValueOptions.Builder(workspaceId, entity, entityValue1).build();
    try {
        service.createValue(createOptions).execute();
    } catch (Exception ex) {
        // Exception is okay if is for Unique Violation
        assertTrue(ex.getLocalizedMessage().startsWith("Unique Violation"));
    }
    try {
        service.createValue(createOptions.newBuilder().value(entityValue2).build()).execute();
    } catch (Exception ex) {
        // Exception is okay if is for Unique Violation
        assertTrue(ex.getLocalizedMessage().startsWith("Unique Violation"));
    }
    try {
        ListValuesOptions listOptions = new ListValuesOptions.Builder().workspaceId(workspaceId).entity(entity).build();
        final ValueCollection response = service.listValues(listOptions).execute();
        assertNotNull(response);
        assertNotNull(response.getValues());
        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.getValues().size() >= 2);
        // Should not be paginated, but just to be sure
        if (response.getPagination().getNextUrl() == null) {
            // assertTrue(response.getValues().stream().filter(r -> r.getValue().equals(synonym1)).count() == 1);
            boolean found1 = false, found2 = false;
            for (ValueExport valueResponse : response.getValues()) {
                found1 |= valueResponse.getValueText().equals(entityValue1);
                found2 |= valueResponse.getValueText().equals(entityValue2);
            }
            assertTrue(found1 && found2);
        }
    } catch (Exception ex) {
        fail(ex.getMessage());
    } finally {
        DeleteValueOptions deleteOptions = new DeleteValueOptions.Builder(workspaceId, entity, entityValue1).build();
        service.deleteValue(deleteOptions).execute();
        service.deleteValue(deleteOptions.newBuilder().value(entityValue2).build()).execute();
    }
}
Also used : CreateEntityOptions(com.ibm.watson.developer_cloud.conversation.v1.model.CreateEntityOptions) DeleteValueOptions(com.ibm.watson.developer_cloud.conversation.v1.model.DeleteValueOptions) ListValuesOptions(com.ibm.watson.developer_cloud.conversation.v1.model.ListValuesOptions) CreateValueOptions(com.ibm.watson.developer_cloud.conversation.v1.model.CreateValueOptions) NotFoundException(com.ibm.watson.developer_cloud.service.exception.NotFoundException) ValueExport(com.ibm.watson.developer_cloud.conversation.v1.model.ValueExport) ValueCollection(com.ibm.watson.developer_cloud.conversation.v1.model.ValueCollection) Test(org.junit.Test)

Example 34 with CreateEntityOptions

use of com.ibm.watson.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.assistant.v1.model.EntityExport) CreateEntityOptions(com.ibm.watson.developer_cloud.assistant.v1.model.CreateEntityOptions) EntityCollection(com.ibm.watson.developer_cloud.assistant.v1.model.EntityCollection) DeleteEntityOptions(com.ibm.watson.developer_cloud.assistant.v1.model.DeleteEntityOptions) ListEntitiesOptions(com.ibm.watson.developer_cloud.assistant.v1.model.ListEntitiesOptions) NotFoundException(com.ibm.watson.developer_cloud.service.exception.NotFoundException) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 35 with CreateEntityOptions

use of com.ibm.watson.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.assistant.v1.model.DeleteSynonymOptions) CreateEntityOptions(com.ibm.watson.developer_cloud.assistant.v1.model.CreateEntityOptions) CreateValueOptions(com.ibm.watson.developer_cloud.assistant.v1.model.CreateValueOptions) UpdateSynonymOptions(com.ibm.watson.developer_cloud.assistant.v1.model.UpdateSynonymOptions) NotFoundException(com.ibm.watson.developer_cloud.service.exception.NotFoundException) CreateSynonymOptions(com.ibm.watson.developer_cloud.assistant.v1.model.CreateSynonymOptions) Synonym(com.ibm.watson.developer_cloud.assistant.v1.model.Synonym) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)47 NotFoundException (com.ibm.watson.developer_cloud.service.exception.NotFoundException)30 CreateEntityOptions (com.ibm.watson.assistant.v1.model.CreateEntityOptions)16 CreateEntityOptions (com.ibm.watson.developer_cloud.assistant.v1.model.CreateEntityOptions)16 CreateEntityOptions (com.ibm.watson.developer_cloud.conversation.v1.model.CreateEntityOptions)16 NotFoundException (com.ibm.cloud.sdk.core.service.exception.NotFoundException)15 CreateValueOptions (com.ibm.watson.assistant.v1.model.CreateValueOptions)12 CreateValueOptions (com.ibm.watson.developer_cloud.assistant.v1.model.CreateValueOptions)12 CreateValueOptions (com.ibm.watson.developer_cloud.conversation.v1.model.CreateValueOptions)12 HashMap (java.util.HashMap)7 CreateSynonymOptions (com.ibm.watson.assistant.v1.model.CreateSynonymOptions)6 DeleteSynonymOptions (com.ibm.watson.assistant.v1.model.DeleteSynonymOptions)6 DeleteValueOptions (com.ibm.watson.assistant.v1.model.DeleteValueOptions)6 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 Date (java.util.Date)6