Search in sources :

Example 1 with ListSynonymsOptions

use of com.ibm.watson.assistant.v1.model.ListSynonymsOptions 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.conversation.v1.model.DeleteSynonymOptions) CreateEntityOptions(com.ibm.watson.developer_cloud.conversation.v1.model.CreateEntityOptions) CreateValueOptions(com.ibm.watson.developer_cloud.conversation.v1.model.CreateValueOptions) ListSynonymsOptions(com.ibm.watson.developer_cloud.conversation.v1.model.ListSynonymsOptions) NotFoundException(com.ibm.watson.developer_cloud.service.exception.NotFoundException) CreateSynonymOptions(com.ibm.watson.developer_cloud.conversation.v1.model.CreateSynonymOptions) Synonym(com.ibm.watson.developer_cloud.conversation.v1.model.Synonym) SynonymCollection(com.ibm.watson.developer_cloud.conversation.v1.model.SynonymCollection) Test(org.junit.Test)

Example 2 with ListSynonymsOptions

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

the class AssistantTest method testListSynonymsWOptions.

// Test the listSynonyms operation with a valid options model parameter
@Test
public void testListSynonymsWOptions() throws Throwable {
    // Register a mock response
    String mockResponseBody = "{\"synonyms\": [{\"synonym\": \"synonym\", \"created\": \"2019-01-01T12:00:00.000Z\", \"updated\": \"2019-01-01T12:00:00.000Z\"}], \"pagination\": {\"refresh_url\": \"refreshUrl\", \"next_url\": \"nextUrl\", \"total\": 5, \"matched\": 7, \"refresh_cursor\": \"refreshCursor\", \"next_cursor\": \"nextCursor\"}}";
    String listSynonymsPath = "/v1/workspaces/testString/entities/testString/values/testString/synonyms";
    server.enqueue(new MockResponse().setHeader("Content-type", "application/json").setResponseCode(200).setBody(mockResponseBody));
    // Construct an instance of the ListSynonymsOptions model
    ListSynonymsOptions listSynonymsOptionsModel = new ListSynonymsOptions.Builder().workspaceId("testString").entity("testString").value("testString").pageLimit(Long.valueOf("26")).includeCount(false).sort("synonym").cursor("testString").includeAudit(false).build();
    // Invoke listSynonyms() with a valid options model and verify the result
    Response<SynonymCollection> response = assistantService.listSynonyms(listSynonymsOptionsModel).execute();
    assertNotNull(response);
    SynonymCollection 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, listSynonymsPath);
    // Verify query params
    Map<String, String> query = TestUtilities.parseQueryString(request);
    assertNotNull(query);
    assertEquals(query.get("version"), "testString");
    assertEquals(Long.valueOf(query.get("page_limit")), Long.valueOf("26"));
    assertEquals(Boolean.valueOf(query.get("include_count")), Boolean.valueOf(false));
    assertEquals(query.get("sort"), "synonym");
    assertEquals(query.get("cursor"), "testString");
    assertEquals(Boolean.valueOf(query.get("include_audit")), Boolean.valueOf(false));
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) ListSynonymsOptions(com.ibm.watson.assistant.v1.model.ListSynonymsOptions) SynonymCollection(com.ibm.watson.assistant.v1.model.SynonymCollection) Test(org.testng.annotations.Test)

Example 3 with ListSynonymsOptions

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

the class Assistant method listSynonyms.

/**
 * List entity value synonyms.
 *
 * <p>List the synonyms for an entity value.
 *
 * @param listSynonymsOptions the {@link ListSynonymsOptions} containing the options for the call
 * @return a {@link ServiceCall} with a result of type {@link SynonymCollection}
 */
public ServiceCall<SynonymCollection> listSynonyms(ListSynonymsOptions listSynonymsOptions) {
    com.ibm.cloud.sdk.core.util.Validator.notNull(listSynonymsOptions, "listSynonymsOptions cannot be null");
    Map<String, String> pathParamsMap = new HashMap<String, String>();
    pathParamsMap.put("workspace_id", listSynonymsOptions.workspaceId());
    pathParamsMap.put("entity", listSynonymsOptions.entity());
    pathParamsMap.put("value", listSynonymsOptions.value());
    RequestBuilder builder = RequestBuilder.get(RequestBuilder.resolveRequestUrl(getServiceUrl(), "/v1/workspaces/{workspace_id}/entities/{entity}/values/{value}/synonyms", pathParamsMap));
    Map<String, String> sdkHeaders = SdkCommon.getSdkHeaders("conversation", "v1", "listSynonyms");
    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 (listSynonymsOptions.pageLimit() != null) {
        builder.query("page_limit", String.valueOf(listSynonymsOptions.pageLimit()));
    }
    if (listSynonymsOptions.includeCount() != null) {
        builder.query("include_count", String.valueOf(listSynonymsOptions.includeCount()));
    }
    if (listSynonymsOptions.sort() != null) {
        builder.query("sort", String.valueOf(listSynonymsOptions.sort()));
    }
    if (listSynonymsOptions.cursor() != null) {
        builder.query("cursor", String.valueOf(listSynonymsOptions.cursor()));
    }
    if (listSynonymsOptions.includeAudit() != null) {
        builder.query("include_audit", String.valueOf(listSynonymsOptions.includeAudit()));
    }
    ResponseConverter<SynonymCollection> responseConverter = ResponseConverterUtils.getValue(new com.google.gson.reflect.TypeToken<SynonymCollection>() {
    }.getType());
    return createServiceCall(builder.build(), responseConverter);
}
Also used : RequestBuilder(com.ibm.cloud.sdk.core.http.RequestBuilder) HashMap(java.util.HashMap) SynonymCollection(com.ibm.watson.assistant.v1.model.SynonymCollection)

Example 4 with ListSynonymsOptions

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

use of com.ibm.watson.assistant.v1.model.ListSynonymsOptions 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)

Aggregations

SynonymCollection (com.ibm.watson.assistant.v1.model.SynonymCollection)3 Test (org.junit.Test)3 ListSynonymsOptions (com.ibm.watson.assistant.v1.model.ListSynonymsOptions)2 NotFoundException (com.ibm.watson.developer_cloud.service.exception.NotFoundException)2 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 CreateSynonymOptions (com.ibm.watson.assistant.v1.model.CreateSynonymOptions)1 CreateValueOptions (com.ibm.watson.assistant.v1.model.CreateValueOptions)1 DeleteSynonymOptions (com.ibm.watson.assistant.v1.model.DeleteSynonymOptions)1 Synonym (com.ibm.watson.assistant.v1.model.Synonym)1 CreateEntityOptions (com.ibm.watson.developer_cloud.assistant.v1.model.CreateEntityOptions)1 CreateSynonymOptions (com.ibm.watson.developer_cloud.assistant.v1.model.CreateSynonymOptions)1 CreateValueOptions (com.ibm.watson.developer_cloud.assistant.v1.model.CreateValueOptions)1 DeleteSynonymOptions (com.ibm.watson.developer_cloud.assistant.v1.model.DeleteSynonymOptions)1 ListSynonymsOptions (com.ibm.watson.developer_cloud.assistant.v1.model.ListSynonymsOptions)1 Synonym (com.ibm.watson.developer_cloud.assistant.v1.model.Synonym)1 SynonymCollection (com.ibm.watson.developer_cloud.assistant.v1.model.SynonymCollection)1 CreateEntityOptions (com.ibm.watson.developer_cloud.conversation.v1.model.CreateEntityOptions)1 CreateSynonymOptions (com.ibm.watson.developer_cloud.conversation.v1.model.CreateSynonymOptions)1