Files
baiyue-firstleg/file/src/test/java/com/baiyue/file/service/InternalBusinessFileServiceTest.java
T

59 lines
2.7 KiB
Java
Raw Normal View History

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");
}
@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());
}
}