刘光辉
5 小时以前 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
package jnpf.dmsService.search;
 
import jnpf.dmsEntity.search.model.DmsSearchIndexSource;
import jnpf.dmsMapper.DmsSearchIndexSourceMapper;
import jnpf.dmsService.search.extract.DmsTextExtractionResult;
import jnpf.dmsService.search.extract.DmsTextExtractionService;
import jnpf.dmsService.search.file.DmsSearchFileSourceService;
import jnpf.dmsService.search.file.DmsTemporarySearchFile;
import jnpf.dmsService.search.meili.DmsMeilisearchClient;
import jnpf.dmsService.search.meili.DmsSearchIndexDocument;
import jnpf.dmsService.search.ocr.DmsOcrAdapterRegistry;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;
 
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Optional;
import java.util.UUID;
 
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
 
class DmsSearchIndexProcessorTest {
 
    @Test
    void extractsUploadedFileAndWritesMeilisearchDocument() throws Exception {
        DmsSearchIndexSourceMapper sourceMapper = mock(DmsSearchIndexSourceMapper.class);
        DmsSearchFileSourceService fileSource = mock(DmsSearchFileSourceService.class);
        DmsTextExtractionService extraction = mock(DmsTextExtractionService.class);
        DmsOcrAdapterRegistry ocr = mock(DmsOcrAdapterRegistry.class);
        DmsSearchIndexWriter meilisearch = mock(DmsSearchIndexWriter.class);
        DmsSearchIndexProcessor processor = new DmsSearchIndexProcessor(sourceMapper, fileSource,
                extraction, ocr, meilisearch);
        DmsSearchIndexTask task = task();
        DmsSearchIndexSource source = source(task);
        Path temporary = Files.createTempFile("dms-search-test-", ".docx");
 
        when(sourceMapper.selectSource(task.getTenantId(), task.getDocumentId(),
                task.getFileVersionId())).thenReturn(source);
        when(fileSource.fetch(task)).thenReturn(new DmsTemporarySearchFile(
                temporary, "quality.docx", "application/docx"));
        when(extraction.extract(temporary, "quality.docx"))
                .thenReturn(new DmsTextExtractionResult("检验操作规程", false));
        when(ocr.configuredAdapter()).thenReturn(Optional.empty());
 
        processor.process(task);
 
        ArgumentCaptor<DmsSearchIndexDocument> captor =
                ArgumentCaptor.forClass(DmsSearchIndexDocument.class);
        verify(meilisearch).publish(captor.capture());
        assertEquals("检验操作规程", captor.getValue().getContent());
        assertEquals(task.getFileVersionId().toString(), captor.getValue().getId());
    }
 
    private DmsSearchIndexTask task() {
        DmsSearchIndexTask task = new DmsSearchIndexTask();
        task.setTenantId("tenant-1");
        task.setDocumentId(UUID.randomUUID());
        task.setFileVersionId(UUID.randomUUID());
        task.setChecksumSha256("a".repeat(64));
        return task;
    }
 
    private DmsSearchIndexSource source(DmsSearchIndexTask task) {
        DmsSearchIndexSource source = new DmsSearchIndexSource();
        source.setTenantId(task.getTenantId());
        source.setDocumentId(task.getDocumentId());
        source.setFileVersionId(task.getFileVersionId());
        source.setDocumentName("检验操作规程");
        source.setFileName("quality.docx");
        return source;
    }
}