刘光辉
4 小时以前 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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
package jnpf.controller;
 
import cn.dev33.satoken.same.SaSameUtil;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import jnpf.audit.sdk.AuditClient;
import jnpf.audit.sdk.AuditEventPublisher;
import jnpf.audit.sdk.aspect.AuditLogAspect;
import jnpf.dmsEntity.storage.form.DmsFileUploadRegisterForm;
import jnpf.exception.DataException;
import jnpf.fileStorage.dms.DmsCommonFileReader;
import jnpf.fileStorage.dms.DmsApprovalPageMergeService;
import jnpf.fileStorage.dms.DmsFileStorageService;
import jnpf.fileStorage.dms.DmsQianpiyeDocxWriter;
import jnpf.fileStorage.dms.FileContentImportService;
import jnpf.i18n.config.I18nProperties;
import jnpf.model.filecontent.FileContentImportForm;
import jnpf.util.context.SpringContext;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.mockito.MockedStatic;
import org.mockito.MockMakers;
import org.springframework.aop.aspectj.annotation.AspectJProxyFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.MessageSource;
import org.springframework.context.support.StaticMessageSource;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.multipart.MultipartFile;
 
import java.util.UUID;
import java.util.Map;
import java.util.function.Function;
 
import static org.junit.Assert.*;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
 
public class FileContentImportHttpTest {
    private final ObjectMapper json = new ObjectMapper();
    private final DmsCommonFileReader reader = mock(DmsCommonFileReader.class);
    private final DmsFileStorageService storage = mock(DmsFileStorageService.class);
    private final DmsApprovalPageMergeService approvalPages = mock(DmsApprovalPageMergeService.class);
    private final DmsQianpiyeDocxWriter qianpiyeWriter = mock(DmsQianpiyeDocxWriter.class);
    private final jnpf.fileStorage.dms.DmsDocxCommentCleaner cleaner = mock(jnpf.fileStorage.dms.DmsDocxCommentCleaner.class);
    private final FileContentImportService service = new FileContentImportService(
            reader, storage, approvalPages, qianpiyeWriter, cleaner, json);
    private final FileContentController controller = new FileContentController(service);
    private final MultipartFile attachment = mock(MultipartFile.class);
    private final FileContentImportForm form = new FileContentImportForm();
    private final String url = "/api/file/Image/annex/main.txt";
    private MockMvc mvc;
    private MockedStatic<SaSameUtil> token;
 
    @Before public void setup() {
        token = mockStatic(SaSameUtil.class, withSettings().mockMaker(MockMakers.INLINE));
        ApplicationContext context = mock(ApplicationContext.class);
        when(context.getBean(MessageSource.class)).thenReturn(new StaticMessageSource());
        when(context.getBean(I18nProperties.class)).thenReturn(new I18nProperties());
        SpringContext.setApplicationContext(context);
        mvc = MockMvcBuilders.standaloneSetup(controller).build();
        form.setDocumentId(UUID.randomUUID());
        form.setFileVersionId(UUID.randomUUID());
        form.setFileName("main.txt");
        when(attachment.getOriginalFilename()).thenReturn("main.txt");
        form.setFileJson("[{\"url\":\"" + url + "\",\"name\":\"main.txt\"}]");
    }
 
    @After public void closeToken() { token.close(); }
 
    private void readAttachment() {
        when(reader.withFile(eq(url), eq(form.getFileJson()), eq(form.getFileName()), any()))
                .thenAnswer(invocation -> invocation
                        .<Function<MultipartFile, DmsFileUploadRegisterForm>>getArgument(3).apply(attachment));
    }
 
