刘光辉
3 小时以前 06f314545ac3514d7ce28939920866ae8188d31c
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
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.LinkedHashMap;
import java.util.Map;
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.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.when;
 
public class DmsFileStorageServiceTest {
 
    @Test
    public void approvedContentReturnsStorageFactsWithoutCallingBackIntoDms() {
        Fixture f = new Fixture();
        DmsFileUploadRegisterForm result = f.service.storeWorkflowContent(f.documentId, f.baseVersionId, f.file);
        assertEquals(f.documentId, result.getDocumentId());
        org.junit.Assert.assertNotNull(result.getFileVersionId());
        org.junit.Assert.assertNotNull(result.getFileObjectId());
        assertEquals("a".repeat(64), result.getChecksumSha256());
        assertEquals(Long.valueOf(12L), result.getSizeBytes());
        assertEquals("jnpf-dms", result.getBucketName());
        verify(f.upload).setSaveFilename(result.getFileObjectId() + ".txt");
        org.mockito.Mockito.verifyNoInteractions(f.dmsResourceApi);
    }
 
    @Test
    public void approvedContentRejectsMissingChecksum() {
        Fixture f = new Fixture(); f.stored.setHashInfo(null);
        assertThrows(DataException.class, () -> f.service.storeWorkflowContent(f.documentId, f.baseVersionId, f.file));
        verify(f.fileStorageService).delete(f.stored);
        org.mockito.Mockito.verifyNoInteractions(f.dmsResourceApi);
    }
 
    @Test
    public void serverErrorRetainsObjectsForReconciliation() {
        Fixture fixture = new Fixture();
        ActionResult<DmsFileUploadVO> response = new ActionResult<>();
        response.setCode(500);
        when(fixture.dmsResourceApi.registerUpload(anyString(), anyString(), fixture.formCaptor.capture()))
                .thenReturn(response);
        assertThrows(DataException.class, fixture::upload);
        verify(fixture.fileStorageService, never()).delete(fixture.stored);
    }
 
    @Test
    public void timeoutAfterRegistrationDoesNotDeletePossiblyCommittedObjects() {
        Fixture fixture = new Fixture();
        when(fixture.dmsResourceApi.registerUpload(anyString(), anyString(), fixture.formCaptor.capture()))
                .thenThrow(new IllegalStateException("response timed out after commit"));
        when(fixture.dmsResourceApi.registerRevision(anyString(), anyString(), fixture.revisionCaptor.capture()))
                .thenThrow(new IllegalStateException("response timed out after commit"));
        assertThrows(IllegalStateException.class, fixture::upload);
        assertThrows(IllegalStateException.class, fixture::revise);
        verify(fixture.fileStorageService, never()).delete(fixture.stored);
    }
 
    @Test
    public void missingRegistrationResponseRetainsUploadedObjects() {
        Fixture fixture = new Fixture();
        assertThrows(DataException.class, fixture::upload);
        assertThrows(DataException.class, fixture::revise);
        verify(fixture.fileStorageService, never()).delete(fixture.stored);
    }
 
    @Test
    public void successWithoutDataRetainsUploadedObjects() {
        Fixture fixture = new Fixture();
        ActionResult<DmsFileUploadVO> response = new ActionResult<>();
        response.setCode(200);
        when(fixture.dmsResourceApi.registerUpload(anyString(), anyString(), fixture.formCaptor.capture()))
                .thenReturn(response);
        when(fixture.dmsResourceApi.registerRevision(anyString(), anyString(), fixture.revisionCaptor.capture()))
                .thenReturn(response);
        assertThrows(DataException.class, fixture::upload);
        assertThrows(DataException.class, fixture::revise);
        verify(fixture.fileStorageService, never()).delete(fixture.stored);
    }
 
    @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(), 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());
        assertEquals("sign-1", form.getAuditSignParameters().get("biz_sign"));
        verify(fixture.dmsResourceApi).registerUpload(anyString(),
                org.mockito.ArgumentMatchers.eq("audit-sign-1"), org.mockito.ArgumentMatchers.any());
        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(), 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(), 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());
        assertEquals("sign-1", form.getAuditSignParameters().get("biz_sign"));
        verify(fixture.dmsResourceApi).registerRevision(anyString(),
                org.mockito.ArgumentMatchers.eq("audit-sign-1"), org.mockito.ArgumentMatchers.any());
        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(), 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() {
            Map<String, Object> signs = new LinkedHashMap<>();
            signs.put("biz_sign", "sign-1");
            return service.upload(folderId, "source-1", "MM-R02", "质量手册",
                    "QUALITY_MANUAL", "dept-quality", "D(00)", "author-1",
                    LocalDate.of(2026, 8, 21), signs, "audit-sign-1", file);
        }
 
        private DmsFileUploadVO revise() {
            Map<String, Object> signs = new LinkedHashMap<>();
            signs.put("biz_sign", "sign-1");
            return service.revise(documentId, baseVersionId, "revision-1", "D(01)",
                    "author-2", LocalDate.of(2026, 8, 22), "更新质量职责", signs,
                    "audit-sign-1", 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;
        }
    }
}