浏览代码

fix(cockpit): inject default freshness period

Kazerin 1 月之前
父节点
当前提交
bfef53a0e8

+ 6 - 2
pipe-network-service/zksy-system/src/main/java/com/zksy/cockpit/foundation/CockpitFreshnessService.java

@@ -1,5 +1,6 @@
 package com.zksy.cockpit.foundation;
 
+import org.springframework.beans.factory.annotation.Value;
 import org.springframework.stereotype.Service;
 
 import java.time.Clock;
@@ -14,7 +15,6 @@ import java.util.Objects;
 public class CockpitFreshnessService {
     private static final DateTimeFormatter TIME_FORMATTER = DateTimeFormatter.ISO_OFFSET_DATE_TIME;
     private static final ZoneId BUSINESS_ZONE = ZoneId.of("Asia/Shanghai");
-    private static final int DEFAULT_CALIBRATION_PERIOD_SECONDS = 60;
     private static final int MAX_CALIBRATION_PERIOD_SECONDS = 3600;
 
     private final CockpitFreshnessAdapter adapter;
@@ -24,10 +24,14 @@ public class CockpitFreshnessService {
     public CockpitFreshnessService(
             CockpitFreshnessAdapter adapter,
             Clock clock,
+            @Value("${cockpit.freshness.default-calibration-period-seconds:60}")
             int defaultCalibrationPeriodSeconds) {
         this.adapter = adapter;
         this.clock = clock;
         this.defaultCalibrationPeriodSeconds = defaultCalibrationPeriodSeconds;
+        if (defaultCalibrationPeriodSeconds < 1 || defaultCalibrationPeriodSeconds > MAX_CALIBRATION_PERIOD_SECONDS) {
+            throw new IllegalArgumentException("defaultCalibrationPeriodSeconds must be between 1 and 3600 seconds");
+        }
     }
 
     public CockpitFreshnessResponse load(String requestId, String deviceCode, Integer calibrationPeriodSeconds) {
@@ -166,4 +170,4 @@ public class CockpitFreshnessService {
     private static boolean isBlank(String value) {
         return value == null || value.trim().isEmpty();
     }
-}
+}

+ 36 - 0
pipe-network-service/zksy-system/src/test/java/com/zksy/cockpit/foundation/CockpitFreshnessServiceWiringTest.java

@@ -0,0 +1,36 @@
+package com.zksy.cockpit.foundation;
+
+import org.junit.jupiter.api.Test;
+import org.springframework.boot.test.context.runner.ApplicationContextRunner;
+
+import java.time.Clock;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+import static org.mockito.Mockito.mock;
+
+class CockpitFreshnessServiceWiringTest {
+    @Test
+    void springWiringUsesDocumentedDefaultCalibrationPeriod() {
+        new ApplicationContextRunner()
+                .withBean(CockpitFreshnessAdapter.class, () -> mock(CockpitFreshnessAdapter.class))
+                .withBean(Clock.class, () -> Clock.systemUTC())
+                .withBean(CockpitFreshnessService.class)
+                .run(context -> {
+                    assertThat(context).hasSingleBean(CockpitFreshnessService.class);
+                    CockpitFreshnessResponse response = context.getBean(CockpitFreshnessService.class)
+                            .load("freshness-wiring-001", "TC-PS-WS-202609020012", null);
+                    assertThat(response.getCalibrationPeriodSeconds()).isEqualTo(60);
+                });
+    }
+
+    @Test
+    void invalidConfiguredDefaultCalibrationPeriodFailsFast() {
+        assertThatThrownBy(() -> new CockpitFreshnessService(
+                mock(CockpitFreshnessAdapter.class),
+                Clock.systemUTC(),
+                0))
+                .isInstanceOf(IllegalArgumentException.class)
+                .hasMessageContaining("between 1 and 3600 seconds");
+    }
+}