2026-08-08 22:36:41 +08:00
|
|
|
package com.baiyue.file.service;
|
|
|
|
|
|
|
|
|
|
import com.baiyue.common.core.exception.BizException;
|
|
|
|
|
import com.baiyue.file.entity.FileObjectEntity;
|
|
|
|
|
import com.baiyue.file.mapper.FileObjectMapper;
|
|
|
|
|
import org.junit.jupiter.api.BeforeEach;
|
|
|
|
|
import org.junit.jupiter.api.Test;
|
|
|
|
|
import org.mockito.*;
|
|
|
|
|
import org.springframework.mock.web.MockMultipartFile;
|
|
|
|
|
|
|
|
|
|
import static org.assertj.core.api.Assertions.*;
|
|
|
|
|
import static org.mockito.ArgumentMatchers.*;
|
|
|
|
|
import static org.mockito.Mockito.*;
|
|
|
|
|
|
|
|
|
|
class InternalBusinessFileServiceTest {
|
|
|
|
|
@Mock FileObjectMapper files;
|
|
|
|
|
@Mock BusinessFileStorage storage;
|
|
|
|
|
@InjectMocks InternalBusinessFileService service;
|
|
|
|
|
|
|
|
|
|
@BeforeEach void setUp() { MockitoAnnotations.openMocks(this); }
|
|
|
|
|
|
|
|
|
|
@Test void customsDocumentUploadPersistsExactBusinessOwnership() {
|
|
|
|
|
MockMultipartFile file = new MockMultipartFile("file", "invoice.txt", "text/plain", "invoice".getBytes());
|
|
|
|
|
when(storage.storeLoading(10001L, 41L, file)).thenReturn(new BusinessFileStorage.Stored(
|
|
|
|
|
"business-files/10001/41/key.txt", "text/plain", 7, "a".repeat(64), "invoice.txt"));
|
|
|
|
|
when(files.insert(any(FileObjectEntity.class))).thenAnswer(invocation -> {
|
|
|
|
|
((FileObjectEntity) invocation.getArgument(0)).setId(71L); return 1;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
var result = service.upload(10001L, "CUSTOMS_DOCUMENT", "41", 7L, file);
|
|
|
|
|
|
|
|
|
|
assertThat(result.fileId()).isEqualTo(71L);
|
|
|
|
|
assertThat(result.businessType()).isEqualTo("CUSTOMS_DOCUMENT");
|
|
|
|
|
assertThat(result.businessId()).isEqualTo("41");
|
|
|
|
|
assertThat(result.contentType()).isEqualTo("text/plain");
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-13 09:49:55 +08:00
|
|
|
@Test void platformContainerAndLoadingDocumentsSupportZeroTenantScope() {
|
|
|
|
|
MockMultipartFile file = new MockMultipartFile("file", "customs.pdf", "application/pdf", "%PDF-1.4".getBytes());
|
|
|
|
|
when(storage.storeLoading(eq(0L), anyLong(), same(file))).thenAnswer(invocation ->
|
|
|
|
|
new BusinessFileStorage.Stored("business-files/0/" + invocation.getArgument(1) + "/customs.pdf",
|
|
|
|
|
"application/pdf", file.getSize(), "b".repeat(64), "customs.pdf"));
|
|
|
|
|
when(files.insert(any(FileObjectEntity.class))).thenAnswer(invocation -> {
|
|
|
|
|
((FileObjectEntity) invocation.getArgument(0)).setId(81L); return 1;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
var clearance = service.upload(0L, "CONTAINER_CLEARANCE_DOCUMENT", "41", 7L, file);
|
|
|
|
|
var customs = service.upload(0L, "LOADING_CUSTOMS_DOCUMENT", "51", 7L, file);
|
2026-08-13 11:57:33 +08:00
|
|
|
var deliveryOrder = service.upload(0L, "CONTAINER_DELIVERY_ORDER", "41", 7L, file);
|
|
|
|
|
var pickupOrder = service.upload(0L, "CONTAINER_PICKUP_ORDER", "41", 7L, file);
|
2026-08-13 09:49:55 +08:00
|
|
|
|
|
|
|
|
assertThat(clearance).extracting(InternalBusinessFileService.Metadata::tenantId,
|
|
|
|
|
InternalBusinessFileService.Metadata::businessType,
|
|
|
|
|
InternalBusinessFileService.Metadata::businessId).containsExactly(0L, "CONTAINER_CLEARANCE_DOCUMENT", "41");
|
|
|
|
|
assertThat(customs).extracting(InternalBusinessFileService.Metadata::tenantId,
|
|
|
|
|
InternalBusinessFileService.Metadata::businessType,
|
|
|
|
|
InternalBusinessFileService.Metadata::businessId).containsExactly(0L, "LOADING_CUSTOMS_DOCUMENT", "51");
|
2026-08-13 11:57:33 +08:00
|
|
|
assertThat(deliveryOrder.businessType()).isEqualTo("CONTAINER_DELIVERY_ORDER");
|
|
|
|
|
assertThat(pickupOrder.businessType()).isEqualTo("CONTAINER_PICKUP_ORDER");
|
|
|
|
|
verify(storage, times(3)).storeLoading(0L, 41L, file);
|
2026-08-13 09:49:55 +08:00
|
|
|
verify(storage).storeLoading(0L, 51L, file);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test void rejectsNegativeTenantAndInvalidOwnershipInputs() {
|
|
|
|
|
MockMultipartFile file = new MockMultipartFile("file", "a.txt", "text/plain", "x".getBytes());
|
|
|
|
|
assertThatThrownBy(() -> service.upload(-1L, "CONTAINER_CLEARANCE_DOCUMENT", "41", 7L, file))
|
|
|
|
|
.isInstanceOf(BizException.class).hasMessageContaining("归属");
|
|
|
|
|
assertThatThrownBy(() -> service.upload(0L, "CONTAINER_CLEARANCE_DOCUMENT", "0", 7L, file))
|
|
|
|
|
.isInstanceOf(BizException.class).hasMessageContaining("业务ID");
|
|
|
|
|
assertThatThrownBy(() -> service.upload(0L, "CONTAINER_CLEARANCE_DOCUMENT", "41", 0L, file))
|
|
|
|
|
.isInstanceOf(BizException.class).hasMessageContaining("操作人");
|
|
|
|
|
verifyNoInteractions(files, storage);
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-08 22:36:41 +08:00
|
|
|
@Test void unsupportedBusinessTypeFailsClosed() {
|
|
|
|
|
assertThatThrownBy(() -> service.upload(10001L, "ARBITRARY", "41", 7L,
|
|
|
|
|
new MockMultipartFile("file", "a.txt", "text/plain", "x".getBytes())))
|
|
|
|
|
.isInstanceOf(BizException.class).hasMessageContaining("不支持");
|
|
|
|
|
verifyNoInteractions(files, storage);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test void downloadUsesValidatedMetadataObjectKey() {
|
|
|
|
|
FileObjectEntity value = new FileObjectEntity(); value.setId(71L); value.setTenantId(10001L);
|
|
|
|
|
value.setBusinessType("CUSTOMS_RECEIPT"); value.setBusinessId("61"); value.setOriginalName("release.txt");
|
|
|
|
|
value.setObjectKey("business-files/10001/61/release.txt"); value.setContentType("text/plain");
|
|
|
|
|
when(files.selectOne(any())).thenReturn(value);
|
|
|
|
|
when(storage.read(value.getObjectKey())).thenReturn("released".getBytes());
|
|
|
|
|
|
|
|
|
|
var result = service.download(10001L, "CUSTOMS_RECEIPT", "61", 71L);
|
|
|
|
|
|
|
|
|
|
assertThat(result.fileName()).isEqualTo("release.txt");
|
|
|
|
|
assertThat(result.content()).isEqualTo("released".getBytes());
|
|
|
|
|
verify(storage).read(value.getObjectKey());
|
|
|
|
|
}
|
2026-08-13 09:49:55 +08:00
|
|
|
|
|
|
|
|
@Test void platformMetadataAndDownloadRequireExactCompositeOwnership() {
|
|
|
|
|
FileObjectEntity value = new FileObjectEntity(); value.setId(91L); value.setTenantId(0L);
|
|
|
|
|
value.setBusinessType("LOADING_CUSTOMS_DOCUMENT"); value.setBusinessId("51");
|
|
|
|
|
value.setOriginalName("declaration.pdf"); value.setObjectKey("business-files/0/51/declaration.pdf");
|
|
|
|
|
value.setContentType("application/pdf"); value.setSha256("c".repeat(64)); value.setSizeBytes(8L);
|
|
|
|
|
when(files.selectOne(any())).thenReturn(value, value, null, null, null);
|
|
|
|
|
when(storage.read(value.getObjectKey())).thenReturn("document".getBytes());
|
|
|
|
|
|
|
|
|
|
assertThat(service.metadata(0L, "LOADING_CUSTOMS_DOCUMENT", "51", 91L).fileName())
|
|
|
|
|
.isEqualTo("declaration.pdf");
|
|
|
|
|
assertThat(service.download(0L, "LOADING_CUSTOMS_DOCUMENT", "51", 91L).content())
|
|
|
|
|
.isEqualTo("document".getBytes());
|
|
|
|
|
assertThatThrownBy(() -> service.metadata(0L, "CONTAINER_CLEARANCE_DOCUMENT", "51", 91L))
|
|
|
|
|
.isInstanceOf(BizException.class).hasMessageContaining("归属不匹配");
|
|
|
|
|
assertThatThrownBy(() -> service.metadata(0L, "LOADING_CUSTOMS_DOCUMENT", "52", 91L))
|
|
|
|
|
.isInstanceOf(BizException.class).hasMessageContaining("归属不匹配");
|
|
|
|
|
assertThatThrownBy(() -> service.metadata(0L, "LOADING_CUSTOMS_DOCUMENT", "51", 92L))
|
|
|
|
|
.isInstanceOf(BizException.class).hasMessageContaining("归属不匹配");
|
|
|
|
|
verify(storage).read(value.getObjectKey());
|
|
|
|
|
}
|
2026-08-08 22:36:41 +08:00
|
|
|
}
|