Search in sources :

Example 31 with CreateEntityOptions

use of com.ibm.watson.developer_cloud.conversation.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.assistant.v1.model.DeleteSynonymOptions) CreateEntityOptions(com.ibm.watson.developer_cloud.assistant.v1.model.CreateEntityOptions) CreateValueOptions(com.ibm.watson.developer_cloud.assistant.v1.model.CreateValueOptions) ListSynonymsOptions(com.ibm.watson.developer_cloud.assistant.v1.model.ListSynonymsOptions) 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) SynonymCollection(com.ibm.watson.developer_cloud.assistant.v1.model.SynonymCollection) Test(org.junit.Test)

Example 32 with CreateEntityOptions

use of com.ibm.watson.developer_cloud.conversation.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.assistant.v1.model.CreateEntityOptions) DeleteValueOptions(com.ibm.watson.developer_cloud.assistant.v1.model.DeleteValueOptions) ListValuesOptions(com.ibm.watson.developer_cloud.assistant.v1.model.ListValuesOptions) CreateValueOptions(com.ibm.watson.developer_cloud.assistant.v1.model.CreateValueOptions) NotFoundException(com.ibm.watson.developer_cloud.service.exception.NotFoundException) ValueExport(com.ibm.watson.developer_cloud.assistant.v1.model.ValueExport) ValueCollection(com.ibm.watson.developer_cloud.assistant.v1.model.ValueCollection) Test(org.junit.Test)

Example 33 with CreateEntityOptions

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

the class ValuesIT method testUpdateValue.

/**
 * Test updateValue.
 */
@Test
public void testUpdateValue() {
    String entity = "beverage";
    String entityValue1 = "coffee" + UUID.randomUUID().toString();
    String entityValue2 = "coffee" + UUID.randomUUID().toString();
    String synonym1 = "java";
    String synonym2 = "joe";
    // metadata
    Map<String, Object> valueMetadata = new HashMap<String, Object>();
    String metadataValue = "value for " + entityValue2;
    valueMetadata.put("key", metadataValue);
    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, entityValue1).build();
        service.createValue(createOptions).execute();
    } catch (Exception ex) {
        // Exception is okay if is for Unique Violation
        assertTrue(ex.getLocalizedMessage().startsWith("Unique Violation"));
    }
    UpdateValueOptions updateOptions = new UpdateValueOptions.Builder().workspaceId(workspaceId).entity(entity).value(entityValue1).newValue(entityValue2).newSynonyms(new ArrayList<String>(Arrays.asList(synonym1, synonym2))).newMetadata(valueMetadata).build();
    Value response = service.updateValue(updateOptions).execute();
    try {
        assertNotNull(response);
        assertNotNull(response.getValueText());
        assertEquals(response.getValueText(), entityValue2);
        GetValueOptions getOptions = new GetValueOptions.Builder(workspaceId, entity, entityValue2).export(true).includeAudit(true).build();
        ValueExport vResponse = service.getValue(getOptions).execute();
        assertNotNull(vResponse);
        assertNotNull(vResponse.getValueText());
        assertEquals(vResponse.getValueText(), entityValue2);
        assertNotNull(vResponse.getCreated());
        assertNotNull(vResponse.getUpdated());
        assertNotNull(vResponse.getSynonyms());
        assertTrue(vResponse.getSynonyms().size() == 2);
        assertTrue(vResponse.getSynonyms().contains(synonym1));
        assertTrue(vResponse.getSynonyms().contains(synonym2));
        // metadata
        assertNotNull(response.getMetadata());
        assertNotNull(response.getMetadata().get("key"));
        assertEquals(response.getMetadata().get("key"), metadataValue);
    } catch (Exception ex) {
        fail(ex.getMessage());
    } finally {
        // Clean up
        DeleteValueOptions deleteOptions = new DeleteValueOptions.Builder(workspaceId, entity, entityValue2).build();
        service.deleteValue(deleteOptions).execute();
    }
}
Also used : GetValueOptions(com.ibm.watson.developer_cloud.assistant.v1.model.GetValueOptions) HashMap(java.util.HashMap) CreateEntityOptions(com.ibm.watson.developer_cloud.assistant.v1.model.CreateEntityOptions) DeleteValueOptions(com.ibm.watson.developer_cloud.assistant.v1.model.DeleteValueOptions) CreateValueOptions(com.ibm.watson.developer_cloud.assistant.v1.model.CreateValueOptions) UpdateValueOptions(com.ibm.watson.developer_cloud.assistant.v1.model.UpdateValueOptions) NotFoundException(com.ibm.watson.developer_cloud.service.exception.NotFoundException) ValueExport(com.ibm.watson.developer_cloud.assistant.v1.model.ValueExport) Value(com.ibm.watson.developer_cloud.assistant.v1.model.Value) 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