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

uk.camsw.rxjava.test.kafka.dsl.KafkaSourceScenario Maven / Gradle / Ivy

The newest version!
package uk.camsw.rxjava.test.kafka.dsl;

import kafka.message.MessageAndMetadata;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import rx.Observable;
import rx.functions.Action1;
import rx.functions.Func0;
import rx.functions.Func1;
import uk.camsw.rxjava.test.dsl.given.BaseGiven;
import uk.camsw.rxjava.test.dsl.scenario.ExecutionContext;
import uk.camsw.rxjava.test.dsl.when.BaseWhen;
import uk.camsw.rxjava.test.kafka.KafkaEnv;
import uk.camsw.rxjava.test.kafka.Topic;
import uk.camsw.rxjava.test.kafka.TopicBuilder;

import java.util.UUID;

/**
 * Create a test scenario where the underlying source is a kafka topic
 *
 * @param  The type of the Key used to partition published events
 * @param  The type of the Event on the source stream
 * @param  The type of the Event emitted by stream under test
 */
public class KafkaSourceScenario {
    private static final Logger logger = LoggerFactory.getLogger(KafkaSourceScenario.class);

    private static final String KEY_TOPIC = KafkaSourceScenario.class.getSimpleName() + "_topic";
    private static final String KEY_ENV = KafkaSourceScenario.class.getSimpleName() + "_env";

    private final ExecutionContext, ?, U, Given, When> context;

    public KafkaSourceScenario(KafkaEnv env) {
        context = new ExecutionContext<>();
        Given given = new Given<>(context);
        context.initSteps(given, new When<>(context));
        given.kafkaEnvironment(env);
    }

    public Given given() {
        return new Given<>(context);
    }

    public static class Given extends BaseGiven, When> {

        private final ExecutionContext, ?, U, Given, When> context;

        public Given(ExecutionContext, ?, U, Given, When> context) {
            super(context);
            this.context = context;
        }

        public Given theStreamUnderTest(Func1, Observable> f) {
            Observable sut = f.call(context.get(KEY_TOPIC));
            context.setStreamUnderTest(sut);
            return this;
        }

        @Override
        public When when() {
            return new When<>(context);
        }

        public Given theTopic(Func0> f) {
            Topic topic = f.call();
            context.put(KEY_TOPIC, topic);
            return this;
        }

        public Given newTopic() {
            return aNewTopic();
        }

        public Given aNewTopic(Action1 config) {
            KafkaEnv env = context.get(KEY_ENV);
            theTopic(() -> {
                String name = "topic-" + UUID.randomUUID().toString();
                TopicBuilder builder = TopicBuilder.newBuilder(env).forTopic(name);
                if (config != null) config.call(builder);
                Topic topic = builder.build();
                context.addFinally(c -> {
                    try {
                        topic.close();
                    } catch (Exception e) {
                        logger.error("Failed to cleanup topic", e);
                    }
                });
                return topic;
            });
            return this;
        }

        public Given aNewTopic() {
            return aNewTopic(null);
        }

        public Given kafkaEnvironment(KafkaEnv envProperties) {
            context.put(KEY_ENV, envProperties);
            return this;
        }
    }

    public static class When extends BaseWhen> {

        private final ExecutionContext, ?, U, Given, When> context;

        public When(ExecutionContext, ?, U, Given, When> context) {
            super(context);
            this.context = context;
        }

        public Producer> theProducer() {
            return new Producer<>(context, context.get(KEY_TOPIC));
        }
    }
}