云
4 天以前 f02d0df2f1dfc7a8b775cf50d45375e57628a5ef
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
package cn.iocoder.yudao.module.mes.service.pd.seal;
 
import cn.iocoder.yudao.framework.common.exception.ServiceException;
import cn.iocoder.yudao.module.bpm.service.task.BpmTaskService;
import org.flowable.task.api.history.HistoricTaskInstance;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.jdbc.core.JdbcTemplate;
import java.io.*;
import java.util.*;
import java.util.zip.*;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.*;
 
@ExtendWith(MockitoExtension.class)
class PdSealServiceTest {
    @InjectMocks PdSealService service;
    @Mock JdbcTemplate jdbc;
    @Mock BpmTaskService tasks;
    @Test void imageLookupIsScopedToCurrentTenant() {
        cn.iocoder.yudao.framework.tenant.core.context.TenantContextHolder.setTenantId(42L);
        try {
            byte[] image = {1, 2};
            when(jdbc.queryForList(anyString(), any(Object[].class)))
                    .thenReturn(List.of(Map.of("image_data", image)));
            assertArrayEquals(image, service.getImage(7));
            verify(jdbc).queryForList(contains("tenant_id=? AND deleted=0"), eq(7L), eq(42L));
        } finally {
            cn.iocoder.yudao.framework.tenant.core.context.TenantContextHolder.clear();
        }
    }
    @Test void refusesImageWhenSealIsNotFound() {
        when(jdbc.queryForList(anyString(), any(Object[].class))).thenReturn(List.of());
        assertThrows(ServiceException.class, () -> service.getImage(7));
    }
    @Test void refusesDownloadWithoutApprovedApplication() {
        when(jdbc.queryForList(anyString(), any(Object[].class))).thenReturn(List.of());
        assertThrows(ServiceException.class, () -> service.download(1, 7));
        verifyNoInteractions(tasks);
    }
    @Test void refusesAutomaticApproval() throws Exception {
        when(jdbc.queryForList(anyString(), any(Object[].class))).thenReturn(List.of(Map.of("process_instance_id", "p", "document_snapshot", new byte[]{1}, "seal_image", new byte[]{2})));
        HistoricTaskInstance task=mock(HistoricTaskInstance.class);
        when(task.getAssignee()).thenReturn("8");
        when(task.getTaskLocalVariables()).thenReturn(Map.of("TASK_STATUS",2,"TASK_REASON","流程已配置免审批,提交后自动通过"));
        when(tasks.getTaskListByProcessInstanceId("p",true)).thenReturn(List.of(task));
        assertThrows(ServiceException.class, () -> service.download(1,7));
    }
    @Test void allowsManualApprovalByApplicant() throws Exception {
        var buffer = new ByteArrayOutputStream();
        try (var zip = new ZipOutputStream(buffer)) {
            zip.putNextEntry(new ZipEntry("word/media/seal.png"));
            zip.write(new byte[]{1});
            zip.closeEntry();
        }
        when(jdbc.queryForList(anyString(), any(Object[].class))).thenReturn(List.of(Map.of(
                "process_instance_id", "p", "document_snapshot", buffer.toByteArray(), "seal_image", new byte[]{2})));
        HistoricTaskInstance task = mock(HistoricTaskInstance.class);
        when(task.getAssignee()).thenReturn("7");
        when(task.getTaskLocalVariables()).thenReturn(Map.of("TASK_STATUS", 2, "TASK_REASON", ""));
        when(tasks.getTaskListByProcessInstanceId("p", true)).thenReturn(List.of(task));
        assertNotNull(service.download(1, 7));
    }
    @Test void approvedDownloadEmbedsSelectedImage() throws Exception {
        var buffer=new ByteArrayOutputStream();
        try(var zip=new ZipOutputStream(buffer)){zip.putNextEntry(new ZipEntry("word/media/seal.png"));zip.write(new byte[]{1});zip.closeEntry();}
        when(jdbc.queryForList(anyString(), any(Object[].class))).thenReturn(List.of(Map.of("process_instance_id","p","document_snapshot",buffer.toByteArray(),"seal_image",new byte[]{2,3})));
        HistoricTaskInstance task=mock(HistoricTaskInstance.class);
        when(task.getAssignee()).thenReturn("8");
        when(task.getTaskLocalVariables()).thenReturn(Map.of("TASK_STATUS",2,"TASK_REASON","同意"));
        when(tasks.getTaskListByProcessInstanceId("p",true)).thenReturn(List.of(task));
        byte[] result=service.download(1,7);
        try(var zip=new ZipInputStream(new ByteArrayInputStream(result))){assertEquals("word/media/seal.png",zip.getNextEntry().getName());assertArrayEquals(new byte[]{2,3},zip.readAllBytes());}
    }
}