Search in sources :

Example 1 with UpdateValueOptions

use of com.ibm.watson.assistant.v1.model.UpdateValueOptions 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.conversation.v1.model.GetValueOptions) HashMap(java.util.HashMap) 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) UpdateValueOptions(com.ibm.watson.developer_cloud.conversation.v1.model.UpdateValueOptions) NotFoundException(com.ibm.watson.developer_cloud.service.exception.NotFoundException) ValueExport(com.ibm.watson.developer_cloud.conversation.v1.model.ValueExport) Value(com.ibm.watson.developer_cloud.conversation.v1.model.Value) Test(org.junit.Test)

Example 2 with UpdateValueOptions

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

the class AssistantTest method testUpdateValueOptionsBuilder.

/**
 * Test UpdateValueOptions builder.
 */
@Test
public void testUpdateValueOptionsBuilder() {
    String entity = "anEntity";
    String value = "aValue";
    String newValue = "renamedValue";
    String valueSynonym0 = "valueSynonym0";
    String valueSynonym1 = "valueSynonym1";
    UpdateValueOptions updateOptions = new UpdateValueOptions.Builder().workspaceId(WORKSPACE_ID).entity(entity).value(value).newValue(newValue).addSynonym(valueSynonym0).addSynonym(valueSynonym1).build();
    assertEquals(updateOptions.workspaceId(), WORKSPACE_ID);
    assertEquals(updateOptions.entity(), entity);
    assertEquals(updateOptions.newValue(), newValue);
    assertEquals(updateOptions.newSynonyms().size(), 2);
    assertEquals(updateOptions.newSynonyms().get(0), valueSynonym0);
    assertEquals(updateOptions.newSynonyms().get(1), valueSynonym1);
    UpdateValueOptions.Builder builder = updateOptions.newBuilder();
    String valueSynonym2 = "valueSynonym2";
    builder.newSynonyms(Arrays.asList(valueSynonym2));
    UpdateValueOptions options2 = builder.build();
    assertEquals(options2.workspaceId(), WORKSPACE_ID);
    assertEquals(options2.entity(), entity);
    assertEquals(options2.newValue(), newValue);
    assertEquals(options2.newSynonyms().size(), 1);
    assertEquals(options2.newSynonyms().get(0), valueSynonym2);
}
Also used : UpdateValueOptions(com.ibm.watson.developer_cloud.assistant.v1.model.UpdateValueOptions) WatsonServiceUnitTest(com.ibm.watson.developer_cloud.WatsonServiceUnitTest) Test(org.junit.Test)

Example 3 with UpdateValueOptions

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

the class Assistant method updateValue.

/**
 * Update entity value.
 *
 * <p>Update an existing entity value with new or modified data. You must provide component
 * objects defining the content of the updated entity value.
 *
 * <p>If you want to update multiple entity values with a single API call, consider using the
 * **[Update entity](#update-entity)** method instead.
 *
 * @param updateValueOptions the {@link UpdateValueOptions} containing the options for the call
 * @return a {@link ServiceCall} with a result of type {@link Value}
 */
public ServiceCall<Value> updateValue(UpdateValueOptions updateValueOptions) {
    com.ibm.cloud.sdk.core.util.Validator.notNull(updateValueOptions, "updateValueOptions cannot be null");
    Map<String, String> pathParamsMap = new HashMap<String, String>();
    pathParamsMap.put("workspace_id", updateValueOptions.workspaceId());
    pathParamsMap.put("entity", updateValueOptions.entity());
    pathParamsMap.put("value", updateValueOptions.value());
    RequestBuilder builder = RequestBuilder.post(RequestBuilder.resolveRequestUrl(getServiceUrl(), "/v1/workspaces/{workspace_id}/entities/{entity}/values/{value}", pathParamsMap));
    Map<String, String> sdkHeaders = SdkCommon.getSdkHeaders("conversation", "v1", "updateValue");
    for (Entry<String, String> header : sdkHeaders.entrySet()) {
        builder.header(header.getKey(), header.getValue());
    }
    builder.header("Accept", "application/json");
    builder.query("version", String.valueOf(this.version));
    if (updateValueOptions.append() != null) {
        builder.query("append", String.valueOf(updateValueOptions.append()));
    }
    if (updateValueOptions.includeAudit() != null) {
        builder.query("include_audit", String.valueOf(updateValueOptions.includeAudit()));
    }
    final JsonObject contentJson = new JsonObject();
    if (updateValueOptions.newValue() != null) {
        contentJson.addProperty("value", updateValueOptions.newValue());
    }
    if (updateValueOptions.newMetadata() != null) {
        contentJson.add("metadata", com.ibm.cloud.sdk.core.util.GsonSingleton.getGson().toJsonTree(updateValueOptions.newMetadata()));
    }
    if (updateValueOptions.newType() != null) {
        contentJson.addProperty("type", updateValueOptions.newType());
    }
    if (updateValueOptions.newSynonyms() != null) {
        contentJson.add("synonyms", com.ibm.cloud.sdk.core.util.GsonSingleton.getGson().toJsonTree(updateValueOptions.newSynonyms()));
    }
    if (updateValueOptions.newPatterns() != null) {
        contentJson.add("patterns", com.ibm.cloud.sdk.core.util.GsonSingleton.getGson().toJsonTree(updateValueOptions.newPatterns()));
    }
    builder.bodyJson(contentJson);
    ResponseConverter<Value> responseConverter = ResponseConverterUtils.getValue(new com.google.gson.reflect.TypeToken<Value>() {
    }.getType());
    return createServiceCall(builder.build(), responseConverter);
}
Also used : RequestBuilder(com.ibm.cloud.sdk.core.http.RequestBuilder) HashMap(java.util.HashMap) Value(com.ibm.watson.assistant.v1.model.Value) JsonObject(com.google.gson.JsonObject)