    @Test public void httpImportDelegatesToOriginalStorageAndReturnsOnlyPhysicalFacts() throws Exception {
        readAttachment();
        DmsFileUploadRegisterForm stored = new DmsFileUploadRegisterForm();
        stored.setDocumentId(form.getDocumentId()); stored.setFileVersionId(form.getFileVersionId());
        stored.setFileObjectId(UUID.randomUUID()); stored.setFileName("main.txt");
        stored.setDocumentType("TXT"); stored.setMimeType("text/plain");
        stored.setSizeBytes(12L); stored.setChecksumSha256("a".repeat(64));
        stored.setStoragePlatform("seaweedfs-1"); stored.setBucketName("jnpf-dms");
        stored.setBasePath(""); stored.setObjectPath("2026/09/18/" + form.getDocumentId() + "/");
        stored.setObjectName(stored.getFileObjectId() + ".txt");
        when(storage.storeWorkflowContent(form.getDocumentId(), form.getFileVersionId(), attachment)).thenReturn(stored);
 
        String body = mvc.perform(post("/Internal/file-content/imports")
                        .header("X-DMS-Service-Id", "jnpf-dms").contentType(MediaType.APPLICATION_JSON)
                        .content(json.writeValueAsBytes(form)))
                .andExpect(status().isOk()).andReturn().getResponse().getContentAsString();
        JsonNode response = json.readTree(body);
        assertEquals(200, response.path("code").asInt());
        JsonNode facts = response.path("data");
        assertEquals(stored.getFileObjectId().toString(), facts.path("fileObjectId").asText());
        assertEquals(stored.getObjectPath(), facts.path("objectPath").asText());
        assertEquals(stored.getObjectName(), facts.path("objectName").asText());
        assertEquals(stored.getChecksumSha256(), facts.path("checksumSha256").asText());
        assertEquals(stored.getBucketName(), facts.path("bucketName").asText());
        assertEquals(stored.getStoragePlatform(), facts.path("storagePlatform").asText());
        assertEquals(stored.getBasePath(), facts.path("basePath").asText());
        assertEquals(stored.getFileName(), facts.path("fileName").asText());
        assertEquals(stored.getDocumentType(), facts.path("documentType").asText());
        assertEquals(stored.getMimeType(), facts.path("mimeType").asText());
        assertEquals(12L, facts.path("sizeBytes").asLong());
        assertEquals(11, facts.size());
        assertFalse(facts.has("documentId")); assertFalse(facts.has("fileVersionId"));
        assertFalse(facts.has("sourceId")); assertFalse(facts.has("status"));
        token.verify(SaSameUtil::checkCurrentRequestToken);
        verify(storage).storeWorkflowContent(form.getDocumentId(), form.getFileVersionId(), attachment);
        verifyNoMoreInteractions(storage);
        verify(reader).withFile(eq(url), eq(form.getFileJson()), eq("main.txt"), any());
        verifyNoMoreInteractions(reader);
        verifyNoInteractions(approvalPages, qianpiyeWriter);
    }
 
    @Test public void approvalPageIsRenderedMergedAndStoredBeforeReturningReceipt() {
        form.setFileName("录入的文件名称");
        String pageJson = "[{\"url\":\"/api/file/Image/annex/approval.docx\"}]";
        form.setSignaturePageId("signature-page");
        form.setSignaturePageFiles(pageJson);
        form.setSignaturePageValues(Map.of("dms.qianpiye.flow.approve-1.approver", "审核人"));
        form.setApprovalFlowVersionId("flow-version");
        MultipartFile page = mock(MultipartFile.class);
        MultipartFile rendered = mock(MultipartFile.class);
        MultipartFile merged = mock(MultipartFile.class);
        MultipartFile cleaned = mock(MultipartFile.class);
        when(cleaned.getOriginalFilename()).thenReturn("main.docx");
        form.setRemoveDocxComments(true);
        when(merged.getOriginalFilename()).thenReturn("main.docx");
        when(cleaner.clean(merged)).thenReturn(cleaned);
        when(attachment.getOriginalFilename()).thenReturn("main.docx");
        when(page.getOriginalFilename()).thenReturn("approval.docx");
        when(approvalPages.withoutApprovalPages(attachment)).thenReturn(attachment);
        readAttachment();
        when(reader.withFile(eq("/api/file/Image/annex/approval.docx"), eq(pageJson), eq("签批页"), any()))
                .thenAnswer(invocation -> invocation
                        .<Function<MultipartFile, DmsFileUploadRegisterForm>>getArgument(3).apply(page));
        when(qianpiyeWriter.render(page, form.getSignaturePageValues(),
                form.getApprovalFlowVersionId())).thenReturn(rendered);
        when(qianpiyeWriter.render(attachment, form.getSignaturePageValues(),
                form.getApprovalFlowVersionId())).thenReturn(attachment);
        when(approvalPages.replaceOrPrepend(attachment, rendered)).thenReturn(merged);
        DmsFileUploadRegisterForm stored = new DmsFileUploadRegisterForm();
        stored.setFileObjectId(UUID.randomUUID());
        when(storage.storeWorkflowContent(eq(form.getDocumentId()), eq(form.getFileVersionId()), any()))
                .thenAnswer(invocation -> {
                    MultipartFile actual = invocation.getArgument(2);
                    assertEquals("录入的文件名称.docx", actual.getOriginalFilename());
                    return stored;
                });
 
        assertEquals(stored.getFileObjectId(), service.importContent(form).getFileObjectId());
 
        verify(qianpiyeWriter).render(page, form.getSignaturePageValues(),
                form.getApprovalFlowVersionId());
        verify(qianpiyeWriter).render(attachment, form.getSignaturePageValues(),
                form.getApprovalFlowVersionId());
        verify(approvalPages).replaceOrPrepend(attachment, rendered);
        org.mockito.InOrder order = inOrder(approvalPages, cleaner, storage);
        order.verify(approvalPages).replaceOrPrepend(attachment, rendered);
        order.verify(cleaner).clean(merged);
        order.verify(storage).storeWorkflowContent(eq(form.getDocumentId()), eq(form.getFileVersionId()),
                argThat(file -> "录入的文件名称.docx".equals(file.getOriginalFilename())));
    }
 
