feat(api): add v2 phase one skeleton

This commit is contained in:
yoyuzh
2026-04-08 14:28:01 +08:00
parent 3afebbb338
commit 9d5fdd9ea3
20 changed files with 1585 additions and 2 deletions

View File

@@ -0,0 +1,36 @@
package com.yoyuzh.api.v2;
import org.junit.jupiter.api.Test;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import static org.assertj.core.api.Assertions.assertThat;
class ApiV2ExceptionHandlerTest {
private final ApiV2ExceptionHandler handler = new ApiV2ExceptionHandler();
@Test
void shouldMapV2BusinessErrorsToV2ResponseEnvelope() {
ResponseEntity<ApiV2Response<Void>> response = handler.handleApiV2Exception(
new ApiV2Exception(ApiV2ErrorCode.FILE_NOT_FOUND, "文件不存在")
);
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.NOT_FOUND);
assertThat(response.getBody()).isNotNull();
assertThat(response.getBody().code()).isEqualTo(ApiV2ErrorCode.FILE_NOT_FOUND.getCode());
assertThat(response.getBody().msg()).isEqualTo("文件不存在");
assertThat(response.getBody().data()).isNull();
}
@Test
void shouldKeepUnknownV2ErrorsInsideTheV2ErrorCodeRange() {
ResponseEntity<ApiV2Response<Void>> response = handler.handleUnknownException(new RuntimeException("boom"));
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.INTERNAL_SERVER_ERROR);
assertThat(response.getBody()).isNotNull();
assertThat(response.getBody().code()).isEqualTo(ApiV2ErrorCode.INTERNAL_ERROR.getCode());
assertThat(response.getBody().msg()).isEqualTo("服务器内部错误");
assertThat(response.getBody().data()).isNull();
}
}

View File

@@ -0,0 +1,30 @@
package com.yoyuzh.api.v2.site;
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 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 SiteV2ControllerTest {
private MockMvc mockMvc;
@BeforeEach
void setUp() {
mockMvc = MockMvcBuilders.standaloneSetup(new SiteV2Controller()).build();
}
@Test
void shouldExposeV2PingWithV2ResponseEnvelope() throws Exception {
mockMvc.perform(get("/api/v2/site/ping"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.code").value(0))
.andExpect(jsonPath("$.msg").value("success"))
.andExpect(jsonPath("$.data.status").value("ok"))
.andExpect(jsonPath("$.data.apiVersion").value("v2"));
}
}