Search in sources :

Example 16 with Synonym

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

the class Assistant method createSynonym.

/**
 * Create entity value synonym.
 *
 * <p>Add a new synonym to an entity value.
 *
 * <p>If you want to create multiple synonyms with a single API call, consider using the **[Update
 * entity](#update-entity)** or **[Update entity value](#update-entity-value)** method instead.
 *
 * @param createSynonymOptions the {@link CreateSynonymOptions} containing the options for the
 *     call
 * @return a {@link ServiceCall} with a result of type {@link Synonym}
 */
public ServiceCall<Synonym> createSynonym(CreateSynonymOptions createSynonymOptions) {
    com.ibm.cloud.sdk.core.util.Validator.notNull(createSynonymOptions, "createSynonymOptions cannot be null");
    Map<String, String> pathParamsMap = new HashMap<String, String>();
    pathParamsMap.put("workspace_id", createSynonymOptions.workspaceId());
    pathParamsMap.put("entity", createSynonymOptions.entity());
    pathParamsMap.put("value", createSynonymOptions.value());
    RequestBuilder builder = RequestBuilder.post(RequestBuilder.resolveRequestUrl(getServiceUrl(), "/v1/workspaces/{workspace_id}/entities/{entity}/values/{value}/synonyms", pathParamsMap));
    Map<String, String> sdkHeaders = SdkCommon.getSdkHeaders("conversation", "v1", "createSynonym");
    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 (createSynonymOptions.includeAudit() != null) {
        builder.query("include_audit", String.valueOf(createSynonymOptions.includeAudit()));
    }
    final JsonObject contentJson = new JsonObject();
    contentJson.addProperty("synonym", createSynonymOptions.synonym());
    builder.bodyJson(contentJson);
    ResponseConverter<Synonym> responseConverter = ResponseConverterUtils.getValue(new com.google.gson.reflect.TypeToken<Synonym>() {
    }.getType());
    return createServiceCall(builder.build(), responseConverter);
}
Also used : RequestBuilder(com.ibm.cloud.sdk.core.http.RequestBuilder) HashMap(java.util.HashMap) JsonObject(com.google.gson.JsonObject) Synonym(com.ibm.watson.assistant.v1.model.Synonym)

Example 17 with Synonym

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

the class AssistantTest method testGetSynonymWOptions.

// Test the getSynonym operation with a valid options model parameter
@Test
public void testGetSynonymWOptions() throws Throwable {
    // Register a mock response
    String mockResponseBody = "{\"synonym\": \"synonym\", \"created\": \"2019-01-01T12:00:00.000Z\", \"updated\": \"2019-01-01T12:00:00.000Z\"}";
    String getSynonymPath = "/v1/workspaces/testString/entities/testString/values/testString/synonyms/testString";
    server.enqueue(new MockResponse().setHeader("Content-type", "application/json").setResponseCode(200).setBody(mockResponseBody));
    // Construct an instance of the GetSynonymOptions model
    GetSynonymOptions getSynonymOptionsModel = new GetSynonymOptions.Builder().workspaceId("testString").entity("testString").value("testString").synonym("testString").includeAudit(false).build();
    // Invoke getSynonym() with a valid options model and verify the result
    Response<Synonym> response = assistantService.getSynonym(getSynonymOptionsModel).execute();
    assertNotNull(response);
    Synonym 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, getSynonymPath);
    // 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) GetSynonymOptions(com.ibm.watson.assistant.v1.model.GetSynonymOptions) Synonym(com.ibm.watson.assistant.v1.model.Synonym) Test(org.testng.annotations.Test)

Example 18 with Synonym

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

the class SynonymsIT method testDeleteSynonym.

/**
 * Test deleteSynonym.
 */
@Test
public void testDeleteSynonym() {
    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"));
    }
    CreateSynonymOptions createOptions = new CreateSynonymOptions.Builder(workspaceId, entity, entityValue, synonym).build();
    Synonym response = service.createSynonym(createOptions).execute().getResult();
    try {
        assertNotNull(response);
        assertNotNull(response.synonym());
        assertEquals(response.synonym(), synonym);
    } catch (Exception ex) {
        DeleteSynonymOptions deleteOptions = new DeleteSynonymOptions.Builder(workspaceId, entity, entityValue, synonym).build();
        service.deleteSynonym(deleteOptions).execute().getResult();
        fail(ex.getMessage());
    }
    DeleteSynonymOptions deleteOptions = new DeleteSynonymOptions.Builder().workspaceId(workspaceId).entity(entity).value(entityValue).synonym(synonym).build();
    service.deleteSynonym(deleteOptions).execute().getResult();
    try {
        GetSynonymOptions getOptions = new GetSynonymOptions.Builder(workspaceId, entity, entityValue, synonym).build();
        service.getSynonym(getOptions).execute().getResult();
        fail("deleteSynonym failed");
    } catch (Exception ex) {
        // Expected result
        assertTrue(ex instanceof NotFoundException);
    }
}
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) 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) Test(org.junit.Test)

