package com.baiyue.common.web; import com.baiyue.common.core.outbox.EventDeliveryJournal; import com.baiyue.common.core.outbox.EventDeliveryProducer; import io.micrometer.core.instrument.MeterRegistry; import io.micrometer.core.instrument.simple.SimpleMeterRegistry; import org.junit.jupiter.api.Test; import org.springframework.boot.autoconfigure.AutoConfigurations; import org.springframework.boot.test.context.runner.ApplicationContextRunner; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.datasource.DriverManagerDataSource; import org.springframework.jdbc.datasource.DataSourceTransactionManager; import org.springframework.transaction.PlatformTransactionManager; import static org.assertj.core.api.Assertions.*; class EventDeliveryObservationAutoConfigurationTest { @Test void configuresMandatoryAuditAndMetricsForServiceLocalJdbc() { var jdbc = new JdbcTemplate(new DriverManagerDataSource("jdbc:h2:mem:auto", "sa", "")); new ApplicationContextRunner().withConfiguration(AutoConfigurations.of(EventDeliveryObservationAutoConfiguration.class)) .withBean(EventDeliveryProducer.class, () -> new EventDeliveryProducer() { }) .withBean(JdbcTemplate.class, () -> jdbc) .withBean(PlatformTransactionManager.class, () -> new DataSourceTransactionManager(jdbc.getDataSource())) .withBean(MeterRegistry.class, SimpleMeterRegistry::new) .run(context -> { assertThat(context).hasSingleBean(EventDeliveryJournal.class); assertThat(context).hasSingleBean(EventDeliveryMetrics.class); }); } @Test void otherJdbcServicesDoNotAcquireTransactionOrMetricsRequirements() { new ApplicationContextRunner().withConfiguration(AutoConfigurations.of(EventDeliveryObservationAutoConfiguration.class)) .withBean(JdbcTemplate.class, () -> new JdbcTemplate(new DriverManagerDataSource("jdbc:h2:mem:other", "sa", ""))) .run(context -> { assertThat(context).hasNotFailed(); assertThat(context).doesNotHaveBean(EventDeliveryJournal.class); assertThat(context).doesNotHaveBean(EventDeliveryMetrics.class); }); } @Test void producerWithoutMeterRegistryFailsInsteadOfSilentlyDisablingObservation() { var jdbc = new JdbcTemplate(new DriverManagerDataSource("jdbc:h2:mem:noRegistry", "sa", "")); new ApplicationContextRunner().withConfiguration(AutoConfigurations.of(EventDeliveryObservationAutoConfiguration.class)) .withBean(EventDeliveryProducer.class, () -> new EventDeliveryProducer() { }) .withBean(JdbcTemplate.class, () -> jdbc) .withBean(PlatformTransactionManager.class, () -> new DataSourceTransactionManager(jdbc.getDataSource())) .run(context -> assertThat(context).hasFailed()); } @Test void missingTransactionManagerFailsRatherThanInstallingNonTransactionalFallback() { new ApplicationContextRunner().withConfiguration(AutoConfigurations.of(EventDeliveryObservationAutoConfiguration.class)) .withBean(EventDeliveryProducer.class, () -> new EventDeliveryProducer() { }) .withBean(JdbcTemplate.class, () -> new JdbcTemplate(new DriverManagerDataSource("jdbc:h2:mem:missing", "sa", ""))) .withBean(MeterRegistry.class, SimpleMeterRegistry::new) .run(context -> assertThat(context).hasFailed()); } }