Search in sources :

Example 1 with AllDocsResult

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

the class CloudantTest method testPostAllDocsWOptions.

@Test
public void testPostAllDocsWOptions() throws Throwable {
    // Schedule some responses.
    String mockResponseBody = "{\"total_rows\": 0, \"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\": \"key\", \"value\": {\"rev\": \"rev\"}}], \"update_seq\": \"updateSeq\"}";
    String postAllDocsPath = "/testString/_all_docs";
    server.enqueue(new MockResponse().setHeader("Content-type", "application/json").setResponseCode(200).setBody(mockResponseBody));
    constructClientService();
    // Construct an instance of the PostAllDocsOptions model
    PostAllDocsOptions postAllDocsOptionsModel = new PostAllDocsOptions.Builder().db("testString").attEncodingInfo(false).attachments(false).conflicts(false).descending(false).includeDocs(false).inclusiveEnd(true).limit(Long.valueOf("10")).skip(Long.valueOf("0")).updateSeq(false).endkey("testString").key("testString").keys(new java.util.ArrayList<String>(java.util.Arrays.asList("testString"))).startkey("0007741142412418284").build();
    // Invoke operation with valid options model (positive test)
    Response<AllDocsResult> response = cloudantService.postAllDocs(postAllDocsOptionsModel).execute();
    assertNotNull(response);
    AllDocsResult 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, postAllDocsPath);
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) AllDocsResult(com.ibm.cloud.cloudant.v1.model.AllDocsResult) PostAllDocsOptions(com.ibm.cloud.cloudant.v1.model.PostAllDocsOptions) Test(org.testng.annotations.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 2 with AllDocsResult

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

the class Cloudant method postPartitionAllDocs.

/**
 * Query a list of all documents in a database partition.
 *
 * Queries the primary index (all document IDs). The results that match the query parameters are returned in a JSON
 * object, including a list of matching documents with basic contents, such as the ID and revision. When no query
 * parameters are specified, results for all documents in the database partition are returned. Optionally, document
 * content or additional metadata can be included in the response.
 *
 * @param postPartitionAllDocsOptions the {@link PostPartitionAllDocsOptions} containing the options for the call
 * @return a {@link ServiceCall} with a result of type {@link AllDocsResult}
 */
public ServiceCall<AllDocsResult> postPartitionAllDocs(PostPartitionAllDocsOptions postPartitionAllDocsOptions) {
    com.ibm.cloud.sdk.core.util.Validator.notNull(postPartitionAllDocsOptions, "postPartitionAllDocsOptions cannot be null");
    Map<String, String> pathParamsMap = new HashMap<String, String>();
    pathParamsMap.put("db", postPartitionAllDocsOptions.db());
    pathParamsMap.put("partition_key", postPartitionAllDocsOptions.partitionKey());
    RequestBuilder builder = RequestBuilder.post(RequestBuilder.resolveRequestUrl(getServiceUrl(), "/{db}/_partition/{partition_key}/_all_docs", pathParamsMap));
    Map<String, String> sdkHeaders = SdkCommon.getSdkHeaders("cloudant", "v1", "postPartitionAllDocs");
    for (Entry<String, String> header : sdkHeaders.entrySet()) {
        builder.header(header.getKey(), header.getValue());
    }
    builder.header("Accept", "application/json");
    final JsonObject contentJson = new JsonObject();
    if (postPartitionAllDocsOptions.attEncodingInfo() != null) {
        contentJson.addProperty("att_encoding_info", postPartitionAllDocsOptions.attEncodingInfo());
    }
    if (postPartitionAllDocsOptions.attachments() != null) {
        contentJson.addProperty("attachments", postPartitionAllDocsOptions.attachments());
    }
    if (postPartitionAllDocsOptions.conflicts() != null) {
        contentJson.addProperty("conflicts", postPartitionAllDocsOptions.conflicts());
    }
    if (postPartitionAllDocsOptions.descending() != null) {
        contentJson.addProperty("descending", postPartitionAllDocsOptions.descending());
    }
    if (postPartitionAllDocsOptions.includeDocs() != null) {
        contentJson.addProperty("include_docs", postPartitionAllDocsOptions.includeDocs());
    }
    if (postPartitionAllDocsOptions.inclusiveEnd() != null) {
        contentJson.addProperty("inclusive_end", postPartitionAllDocsOptions.inclusiveEnd());
    }
    if (postPartitionAllDocsOptions.limit() != null) {
        contentJson.addProperty("limit", postPartitionAllDocsOptions.limit());
    }
    if (postPartitionAllDocsOptions.skip() != null) {
        contentJson.addProperty("skip", postPartitionAllDocsOptions.skip());
    }
    if (postPartitionAllDocsOptions.updateSeq() != null) {
        contentJson.addProperty("update_seq", postPartitionAllDocsOptions.updateSeq());
    }
    if (postPartitionAllDocsOptions.endkey() != null) {
        contentJson.addProperty("endkey", postPartitionAllDocsOptions.endkey());
    }
    if (postPartitionAllDocsOptions.key() != null) {
        contentJson.addProperty("key", postPartitionAllDocsOptions.key());
    }
    if (postPartitionAllDocsOptions.keys() != null) {
        contentJson.add("keys", com.ibm.cloud.sdk.core.util.GsonSingleton.getGson().toJsonTree(postPartitionAllDocsOptions.keys()));
    }
    if (postPartitionAllDocsOptions.startkey() != null) {
        contentJson.addProperty("startkey", postPartitionAllDocsOptions.startkey());
    }
    builder.bodyJson(contentJson);
    ResponseConverter<AllDocsResult> responseConverter = ResponseConverterUtils.getValue(new com.google.gson.reflect.TypeToken<AllDocsResult>() {
    }.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) AllDocsResult(com.ibm.cloud.cloudant.v1.model.AllDocsResult)

Example 3 with AllDocsResult

use of com.ibm.cloud.cloudant.v1.model.AllDocsResult in project knative-eventing-java-app by IBM.

the class CloudEventStoreCloudant method getEvents.

@Override
public List<CloudEvent<?, ?>> getEvents() {
    try {
        List<CloudEvent<?, ?>> events = new ArrayList<>();
        PostAllDocsOptions docsOptions = new PostAllDocsOptions.Builder().db(this.dbName).includeDocs(true).build();
        AllDocsResult allDocResults = this.client.postAllDocs(docsOptions).execute().getResult();
        for (DocsResultRow docResult : allDocResults.getRows()) {
            Document document = docResult.getDoc();
            @SuppressWarnings("rawtypes") CloudEventImpl evt = this.gson.fromJson(document.toString(), CloudEventImpl.class);
            events.add(evt);
        }
        return events;
    } catch (NotFoundException e) {
        logger.warn("Unable to retrieve all documents from Cloudant", e);
        return Collections.emptyList();
    }
}
Also used : DocsResultRow(com.ibm.cloud.cloudant.v1.model.DocsResultRow) ArrayList(java.util.ArrayList) NotFoundException(com.ibm.cloud.sdk.core.service.exception.NotFoundException) AllDocsResult(com.ibm.cloud.cloudant.v1.model.AllDocsResult) PostAllDocsOptions(com.ibm.cloud.cloudant.v1.model.PostAllDocsOptions) Document(com.ibm.cloud.cloudant.v1.model.Document) CloudEvent(io.cloudevents.CloudEvent) CloudEventImpl(io.cloudevents.v02.CloudEventImpl)

Example 4 with AllDocsResult

use of com.ibm.cloud.cloudant.v1.model.AllDocsResult in project knative-eventing-java-app by IBM.

the class CloudEventStoreCloudant method removeAllEvents.

@Override
public void removeAllEvents() throws Exception {
    try {
        PostAllDocsOptions docsOptions = new PostAllDocsOptions.Builder().db(this.dbName).includeDocs(true).build();
        AllDocsResult allDocResults = this.client.postAllDocs(docsOptions).execute().getResult();
        for (DocsResultRow docResult : allDocResults.getRows()) {
            Document document = docResult.getDoc();
            DeleteDocumentOptions deleteDocumentOptions = new DeleteDocumentOptions.Builder().db(this.dbName).docId(document.getId()).rev(document.getRev()).build();
            DocumentResult deleteDocumentResponse = client.deleteDocument(deleteDocumentOptions).execute().getResult();
            if (!deleteDocumentResponse.isOk()) {
                logger.info("Could not delete a document.");
            }
        }
    } catch (NotFoundException e) {
        String errMsg = "Unable to retrieve all documents from Cloudant";
        logger.error(errMsg, e);
        throw new Exception(errMsg, e);
    }
}
Also used : DocsResultRow(com.ibm.cloud.cloudant.v1.model.DocsResultRow) DocumentResult(com.ibm.cloud.cloudant.v1.model.DocumentResult) GsonBuilder(com.google.gson.GsonBuilder) NotFoundException(com.ibm.cloud.sdk.core.service.exception.NotFoundException) AllDocsResult(com.ibm.cloud.cloudant.v1.model.AllDocsResult) PostAllDocsOptions(com.ibm.cloud.cloudant.v1.model.PostAllDocsOptions) Document(com.ibm.cloud.cloudant.v1.model.Document) DeleteDocumentOptions(com.ibm.cloud.cloudant.v1.model.DeleteDocumentOptions) NotFoundException(com.ibm.cloud.sdk.core.service.exception.NotFoundException)

Example 5 with AllDocsResult

use of com.ibm.cloud.cloudant.v1.model.AllDocsResult in project knative-eventing-java-app by IBM.

the class CloudEventStoreCloudant method getNumEvents.

@Override
public long getNumEvents() {
    try {
        PostAllDocsOptions docsOptions = new PostAllDocsOptions.Builder().db(this.dbName).build();
        AllDocsResult allDocResults = this.client.postAllDocs(docsOptions).execute().getResult();
        return allDocResults.getTotalRows();
    } catch (Exception e) {
        logger.warn("Unable to retrieve all documents from Cloudant", e);
        return -1;
    }
}
Also used : GsonBuilder(com.google.gson.GsonBuilder) AllDocsResult(com.ibm.cloud.cloudant.v1.model.AllDocsResult) PostAllDocsOptions(com.ibm.cloud.cloudant.v1.model.PostAllDocsOptions) NotFoundException(com.ibm.cloud.sdk.core.service.exception.NotFoundException)

Aggregations

AllDocsResult (com.ibm.cloud.cloudant.v1.model.AllDocsResult)11 PostAllDocsOptions (com.ibm.cloud.cloudant.v1.model.PostAllDocsOptions)4 HashMap (java.util.HashMap)4 Test (org.testng.annotations.Test)4 JsonObject (com.google.gson.JsonObject)3 DocsResultRow (com.ibm.cloud.cloudant.v1.model.DocsResultRow)3 Document (com.ibm.cloud.cloudant.v1.model.Document)3 RequestBuilder (com.ibm.cloud.sdk.core.http.RequestBuilder)3 NotFoundException (com.ibm.cloud.sdk.core.service.exception.NotFoundException)3 ArrayList (java.util.ArrayList)3 MockResponse (okhttp3.mockwebserver.MockResponse)3 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)3 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)3 GsonBuilder (com.google.gson.GsonBuilder)2 Gson (com.google.gson.Gson)1 DeleteDocumentOptions (com.ibm.cloud.cloudant.v1.model.DeleteDocumentOptions)1 DocumentResult (com.ibm.cloud.cloudant.v1.model.DocumentResult)1 PostDesignDocsOptions (com.ibm.cloud.cloudant.v1.model.PostDesignDocsOptions)1 PostPartitionAllDocsOptions (com.ibm.cloud.cloudant.v1.model.PostPartitionAllDocsOptions)1 CloudEvent (io.cloudevents.CloudEvent)1