|
|
@@ -0,0 +1,110 @@
|
|
|
+package com.zksy.web.controller.cockpit;
|
|
|
+
|
|
|
+import com.zksy.cockpit.display.CockpitMapDisplayResponse;
|
|
|
+import com.zksy.cockpit.display.CockpitMapDisplayService;
|
|
|
+import com.zksy.cockpit.foundation.CockpitInvalidRequestException;
|
|
|
+import com.zksy.cockpit.foundation.CockpitMemberCursorExpiredException;
|
|
|
+import com.zksy.cockpit.foundation.CockpitReadException;
|
|
|
+import com.zksy.cockpit.foundation.CockpitSnapshotExpiredException;
|
|
|
+import org.junit.jupiter.api.BeforeEach;
|
|
|
+import org.junit.jupiter.api.Test;
|
|
|
+import org.springframework.test.web.servlet.MockMvc;
|
|
|
+import org.springframework.test.web.servlet.setup.MockMvcBuilders;
|
|
|
+
|
|
|
+import java.time.Clock;
|
|
|
+import java.time.Instant;
|
|
|
+import java.time.ZoneOffset;
|
|
|
+import java.util.Collections;
|
|
|
+
|
|
|
+import static org.mockito.ArgumentMatchers.any;
|
|
|
+import static org.mockito.ArgumentMatchers.anyString;
|
|
|
+import static org.mockito.Mockito.mock;
|
|
|
+import static org.mockito.Mockito.when;
|
|
|
+import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
|
|
+import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
|
|
|
+import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
|
|
+
|
|
|
+class CockpitMapDisplayControllerTest {
|
|
|
+ private static final String SNAPSHOT = "2026-11-25T10:00:00+08:00";
|
|
|
+ private static final Clock CLOCK = Clock.fixed(
|
|
|
+ Instant.parse("2026-11-25T02:00:00Z"), ZoneOffset.ofHours(8));
|
|
|
+
|
|
|
+ private CockpitMapDisplayService service;
|
|
|
+ private MockMvc mockMvc;
|
|
|
+
|
|
|
+ @BeforeEach
|
|
|
+ void setUp() {
|
|
|
+ service = mock(CockpitMapDisplayService.class);
|
|
|
+ mockMvc = MockMvcBuilders.standaloneSetup(new CockpitMapDisplayController(service))
|
|
|
+ .setControllerAdvice(new CockpitExceptionAdvice(CLOCK))
|
|
|
+ .build();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void classLevelRouteKeepsBucketsUrlAndPayload() throws Exception {
|
|
|
+ when(service.buckets(anyString(), anyString(), any(), any(), any(), any(), any(), any()))
|
|
|
+ .thenReturn(response());
|
|
|
+ mockMvc.perform(get("/api/cockpit/v1/map-display-buckets")
|
|
|
+ .header("X-Request-Id", "display-request-001")
|
|
|
+ .param("bbox", "110.39,28.43,110.4,28.44")
|
|
|
+ .param("zoomBand", "4"))
|
|
|
+ .andExpect(status().isOk())
|
|
|
+ .andExpect(jsonPath("$.code").value(200))
|
|
|
+ .andExpect(jsonPath("$.data.requestId").value("display-request-001"))
|
|
|
+ .andExpect(jsonPath("$.data.contract").value("display-buckets-v1"))
|
|
|
+ .andExpect(jsonPath("$.data.statusVersion").value(SNAPSHOT));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void invalidRequestUsesUnifiedAdvice() throws Exception {
|
|
|
+ when(service.buckets(anyString(), anyString(), any(), any(), any(), any(), any(), any()))
|
|
|
+ .thenThrow(new CockpitInvalidRequestException("bbox 格式不合法"));
|
|
|
+ mockMvc.perform(get("/api/cockpit/v1/map-display-buckets")
|
|
|
+ .header("X-Request-Id", "display-request-002")
|
|
|
+ .param("bbox", "bad")
|
|
|
+ .param("zoomBand", "4"))
|
|
|
+ .andExpect(status().isBadRequest())
|
|
|
+ .andExpect(jsonPath("$.data.category").value("INVALID_REQUEST"))
|
|
|
+ .andExpect(jsonPath("$.data.message").value("bbox 格式不合法"))
|
|
|
+ .andExpect(jsonPath("$.data.requestId").value("display-request-002"));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void cursorExpiredUsesUnifiedAdvice() throws Exception {
|
|
|
+ when(service.members(anyString(), anyString(), any(), any()))
|
|
|
+ .thenThrow(new CockpitMemberCursorExpiredException("成员游标已失效,请重新加载聚合桶"));
|
|
|
+ mockMvc.perform(get("/api/cockpit/v1/map-display-bucket-members")
|
|
|
+ .header("X-Request-Id", "display-request-003")
|
|
|
+ .param("cursor", "expired"))
|
|
|
+ .andExpect(status().isBadRequest())
|
|
|
+ .andExpect(jsonPath("$.data.category").value("MEMBER_CURSOR_EXPIRED"))
|
|
|
+ .andExpect(jsonPath("$.data.message").value("成员游标已失效,请重新加载聚合桶"));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void snapshotExpiredAndReadFailureKeepExistingCategories() throws Exception {
|
|
|
+ when(service.buckets(anyString(), anyString(), any(), any(), any(), any(), any(), any()))
|
|
|
+ .thenThrow(new CockpitSnapshotExpiredException("地图快照已失效,请重新加载"));
|
|
|
+ mockMvc.perform(get("/api/cockpit/v1/map-display-buckets")
|
|
|
+ .param("bbox", "110.39,28.43,110.4,28.44")
|
|
|
+ .param("zoomBand", "4"))
|
|
|
+ .andExpect(status().isBadRequest())
|
|
|
+ .andExpect(jsonPath("$.data.category").value("SNAPSHOT_EXPIRED"))
|
|
|
+ .andExpect(jsonPath("$.data.message").value("地图快照已失效,请重新加载"));
|
|
|
+
|
|
|
+ when(service.members(anyString(), anyString(), any(), any()))
|
|
|
+ .thenThrow(new CockpitReadException("显示聚合数据暂不可用"));
|
|
|
+ mockMvc.perform(get("/api/cockpit/v1/map-display-bucket-members")
|
|
|
+ .param("cursor", "c"))
|
|
|
+ .andExpect(status().isInternalServerError())
|
|
|
+ .andExpect(jsonPath("$.data.category").value("SERVER_ERROR"))
|
|
|
+ .andExpect(jsonPath("$.data.message").value("显示聚合数据暂不可用"));
|
|
|
+ }
|
|
|
+
|
|
|
+ private static CockpitMapDisplayResponse response() {
|
|
|
+ return new CockpitMapDisplayResponse("display-request-001", SNAPSHOT, SNAPSHOT, 4,
|
|
|
+ "110.39,28.43,110.4,28.44", 1000, false,
|
|
|
+ new CockpitMapDisplayResponse.Filters(null, null, null, "NONE", null, null),
|
|
|
+ Collections.emptyList());
|
|
|
+ }
|
|
|
+}
|