Example 4 with UpdateValueOptions

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

the class ConversationTest method testUpdateValueOptionsBuilder.

/**
 * Test UpdateValueOptions builder.
 */
@Test
public void testUpdateValueOptionsBuilder() {
    String entity = "anEntity";
    String value = "aValue";
    String newValue = "renamedValue";
    String valueSynonym0 = "valueSynonym0";
    String valueSynonym1 = "valueSynonym1";
    UpdateValueOptions updateOptions = new UpdateValueOptions.Builder().workspaceId(WORKSPACE_ID).entity(entity).value(value).newValue(newValue).addSynonym(valueSynonym0).addSynonym(valueSynonym1).build();
    assertEquals(updateOptions.workspaceId(), WORKSPACE_ID);
    assertEquals(updateOptions.entity(), entity);
    assertEquals(updateOptions.newValue(), newValue);
    assertEquals(updateOptions.newSynonyms().size(), 2);
    assertEquals(updateOptions.newSynonyms().get(0), valueSynonym0);
    assertEquals(updateOptions.newSynonyms().get(1), valueSynonym1);
    UpdateValueOptions.Builder builder = updateOptions.newBuilder();
    String valueSynonym2 = "valueSynonym2";
    builder.newSynonyms(Arrays.asList(valueSynonym2));
    UpdateValueOptions options2 = builder.build();
    assertEquals(options2.workspaceId(), WORKSPACE_ID);
    assertEquals(options2.entity(), entity);
    assertEquals(options2.newValue(), newValue);
    assertEquals(options2.newSynonyms().size(), 1);
    assertEquals(options2.newSynonyms().get(0), valueSynonym2);
}
Also used : UpdateValueOptions(com.ibm.watson.developer_cloud.conversation.v1.model.UpdateValueOptions) WatsonServiceUnitTest(com.ibm.watson.developer_cloud.WatsonServiceUnitTest) Test(org.junit.Test)

Example 5 with UpdateValueOptions

use of com.ibm.watson.assistant.v1.model.UpdateValueOptions 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)5 HashMap (java.util.HashMap)4 Value (com.ibm.watson.assistant.v1.model.Value)3 UpdateValueOptions (com.ibm.watson.assistant.v1.model.UpdateValueOptions)2 WatsonServiceUnitTest (com.ibm.watson.developer_cloud.WatsonServiceUnitTest)2 UpdateValueOptions (com.ibm.watson.developer_cloud.assistant.v1.model.UpdateValueOptions)2 UpdateValueOptions (com.ibm.watson.developer_cloud.conversation.v1.model.UpdateValueOptions)2 NotFoundException (com.ibm.watson.developer_cloud.service.exception.NotFoundException)2 JsonObject (com.google.gson.JsonObject)1 RequestBuilder (com.ibm.cloud.sdk.core.http.RequestBuilder)1 NotFoundException (com.ibm.cloud.sdk.core.service.exception.NotFoundException)1 CreateEntityOptions (com.ibm.watson.assistant.v1.model.CreateEntityOptions)1 CreateValue (com.ibm.watson.assistant.v1.model.CreateValue)1 CreateValueOptions (com.ibm.watson.assistant.v1.model.CreateValueOptions)1 DeleteValueOptions (com.ibm.watson.assistant.v1.model.DeleteValueOptions)1 DialogNodeOutputOptionsElementValue (com.ibm.watson.assistant.v1.model.DialogNodeOutputOptionsElementValue)1 GetValueOptions (com.ibm.watson.assistant.v1.model.GetValueOptions)1 CreateEntityOptions (com.ibm.watson.developer_cloud.assistant.v1.model.CreateEntityOptions)1 CreateValueOptions (com.ibm.watson.developer_cloud.assistant.v1.model.CreateValueOptions)1 DeleteValueOptions (com.ibm.watson.developer_cloud.assistant.v1.model.DeleteValueOptions)1