Skip to content

Commit

Permalink
feature(s3-connector): minor corrections before rebase
Browse files Browse the repository at this point in the history
  • Loading branch information
mathias-vandaele committed Dec 18, 2024
1 parent 9fb8448 commit 809fa3f
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,6 @@ public class S3ConnectorFunction implements OutboundConnectorFunction {
public Object execute(OutboundConnectorContext context) {
Function<DocumentCreationRequest, Document> createDocument = context::createDocument;
S3Request s3Request = context.bindVariables(S3Request.class);
return S3Executor.create(s3Request, createDocument).execute(s3Request.getData());
return S3Executor.create(s3Request, createDocument).execute(s3Request.getAction());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import io.camunda.connector.aws.s3.model.request.*;
import io.camunda.connector.aws.s3.model.response.DeleteResponse;
import io.camunda.connector.aws.s3.model.response.DownloadResponse;
import io.camunda.connector.aws.s3.model.response.Element;
import io.camunda.connector.aws.s3.model.response.UploadResponse;
import io.camunda.document.Document;
import io.camunda.document.store.DocumentCreationRequest;
Expand Down Expand Up @@ -55,33 +56,32 @@ public Object execute(S3Action s3Action) {
return switch (s3Action) {
case DeleteS3Action deleteS3Action -> delete(deleteS3Action);
case DownloadS3Action downloadS3Action -> download(downloadS3Action);
case UploadS3Action uploadS3Action -> upload(uploadS3Action);
case UploadObject uploadObject -> upload(uploadObject);
};
}

private Object upload(UploadS3Action uploadS3Action) {
Long contentLength = uploadS3Action.document().metadata().getSize();
String contentType = uploadS3Action.document().metadata().getContentType();
private Object upload(UploadObject uploadObject) {
Long contentLength = uploadObject.document().metadata().getSize();
String contentType = uploadObject.document().metadata().getContentType();

PutObjectRequest putObjectRequest =
PutObjectRequest.builder()
.bucket(uploadS3Action.bucket())
.bucket(uploadObject.bucket())
.key(
Optional.ofNullable(uploadS3Action.key())
.orElse(uploadS3Action.document().metadata().getFileName()))
Optional.ofNullable(uploadObject.key())
.orElse(uploadObject.document().metadata().getFileName()))
.contentLength(contentLength)
.contentType(contentType)
.build();

this.s3Client.putObject(
putObjectRequest,
RequestBody.fromInputStream(uploadS3Action.document().asInputStream(), contentLength));
RequestBody.fromInputStream(uploadObject.document().asInputStream(), contentLength));

return new UploadResponse(
uploadS3Action.bucket(),
uploadS3Action.key(),
String.format(
"https://%s.s3.amazonaws.com/%s", uploadS3Action.bucket(), uploadS3Action.key()));
uploadObject.bucket(),
uploadObject.key(),
String.format("https://%s.s3.amazonaws.com/%s", uploadObject.bucket(), uploadObject.key()));
}

