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

no.unit.nva.stubs.FakeFirehoseClient Maven / Gradle / Ivy

The newest version!
package no.unit.nva.stubs;

import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Stream;
import nva.commons.core.JacocoGenerated;
import software.amazon.awssdk.awscore.exception.AwsServiceException;
import software.amazon.awssdk.services.firehose.FirehoseClient;
import software.amazon.awssdk.services.firehose.model.FirehoseException;
import software.amazon.awssdk.services.firehose.model.PutRecordBatchRequest;
import software.amazon.awssdk.services.firehose.model.PutRecordBatchResponse;
import software.amazon.awssdk.services.firehose.model.PutRecordRequest;
import software.amazon.awssdk.services.firehose.model.PutRecordResponse;
import software.amazon.awssdk.services.firehose.model.Record;

/**
 * A Kinesis Firehose collects the records that are pushed to it and stores them in an S3 bucket. The important feature
 * is that it collects multiple records in ZIP files and organizes the ZIP files by timestamp:
 * s3://some-bucket/some-folder/2023/05/08/18/some-prefix-2023-05-08-18-01-17-lfksdjflskd-srdfsdef-sefsdf-sdfsd
 * -sgdfsdfsdfs.gz
 */
public class FakeFirehoseClient implements FirehoseClient {

    public static final String EMPTY_RECORDS_MESSAGE_TEMPLATE =
        "Value %s at 'records' failed to satisfy constraint: Member must have length greater "
        + "than or equal to 1";
    private final List records = new ArrayList<>();

    @Override
    public PutRecordResponse putRecord(PutRecordRequest putRecordRequest) {
        records.add(putRecordRequest.record());
        return PutRecordResponse.builder().build();
    }

    @Override
    public PutRecordBatchResponse putRecordBatch(PutRecordBatchRequest putRecordBatchRequest) {
        if (putRecordBatchRequest.records().isEmpty()) {
            throw emptyBatchException();
        }
        records.addAll(putRecordBatchRequest.records());
        return PutRecordBatchResponse.builder().build();
    }

    @JacocoGenerated
    @Override
    public String serviceName() {
        return "FakeFirehoseClient";
    }

    @Override
    @JacocoGenerated
    public void close() {
        //NO-OP
    }

    public List getRecords() {
        return this.records;
    }

    public Stream extractPushedContent() {
        return this.getRecords().stream()
                   .map(Record::data)
                   .map(data -> data.asString(StandardCharsets.UTF_8));
    }

    public  Stream extractPushedContent(Function parser) {
        return extractPushedContent().map(parser::apply);
    }

    private AwsServiceException emptyBatchException() {
        return FirehoseException.builder().message(String.format(EMPTY_RECORDS_MESSAGE_TEMPLATE, records)).build();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy