Search in sources :

Example 1 with PostViewOptions

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

the class CloudantTest method testPostViewAsStreamWOptions.

@Test
public void testPostViewAsStreamWOptions() throws Throwable {
    // Schedule some responses.
    String mockResponseBody = "{\"foo\": \"this is a mock response for JSON streaming\"}";
    String postViewAsStreamPath = "/testString/_design/testString/_view/testString";
    server.enqueue(new MockResponse().setHeader("Content-type", "application/json").setResponseCode(200).setBody(mockResponseBody));
    constructClientService();
    // Construct an instance of the PostViewOptions model
    PostViewOptions postViewOptionsModel = new PostViewOptions.Builder().db("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.postViewAsStream(postViewOptionsModel).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, postViewAsStreamPath);
    // 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) InputStream(java.io.InputStream) SecurityObject(com.ibm.cloud.cloudant.v1.model.SecurityObject) GeoJsonGeometryObject(com.ibm.cloud.cloudant.v1.model.GeoJsonGeometryObject) PostViewOptions(com.ibm.cloud.cloudant.v1.model.PostViewOptions) Test(org.testng.annotations.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 2 with PostViewOptions

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

the class Cloudant method postView.

/**
 * Query a MapReduce view.
 *
 * This operation queries the specified MapReduce view of the specified design document. By default, the map and
 * reduce functions of the view are run to update the view before returning the response. The advantage of using the
 * HTTP `POST` method is that the query is submitted as a JSON object in the request body. This avoids the limitations
 * of passing query options as URL query parameters of a `GET` request.
 *
 * @param postViewOptions the {@link PostViewOptions} containing the options for the call
 * @return a {@link ServiceCall} with a result of type {@link ViewResult}
 */
public ServiceCall<ViewResult> postView(PostViewOptions postViewOptions) {
    com.ibm.cloud.sdk.core.util.Validator.notNull(postViewOptions, "postViewOptions cannot be null");
    Map<String, String> pathParamsMap = new HashMap<String, String>();
    pathParamsMap.put("db", postViewOptions.db());
    pathParamsMap.put("ddoc", postViewOptions.ddoc());
    pathParamsMap.put("view", postViewOptions.view());
    RequestBuilder builder = RequestBuilder.post(RequestBuilder.resolveRequestUrl(getServiceUrl(), "/{db}/_design/{ddoc}/_view/{view}", pathParamsMap));
    Map<String, String> sdkHeaders = SdkCommon.getSdkHeaders("cloudant", "v1", "postView");
    for (Entry<String, String> header : sdkHeaders.entrySet()) {
        builder.header(header.getKey(), header.getValue());
    }
    builder.header("Accept", "application/json");
    final JsonObject contentJson = new JsonObject();
    if (postViewOptions.attEncodingInfo() != null) {
        contentJson.addProperty("att_encoding_info", postViewOptions.attEncodingInfo());
    }
    if (postViewOptions.attachments() != null) {
        contentJson.addProperty("attachments", postViewOptions.attachments());
    }
    if (postViewOptions.conflicts() != null) {
        contentJson.addProperty("conflicts", postViewOptions.conflicts());
    }
    if (postViewOptions.descending() != null) {
        contentJson.addProperty("descending", postViewOptions.descending());
    }
    if (postViewOptions.includeDocs() != null) {
        contentJson.addProperty("include_docs", postViewOptions.includeDocs());
    }
    if (postViewOptions.inclusiveEnd() != null) {
        contentJson.addProperty("inclusive_end", postViewOptions.inclusiveEnd());
    }
    if (postViewOptions.limit() != null) {
        contentJson.addProperty("limit", postViewOptions.limit());
    }
    if (postViewOptions.skip() != null) {
        contentJson.addProperty("skip", postViewOptions.skip());
    }
    if (postViewOptions.updateSeq() != null) {
        contentJson.addProperty("update_seq", postViewOptions.updateSeq());
    }
    if (postViewOptions.endkey() != null) {
        contentJson.add("endkey", com.ibm.cloud.sdk.core.util.GsonSingleton.getGson().toJsonTree(postViewOptions.endkey()));
    }
    if (postViewOptions.endkeyDocid() != null) {
        contentJson.addProperty("endkey_docid", postViewOptions.endkeyDocid());
    }
    if (postViewOptions.group() != null) {
        contentJson.addProperty("group", postViewOptions.group());
    }
    if (postViewOptions.groupLevel() != null) {
        contentJson.addProperty("group_level", postViewOptions.groupLevel());
    }
    if (postViewOptions.key() != null) {
        contentJson.add("key", com.ibm.cloud.sdk.core.util.GsonSingleton.getGson().toJsonTree(postViewOptions.key()));
    }
    if (postViewOptions.keys() != null) {
        contentJson.add("keys", com.ibm.cloud.sdk.core.util.GsonSingleton.getGson().toJsonTree(postViewOptions.keys()));
    }
    if (postViewOptions.reduce() != null) {
        contentJson.addProperty("reduce", postViewOptions.reduce());
    }
    if (postViewOptions.stable() != null) {
        contentJson.addProperty("stable", postViewOptions.stable());
    }
    if (postViewOptions.startkey() != null) {
        contentJson.add("startkey", com.ibm.cloud.sdk.core.util.GsonSingleton.getGson().toJsonTree(postViewOptions.startkey()));
    }
    if (postViewOptions.startkeyDocid() != null) {
        contentJson.addProperty("startkey_docid", postViewOptions.startkeyDocid());
    }
    if (postViewOptions.update() != null) {
        contentJson.addProperty("update", postViewOptions.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)

Example 3 with PostViewOptions

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

the class PostViewOptionsTest method testPostViewOptions.

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

Example 4 with PostViewOptions

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

the class CloudantTest method testPostViewWOptions.

@Test
public void testPostViewWOptions() 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 postViewPath = "/testString/_design/testString/_view/testString";
    server.enqueue(new MockResponse().setHeader("Content-type", "application/json").setResponseCode(200).setBody(mockResponseBody));
    constructClientService();
    // Construct an instance of the PostViewOptions model
    PostViewOptions postViewOptionsModel = new PostViewOptions.Builder().db("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.postView(postViewOptionsModel).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, postViewPath);
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) SecurityObject(com.ibm.cloud.cloudant.v1.model.SecurityObject) GeoJsonGeometryObject(com.ibm.cloud.cloudant.v1.model.GeoJsonGeometryObject) PostViewOptions(com.ibm.cloud.cloudant.v1.model.PostViewOptions) ViewResult(com.ibm.cloud.cloudant.v1.model.ViewResult) Test(org.testng.annotations.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Aggregations

PostViewOptions (com.ibm.cloud.cloudant.v1.model.PostViewOptions)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