This commit is contained in:
2026-09-29 18:47:49 +08:00
parent c2927ab9a6
commit a530205d2b
6 changed files with 22 additions and 30 deletions
@@ -18,9 +18,6 @@ public final class EventTypes {
public static final String BUSINESS_FEE_SOURCE_CHANGED = "BusinessFeeSourceChanged";
public static final String WALLET_COMMAND_REQUESTED = "WalletCommandRequested";
public static final String WALLET_COMMAND_COMPLETED = "WalletCommandCompleted";
public static final String IAM_USER_STATUS_CHANGED = "IamUserStatusChanged";
public static final String IAM_TENANT_STATUS_CHANGED = "IamTenantStatusChanged";
public static final String IAM_PERMISSION_VERSION_CHANGED = "IamPermissionVersionChanged";
public static final String LAST_MILE_HANDOFF_ACCEPTED = "LastMileHandoffAccepted";
public static final String LAST_MILE_HANDOFF_REJECTED = "LastMileHandoffRejected";
public static final String LAST_MILE_MILESTONE_CHANGED = "LastMileMilestoneChanged";
@@ -1,25 +0,0 @@
package com.baiyue.common.event;
import java.time.Instant;
import java.util.Set;
/** IAM 向海外服务发布的 V1 安全投影事件负载。 */
public final class IamSecurityEventsV1 {
public static final int VERSION = 1;
private IamSecurityEventsV1() { }
public record UserStatusChanged(long authUserId, long tenantId, String appCode,
String status, long permissionVersion, Instant changedAt) { }
public record TenantStatusChanged(long tenantId, String status, Instant changedAt) { }
public record PermissionVersionChanged(long authUserId, long tenantId, String appCode,
long permissionVersion, String reason, Instant changedAt,
Set<String> roles, Set<String> authorities) {
public PermissionVersionChanged {
roles = roles == null ? Set.of() : Set.copyOf(roles);
authorities = authorities == null ? Set.of() : Set.copyOf(authorities);
}
}
}
@@ -44,6 +44,6 @@ class DeliveryAckTest {
assertThat(EventDeliveryRoute.forEventType(EventTypes.OVERSEAS_CONTAINER_UNLOADED).serviceName()).isEqualTo("firstleg-service");
assertThat(EventDeliveryRoute.forEventType(EventTypes.WALLET_COMMAND_COMPLETED).serviceName()).isEqualTo("lastleg-service");
assertThatThrownBy(() -> EventDeliveryRoute.forEventType("UnknownEvent")).isInstanceOf(IllegalArgumentException.class);
assertThatThrownBy(() -> EventDeliveryRoute.forEventType(EventTypes.IAM_USER_STATUS_CHANGED)).isInstanceOf(IllegalArgumentException.class);
assertThatThrownBy(() -> EventDeliveryRoute.forEventType("IamUserStatusChanged")).isInstanceOf(IllegalArgumentException.class);
}
}
@@ -5,6 +5,7 @@ import com.baiyue.common.core.outbox.EventDeliveryProducer;
import io.micrometer.core.instrument.MeterRegistry;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.jdbc.JdbcTemplateAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration;
import org.springframework.context.annotation.Bean;
@@ -13,6 +14,7 @@ import org.springframework.transaction.PlatformTransactionManager;
@AutoConfiguration(after = {JdbcTemplateAutoConfiguration.class, DataSourceTransactionManagerAutoConfiguration.class})
@ConditionalOnBean(EventDeliveryProducer.class)
@ConditionalOnProperty(prefix = "baiyue.event-delivery", name = "observation-enabled", havingValue = "true")
public class EventDeliveryObservationAutoConfiguration {
@Bean
EventDeliveryJournal eventDeliveryJournal(JdbcTemplate jdbc, PlatformTransactionManager manager) {
@@ -11,12 +11,14 @@ import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.beans.factory.annotation.Autowired;
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))
.withPropertyValues("baiyue.event-delivery.observation-enabled=true")
.withBean(EventDeliveryProducer.class, () -> new EventDeliveryProducer() { })
.withBean(JdbcTemplate.class, () -> jdbc)
.withBean(PlatformTransactionManager.class, () -> new DataSourceTransactionManager(jdbc.getDataSource()))
@@ -28,6 +30,7 @@ class EventDeliveryObservationAutoConfigurationTest {
}
@Test void otherJdbcServicesDoNotAcquireTransactionOrMetricsRequirements() {
new ApplicationContextRunner().withConfiguration(AutoConfigurations.of(EventDeliveryObservationAutoConfiguration.class))
.withPropertyValues("baiyue.event-delivery.observation-enabled=true")
.withBean(JdbcTemplate.class, () -> new JdbcTemplate(new DriverManagerDataSource("jdbc:h2:mem:other", "sa", "")))
.run(context -> {
assertThat(context).hasNotFailed();
@@ -38,13 +41,28 @@ class EventDeliveryObservationAutoConfigurationTest {
@Test void producerWithoutMeterRegistryFailsInsteadOfSilentlyDisablingObservation() {
var jdbc = new JdbcTemplate(new DriverManagerDataSource("jdbc:h2:mem:noRegistry", "sa", ""));
new ApplicationContextRunner().withConfiguration(AutoConfigurations.of(EventDeliveryObservationAutoConfiguration.class))
.withPropertyValues("baiyue.event-delivery.observation-enabled=true")
.withBean(EventDeliveryProducer.class, () -> new EventDeliveryProducer() { })
.withBean(JdbcTemplate.class, () -> jdbc)
.withBean(PlatformTransactionManager.class, () -> new DataSourceTransactionManager(jdbc.getDataSource()))
.run(context -> assertThat(context).hasFailed());
}
static class RequiredProducer implements EventDeliveryProducer {
@Autowired EventDeliveryJournal journal;
@Autowired EventDeliveryMetrics metrics;
}
@Test void producerMissingTheExplicitObservationSwitchCannotStart() {
var jdbc = new JdbcTemplate(new DriverManagerDataSource("jdbc:h2:mem:noSwitch", "sa", ""));
new ApplicationContextRunner().withConfiguration(AutoConfigurations.of(EventDeliveryObservationAutoConfiguration.class))
.withBean(RequiredProducer.class, RequiredProducer::new)
.withBean(JdbcTemplate.class, () -> jdbc)
.withBean(PlatformTransactionManager.class, () -> new DataSourceTransactionManager(jdbc.getDataSource()))
.withBean(MeterRegistry.class, SimpleMeterRegistry::new)
.run(context -> assertThat(context).hasFailed());
}
@Test void missingTransactionManagerFailsRatherThanInstallingNonTransactionalFallback() {
new ApplicationContextRunner().withConfiguration(AutoConfigurations.of(EventDeliveryObservationAutoConfiguration.class))
.withPropertyValues("baiyue.event-delivery.observation-enabled=true")
.withBean(EventDeliveryProducer.class, () -> new EventDeliveryProducer() { })
.withBean(JdbcTemplate.class, () -> new JdbcTemplate(new DriverManagerDataSource("jdbc:h2:mem:missing", "sa", "")))
.withBean(MeterRegistry.class, SimpleMeterRegistry::new)
@@ -152,7 +152,7 @@ class LastlegOrderControllerTest {
@Test
void iamSecurityEventsAreRejectedBeforeLoadingCompletedConsumer() {
EventEnvelope event = mock(EventEnvelope.class);
when(event.eventType()).thenReturn(EventTypes.IAM_USER_STATUS_CHANGED);
when(event.eventType()).thenReturn("IamUserStatusChanged");
LoadingCompletedConsumer loading = (LoadingCompletedConsumer) ReflectionTestUtils.getField(controller, "consumer");
assertThrows(com.baiyue.common.event.EventProtocolException.class, () -> controller.receive("internal-token", event));