刘光辉
15 小时以前 34981c30a78e8bbd7791131059a9210f9928b62c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package jnpf.fileStorage.dms;
 
import jnpf.base.ActionResult;
import jnpf.config.ConfigValueUtil;
import jnpf.dms.DmsResourceApi;
import jnpf.dmsEntity.storage.form.DmsFileRevisionRegisterForm;
import jnpf.dmsEntity.storage.form.DmsFileUploadRegisterForm;
import jnpf.dmsEntity.storage.vo.DmsFileUploadVO;
import jnpf.exception.DataException;
import org.dromara.x.file.storage.core.FileInfo;
import org.dromara.x.file.storage.core.FileStorageService;
import org.dromara.x.file.storage.core.hash.HashInfo;
import org.dromara.x.file.storage.core.platform.MinioFileStorage;
import org.dromara.x.file.storage.core.upload.UploadPretreatment;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
import org.springframework.web.multipart.MultipartFile;
 
import java.time.LocalDate;
import java.util.UUID;
 
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
 
public class DmsFileStorageServiceTest {
 
    @Test
    public void uploadsToDedicatedSeaweedFsPlatformAndRegistersDmsResources() {
        Fixture fixture = new Fixture();
        DmsFileUploadVO registered = fixture.registeredViewForStoredObject();
        ActionResult<DmsFileUploadVO> response = mock(ActionResult.class);
        when(response.getCode()).thenReturn(200);
        when(response.getData()).thenReturn(registered);
        when(fixture.dmsResourceApi.registerUpload(anyString(), fixture.formCaptor.capture()))
                .thenReturn(response);
 
        DmsFileUploadVO result = fixture.upload();
 
        assertEquals(registered, result);
        verify(fixture.upload).setPlatform("seaweedfs-1");
        verify(fixture.upload).setHashCalculatorSha256();
        DmsFileUploadRegisterForm form = fixture.formCaptor.getValue();
        assertEquals(fixture.folderId, form.getFolderId());
        assertEquals("MM-R02", form.getDocumentNo());
        assertEquals("质量手册", form.getDocumentName());
        assertEquals("QUALITY_MANUAL", form.getDocumentType());
        assertEquals("dept-quality", form.getResponsibleDeptId());
        assertEquals("D(00)", form.getVersionLabel());
        assertEquals("author-1", form.getPreparedBy());
        assertEquals(LocalDate.of(2026, 8, 21), form.getPreparedOn());
        assertEquals("seaweedfs-1", form.getStoragePlatform());
        assertEquals("jnpf-dms", form.getBucketName());
        assertEquals("a".repeat(64), form.getChecksumSha256());
        assertTrue(form.getDocumentId().version() == 7);
    }
 
    @Test
    public void deletesStoredObjectWhenDmsRegistrationFails() {
        Fixture fixture = new Fixture();
        ActionResult<DmsFileUploadVO> response = mock(ActionResult.class);
        when(response.getCode()).thenReturn(400);
        when(response.getMsg()).thenReturn("DMS rejected");
        when(fixture.dmsResourceApi.registerUpload(anyString(), fixture.formCaptor.capture()))
                .thenReturn(response);
        when(fixture.fileStorageService.delete(fixture.stored)).thenReturn(true);
 
        assertThrows(DataException.class,
                fixture::upload);
 
        verify(fixture.fileStorageService).delete(fixture.stored);
    }
 
    @Test
    public void uploadsRevisionToSameDocumentAndRegistersNewVersion() {
        Fixture fixture = new Fixture();
        DmsFileUploadVO registered = fixture.registeredViewForStoredObject();
        ActionResult<DmsFileUploadVO> response = mock(ActionResult.class);
        when(response.getCode()).thenReturn(200);
        when(response.getData()).thenReturn(registered);
        when(fixture.dmsResourceApi.registerRevision(anyString(), fixture.revisionCaptor.capture()))
                .thenReturn(response);
 
        DmsFileUploadVO result = fixture.revise();
 
        assertEquals(registered, result);
        verify(fixture.upload).setPlatform("seaweedfs-1");
        DmsFileRevisionRegisterForm form = fixture.revisionCaptor.getValue();
        assertEquals(fixture.documentId, form.getDocumentId());
        assertEquals(fixture.baseVersionId, form.getBaseFileVersionId());
        assertEquals("D(01)", form.getVersionLabel());
        assertEquals("更新质量职责", form.getRevisionReason());
        assertEquals("author-2", form.getPreparedBy());
        assertEquals(LocalDate.of(2026, 8, 22), form.getPreparedOn());
        assertTrue(form.getFileVersionId().version() == 7);
        assertTrue(form.getFileObjectId().version() == 7);
    }
 
