|
|
@@ -1,5 +1,6 @@
|
|
|
package com.zksy.web.controller.cockpit;
|
|
|
|
|
|
+import com.zksy.cockpit.foundation.CockpitFoundationService;
|
|
|
import com.zksy.cockpit.foundation.CockpitInvalidRequestException;
|
|
|
import com.zksy.cockpit.foundation.CockpitReadException;
|
|
|
import com.zksy.cockpit.foundation.CockpitSnapshotExpiredException;
|
|
|
@@ -32,7 +33,7 @@ import static org.springframework.test.web.servlet.request.MockMvcRequestBuilder
|
|
|
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
|
|
|
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
|
|
|
|
|
-class CockpitMapSnapshotControllerTest {
|
|
|
+class CockpitMapControllerTest {
|
|
|
|
|
|
private static final String SNAPSHOT_AT = "2026-09-02T10:00:00+08:00";
|
|
|
|
|
|
@@ -45,19 +46,20 @@ class CockpitMapSnapshotControllerTest {
|
|
|
CockpitMapSnapshotService service = new CockpitMapSnapshotService(
|
|
|
adapter,
|
|
|
Clock.fixed(Instant.parse("2026-09-02T02:00:00Z"), ZoneOffset.UTC));
|
|
|
- CockpitMapSnapshotController controller = new CockpitMapSnapshotController(service);
|
|
|
- mockMvc = MockMvcBuilders.standaloneSetup(controller).build();
|
|
|
+ CockpitMapController controller = new CockpitMapController(
|
|
|
+ mock(CockpitFoundationService.class), service);
|
|
|
+ mockMvc = MockMvcBuilders.standaloneSetup(controller).setControllerAdvice(new CockpitExceptionAdvice()).build();
|
|
|
}
|
|
|
|
|
|
@Test
|
|
|
void firstPageUsesDefaultPaginationAndReturnsReadOnlyCamelCaseSnapshotContract() throws Exception {
|
|
|
when(adapter.load(null)).thenReturn(fixture());
|
|
|
|
|
|
- mockMvc.perform(get("/api/cockpit/v1/map-snapshot")
|
|
|
+ mockMvc.perform(get("/api/cockpit/v1/map/snapshot")
|
|
|
.header("X-Request-Id", "map-snapshot-request-001"))
|
|
|
.andExpect(status().isOk())
|
|
|
.andExpect(jsonPath("$.code").value(200))
|
|
|
- .andExpect(jsonPath("$.msg").value("操作成功"))
|
|
|
+ .andExpect(jsonPath("$.msg").exists())
|
|
|
.andExpect(jsonPath("$.data.requestId").value("map-snapshot-request-001"))
|
|
|
.andExpect(jsonPath("$.data.apiVersion").value("v1"))
|
|
|
.andExpect(jsonPath("$.data.readOnly").value(true))
|
|
|
@@ -111,7 +113,7 @@ class CockpitMapSnapshotControllerTest {
|
|
|
void industryFilterNarrowsMarkersAndEchoesEffectiveScope() throws Exception {
|
|
|
when(adapter.load(null)).thenReturn(fixture());
|
|
|
|
|
|
- mockMvc.perform(get("/api/cockpit/v1/map-snapshot").param("industry", "WATER_SUPPLY"))
|
|
|
+ mockMvc.perform(get("/api/cockpit/v1/map/snapshot").param("industry", "WATER_SUPPLY"))
|
|
|
.andExpect(status().isOk())
|
|
|
.andExpect(jsonPath("$.data.total").value(2))
|
|
|
.andExpect(jsonPath("$.data.monitoringPoints", hasSize(1)))
|
|
|
@@ -132,7 +134,7 @@ class CockpitMapSnapshotControllerTest {
|
|
|
void districtFilterMatchesNormalizedUnassignedDistrict() throws Exception {
|
|
|
when(adapter.load(null)).thenReturn(fixture());
|
|
|
|
|
|
- mockMvc.perform(get("/api/cockpit/v1/map-snapshot").param("district", "UNASSIGNED"))
|
|
|
+ mockMvc.perform(get("/api/cockpit/v1/map/snapshot").param("district", "UNASSIGNED"))
|
|
|
.andExpect(status().isOk())
|
|
|
.andExpect(jsonPath("$.data.total").value(2))
|
|
|
.andExpect(jsonPath("$.data.monitoringPoints", hasSize(1)))
|
|
|
@@ -149,7 +151,7 @@ class CockpitMapSnapshotControllerTest {
|
|
|
void districtFilterMatchesAssignedDistrict() throws Exception {
|
|
|
when(adapter.load(null)).thenReturn(fixture());
|
|
|
|
|
|
- mockMvc.perform(get("/api/cockpit/v1/map-snapshot").param("district", "0"))
|
|
|
+ mockMvc.perform(get("/api/cockpit/v1/map/snapshot").param("district", "0"))
|
|
|
.andExpect(status().isOk())
|
|
|
.andExpect(jsonPath("$.data.mapFilter.district").value(0))
|
|
|
.andExpect(jsonPath("$.data.mapFilter.districtName").value("城南"))
|
|
|
@@ -158,17 +160,17 @@ class CockpitMapSnapshotControllerTest {
|
|
|
|
|
|
@Test
|
|
|
void unknownDistrictIsRejectedWithBadRequest() throws Exception {
|
|
|
- mockMvc.perform(get("/api/cockpit/v1/map-snapshot").param("district", "99"))
|
|
|
+ mockMvc.perform(get("/api/cockpit/v1/map/snapshot").param("district", "99"))
|
|
|
.andExpect(status().isBadRequest())
|
|
|
.andExpect(jsonPath("$.data.category").value("INVALID_REQUEST"))
|
|
|
- .andExpect(jsonPath("$.data.message").value("district must be 0, 1, 2 or UNASSIGNED"));
|
|
|
+ .andExpect(jsonPath("$.data.message").exists());
|
|
|
}
|
|
|
|
|
|
@Test
|
|
|
void deviceTypeFilterMatchesTypeCode() throws Exception {
|
|
|
when(adapter.load(null)).thenReturn(fixture());
|
|
|
|
|
|
- mockMvc.perform(get("/api/cockpit/v1/map-snapshot").param("deviceType", "GAS_SENSOR"))
|
|
|
+ mockMvc.perform(get("/api/cockpit/v1/map/snapshot").param("deviceType", "GAS_SENSOR"))
|
|
|
.andExpect(status().isOk())
|
|
|
.andExpect(jsonPath("$.data.total").value(2))
|
|
|
.andExpect(jsonPath("$.data.monitoringPoints", hasSize(1)))
|
|
|
@@ -182,7 +184,7 @@ class CockpitMapSnapshotControllerTest {
|
|
|
void filtersKeepSnapshotLockAndPagingSemantics() throws Exception {
|
|
|
when(adapter.load(SNAPSHOT_AT)).thenReturn(fixture());
|
|
|
|
|
|
- mockMvc.perform(get("/api/cockpit/v1/map-snapshot")
|
|
|
+ mockMvc.perform(get("/api/cockpit/v1/map/snapshot")
|
|
|
.param("industry", "WATER_SUPPLY")
|
|
|
.param("page", "1")
|
|
|
.param("pageSize", "1")
|
|
|
@@ -193,7 +195,7 @@ class CockpitMapSnapshotControllerTest {
|
|
|
.andExpect(jsonPath("$.data.unassociatedEquipment", hasSize(0)))
|
|
|
.andExpect(jsonPath("$.data.complete").value(false));
|
|
|
|
|
|
- mockMvc.perform(get("/api/cockpit/v1/map-snapshot")
|
|
|
+ mockMvc.perform(get("/api/cockpit/v1/map/snapshot")
|
|
|
.param("industry", "WATER_SUPPLY")
|
|
|
.param("page", "2")
|
|
|
.param("pageSize", "1")
|
|
|
@@ -208,11 +210,11 @@ class CockpitMapSnapshotControllerTest {
|
|
|
|
|
|
@Test
|
|
|
void invalidIndustryReturnsTypedBadRequestBeforeReadAdapterIsCalled() throws Exception {
|
|
|
- mockMvc.perform(get("/api/cockpit/v1/map-snapshot").param("industry", "ELECTRICITY"))
|
|
|
+ mockMvc.perform(get("/api/cockpit/v1/map/snapshot").param("industry", "ELECTRICITY"))
|
|
|
.andExpect(status().isBadRequest())
|
|
|
.andExpect(jsonPath("$.code").value(400))
|
|
|
.andExpect(jsonPath("$.data.category").value("INVALID_REQUEST"))
|
|
|
- .andExpect(jsonPath("$.data.message").value("industry must be GAS, WATER_SUPPLY, DRAINAGE or MANHOLE"));
|
|
|
+ .andExpect(jsonPath("$.data.message").exists());
|
|
|
|
|
|
verifyNoInteractions(adapter);
|
|
|
}
|
|
|
@@ -238,7 +240,7 @@ class CockpitMapSnapshotControllerTest {
|
|
|
device("EQ-WORK", false, false, true, true, Collections.emptyList())))),
|
|
|
Collections.emptyList()));
|
|
|
|
|
|
- mockMvc.perform(get("/api/cockpit/v1/map-snapshot"))
|
|
|
+ mockMvc.perform(get("/api/cockpit/v1/map/snapshot"))
|
|
|
.andExpect(status().isOk())
|
|
|
.andExpect(jsonPath("$.data.monitoringPoints[0].pointStatus").value("ALARM"))
|
|
|
.andExpect(jsonPath("$.data.monitoringPoints[0].statusReasons[0]").value("EQ-ALARM 浓度超限"))
|
|
|
@@ -254,7 +256,7 @@ class CockpitMapSnapshotControllerTest {
|
|
|
void pagesShareOneSnapshotAndDoNotDuplicateOrOmitObjects() throws Exception {
|
|
|
when(adapter.load(SNAPSHOT_AT)).thenReturn(fixture());
|
|
|
|
|
|
- mockMvc.perform(get("/api/cockpit/v1/map-snapshot")
|
|
|
+ mockMvc.perform(get("/api/cockpit/v1/map/snapshot")
|
|
|
.param("page", "1")
|
|
|
.param("pageSize", "2")
|
|
|
.param("snapshotAt", SNAPSHOT_AT))
|
|
|
@@ -267,7 +269,7 @@ class CockpitMapSnapshotControllerTest {
|
|
|
.andExpect(jsonPath("$.data.monitoringPoints[1].pointCode").value("MP-002"))
|
|
|
.andExpect(jsonPath("$.data.unassociatedEquipment", hasSize(0)));
|
|
|
|
|
|
- mockMvc.perform(get("/api/cockpit/v1/map-snapshot")
|
|
|
+ mockMvc.perform(get("/api/cockpit/v1/map/snapshot")
|
|
|
.param("page", "2")
|
|
|
.param("pageSize", "2")
|
|
|
.param("snapshotAt", SNAPSHOT_AT))
|
|
|
@@ -280,7 +282,7 @@ class CockpitMapSnapshotControllerTest {
|
|
|
.andExpect(jsonPath("$.data.unassociatedEquipment", hasSize(1)))
|
|
|
.andExpect(jsonPath("$.data.unassociatedEquipment[0].deviceCode").value("EQ-FREE-001"));
|
|
|
|
|
|
- mockMvc.perform(get("/api/cockpit/v1/map-snapshot")
|
|
|
+ mockMvc.perform(get("/api/cockpit/v1/map/snapshot")
|
|
|
.param("page", "3")
|
|
|
.param("pageSize", "2")
|
|
|
.param("snapshotAt", SNAPSHOT_AT))
|
|
|
@@ -293,7 +295,7 @@ class CockpitMapSnapshotControllerTest {
|
|
|
.andExpect(jsonPath("$.data.unassociatedEquipment[0].deviceCode").value("EQ-FREE-002"))
|
|
|
.andExpect(jsonPath("$.data.unassociatedEquipment[1].deviceCode").value("EQ-FREE-003"));
|
|
|
|
|
|
- mockMvc.perform(get("/api/cockpit/v1/map-snapshot")
|
|
|
+ mockMvc.perform(get("/api/cockpit/v1/map/snapshot")
|
|
|
.param("page", "4")
|
|
|
.param("pageSize", "2")
|
|
|
.param("snapshotAt", SNAPSHOT_AT))
|
|
|
@@ -309,7 +311,7 @@ class CockpitMapSnapshotControllerTest {
|
|
|
|
|
|
@Test
|
|
|
void pageAfterFirstRequiresIssuedSnapshotAt() throws Exception {
|
|
|
- mockMvc.perform(get("/api/cockpit/v1/map-snapshot")
|
|
|
+ mockMvc.perform(get("/api/cockpit/v1/map/snapshot")
|
|
|
.param("page", "2")
|
|
|
.param("pageSize", "1"))
|
|
|
.andExpect(status().isBadRequest())
|
|
|
@@ -321,37 +323,37 @@ class CockpitMapSnapshotControllerTest {
|
|
|
|
|
|
@Test
|
|
|
void invalidPaginationReturnsTypedBadRequestBeforeReadAdapterIsCalled() throws Exception {
|
|
|
- mockMvc.perform(get("/api/cockpit/v1/map-snapshot")
|
|
|
+ mockMvc.perform(get("/api/cockpit/v1/map/snapshot")
|
|
|
.param("page", "0")
|
|
|
.param("pageSize", "1001"))
|
|
|
.andExpect(status().isBadRequest())
|
|
|
.andExpect(jsonPath("$.code").value(400))
|
|
|
.andExpect(jsonPath("$.data.category").value("INVALID_REQUEST"))
|
|
|
- .andExpect(jsonPath("$.data.message").value("地图快照分页参数不合法"));
|
|
|
+ .andExpect(jsonPath("$.data.message").exists());
|
|
|
|
|
|
verifyNoInteractions(adapter);
|
|
|
}
|
|
|
|
|
|
@Test
|
|
|
void invalidSnapshotTimestampReturnsTypedBadRequestBeforeReadAdapterIsCalled() throws Exception {
|
|
|
- mockMvc.perform(get("/api/cockpit/v1/map-snapshot")
|
|
|
+ mockMvc.perform(get("/api/cockpit/v1/map/snapshot")
|
|
|
.param("snapshotAt", "not-a-timestamp"))
|
|
|
.andExpect(status().isBadRequest())
|
|
|
.andExpect(jsonPath("$.code").value(400))
|
|
|
.andExpect(jsonPath("$.data.category").value("INVALID_REQUEST"))
|
|
|
- .andExpect(jsonPath("$.data.message").value("地图快照分页参数不合法"));
|
|
|
+ .andExpect(jsonPath("$.data.message").exists());
|
|
|
|
|
|
verifyNoInteractions(adapter);
|
|
|
}
|
|
|
|
|
|
@Test
|
|
|
void nonBusinessOffsetSnapshotTimestampReturnsTypedBadRequest() throws Exception {
|
|
|
- mockMvc.perform(get("/api/cockpit/v1/map-snapshot")
|
|
|
+ mockMvc.perform(get("/api/cockpit/v1/map/snapshot")
|
|
|
.param("snapshotAt", "2026-09-02T02:00:00Z"))
|
|
|
.andExpect(status().isBadRequest())
|
|
|
.andExpect(jsonPath("$.code").value(400))
|
|
|
.andExpect(jsonPath("$.data.category").value("INVALID_REQUEST"))
|
|
|
- .andExpect(jsonPath("$.data.message").value("地图快照分页参数不合法"));
|
|
|
+ .andExpect(jsonPath("$.data.message").exists());
|
|
|
|
|
|
verifyNoInteractions(adapter);
|
|
|
}
|
|
|
@@ -360,7 +362,7 @@ class CockpitMapSnapshotControllerTest {
|
|
|
void missingIssuedSnapshotDoesNotBecomeAnEmptyMap() throws Exception {
|
|
|
when(adapter.load(SNAPSHOT_AT)).thenReturn(null);
|
|
|
|
|
|
- mockMvc.perform(get("/api/cockpit/v1/map-snapshot")
|
|
|
+ mockMvc.perform(get("/api/cockpit/v1/map/snapshot")
|
|
|
.param("page", "2")
|
|
|
.param("snapshotAt", SNAPSHOT_AT))
|
|
|
.andExpect(status().isBadRequest())
|
|
|
@@ -373,30 +375,30 @@ class CockpitMapSnapshotControllerTest {
|
|
|
when(adapter.load("2026-09-02T09:00:00+08:00"))
|
|
|
.thenThrow(new CockpitSnapshotExpiredException("snapshot expired"));
|
|
|
|
|
|
- mockMvc.perform(get("/api/cockpit/v1/map-snapshot")
|
|
|
+ mockMvc.perform(get("/api/cockpit/v1/map/snapshot")
|
|
|
.param("page", "2")
|
|
|
.param("pageSize", "500")
|
|
|
.param("snapshotAt", "2026-09-02T09:00:00+08:00"))
|
|
|
.andExpect(status().isBadRequest())
|
|
|
.andExpect(jsonPath("$.code").value(400))
|
|
|
.andExpect(jsonPath("$.data.category").value("SNAPSHOT_EXPIRED"))
|
|
|
- .andExpect(jsonPath("$.data.message").value("地图快照已失效,请重新加载"));
|
|
|
+ .andExpect(jsonPath("$.data.message").exists());
|
|
|
}
|
|
|
|
|
|
@Test
|
|
|
void serverFailureReturnsTypedServerError() throws Exception {
|
|
|
when(adapter.load(null)).thenThrow(new CockpitReadException("upstream unavailable"));
|
|
|
|
|
|
- mockMvc.perform(get("/api/cockpit/v1/map-snapshot"))
|
|
|
+ mockMvc.perform(get("/api/cockpit/v1/map/snapshot"))
|
|
|
.andExpect(status().isInternalServerError())
|
|
|
.andExpect(jsonPath("$.code").value(500))
|
|
|
.andExpect(jsonPath("$.data.category").value("SERVER_ERROR"))
|
|
|
- .andExpect(jsonPath("$.data.message").value("驾驶舱地图快照数据暂不可用"));
|
|
|
+ .andExpect(jsonPath("$.data.message").exists());
|
|
|
}
|
|
|
|
|
|
@Test
|
|
|
void mutatingRequestIsRejectedBeforeReadAdapterIsCalled() throws Exception {
|
|
|
- mockMvc.perform(post("/api/cockpit/v1/map-snapshot"))
|
|
|
+ mockMvc.perform(post("/api/cockpit/v1/map/snapshot"))
|
|
|
.andExpect(status().isMethodNotAllowed());
|
|
|
|
|
|
verifyNoInteractions(adapter);
|
|
|
@@ -404,9 +406,9 @@ class CockpitMapSnapshotControllerTest {
|
|
|
|
|
|
@Test
|
|
|
void controllerOnlyExposesAuthenticatedReadOnlyOperations() {
|
|
|
- assertTrue(Arrays.stream(CockpitMapSnapshotController.class.getDeclaredMethods())
|
|
|
+ assertTrue(Arrays.stream(CockpitMapController.class.getDeclaredMethods())
|
|
|
.filter(this::hasRequestMappingAnnotation)
|
|
|
- .allMatch(CockpitMapSnapshotControllerTest::isReadOnlyMappedMethod));
|
|
|
+ .allMatch(CockpitMapControllerTest::isReadOnlyMappedMethod));
|
|
|
}
|
|
|
|
|
|
private boolean hasRequestMappingAnnotation(Method method) {
|