Search in sources :

Example 6 with QueryOptions

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

the class DiscoveryTest method testQueryWOptions.

// Test the query operation with a valid options model parameter
@Test
public void testQueryWOptions() throws Throwable {
    // Register a mock response
    String mockResponseBody = "{\"matching_results\": 15, \"results\": [{\"id\": \"id\", \"metadata\": {\"mapKey\": \"anyValue\"}, \"collection_id\": \"collectionId\", \"result_metadata\": {\"score\": 5, \"confidence\": 10}}], \"aggregations\": [{\"type\": \"filter\", \"match\": \"match\", \"matching_results\": 15}], \"passages\": [{\"document_id\": \"documentId\", \"passage_score\": 12, \"passage_text\": \"passageText\", \"start_offset\": 11, \"end_offset\": 9, \"field\": \"field\"}], \"duplicates_removed\": 17, \"session_token\": \"sessionToken\", \"retrieval_details\": {\"document_retrieval_strategy\": \"untrained\"}, \"suggested_query\": \"suggestedQuery\"}";
    String queryPath = "/v1/environments/testString/collections/testString/query";
    server.enqueue(new MockResponse().setHeader("Content-type", "application/json").setResponseCode(200).setBody(mockResponseBody));
    // Construct an instance of the QueryOptions model
    QueryOptions queryOptionsModel = new QueryOptions.Builder().environmentId("testString").collectionId("testString").filter("testString").query("testString").naturalLanguageQuery("testString").passages(true).aggregation("testString").count(Long.valueOf("26")).xReturn("testString").offset(Long.valueOf("26")).sort("testString").highlight(false).passagesFields("testString").passagesCount(Long.valueOf("100")).passagesCharacters(Long.valueOf("50")).deduplicate(false).deduplicateField("testString").similar(false).similarDocumentIds("testString").similarFields("testString").bias("testString").spellingSuggestions(false).xWatsonLoggingOptOut(false).build();
    // Invoke query() with a valid options model and verify the result
    Response<QueryResponse> response = discoveryService.query(queryOptionsModel).execute();
    assertNotNull(response);
    QueryResponse 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, queryPath);
    // 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) LogQueryResponse(com.ibm.watson.discovery.v1.model.LogQueryResponse) QueryResponse(com.ibm.watson.discovery.v1.model.QueryResponse) FederatedQueryOptions(com.ibm.watson.discovery.v1.model.FederatedQueryOptions) GetMetricsQueryOptions(com.ibm.watson.discovery.v1.model.GetMetricsQueryOptions) QueryOptions(com.ibm.watson.discovery.v1.model.QueryOptions) Test(org.testng.annotations.Test)

Aggregations

JsonObject (com.google.gson.JsonObject)3 QueryResponse (com.ibm.watson.discovery.v2.model.QueryResponse)3 FileInputStream (java.io.FileInputStream)3 InputStream (java.io.InputStream)3 JsonPrimitive (com.google.gson.JsonPrimitive)2 WatsonServiceTest (com.ibm.watson.developer_cloud.WatsonServiceTest)2 Collection (com.ibm.watson.developer_cloud.discovery.v1.model.Collection)2 DocumentAccepted (com.ibm.watson.developer_cloud.discovery.v1.model.DocumentAccepted)2 QueryOptions (com.ibm.watson.developer_cloud.discovery.v1.model.QueryOptions)2 QueryResponse (com.ibm.watson.developer_cloud.discovery.v1.model.QueryResponse)2 WaitFor (com.ibm.watson.developer_cloud.util.WaitFor)2 QueryOptions (com.ibm.watson.discovery.v2.model.QueryOptions)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 MockResponse (okhttp3.mockwebserver.MockResponse)2 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)2 Ignore (org.junit.Ignore)2 Test (org.junit.Test)2 Test (org.testng.annotations.Test)2 RequestBuilder (com.ibm.cloud.sdk.core.http.RequestBuilder)1 Authenticator (com.ibm.cloud.sdk.core.security.Authenticator)1