Search in sources :

Example 41 with CreateEntityOptions

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

the class AssistantTest method testCreateEntityWOptions.

// Test the createEntity operation with a valid options model parameter
@Test
public void testCreateEntityWOptions() 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 createEntityPath = "/v1/workspaces/testString/entities";
    server.enqueue(new MockResponse().setHeader("Content-type", "application/json").setResponseCode(201).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 CreateEntityOptions model
    CreateEntityOptions createEntityOptionsModel = new CreateEntityOptions.Builder().workspaceId("testString").entity("testString").description("testString").metadata(new java.util.HashMap<String, Object>() {

        {
            put("foo", "testString");
        }
    }).fuzzyMatch(true).values(new java.util.ArrayList<CreateValue>(java.util.Arrays.asList(createValueModel))).includeAudit(false).build();
    // Invoke createEntity() with a valid options model and verify the result
    Response<Entity> response = assistantService.createEntity(createEntityOptionsModel).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, createEntityPath);
    // Verify query params
    Map<String, String> query = TestUtilities.parseQueryString(request);
    assertNotNull(query);
    assertEquals(query.get("version"), "testString");
    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) CreateEntityOptions(com.ibm.watson.assistant.v1.model.CreateEntityOptions) Test(org.testng.annotations.Test)

Example 42 with CreateEntityOptions

use of com.ibm.watson.assistant.v1.model.CreateEntityOptions 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().getResult();
    try {
        assertNotNull(response);
        assertNotNull(response.getEntity());
        assertEquals(response.getEntity(), 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().getResult();
        fail(ex.getMessage());
    }
    DeleteEntityOptions deleteOptions = new DeleteEntityOptions.Builder(workspaceId, entity).build();
    service.deleteEntity(deleteOptions).execute().getResult();
    try {
        GetEntityOptions getOptions = new GetEntityOptions.Builder(workspaceId, entity).build();
        service.getEntity(getOptions).execute().getResult();
        fail("deleteEntity failed");
    } catch (Exception ex) {
        // Expected result
        assertTrue(ex instanceof NotFoundException);
    }
}
Also used : Entity(com.ibm.watson.assistant.v1.model.Entity) CreateEntityOptions(com.ibm.watson.assistant.v1.model.CreateEntityOptions) DeleteEntityOptions(com.ibm.watson.assistant.v1.model.DeleteEntityOptions) NotFoundException(com.ibm.cloud.sdk.core.service.exception.NotFoundException) NotFoundException(com.ibm.cloud.sdk.core.service.exception.NotFoundException) GetEntityOptions(com.ibm.watson.assistant.v1.model.GetEntityOptions) Test(org.junit.Test)

Example 43 with CreateEntityOptions

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

the class EntitiesIT method testListEntitiesWithPaging.

/**
 * Test listEntities with pagination.
 */
@Test
@Ignore("To be run locally until we fix the Rate limitation issue")
public void testListEntitiesWithPaging() {
    // gotta be unique
    String entity1 = "Hello" + UUID.randomUUID().toString();
    // gotta be unique
    String entity2 = "Goodbye" + UUID.randomUUID().toString();
    CreateEntityOptions createOptions = new CreateEntityOptions.Builder(workspaceId, entity1).build();
    service.createEntity(createOptions).execute().getResult();
    service.createEntity(createOptions.newBuilder().entity(entity2).build()).execute().getResult();
    try {
        ListEntitiesOptions listOptions = new ListEntitiesOptions.Builder(workspaceId).sort("entity").pageLimit(1L).build();
        EntityCollection response = service.listEntities(listOptions).execute().getResult();
        assertNotNull(response);
        assertNotNull(response.getEntities());
        assertNotNull(response.getPagination());
        assertNotNull(response.getPagination().getRefreshUrl());
        assertNotNull(response.getPagination().getNextUrl());
        assertNotNull(response.getPagination().getNextCursor());
        assertTrue(!response.getEntities().get(0).getEntity().equals(entity1));
        Entity ieResponse = null;
        while (response.getPagination().getNextCursor() != null) {
            assertNotNull(response.getEntities());
            assertTrue(response.getEntities().size() == 1);
            if (response.getEntities().get(0).getEntity().equals(entity1)) {
                ieResponse = response.getEntities().get(0);
                break;
            }
            String cursor = response.getPagination().getNextCursor();
            response = service.listEntities(listOptions.newBuilder().cursor(cursor).build()).execute().getResult();
        }
        assertNotNull(ieResponse);
    } catch (Exception ex) {
        fail(ex.getMessage());
    } finally {
        // Clean up
        DeleteEntityOptions deleteOptions = new DeleteEntityOptions.Builder(workspaceId, entity1).build();
        service.deleteEntity(deleteOptions).execute().getResult();
        service.deleteEntity(deleteOptions.newBuilder().entity(entity2).build()).execute().getResult();
    }
}
Also used : Entity(com.ibm.watson.assistant.v1.model.Entity) CreateEntityOptions(com.ibm.watson.assistant.v1.model.CreateEntityOptions) EntityCollection(com.ibm.watson.assistant.v1.model.EntityCollection) DeleteEntityOptions(com.ibm.watson.assistant.v1.model.DeleteEntityOptions) ListEntitiesOptions(com.ibm.watson.assistant.v1.model.ListEntitiesOptions) NotFoundException(com.ibm.cloud.sdk.core.service.exception.NotFoundException) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 44 with CreateEntityOptions

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

