Search in sources :

Example 1 with UpdateEntityOptions

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

the class ConversationTest method testUpdateEntityOptionsBuilder.

/**
 * Test UpdateEntityOptions builder.
 */
@Test
public void testUpdateEntityOptionsBuilder() {
    String entity = "anEntity";
    String newEntity = "renamedEntity";
    CreateValue entityValue0 = new CreateValue.Builder().value("entityValue0").addPattern("pattern0").build();
    CreateValue entityValue1 = new CreateValue.Builder().value("entityValue1").addPattern("pattern1").build();
    UpdateEntityOptions updateOptions = new UpdateEntityOptions.Builder().workspaceId(WORKSPACE_ID).entity(entity).newEntity(newEntity).addValue(entityValue0).addValue(entityValue1).build();
    assertEquals(updateOptions.workspaceId(), WORKSPACE_ID);
    assertEquals(updateOptions.entity(), entity);
    assertEquals(updateOptions.newEntity(), newEntity);
    assertEquals(updateOptions.newValues().size(), 2);
    assertEquals(updateOptions.newValues().get(0), entityValue0);
    assertEquals(updateOptions.newValues().get(1), entityValue1);
    UpdateEntityOptions.Builder builder = updateOptions.newBuilder();
    CreateValue entityValue2 = new CreateValue.Builder().value("entityValue2").addPattern("pattern2").build();
    builder.newValues(Arrays.asList(entityValue2));
    UpdateEntityOptions options2 = builder.build();
    assertEquals(options2.workspaceId(), WORKSPACE_ID);
    assertEquals(options2.entity(), entity);
    assertEquals(options2.newEntity(), newEntity);
    assertEquals(options2.newValues().size(), 1);
    assertEquals(options2.newValues().get(0), entityValue2);
}
Also used : CreateValue(com.ibm.watson.developer_cloud.conversation.v1.model.CreateValue) UpdateEntityOptions(com.ibm.watson.developer_cloud.conversation.v1.model.UpdateEntityOptions) WatsonServiceUnitTest(com.ibm.watson.developer_cloud.WatsonServiceUnitTest) Test(org.junit.Test)

Example 2 with UpdateEntityOptions

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

the class AssistantTest method testUpdateEntityOptionsBuilder.

/**
 * Test UpdateEntityOptions builder.
 */
@Test
public void testUpdateEntityOptionsBuilder() {
    String entity = "anEntity";
    String newEntity = "renamedEntity";
    CreateValue entityValue0 = new CreateValue.Builder().value("entityValue0").addPattern("pattern0").build();
    CreateValue entityValue1 = new CreateValue.Builder().value("entityValue1").addPattern("pattern1").build();
    UpdateEntityOptions updateOptions = new UpdateEntityOptions.Builder().workspaceId(WORKSPACE_ID).entity(entity).newEntity(newEntity).addValue(entityValue0).addValue(entityValue1).build();
    assertEquals(updateOptions.workspaceId(), WORKSPACE_ID);
    assertEquals(updateOptions.entity(), entity);
    assertEquals(updateOptions.newEntity(), newEntity);
    assertEquals(updateOptions.newValues().size(), 2);
    assertEquals(updateOptions.newValues().get(0), entityValue0);
    assertEquals(updateOptions.newValues().get(1), entityValue1);
    UpdateEntityOptions.Builder builder = updateOptions.newBuilder();
    CreateValue entityValue2 = new CreateValue.Builder().value("entityValue2").addPattern("pattern2").build();
    builder.newValues(Arrays.asList(entityValue2));
    UpdateEntityOptions options2 = builder.build();
    assertEquals(options2.workspaceId(), WORKSPACE_ID);
    assertEquals(options2.entity(), entity);
    assertEquals(options2.newEntity(), newEntity);
    assertEquals(options2.newValues().size(), 1);
    assertEquals(options2.newValues().get(0), entityValue2);
}
Also used : CreateValue(com.ibm.watson.developer_cloud.assistant.v1.model.CreateValue) UpdateEntityOptions(com.ibm.watson.developer_cloud.assistant.v1.model.UpdateEntityOptions) WatsonServiceUnitTest(com.ibm.watson.developer_cloud.WatsonServiceUnitTest) Test(org.junit.Test)

Example 3 with UpdateEntityOptions

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

the class AssistantTest method testUpdateEntityWOptions.

// Test the updateEntity operation with a valid options model parameter
@Test
public void testUpdateEntityWOptions() throws Throwable {
    // Register a mock response
    String mockResponseBody = "{\"entity\": \"entity\", \"description\": \"description\", \"metadata\": {\"mapKey\": \"anyValue\"}, \"fuzzy_match\": true, \"created\": \"2019-01-01T12:00:00.000Z\", \"updated\": \"2019-01-01T12:00:00.000Z\", \"values\": [{\"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 updateEntityPath = "/v1/workspaces/testString/entities/testString";
    server.enqueue(new MockResponse().setHeader("Content-type", "application/json").setResponseCode(200).setBody(mockResponseBody));
    // Construct an instance of the CreateValue model
    CreateValue createValueModel = new CreateValue.Builder().value("testString").metadata(new java.util.HashMap<String, Object>() {

        {
            put("foo", "testString");
        }
    }).type("synonyms").synonyms(new java.util.ArrayList<String>(java.util.Arrays.asList("testString"))).patterns(new java.util.ArrayList<String>(java.util.Arrays.asList("testString"))).build();
    // Construct an instance of the UpdateEntityOptions model
    UpdateEntityOptions updateEntityOptionsModel = new UpdateEntityOptions.Builder().workspaceId("testString").entity("testString").newEntity("testString").newDescription("testString").newMetadata(new java.util.HashMap<String, Object>() {

        {
            put("foo", "testString");
        }
    }).newFuzzyMatch(true).newValues(new java.util.ArrayList<CreateValue>(java.util.Arrays.asList(createValueModel))).append(false).includeAudit(false).build();
    // Invoke updateEntity() with a valid options model and verify the result
    Response<Entity> response = assistantService.updateEntity(updateEntityOptionsModel).execute();
    assertNotNull(response);
    Entity 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, updateEntityPath);
    // 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) Entity(com.ibm.watson.assistant.v1.model.Entity) CreateEntity(com.ibm.watson.assistant.v1.model.CreateEntity) RuntimeEntity(com.ibm.watson.assistant.v1.model.RuntimeEntity) CreateValue(com.ibm.watson.assistant.v1.model.CreateValue) HashMap(java.util.HashMap) UpdateEntityOptions(com.ibm.watson.assistant.v1.model.UpdateEntityOptions) Test(org.testng.annotations.Test)

