Search in sources :

Example 6 with UpdateValueOptions

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

the class AssistantTest method testUpdateValueWOptions.

// Test the updateValue operation with a valid options model parameter
@Test
public void testUpdateValueWOptions() throws Throwable {
    // Register a mock response
    String mockResponseBody = "{\"value\": \"value\", \"metadata\": {\"mapKey\": \"anyValue\"}, \"type\": \"synonyms\", \"synonyms\": [\"synonym\"], \"patterns\": [\"pattern\"], \"created\": \"2019-01-01T12:00:00.000Z\", \"updated\": \"2019-01-01T12:00:00.000Z\"}";
    String updateValuePath = "/v1/workspaces/testString/entities/testString/values/testString";
    server.enqueue(new MockResponse().setHeader("Content-type", "application/json").setResponseCode(200).setBody(mockResponseBody));
    // Construct an instance of the UpdateValueOptions model
    UpdateValueOptions updateValueOptionsModel = new UpdateValueOptions.Builder().workspaceId("testString").entity("testString").value("testString").newValue("testString").newMetadata(new java.util.HashMap<String, Object>() {

        {
            put("foo", "testString");
        }
    }).newType("synonyms").newSynonyms(new java.util.ArrayList<String>(java.util.Arrays.asList("testString"))).newPatterns(new java.util.ArrayList<String>(java.util.Arrays.asList("testString"))).append(false).includeAudit(false).build();
    // Invoke updateValue() with a valid options model and verify the result
    Response<Value> response = assistantService.updateValue(updateValueOptionsModel).execute();
    assertNotNull(response);
    Value responseObj = response.getResult();
    assertNotNull(responseObj);
    // Verify the contents of the request sent to the mock server
    RecordedRequest request = server.takeRequest();
    assertNotNull(request);
    assertEquals(request.getMethod(), "POST");
    // Verify request path
    String parsedPath = TestUtilities.parseReqPath(request);
    assertEquals(parsedPath, updateValuePath);
    // Verify query params
    Map<String, String> query = TestUtilities.parseQueryString(request);
    assertNotNull(query);
    assertEquals(query.get("version"), "testString");
    assertEquals(Boolean.valueOf(query.get("append")), Boolean.valueOf(false));
    assertEquals(Boolean.valueOf(query.get("include_audit")), Boolean.valueOf(false));
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) UpdateValueOptions(com.ibm.watson.assistant.v1.model.UpdateValueOptions) DialogNodeOutputOptionsElementValue(com.ibm.watson.assistant.v1.model.DialogNodeOutputOptionsElementValue) CreateValue(com.ibm.watson.assistant.v1.model.CreateValue) Value(com.ibm.watson.assistant.v1.model.Value) Test(org.testng.annotations.Test)

Example 7 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 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)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