Search in sources :

Example 1 with Entity

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

the class EntitiesIT method testCreateEntity.

/**
 * Test createEntity.
 */
@Test
public void testCreateEntity() {
    // gotta be unique
    String entity = "Hello" + UUID.randomUUID().toString();
    String entityDescription = "Description of " + entity;
    Map<String, Object> entityMetadata = new HashMap<String, Object>();
    String metadataValue = "value for " + entity;
    entityMetadata.put("key", metadataValue);
    CreateEntityOptions.Builder optionsBuilder = new CreateEntityOptions.Builder();
    optionsBuilder.workspaceId(workspaceId);
    optionsBuilder.entity(entity);
    optionsBuilder.description(entityDescription);
    optionsBuilder.metadata(entityMetadata);
    // default is false
    optionsBuilder.fuzzyMatch(true);
    Entity response = service.createEntity(optionsBuilder.build()).execute();
    try {
        assertNotNull(response);
        assertNotNull(response.getEntityName());
        assertEquals(response.getEntityName(), entity);
        assertNotNull(response.getDescription());
        assertEquals(response.getDescription(), entityDescription);
        assertNotNull(response.getMetadata());
        assertNotNull(response.getMetadata().get("key"));
        assertEquals(response.getMetadata().get("key"), metadataValue);
        assertNotNull(response.isFuzzyMatch());
        assertTrue(response.isFuzzyMatch());
    } catch (Exception ex) {
        fail(ex.getMessage());
    } finally {
        // Clean up
        DeleteEntityOptions deleteOptions = new DeleteEntityOptions.Builder(workspaceId, entity).build();
        service.deleteEntity(deleteOptions).execute();
    }
}
Also used : Entity(com.ibm.watson.developer_cloud.assistant.v1.model.Entity) HashMap(java.util.HashMap) CreateEntityOptions(com.ibm.watson.developer_cloud.assistant.v1.model.CreateEntityOptions) DeleteEntityOptions(com.ibm.watson.developer_cloud.assistant.v1.model.DeleteEntityOptions) NotFoundException(com.ibm.watson.developer_cloud.service.exception.NotFoundException) Test(org.junit.Test)

Example 2 with Entity

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

the class EntitiesIT method testDeleteEntity.

/**
 * Test deleteEntity.
 */
@Test
public void testDeleteEntity() {
    // gotta be unique
    String entity = "Hello" + UUID.randomUUID().toString();
    CreateEntityOptions options = new CreateEntityOptions.Builder(workspaceId, entity).build();
    Entity response = service.createEntity(options).execute();
    try {
        assertNotNull(response);
        assertNotNull(response.getEntityName());
        assertEquals(response.getEntityName(), entity);
        assertNull(response.getDescription());
        assertNull(response.getMetadata());
        assertTrue(response.isFuzzyMatch() == null || response.isFuzzyMatch().equals(Boolean.FALSE));
    } catch (Exception ex) {
        DeleteEntityOptions deleteOptions = new DeleteEntityOptions.Builder(workspaceId, entity).build();
        service.deleteEntity(deleteOptions).execute();
        fail(ex.getMessage());
    }
    DeleteEntityOptions deleteOptions = new DeleteEntityOptions.Builder(workspaceId, entity).build();
    service.deleteEntity(deleteOptions).execute();
    try {
        GetEntityOptions getOptions = new GetEntityOptions.Builder(workspaceId, entity).build();
        service.getEntity(getOptions).execute();
        fail("deleteEntity failed");
    } catch (Exception ex) {
        // Expected result
        assertTrue(ex instanceof NotFoundException);
    }
}
Also used : Entity(com.ibm.watson.developer_cloud.assistant.v1.model.Entity) CreateEntityOptions(com.ibm.watson.developer_cloud.assistant.v1.model.CreateEntityOptions) DeleteEntityOptions(com.ibm.watson.developer_cloud.assistant.v1.model.DeleteEntityOptions) NotFoundException(com.ibm.watson.developer_cloud.service.exception.NotFoundException) NotFoundException(com.ibm.watson.developer_cloud.service.exception.NotFoundException) GetEntityOptions(com.ibm.watson.developer_cloud.assistant.v1.model.GetEntityOptions) Test(org.junit.Test)

Example 3 with Entity

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

the class Conversation method createEntity.

/**
 * Create entity.
 *
 * Create a new entity.
 *
 * @param createEntityOptions the {@link CreateEntityOptions} containing the options for the call
 * @return a {@link ServiceCall} with a response type of {@link Entity}
 */
public ServiceCall<Entity> createEntity(CreateEntityOptions createEntityOptions) {
    Validator.notNull(createEntityOptions, "createEntityOptions cannot be null");
    String[] pathSegments = { "v1/workspaces", "entities" };
    String[] pathParameters = { createEntityOptions.workspaceId() };
    RequestBuilder builder = RequestBuilder.post(RequestBuilder.constructHttpUrl(getEndPoint(), pathSegments, pathParameters));
    builder.query(VERSION, versionDate);
    final JsonObject contentJson = new JsonObject();
    contentJson.addProperty("entity", createEntityOptions.entity());
    if (createEntityOptions.description() != null) {
        contentJson.addProperty("description", createEntityOptions.description());
    }
    if (createEntityOptions.metadata() != null) {
        contentJson.add("metadata", GsonSingleton.getGson().toJsonTree(createEntityOptions.metadata()));
    }
    if (createEntityOptions.values() != null) {
        contentJson.add("values", GsonSingleton.getGson().toJsonTree(createEntityOptions.values()));
    }
    if (createEntityOptions.fuzzyMatch() != null) {
        contentJson.addProperty("fuzzy_match", createEntityOptions.fuzzyMatch());
    }
    builder.bodyJson(contentJson);
    return createServiceCall(builder.build(), ResponseConverterUtils.getObject(Entity.class));
}
Also used : Entity(com.ibm.watson.developer_cloud.conversation.v1.model.Entity) RequestBuilder(com.ibm.watson.developer_cloud.http.RequestBuilder) JsonObject(com.google.gson.JsonObject)

