刘光辉
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
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
package jnpf.dmsService;
 
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
import jnpf.audit.sdk.annotation.AuditLog;
import jnpf.base.UserInfo;
import jnpf.dmsEntity.DmsDanganJieyueFuyinShenqingZixiangEntity;
import jnpf.dmsEntity.form.DmsDanganJieyueFuyinJieshouForm;
import jnpf.dmsEntity.vo.DmsDanganJieyueFuyinJieshouCheckVO;
import jnpf.dmsMapper.DmsDanganJieyueFuyinShenqingZixiangMapper;
import jnpf.dmsPermission.DmsCurrentIdentity;
import jnpf.dmsService.impl.DmsDanganJieyueFuyinJieshouServiceImpl;
import jnpf.exception.DataException;
import org.apache.ibatis.builder.MapperBuilderAssistant;
import org.apache.ibatis.session.Configuration;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;
 
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
 
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.isNull;
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 DmsDanganJieyueFuyinJieshouServiceTest {
 
    @BeforeEach
    void setUp() {
        if (TableInfoHelper.getTableInfo(DmsDanganJieyueFuyinShenqingZixiangEntity.class) == null) {
            TableInfoHelper.initTableInfo(new MapperBuilderAssistant(new Configuration(), ""),
                    DmsDanganJieyueFuyinShenqingZixiangEntity.class);
        }
    }
 
    @Test
    void declaresTheBatchReceiveAuditEvent() throws NoSuchMethodException {
        Method method = DmsDanganJieyueFuyinJieshouServiceImpl.class
                .getMethod("jieshou", List.class);
        AuditLog auditLog = method.getAnnotation(AuditLog.class);
 
        assertNotNull(auditLog);
        assertEquals("LOAN_COPY_RECEIVE", auditLog.action());
        assertEquals("档案借阅复印接收", auditLog.label());
        assertEquals("DMS_LOAN_COPY", auditLog.bizType());
        assertEquals("档案借阅/复印接收", auditLog.bizModule());
        assertEquals("", auditLog.bizCodeExpr());
        assertTrue(!auditLog.bizCodeRequired());
        assertEquals("dms_dangan_jieyue_fuyin_shenqing_zixiang", auditLog.targetTable());
        assertEquals("#p0[0].fId", auditLog.targetIdExpr());
        assertEquals("#p0.![fId]", auditLog.targetIdsExpr());
        assertTrue(auditLog.diff());
        assertEquals(DmsDanganJieyueFuyinShenqingZixiangEntity.class, auditLog.entityClass());
    }
 
    @Test
    void receivesEveryRequestedDetailWithTheCurrentUserAndDepartment() throws Exception {
        DmsDanganJieyueFuyinShenqingZixiangMapper mapper = mock(DmsDanganJieyueFuyinShenqingZixiangMapper.class);
        DmsCurrentIdentity currentIdentity = mock(DmsCurrentIdentity.class);
        when(currentIdentity.requireUser()).thenReturn(user("user-1", "org-1"));
        when(mapper.selectList(any())).thenReturn(Arrays.asList(detail("detail-1", null), detail("detail-2", "")));
        when(mapper.update(isNull(), any())).thenReturn(2);
 
        service(mapper, currentIdentity).jieshou(Arrays.asList(item("detail-1"), item("detail-2")));
 
        ArgumentCaptor<Wrapper<DmsDanganJieyueFuyinShenqingZixiangEntity>> captor = ArgumentCaptor.forClass(Wrapper.class);
        verify(mapper).update(isNull(), captor.capture());
        com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper<?> update =
                (com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper<?>) captor.getValue();
        String sqlSet = update.getSqlSet();
        assertTrue(sqlSet.contains("jieshou_ren"));
        assertTrue(sqlSet.contains("jieshou_riqi"));
        assertTrue(sqlSet.contains("jieshou_bumen"));
        assertTrue(update.getParamNameValuePairs().containsValue("user-1"));
        assertTrue(update.getParamNameValuePairs().containsValue("org-1"));
        assertNotNull(update.getParamNameValuePairs().values().stream()
                .filter(Date.class::isInstance)
                .findFirst()
                .orElse(null));
    }
 
    @Test
    void rejectsMismatchedIdsBeforeUpdating() {
        DmsDanganJieyueFuyinShenqingZixiangMapper mapper = mock(DmsDanganJieyueFuyinShenqingZixiangMapper.class);
        DmsCurrentIdentity currentIdentity = mock(DmsCurrentIdentity.class);
        DmsDanganJieyueFuyinJieshouForm form = item("detail-1");
        form.setId("another-detail");
 
        DataException error = assertThrows(DataException.class,
                () -> service(mapper, currentIdentity).jieshou(Arrays.asList(form)));
 
        assertEquals("档案借阅复印子项id与f_id不一致", error.getMessage());
        verify(mapper, never()).update(any(), any());
        verify(currentIdentity, never()).requireUser();
    }
 
    @Test
    void rejectsDetailsThatHaveAlreadyBeenReceived() {
        DmsDanganJieyueFuyinShenqingZixiangMapper mapper = mock(DmsDanganJieyueFuyinShenqingZixiangMapper.class);
        DmsCurrentIdentity currentIdentity = mock(DmsCurrentIdentity.class);
        when(mapper.selectList(any())).thenReturn(Arrays.asList(detail("detail-1", "user-0")));
 
        DataException error = assertThrows(DataException.class,
                () -> service(mapper, currentIdentity).jieshou(Arrays.asList(item("detail-1"))));
 
        assertEquals("档案借阅复印子项已接收,不能重复接收", error.getMessage());
        verify(mapper, never()).update(any(), any());
        verify(currentIdentity, never()).requireUser();
    }
 
    @Test
    void receivesDetailsWhenTheCurrentUserHasNoDepartment() throws Exception {
        DmsDanganJieyueFuyinShenqingZixiangMapper mapper = mock(DmsDanganJieyueFuyinShenqingZixiangMapper.class);
        DmsCurrentIdentity currentIdentity = mock(DmsCurrentIdentity.class);
        when(currentIdentity.requireUser()).thenReturn(user("user-1", " "));
        when(mapper.selectList(any())).thenReturn(Arrays.asList(detail("detail-1", null)));
        when(mapper.update(isNull(), any())).thenReturn(1);
 
        service(mapper, currentIdentity).jieshou(Arrays.asList(item("detail-1")));
 
        ArgumentCaptor<Wrapper<DmsDanganJieyueFuyinShenqingZixiangEntity>> captor = ArgumentCaptor.forClass(Wrapper.class);
        verify(mapper).update(isNull(), captor.capture());
        com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper<?> update =
                (com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper<?>) captor.getValue();
        assertTrue(update.getParamNameValuePairs().containsValue(null));
    }
 
    @Test
    void usesRequestedReceiverAndDepartmentWhenTheyAreProvided() throws Exception {
        DmsDanganJieyueFuyinShenqingZixiangMapper mapper = mock(DmsDanganJieyueFuyinShenqingZixiangMapper.class);
        DmsCurrentIdentity currentIdentity = mock(DmsCurrentIdentity.class);
        when(currentIdentity.requireUser()).thenReturn(user("current-user", "current-department"));
        when(mapper.selectList(any())).thenReturn(Arrays.asList(detail("detail-1", null)));
        when(mapper.update(isNull(), any())).thenReturn(1);
        DmsDanganJieyueFuyinJieshouForm form = item("detail-1");
        form.setJieshouRen("requested-user");
        form.setJieshouBumen("requested-department");
 
        service(mapper, currentIdentity).jieshou(Arrays.asList(form));
 
        ArgumentCaptor<Wrapper<DmsDanganJieyueFuyinShenqingZixiangEntity>> captor = ArgumentCaptor.forClass(Wrapper.class);
        verify(mapper).update(isNull(), captor.capture());
        com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper<?> update =
                (com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper<?>) captor.getValue();
        assertTrue(update.getParamNameValuePairs().containsValue("requested-user"));
        assertTrue(update.getParamNameValuePairs().containsValue("requested-department"));
    }
 
    @Test
    void returnsTheAvailableDetailForABoxNumberOrId() throws Exception {
        DmsDanganJieyueFuyinShenqingZixiangMapper mapper = mock(DmsDanganJieyueFuyinShenqingZixiangMapper.class);
        DmsCurrentIdentity currentIdentity = mock(DmsCurrentIdentity.class);
        DmsDanganJieyueFuyinShenqingZixiangEntity detail = detail("detail-1", null);
        detail.setDanganheId("box-1");
        detail.setDanganheBianhao("DAH-001");
        detail.setDanganheMingcheng("档案盒一");
        detail.setShenqingRen("applicant-1");
        detail.setShenqingBumen("department-1");
        detail.setShifouYitongguo("yes");
        when(mapper.selectList(any())).thenReturn(Arrays.asList(detail));
 
        DmsDanganJieyueFuyinJieshouCheckVO result = service(mapper, currentIdentity).check("DAH-001");
 
        assertEquals("detail-1", result.getId());
        assertEquals("box-1", result.getDanganheId());
        assertEquals("DAH-001", result.getDanganheBianhao());
        assertEquals("档案盒一", result.getDanganheMingcheng());
        assertEquals("applicant-1", result.getShenqingRen());
        assertEquals("department-1", result.getShenqingBumen());
    }
 
    @Test
    void rejectsWhenNoAvailableDetailExistsForTheBox() {
        DmsDanganJieyueFuyinShenqingZixiangMapper mapper = mock(DmsDanganJieyueFuyinShenqingZixiangMapper.class);
        DmsCurrentIdentity currentIdentity = mock(DmsCurrentIdentity.class);
        when(mapper.selectList(any())).thenReturn(Arrays.asList());
 
        DataException error = assertThrows(DataException.class,
                () -> service(mapper, currentIdentity).check("DAH-001"));
 
        assertEquals("未找到可接收的档案借阅复印子项", error.getMessage());
    }
 
    private DmsDanganJieyueFuyinJieshouService service(
            DmsDanganJieyueFuyinShenqingZixiangMapper mapper, DmsCurrentIdentity currentIdentity) {
        return new DmsDanganJieyueFuyinJieshouServiceImpl(mapper, currentIdentity);
    }
 
    private DmsDanganJieyueFuyinJieshouForm item(String id) {
        DmsDanganJieyueFuyinJieshouForm form = new DmsDanganJieyueFuyinJieshouForm();
        form.setId(id);
        form.setFId(id);
        return form;
    }
 
    private DmsDanganJieyueFuyinShenqingZixiangEntity detail(String id, String jieshouRen) {
        DmsDanganJieyueFuyinShenqingZixiangEntity detail = new DmsDanganJieyueFuyinShenqingZixiangEntity();
        detail.setId(id);
        detail.setJieshouRen(jieshouRen);
        return detail;
    }
 
    private UserInfo user(String userId, String organizeId) {
        UserInfo user = new UserInfo();
        user.setUserId(userId);
        user.setOrganizeId(organizeId);
        return user;
    }
}