package com.lingniu.ingest.sink.archive.config;

import com.lingniu.ingest.sink.archive.ArchiveStore;
import com.lingniu.ingest.sink.archive.RawArchiveEventSink;
import org.junit.jupiter.api.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.core.env.MapPropertySource;

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

class SinkArchiveAutoConfigurationTest {

    @Test
    void localArchiveTypeCreatesStoreAndSink() {
        try (AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext()) {
            context.getEnvironment().getPropertySources().addFirst(new MapPropertySource(
                    "test",
                    java.util.Map.of("lingniu.ingest.sink.archive.path", "target/test-archive")));
            context.register(SinkArchiveAutoConfiguration.class);
            context.refresh();

            assertThat(context.getBeansOfType(ArchiveStore.class)).hasSize(1);
            assertThat(context.getBeansOfType(RawArchiveEventSink.class)).hasSize(1);
        }
    }

    @Test
    void nonLocalArchiveTypeDoesNotCreateSinkWithoutStore() {
        try (AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext()) {
            context.getEnvironment().getPropertySources().addFirst(new MapPropertySource(
                    "test",
                    java.util.Map.of("lingniu.ingest.sink.archive.type", "oss")));
            context.register(SinkArchiveAutoConfiguration.class);
            context.refresh();

            assertThat(context.getBeansOfType(ArchiveStore.class)).isEmpty();
            assertThat(context.getBeansOfType(RawArchiveEventSink.class)).isEmpty();
        }
    }
}