the class SynonymsIT method testListSynonyms.

/**
 * Test listSynonyms.
 */
@Test
public void testListSynonyms() {
    String entity = "beverage";
    String entityValue = "coffee";
    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"));
    }
    try {
        CreateValueOptions createOptions = new CreateValueOptions.Builder(workspaceId, entity, entityValue).build();
        service.createValue(createOptions).execute().getResult();
    } catch (Exception ex) {
        // Exception is okay if is for Unique Violation
        assertTrue(ex.getLocalizedMessage().startsWith("Unique Violation"));
    }
    try {
        CreateSynonymOptions createOptions = new CreateSynonymOptions.Builder(workspaceId, entity, entityValue, synonym1).build();
        service.createSynonym(createOptions).execute().getResult();
        service.createSynonym(createOptions.newBuilder().synonym(synonym2).build()).execute().getResult();
    } catch (Exception ex) {
        // Exception is okay if is for Unique Violation
        assertTrue(ex.getLocalizedMessage().startsWith("Unique Violation"));
    }
    try {
        ListSynonymsOptions listOptions = new ListSynonymsOptions.Builder().workspaceId(workspaceId).entity(entity).value(entityValue).build();
        final SynonymCollection response = service.listSynonyms(listOptions).execute().getResult();
        assertNotNull(response);
        assertNotNull(response.getSynonyms());
        assertNotNull(response.getPagination());
        assertNotNull(response.getPagination().getRefreshUrl());
        // nextUrl may be null
        if (response.getPagination().getNextUrl() == null) {
            assertNull(response.getPagination().getNextCursor());
        } else {
            assertNotNull(response.getPagination().getNextCursor());
        }
        assertTrue(response.getSynonyms().size() >= 2);
        // Should not be paginated, but just to be sure
        if (response.getPagination().getNextUrl() == null) {
            // assertTrue(response.getSynonyms().stream().filter(r ->
            // r.getSynonym().equals(synonym1)).count() == 1);
            boolean found1 = false;
            boolean found2 = false;
            for (Synonym synonymResponse : response.getSynonyms()) {
                found1 |= synonymResponse.synonym().equals(synonym1);
                found2 |= synonymResponse.synonym().equals(synonym2);
            }
            assertTrue(found1 && found2);
        }
    } catch (Exception ex) {
        fail(ex.getMessage());
    } finally {
        DeleteSynonymOptions deleteOptions = new DeleteSynonymOptions.Builder(workspaceId, entity, entityValue, synonym1).build();
        service.deleteSynonym(deleteOptions).execute().getResult();
        service.deleteSynonym(deleteOptions.newBuilder().synonym(synonym2).build()).execute().getResult();
    }
}
Also used : DeleteSynonymOptions(com.ibm.watson.assistant.v1.model.DeleteSynonymOptions) CreateEntityOptions(com.ibm.watson.assistant.v1.model.CreateEntityOptions) CreateValueOptions(com.ibm.watson.assistant.v1.model.CreateValueOptions) ListSynonymsOptions(com.ibm.watson.assistant.v1.model.ListSynonymsOptions) NotFoundException(com.ibm.cloud.sdk.core.service.exception.NotFoundException) CreateSynonymOptions(com.ibm.watson.assistant.v1.model.CreateSynonymOptions) Synonym(com.ibm.watson.assistant.v1.model.Synonym) SynonymCollection(com.ibm.watson.assistant.v1.model.SynonymCollection) Test(org.junit.Test)

