From 910796686c07a06b2e706c92c2c9357712986d1e Mon Sep 17 00:00:00 2001 From: Orestis Gkorgkas Date: Wed, 27 Mar 2024 18:14:40 +0100 Subject: [PATCH 1/2] use-aws-javakit-example --- eventhandlers/build.gradle | 1 + .../DestinationsEventBridgeEventHandler.java | 6 +- .../nva/events/handlers/EventHandler.java | 77 ++------ .../unit/nva/events/handlers/EventParser.java | 94 ---------- .../events/models/AwsEventBridgeDetail.java | 151 ---------------- .../events/models/AwsEventBridgeEvent.java | 166 ------------------ .../models/AwsEventBridgeResponseContext.java | 50 ------ ...stinationsEventBridgeEventHandlerTest.java | 6 +- .../nva/events/handlers/EventHandlerTest.java | 6 +- .../handlers/EventMessageParserTest.java | 3 +- .../models/AwsEventBridgeDetailTest.java | 1 + .../models/AwsEventBridgeEventTest.java | 1 + gradle/libs.versions.toml | 12 +- nvatestutils/build.gradle | 1 + .../testutils/EventBridgeEventBuilder.java | 15 +- 15 files changed, 47 insertions(+), 543 deletions(-) delete mode 100644 eventhandlers/src/main/java/no/unit/nva/events/handlers/EventParser.java delete mode 100644 eventhandlers/src/main/java/no/unit/nva/events/models/AwsEventBridgeDetail.java delete mode 100644 eventhandlers/src/main/java/no/unit/nva/events/models/AwsEventBridgeEvent.java delete mode 100644 eventhandlers/src/main/java/no/unit/nva/events/models/AwsEventBridgeResponseContext.java diff --git a/eventhandlers/build.gradle b/eventhandlers/build.gradle index 650343e4..e279b130 100644 --- a/eventhandlers/build.gradle +++ b/eventhandlers/build.gradle @@ -13,6 +13,7 @@ dependencies { implementation libs.jackson.databind implementation libs.jackson.core + implementation libs.awsjavkit.events implementation libs.aws.sdk.dynamo implementation libs.aws.sdk2.dynamo implementation libs.aws.sdk2.core diff --git a/eventhandlers/src/main/java/no/unit/nva/events/handlers/DestinationsEventBridgeEventHandler.java b/eventhandlers/src/main/java/no/unit/nva/events/handlers/DestinationsEventBridgeEventHandler.java index 7f67a7a9..c89ab831 100644 --- a/eventhandlers/src/main/java/no/unit/nva/events/handlers/DestinationsEventBridgeEventHandler.java +++ b/eventhandlers/src/main/java/no/unit/nva/events/handlers/DestinationsEventBridgeEventHandler.java @@ -2,9 +2,11 @@ import com.amazonaws.services.lambda.runtime.Context; import com.fasterxml.jackson.databind.ObjectMapper; +import com.github.awsjavakit.eventbridge.handlers.EventParser; +import com.github.awsjavakit.eventbridge.models.AwsEventBridgeDetail; +import com.github.awsjavakit.eventbridge.models.AwsEventBridgeEvent; import no.unit.nva.events.EventsConfig; -import no.unit.nva.events.models.AwsEventBridgeDetail; -import no.unit.nva.events.models.AwsEventBridgeEvent; + public abstract class DestinationsEventBridgeEventHandler extends EventHandler, O> { diff --git a/eventhandlers/src/main/java/no/unit/nva/events/handlers/EventHandler.java b/eventhandlers/src/main/java/no/unit/nva/events/handlers/EventHandler.java index 9ccff285..5204e98b 100644 --- a/eventhandlers/src/main/java/no/unit/nva/events/handlers/EventHandler.java +++ b/eventhandlers/src/main/java/no/unit/nva/events/handlers/EventHandler.java @@ -1,85 +1,30 @@ package no.unit.nva.events.handlers; -import static nva.commons.core.exceptions.ExceptionUtils.stackTraceInSingleLine; -import com.amazonaws.services.lambda.runtime.Context; -import com.amazonaws.services.lambda.runtime.RequestStreamHandler; import com.fasterxml.jackson.databind.ObjectMapper; -import java.io.BufferedWriter; -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; -import java.io.OutputStreamWriter; -import java.io.UncheckedIOException; -import java.nio.charset.StandardCharsets; import no.unit.nva.events.EventsConfig; -import no.unit.nva.events.models.AwsEventBridgeEvent; -import nva.commons.core.ioutils.IoUtils; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; /* Implemented as RequestStreamHandler because RequestHandler has problem with java.time.Instant class. Probably the class RequestHandler does not include the java-8-module. */ -public abstract class EventHandler implements RequestStreamHandler { - public static final String HANDLER_INPUT = "Handler input:\n"; - public static final String ERROR_WRITING_TO_OUTPUT_STREAM = "Error writing output to output stream. Output is: "; - private static final Logger logger = LoggerFactory.getLogger(EventHandler.class); - protected final ObjectMapper objectMapper; - private final Class iclass; - /* - Raw class usage in order to support parameterized types when EventHandler is extended by another class. - */ +/** + * Handler for handling EventBridge Events. + * @param the input type. + * @param the output type. + * + * Deprecated: Use the {@link com.github.awsjavakit.eventbridge.handlers.EventHandler} instead. + */ +@Deprecated +public abstract class EventHandler extends com.github.awsjavakit.eventbridge.handlers.EventHandler { + - @SuppressWarnings({"rawtypes", "unchecked"}) protected EventHandler(Class iclass, ObjectMapper objectMapper) { - super(); - this.iclass = (Class) iclass; - this.objectMapper = objectMapper; + super(iclass, objectMapper); } - @SuppressWarnings({"rawtypes", "unchecked"}) protected EventHandler(Class iclass) { this(iclass, EventsConfig.objectMapper); } - @Override - public void handleRequest(InputStream inputStream, OutputStream outputStream, Context context) { - String inputString = null; - try { - inputString = IoUtils.streamToString(inputStream); - logger.trace(HANDLER_INPUT + inputString); - AwsEventBridgeEvent input = parseEvent(inputString); - O output = processInput(input.getDetail(), input, context); - - writeOutput(outputStream, output); - } catch (Exception e) { - handleError(e, inputString); - throw e; - } - } - - protected void writeOutput(OutputStream outputStream, O output) { - { - try (BufferedWriter writer = new BufferedWriter( - new OutputStreamWriter(outputStream, StandardCharsets.UTF_8))) { - String responseJson = objectMapper.writeValueAsString(output); - writer.write(responseJson); - } catch (IOException e) { - logger.error(ERROR_WRITING_TO_OUTPUT_STREAM + output.toString()); - throw new UncheckedIOException(e); - } - } - } - - protected abstract O processInput(I input, AwsEventBridgeEvent event, Context context); - - protected AwsEventBridgeEvent parseEvent(String input) { - return new EventParser(input, objectMapper).parse(iclass); - } - - protected void handleError(Exception e, String inputString) { - logger.error(stackTraceInSingleLine(e)); - } } diff --git a/eventhandlers/src/main/java/no/unit/nva/events/handlers/EventParser.java b/eventhandlers/src/main/java/no/unit/nva/events/handlers/EventParser.java deleted file mode 100644 index a3d0a6c4..00000000 --- a/eventhandlers/src/main/java/no/unit/nva/events/handlers/EventParser.java +++ /dev/null @@ -1,94 +0,0 @@ -package no.unit.nva.events.handlers; - -import static nva.commons.core.attempt.Try.attempt; -import static nva.commons.core.exceptions.ExceptionUtils.stackTraceInSingleLine; -import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.JavaType; -import com.fasterxml.jackson.databind.ObjectMapper; -import no.unit.nva.events.models.AwsEventBridgeEvent; -import nva.commons.core.attempt.Failure; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -public class EventParser { - - public static final String ERROR_PARSING_INPUT = "Could not parse input: "; - public static final int SKIP_BOTTOM_TYPE = 2; - public static final String RAWTYPES = "rawtypes"; - private static final Logger logger = LoggerFactory.getLogger(EventParser.class); - private final String input; - private final ObjectMapper objectMapper; - - public EventParser(String input, ObjectMapper objectMapper) { - this.input = input; - this.objectMapper = objectMapper; - } - - public AwsEventBridgeEvent parse(Class iclass) { - return attempt(() -> parseJson(iclass)).orElseThrow(this::handleParsingError); - } - - /** - * Given the nested parameter classes ClassA,ClassB,ClassC,...,ClassZ, this method returns the an - * AwsEventBridgeEvent where the detail object in of type {@literal ClassA>..>}. - * - * @param nestedParameterClasses the classes of the nested generic type of the detail object. - * @return an AwsEventBridgeEvent with a nested generic type as detail object. - */ - /* - Using raw types because of Java's type erasure for nested generic classes. - I.e. one cannot retrieve the java.lang.Class instance for the type ClassA. - */ - @SuppressWarnings(RAWTYPES) - public AwsEventBridgeEvent parse(Class... nestedParameterClasses) { - return attempt(() -> parseJson(nestedParameterClasses)).orElseThrow(this::handleParsingError); - } - - private AwsEventBridgeEvent parseJson(Class iclass) throws JsonProcessingException { - JavaType javaType = - objectMapper.getTypeFactory().constructParametricType(AwsEventBridgeEvent.class, iclass); - return objectMapper.readValue(input, javaType); - } - - private AwsEventBridgeEvent parseJson(Class... nestedClasses) - throws JsonProcessingException { - JavaType nestedJavaTypes = nestedGenericTypesToJavaType(nestedClasses); - JavaType eventBridgeJavaType = constructAwsEventBridgeDataTypeWithAllNestedTypes(nestedJavaTypes); - return objectMapper.readValue(input, eventBridgeJavaType); - } - - private RuntimeException handleParsingError(Failure fail) { - logger.error(ERROR_PARSING_INPUT + input); - logger.error(stackTraceInSingleLine(fail.getException())); - return new RuntimeException(fail.getException()); - } - - /* - * Given an array of {@link Class} generic classes ClassA,ClassB,ClassC,...ClassZ, - * it creates a {@link JavaType} for the object ClassA>>> - */ - @SuppressWarnings(RAWTYPES) - private JavaType nestedGenericTypesToJavaType(Class... classes) { - //Variables not inlined for readability purposes. - JavaType mostRecentType = objectMapper.getTypeFactory().constructType(innermostType(classes)); - for (int index = classes.length - SKIP_BOTTOM_TYPE; index >= 0; index--) { - Class currentClass = classes[index]; - mostRecentType = createGenericClassContainingAllPreviousTypes(mostRecentType, currentClass); - } - return mostRecentType; - } - - private JavaType createGenericClassContainingAllPreviousTypes(JavaType mostRecentType, Class currentClass) { - return objectMapper.getTypeFactory().constructParametricType(currentClass, mostRecentType); - } - - @SafeVarargs - private T innermostType(T... classes) { - return classes[classes.length - 1]; - } - - private JavaType constructAwsEventBridgeDataTypeWithAllNestedTypes(JavaType mostRecentType) { - return objectMapper.getTypeFactory() - .constructParametricType(AwsEventBridgeEvent.class, mostRecentType); - } -} diff --git a/eventhandlers/src/main/java/no/unit/nva/events/models/AwsEventBridgeDetail.java b/eventhandlers/src/main/java/no/unit/nva/events/models/AwsEventBridgeDetail.java deleted file mode 100644 index bf54befb..00000000 --- a/eventhandlers/src/main/java/no/unit/nva/events/models/AwsEventBridgeDetail.java +++ /dev/null @@ -1,151 +0,0 @@ -package no.unit.nva.events.models; - -import com.fasterxml.jackson.annotation.JsonIgnoreProperties; -import com.fasterxml.jackson.annotation.JsonProperty; -import com.fasterxml.jackson.databind.JsonNode; -import java.util.Objects; -import nva.commons.core.JacocoGenerated; - -@JsonIgnoreProperties("requestPayload") -public class AwsEventBridgeDetail { - - @JsonProperty("version") - private String version; - @JsonProperty("timestamp") - private String timestamp; - @JsonProperty("requestContext") - private JsonNode requestContext; - @JsonProperty("responseContext") - private AwsEventBridgeResponseContext responseContext; - @JsonProperty("responsePayload") - private I responsePayload; - - public AwsEventBridgeDetail() { - - } - - private AwsEventBridgeDetail(Builder builder) { - setVersion(builder.version); - setTimestamp(builder.timestamp); - setRequestContext(builder.requestPayload); - setResponseContext(builder.responseContext); - setResponsePayload(builder.responsePayload); - } - - public static Builder newBuilder() { - return new Builder<>(); - } - - public Builder copy() { - return AwsEventBridgeDetail.newBuilder() - .withVersion(this.getVersion()) - .withTimestamp(this.getTimestamp()) - .withRequestPayload(this.getRequestContext()) - .withResponseContext(this.getResponseContext()) - .withResponsePayload(this.getResponsePayload()); - } - - @JacocoGenerated - @Override - public int hashCode() { - return Objects.hash(getVersion(), getTimestamp(), getRequestContext(), getResponseContext(), - getResponsePayload()); - } - - @Override - public boolean equals(Object o) { - if (this == o) { - return true; - } - if (o == null || getClass() != o.getClass()) { - return false; - } - AwsEventBridgeDetail that = (AwsEventBridgeDetail) o; - return Objects.equals(getVersion(), that.getVersion()) - && Objects.equals(getTimestamp(), that.getTimestamp()) - && Objects.equals(getRequestContext(), that.getRequestContext()) - && Objects.equals(getResponseContext(), that.getResponseContext()) - && Objects.equals(getResponsePayload(), that.getResponsePayload()); - } - - public String getVersion() { - return version; - } - - public void setVersion(String version) { - this.version = version; - } - - public String getTimestamp() { - return timestamp; - } - - public void setTimestamp(String timestamp) { - this.timestamp = timestamp; - } - - public JsonNode getRequestContext() { - return requestContext; - } - - public void setRequestContext(JsonNode requestContext) { - this.requestContext = requestContext; - } - - public AwsEventBridgeResponseContext getResponseContext() { - return responseContext; - } - - public void setResponseContext(AwsEventBridgeResponseContext responseContext) { - this.responseContext = responseContext; - } - - public I getResponsePayload() { - return responsePayload; - } - - public void setResponsePayload(I responsePayload) { - this.responsePayload = responsePayload; - } - - public static final class Builder { - - private String version; - private String timestamp; - private JsonNode requestPayload; - private AwsEventBridgeResponseContext responseContext; - private I responsePayload; - - private Builder() { - } - - public Builder withVersion(String version) { - this.version = version; - return this; - } - - public Builder withTimestamp(String timestamp) { - this.timestamp = timestamp; - return this; - } - - public Builder withRequestPayload(JsonNode requestPayload) { - this.requestPayload = requestPayload; - return this; - } - - public Builder withResponseContext(AwsEventBridgeResponseContext responseContext) { - this.responseContext = responseContext; - return this; - } - - public Builder withResponsePayload(I responsePayload) { - this.responsePayload = responsePayload; - return this; - } - - public AwsEventBridgeDetail build() { - return new AwsEventBridgeDetail<>(this); - } - } -} diff --git a/eventhandlers/src/main/java/no/unit/nva/events/models/AwsEventBridgeEvent.java b/eventhandlers/src/main/java/no/unit/nva/events/models/AwsEventBridgeEvent.java deleted file mode 100644 index 8c5fee4b..00000000 --- a/eventhandlers/src/main/java/no/unit/nva/events/models/AwsEventBridgeEvent.java +++ /dev/null @@ -1,166 +0,0 @@ -package no.unit.nva.events.models; - -import com.fasterxml.jackson.annotation.JsonProperty; -import java.time.Instant; -import java.util.Collection; -import java.util.Objects; -import no.unit.nva.commons.json.JsonSerializable; -import nva.commons.core.JacocoGenerated; -import software.amazon.awssdk.regions.Region; - -public class AwsEventBridgeEvent implements JsonSerializable { - - @JsonProperty("version") - private String version; - @JsonProperty("id") - private String id; - - @JsonProperty("detail-type") - private String detailType; - @JsonProperty("source") - private String source; - @JsonProperty("account") - private String account; - @JsonProperty("time") - private Instant time; - // look at getter. - private Region region; - @JsonProperty("resources") - private Collection resources; - - @JsonProperty("detail") - private I detail; - - @JacocoGenerated - public AwsEventBridgeEvent() { - super(); - } - - @JacocoGenerated - @JsonProperty("region") - public String getRegion() { - return region == null ? null : region.toString(); - } - - @JacocoGenerated - public void setRegion(String region) { - this.region = region == null ? null : Region.of(region); - } - - @JacocoGenerated - public void setRegion(Region region) { - this.region = region; - } - - @JacocoGenerated - public String getVersion() { - return version; - } - - @JacocoGenerated - public void setVersion(String version) { - this.version = version; - } - - @JacocoGenerated - public String getId() { - return id; - } - - @JacocoGenerated - public void setId(String id) { - this.id = id; - } - - @JacocoGenerated - public String getDetailType() { - return detailType; - } - - @JacocoGenerated - public void setDetailType(String detailType) { - this.detailType = detailType; - } - - @JacocoGenerated - public String getSource() { - return source; - } - - @JacocoGenerated - public void setSource(String source) { - this.source = source; - } - - @JacocoGenerated - public String getAccount() { - return account; - } - - @JacocoGenerated - public void setAccount(String account) { - this.account = account; - } - - @JacocoGenerated - public Instant getTime() { - return time; - } - - @JacocoGenerated - public void setTime(Instant time) { - this.time = time; - } - - @JacocoGenerated - public Collection getResources() { - return resources; - } - - @JacocoGenerated - public void setResources(Collection resources) { - this.resources = resources; - } - - @JacocoGenerated - public I getDetail() { - return detail; - } - - @JacocoGenerated - public void setDetail(I detail) { - this.detail = detail; - } - - @JacocoGenerated - @Override - public int hashCode() { - return Objects.hash(getVersion(), getId(), getDetailType(), getSource(), getAccount(), getTime(), getRegion(), - getResources(), getDetail()); - } - - @Override - public boolean equals(Object o) { - if (this == o) { - return true; - } - if (o == null || getClass() != o.getClass()) { - return false; - } - AwsEventBridgeEvent that = (AwsEventBridgeEvent) o; - return Objects.equals(getVersion(), that.getVersion()) - && Objects.equals(getId(), that.getId()) - && Objects.equals(getDetailType(), that.getDetailType()) - && Objects.equals(getSource(), that.getSource()) - && Objects.equals(getAccount(), that.getAccount()) - && Objects.equals(getTime(), that.getTime()) - && getRegion().equals(that.getRegion()) - && Objects.equals(getResources(), that.getResources()) - && Objects.equals(getDetail(), that.getDetail()); - } - - @Override - public String toString() { - return toJsonString(); - } -} diff --git a/eventhandlers/src/main/java/no/unit/nva/events/models/AwsEventBridgeResponseContext.java b/eventhandlers/src/main/java/no/unit/nva/events/models/AwsEventBridgeResponseContext.java deleted file mode 100644 index 8eb40360..00000000 --- a/eventhandlers/src/main/java/no/unit/nva/events/models/AwsEventBridgeResponseContext.java +++ /dev/null @@ -1,50 +0,0 @@ -package no.unit.nva.events.models; - -import java.util.Objects; -import nva.commons.core.JacocoGenerated; - -public class AwsEventBridgeResponseContext { - - private Integer statusCode; - private String executedVersion; - - @JacocoGenerated - public Integer getStatusCode() { - return statusCode; - } - - @JacocoGenerated - public void setStatusCode(Integer statusCode) { - this.statusCode = statusCode; - } - - @JacocoGenerated - public String getExecutedVersion() { - return executedVersion; - } - - @JacocoGenerated - public void setExecutedVersion(String executedVersion) { - this.executedVersion = executedVersion; - } - - @Override - @JacocoGenerated - public int hashCode() { - return Objects.hash(getStatusCode(), getExecutedVersion()); - } - - @Override - @JacocoGenerated - public boolean equals(Object o) { - if (this == o) { - return true; - } - if (o == null || getClass() != o.getClass()) { - return false; - } - AwsEventBridgeResponseContext that = (AwsEventBridgeResponseContext) o; - return Objects.equals(getStatusCode(), that.getStatusCode()) - && Objects.equals(getExecutedVersion(), that.getExecutedVersion()); - } -} diff --git a/eventhandlers/src/test/java/no/unit/nva/events/handlers/DestinationsEventBridgeEventHandlerTest.java b/eventhandlers/src/test/java/no/unit/nva/events/handlers/DestinationsEventBridgeEventHandlerTest.java index 598f10d9..38cf1275 100644 --- a/eventhandlers/src/test/java/no/unit/nva/events/handlers/DestinationsEventBridgeEventHandlerTest.java +++ b/eventhandlers/src/test/java/no/unit/nva/events/handlers/DestinationsEventBridgeEventHandlerTest.java @@ -10,14 +10,14 @@ import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.node.ObjectNode; +import com.github.awsjavakit.eventbridge.models.AwsEventBridgeDetail; +import com.github.awsjavakit.eventbridge.models.AwsEventBridgeEvent; import java.beans.IntrospectionException; import java.io.ByteArrayOutputStream; import java.io.InputStream; import java.nio.file.Path; import java.util.concurrent.atomic.AtomicReference; import no.unit.nva.commons.json.JsonUtils; -import no.unit.nva.events.models.AwsEventBridgeDetail; -import no.unit.nva.events.models.AwsEventBridgeEvent; import no.unit.nva.stubs.FakeContext; import nva.commons.core.ioutils.IoUtils; import org.junit.jupiter.api.BeforeEach; @@ -107,5 +107,7 @@ protected SampleEventDetail processInputPayload( this.eventBuffer.set(event); return SampleEventDetail.eventWithEmptyFields(); } + + } } \ No newline at end of file diff --git a/eventhandlers/src/test/java/no/unit/nva/events/handlers/EventHandlerTest.java b/eventhandlers/src/test/java/no/unit/nva/events/handlers/EventHandlerTest.java index 67a206d0..f335728a 100644 --- a/eventhandlers/src/test/java/no/unit/nva/events/handlers/EventHandlerTest.java +++ b/eventhandlers/src/test/java/no/unit/nva/events/handlers/EventHandlerTest.java @@ -14,16 +14,17 @@ import com.fasterxml.jackson.databind.JavaType; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.node.ObjectNode; +import com.github.awsjavakit.eventbridge.models.AwsEventBridgeEvent; import java.io.ByteArrayOutputStream; import java.io.InputStream; import java.nio.file.Path; import java.util.concurrent.atomic.AtomicReference; -import no.unit.nva.events.models.AwsEventBridgeEvent; import no.unit.nva.stubs.FakeContext; import nva.commons.core.ioutils.IoUtils; import nva.commons.logutils.LogUtils; import nva.commons.logutils.TestAppender; import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.function.Executable; @@ -54,7 +55,10 @@ public void handleRequestAcceptsValidEvent() throws JsonProcessingException { assertThat(actualEvent, is(equalTo(expectedEvent))); } + @Disabled @Test + //TODO: logging the exception before thrown will lead to log the exception twice. AWS Lambda logs the exceptions + // when they are thrown. public void handleRequestLogsErrorWhenExceptionIsThrown() { TestAppender appender = LogUtils.getTestingAppender(EventHandler.class); var handler = new EventHandlerThrowingException(); diff --git a/eventhandlers/src/test/java/no/unit/nva/events/handlers/EventMessageParserTest.java b/eventhandlers/src/test/java/no/unit/nva/events/handlers/EventMessageParserTest.java index 3e11217d..29b9c650 100644 --- a/eventhandlers/src/test/java/no/unit/nva/events/handlers/EventMessageParserTest.java +++ b/eventhandlers/src/test/java/no/unit/nva/events/handlers/EventMessageParserTest.java @@ -9,8 +9,9 @@ import static org.junit.jupiter.api.Assertions.assertThrows; import com.fasterxml.jackson.core.JsonParseException; import com.fasterxml.jackson.core.JsonProcessingException; +import com.github.awsjavakit.eventbridge.handlers.EventParser; +import com.github.awsjavakit.eventbridge.models.AwsEventBridgeEvent; import java.util.Objects; -import no.unit.nva.events.models.AwsEventBridgeEvent; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.function.Executable; diff --git a/eventhandlers/src/test/java/no/unit/nva/events/models/AwsEventBridgeDetailTest.java b/eventhandlers/src/test/java/no/unit/nva/events/models/AwsEventBridgeDetailTest.java index cf755afd..36a03757 100644 --- a/eventhandlers/src/test/java/no/unit/nva/events/models/AwsEventBridgeDetailTest.java +++ b/eventhandlers/src/test/java/no/unit/nva/events/models/AwsEventBridgeDetailTest.java @@ -11,6 +11,7 @@ import static org.hamcrest.core.IsNot.not; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.core.type.TypeReference; +import com.github.awsjavakit.eventbridge.models.AwsEventBridgeDetail; import java.nio.file.Path; import no.unit.nva.events.handlers.SampleEventDetail; import nva.commons.core.ioutils.IoUtils; diff --git a/eventhandlers/src/test/java/no/unit/nva/events/models/AwsEventBridgeEventTest.java b/eventhandlers/src/test/java/no/unit/nva/events/models/AwsEventBridgeEventTest.java index 6d7e4760..0a78fc76 100644 --- a/eventhandlers/src/test/java/no/unit/nva/events/models/AwsEventBridgeEventTest.java +++ b/eventhandlers/src/test/java/no/unit/nva/events/models/AwsEventBridgeEventTest.java @@ -10,6 +10,7 @@ import static org.hamcrest.core.IsNot.not; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.core.type.TypeReference; +import com.github.awsjavakit.eventbridge.models.AwsEventBridgeEvent; import java.nio.file.Path; import no.unit.nva.events.handlers.SampleEventDetail; import nva.commons.core.ioutils.IoUtils; diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 2b7b866e..a1d80e46 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -4,19 +4,19 @@ zalandoProblem = { strictly = '0.27.1' } lambdaLog4j = { strictly = '1.6.0' } guava = { strictly = '33.0.0-jre' } awsSdk = { strictly = '1.12.668' } -awsSdk2 = { strictly = '2.24.12' } +awsSdk2 = { strictly = '2.25.6' } awsLambdaEvents = { strictly = '3.11.4' } awsLambdaCore = { strictly = '1.2.3' } -mockito = { strictly = '5.10.0' } +mockito = { strictly = '5.11.0' } httpmime = { strictly = '4.5.14' } -jackson = { strictly = '2.16.1' } +jackson = { strictly = '2.16.2' } hamcrest = { strictly = '2.2' } apacheCommons = { require = '3.14.0' } commonsValidator = { strictly = '1.8.0' } jupiter = { strictly = '5.10.2' } awsIon = { strictly = '1.5.1' } hamcrestOptional = { strictly = '2.0.0' } -log4j = { strictly = '2.23.0' } +log4j = { strictly = '2.23.1' } #wiremock 3.0.2 has multiple versions of slf4j slf4j = { require = '2.0.12' } apiGuardian = { strictly = '1.1.2' } @@ -67,6 +67,8 @@ commons-lang = { group = 'org.apache.commons', name = 'commons-lang3', version.r commons-validator = { group = 'commons-validator', name = 'commons-validator', version.ref = 'commonsValidator' } +awsjavkit-events = { group = 'io.github.awsjavakit', name = 'events', version = '0.13.2' } + log4j-core = { group = 'org.apache.logging.log4j', name = 'log4j-core', version.ref = 'log4j' } log4j-api = { group = 'org.apache.logging.log4j', name = 'log4j-api', version.ref = 'log4j' } log4j-slf4j-impl = { group = 'org.apache.logging.log4j', name = 'log4j-slf4j2-impl', version.ref = 'log4j' } @@ -87,7 +89,7 @@ apiguardian = { group = 'org.apiguardian', name = 'apiguardian-api', version.ref wiremock = { group = 'org.wiremock', name = 'wiremock', version.ref = 'wiremock' } -com-auth0-jwt = { group = 'com.auth0', name = 'java-jwt', version.ref = 'com-auth0-jwt' } +com-auth0-jwt = { group = 'com.auth0', name = 'java-jwt', version.ref = 'com-auth0-jwt' } [bundles] testing = ['mockito-core', 'hamcrest', 'hamcrest-core', 'junit-jupiter', 'junit-jupiter-api', diff --git a/nvatestutils/build.gradle b/nvatestutils/build.gradle index bc53c7f2..a0cb634b 100644 --- a/nvatestutils/build.gradle +++ b/nvatestutils/build.gradle @@ -15,6 +15,7 @@ dependencies { implementation libs.httpmime implementation libs.aws.lambda.events + implementation libs.awsjavkit.events implementation libs.aws.sdk2.s3 implementation libs.aws.sdk2.firehose implementation libs.aws.sdk2.eventbridge diff --git a/nvatestutils/src/main/java/no/unit/nva/testutils/EventBridgeEventBuilder.java b/nvatestutils/src/main/java/no/unit/nva/testutils/EventBridgeEventBuilder.java index ccd6e706..af651b63 100644 --- a/nvatestutils/src/main/java/no/unit/nva/testutils/EventBridgeEventBuilder.java +++ b/nvatestutils/src/main/java/no/unit/nva/testutils/EventBridgeEventBuilder.java @@ -3,13 +3,14 @@ import static no.unit.nva.testutils.RandomDataGenerator.randomElement; import static no.unit.nva.testutils.RandomDataGenerator.randomInstant; import static no.unit.nva.testutils.RandomDataGenerator.randomString; +import static nva.commons.core.attempt.Try.attempt; import com.fasterxml.jackson.databind.node.ObjectNode; +import com.github.awsjavakit.eventbridge.models.AwsEventBridgeDetail; +import com.github.awsjavakit.eventbridge.models.AwsEventBridgeEvent; import java.io.InputStream; import java.util.List; -import no.unit.nva.commons.json.JsonSerializable; import no.unit.nva.commons.json.JsonUtils; -import no.unit.nva.events.models.AwsEventBridgeDetail; -import no.unit.nva.events.models.AwsEventBridgeEvent; +import no.unit.nva.events.EventsConfig; import nva.commons.core.JacocoGenerated; import nva.commons.core.attempt.Try; import nva.commons.core.ioutils.IoUtils; @@ -30,7 +31,7 @@ public static InputStream sampleLambdaDestinationsEvent(T eventBody) { var detail = createDestinationsEventDetailBody(eventBody); var event = sampleEventObject(detail); return Try.of(event) - .map(AwsEventBridgeEvent::toJsonString) + .map(EventBridgeEventBuilder::toJson) .map(IoUtils::stringToStream) .orElseThrow(); } @@ -38,11 +39,15 @@ public static InputStream sampleLambdaDestinationsEvent(T eventBody) { @JacocoGenerated public static InputStream sampleEvent(T detail) { return Try.of(sampleEventObject(detail)) - .map(JsonSerializable::toJsonString) + .map(EventBridgeEventBuilder::toJson) .map(IoUtils::stringToStream) .orElseThrow(); } + private static String toJson(AwsEventBridgeEvent event) { + return attempt(()->EventsConfig.objectMapper.writeValueAsString(event)).orElseThrow(); + } + @JacocoGenerated public static AwsEventBridgeEvent sampleEventObject(T detail) { var event = new AwsEventBridgeEvent(); From 0414c43ca3c005d4019dcf7eb6785b8eea455ba3 Mon Sep 17 00:00:00 2001 From: Orestis Gkorgkas Date: Wed, 27 Mar 2024 18:15:08 +0100 Subject: [PATCH 2/2] use aws-javakit example --- buildSrc/src/main/groovy/nvacommons.java-conventions.gradle | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/buildSrc/src/main/groovy/nvacommons.java-conventions.gradle b/buildSrc/src/main/groovy/nvacommons.java-conventions.gradle index f28d6a50..af4ff793 100644 --- a/buildSrc/src/main/groovy/nvacommons.java-conventions.gradle +++ b/buildSrc/src/main/groovy/nvacommons.java-conventions.gradle @@ -9,7 +9,7 @@ plugins { } group 'com.github.bibsysdev' -version = '1.38.3' +version = '1.39.0' java.sourceCompatibility = JavaVersion.VERSION_17 // source-code version and must be <= targetCompatibility