fix: make loading evidence replay idempotent

This commit is contained in:
2026-09-01 22:59:50 +08:00
parent d965a82cfd
commit e19224c4b7
3 changed files with 87 additions and 6 deletions
@@ -13,12 +13,17 @@ import jakarta.annotation.Resource;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
import java.util.List;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.util.HexFormat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@@ -98,6 +103,38 @@ class CabinetLoadingEvidenceServiceTest {
verify(mapper).insert(org.mockito.ArgumentMatchers.any(CabinetLoadingEvidenceEntity.class));
}
@Test
void multipartReplayUsesRequestAndContentHashBeforeUploadingAgain() throws Exception {
when(objectMapper.writeValueAsString(org.mockito.ArgumentMatchers.any())).thenReturn("request");
CabinetLoadingEvidenceEntity persisted = evidence("PHOTO_STEP_1");
persisted.setId(401L); persisted.setSourceTaskId(20L); persisted.setFileId(301L);
persisted.setOperationKey("photo-replay");
persisted.setRequestHash(HexFormat.of().formatHex(MessageDigest.getInstance("SHA-256")
.digest("request".getBytes(StandardCharsets.UTF_8))));
persisted.setVersion(0);
when(mapper.byOperation(20L, "photo-replay")).thenReturn(null, null, persisted);
when(mapper.byContainer(99L)).thenReturn(List.of(persisted));
var upload = new LoadingEvidenceFileClient.UploadResult(301L, "PLATFORM", 0L,
"LOADING_PHOTO_STEP_1", "99", "photo.png", "image/png", 5L);
when(files.upload(org.mockito.ArgumentMatchers.anyString(), org.mockito.ArgumentMatchers.eq(9L),
org.mockito.ArgumentMatchers.eq(99L), org.mockito.ArgumentMatchers.eq("PHOTO_STEP_1"),
org.mockito.ArgumentMatchers.any())).thenReturn(com.baiyue.common.core.api.ApiResponse.success(upload));
var metadata = new LoadingEvidenceFileClient.FileMetadata(301L, "PLATFORM", 0L,
"LOADING_PHOTO_STEP_1", "99", "photo.png", "image/png");
when(files.validate(org.mockito.ArgumentMatchers.anyString(), org.mockito.ArgumentMatchers.eq(99L),
org.mockito.ArgumentMatchers.eq("PHOTO_STEP_1"), org.mockito.ArgumentMatchers.eq(301L)))
.thenReturn(com.baiyue.common.core.api.ApiResponse.success(metadata));
var file = new MockMultipartFile("file", "photo.png", "image/png", "photo".getBytes(StandardCharsets.UTF_8));
service.upload(20L, 8L, "photo-replay", 0, "PHOTO_STEP_1", null, "remark", file, 9L, "operator");
service.upload(20L, 8L, "photo-replay", 0, "PHOTO_STEP_1", null, "remark", file, 9L, "operator");
verify(files, times(1)).upload(org.mockito.ArgumentMatchers.anyString(), org.mockito.ArgumentMatchers.eq(9L),
org.mockito.ArgumentMatchers.eq(99L), org.mockito.ArgumentMatchers.eq("PHOTO_STEP_1"),
org.mockito.ArgumentMatchers.any());
verify(mapper, times(1)).insert(org.mockito.ArgumentMatchers.any(CabinetLoadingEvidenceEntity.class));
}
private static CabinetLoadingEvidenceEntity evidence(String type) {
CabinetLoadingEvidenceEntity value = new CabinetLoadingEvidenceEntity();
value.setEvidenceType(type); value.setStatus("ACTIVE"); value.setContainerBookingId(99L);