Search in sources :

Example 1 with PostPartitionSearchOptions

use of com.ibm.cloud.cloudant.v1.model.PostPartitionSearchOptions in project cloudant-java-sdk by IBM.

the class CloudantTest method testPostPartitionSearchAsStreamWOptions.

@Test
public void testPostPartitionSearchAsStreamWOptions() throws Throwable {
    // Schedule some responses.
    String mockResponseBody = "{\"foo\": \"this is a mock response for JSON streaming\"}";
    String postPartitionSearchAsStreamPath = "/testString/_partition/testString/_design/testString/_search/testString";
    server.enqueue(new MockResponse().setHeader("Content-type", "application/json").setResponseCode(200).setBody(mockResponseBody));
    constructClientService();
    // Construct an instance of the PostPartitionSearchOptions model
    PostPartitionSearchOptions postPartitionSearchOptionsModel = new PostPartitionSearchOptions.Builder().db("testString").partitionKey("testString").ddoc("testString").index("testString").query("testString").bookmark("testString").highlightFields(new java.util.ArrayList<String>(java.util.Arrays.asList("testString"))).highlightNumber(Long.valueOf("1")).highlightPostTag("</em>").highlightPreTag("<em>").highlightSize(Long.valueOf("1")).includeDocs(false).includeFields(new java.util.ArrayList<String>(java.util.Arrays.asList("testString"))).limit(Long.valueOf("3")).sort(new java.util.ArrayList<String>(java.util.Arrays.asList("testString"))).stale("ok").build();
    // Invoke operation with valid options model (positive test)
    Response<InputStream> response = cloudantService.postPartitionSearchAsStream(postPartitionSearchOptionsModel).execute();
    assertNotNull(response);
    InputStream responseObj = response.getResult();
    assertNotNull(responseObj);
    // Verify the contents of the request
    RecordedRequest request = server.takeRequest();
    assertNotNull(request);
    assertEquals(request.getMethod(), "POST");
    // Check query
    Map<String, String> query = TestUtilities.parseQueryString(request);
    assertNull(query);
    // Check request path
    String parsedPath = TestUtilities.parseReqPath(request);
    assertEquals(parsedPath, postPartitionSearchAsStreamPath);
    // Verify streamed JSON response
    java.util.Scanner s = new java.util.Scanner(responseObj).useDelimiter("\\A");
    String streamedResponseBody = s.hasNext() ? s.next() : "";
    assertEquals(streamedResponseBody, mockResponseBody);
}
Also used : PostPartitionSearchOptions(com.ibm.cloud.cloudant.v1.model.PostPartitionSearchOptions) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) InputStream(java.io.InputStream) Test(org.testng.annotations.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 2 with PostPartitionSearchOptions

use of com.ibm.cloud.cloudant.v1.model.PostPartitionSearchOptions in project cloudant-java-sdk by IBM.

the class PostPartitionSearchOptionsTest method testPostPartitionSearchOptions.

@Test
public void testPostPartitionSearchOptions() throws Throwable {
    PostPartitionSearchOptions postPartitionSearchOptionsModel = new PostPartitionSearchOptions.Builder().db("testString").partitionKey("testString").ddoc("testString").index("testString").query("testString").bookmark("testString").highlightFields(new java.util.ArrayList<String>(java.util.Arrays.asList("testString"))).highlightNumber(Long.valueOf("1")).highlightPostTag("</em>").highlightPreTag("<em>").highlightSize(Long.valueOf("1")).includeDocs(false).includeFields(new java.util.ArrayList<String>(java.util.Arrays.asList("testString"))).limit(Long.valueOf("0")).sort(new java.util.ArrayList<String>(java.util.Arrays.asList("testString"))).stale("ok").build();
    assertEquals(postPartitionSearchOptionsModel.db(), "testString");
    assertEquals(postPartitionSearchOptionsModel.partitionKey(), "testString");
    assertEquals(postPartitionSearchOptionsModel.ddoc(), "testString");
    assertEquals(postPartitionSearchOptionsModel.index(), "testString");
    assertEquals(postPartitionSearchOptionsModel.query(), "testString");
    assertEquals(postPartitionSearchOptionsModel.bookmark(), "testString");
    assertEquals(postPartitionSearchOptionsModel.highlightFields(), new java.util.ArrayList<String>(java.util.Arrays.asList("testString")));
    assertEquals(postPartitionSearchOptionsModel.highlightNumber(), Long.valueOf("1"));
    assertEquals(postPartitionSearchOptionsModel.highlightPostTag(), "</em>");
    assertEquals(postPartitionSearchOptionsModel.highlightPreTag(), "<em>");
    assertEquals(postPartitionSearchOptionsModel.highlightSize(), Long.valueOf("1"));
    assertEquals(postPartitionSearchOptionsModel.includeDocs(), Boolean.valueOf(false));
    assertEquals(postPartitionSearchOptionsModel.includeFields(), new java.util.ArrayList<String>(java.util.Arrays.asList("testString")));
    assertEquals(postPartitionSearchOptionsModel.limit(), Long.valueOf("0"));
    assertEquals(postPartitionSearchOptionsModel.sort(), new java.util.ArrayList<String>(java.util.Arrays.asList("testString")));
    assertEquals(postPartitionSearchOptionsModel.stale(), "ok");
}
Also used : PostPartitionSearchOptions(com.ibm.cloud.cloudant.v1.model.PostPartitionSearchOptions) Test(org.testng.annotations.Test)

Example 3 with PostPartitionSearchOptions

use of com.ibm.cloud.cloudant.v1.model.PostPartitionSearchOptions in project cloudant-java-sdk by IBM.

the class CloudantTest method testPostPartitionSearchWOptions.

@Test
public void testPostPartitionSearchWOptions() throws Throwable {
    // Schedule some responses.
    String mockResponseBody = "{\"total_rows\": 0, \"bookmark\": \"bookmark\", \"by\": \"by\", \"counts\": {\"mapKey\": {\"mapKey\": 0}}, \"ranges\": {\"mapKey\": {\"mapKey\": 0}}, \"rows\": [{\"doc\": {\"_attachments\": {\"mapKey\": {\"content_type\": \"contentType\", \"data\": \"VGhpcyBpcyBhbiBlbmNvZGVkIGJ5dGUgYXJyYXku\", \"digest\": \"digest\", \"encoded_length\": 0, \"encoding\": \"encoding\", \"follows\": false, \"length\": 0, \"revpos\": 1, \"stub\": true}}, \"_conflicts\": [\"conflicts\"], \"_deleted\": false, \"_deleted_conflicts\": [\"deletedConflicts\"], \"_id\": \"id\", \"_local_seq\": \"localSeq\", \"_rev\": \"rev\", \"_revisions\": {\"ids\": [\"ids\"], \"start\": 1}, \"_revs_info\": [{\"rev\": \"rev\", \"status\": \"available\"}]}, \"fields\": {\"mapKey\": \"anyValue\"}, \"highlights\": {\"mapKey\": [\"inner\"]}, \"id\": \"id\"}], \"groups\": [{\"total_rows\": 0, \"bookmark\": \"bookmark\", \"by\": \"by\", \"counts\": {\"mapKey\": {\"mapKey\": 0}}, \"ranges\": {\"mapKey\": {\"mapKey\": 0}}, \"rows\": [{\"doc\": {\"_attachments\": {\"mapKey\": {\"content_type\": \"contentType\", \"data\": \"VGhpcyBpcyBhbiBlbmNvZGVkIGJ5dGUgYXJyYXku\", \"digest\": \"digest\", \"encoded_length\": 0, \"encoding\": \"encoding\", \"follows\": false, \"length\": 0, \"revpos\": 1, \"stub\": true}}, \"_conflicts\": [\"conflicts\"], \"_deleted\": false, \"_deleted_conflicts\": [\"deletedConflicts\"], \"_id\": \"id\", \"_local_seq\": \"localSeq\", \"_rev\": \"rev\", \"_revisions\": {\"ids\": [\"ids\"], \"start\": 1}, \"_revs_info\": [{\"rev\": \"rev\", \"status\": \"available\"}]}, \"fields\": {\"mapKey\": \"anyValue\"}, \"highlights\": {\"mapKey\": [\"inner\"]}, \"id\": \"id\"}]}]}";
    String postPartitionSearchPath = "/testString/_partition/testString/_design/testString/_search/testString";
    server.enqueue(new MockResponse().setHeader("Content-type", "application/json").setResponseCode(200).setBody(mockResponseBody));
    constructClientService();
    // Construct an instance of the PostPartitionSearchOptions model
    PostPartitionSearchOptions postPartitionSearchOptionsModel = new PostPartitionSearchOptions.Builder().db("testString").partitionKey("testString").ddoc("testString").index("testString").query("testString").bookmark("testString").highlightFields(new java.util.ArrayList<String>(java.util.Arrays.asList("testString"))).highlightNumber(Long.valueOf("1")).highlightPostTag("</em>").highlightPreTag("<em>").highlightSize(Long.valueOf("1")).includeDocs(false).includeFields(new java.util.ArrayList<String>(java.util.Arrays.asList("testString"))).limit(Long.valueOf("3")).sort(new java.util.ArrayList<String>(java.util.Arrays.asList("testString"))).stale("ok").build();
    // Invoke operation with valid options model (positive test)
    Response<SearchResult> response = cloudantService.postPartitionSearch(postPartitionSearchOptionsModel).execute();
    assertNotNull(response);
    SearchResult responseObj = response.getResult();
    assertNotNull(responseObj);
    // Verify the contents of the request
    RecordedRequest request = server.takeRequest();
    assertNotNull(request);
    assertEquals(request.getMethod(), "POST");
    // Check query
    Map<String, String> query = TestUtilities.parseQueryString(request);
    assertNull(query);
    // Check request path
    String parsedPath = TestUtilities.parseReqPath(request);
    assertEquals(parsedPath, postPartitionSearchPath);
}
Also used : PostPartitionSearchOptions(com.ibm.cloud.cloudant.v1.model.PostPartitionSearchOptions) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) SearchResult(com.ibm.cloud.cloudant.v1.model.SearchResult) Test(org.testng.annotations.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 4 with PostPartitionSearchOptions

use of com.ibm.cloud.cloudant.v1.model.PostPartitionSearchOptions in project cloudant-java-sdk by IBM.

the class Cloudant method postPartitionSearch.

/**
 * Query a database partition search index.
 *
 * Partitioned Search indexes, which are defined in design documents, allow partition databases to be queried by using
 * Lucene Query Parser Syntax. Search indexes are defined by an index function, similar to a map function in MapReduce
 * views. The index function decides what data to index and store in the index.
 *
 * @param postPartitionSearchOptions the {@link PostPartitionSearchOptions} containing the options for the call
 * @return a {@link ServiceCall} with a result of type {@link SearchResult}
 */
public ServiceCall<SearchResult> postPartitionSearch(PostPartitionSearchOptions postPartitionSearchOptions) {
    com.ibm.cloud.sdk.core.util.Validator.notNull(postPartitionSearchOptions, "postPartitionSearchOptions cannot be null");
    Map<String, String> pathParamsMap = new HashMap<String, String>();
    pathParamsMap.put("db", postPartitionSearchOptions.db());
    pathParamsMap.put("partition_key", postPartitionSearchOptions.partitionKey());
    pathParamsMap.put("ddoc", postPartitionSearchOptions.ddoc());
    pathParamsMap.put("index", postPartitionSearchOptions.index());
    RequestBuilder builder = RequestBuilder.post(RequestBuilder.resolveRequestUrl(getServiceUrl(), "/{db}/_partition/{partition_key}/_design/{ddoc}/_search/{index}", pathParamsMap));
    Map<String, String> sdkHeaders = SdkCommon.getSdkHeaders("cloudant", "v1", "postPartitionSearch");
    for (Entry<String, String> header : sdkHeaders.entrySet()) {
        builder.header(header.getKey(), header.getValue());
    }
    builder.header("Accept", "application/json");
    final JsonObject contentJson = new JsonObject();
    contentJson.addProperty("query", postPartitionSearchOptions.query());
    if (postPartitionSearchOptions.bookmark() != null) {
        contentJson.addProperty("bookmark", postPartitionSearchOptions.bookmark());
    }
    if (postPartitionSearchOptions.highlightFields() != null) {
        contentJson.add("highlight_fields", com.ibm.cloud.sdk.core.util.GsonSingleton.getGson().toJsonTree(postPartitionSearchOptions.highlightFields()));
    }
    if (postPartitionSearchOptions.highlightNumber() != null) {
        contentJson.addProperty("highlight_number", postPartitionSearchOptions.highlightNumber());
    }
    if (postPartitionSearchOptions.highlightPostTag() != null) {
        contentJson.addProperty("highlight_post_tag", postPartitionSearchOptions.highlightPostTag());
    }
    if (postPartitionSearchOptions.highlightPreTag() != null) {
        contentJson.addProperty("highlight_pre_tag", postPartitionSearchOptions.highlightPreTag());
    }
    if (postPartitionSearchOptions.highlightSize() != null) {
        contentJson.addProperty("highlight_size", postPartitionSearchOptions.highlightSize());
    }
    if (postPartitionSearchOptions.includeDocs() != null) {
        contentJson.addProperty("include_docs", postPartitionSearchOptions.includeDocs());
    }
    if (postPartitionSearchOptions.includeFields() != null) {
        contentJson.add("include_fields", com.ibm.cloud.sdk.core.util.GsonSingleton.getGson().toJsonTree(postPartitionSearchOptions.includeFields()));
    }
    if (postPartitionSearchOptions.limit() != null) {
        contentJson.addProperty("limit", postPartitionSearchOptions.limit());
    }
    if (postPartitionSearchOptions.sort() != null) {
        contentJson.add("sort", com.ibm.cloud.sdk.core.util.GsonSingleton.getGson().toJsonTree(postPartitionSearchOptions.sort()));
    }
    if (postPartitionSearchOptions.stale() != null) {
        contentJson.addProperty("stale", postPartitionSearchOptions.stale());
    }
    builder.bodyJson(contentJson);
    ResponseConverter<SearchResult> responseConverter = ResponseConverterUtils.getValue(new com.google.gson.reflect.TypeToken<SearchResult>() {
    }.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) SearchResult(com.ibm.cloud.cloudant.v1.model.SearchResult)

Aggregations

PostPartitionSearchOptions (com.ibm.cloud.cloudant.v1.model.PostPartitionSearchOptions)3 Test (org.testng.annotations.Test)3 SearchResult (com.ibm.cloud.cloudant.v1.model.SearchResult)2 MockResponse (okhttp3.mockwebserver.MockResponse)2 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)2 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)2 JsonObject (com.google.gson.JsonObject)1 RequestBuilder (com.ibm.cloud.sdk.core.http.RequestBuilder)1 InputStream (java.io.InputStream)1 HashMap (java.util.HashMap)1