    @Test public void cleanupRunsBeforeStorageWithoutApprovalPageAndFailureStopsStorage() {
        form.setFileName("main.docx");
        form.setRemoveDocxComments(true);
        when(attachment.getOriginalFilename()).thenReturn("main.docx");
        readAttachment();
        when(cleaner.clean(attachment)).thenThrow(new DataException("invalid docx"));
        assertThrows(DataException.class, () -> service.importContent(form));
        verifyNoInteractions(storage);
        MultipartFile cleaned = mock(MultipartFile.class);
        when(cleaned.getOriginalFilename()).thenReturn("main.docx");
        doReturn(cleaned).when(cleaner).clean(attachment);
        when(storage.storeWorkflowContent(form.getDocumentId(), form.getFileVersionId(), cleaned))
                .thenReturn(new DmsFileUploadRegisterForm());
        service.importContent(form);
        verify(storage).storeWorkflowContent(form.getDocumentId(), form.getFileVersionId(), cleaned);
    }
 
    @Test public void approvalPageConfigurationDoesNotBlockImageImport() {
        form.setRemoveDocxComments(true);
        form.setFileName("图片测试PIC-SMP100-01000.png");
        String pageJson = "[{\"url\":\"/api/file/Image/annex/approval.docx\"}]";
        form.setSignaturePageId("signature-page");
        form.setSignaturePageFiles(pageJson);
        when(attachment.getOriginalFilename()).thenReturn("图片测试PIC-SMP100-01000.png");
        readAttachment();
        DmsFileUploadRegisterForm stored = new DmsFileUploadRegisterForm();
        stored.setFileObjectId(UUID.randomUUID());
        when(storage.storeWorkflowContent(form.getDocumentId(), form.getFileVersionId(), attachment))
                .thenReturn(stored);
 
        assertEquals(stored.getFileObjectId(), service.importContent(form).getFileObjectId());
 
        verify(reader).withFile(eq(url), eq(form.getFileJson()), eq(form.getFileName()), any());
        verifyNoMoreInteractions(reader);
        verifyNoInteractions(approvalPages, qianpiyeWriter);
        verify(storage).storeWorkflowContent(form.getDocumentId(), form.getFileVersionId(), attachment);
    }
 
