no.unit.nva.testutils.EventBridgeEventBuilder Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of nvatestutils Show documentation
Show all versions of nvatestutils Show documentation
A commons library for the NVA project
The newest version!
package no.unit.nva.testutils;
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 com.fasterxml.jackson.databind.node.ObjectNode;
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 nva.commons.core.JacocoGenerated;
import nva.commons.core.attempt.Try;
import nva.commons.core.ioutils.IoUtils;
import software.amazon.awssdk.regions.Region;
@JacocoGenerated
@SuppressWarnings("FeatureEnvy")
public final class EventBridgeEventBuilder {
public static final ObjectNode EMPTY_OBJECT = JsonUtils.dtoObjectMapper.createObjectNode();
private EventBridgeEventBuilder() {
}
@JacocoGenerated
public static InputStream sampleLambdaDestinationsEvent(T eventBody) {
var detail = createDestinationsEventDetailBody(eventBody);
var event = sampleEventObject(detail);
return Try.of(event)
.map(AwsEventBridgeEvent::toJsonString)
.map(IoUtils::stringToStream)
.orElseThrow();
}
@JacocoGenerated
public static InputStream sampleEvent(T detail) {
return Try.of(sampleEventObject(detail))
.map(JsonSerializable::toJsonString)
.map(IoUtils::stringToStream)
.orElseThrow();
}
@JacocoGenerated
public static AwsEventBridgeEvent sampleEventObject(T detail) {
var event = new AwsEventBridgeEvent();
event.setDetail(detail);
event.setVersion(randomString());
event.setResources(List.of(randomString()));
event.setId(randomString());
event.setRegion(randomElement(Region.regions()));
event.setTime(randomInstant());
event.setSource(randomString());
event.setAccount(randomString());
return event;
}
@JacocoGenerated
private static AwsEventBridgeDetail createDestinationsEventDetailBody(T eventBody) {
return AwsEventBridgeDetail.newBuilder()
.withRequestPayload(EMPTY_OBJECT)
.withTimestamp(randomInstant().toString())
.withVersion(randomString())
.withResponsePayload(eventBody)
.build();
}
}