package com.lingniu.ingest.xindapushapp;

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 XindaPushAppDefaultsTest {

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

        assertThat(properties)
                .containsEntry("spring.application.name", "xinda-push-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("server.port", "${HTTP_PORT:20600}")
                .containsEntry("lingniu.ingest.xinda-push.enabled", "${XINDA_PUSH_ENABLED:false}")
                .containsEntry("lingniu.ingest.xinda-push.host", "${XINDA_PUSH_HOST:}")
                .containsEntry("lingniu.ingest.xinda-push.port", "${XINDA_PUSH_PORT:10100}")
                .containsEntry("lingniu.ingest.xinda-push.username", "${XINDA_PUSH_USERNAME:}")
                .containsEntry("lingniu.ingest.xinda-push.subscribe-msg-ids[0]", "${XINDA_PUSH_MSG_ID_LOCATION:0200}")
                .containsEntry("lingniu.ingest.sink.kafka.enabled", true)
                .containsEntry("lingniu.ingest.sink.kafka.consumer.enabled", false)
                .containsEntry("lingniu.ingest.sink.kafka.topics.realtime", "${KAFKA_TOPIC_XINDA_PUSH_EVENT:vehicle.event.xinda-push.v1}")
                .containsEntry("lingniu.ingest.sink.kafka.topics.raw-archive", "${KAFKA_TOPIC_XINDA_PUSH_RAW:vehicle.raw.xinda-push.v1}")
                .containsEntry("lingniu.ingest.sink.kafka.topics.dlq", "${KAFKA_TOPIC_XINDA_PUSH_DLQ:vehicle.dlq.xinda-push.v1}")
                .containsEntry("lingniu.ingest.identity.store", "${VEHICLE_IDENTITY_STORE:mysql}")
                .containsEntry("lingniu.ingest.identity.mysql.table-name",
                        "${VEHICLE_IDENTITY_MYSQL_TABLE:vehicle_identity_bindings}")
                .containsEntry("lingniu.ingest.sink.archive.enabled", "${SINK_ARCHIVE_ENABLED:true}")
                .containsEntry("lingniu.ingest.event-file-store.enabled", false)
                .containsEntry("lingniu.ingest.event-history.enabled", false)
                .containsEntry("lingniu.ingest.vehicle-state.enabled", false)
                .containsEntry("lingniu.ingest.vehicle-stat.enabled", false);

        assertThat(applicationYaml())
                .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);
    }
}
