刘光辉
2 小时以前 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
package jnpf.dmsService;
 
import jnpf.base.ActionResult;
import jnpf.dms.DmsResourceApi;
import jnpf.dmsEntity.DmsWenjianBanbenEntity;
import jnpf.dmsEntity.DmsWenjianChayueShenqingEntity;
import jnpf.dmsEntity.DmsWenjianChayueShenqingZixiangEntity;
import jnpf.dmsEntity.permission.form.DmsPersonalFolderBorrowForm;
import jnpf.dmsEntity.permission.vo.DmsPersonalFolderAddResultVO;
import jnpf.dmsMapper.DmsWenjianBanbenMapper;
import jnpf.dmsMapper.DmsWenjianChayueShenqingMapper;
import jnpf.dmsMapper.DmsWenjianChayueShenqingZixiangMapper;
import jnpf.exception.DataException;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;
 
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Arrays;
import java.util.Collections;
import java.util.UUID;
 
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
 
class DmsWenjianFuzhuGuanliChayueServiceTest {
 
    private static final ZoneId BUSINESS_ZONE = ZoneId.of("Asia/Shanghai");
    private static final UUID DOCUMENT_ID =
            UUID.fromString("018f0000-0000-7000-8000-000000000801");
    private static final UUID FILE_VERSION_ID =
            UUID.fromString("018f0000-0000-7000-8000-000000000802");
 
    @Test
    void authorizesApplicationVersionsForTheApplicant() {
        Fixture fixture = new Fixture();
        when(fixture.shenqingMapper.selectList(any()))
                .thenReturn(Collections.singletonList(application()));
        when(fixture.zixiangMapper.selectList(any()))
                .thenReturn(Arrays.asList(detail("version-1"), detail("version-1")));
        when(fixture.banbenMapper.selectList(any()))
                .thenReturn(Collections.singletonList(banben("version-1")));
        DmsPersonalFolderAddResultVO expected = new DmsPersonalFolderAddResultVO(1, 1);
        when(fixture.dmsResourceApi.addBorrowedFilesToPersonalFolder(
                eq("jnpf-flowable"), any())).thenReturn(ActionResult.success("success", expected));
 
        DmsPersonalFolderAddResultVO result = fixture.service.chayueWenjianShouquan(
                " flow-1 ", " task-1 ");
 
        assertSame(expected, result);
        ArgumentCaptor<DmsPersonalFolderBorrowForm> formCaptor =
                ArgumentCaptor.forClass(DmsPersonalFolderBorrowForm.class);
        verify(fixture.dmsResourceApi).addBorrowedFilesToPersonalFolder(
                eq("jnpf-flowable"), formCaptor.capture());
        DmsPersonalFolderBorrowForm form = formCaptor.getValue();
        assertEquals("applicant-1", form.getUserId());
        assertEquals(1, form.getFiles().size());
        assertEquals(DOCUMENT_ID, form.getFiles().get(0).getDocumentId());
        assertEquals(FILE_VERSION_ID, form.getFiles().get(0).getFileVersionId());
        LocalDateTime validTo = form.getFiles().get(0).getValidTo().toInstant()
                .atZone(BUSINESS_ZONE).toLocalDateTime();
        assertEquals(LocalDate.now(BUSINESS_ZONE).plusDays(2), validTo.toLocalDate());
        assertEquals(23, validTo.getHour());
        assertEquals(59, validTo.getMinute());
        assertEquals(59, validTo.getSecond());
        assertEquals(0, validTo.getNano());
    }
 
    @Test
    void rejectsPartialBusinessVersionMatchesBeforeCallingTheAuthorizationApi() {
        Fixture fixture = new Fixture();
        when(fixture.shenqingMapper.selectList(any()))
                .thenReturn(Collections.singletonList(application()));
        when(fixture.zixiangMapper.selectList(any()))
                .thenReturn(Arrays.asList(detail("version-1"), detail("version-2")));
        when(fixture.banbenMapper.selectList(any()))
                .thenReturn(Collections.singletonList(banben("version-1")));
 
        assertThrows(DataException.class,
                () -> fixture.service.chayueWenjianShouquan("flow-1", "task-1"));
 
        verify(fixture.dmsResourceApi, never())
                .addBorrowedFilesToPersonalFolder(any(), any());
    }
 
    @Test
    void rejectsNonNumericChayueQixianBeforeCallingTheAuthorizationApi() {
        Fixture fixture = new Fixture();
        when(fixture.shenqingMapper.selectList(any()))
                .thenReturn(Collections.singletonList(application()));
        when(fixture.zixiangMapper.selectList(any()))
                .thenReturn(Collections.singletonList(detail("version-1", "invalid")));
 
        assertThrows(DataException.class,
                () -> fixture.service.chayueWenjianShouquan("flow-1", "task-1"));
 
        verify(fixture.banbenMapper, never()).selectList(any());
        verify(fixture.dmsResourceApi, never())
                .addBorrowedFilesToPersonalFolder(any(), any());
    }
 
    private static DmsWenjianChayueShenqingEntity application() {
        DmsWenjianChayueShenqingEntity entity = new DmsWenjianChayueShenqingEntity();
        entity.setId("application-1");
        entity.setShenqingren(" applicant-1 ");
        return entity;
    }
 
    private static DmsWenjianChayueShenqingZixiangEntity detail(String banbenId) {
        return detail(banbenId, "2");
    }
 
    private static DmsWenjianChayueShenqingZixiangEntity detail(
            String banbenId, String chayueQixian) {
        DmsWenjianChayueShenqingZixiangEntity entity =
                new DmsWenjianChayueShenqingZixiangEntity();
        entity.setBanbenId(banbenId);
        entity.setChayueQixian(chayueQixian);
        return entity;
    }
 
    private static DmsWenjianBanbenEntity banben(String banbenId) {
        DmsWenjianBanbenEntity entity = new DmsWenjianBanbenEntity();
        entity.setId(banbenId);
        entity.setTenantId("tenant-1");
        entity.setWenjianZhongxinWenjianId(DOCUMENT_ID.toString());
        entity.setWenjianZhongxinBanbenId(FILE_VERSION_ID.toString());
        return entity;
    }
 
    private static class Fixture {
        private final DmsWenjianChayueShenqingMapper shenqingMapper =
                mock(DmsWenjianChayueShenqingMapper.class);
        private final DmsWenjianChayueShenqingZixiangMapper zixiangMapper =
                mock(DmsWenjianChayueShenqingZixiangMapper.class);
        private final DmsWenjianBanbenMapper banbenMapper = mock(DmsWenjianBanbenMapper.class);
        private final DmsResourceApi dmsResourceApi = mock(DmsResourceApi.class);
        private final DmsWenjianFuzhuGuanliChayueService service =
                new DmsWenjianFuzhuGuanliChayueService(
                        shenqingMapper, zixiangMapper, banbenMapper, dmsResourceApi);
    }
}