Example 4 with UpdateEntityOptions

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

the class Assistant method updateEntity.

/**
 * Update entity.
 *
 * <p>Update an existing entity with new or modified data. You must provide component objects
 * defining the content of the updated entity.
 *
 * <p>If you want to update multiple entities with a single API call, consider using the **[Update
 * workspace](#update-workspace)** method instead.
 *
 * @param updateEntityOptions the {@link UpdateEntityOptions} containing the options for the call
 * @return a {@link ServiceCall} with a result of type {@link Entity}
 */
public ServiceCall<Entity> updateEntity(UpdateEntityOptions updateEntityOptions) {
    com.ibm.cloud.sdk.core.util.Validator.notNull(updateEntityOptions, "updateEntityOptions cannot be null");
    Map<String, String> pathParamsMap = new HashMap<String, String>();
    pathParamsMap.put("workspace_id", updateEntityOptions.workspaceId());
    pathParamsMap.put("entity", updateEntityOptions.entity());
    RequestBuilder builder = RequestBuilder.post(RequestBuilder.resolveRequestUrl(getServiceUrl(), "/v1/workspaces/{workspace_id}/entities/{entity}", pathParamsMap));
    Map<String, String> sdkHeaders = SdkCommon.getSdkHeaders("conversation", "v1", "updateEntity");
    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 (updateEntityOptions.append() != null) {
        builder.query("append", String.valueOf(updateEntityOptions.append()));
    }
    if (updateEntityOptions.includeAudit() != null) {
        builder.query("include_audit", String.valueOf(updateEntityOptions.includeAudit()));
    }
    final JsonObject contentJson = new JsonObject();
    if (updateEntityOptions.newEntity() != null) {
        contentJson.addProperty("entity", updateEntityOptions.newEntity());
    }
    if (updateEntityOptions.newDescription() != null) {
        contentJson.addProperty("description", updateEntityOptions.newDescription());
    }
    if (updateEntityOptions.newMetadata() != null) {
        contentJson.add("metadata", com.ibm.cloud.sdk.core.util.GsonSingleton.getGson().toJsonTree(updateEntityOptions.newMetadata()));
    }
    if (updateEntityOptions.newFuzzyMatch() != null) {
        contentJson.addProperty("fuzzy_match", updateEntityOptions.newFuzzyMatch());
    }
    if (updateEntityOptions.newValues() != null) {
        contentJson.add("values", com.ibm.cloud.sdk.core.util.GsonSingleton.getGson().toJsonTree(updateEntityOptions.newValues()));
    }
    builder.bodyJson(contentJson);
    ResponseConverter<Entity> responseConverter = ResponseConverterUtils.getValue(new com.google.gson.reflect.TypeToken<Entity>() {
    }.getType());
    return createServiceCall(builder.build(), responseConverter);
}
Also used : Entity(com.ibm.watson.assistant.v1.model.Entity) RequestBuilder(com.ibm.cloud.sdk.core.http.RequestBuilder) HashMap(java.util.HashMap) JsonObject(com.google.gson.JsonObject)

Aggregations

Entity (com.ibm.watson.assistant.v1.model.Entity)2 WatsonServiceUnitTest (com.ibm.watson.developer_cloud.WatsonServiceUnitTest)2 HashMap (java.util.HashMap)2 Test (org.junit.Test)2 JsonObject (com.google.gson.JsonObject)1 RequestBuilder (com.ibm.cloud.sdk.core.http.RequestBuilder)1 CreateEntity (com.ibm.watson.assistant.v1.model.CreateEntity)1 CreateValue (com.ibm.watson.assistant.v1.model.CreateValue)1 RuntimeEntity (com.ibm.watson.assistant.v1.model.RuntimeEntity)1 UpdateEntityOptions (com.ibm.watson.assistant.v1.model.UpdateEntityOptions)1 CreateValue (com.ibm.watson.developer_cloud.assistant.v1.model.CreateValue)1 UpdateEntityOptions (com.ibm.watson.developer_cloud.assistant.v1.model.UpdateEntityOptions)1 CreateValue (com.ibm.watson.developer_cloud.conversation.v1.model.CreateValue)1 UpdateEntityOptions (com.ibm.watson.developer_cloud.conversation.v1.model.UpdateEntityOptions)1 MockResponse (okhttp3.mockwebserver.MockResponse)1 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)1 Test (org.testng.annotations.Test)1