Example 45 with CreateEntityOptions

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

the class SynonymsIT method testGetSynonym.

/**
 * Test getSynonym.
 */
@Test
public void testGetSynonym() {
    String entity = "beverage";
    String entityValue = "orange juice";
    String synonym = "OJ";
    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, entityValue).build();
        service.createValue(createOptions).execute().getResult();
    } catch (Exception ex) {
        // Exception is okay if is for Unique Violation
        assertTrue(ex.getLocalizedMessage().startsWith("Unique Violation"));
    }
    Date start = new Date();
    CreateSynonymOptions createOptions = new CreateSynonymOptions.Builder(workspaceId, entity, entityValue, synonym).build();
    service.createSynonym(createOptions).execute().getResult();
    try {
        GetSynonymOptions getOptions = new GetSynonymOptions.Builder(workspaceId, entity, entityValue, synonym).includeAudit(true).build();
        Synonym response = service.getSynonym(getOptions).execute().getResult();
        assertNotNull(response);
        assertNotNull(response.synonym());
        assertEquals(response.synonym(), synonym);
        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));
    } catch (Exception ex) {
        fail(ex.getMessage());
    } finally {
        DeleteSynonymOptions deleteOptions = new DeleteSynonymOptions.Builder(workspaceId, entity, entityValue, synonym).build();
        service.deleteSynonym(deleteOptions).execute().getResult();
    }
}
Also used : DeleteSynonymOptions(com.ibm.watson.assistant.v1.model.DeleteSynonymOptions) CreateEntityOptions(com.ibm.watson.assistant.v1.model.CreateEntityOptions) CreateValueOptions(com.ibm.watson.assistant.v1.model.CreateValueOptions) GetSynonymOptions(com.ibm.watson.assistant.v1.model.GetSynonymOptions) NotFoundException(com.ibm.cloud.sdk.core.service.exception.NotFoundException) Date(java.util.Date) CreateSynonymOptions(com.ibm.watson.assistant.v1.model.CreateSynonymOptions) Synonym(com.ibm.watson.assistant.v1.model.Synonym) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)47 NotFoundException (com.ibm.watson.developer_cloud.service.exception.NotFoundException)30 CreateEntityOptions (com.ibm.watson.assistant.v1.model.CreateEntityOptions)16 CreateEntityOptions (com.ibm.watson.developer_cloud.assistant.v1.model.CreateEntityOptions)16 CreateEntityOptions (com.ibm.watson.developer_cloud.conversation.v1.model.CreateEntityOptions)16 NotFoundException (com.ibm.cloud.sdk.core.service.exception.NotFoundException)15 CreateValueOptions (com.ibm.watson.assistant.v1.model.CreateValueOptions)12 CreateValueOptions (com.ibm.watson.developer_cloud.assistant.v1.model.CreateValueOptions)12 CreateValueOptions (com.ibm.watson.developer_cloud.conversation.v1.model.CreateValueOptions)12 HashMap (java.util.HashMap)7 CreateSynonymOptions (com.ibm.watson.assistant.v1.model.CreateSynonymOptions)6 DeleteSynonymOptions (com.ibm.watson.assistant.v1.model.DeleteSynonymOptions)6 DeleteValueOptions (com.ibm.watson.assistant.v1.model.DeleteValueOptions)6 CreateSynonymOptions (com.ibm.watson.developer_cloud.assistant.v1.model.CreateSynonymOptions)6 DeleteSynonymOptions (com.ibm.watson.developer_cloud.assistant.v1.model.DeleteSynonymOptions)6 DeleteValueOptions (com.ibm.watson.developer_cloud.assistant.v1.model.DeleteValueOptions)6 CreateSynonymOptions (com.ibm.watson.developer_cloud.conversation.v1.model.CreateSynonymOptions)6 DeleteSynonymOptions (com.ibm.watson.developer_cloud.conversation.v1.model.DeleteSynonymOptions)6 DeleteValueOptions (com.ibm.watson.developer_cloud.conversation.v1.model.DeleteValueOptions)6 Date (java.util.Date)6