use of com.ibm.cloud.cloudant.v1.model.Attachment in project cloudant-java-sdk by IBM.
the class CloudantTest method testPostDocumentWOptions.
@Test
public void testPostDocumentWOptions() throws Throwable {
// Schedule some responses.
String mockResponseBody = "{\"id\": \"id\", \"rev\": \"rev\", \"ok\": true, \"caused_by\": \"causedBy\", \"error\": \"error\", \"reason\": \"reason\"}";
String postDocumentPath = "/testString";
server.enqueue(new MockResponse().setHeader("Content-type", "application/json").setResponseCode(201).setBody(mockResponseBody));
constructClientService();
// Construct an instance of the Attachment model
Attachment attachmentModel = new Attachment.Builder().contentType("testString").data(TestUtilities.createMockByteArray("This is a mock byte array value.")).digest("testString").encodedLength(Long.valueOf("0")).encoding("testString").follows(true).length(Long.valueOf("0")).revpos(Long.valueOf("1")).stub(true).build();
// Construct an instance of the Revisions model
Revisions revisionsModel = new Revisions.Builder().ids(new java.util.ArrayList<String>(java.util.Arrays.asList("testString"))).start(Long.valueOf("1")).build();
// Construct an instance of the DocumentRevisionStatus model
DocumentRevisionStatus documentRevisionStatusModel = new DocumentRevisionStatus.Builder().rev("testString").status("available").build();
// Construct an instance of the Document model
Document documentModel = new Document.Builder().attachments(new java.util.HashMap<String, Attachment>() {
{
put("foo", attachmentModel);
}
}).conflicts(new java.util.ArrayList<String>(java.util.Arrays.asList("testString"))).deleted(true).deletedConflicts(new java.util.ArrayList<String>(java.util.Arrays.asList("testString"))).id("testString").localSeq("testString").rev("testString").revisions(revisionsModel).revsInfo(new java.util.ArrayList<DocumentRevisionStatus>(java.util.Arrays.asList(documentRevisionStatusModel))).add("foo", "testString").build();
// Construct an instance of the PostDocumentOptions model
PostDocumentOptions postDocumentOptionsModel = new PostDocumentOptions.Builder().db("testString").document(documentModel).contentType("application/json").batch("ok").build();
// Invoke operation with valid options model (positive test)
Response<DocumentResult> response = cloudantService.postDocument(postDocumentOptionsModel).execute();
assertNotNull(response);
DocumentResult 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);
assertNotNull(query);
// Get query params
assertEquals(query.get("batch"), "ok");
// Check request path
String parsedPath = TestUtilities.parseReqPath(request);
assertEquals(parsedPath, postDocumentPath);
}
use of com.ibm.cloud.cloudant.v1.model.Attachment in project cloudant-java-sdk by IBM.
the class CloudantTest method testPutLocalDocumentWOptions.
@Test
public void testPutLocalDocumentWOptions() throws Throwable {
// Schedule some responses.
String mockResponseBody = "{\"id\": \"id\", \"rev\": \"rev\", \"ok\": true, \"caused_by\": \"causedBy\", \"error\": \"error\", \"reason\": \"reason\"}";
String putLocalDocumentPath = "/testString/_local/testString";
server.enqueue(new MockResponse().setHeader("Content-type", "application/json").setResponseCode(201).setBody(mockResponseBody));
constructClientService();
// Construct an instance of the Attachment model
Attachment attachmentModel = new Attachment.Builder().contentType("testString").data(TestUtilities.createMockByteArray("This is a mock byte array value.")).digest("testString").encodedLength(Long.valueOf("0")).encoding("testString").follows(true).length(Long.valueOf("0")).revpos(Long.valueOf("1")).stub(true).build();
// Construct an instance of the Revisions model
Revisions revisionsModel = new Revisions.Builder().ids(new java.util.ArrayList<String>(java.util.Arrays.asList("testString"))).start(Long.valueOf("1")).build();
// Construct an instance of the DocumentRevisionStatus model
DocumentRevisionStatus documentRevisionStatusModel = new DocumentRevisionStatus.Builder().rev("testString").status("available").build();
// Construct an instance of the Document model
Document documentModel = new Document.Builder().attachments(new java.util.HashMap<String, Attachment>() {
{
put("foo", attachmentModel);
}
}).conflicts(new java.util.ArrayList<String>(java.util.Arrays.asList("testString"))).deleted(true).deletedConflicts(new java.util.ArrayList<String>(java.util.Arrays.asList("testString"))).id("exampleid").localSeq("testString").rev("testString").revisions(revisionsModel).revsInfo(new java.util.ArrayList<DocumentRevisionStatus>(java.util.Arrays.asList(documentRevisionStatusModel))).add("brand", "Foo").add("colours", "[\"red\",\"green\",\"black\",\"blue\"]").add("description", "Slim Colourful Design Electronic Cooking Appliance for ...").add("image", "assets/img/0gmsnghhew.jpg").add("keywords", "[\"Foo\",\"Scales\",\"Weight\",\"Digital\",\"Kitchen\"]").add("name", "Digital Kitchen Scales").add("price", "14.99").add("productid", "1000042").add("taxonomy", "[\"Home\",\"Kitchen\",\"Small Appliances\"]").add("type", "product").build();
// Construct an instance of the PutLocalDocumentOptions model
PutLocalDocumentOptions putLocalDocumentOptionsModel = new PutLocalDocumentOptions.Builder().db("testString").docId("testString").document(documentModel).contentType("application/json").batch("ok").build();
// Invoke operation with valid options model (positive test)
Response<DocumentResult> response = cloudantService.putLocalDocument(putLocalDocumentOptionsModel).execute();
assertNotNull(response);
DocumentResult responseObj = response.getResult();
assertNotNull(responseObj);
// Verify the contents of the request
RecordedRequest request = server.takeRequest();
assertNotNull(request);
assertEquals(request.getMethod(), "PUT");
// Check query
Map<String, String> query = TestUtilities.parseQueryString(request);
assertNotNull(query);
// Get query params
assertEquals(query.get("batch"), "ok");
// Check request path
String parsedPath = TestUtilities.parseReqPath(request);
assertEquals(parsedPath, putLocalDocumentPath);
}
use of com.ibm.cloud.cloudant.v1.model.Attachment in project cloudant-java-sdk by IBM.
the class CloudantTest method testPutReplicationDocumentWOptions.
@Test
public void testPutReplicationDocumentWOptions() throws Throwable {
// Schedule some responses.
String mockResponseBody = "{\"id\": \"id\", \"rev\": \"rev\", \"ok\": true, \"caused_by\": \"causedBy\", \"error\": \"error\", \"reason\": \"reason\"}";
String putReplicationDocumentPath = "/_replicator/testString";
server.enqueue(new MockResponse().setHeader("Content-type", "application/json").setResponseCode(201).setBody(mockResponseBody));
constructClientService();
// Construct an instance of the Attachment model
Attachment attachmentModel = new Attachment.Builder().contentType("testString").data(TestUtilities.createMockByteArray("This is a mock byte array value.")).digest("testString").encodedLength(Long.valueOf("0")).encoding("testString").follows(true).length(Long.valueOf("0")).revpos(Long.valueOf("1")).stub(true).build();
// Construct an instance of the Revisions model
Revisions revisionsModel = new Revisions.Builder().ids(new java.util.ArrayList<String>(java.util.Arrays.asList("testString"))).start(Long.valueOf("1")).build();
// Construct an instance of the DocumentRevisionStatus model
DocumentRevisionStatus documentRevisionStatusModel = new DocumentRevisionStatus.Builder().rev("testString").status("available").build();
// Construct an instance of the ReplicationCreateTargetParameters model
ReplicationCreateTargetParameters replicationCreateTargetParametersModel = new ReplicationCreateTargetParameters.Builder().n(Long.valueOf("1")).partitioned(false).q(Long.valueOf("26")).build();
// Construct an instance of the ReplicationDatabaseAuthBasic model
ReplicationDatabaseAuthBasic replicationDatabaseAuthBasicModel = new ReplicationDatabaseAuthBasic.Builder().password("testString").username("testString").build();
// Construct an instance of the ReplicationDatabaseAuthIam model
ReplicationDatabaseAuthIam replicationDatabaseAuthIamModel = new ReplicationDatabaseAuthIam.Builder().apiKey("testString").build();
// Construct an instance of the ReplicationDatabaseAuth model
ReplicationDatabaseAuth replicationDatabaseAuthModel = new ReplicationDatabaseAuth.Builder().basic(replicationDatabaseAuthBasicModel).iam(replicationDatabaseAuthIamModel).build();
// Construct an instance of the ReplicationDatabase model
ReplicationDatabase replicationDatabaseModel = new ReplicationDatabase.Builder().auth(replicationDatabaseAuthModel).headers(new java.util.HashMap<String, String>() {
{
put("foo", "testString");
}
}).url("testString").build();
// Construct an instance of the UserContext model
UserContext userContextModel = new UserContext.Builder().db("testString").name("testString").roles(new java.util.ArrayList<String>(java.util.Arrays.asList("_reader"))).build();
// Construct an instance of the ReplicationDocument model
ReplicationDocument replicationDocumentModel = new ReplicationDocument.Builder().attachments(new java.util.HashMap<String, Attachment>() {
{
put("foo", attachmentModel);
}
}).conflicts(new java.util.ArrayList<String>(java.util.Arrays.asList("testString"))).deleted(true).deletedConflicts(new java.util.ArrayList<String>(java.util.Arrays.asList("testString"))).id("testString").localSeq("testString").rev("testString").revisions(revisionsModel).revsInfo(new java.util.ArrayList<DocumentRevisionStatus>(java.util.Arrays.asList(documentRevisionStatusModel))).cancel(true).checkpointInterval(Long.valueOf("0")).connectionTimeout(Long.valueOf("0")).continuous(false).createTarget(false).createTargetParams(replicationCreateTargetParametersModel).docIds(new java.util.ArrayList<String>(java.util.Arrays.asList("testString"))).filter("testString").httpConnections(Long.valueOf("1")).queryParams(new java.util.HashMap<String, String>() {
{
put("foo", "testString");
}
}).retriesPerRequest(Long.valueOf("0")).selector(new java.util.HashMap<String, Object>() {
{
put("foo", "testString");
}
}).sinceSeq("testString").socketOptions("testString").source(replicationDatabaseModel).sourceProxy("testString").target(replicationDatabaseModel).targetProxy("testString").useCheckpoints(true).userCtx(userContextModel).workerBatchSize(Long.valueOf("1")).workerProcesses(Long.valueOf("1")).add("foo", "testString").build();
// Construct an instance of the PutReplicationDocumentOptions model
PutReplicationDocumentOptions putReplicationDocumentOptionsModel = new PutReplicationDocumentOptions.Builder().docId("testString").replicationDocument(replicationDocumentModel).ifMatch("testString").batch("ok").newEdits(false).rev("testString").build();
// Invoke operation with valid options model (positive test)
Response<DocumentResult> response = cloudantService.putReplicationDocument(putReplicationDocumentOptionsModel).execute();
assertNotNull(response);
DocumentResult responseObj = response.getResult();
assertNotNull(responseObj);
// Verify the contents of the request
RecordedRequest request = server.takeRequest();
assertNotNull(request);
assertEquals(request.getMethod(), "PUT");
// Check query
Map<String, String> query = TestUtilities.parseQueryString(request);
assertNotNull(query);
// Get query params
assertEquals(query.get("batch"), "ok");
assertEquals(Boolean.valueOf(query.get("new_edits")), Boolean.valueOf(false));
assertEquals(query.get("rev"), "testString");
// Check request path
String parsedPath = TestUtilities.parseReqPath(request);
assertEquals(parsedPath, putReplicationDocumentPath);
}
use of com.ibm.cloud.cloudant.v1.model.Attachment in project cloudant-java-sdk by IBM.
the class Cloudant method putAttachment.
/**
* Create or modify an attachment.
*
* Uploads the supplied content as an attachment to the specified document. The attachment name that you provide must
* be a URL encoded string. You must supply the Content-Type header, and for an existing document, you must also
* supply either the `rev` query argument or the `If-Match` HTTP header. If you omit the revision, a new, otherwise
* empty, document is created with the provided attachment, or a conflict occurs. If the uploaded attachment uses an
* existing attachment name in the remote database, it updates the corresponding stored content of the database. Since
* you must supply the revision information to add an attachment to the document, this serves as validation to update
* the existing attachment.
*
* @param putAttachmentOptions the {@link PutAttachmentOptions} containing the options for the call
* @return a {@link ServiceCall} with a result of type {@link DocumentResult}
*/
public ServiceCall<DocumentResult> putAttachment(PutAttachmentOptions putAttachmentOptions) {
com.ibm.cloud.sdk.core.util.Validator.notNull(putAttachmentOptions, "putAttachmentOptions cannot be null");
Map<String, String> pathParamsMap = new HashMap<String, String>();
pathParamsMap.put("db", putAttachmentOptions.db());
pathParamsMap.put("doc_id", putAttachmentOptions.docId());
pathParamsMap.put("attachment_name", putAttachmentOptions.attachmentName());
RequestBuilder builder = RequestBuilder.put(RequestBuilder.resolveRequestUrl(getServiceUrl(), "/{db}/{doc_id}/{attachment_name}", pathParamsMap));
Map<String, String> sdkHeaders = SdkCommon.getSdkHeaders("cloudant", "v1", "putAttachment");
for (Entry<String, String> header : sdkHeaders.entrySet()) {
builder.header(header.getKey(), header.getValue());
}
builder.header("Accept", "application/json");
builder.header("Content-Type", putAttachmentOptions.contentType());
if (putAttachmentOptions.ifMatch() != null) {
builder.header("If-Match", putAttachmentOptions.ifMatch());
}
if (putAttachmentOptions.rev() != null) {
builder.query("rev", String.valueOf(putAttachmentOptions.rev()));
}
builder.bodyContent(putAttachmentOptions.contentType(), null, null, putAttachmentOptions.attachment());
ResponseConverter<DocumentResult> responseConverter = ResponseConverterUtils.getValue(new com.google.gson.reflect.TypeToken<DocumentResult>() {
}.getType());
return createServiceCall(builder.build(), responseConverter);
}
use of com.ibm.cloud.cloudant.v1.model.Attachment in project cloudant-java-sdk by IBM.
the class AttachmentTest method testAttachment.
@Test
public void testAttachment() throws Throwable {
Attachment attachmentModel = new Attachment.Builder().contentType("testString").data(TestUtilities.createMockByteArray("This is a mock byte array value.")).digest("testString").encodedLength(Long.valueOf("0")).encoding("testString").follows(true).length(Long.valueOf("0")).revpos(Long.valueOf("1")).stub(true).build();
assertEquals(attachmentModel.contentType(), "testString");
assertEquals(attachmentModel.data(), TestUtilities.createMockByteArray("This is a mock byte array value."));
assertEquals(attachmentModel.digest(), "testString");
assertEquals(attachmentModel.encodedLength(), Long.valueOf("0"));
assertEquals(attachmentModel.encoding(), "testString");
assertEquals(attachmentModel.follows(), Boolean.valueOf(true));
assertEquals(attachmentModel.length(), Long.valueOf("0"));
assertEquals(attachmentModel.revpos(), Long.valueOf("1"));
assertEquals(attachmentModel.stub(), Boolean.valueOf(true));
String json = TestUtilities.serialize(attachmentModel);
Attachment attachmentModelNew = TestUtilities.deserialize(json, Attachment.class);
assertTrue(attachmentModelNew instanceof Attachment);
assertEquals(attachmentModelNew.contentType(), "testString");
assertEquals(attachmentModelNew.data(), TestUtilities.createMockByteArray("This is a mock byte array value."));
assertEquals(attachmentModelNew.digest(), "testString");
assertEquals(attachmentModelNew.encodedLength(), Long.valueOf("0"));
assertEquals(attachmentModelNew.encoding(), "testString");
assertEquals(attachmentModelNew.follows(), Boolean.valueOf(true));
assertEquals(attachmentModelNew.length(), Long.valueOf("0"));
assertEquals(attachmentModelNew.revpos(), Long.valueOf("1"));
assertEquals(attachmentModelNew.stub(), Boolean.valueOf(true));
}
Aggregations