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

com.turbospaces.boot.test.KafkaPGBootRuleContext Maven / Gradle / Ivy

package com.turbospaces.boot.test;

import java.util.Objects;
import java.util.UUID;

import javax.annotation.OverridingMethodsMustInvokeSuper;

import com.google.cloud.spring.pubsub.core.subscriber.PubSubSubscriberTemplate;
import io.grpc.ManagedChannel;
import org.apache.commons.lang3.time.StopWatch;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.springframework.cloud.service.common.PostgresqlServiceInfo;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.kafka.test.EmbeddedKafkaBroker;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.containers.Network;

import com.turbospaces.api.facade.ResponseWrapperFacade;
import com.turbospaces.kafka.consumer.KafkaAcceptorChannel;
import com.turbospaces.rpc.CompletableRequestReplyMapper;
import com.turbospaces.ups.KafkaServiceInfo;

import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@Getter
@Setter
public class KafkaPGBootRuleContext implements ExtensionContext.Store.CloseableResource {
    private Network network;
    private GenericContainer postgresql; // ~ docker image

    private CompletableRequestReplyMapper reqReplyMapper;
    private KafkaTemplate kafkaTemplate;
    private KafkaAcceptorChannel kafkaChannel;
    private EmbeddedKafkaBroker kafka;

    private ManagedChannel pubsubChannel;
    private PubSubSubscriberTemplate pubsubSubscriber;

    private PostgresqlServiceInfo posi, pasi;
    private KafkaServiceInfo ksi;

    @Override
    @OverridingMethodsMustInvokeSuper
    public void close() throws Exception {
        if (Objects.nonNull(postgresql)) {
            log.info("stopping docker PostgreSQL: {} ...", postgresql);
            postgresql.stop();
        }

        if (Objects.nonNull(kafkaTemplate)) {
            log.info("stopping embedded kafka template: {} ...", kafkaTemplate);
            kafkaTemplate.destroy();
        }

        if (Objects.nonNull(kafkaChannel)) {
            log.info("stopping embedded request-reply channel: {} ...", kafkaTemplate);
            kafkaChannel.stop();
        }

        if (Objects.nonNull(kafka)) {
            StopWatch stopWatch = StopWatch.createStarted();
            log.info("stopping embedded Kafka: ...");
            kafka.destroy();
            stopWatch.stop();
            log.info("stopped embedded Kafka in {} ...", stopWatch.toString());
        }

        if (Objects.nonNull(reqReplyMapper)) {
            reqReplyMapper.destroy();
        }
        if (Objects.nonNull(pubsubSubscriber)) {
            pubsubSubscriber.destroy();
        }

        if (Objects.nonNull(pubsubChannel)) {
            pubsubChannel.shutdown();
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy