package com.lingniu.ingest.protocol.gb32960.config;

import com.lingniu.ingest.core.dispatcher.Dispatcher;
import com.lingniu.ingest.protocol.gb32960.codec.Gb32960MessageDecoder;
import com.lingniu.ingest.protocol.gb32960.inbound.Gb32960NettyServer;
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;

class Gb32960AutoConfigurationTest {

    private final ApplicationContextRunner runner = new ApplicationContextRunner()
            .withConfiguration(AutoConfigurations.of(Gb32960AutoConfiguration.class))
            .withBean(Dispatcher.class, () -> new Dispatcher(null, null, null, null, null))
            .withPropertyValues(
                    "lingniu.ingest.gb32960.enabled=true",
                    "lingniu.ingest.gb32960.port=0");

    @Test
    void createsDecoderWithoutTcpServerWhenServerDisabled() {
        runner.withPropertyValues("lingniu.ingest.gb32960.server.enabled=false")
                .run(context -> {
                    assertThat(context).hasSingleBean(Gb32960MessageDecoder.class);
                    assertThat(context).doesNotHaveBean(Gb32960NettyServer.class);
                });
    }

    @Test
    void createsTcpServerByDefaultWhenProtocolEnabled() {
        runner.run(context -> {
            assertThat(context).hasSingleBean(Gb32960MessageDecoder.class);
            assertThat(context).hasSingleBean(Gb32960NettyServer.class);
        });
    }
}
