刘光辉
16 小时以前 34981c30a78e8bbd7791131059a9210f9928b62c
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
package jnpf.dmsService;
 
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
import jnpf.dmsEntity.DmsDanganCundangShenqingEntity;
import jnpf.dmsEntity.DmsDanganCundangShenqingDanganheEntity;
import jnpf.dmsEntity.form.DmsDanganCundangJieshouDanganheForm;
import jnpf.dmsEntity.form.DmsDanganCundangJieshouForm;
import jnpf.dmsMapper.DmsDanganCundangShenqingDanganheMapper;
import jnpf.dmsMapper.DmsDanganCundangShenqingMapper;
import jnpf.dmsService.impl.DmsDanganCundangJieshouServiceImpl;
import jnpf.exception.DataException;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.BeforeEach;
import org.mockito.ArgumentCaptor;
import org.apache.ibatis.builder.MapperBuilderAssistant;
import org.apache.ibatis.session.Configuration;
 
import java.util.Arrays;
import java.util.Date;
 
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 DmsDanganCundangJieshouServiceTest {
 
    @BeforeEach
    void setUp() {
        if (TableInfoHelper.getTableInfo(DmsDanganCundangShenqingDanganheEntity.class) == null) {
            TableInfoHelper.initTableInfo(new MapperBuilderAssistant(new Configuration(), ""),
                    DmsDanganCundangShenqingDanganheEntity.class);
        }
        if (TableInfoHelper.getTableInfo(DmsDanganCundangShenqingEntity.class) == null) {
            TableInfoHelper.initTableInfo(new MapperBuilderAssistant(new Configuration(), ""),
                    DmsDanganCundangShenqingEntity.class);
        }
    }
 
    @Test
    void receivesEveryRequestedDanganheInOneConditionalUpdate() throws Exception {
        DmsDanganCundangShenqingDanganheMapper mapper = mock(DmsDanganCundangShenqingDanganheMapper.class);
        when(mapper.selectList(any())).thenReturn(Arrays.asList(danganhe("box-1", null), danganhe("box-2", "")));
        when(mapper.update(isNull(), any())).thenReturn(2);
        DmsDanganCundangJieshouService service = service(mapper);
 
        service.jieshou(form(item("box-1", "box-1"), item("box-2", "box-2")));
 
        ArgumentCaptor<Wrapper<DmsDanganCundangShenqingDanganheEntity>> 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("yijiao_ren"));
        assertTrue(sqlSet.contains("biz_sign"));
        assertTrue(sqlSet.contains("biz_review_sign"));
        assertTrue(sqlSet.contains("jieshou_riqi"));
        assertTrue(update.getParamNameValuePairs().containsValue("receiver-1"));
        assertTrue(update.getParamNameValuePairs().containsValue("reviewer-1"));
        assertTrue(update.getParamNameValuePairs().containsValue("sign-1"));
        assertTrue(update.getParamNameValuePairs().containsValue("review-sign-1"));
        assertNotNull(update.getParamNameValuePairs().values().stream()
                .filter(Date.class::isInstance)
                .findFirst()
                .orElse(null));
    }
 
    @Test
    void rejectsWhenAnyDanganheHasAlreadyBeenReceived() {
        DmsDanganCundangShenqingDanganheMapper mapper = mock(DmsDanganCundangShenqingDanganheMapper.class);
        when(mapper.selectList(any())).thenReturn(Arrays.asList(danganhe("box-1", null), danganhe("box-2", "receiver-0")));
        DmsDanganCundangJieshouService service = service(mapper);
 
        DataException error = assertThrows(DataException.class,
                () -> service.jieshou(form(item("box-1", "box-1"), item("box-2", "box-2"))));
 
        assertEquals("档案盒已接收,不能重复接收", error.getMessage());
        verify(mapper, never()).update(any(), any());
    }
 
    @Test
    void rollsBackWhenAnotherRequestReceivesARecordBeforeTheUpdate() {
        DmsDanganCundangShenqingDanganheMapper mapper = mock(DmsDanganCundangShenqingDanganheMapper.class);
        when(mapper.selectList(any())).thenReturn(Arrays.asList(danganhe("box-1", null), danganhe("box-2", null)));
        when(mapper.update(isNull(), any())).thenReturn(1);
        DmsDanganCundangJieshouService service = service(mapper);
 
        DataException error = assertThrows(DataException.class,
                () -> service.jieshou(form(item("box-1", "box-1"), item("box-2", "box-2"))));
 
        assertEquals("档案盒已接收,不能重复接收", error.getMessage());
    }
 
    @Test
    void checkSucceedsForAnApprovedUnreceivedDanganhe() throws Exception {
        DmsDanganCundangShenqingDanganheMapper danganheMapper = mock(DmsDanganCundangShenqingDanganheMapper.class);
        DmsDanganCundangShenqingMapper shenqingMapper = mock(DmsDanganCundangShenqingMapper.class);
        when(danganheMapper.selectList(any())).thenReturn(Arrays.asList(danganhe("box-1", null, "shenqing-1")));
        when(shenqingMapper.selectOne(any())).thenReturn(shenqing("shenqing-1", 2));
        DmsDanganCundangJieshouService service = new DmsDanganCundangJieshouServiceImpl(
                danganheMapper, shenqingMapper);
 
        DmsDanganCundangShenqingDanganheEntity result = service.check("box-1");
 
        assertEquals("box-1", result.getId());
        verify(danganheMapper).selectList(any());
        verify(shenqingMapper).selectOne(any());
    }
 
    @Test
    void checkRejectsUnknownDanganheBeforeCheckingApplication() {
        DmsDanganCundangShenqingDanganheMapper danganheMapper = mock(DmsDanganCundangShenqingDanganheMapper.class);
        DmsDanganCundangShenqingMapper shenqingMapper = mock(DmsDanganCundangShenqingMapper.class);
        when(danganheMapper.selectList(any())).thenReturn(Arrays.asList());
        DmsDanganCundangJieshouService service = new DmsDanganCundangJieshouServiceImpl(
                danganheMapper, shenqingMapper);
 
        DataException error = assertThrows(DataException.class, () -> service.check("missing-box"));
 
        assertEquals("未找到对应的档案盒记录", error.getMessage());
        verify(shenqingMapper, never()).selectOne(any());
    }
 
    @Test
    void checkRejectsApplicationThatHasNotPassed() {
        DmsDanganCundangShenqingDanganheMapper danganheMapper = mock(DmsDanganCundangShenqingDanganheMapper.class);
        DmsDanganCundangShenqingMapper shenqingMapper = mock(DmsDanganCundangShenqingMapper.class);
        when(danganheMapper.selectList(any())).thenReturn(Arrays.asList(danganhe("box-1", null, "shenqing-1")));
        when(shenqingMapper.selectOne(any())).thenReturn(shenqing("shenqing-1", 1));
        DmsDanganCundangJieshouService service = new DmsDanganCundangJieshouServiceImpl(
                danganheMapper, shenqingMapper);
 
        DataException error = assertThrows(DataException.class, () -> service.check("box-1"));
 
        assertEquals("档案存档申请尚未审批通过,无法接收", error.getMessage());
    }
 
    @Test
    void checkRejectsDanganheThatHasAlreadyBeenReceived() {
        DmsDanganCundangShenqingDanganheMapper danganheMapper = mock(DmsDanganCundangShenqingDanganheMapper.class);
        DmsDanganCundangShenqingMapper shenqingMapper = mock(DmsDanganCundangShenqingMapper.class);
        when(danganheMapper.selectList(any())).thenReturn(Arrays.asList(danganhe("box-1", "receiver-0", "shenqing-1")));
        when(shenqingMapper.selectOne(any())).thenReturn(shenqing("shenqing-1", 2));
        DmsDanganCundangJieshouService service = new DmsDanganCundangJieshouServiceImpl(
                danganheMapper, shenqingMapper);
 
        DataException error = assertThrows(DataException.class, () -> service.check("box-1"));
 
        assertEquals("档案盒已接收,不能重复接收", error.getMessage());
    }
 
    private DmsDanganCundangJieshouService service(DmsDanganCundangShenqingDanganheMapper danganheMapper) {
        return new DmsDanganCundangJieshouServiceImpl(danganheMapper,
                mock(DmsDanganCundangShenqingMapper.class));
    }
 
    private DmsDanganCundangJieshouForm form(DmsDanganCundangJieshouDanganheForm... danganhe) {
        DmsDanganCundangJieshouForm form = new DmsDanganCundangJieshouForm();
        form.setDanganheXinxi(Arrays.asList(danganhe));
        form.setBizUserId("receiver-1");
        form.setBizReviewUser("reviewer-1");
        form.setBizSign("sign-1");
        form.setBizReviewSign("review-sign-1");
        return form;
    }
 
    private DmsDanganCundangJieshouDanganheForm item(String id, String fId) {
        DmsDanganCundangJieshouDanganheForm item = new DmsDanganCundangJieshouDanganheForm();
        item.setId(id);
        item.setFId(fId);
        return item;
    }
 
    private DmsDanganCundangShenqingDanganheEntity danganhe(String id, String jieshouRen) {
        return danganhe(id, jieshouRen, null);
    }
 
    private DmsDanganCundangShenqingDanganheEntity danganhe(String id, String jieshouRen, String foreignId) {
        DmsDanganCundangShenqingDanganheEntity result = new DmsDanganCundangShenqingDanganheEntity();
        result.setId(id);
        result.setJieshouRen(jieshouRen);
        result.setForeignId(foreignId);
        result.setJieshouRiqi(new Date());
        return result;
    }
 
    private DmsDanganCundangShenqingEntity shenqing(String id, Integer flowState) {
        DmsDanganCundangShenqingEntity result = new DmsDanganCundangShenqingEntity();
        result.setId(id);
        result.setFlowState(flowState);
        return result;
    }
}