Merge remote-tracking branch 'origin/test' into dev
# Conflicts: # firstleg/src/main/java/com/baiyue/firstleg/loading/service/CabinetLoadingEvidenceService.java # firstleg/src/test/java/com/baiyue/firstleg/loading/service/CabinetLoadingEvidenceServiceTest.java # scripts/admin-test-env-modules/ocean-bookings.mjs # scripts/admin-test-env-modules/phase3-container-customs-fixture.mjs # scripts/admin-test-env-modules/phase3-container-documents-contract.test.mjs # scripts/admin-test-env-modules/phase3-container-loading.mjs # scripts/admin-test-env-modules/phase3-customs.mjs # scripts/admin-test-env-modules/phase3-loading-finalize.mjs # scripts/admin-test-env-modules/phase3-loading.mjs # scripts/admin-test-env-modules/phase3-port-schedules.mjs # scripts/admin-test-env-modules/service-center-flow.mjs
This commit is contained in:
+23
-19
@@ -24,8 +24,8 @@ import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.io.IOException;
|
||||
import java.security.MessageDigest;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.util.HexFormat;
|
||||
@@ -45,13 +45,13 @@ public class CabinetLoadingEvidenceService {
|
||||
|
||||
@Transactional
|
||||
public EvidenceList upsert(long taskId, String key, UpsertRequest request, long operatorId, String operatorName) {
|
||||
Scope scope = scope(taskId, request.warehouseId(), operatorId, true);
|
||||
requireKey(key);
|
||||
return persist(scope, taskId, key, request, hash(request), operatorId, operatorName);
|
||||
return upsert(taskId, key, request, operatorId, operatorName, hash(request));
|
||||
}
|
||||
|
||||
private EvidenceList persist(Scope scope, long taskId, String key, UpsertRequest request,
|
||||
String requestHash, long operatorId, String operatorName) {
|
||||
private EvidenceList upsert(long taskId, String key, UpsertRequest request, long operatorId,
|
||||
String operatorName, String requestHash) {
|
||||
Scope scope = scope(taskId, request.warehouseId(), operatorId, true);
|
||||
requireKey(key);
|
||||
CabinetLoadingEvidenceEntity replay = mapper.byOperation(taskId, key);
|
||||
if (replay != null) {
|
||||
if (!Objects.equals(requestHash, replay.getRequestHash())) throw conflict("相同幂等键的请求内容不一致");
|
||||
@@ -93,22 +93,20 @@ public class CabinetLoadingEvidenceService {
|
||||
long operatorId, String operatorName) {
|
||||
Scope scope = scope(taskId, warehouseId, operatorId, true);
|
||||
requireKey(key);
|
||||
String type = evidenceType.trim();
|
||||
if (!LoadingEvidenceType.VALUES.contains(type)) throw bad("不支持的装柜证据类型");
|
||||
String normalizedSealNo = LoadingEvidenceType.SEAL.equals(type) ? normalizedSeal(sealNo) : null;
|
||||
String requestHash = hash(new UploadHashRequest(warehouseId, expectedVersion, type,
|
||||
normalizedSealNo, remark, multipartHash(multipart)));
|
||||
String requestHash = uploadHash(warehouseId, expectedVersion, evidenceType, sealNo, remark, multipart);
|
||||
CabinetLoadingEvidenceEntity replay = mapper.byOperation(taskId, key);
|
||||
if (replay != null) {
|
||||
if (!Objects.equals(requestHash, replay.getRequestHash())) throw conflict("相同幂等键的请求内容不一致");
|
||||
if (!Objects.equals(requestHash, replay.getRequestHash())) {
|
||||
throw conflict("相同幂等键的请求内容不一致");
|
||||
}
|
||||
return view(scope);
|
||||
}
|
||||
try {
|
||||
var response = files.upload(internalToken, operatorId, scope.containerBookingId(), type, multipart);
|
||||
var response = files.upload(internalToken, operatorId, scope.containerBookingId(), evidenceType, multipart);
|
||||
var uploaded = response == null ? null : response.data();
|
||||
if (uploaded == null) throw new BizException(ResultCode.SERVICE_UNAVAILABLE, "装柜证据文件上传失败");
|
||||
return persist(scope, taskId, key, new UpsertRequest(warehouseId, expectedVersion, type,
|
||||
uploaded.fileId(), normalizedSealNo, remark), requestHash, operatorId, operatorName);
|
||||
return upsert(taskId, key, new UpsertRequest(warehouseId, expectedVersion, evidenceType,
|
||||
uploaded.fileId(), sealNo, remark), operatorId, operatorName, requestHash);
|
||||
} catch (FeignException exception) {
|
||||
throw new BizException(ResultCode.SERVICE_UNAVAILABLE, "文件服务暂不可用");
|
||||
}
|
||||
@@ -197,9 +195,17 @@ public class CabinetLoadingEvidenceService {
|
||||
objectMapper.writeValueAsString(value).getBytes(StandardCharsets.UTF_8))); }
|
||||
catch (JsonProcessingException | NoSuchAlgorithmException exception) { throw new IllegalStateException(exception); }
|
||||
}
|
||||
private static String multipartHash(MultipartFile file) {
|
||||
private static String uploadHash(long warehouseId, int expectedVersion, String evidenceType,
|
||||
String sealNo, String remark, MultipartFile multipart) {
|
||||
try {
|
||||
return HexFormat.of().formatHex(MessageDigest.getInstance("SHA-256").digest(file.getBytes()));
|
||||
MessageDigest digest = MessageDigest.getInstance("SHA-256");
|
||||
String metadata = warehouseId + "|" + expectedVersion + "|" + evidenceType + "|"
|
||||
+ Objects.toString(sealNo, "") + "|" + Objects.toString(remark, "") + "|"
|
||||
+ Objects.toString(multipart.getOriginalFilename(), "") + "|"
|
||||
+ Objects.toString(multipart.getContentType(), "") + "|" + multipart.getSize() + "|";
|
||||
digest.update(metadata.getBytes(StandardCharsets.UTF_8));
|
||||
digest.update(multipart.getBytes());
|
||||
return HexFormat.of().formatHex(digest.digest());
|
||||
} catch (IOException exception) {
|
||||
throw bad("装柜证据文件读取失败");
|
||||
} catch (NoSuchAlgorithmException exception) {
|
||||
@@ -214,7 +220,5 @@ public class CabinetLoadingEvidenceService {
|
||||
private static BizException bad(String message) { return new BizException(ResultCode.BAD_REQUEST, message); }
|
||||
private static BizException conflict(String message) { return new BizException(ResultCode.CONFLICT, message); }
|
||||
private static String id(Long value) { return value == null ? null : value.toString(); }
|
||||
private record UploadHashRequest(long warehouseId, int expectedVersion, String evidenceType,
|
||||
String sealNo, String remark, String fileHash) {}
|
||||
private record Scope(LoadingPickTaskEntity task, long containerBookingId) {}
|
||||
}
|
||||
|
||||
+26
-27
@@ -13,15 +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 org.springframework.mock.web.MockMultipartFile;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
|
||||
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.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyLong;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
@@ -104,35 +106,32 @@ class CabinetLoadingEvidenceServiceTest {
|
||||
}
|
||||
|
||||
@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));
|
||||
void multipartReplayUsesContentHashBeforeUploadingAnotherFile() {
|
||||
var multipart = new MockMultipartFile("file", "photo.png", "image/png", new byte[] {1, 2, 3});
|
||||
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));
|
||||
"LOADING_PHOTO_STEP_1", "99", "photo.png", "image/png", 3L);
|
||||
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)))
|
||||
when(files.upload(anyString(), anyLong(), anyLong(), anyString(), any()))
|
||||
.thenReturn(com.baiyue.common.core.api.ApiResponse.success(upload));
|
||||
when(files.validate(anyString(), anyLong(), anyString(), anyLong()))
|
||||
.thenReturn(com.baiyue.common.core.api.ApiResponse.success(metadata));
|
||||
var file = new MockMultipartFile("file", "photo.png", "image/png", "photo".getBytes(StandardCharsets.UTF_8));
|
||||
when(mapper.byContainer(99L)).thenReturn(List.of());
|
||||
|
||||
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");
|
||||
service.upload(20L, 8L, "same-key", 0, "PHOTO_STEP_1", null, "remark",
|
||||
multipart, 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));
|
||||
ArgumentCaptor<CabinetLoadingEvidenceEntity> inserted =
|
||||
ArgumentCaptor.forClass(CabinetLoadingEvidenceEntity.class);
|
||||
verify(mapper).insert(inserted.capture());
|
||||
when(mapper.byOperation(20L, "same-key")).thenReturn(inserted.getValue());
|
||||
when(mapper.byContainer(99L)).thenReturn(List.of(inserted.getValue()));
|
||||
|
||||
var replay = service.upload(20L, 8L, "same-key", 0, "PHOTO_STEP_1", null, "remark",
|
||||
multipart, 9L, "operator");
|
||||
|
||||
assertThat(replay.evidences()).hasSize(1);
|
||||
verify(files, times(1)).upload(anyString(), anyLong(), anyLong(), anyString(), any());
|
||||
}
|
||||
|
||||
private static CabinetLoadingEvidenceEntity evidence(String type) {
|
||||
|
||||
+8
@@ -58,6 +58,10 @@ class SmartLoadingCandidateMapperContractTest {
|
||||
.contains("SELECT SUM(li.package_count)")
|
||||
.contains("li.deleted=0")
|
||||
.contains("ORDER BY o.inbounded_at ASC, o.id ASC, i.inbound_detail_id ASC");
|
||||
|
||||
assertThat(sql.indexOf("NULL blockReason"))
|
||||
.as("record constructor projection must keep status fields in their trailing positions")
|
||||
.isLessThan(sql.indexOf("o.order_status_snapshot orderStatusSnapshot"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -90,6 +94,10 @@ class SmartLoadingCandidateMapperContractTest {
|
||||
.contains("source.fulfillment_type='ONE_CLICK_DROPSHIP'")
|
||||
.contains("i.inbound_id=o.id")
|
||||
.contains("FOR UPDATE");
|
||||
|
||||
assertThat(sql.indexOf("NULL blockReason"))
|
||||
.as("record constructor projection must keep status fields in their trailing positions")
|
||||
.isLessThan(sql.indexOf("o.order_status_snapshot orderStatusSnapshot"));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
Reference in New Issue
Block a user