    @Test public void formalImportUsesEnteredTitleAndPreservesBytesAndSourceExtension() throws Exception {
        for (String title : new String[]{"设备清洁操作规程", "设备清洁操作规程.DOCX", "设备清洁操作规程.v2"}) {
            form.setFileName(title);
            byte[] bytes = "original content".getBytes(java.nio.charset.StandardCharsets.UTF_8);
            when(attachment.getOriginalFilename()).thenReturn("错误上传名.docx");
            when(attachment.getBytes()).thenReturn(bytes);
            when(attachment.getSize()).thenReturn((long) bytes.length);
            when(attachment.getInputStream()).thenAnswer(i -> new java.io.ByteArrayInputStream(bytes));
            readAttachment();
            when(storage.storeWorkflowContent(eq(form.getDocumentId()), eq(form.getFileVersionId()), any()))
                    .thenAnswer(invocation -> {
                        MultipartFile actual = invocation.getArgument(2);
                        String expected = title.endsWith(".DOCX") ? title : title + ".docx";
                        assertEquals(expected, actual.getOriginalFilename());
                        assertArrayEquals(bytes, actual.getBytes());
                        assertArrayEquals(bytes, actual.getInputStream().readAllBytes());
                        assertEquals(bytes.length, actual.getSize());
                        DmsFileUploadRegisterForm stored = new DmsFileUploadRegisterForm();
                        stored.setFileName(actual.getOriginalFilename());
                        return stored;
                    });
            assertEquals(title.endsWith(".DOCX") ? title : title + ".docx", service.importContent(form).getFileName());
        }
        verifyNoInteractions(approvalPages, qianpiyeWriter, cleaner);
    }
 
    @Test public void httpRequiresBothReservedIdsAndAttachmentBeforeStorage() throws Exception {
        for (String field : new String[]{"documentId", "fileVersionId", "fileJson", "fileName"}) {
            com.fasterxml.jackson.databind.node.ObjectNode request = json.valueToTree(form);
            request.remove(field);
            mvc.perform(post("/Internal/file-content/imports").header("X-DMS-Service-Id", "jnpf-dms")
                            .contentType(MediaType.APPLICATION_JSON).content(json.writeValueAsBytes(request)))
                    .andExpect(status().isBadRequest());
        }
        mvc.perform(post("/Internal/file-content/imports").contentType(MediaType.APPLICATION_JSON)
                        .content(json.writeValueAsBytes(form))).andExpect(status().isBadRequest());
        verifyNoInteractions(reader, storage);
    }
 
    @Test public void invalidServiceCannotReadOrCopyAttachments() {
        assertThrows(DataException.class, () -> controller.importContent("other-service", form));
        token.verify(SaSameUtil::checkCurrentRequestToken);
        verifyNoInteractions(reader, storage);
    }
 
    @Test public void invalidInternalTokenStopsBeforeStorage() {
        IllegalStateException failure = new IllegalStateException("invalid internal token");
        token.when(SaSameUtil::checkCurrentRequestToken).thenThrow(failure);
        assertSame(failure, assertThrows(IllegalStateException.class,
                () -> controller.importContent("jnpf-dms", form)));
        verifyNoInteractions(reader, storage);
    }
 
    @Test public void rejectsMultipleOrMalformedAttachmentsBeforeReading() {
        for (String value : new String[]{"[]", "[{\"url\":\"a\"},{\"url\":\"b\"}]",
                "{}", "null", "not-json", "{\"url\":1}", "{\"url\":\" \"}"}) {
            form.setFileJson(value);
            assertThrows(DataException.class, () -> service.importContent(form));
        }
        verifyNoInteractions(reader, storage);
    }
 
    @Test public void originalStorageFailureIsNotConvertedIntoASuccessReceipt() {
        readAttachment();
        DataException failure = new DataException("SeaweedFS 上传失败或未生成 SHA-256");
        when(storage.storeWorkflowContent(form.getDocumentId(), form.getFileVersionId(), attachment)).thenThrow(failure);
        assertSame(failure, assertThrows(DataException.class, () -> service.importContent(form)));
        verify(storage).storeWorkflowContent(form.getDocumentId(), form.getFileVersionId(), attachment);
        verifyNoMoreInteractions(storage);
    }
 
    @Test public void successfulImportReturnsPhysicalObjectWithoutTechnicalAudit() {
        readAttachment();
        DmsFileUploadRegisterForm stored = new DmsFileUploadRegisterForm();
        stored.setFileObjectId(UUID.randomUUID());
        when(storage.storeWorkflowContent(form.getDocumentId(), form.getFileVersionId(), attachment))
                .thenReturn(stored);
        AuditClient audit = mock(AuditClient.class);
        AspectJProxyFactory factory = new AspectJProxyFactory(service);
        factory.addAspect(new AuditLogAspect(audit, mock(AuditEventPublisher.class),
                mock(ApplicationContext.class)));
        FileContentImportService audited = factory.getProxy();
 
        assertEquals(stored.getFileObjectId(), audited.importContent(form).getFileObjectId());
 
        verifyNoInteractions(audit);
    }
}