All Downloads are FREE. Search and download functionalities are using the official Maven repository.

no.unit.nva.testutils.EventBridgeEventBuilder Maven / Gradle / Ivy

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();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy