feat(files): track v2 upload session parts

This commit is contained in:
yoyuzh
2026-04-08 15:22:52 +08:00
parent 35b0691188
commit 06a95bc489
9 changed files with 186 additions and 0 deletions

View File

@@ -27,6 +27,7 @@ import static org.mockito.Mockito.when;
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.user;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@@ -103,6 +104,29 @@ class UploadSessionV2ControllerTest {
.andExpect(jsonPath("$.data.status").value("COMPLETED"));
}
@Test
void shouldRecordUploadSessionPartWithV2Envelope() throws Exception {
User user = createUser(7L);
UploadSession session = createSession(user);
session.setStatus(UploadSessionStatus.UPLOADING);
when(userDetailsService.loadDomainUser("alice")).thenReturn(user);
when(uploadSessionService.recordUploadedPart(eq(user), eq("session-1"), eq(1), any())).thenReturn(session);
mockMvc.perform(put("/api/v2/files/upload-sessions/session-1/parts/1")
.with(user(userDetails()))
.contentType(MediaType.APPLICATION_JSON)
.content("""
{
"etag": "etag-1",
"size": 8388608
}
"""))
.andExpect(status().isOk())
.andExpect(jsonPath("$.code").value(0))
.andExpect(jsonPath("$.data.sessionId").value("session-1"))
.andExpect(jsonPath("$.data.status").value("UPLOADING"));
}
private UserDetails userDetails() {
return org.springframework.security.core.userdetails.User
.withUsername("alice")

View File

@@ -130,6 +130,55 @@ class UploadSessionServiceTest {
.isInstanceOf(BusinessException.class);
}
@Test
void shouldRecordUploadedPartAndMoveSessionToUploading() {
User user = createUser(7L);
UploadSession session = createSession(user);
session.setChunkCount(3);
when(uploadSessionRepository.findBySessionIdAndUserId("session-1", 7L))
.thenReturn(Optional.of(session));
when(uploadSessionRepository.save(any(UploadSession.class))).thenAnswer(invocation -> invocation.getArgument(0));
UploadSession result = uploadSessionService.recordUploadedPart(
user,
"session-1",
1,
new UploadSessionPartCommand("etag-1", 8L * 1024 * 1024)
);
assertThat(result.getStatus()).isEqualTo(UploadSessionStatus.UPLOADING);
assertThat(result.getUploadedPartsJson()).contains("\"partIndex\":1");
assertThat(result.getUploadedPartsJson()).contains("\"etag\":\"etag-1\"");
assertThat(result.getUploadedPartsJson()).contains("\"size\":8388608");
UploadSession secondResult = uploadSessionService.recordUploadedPart(
user,
"session-1",
2,
new UploadSessionPartCommand("etag-2", 4L)
);
assertThat(secondResult.getUploadedPartsJson()).contains("\"partIndex\":1");
assertThat(secondResult.getUploadedPartsJson()).contains("\"partIndex\":2");
assertThat(secondResult.getUploadedPartsJson()).contains("\"etag\":\"etag-2\"");
}
@Test
void shouldRejectUploadedPartOutsideSessionRange() {
User user = createUser(7L);
UploadSession session = createSession(user);
session.setChunkCount(3);
when(uploadSessionRepository.findBySessionIdAndUserId("session-1", 7L))
.thenReturn(Optional.of(session));
assertThatThrownBy(() -> uploadSessionService.recordUploadedPart(
user,
"session-1",
3,
new UploadSessionPartCommand("etag-3", 1L)
)).isInstanceOf(BusinessException.class);
}
private User createUser(Long id) {
User user = new User();
user.setId(id);