package com.lingniu.ingest.tdenginehistory.config;

import com.lingniu.ingest.tdenginehistory.TdengineHistorySchema;
import com.lingniu.ingest.tdenginehistory.TdengineHistoryReader;
import com.lingniu.ingest.tdenginehistory.TdengineHistoryWriter;
import com.zaxxer.hikari.HikariDataSource;
import org.junit.jupiter.api.Test;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;

import javax.sql.DataSource;

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

class TdengineHistoryAutoConfigurationTest {

    private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
            .withConfiguration(AutoConfigurations.of(TdengineHistoryAutoConfiguration.class));

    @Test
    void staysOffByDefault() {
        contextRunner.run(context -> {
            assertThat(context).doesNotHaveBean(TdengineHistorySchema.class);
            assertThat(context).doesNotHaveBean(DataSource.class);
            assertThat(context.getBean(TdengineHistoryProperties.class).isEnabled()).isFalse();
        });
    }

    @Test
    void createsSchemaWhenEnabled() {
        contextRunner
                .withPropertyValues(
                        "lingniu.ingest.tdengine-history.enabled=true",
                        "lingniu.ingest.tdengine-history.database=vehicle_history_hot")
                .run(context -> {
                    assertThat(context).hasSingleBean(TdengineHistorySchema.class);
                    assertThat(context.getBean(TdengineHistorySchema.class).bootstrapSql().getFirst())
                            .isEqualTo("CREATE DATABASE IF NOT EXISTS vehicle_history_hot PRECISION 'ms'");
                });
    }

    @Test
    void createsWriterWhenEnabledAndDataSourceExists() {
        contextRunner
                .withBean(DataSource.class, () -> mock(DataSource.class))
                .withPropertyValues("lingniu.ingest.tdengine-history.enabled=true")
                .run(context -> assertThat(context).hasSingleBean(TdengineHistoryWriter.class));
    }

    @Test
    void createsReaderWhenEnabledAndDataSourceExists() {
        contextRunner
                .withBean(DataSource.class, () -> mock(DataSource.class))
                .withPropertyValues("lingniu.ingest.tdengine-history.enabled=true")
                .run(context -> assertThat(context).hasSingleBean(TdengineHistoryReader.class));
    }

    @Test
    void createsTdengineDataSourceWhenEnabled() {
        contextRunner
                .withPropertyValues(
                        "lingniu.ingest.tdengine-history.enabled=true",
                        "lingniu.ingest.tdengine-history.jdbc-url=jdbc:TAOS-RS://tdengine:6041/vehicle_history",
                        "lingniu.ingest.tdengine-history.username=root",
                        "lingniu.ingest.tdengine-history.password=secret",
                        "lingniu.ingest.tdengine-history.maximum-pool-size=24",
                        "lingniu.ingest.tdengine-history.minimum-idle=3",
                        "lingniu.ingest.tdengine-history.initialization-fail-timeout-millis=-1")
                .run(context -> {
                    assertThat(context).hasSingleBean(DataSource.class);
                    HikariDataSource dataSource = context.getBean(HikariDataSource.class);
                    assertThat(dataSource.getJdbcUrl()).isEqualTo("jdbc:TAOS-RS://tdengine:6041/vehicle_history");
                    assertThat(dataSource.getUsername()).isEqualTo("root");
                    assertThat(dataSource.getMaximumPoolSize()).isEqualTo(24);
                    assertThat(dataSource.getMinimumIdle()).isEqualTo(3);
                    assertThat(context).hasSingleBean(TdengineHistoryWriter.class);
                    assertThat(context).hasSingleBean(TdengineHistoryReader.class);
                });
    }
}
