Search in sources :

Example 11 with CreateValueOptions

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

the class ValuesIT method testDeleteValue.

/**
 * Test deleteValue.
 */
@Test
public void testDeleteValue() {
    String entity = "beverage";
    String entityValue = "coffee" + UUID.randomUUID().toString();
    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).build();
    Value response = service.createValue(createOptions).execute();
    try {
        assertNotNull(response);
        assertNotNull(response.getValueText());
        assertEquals(response.getValueText(), entityValue);
        assertNull(response.getMetadata());
    } catch (Exception ex) {
        // Clean up
        DeleteValueOptions deleteOptions = new DeleteValueOptions.Builder(workspaceId, entity, entityValue).build();
        service.deleteValue(deleteOptions).execute();
        fail(ex.getMessage());
    }
    DeleteValueOptions deleteOptions = new DeleteValueOptions.Builder().workspaceId(workspaceId).entity(entity).value(entityValue).build();
    service.deleteValue(deleteOptions).execute();
    try {
        GetValueOptions getOptions = new GetValueOptions.Builder(workspaceId, entity, entityValue).build();
        service.getValue(getOptions).execute();
        fail("deleteValue failed");
    } catch (Exception ex) {
        // Expected result
        assertTrue(ex instanceof NotFoundException);
    }
}
Also used : GetValueOptions(com.ibm.watson.developer_cloud.assistant.v1.model.GetValueOptions) 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) Value(com.ibm.watson.developer_cloud.assistant.v1.model.Value) NotFoundException(com.ibm.watson.developer_cloud.service.exception.NotFoundException) NotFoundException(com.ibm.watson.developer_cloud.service.exception.NotFoundException) Test(org.junit.Test)

Example 12 with CreateValueOptions

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

the class ValuesIT method testListValuesWithPaging.

/**
 * Test listValues with pagination.
 */
@Test
public void testListValuesWithPaging() {
    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.Builder listOptionsBuilder = new ListValuesOptions.Builder(workspaceId, entity);
        listOptionsBuilder.sort("updated");
        listOptionsBuilder.pageLimit(1L);
        listOptionsBuilder.export(true);
        ValueCollection response = service.listValues(listOptionsBuilder.build()).execute();
        assertNotNull(response);
        assertNotNull(response.getPagination());
        assertNotNull(response.getPagination().getRefreshUrl());
        assertNotNull(response.getPagination().getNextUrl());
        assertNotNull(response.getPagination().getCursor());
        boolean found1 = false, found2 = false;
        while (true) {
            assertNotNull(response.getValues());
            assertTrue(response.getValues().size() == 1);
            found1 |= response.getValues().get(0).getValueText().equals(entityValue1);
            found2 |= response.getValues().get(0).getValueText().equals(entityValue2);
            if (response.getPagination().getCursor() == null) {
                break;
            }
            String cursor = response.getPagination().getCursor();
            response = service.listValues(listOptionsBuilder.cursor(cursor).build()).execute();
        }
        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) ValueCollection(com.ibm.watson.developer_cloud.assistant.v1.model.ValueCollection) Test(org.junit.Test)

Example 13 with CreateValueOptions

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

the class ValuesIT method testCreateValue.

/**
 * Test createValue.
 */
@Test
public void testCreateValue() {
    String entity = "beverage";
    String entityValue = "coffee" + UUID.randomUUID().toString();
    // metadata
    Map<String, Object> valueMetadata = new HashMap<String, Object>();
    String metadataValue = "value for " + entityValue;
    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"));
    }
    CreateValueOptions createOptions = new CreateValueOptions.Builder().workspaceId(workspaceId).entity(entity).value(entityValue).metadata(valueMetadata).build();
    Value response = service.createValue(createOptions).execute();
    try {
        assertNotNull(response);
        assertNotNull(response.getValueText());
        assertEquals(response.getValueText(), entityValue);
        assertNotNull(response.getMetadata());
        // 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, entityValue).build();
        service.deleteValue(deleteOptions).execute();
    }
}
Also used : 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) Value(com.ibm.watson.developer_cloud.assistant.v1.model.Value) NotFoundException(com.ibm.watson.developer_cloud.service.exception.NotFoundException) Test(org.junit.Test)

Example 14 with CreateValueOptions

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

the class AssistantTest method testCreateValueOptionsBuilder.

/**
 * Test CreateValueOptions builder.
 */
