Search in sources :

Example 1 with GetAutocompletionOptions

use of com.ibm.watson.discovery.v2.model.GetAutocompletionOptions in project java-sdk by watson-developer-cloud.

the class DiscoveryTest method testGetAutocompletionWOptions.

// Test the getAutocompletion operation with a valid options model parameter
@Test
public void testGetAutocompletionWOptions() throws Throwable {
    // Register a mock response
    String mockResponseBody = "{\"completions\": [\"completions\"]}";
    String getAutocompletionPath = "/v1/environments/testString/collections/testString/autocompletion";
    server.enqueue(new MockResponse().setHeader("Content-type", "application/json").setResponseCode(200).setBody(mockResponseBody));
    // Construct an instance of the GetAutocompletionOptions model
    GetAutocompletionOptions getAutocompletionOptionsModel = new GetAutocompletionOptions.Builder().environmentId("testString").collectionId("testString").prefix("testString").field("testString").count(Long.valueOf("26")).build();
    // Invoke getAutocompletion() with a valid options model and verify the result
    Response<Completions> response = discoveryService.getAutocompletion(getAutocompletionOptionsModel).execute();
    assertNotNull(response);
    Completions 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, getAutocompletionPath);
    // Verify query params
    Map<String, String> query = TestUtilities.parseQueryString(request);
    assertNotNull(query);
    assertEquals(query.get("version"), "testString");
    assertEquals(query.get("prefix"), "testString");
    assertEquals(query.get("field"), "testString");
    assertEquals(Long.valueOf(query.get("count")), Long.valueOf("26"));
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) GetAutocompletionOptions(com.ibm.watson.discovery.v1.model.GetAutocompletionOptions) Completions(com.ibm.watson.discovery.v1.model.Completions) Test(org.testng.annotations.Test)

Example 2 with GetAutocompletionOptions

use of com.ibm.watson.discovery.v2.model.GetAutocompletionOptions in project java-sdk by watson-developer-cloud.

the class DiscoveryTest method testGetAutocompletionWOptions.

@Test
public void testGetAutocompletionWOptions() throws Throwable {
    // Schedule some responses.
    String mockResponseBody = "{\"completions\": [\"completions\"]}";
    String getAutocompletionPath = "/v2/projects/testString/autocompletion";
    server.enqueue(new MockResponse().setHeader("Content-type", "application/json").setResponseCode(200).setBody(mockResponseBody));
    constructClientService();
    // Construct an instance of the GetAutocompletionOptions model
    GetAutocompletionOptions getAutocompletionOptionsModel = new GetAutocompletionOptions.Builder().projectId("testString").prefix("testString").collectionIds(new java.util.ArrayList<String>(java.util.Arrays.asList("testString"))).field("testString").count(Long.valueOf("26")).build();
    // Invoke operation with valid options model (positive test)
    Response<Completions> response = discoveryService.getAutocompletion(getAutocompletionOptionsModel).execute();
    assertNotNull(response);
    Completions responseObj = response.getResult();
    assertNotNull(responseObj);
    // Verify the contents of the request
    RecordedRequest request = server.takeRequest();
    assertNotNull(request);
    assertEquals(request.getMethod(), "GET");
    // Check query
    Map<String, String> query = TestUtilities.parseQueryString(request);
    assertNotNull(query);
    // Get query params
    assertEquals(query.get("version"), "testString");
    assertEquals(query.get("prefix"), "testString");
    assertEquals(query.get("collection_ids"), RequestUtils.join(new java.util.ArrayList<String>(java.util.Arrays.asList("testString")), ","));
    assertEquals(query.get("field"), "testString");
    assertEquals(Long.valueOf(query.get("count")), Long.valueOf("26"));
    // Check request path
    String parsedPath = TestUtilities.parseReqPath(request);
    assertEquals(parsedPath, getAutocompletionPath);
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) GetAutocompletionOptions(com.ibm.watson.discovery.v2.model.GetAutocompletionOptions) Completions(com.ibm.watson.discovery.v2.model.Completions) Test(org.testng.annotations.Test)

Example 3 with GetAutocompletionOptions

use of com.ibm.watson.discovery.v2.model.GetAutocompletionOptions in project java-sdk by watson-developer-cloud.

the class Discovery method getAutocompletion.

/**
 * Get Autocomplete Suggestions.
 *
 * <p>Returns completion query suggestions for the specified prefix.
 *
 * @param getAutocompletionOptions the {@link GetAutocompletionOptions} containing the options for
 *     the call
 * @return a {@link ServiceCall} with a result of type {@link Completions}
 */
public ServiceCall<Completions> getAutocompletion(GetAutocompletionOptions getAutocompletionOptions) {
    com.ibm.cloud.sdk.core.util.Validator.notNull(getAutocompletionOptions, "getAutocompletionOptions cannot be null");
    Map<String, String> pathParamsMap = new HashMap<String, String>();
    pathParamsMap.put("project_id", getAutocompletionOptions.projectId());
    RequestBuilder builder = RequestBuilder.get(RequestBuilder.resolveRequestUrl(getServiceUrl(), "/v2/projects/{project_id}/autocompletion", pathParamsMap));
    Map<String, String> sdkHeaders = SdkCommon.getSdkHeaders("discovery", "v2", "getAutocompletion");
    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));
    builder.query("prefix", String.valueOf(getAutocompletionOptions.prefix()));
    if (getAutocompletionOptions.collectionIds() != null) {
        builder.query("collection_ids", RequestUtils.join(getAutocompletionOptions.collectionIds(), ","));
    }
    if (getAutocompletionOptions.field() != null) {
        builder.query("field", String.valueOf(getAutocompletionOptions.field()));
    }
    if (getAutocompletionOptions.count() != null) {
        builder.query("count", String.valueOf(getAutocompletionOptions.count()));
    }
    ResponseConverter<Completions> responseConverter = ResponseConverterUtils.getValue(new com.google.gson.reflect.TypeToken<Completions>() {
    }.getType());
    return createServiceCall(builder.build(), responseConverter);
}
Also used : RequestBuilder(com.ibm.cloud.sdk.core.http.RequestBuilder) HashMap(java.util.HashMap) Completions(com.ibm.watson.discovery.v2.model.Completions)

Aggregations

Completions (com.ibm.watson.discovery.v2.model.Completions)2 MockResponse (okhttp3.mockwebserver.MockResponse)2 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)2 Test (org.testng.annotations.Test)2 RequestBuilder (com.ibm.cloud.sdk.core.http.RequestBuilder)1 Completions (com.ibm.watson.discovery.v1.model.Completions)1 GetAutocompletionOptions (com.ibm.watson.discovery.v1.model.GetAutocompletionOptions)1 GetAutocompletionOptions (com.ibm.watson.discovery.v2.model.GetAutocompletionOptions)1 HashMap (java.util.HashMap)1