Example 19 with Synonym

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

the class SynonymsIT method testCreateSynonym.

/**
 * Test createSynonym.
 */
@Test
public void testCreateSynonym() {
    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"));
    }
    CreateSynonymOptions createOptions = new CreateSynonymOptions.Builder().workspaceId(workspaceId).entity(entity).value(entityValue).synonym(synonym).build();
    Synonym response = service.createSynonym(createOptions).execute().getResult();
    try {
        assertNotNull(response);
        assertNotNull(response.synonym());
        assertEquals(response.synonym(), synonym);
    } catch (Exception ex) {
        fail(ex.getMessage());
    } finally {
        // Clean up
        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) CreateSynonymOptions(com.ibm.watson.assistant.v1.model.CreateSynonymOptions) CreateValueOptions(com.ibm.watson.assistant.v1.model.CreateValueOptions) Synonym(com.ibm.watson.assistant.v1.model.Synonym) NotFoundException(com.ibm.cloud.sdk.core.service.exception.NotFoundException) Test(org.junit.Test)

Example 20 with Synonym

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

the class SynonymsIT method testListSynonymsWithPaging.

/**
 * Test listSynonyms with pagination.
 */
@Test
public void testListSynonymsWithPaging() {
    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"));
    }
    CreateSynonymOptions createOptions = new CreateSynonymOptions.Builder(workspaceId, entity, entityValue, synonym1).build();
    try {
        service.createSynonym(createOptions).execute().getResult();
    } catch (Exception ex) {
        // Exception is okay if is for Unique Violation
        assertTrue(ex.getLocalizedMessage().startsWith("Unique Violation"));
    }
    try {
        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.Builder listOptionsBuilder = new ListSynonymsOptions.Builder(workspaceId, entity, entityValue);
        listOptionsBuilder.sort("modified");
        listOptionsBuilder.pageLimit(1L);
        SynonymCollection response = service.listSynonyms(listOptionsBuilder.build()).execute().getResult();
        assertNotNull(response);
        assertNotNull(response.getPagination());
        assertNotNull(response.getPagination().getRefreshUrl());
        assertNotNull(response.getPagination().getNextUrl());
        assertNotNull(response.getPagination().getNextCursor());
        boolean found1 = false;
        boolean found2 = false;
        while (true) {
            assertNotNull(response.getSynonyms());
            assertTrue(response.getSynonyms().size() == 1);
            found1 |= response.getSynonyms().get(0).synonym().equals(synonym1);
            found2 |= response.getSynonyms().get(0).synonym().equals(synonym2);
            if (response.getPagination().getNextCursor() == null) {
                break;
            }
            String cursor = response.getPagination().getNextCursor();
            response = service.listSynonyms(listOptionsBuilder.cursor(cursor).build()).execute().getResult();
        }
        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) SynonymCollection(com.ibm.watson.assistant.v1.model.SynonymCollection) Test(org.junit.Test)

Aggregations

MockResponse (okhttp3.mockwebserver.MockResponse)17 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)17 Test (org.testng.annotations.Test)17 Test (org.junit.Test)16 Synonym (com.ibm.watson.assistant.v1.model.Synonym)11 NotFoundException (com.ibm.watson.developer_cloud.service.exception.NotFoundException)10 Synonym (com.ibm.watson.developer_cloud.assistant.v1.model.Synonym)8 Synonym (com.ibm.watson.developer_cloud.conversation.v1.model.Synonym)8 CreateEntityOptions (com.ibm.watson.assistant.v1.model.CreateEntityOptions)7 CreateSynonymOptions (com.ibm.watson.assistant.v1.model.CreateSynonymOptions)7 CreateValue (com.ibm.watson.assistant.v1.model.CreateValue)7 CreateValueOptions (com.ibm.watson.assistant.v1.model.CreateValueOptions)7 DeleteSynonymOptions (com.ibm.watson.assistant.v1.model.DeleteSynonymOptions)7 HashMap (java.util.HashMap)7 JsonObject (com.google.gson.JsonObject)6 NotFoundException (com.ibm.cloud.sdk.core.service.exception.NotFoundException)6 RequestBuilder (com.ibm.watson.developer_cloud.http.RequestBuilder)6 CreateEntity (com.ibm.watson.assistant.v1.model.CreateEntity)5 CreateEntityOptions (com.ibm.watson.developer_cloud.assistant.v1.model.CreateEntityOptions)5 CreateSynonymOptions (com.ibm.watson.developer_cloud.assistant.v1.model.CreateSynonymOptions)5