package com.lingniu.ingest.xindapushapp;

import com.lingniu.ingest.core.config.IngestCoreAutoConfiguration;
import com.lingniu.ingest.identity.config.VehicleIdentityAutoConfiguration;
import com.lingniu.ingest.inbound.xinda.XindaPushClient;
import com.lingniu.ingest.inbound.xinda.XindaPushEventMapper;
import com.lingniu.ingest.inbound.xinda.XindaPushRealtimeHandler;
import com.lingniu.ingest.inbound.xinda.config.XindaPushAutoConfiguration;
import com.lingniu.ingest.sink.archive.ArchiveStore;
import com.lingniu.ingest.sink.archive.RawArchiveEventSink;
import com.lingniu.ingest.sink.archive.config.SinkArchiveAutoConfiguration;
import com.lingniu.ingest.sink.kafka.KafkaEnvelopeDeadLetterSink;
import com.lingniu.ingest.sink.kafka.KafkaEventSink;
import com.lingniu.ingest.sink.kafka.KafkaSinkAutoConfiguration;
import org.apache.kafka.clients.producer.KafkaProducer;
import org.junit.jupiter.api.Test;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;

class XindaPushAppCompositionTest {

    private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
            .withConfiguration(AutoConfigurations.of(
                    IngestCoreAutoConfiguration.class,
                    VehicleIdentityAutoConfiguration.class,
                    KafkaSinkAutoConfiguration.class,
                    SinkArchiveAutoConfiguration.class,
                    XindaPushAutoConfiguration.class))
            .withAllowBeanDefinitionOverriding(true)
            .withBean("kafkaProducer", KafkaProducer.class, XindaPushAppCompositionTest::kafkaProducer)
            .withPropertyValues(
                    "lingniu.ingest.xinda-push.enabled=true",
                    "lingniu.ingest.xinda-push.host=127.0.0.1",
                    "lingniu.ingest.xinda-push.port=10100",
                    "lingniu.ingest.xinda-push.username=test",
                    "lingniu.ingest.xinda-push.password=test",
                    "lingniu.ingest.identity.store=mysql",
                    "lingniu.ingest.identity.mysql.initialize-schema=false",
                    "lingniu.ingest.sink.kafka.enabled=true",
                    "lingniu.ingest.sink.kafka.bootstrap-servers=localhost:9092",
                    "lingniu.ingest.sink.kafka.consumer.enabled=false",
                    "lingniu.ingest.sink.archive.enabled=true",
                    "lingniu.ingest.sink.archive.path=target/test-archive-xinda",
                    "lingniu.ingest.event-file-store.enabled=false",
                    "lingniu.ingest.event-history.enabled=false",
                    "lingniu.ingest.vehicle-state.enabled=false",
                    "lingniu.ingest.vehicle-stat.enabled=false");

    @Test
    void createsXindaPushIngressAndKafkaSinksWithoutHistoryStorage() {
        contextRunner.run(context -> {
            assertThat(context).hasSingleBean(XindaPushClient.class);
            assertThat(context).hasSingleBean(XindaPushEventMapper.class);
            assertThat(context).hasSingleBean(XindaPushRealtimeHandler.class);
            assertThat(context).hasSingleBean(KafkaEventSink.class);
            assertThat(context).hasSingleBean(KafkaEnvelopeDeadLetterSink.class);
            assertThat(context).hasSingleBean(ArchiveStore.class);
            assertThat(context).hasSingleBean(RawArchiveEventSink.class);
        });
    }

    @SuppressWarnings("unchecked")
    private static KafkaProducer<String, byte[]> kafkaProducer() {
        return mock(KafkaProducer.class);
    }
}
