Search in sources :

Example 1 with PostPartitionViewOptions

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

the class CloudantTest method testPostPartitionViewAsStreamWOptions.

@Test
public void testPostPartitionViewAsStreamWOptions() throws Throwable {
    // Schedule some responses.
    String mockResponseBody = "{\"foo\": \"this is a mock response for JSON streaming\"}";
    String postPartitionViewAsStreamPath = "/testString/_partition/testString/_design/testString/_view/testString";
    server.enqueue(new MockResponse().setHeader("Content-type", "application/json").setResponseCode(200).setBody(mockResponseBody));
    constructClientService();
    // Construct an instance of the PostPartitionViewOptions model
    PostPartitionViewOptions postPartitionViewOptionsModel = new PostPartitionViewOptions.Builder().db("testString").partitionKey("testString").ddoc("testString").view("testString").attEncodingInfo(false).attachments(false).conflicts(false).descending(false).includeDocs(true).inclusiveEnd(true).limit(Long.valueOf("10")).skip(Long.valueOf("0")).updateSeq(false).endkey("testString").endkeyDocid("testString").group(false).groupLevel(Long.valueOf("1")).key("testString").keys(new java.util.ArrayList<Object>(java.util.Arrays.asList("examplekey"))).reduce(true).stable(false).startkey("testString").startkeyDocid("testString").update("true").build();
    // Invoke operation with valid options model (positive test)
    Response<InputStream> response = cloudantService.postPartitionViewAsStream(postPartitionViewOptionsModel).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, postPartitionViewAsStreamPath);
    // 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 : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) PostPartitionViewOptions(com.ibm.cloud.cloudant.v1.model.PostPartitionViewOptions) InputStream(java.io.InputStream) SecurityObject(com.ibm.cloud.cloudant.v1.model.SecurityObject) GeoJsonGeometryObject(com.ibm.cloud.cloudant.v1.model.GeoJsonGeometryObject) Test(org.testng.annotations.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 2 with PostPartitionViewOptions

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

the class CloudantTest method testPostPartitionViewWOptions.

@Test
public void testPostPartitionViewWOptions() throws Throwable {
    // Schedule some responses.
    String mockResponseBody = "{\"total_rows\": 0, \"update_seq\": \"updateSeq\", \"rows\": [{\"caused_by\": \"causedBy\", \"error\": \"error\", \"reason\": \"reason\", \"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\"}]}, \"id\": \"id\", \"key\": \"anyValue\", \"value\": \"anyValue\"}]}";
    String postPartitionViewPath = "/testString/_partition/testString/_design/testString/_view/testString";
    server.enqueue(new MockResponse().setHeader("Content-type", "application/json").setResponseCode(200).setBody(mockResponseBody));
    constructClientService();
    // Construct an instance of the PostPartitionViewOptions model
    PostPartitionViewOptions postPartitionViewOptionsModel = new PostPartitionViewOptions.Builder().db("testString").partitionKey("testString").ddoc("testString").view("testString").attEncodingInfo(false).attachments(false).conflicts(false).descending(false).includeDocs(true).inclusiveEnd(true).limit(Long.valueOf("10")).skip(Long.valueOf("0")).updateSeq(false).endkey("testString").endkeyDocid("testString").group(false).groupLevel(Long.valueOf("1")).key("testString").keys(new java.util.ArrayList<Object>(java.util.Arrays.asList("examplekey"))).reduce(true).stable(false).startkey("testString").startkeyDocid("testString").update("true").build();
    // Invoke operation with valid options model (positive test)
    Response<ViewResult> response = cloudantService.postPartitionView(postPartitionViewOptionsModel).execute();
    assertNotNull(response);
    ViewResult 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, postPartitionViewPath);
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) PostPartitionViewOptions(com.ibm.cloud.cloudant.v1.model.PostPartitionViewOptions) SecurityObject(com.ibm.cloud.cloudant.v1.model.SecurityObject) GeoJsonGeometryObject(com.ibm.cloud.cloudant.v1.model.GeoJsonGeometryObject) ViewResult(com.ibm.cloud.cloudant.v1.model.ViewResult) Test(org.testng.annotations.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 3 with PostPartitionViewOptions

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

the class PostPartitionViewOptionsTest method testPostPartitionViewOptions.

@Test
public void testPostPartitionViewOptions() throws Throwable {
    PostPartitionViewOptions postPartitionViewOptionsModel = new PostPartitionViewOptions.Builder().db("testString").partitionKey("testString").ddoc("testString").view("testString").attEncodingInfo(false).attachments(false).conflicts(false).descending(false).includeDocs(false).inclusiveEnd(true).limit(Long.valueOf("0")).skip(Long.valueOf("0")).updateSeq(false).endkey("testString").endkeyDocid("testString").group(false).groupLevel(Long.valueOf("1")).key("testString").keys(new java.util.ArrayList<Object>(java.util.Arrays.asList("testString"))).reduce(true).stable(false).startkey("testString").startkeyDocid("testString").update("true").build();
    assertEquals(postPartitionViewOptionsModel.db(), "testString");
    assertEquals(postPartitionViewOptionsModel.partitionKey(), "testString");
    assertEquals(postPartitionViewOptionsModel.ddoc(), "testString");
    assertEquals(postPartitionViewOptionsModel.view(), "testString");
    assertEquals(postPartitionViewOptionsModel.attEncodingInfo(), Boolean.valueOf(false));
    assertEquals(postPartitionViewOptionsModel.attachments(), Boolean.valueOf(false));
    assertEquals(postPartitionViewOptionsModel.conflicts(), Boolean.valueOf(false));
    assertEquals(postPartitionViewOptionsModel.descending(), Boolean.valueOf(false));
    assertEquals(postPartitionViewOptionsModel.includeDocs(), Boolean.valueOf(false));
    assertEquals(postPartitionViewOptionsModel.inclusiveEnd(), Boolean.valueOf(true));
    assertEquals(postPartitionViewOptionsModel.limit(), Long.valueOf("0"));
    assertEquals(postPartitionViewOptionsModel.skip(), Long.valueOf("0"));
    assertEquals(postPartitionViewOptionsModel.updateSeq(), Boolean.valueOf(false));
    assertEquals(postPartitionViewOptionsModel.endkey(), "testString");
    assertEquals(postPartitionViewOptionsModel.endkeyDocid(), "testString");
    assertEquals(postPartitionViewOptionsModel.group(), Boolean.valueOf(false));
    assertEquals(postPartitionViewOptionsModel.groupLevel(), Long.valueOf("1"));
    assertEquals(postPartitionViewOptionsModel.key(), "testString");
    assertEquals(postPartitionViewOptionsModel.keys(), new java.util.ArrayList<Object>(java.util.Arrays.asList("testString")));
    assertEquals(postPartitionViewOptionsModel.reduce(), Boolean.valueOf(true));
    assertEquals(postPartitionViewOptionsModel.stable(), Boolean.valueOf(false));
    assertEquals(postPartitionViewOptionsModel.startkey(), "testString");
    assertEquals(postPartitionViewOptionsModel.startkeyDocid(), "testString");
    assertEquals(postPartitionViewOptionsModel.update(), "true");
}
Also used : PostPartitionViewOptions(com.ibm.cloud.cloudant.v1.model.PostPartitionViewOptions) Test(org.testng.annotations.Test)

Example 4 with PostPartitionViewOptions

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

the class Cloudant method postPartitionView.

/**
 * Query a database partition MapReduce view function.
 *
 * Runs the specified view function from the specified design document. Unlike `GET /{db}/_design/{ddoc}/_view/{view}`
 * for accessing views, the POST method supports the specification of explicit keys to be retrieved from the view
 * results. The remainder of the POST view functionality is identical to the `GET /{db}/_design/{ddoc}/_view/{view}`
 * API.
 *
 * @param postPartitionViewOptions the {@link PostPartitionViewOptions} containing the options for the call
 * @return a {@link ServiceCall} with a result of type {@link ViewResult}
 */
public ServiceCall<ViewResult> postPartitionView(PostPartitionViewOptions postPartitionViewOptions) {
    com.ibm.cloud.sdk.core.util.Validator.notNull(postPartitionViewOptions, "postPartitionViewOptions cannot be null");
    Map<String, String> pathParamsMap = new HashMap<String, String>();
    pathParamsMap.put("db", postPartitionViewOptions.db());
    pathParamsMap.put("partition_key", postPartitionViewOptions.partitionKey());
    pathParamsMap.put("ddoc", postPartitionViewOptions.ddoc());
    pathParamsMap.put("view", postPartitionViewOptions.view());
    RequestBuilder builder = RequestBuilder.post(RequestBuilder.resolveRequestUrl(getServiceUrl(), "/{db}/_partition/{partition_key}/_design/{ddoc}/_view/{view}", pathParamsMap));
    Map<String, String> sdkHeaders = SdkCommon.getSdkHeaders("cloudant", "v1", "postPartitionView");
    for (Entry<String, String> header : sdkHeaders.entrySet()) {
        builder.header(header.getKey(), header.getValue());
    }
    builder.header("Accept", "application/json");
    final JsonObject contentJson = new JsonObject();
    if (postPartitionViewOptions.attEncodingInfo() != null) {
        contentJson.addProperty("att_encoding_info", postPartitionViewOptions.attEncodingInfo());
    }
    if (postPartitionViewOptions.attachments() != null) {
        contentJson.addProperty("attachments", postPartitionViewOptions.attachments());
    }
    if (postPartitionViewOptions.conflicts() != null) {
        contentJson.addProperty("conflicts", postPartitionViewOptions.conflicts());
    }
    if (postPartitionViewOptions.descending() != null) {
        contentJson.addProperty("descending", postPartitionViewOptions.descending());
    }
    if (postPartitionViewOptions.includeDocs() != null) {
        contentJson.addProperty("include_docs", postPartitionViewOptions.includeDocs());
    }
    if (postPartitionViewOptions.inclusiveEnd() != null) {
        contentJson.addProperty("inclusive_end", postPartitionViewOptions.inclusiveEnd());
    }
    if (postPartitionViewOptions.limit() != null) {
        contentJson.addProperty("limit", postPartitionViewOptions.limit());
    }
    if (postPartitionViewOptions.skip() != null) {
        contentJson.addProperty("skip", postPartitionViewOptions.skip());
    }
    if (postPartitionViewOptions.updateSeq() != null) {
        contentJson.addProperty("update_seq", postPartitionViewOptions.updateSeq());
    }
    if (postPartitionViewOptions.endkey() != null) {
        contentJson.add("endkey", com.ibm.cloud.sdk.core.util.GsonSingleton.getGson().toJsonTree(postPartitionViewOptions.endkey()));
    }
    if (postPartitionViewOptions.endkeyDocid() != null) {
        contentJson.addProperty("endkey_docid", postPartitionViewOptions.endkeyDocid());
    }
    if (postPartitionViewOptions.group() != null) {
        contentJson.addProperty("group", postPartitionViewOptions.group());
    }
    if (postPartitionViewOptions.groupLevel() != null) {
        contentJson.addProperty("group_level", postPartitionViewOptions.groupLevel());
    }
    if (postPartitionViewOptions.key() != null) {
        contentJson.add("key", com.ibm.cloud.sdk.core.util.GsonSingleton.getGson().toJsonTree(postPartitionViewOptions.key()));
    }
    if (postPartitionViewOptions.keys() != null) {
        contentJson.add("keys", com.ibm.cloud.sdk.core.util.GsonSingleton.getGson().toJsonTree(postPartitionViewOptions.keys()));
    }
    if (postPartitionViewOptions.reduce() != null) {
        contentJson.addProperty("reduce", postPartitionViewOptions.reduce());
    }
    if (postPartitionViewOptions.stable() != null) {
        contentJson.addProperty("stable", postPartitionViewOptions.stable());
    }
    if (postPartitionViewOptions.startkey() != null) {
        contentJson.add("startkey", com.ibm.cloud.sdk.core.util.GsonSingleton.getGson().toJsonTree(postPartitionViewOptions.startkey()));
    }
    if (postPartitionViewOptions.startkeyDocid() != null) {
        contentJson.addProperty("startkey_docid", postPartitionViewOptions.startkeyDocid());
    }
    if (postPartitionViewOptions.update() != null) {
        contentJson.addProperty("update", postPartitionViewOptions.update());
    }
    builder.bodyJson(contentJson);
    ResponseConverter<ViewResult> responseConverter = ResponseConverterUtils.getValue(new com.google.gson.reflect.TypeToken<ViewResult>() {
    }.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) ViewResult(com.ibm.cloud.cloudant.v1.model.ViewResult)

Aggregations

PostPartitionViewOptions (com.ibm.cloud.cloudant.v1.model.PostPartitionViewOptions)3 Test (org.testng.annotations.Test)3 GeoJsonGeometryObject (com.ibm.cloud.cloudant.v1.model.GeoJsonGeometryObject)2 SecurityObject (com.ibm.cloud.cloudant.v1.model.SecurityObject)2 ViewResult (com.ibm.cloud.cloudant.v1.model.ViewResult)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