    @Test
    public void deletesRevisionObjectWhenDmsRegistrationFails() {
        Fixture fixture = new Fixture();
        ActionResult<DmsFileUploadVO> response = mock(ActionResult.class);
        when(response.getCode()).thenReturn(400);
        when(response.getMsg()).thenReturn("current version changed");
        when(fixture.dmsResourceApi.registerRevision(anyString(), fixture.revisionCaptor.capture()))
                .thenReturn(response);
        when(fixture.fileStorageService.delete(fixture.stored)).thenReturn(true);
 
        assertThrows(DataException.class, fixture::revise);
 
        verify(fixture.fileStorageService).delete(fixture.stored);
    }
 
    private static class Fixture {
        private final UUID folderId = UUID.fromString("018f0000-0000-7000-8000-000000000401");
        private final UUID documentId = UUID.fromString("018f0000-0000-7000-8000-000000000402");
        private final UUID baseVersionId = UUID.fromString("018f0000-0000-7000-8000-000000000403");
        private final FileStorageService fileStorageService = mock(FileStorageService.class);
        private final DmsResourceApi dmsResourceApi = mock(DmsResourceApi.class);
        private final ConfigValueUtil configValueUtil = mock(ConfigValueUtil.class);
        private final MultipartFile file = mock(MultipartFile.class);
        private final UploadPretreatment upload = mock(UploadPretreatment.class);
        private final MinioFileStorage minio = mock(MinioFileStorage.class);
        private final FileInfo stored = storedFile();
        private final ArgumentCaptor<DmsFileUploadRegisterForm> formCaptor =
                ArgumentCaptor.forClass(DmsFileUploadRegisterForm.class);
        private final ArgumentCaptor<DmsFileRevisionRegisterForm> revisionCaptor =
                ArgumentCaptor.forClass(DmsFileRevisionRegisterForm.class);
        private final DmsFileStorageService service =
                new DmsFileStorageService(fileStorageService, dmsResourceApi, configValueUtil);
 
        private Fixture() {
            when(file.isEmpty()).thenReturn(false);
            when(file.getOriginalFilename()).thenReturn("report.txt");
            when(configValueUtil.getAllowUploadFileType()).thenReturn("txt,pdf,doc,docx");
            when(fileStorageService.getFileStorageVerify("seaweedfs-1")).thenReturn(minio);
            when(fileStorageService.getFileStorage("seaweedfs-1")).thenReturn(minio);
            when(minio.getBucketName()).thenReturn("jnpf-dms");
            when(fileStorageService.of(file)).thenReturn(upload);
            when(upload.setPlatform("seaweedfs-1")).thenReturn(upload);
            when(upload.setPath(anyString())).thenReturn(upload);
            when(upload.setSaveFilename(anyString())).thenReturn(upload);
            when(upload.setOriginalFilename("report.txt")).thenReturn(upload);
            when(upload.setHashCalculatorSha256()).thenReturn(upload);
            when(upload.upload()).thenReturn(stored);
        }
 
        private DmsFileUploadVO registeredViewForStoredObject() {
            DmsFileUploadVO view = new DmsFileUploadVO();
            view.setStoragePlatform(stored.getPlatform());
            view.setBasePath(stored.getBasePath());
            view.setObjectPath(stored.getPath());
            view.setObjectName(stored.getFilename());
            return view;
        }
 
        private DmsFileUploadVO upload() {
            return service.upload(folderId, "source-1", "MM-R02", "质量手册",
                    "QUALITY_MANUAL", "dept-quality", "D(00)", "author-1",
                    LocalDate.of(2026, 8, 21), file);
        }
 
        private DmsFileUploadVO revise() {
            return service.revise(documentId, baseVersionId, "revision-1", "D(01)",
                    "author-2", LocalDate.of(2026, 8, 22), "更新质量职责", file);
        }
 
        private static FileInfo storedFile() {
            HashInfo hashInfo = new HashInfo();
            hashInfo.put("SHA-256", "a".repeat(64));
            FileInfo info = new FileInfo();
            info.setPlatform("seaweedfs-1");
            info.setBasePath("dms/");
            info.setPath("2026/08/17/document/");
            info.setFilename("version.txt");
            info.setOriginalFilename("report.txt");
            info.setContentType("text/plain");
            info.setSize(12L);
            info.setHashInfo(hashInfo);
            return info;
        }
    }
}