Search in sources :

Example 16 with DeleteSynonymOptions

use of com.ibm.watson.assistant.v1.model.DeleteSynonymOptions 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();
    } 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();
    } 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();
        service.createSynonym(createOptions.newBuilder().synonym(synonym2).build()).execute();
    } 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();
        assertNotNull(response);
        assertNotNull(response.getSynonyms());
        assertNotNull(response.getPagination());
        assertNotNull(response.getPagination().getRefreshUrl());
        // nextUrl may be null
        if (response.getPagination().getNextUrl() == null) {
            assertNull(response.getPagination().getCursor());
        } else {
            assertNotNull(response.getPagination().getCursor());
        }
        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, found2 = false;
            for (Synonym synonymResponse : response.getSynonyms()) {
                found1 |= synonymResponse.getSynonymText().equals(synonym1);
                found2 |= synonymResponse.getSynonymText().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();
        service.deleteSynonym(deleteOptions.newBuilder().synonym(synonym2).build()).execute();
    }
}
Also used : DeleteSynonymOptions(com.ibm.watson.developer_cloud.assistant.v1.model.DeleteSynonymOptions) CreateEntityOptions(com.ibm.watson.developer_cloud.assistant.v1.model.CreateEntityOptions) CreateValueOptions(com.ibm.watson.developer_cloud.assistant.v1.model.CreateValueOptions) ListSynonymsOptions(com.ibm.watson.developer_cloud.assistant.v1.model.ListSynonymsOptions) NotFoundException(com.ibm.watson.developer_cloud.service.exception.NotFoundException) CreateSynonymOptions(com.ibm.watson.developer_cloud.assistant.v1.model.CreateSynonymOptions) Synonym(com.ibm.watson.developer_cloud.assistant.v1.model.Synonym) SynonymCollection(com.ibm.watson.developer_cloud.assistant.v1.model.SynonymCollection) Test(org.junit.Test)

Example 17 with DeleteSynonymOptions

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

the class AssistantTest method testDeleteSynonymWOptions.

// Test the deleteSynonym operation with a valid options model parameter
@Test
public void testDeleteSynonymWOptions() throws Throwable {
    // Register a mock response
    String mockResponseBody = "";
    String deleteSynonymPath = "/v1/workspaces/testString/entities/testString/values/testString/synonyms/testString";
    server.enqueue(new MockResponse().setResponseCode(200).setBody(mockResponseBody));
    // Construct an instance of the DeleteSynonymOptions model
    DeleteSynonymOptions deleteSynonymOptionsModel = new DeleteSynonymOptions.Builder().workspaceId("testString").entity("testString").value("testString").synonym("testString").build();
    // Invoke deleteSynonym() with a valid options model and verify the result
    Response<Void> response = assistantService.deleteSynonym(deleteSynonymOptionsModel).execute();
    assertNotNull(response);
    Void responseObj = response.getResult();
    assertNull(responseObj);
    // Verify the contents of the request sent to the mock server
    RecordedRequest request = server.takeRequest();
    assertNotNull(request);
    assertEquals(request.getMethod(), "DELETE");
    // Verify request path
    String parsedPath = TestUtilities.parseReqPath(request);
    assertEquals(parsedPath, deleteSynonymPath);
    // Verify query params
    Map<String, String> query = TestUtilities.parseQueryString(request);
    assertNotNull(query);
    assertEquals(query.get("version"), "testString");
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) DeleteSynonymOptions(com.ibm.watson.assistant.v1.model.DeleteSynonymOptions) Test(org.testng.annotations.Test)

Example 18 with DeleteSynonymOptions

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

use of com.ibm.watson.assistant.v1.model.DeleteSynonymOptions 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)18 NotFoundException (com.ibm.watson.developer_cloud.service.exception.NotFoundException)12 DeleteSynonymOptions (com.ibm.watson.assistant.v1.model.DeleteSynonymOptions)7 NotFoundException (com.ibm.cloud.sdk.core.service.exception.NotFoundException)6 CreateEntityOptions (com.ibm.watson.assistant.v1.model.CreateEntityOptions)6 CreateSynonymOptions (com.ibm.watson.assistant.v1.model.CreateSynonymOptions)6 CreateValueOptions (com.ibm.watson.assistant.v1.model.CreateValueOptions)6 CreateEntityOptions (com.ibm.watson.developer_cloud.assistant.v1.model.CreateEntityOptions)6 CreateSynonymOptions (com.ibm.watson.developer_cloud.assistant.v1.model.CreateSynonymOptions)6 CreateValueOptions (com.ibm.watson.developer_cloud.assistant.v1.model.CreateValueOptions)6 DeleteSynonymOptions (com.ibm.watson.developer_cloud.assistant.v1.model.DeleteSynonymOptions)6 CreateEntityOptions (com.ibm.watson.developer_cloud.conversation.v1.model.CreateEntityOptions)6 CreateSynonymOptions (com.ibm.watson.developer_cloud.conversation.v1.model.CreateSynonymOptions)6 CreateValueOptions (com.ibm.watson.developer_cloud.conversation.v1.model.CreateValueOptions)6 DeleteSynonymOptions (com.ibm.watson.developer_cloud.conversation.v1.model.DeleteSynonymOptions)6 Synonym (com.ibm.watson.assistant.v1.model.Synonym)5 Synonym (com.ibm.watson.developer_cloud.assistant.v1.model.Synonym)5 Synonym (com.ibm.watson.developer_cloud.conversation.v1.model.Synonym)5 Date (java.util.Date)3 GetSynonymOptions (com.ibm.watson.assistant.v1.model.GetSynonymOptions)2