package com.lingniu.ingest.analyticsapp;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.config.YamlPropertiesFactoryBean;
import org.springframework.core.io.ClassPathResource;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Properties;

import static org.assertj.core.api.Assertions.assertThat;

class VehicleAnalyticsAppDefaultsTest {

    @Test
    void applicationDefaultsKeepAnalyticsAsStatConsumerRuntime() throws IOException {
        Properties properties = applicationProperties();

        assertThat(properties)
                .containsEntry("spring.application.name", "vehicle-analytics-app")
                .containsEntry(
                        "spring.config.import[0]",
                        "optional:nacos:${spring.application.name}.${NACOS_CONFIG_FILE_EXTENSION:yml}?group=${NACOS_GROUP:DEFAULT_GROUP}&refreshEnabled=${NACOS_REFRESH_ENABLED:true}")
                .containsEntry("spring.cloud.nacos.config.server-addr", "${NACOS_SERVER_ADDR:127.0.0.1:8848}")
                .containsEntry("lingniu.ingest.vehicle-stat.enabled", "${VEHICLE_STAT_ENABLED:true}")
                .containsEntry(
                        "lingniu.ingest.vehicle-stat.jt808.enabled",
                        "${VEHICLE_STAT_JT808_MILEAGE_ENABLED:true}")
                .containsEntry("lingniu.ingest.sink.kafka.enabled", true)
                .containsEntry("lingniu.ingest.sink.kafka.consumer.enabled", "${KAFKA_CONSUMER_ENABLED:true}")
                .containsEntry(
                        "lingniu.ingest.sink.kafka.consumer.bindings.vehicleStatEnvelopeConsumerProcessor.enabled",
                        true)
                .containsEntry(
                        "lingniu.ingest.sink.kafka.consumer.bindings.vehicleStatEnvelopeConsumerProcessor.group-id",
                        "${KAFKA_GROUP_STAT:vehicle-stat}")
                .containsEntry("management.endpoints.web.exposure.include", "health,info,metrics,prometheus");

        assertThat(properties.stringPropertyNames())
                .noneMatch(name -> name.startsWith("lingniu.ingest.gb32960."))
                .noneMatch(name -> name.startsWith("lingniu.ingest.event-history."))
                .noneMatch(name -> name.startsWith("lingniu.ingest.sink.archive."))
                .noneMatch(name -> name.startsWith("lingniu.ingest.event-file-store."))
                .noneMatch(name -> name.startsWith("lingniu.ingest.vehicle-state."))
                .noneMatch(name -> name.contains("vehicleStateEnvelopeConsumerProcessor"))
                .noneMatch(name -> name.startsWith("spring.data.redis."))
                .noneMatch(name -> name.startsWith("management.health.redis."));
        assertThat(applicationYaml())
                .doesNotContain("gb32960:")
                .doesNotContain("event-history:")
                .doesNotContain("archive:")
                .doesNotContain("event-file-store:")
                .doesNotContain("vehicle-state:")
                .doesNotContain("vehicleStateEnvelopeConsumerProcessor")
                .doesNotContain("redis:")
                .doesNotContain("KAFKA_ENABLED");
    }

    private static Properties applicationProperties() {
        YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean();
        factory.setResources(new ClassPathResource("application.yml"));

        Properties properties = factory.getObject();

        assertThat(properties).isNotNull();
        return properties;
    }

    private static String applicationYaml() throws IOException {
        return new String(new ClassPathResource("application.yml").getInputStream().readAllBytes(),
                StandardCharsets.UTF_8);
    }
}
