Search in sources :

Example 6 with GetValueOptions

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

the class Assistant method getValue.

/**
 * Get entity value.
 *
 * <p>Get information about an entity value.
 *
 * @param getValueOptions the {@link GetValueOptions} containing the options for the call
 * @return a {@link ServiceCall} with a result of type {@link Value}
 */
public ServiceCall<Value> getValue(GetValueOptions getValueOptions) {
    com.ibm.cloud.sdk.core.util.Validator.notNull(getValueOptions, "getValueOptions cannot be null");
    Map<String, String> pathParamsMap = new HashMap<String, String>();
    pathParamsMap.put("workspace_id", getValueOptions.workspaceId());
    pathParamsMap.put("entity", getValueOptions.entity());
    pathParamsMap.put("value", getValueOptions.value());
    RequestBuilder builder = RequestBuilder.get(RequestBuilder.resolveRequestUrl(getServiceUrl(), "/v1/workspaces/{workspace_id}/entities/{entity}/values/{value}", pathParamsMap));
    Map<String, String> sdkHeaders = SdkCommon.getSdkHeaders("conversation", "v1", "getValue");
    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 (getValueOptions.export() != null) {
        builder.query("export", String.valueOf(getValueOptions.export()));
    }
    if (getValueOptions.includeAudit() != null) {
        builder.query("include_audit", String.valueOf(getValueOptions.includeAudit()));
    }
    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)

Example 7 with GetValueOptions

use of com.ibm.watson.assistant.v1.model.GetValueOptions 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().getResult();
    } 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().getResult();
    Date start = new Date();
    try {
        GetValueOptions getOptions = new GetValueOptions.Builder(workspaceId, entity, entityValue).export(true).includeAudit(true).build();
        Value response = service.getValue(getOptions).execute().getResult();
        assertNotNull(response);
        assertNotNull(response.value());
        assertEquals(response.value(), entityValue);
        assertNotNull(response.created());
        assertNotNull(response.updated());
        Date now = new Date();
        assertTrue(fuzzyBefore(response.created(), now));
        assertTrue(fuzzyAfter(response.created(), start));
        assertTrue(fuzzyBefore(response.updated(), now));
        assertTrue(fuzzyAfter(response.updated(), start));
        assertNotNull(response.synonyms());
        assertTrue(response.synonyms().size() == 2);
        assertTrue(response.synonyms().contains(synonym1));
        assertTrue(response.synonyms().contains(synonym2));
    } catch (Exception ex) {
        fail(ex.getMessage());
    } finally {
        DeleteValueOptions deleteOptions = new DeleteValueOptions.Builder(workspaceId, entity, entityValue).build();
        service.deleteValue(deleteOptions).execute().getResult();
    }
}
Also used : GetValueOptions(com.ibm.watson.assistant.v1.model.GetValueOptions) CreateEntityOptions(com.ibm.watson.assistant.v1.model.CreateEntityOptions) DeleteValueOptions(com.ibm.watson.assistant.v1.model.DeleteValueOptions) CreateValueOptions(com.ibm.watson.assistant.v1.model.CreateValueOptions) ArrayList(java.util.ArrayList) NotFoundException(com.ibm.cloud.sdk.core.service.exception.NotFoundException) Date(java.util.Date) Value(com.ibm.watson.assistant.v1.model.Value) Test(org.junit.Test)

Example 8 with GetValueOptions

use of com.ibm.watson.assistant.v1.model.GetValueOptions 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 9 with GetValueOptions

use of com.ibm.watson.assistant.v1.model.GetValueOptions 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)

Example 10 with GetValueOptions

use of com.ibm.watson.assistant.v1.model.GetValueOptions 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 metadataValue = "value for " + entityValue2;
    valueMetadata.put("key", metadataValue);
    try {
        CreateEntityOptions createOptions = new CreateEntityOptions.Builder(workspaceId, entity).build();
        service.createEntity(createOptions).execute().getResult();
    } 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().getResult();
    } 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<>(Arrays.asList(synonym1, synonym2))).newMetadata(valueMetadata).build();
    Value response = service.updateValue(updateOptions).execute().getResult();
    try {
        assertNotNull(response);
        assertNotNull(response.value());
        assertEquals(response.value(), entityValue2);
        GetValueOptions getOptions = new GetValueOptions.Builder(workspaceId, entity, entityValue2).export(true).includeAudit(true).build();
        Value vResponse = service.getValue(getOptions).execute().getResult();
        assertNotNull(vResponse);
        assertNotNull(vResponse.value());
        assertEquals(vResponse.value(), entityValue2);
        assertNotNull(vResponse.created());
        assertNotNull(vResponse.updated());
        assertNotNull(vResponse.synonyms());
        assertTrue(vResponse.synonyms().size() == 2);
        assertTrue(vResponse.synonyms().contains(synonym1));
        assertTrue(vResponse.synonyms().contains(synonym2));
        // metadata
        assertNotNull(response.metadata());
        assertNotNull(response.metadata().get("key"));
        assertEquals(response.metadata().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().getResult();
    }
}
Also used : GetValueOptions(com.ibm.watson.assistant.v1.model.GetValueOptions) HashMap(java.util.HashMap) CreateEntityOptions(com.ibm.watson.assistant.v1.model.CreateEntityOptions) DeleteValueOptions(com.ibm.watson.assistant.v1.model.DeleteValueOptions) CreateValueOptions(com.ibm.watson.assistant.v1.model.CreateValueOptions) UpdateValueOptions(com.ibm.watson.assistant.v1.model.UpdateValueOptions) NotFoundException(com.ibm.cloud.sdk.core.service.exception.NotFoundException) Value(com.ibm.watson.assistant.v1.model.Value) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)9 NotFoundException (com.ibm.watson.developer_cloud.service.exception.NotFoundException)6 Value (com.ibm.watson.assistant.v1.model.Value)5 GetValueOptions (com.ibm.watson.assistant.v1.model.GetValueOptions)4 HashMap (java.util.HashMap)4 NotFoundException (com.ibm.cloud.sdk.core.service.exception.NotFoundException)3 CreateEntityOptions (com.ibm.watson.assistant.v1.model.CreateEntityOptions)3 CreateValueOptions (com.ibm.watson.assistant.v1.model.CreateValueOptions)3 DeleteValueOptions (com.ibm.watson.assistant.v1.model.DeleteValueOptions)3 CreateEntityOptions (com.ibm.watson.developer_cloud.assistant.v1.model.CreateEntityOptions)3 CreateValueOptions (com.ibm.watson.developer_cloud.assistant.v1.model.CreateValueOptions)3 DeleteValueOptions (com.ibm.watson.developer_cloud.assistant.v1.model.DeleteValueOptions)3 GetValueOptions (com.ibm.watson.developer_cloud.assistant.v1.model.GetValueOptions)3 CreateEntityOptions (com.ibm.watson.developer_cloud.conversation.v1.model.CreateEntityOptions)3 CreateValueOptions (com.ibm.watson.developer_cloud.conversation.v1.model.CreateValueOptions)3 DeleteValueOptions (com.ibm.watson.developer_cloud.conversation.v1.model.DeleteValueOptions)3 GetValueOptions (com.ibm.watson.developer_cloud.conversation.v1.model.GetValueOptions)3 ArrayList (java.util.ArrayList)3 Date (java.util.Date)3 Value (com.ibm.watson.developer_cloud.assistant.v1.model.Value)2