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);
}
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);
}
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();
}
}
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);
}
}
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;
}
}
Aggregations