@Test
public void testCreateValueOptionsBuilder() {
    String entity = "anEntity";
    String value = "aValue";
    String valueSynonym0 = "valueSynonym0";
    String valueSynonym1 = "valueSynonym1";
    String valuePattern0 = "valuePattern0";
    String valuePattern1 = "valuePattern1";
    String valueType = "patterns";
    CreateValueOptions createOptions = new CreateValueOptions.Builder().workspaceId(WORKSPACE_ID).entity(entity).value(value).addSynonym(valueSynonym0).addSynonym(valueSynonym1).addPattern(valuePattern0).addPattern(valuePattern1).valueType(valueType).build();
    assertEquals(createOptions.workspaceId(), WORKSPACE_ID);
    assertEquals(createOptions.entity(), entity);
    assertEquals(createOptions.value(), value);
    assertEquals(createOptions.synonyms().size(), 2);
    assertEquals(createOptions.synonyms().get(0), valueSynonym0);
    assertEquals(createOptions.synonyms().get(1), valueSynonym1);
    assertEquals(createOptions.patterns().size(), 2);
    assertEquals(createOptions.patterns().get(0), valuePattern0);
    assertEquals(createOptions.patterns().get(1), valuePattern1);
    assertEquals(createOptions.valueType(), valueType);
    CreateValueOptions.Builder builder = createOptions.newBuilder();
    String valueSynonym2 = "valueSynonym2";
    String valuePattern2 = "valuePattern2";
    builder.synonyms(Arrays.asList(valueSynonym2));
    builder.patterns(Arrays.asList(valuePattern2));
    CreateValueOptions options2 = builder.build();
    assertEquals(options2.workspaceId(), WORKSPACE_ID);
    assertEquals(options2.entity(), entity);
    assertEquals(options2.value(), value);
    assertEquals(options2.synonyms().size(), 1);
    assertEquals(options2.synonyms().get(0), valueSynonym2);
    assertEquals(options2.patterns().size(), 1);
    assertEquals(options2.patterns().get(0), valuePattern2);
}
Also used : CreateValueOptions(com.ibm.watson.developer_cloud.assistant.v1.model.CreateValueOptions) WatsonServiceUnitTest(com.ibm.watson.developer_cloud.WatsonServiceUnitTest) Test(org.junit.Test)

Example 15 with CreateValueOptions

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

the class ConversationTest method testCreateValueOptionsBuilder.

/**
 * Test CreateValueOptions builder.
 */
@Test
public void testCreateValueOptionsBuilder() {
    String entity = "anEntity";
    String value = "aValue";
    String valueSynonym0 = "valueSynonym0";
    String valueSynonym1 = "valueSynonym1";
    String valuePattern0 = "valuePattern0";
    String valuePattern1 = "valuePattern1";
    String valueType = "patterns";
    CreateValueOptions createOptions = new CreateValueOptions.Builder().workspaceId(WORKSPACE_ID).entity(entity).value(value).addSynonym(valueSynonym0).addSynonym(valueSynonym1).addPattern(valuePattern0).addPattern(valuePattern1).valueType(valueType).build();
    assertEquals(createOptions.workspaceId(), WORKSPACE_ID);
    assertEquals(createOptions.entity(), entity);
    assertEquals(createOptions.value(), value);
    assertEquals(createOptions.synonyms().size(), 2);
    assertEquals(createOptions.synonyms().get(0), valueSynonym0);
    assertEquals(createOptions.synonyms().get(1), valueSynonym1);
    assertEquals(createOptions.patterns().size(), 2);
    assertEquals(createOptions.patterns().get(0), valuePattern0);
    assertEquals(createOptions.patterns().get(1), valuePattern1);
    assertEquals(createOptions.valueType(), valueType);
    CreateValueOptions.Builder builder = createOptions.newBuilder();
    String valueSynonym2 = "valueSynonym2";
    String valuePattern2 = "valuePattern2";
    builder.synonyms(Arrays.asList(valueSynonym2));
    builder.patterns(Arrays.asList(valuePattern2));
    CreateValueOptions options2 = builder.build();
    assertEquals(options2.workspaceId(), WORKSPACE_ID);
    assertEquals(options2.entity(), entity);
    assertEquals(options2.value(), value);
    assertEquals(options2.synonyms().size(), 1);
    assertEquals(options2.synonyms().get(0), valueSynonym2);
    assertEquals(options2.patterns().size(), 1);
    assertEquals(options2.patterns().get(0), valuePattern2);
}
Also used : CreateValueOptions(com.ibm.watson.developer_cloud.conversation.v1.model.CreateValueOptions) WatsonServiceUnitTest(com.ibm.watson.developer_cloud.WatsonServiceUnitTest) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)26 NotFoundException (com.ibm.watson.developer_cloud.service.exception.NotFoundException)24 CreateValueOptions (com.ibm.watson.developer_cloud.assistant.v1.model.CreateValueOptions)13 CreateValueOptions (com.ibm.watson.developer_cloud.conversation.v1.model.CreateValueOptions)13 CreateEntityOptions (com.ibm.watson.developer_cloud.assistant.v1.model.CreateEntityOptions)12 CreateEntityOptions (com.ibm.watson.developer_cloud.conversation.v1.model.CreateEntityOptions)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 Value (com.ibm.watson.developer_cloud.assistant.v1.model.Value)4 Date (java.util.Date)4 HashMap (java.util.HashMap)4 GetValueOptions (com.ibm.watson.developer_cloud.assistant.v1.model.GetValueOptions)3 ValueExport (com.ibm.watson.developer_cloud.assistant.v1.model.ValueExport)3 GetValueOptions (com.ibm.watson.developer_cloud.conversation.v1.model.GetValueOptions)3