Example 4 with Entity

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

the class Assistant method updateEntity.

/**
 * Update entity.
 *
 * Update an existing entity with new or modified data. You must provide component objects defining the content of the
 * updated entity. This operation is limited to 1000 requests per 30 minutes. For more information, see **Rate
 * limiting**.
 *
 * @param updateEntityOptions the {@link UpdateEntityOptions} containing the options for the call
 * @return a {@link ServiceCall} with a response type of {@link Entity}
 */
public ServiceCall<Entity> updateEntity(UpdateEntityOptions updateEntityOptions) {
    Validator.notNull(updateEntityOptions, "updateEntityOptions cannot be null");
    String[] pathSegments = { "v1/workspaces", "entities" };
    String[] pathParameters = { updateEntityOptions.workspaceId(), updateEntityOptions.entity() };
    RequestBuilder builder = RequestBuilder.post(RequestBuilder.constructHttpUrl(getEndPoint(), pathSegments, pathParameters));
    builder.query(VERSION, versionDate);
    final JsonObject contentJson = new JsonObject();
    if (updateEntityOptions.newFuzzyMatch() != null) {
        contentJson.addProperty("fuzzy_match", updateEntityOptions.newFuzzyMatch());
    }
    if (updateEntityOptions.newEntity() != null) {
        contentJson.addProperty("entity", updateEntityOptions.newEntity());
    }
    if (updateEntityOptions.newMetadata() != null) {
        contentJson.add("metadata", GsonSingleton.getGson().toJsonTree(updateEntityOptions.newMetadata()));
    }
    if (updateEntityOptions.newValues() != null) {
        contentJson.add("values", GsonSingleton.getGson().toJsonTree(updateEntityOptions.newValues()));
    }
    if (updateEntityOptions.newDescription() != null) {
        contentJson.addProperty("description", updateEntityOptions.newDescription());
    }
    builder.bodyJson(contentJson);
    return createServiceCall(builder.build(), ResponseConverterUtils.getObject(Entity.class));
}
Also used : Entity(com.ibm.watson.developer_cloud.assistant.v1.model.Entity) RequestBuilder(com.ibm.watson.developer_cloud.http.RequestBuilder) JsonObject(com.google.gson.JsonObject)

Example 5 with Entity

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

the class AssistantTest method testGetValueWOptions.

// Test the getValue operation with a valid options model parameter
@Test
public void testGetValueWOptions() 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 getValuePath = "/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 GetValueOptions model
    GetValueOptions getValueOptionsModel = new GetValueOptions.Builder().workspaceId("testString").entity("testString").value("testString").export(false).includeAudit(false).build();
    // Invoke getValue() with a valid options model and verify the result
    Response<Value> response = assistantService.getValue(getValueOptionsModel).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(), "GET");
    // Verify request path
    String parsedPath = TestUtilities.parseReqPath(request);
    assertEquals(parsedPath, getValuePath);
    // Verify query params
    Map<String, String> query = TestUtilities.parseQueryString(request);
    assertNotNull(query);
    assertEquals(query.get("version"), "testString");
    assertEquals(Boolean.valueOf(query.get("export")), Boolean.valueOf(false));
    assertEquals(Boolean.valueOf(query.get("include_audit")), Boolean.valueOf(false));
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) GetValueOptions(com.ibm.watson.assistant.v1.model.GetValueOptions) 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)

Aggregations

MockResponse (okhttp3.mockwebserver.MockResponse)32 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)32 Test (org.testng.annotations.Test)32 HashMap (java.util.HashMap)27 Test (org.junit.Test)24 CreateEntityOptions (com.ibm.watson.assistant.v1.model.CreateEntityOptions)19 NotFoundException (com.ibm.cloud.sdk.core.service.exception.NotFoundException)18 RequestBuilder (com.ibm.cloud.sdk.core.http.RequestBuilder)13 CreateValueOptions (com.ibm.watson.assistant.v1.model.CreateValueOptions)13 Entity (com.ibm.watson.assistant.v1.model.Entity)12 Value (com.ibm.watson.assistant.v1.model.Value)12 Synonym (com.ibm.watson.assistant.v1.model.Synonym)11 JsonObject (com.google.gson.JsonObject)10 CreateValue (com.ibm.watson.assistant.v1.model.CreateValue)10 Entity (model.Entity)10 CreateSynonymOptions (com.ibm.watson.assistant.v1.model.CreateSynonymOptions)7 DeleteEntityOptions (com.ibm.watson.assistant.v1.model.DeleteEntityOptions)7 DeleteSynonymOptions (com.ibm.watson.assistant.v1.model.DeleteSynonymOptions)7 DeleteValueOptions (com.ibm.watson.assistant.v1.model.DeleteValueOptions)7 Example (com.ibm.watson.assistant.v1.model.Example)7