use of com.ibm.cloud.cloudant.v1.model.PutAttachmentOptions in project cloudant-java-sdk by IBM.
the class CloudantTest method testPutAttachmentWOptions.
@Test
public void testPutAttachmentWOptions() throws Throwable {
// Schedule some responses.
String mockResponseBody = "{\"id\": \"id\", \"rev\": \"rev\", \"ok\": true, \"caused_by\": \"causedBy\", \"error\": \"error\", \"reason\": \"reason\"}";
String putAttachmentPath = "/testString/testString/testString";
server.enqueue(new MockResponse().setHeader("Content-type", "application/json").setResponseCode(201).setBody(mockResponseBody));
constructClientService();
// Construct an instance of the PutAttachmentOptions model
PutAttachmentOptions putAttachmentOptionsModel = new PutAttachmentOptions.Builder().db("testString").docId("testString").attachmentName("testString").attachment(TestUtilities.createMockStream("This is a mock file.")).contentType("application/octet-stream").ifMatch("testString").rev("testString").build();
// Invoke operation with valid options model (positive test)
Response<DocumentResult> response = cloudantService.putAttachment(putAttachmentOptionsModel).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");
assertEquals(request.getHeader("Content-Type"), "application/octet-stream");
// Check query
Map<String, String> query = TestUtilities.parseQueryString(request);
assertNotNull(query);
// Get query params
assertEquals(query.get("rev"), "testString");
// Check request path
String parsedPath = TestUtilities.parseReqPath(request);
assertEquals(parsedPath, putAttachmentPath);
}
use of com.ibm.cloud.cloudant.v1.model.PutAttachmentOptions in project cloudant-java-sdk by IBM.
the class PutAttachmentOptionsTest method testPutAttachmentOptions.
@Test
public void testPutAttachmentOptions() throws Throwable {
PutAttachmentOptions putAttachmentOptionsModel = new PutAttachmentOptions.Builder().db("testString").docId("testString").attachmentName("testString").attachment(TestUtilities.createMockStream("This is a mock file.")).contentType("application/octet-stream").ifMatch("testString").rev("testString").build();
assertEquals(putAttachmentOptionsModel.db(), "testString");
assertEquals(putAttachmentOptionsModel.docId(), "testString");
assertEquals(putAttachmentOptionsModel.attachmentName(), "testString");
assertEquals(IOUtils.toString(putAttachmentOptionsModel.attachment()), IOUtils.toString(TestUtilities.createMockStream("This is a mock file.")));
assertEquals(putAttachmentOptionsModel.contentType(), "application/octet-stream");
assertEquals(putAttachmentOptionsModel.ifMatch(), "testString");
assertEquals(putAttachmentOptionsModel.rev(), "testString");
}
use of com.ibm.cloud.cloudant.v1.model.PutAttachmentOptions 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);
}
Aggregations