private DownloadResponse download(DownloadS3Action downloadS3Action) {
Expand All @@ -107,7 +107,9 @@ private DownloadResponse download(DownloadS3Action downloadS3Action) {
.andThen(
document ->
new DownloadResponse(
downloadS3Action.bucket(), downloadS3Action.key(), document, null))
downloadS3Action.bucket(),
downloadS3Action.key(),
new Element.DocumentContent(document)))
.apply(
DocumentCreationRequest.from(getObjectResponse)
.contentType(getObjectResponse.response().contentType())
Expand All @@ -122,11 +124,14 @@ private DownloadResponse retrieveResponseWithContent(
byte[] rawBytes = responseResponseInputStream.readAllBytes();
return switch (responseResponseInputStream.response().contentType()) {
case "text/plain" ->
new DownloadResponse(bucket, key, null, new String(rawBytes, StandardCharsets.UTF_8));
new DownloadResponse(
bucket, key, new Element.StringContent(new String(rawBytes, StandardCharsets.UTF_8)));
case "application/json" ->
new DownloadResponse(bucket, key, null, new ObjectMapper().readTree(rawBytes));
new DownloadResponse(
bucket, key, new Element.JsonContent(new ObjectMapper().readTree(rawBytes)));
default ->
new DownloadResponse(bucket, key, null, Base64.getEncoder().encodeToString(rawBytes));
new DownloadResponse(
bucket, key, new Element.StringContent(Base64.getEncoder().encodeToString(rawBytes)));
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@
name = "actionDiscriminator",
defaultValue = "uploadObject")
@TemplateSubType(id = "action", label = "Action")
public sealed interface S3Action permits DeleteS3Action, DownloadS3Action, UploadS3Action {}
public sealed interface S3Action permits DeleteS3Action, DownloadS3Action, UploadObject {}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class S3Request extends AwsBaseRequest {
@JsonSubTypes(
value = {
@JsonSubTypes.Type(value = DeleteS3Action.class, name = "deleteObject"),
@JsonSubTypes.Type(value = UploadS3Action.class, name = "uploadObject"),
@JsonSubTypes.Type(value = UploadObject.class, name = "uploadObject"),
@JsonSubTypes.Type(value = DownloadS3Action.class, name = "downloadObject"),
})
@Valid
Expand All @@ -32,11 +32,11 @@ public class S3Request extends AwsBaseRequest {

public S3Request() {}

public S3Action getData() {
public S3Action getAction() {
return action;
}

public void setData(S3Action data) {
this.action = data;
public void setAction(S3Action action) {
this.action = action;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
import io.camunda.document.Document;
import jakarta.validation.constraints.NotBlank;

@TemplateSubType(id = "uploadObject", label = "Upload document")
public record UploadS3Action(
@TemplateSubType(id = "uploadObject", label = "Upload object")
public record UploadObject(
@TemplateProperty(
label = "AWS bucket",
id = "uploadActionBucket",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,5 @@
*/
package io.camunda.connector.aws.s3.model.response;

import io.camunda.document.Document;

public record DownloadResponse(String bucket, String key, Document document, Object content) {}
public record DownloadResponse(String bucket, String key, Element element) {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package io.camunda.connector.aws.s3.model.response;

import io.camunda.document.Document;

public interface Element {
record DocumentContent(Document document) implements Element {}

record StringContent(String content) implements Element {}

record JsonContent(Object content) implements Element {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ void executeDownloadActionReturnsCorrectResult(String variables) {

try (MockedStatic<S3Executor> s3ExecutorMockedStatic = Mockito.mockStatic(S3Executor.class)) {
s3ExecutorMockedStatic.when(() -> S3Executor.create(any(), any())).thenReturn(s3Executor);
when(s3Executor.execute(any())).thenReturn(new DownloadResponse("test", "test", null, null));
when(s3Executor.execute(any())).thenReturn(new DownloadResponse("test", "test", null));
var response = s3ConnectorFunction.execute(context);
Assertions.assertNotNull(response);
Assertions.assertInstanceOf(DownloadResponse.class, response);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@
import io.camunda.connector.aws.s3.model.request.DeleteS3Action;
import io.camunda.connector.aws.s3.model.request.DownloadS3Action;
import io.camunda.connector.aws.s3.model.request.S3Action;
import io.camunda.connector.aws.s3.model.request.UploadS3Action;
import io.camunda.connector.aws.s3.model.request.UploadObject;
import io.camunda.connector.aws.s3.model.response.DeleteResponse;
import io.camunda.connector.aws.s3.model.response.DownloadResponse;
import io.camunda.connector.aws.s3.model.response.Element;
import io.camunda.connector.aws.s3.model.response.UploadResponse;
import io.camunda.document.Document;
import io.camunda.document.store.DocumentCreationRequest;
Expand Down Expand Up @@ -53,7 +54,7 @@ void executeUploadAction() {
Function<DocumentCreationRequest, Document> function = doc -> mock(Document.class);
S3Executor executor = new S3Executor(s3Client, function);
Document document = mock(Document.class, RETURNS_DEEP_STUBS);
S3Action s3Action = new UploadS3Action("test", "key", document);
S3Action s3Action = new UploadObject("test", "key", document);

when(document.metadata().getSize()).thenReturn(42L);
when(document.metadata().getContentType()).thenReturn("application/octet-stream");
Expand Down Expand Up @@ -81,8 +82,7 @@ void executeDownloadAsDocumentAction() {

verify(s3Client, times(1)).getObject(any(GetObjectRequest.class));
assertInstanceOf(DownloadResponse.class, object);
assertNull(((DownloadResponse) object).content());
assertNotNull(((DownloadResponse) object).document());
assertInstanceOf(Element.DocumentContent.class, ((DownloadResponse) object).element());
}

@Test
Expand All @@ -104,9 +104,9 @@ void executeDownloadAsTextContentAction() throws IOException {

verify(s3Client, times(1)).getObject(any(GetObjectRequest.class));
assertInstanceOf(DownloadResponse.class, object);
assertNotNull(((DownloadResponse) object).content());
assertNull(((DownloadResponse) object).document());
assertEquals("Hello World", ((DownloadResponse) object).content());
assertNotNull(((DownloadResponse) object).element());
assertInstanceOf(Element.StringContent.class, ((DownloadResponse) object).element());
assertEquals("Hello World", ((DownloadResponse) object).element());
}

@Test
Expand All @@ -129,9 +129,8 @@ void executeDownloadAsJsonContentAction() throws IOException {
verify(s3Client, times(1)).getObject(any(GetObjectRequest.class));
assertInstanceOf(DownloadResponse.class, object);
DownloadResponse downloadResponse = (DownloadResponse) object;
assertNotNull(downloadResponse.content());
assertNull(downloadResponse.document());
assertEquals("World", ((ObjectNode) downloadResponse.content()).get("Hello").asText());
assertNotNull(downloadResponse.element());
assertEquals("World", ((ObjectNode) downloadResponse.element()).get("Hello").asText());
}

@Test
Expand All @@ -154,9 +153,8 @@ void executeDownloadAsBase64BytesContentAction() throws IOException {
verify(s3Client, times(1)).getObject(any(GetObjectRequest.class));
assertInstanceOf(DownloadResponse.class, object);
DownloadResponse downloadResponse = (DownloadResponse) object;
assertNotNull(downloadResponse.content());
assertNull(downloadResponse.document());
assertNotNull(downloadResponse.element());
assertEquals(
Base64.getEncoder().encodeToString("Hello".getBytes()), downloadResponse.content());
Base64.getEncoder().encodeToString("Hello".getBytes()), downloadResponse.element());
}
}

0 comments on commit 809fa3f

Please sign in to comment.