刘光辉
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
package jnpf.dmsService.impl;
 
import jnpf.base.Pagination;
import jnpf.base.UserInfo;
import jnpf.dmsEntity.DmsWenjianBianhaoEntity;
import jnpf.dmsEntity.DmsWenjianBianhaoGuizeEntity;
import jnpf.dmsEntity.DmsWenjianBianhaoGuizeZixiangEntity;
import jnpf.dmsEntity.DmsWenjianBianhaoShenqingEntity;
import jnpf.dmsEntity.DmsWenjianMingchengBiangengShenqingEntity;
import jnpf.dmsEntity.wenjianbianhao.DmsFlowEventType;
import jnpf.dmsMapper.DmsWenjianBianhaoGuizeMapper;
import jnpf.dmsMapper.DmsWenjianBianhaoGuizeZixiangMapper;
import jnpf.dmsMapper.DmsWenjianBianhaoLiushuiJishuMapper;
import jnpf.dmsMapper.DmsWenjianBianhaoMapper;
import jnpf.dmsMapper.DmsWenjianBianhaoShenqingMapper;
import jnpf.dmsMapper.DmsWenjianMingchengBiangengShenqingMapper;
import jnpf.dmsPermission.DmsCurrentIdentity;
import jnpf.dmsService.DmsWenjianBianhaoPermissionService;
import jnpf.exception.DataException;
import jnpf.permission.UserApi;
import jnpf.permission.entity.UserEntity;
import org.junit.jupiter.api.BeforeEach;
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 java.util.Arrays;
import java.util.Collections;
import java.util.Date;
 
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.lenient;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
 
@ExtendWith(MockitoExtension.class)
class DmsWenjianBianhaoApplicationServiceImplTest {
 
    private static final String TENANT_ID = "tenant-1";
    private static final String FLOW_ID = "flow-1";
    private static final String TASK_ID = "task-1";
 
    @Mock private DmsCurrentIdentity identity;
    @Mock private DmsWenjianBianhaoPermissionService permissionService;
    @Mock private DmsWenjianBianhaoGuizeMapper ruleMapper;
    @Mock private DmsWenjianBianhaoGuizeZixiangMapper partMapper;
    @Mock private DmsWenjianBianhaoShenqingMapper applicationMapper;
    @Mock private DmsWenjianBianhaoMapper numberMapper;
    @Mock private DmsWenjianBianhaoLiushuiJishuMapper counterMapper;
    @Mock private DmsWenjianMingchengBiangengShenqingMapper nameChangeMapper;
    @Mock private UserApi userApi;
 
    @InjectMocks
    private DmsWenjianBianhaoApplicationServiceImpl service;
 
    @BeforeEach
    void setUpIdentity() {
        UserInfo user = new UserInfo();
        user.setUserId("operator-1");
        user.setTenantId(TENANT_ID);
        when(identity.requireUser()).thenReturn(user);
        lenient().when(identity.tenantId(user)).thenReturn(TENANT_ID);
    }
 
    @Test
    void enabledRulesArePermissionFilteredBeforePaging() throws DataException {
        Pagination pagination = new Pagination();
        pagination.setCurrentPage(1);
        pagination.setPageSize(20);
        DmsWenjianBianhaoGuizeEntity allowed = rule("rule-1", "QC-SOP规则");
        allowed.setGuizeLeixing("SOP");
        allowed.setWenjianMulu("/质量控制部QC/SOP");
        DmsWenjianBianhaoGuizeEntity denied = rule("rule-2", "QA-SOP规则");
        when(ruleMapper.selectEnabled(TENANT_ID)).thenReturn(Arrays.asList(allowed, denied));
        when(permissionService.canApplyRule("operator-1", "rule-1")).thenReturn(true);
        when(permissionService.canApplyRule("operator-1", "rule-2")).thenReturn(false);
        when(ruleMapper.selectActiveById(TENANT_ID, "rule-1")).thenReturn(allowed);
        when(partMapper.selectActiveByRule(TENANT_ID, "rule-1")).thenReturn(Arrays.asList(
                part("1", "gudingzhi", "QC", "-"),
                part("2", "liushuihao", "000", "")));
 
        jnpf.dmsEntity.wenjianbianhao.vo.DmsWenjianBianhaoRuleVO result =
                service.enabledRules(pagination, null, "SOP").get(0);
 
        assertEquals("rule-1", result.getId());
        assertEquals("QC-待审", result.getYulanBianhao());
        assertEquals("QC-SOP规则 / QC-待审", result.getGuizeXianshi());
        assertEquals(1, pagination.getTotal());
        verify(permissionService).canApplyRule("operator-1", "rule-1");
        verify(permissionService).canApplyRule("operator-1", "rule-2");
    }
 
    @Test
    void rejectsEventUntilFormAndWorkflowStatesMatch() {
        DmsWenjianBianhaoShenqingEntity application = application(1);
        when(applicationMapper.selectByFlowForUpdate(TENANT_ID, FLOW_ID, TASK_ID))
                .thenReturn(Collections.singletonList(application));
 
        assertThrows(DataException.class, () -> service.syncApplicationFlow(
                FLOW_ID, TASK_ID, 2, DmsFlowEventType.END));
 
        verify(numberMapper, never()).selectByApplicationForUpdate(any(), any());
    }
 
    @Test
    void startEventFreezesOrderedRuleSnapshotAndUsesApplicantPermission() {
        DmsWenjianBianhaoShenqingEntity application = application(1);
        application.setShenqingRenId("applicant-1");
        DmsWenjianBianhaoEntity number = number("number-1", "rule-1", "设备清洁规程");
        when(applicationMapper.selectByFlowForUpdate(TENANT_ID, FLOW_ID, TASK_ID))
                .thenReturn(Collections.singletonList(application));
        when(numberMapper.selectByApplicationForUpdate(TENANT_ID, application.getId()))
                .thenReturn(Collections.singletonList(number));
        when(permissionService.canApplyRule("applicant-1", "rule-1")).thenReturn(true);
        when(ruleMapper.selectActiveById(TENANT_ID, "rule-1"))
                .thenReturn(rule("rule-1", "QC-SOP规则"));
        when(partMapper.selectActiveByRule(TENANT_ID, "rule-1")).thenReturn(Arrays.asList(
                part("1", "gudingzhi", "QC", "-"),
                part("2", "liushuihao", "000", "")));
        when(numberMapper.updateById(number)).thenReturn(1);
 
        service.syncApplicationFlow(FLOW_ID, TASK_ID, 1, DmsFlowEventType.START);
        service.syncApplicationFlow(FLOW_ID, TASK_ID, 1, DmsFlowEventType.START);
 
        assertEquals("pending_review", number.getBizStatus());
        assertEquals("rule-1", number.getGuizeKuaizhaoId());
        assertEquals("QC-{SERIAL:3}", number.getBianhaoMuban());
        assertEquals("QC-待审", number.getYulanBianhao());
        assertEquals("QC-待审", number.getShenqingBianhao());
        assertEquals("设备清洁规程", number.getDangqianWenjianMingcheng());
        assertEquals(1, number.getMingchengBanben());
        verify(permissionService).canApplyRule("applicant-1", "rule-1");
        verify(ruleMapper, times(1)).selectActiveById(TENANT_ID, "rule-1");
        verify(partMapper, times(1)).selectActiveByRule(TENANT_ID, "rule-1");
        verify(numberMapper, times(1)).updateById(number);
    }
 
    @Test
    void repeatedPassedEventDoesNotConsumeAnotherSerial() {
        DmsWenjianBianhaoShenqingEntity application = application(2);
        DmsWenjianBianhaoEntity number = number("number-1", "rule-1", "设备清洁规程");
        number.setGuizeKuaizhao("[{}]");
        number.setGuizeKuaizhaoId("rule-1");
        number.setBianhaoMuban("QC-{SERIAL:3}");
        number.setBizStatus("pending_review");
        when(applicationMapper.selectByFlowForUpdate(TENANT_ID, FLOW_ID, TASK_ID))
                .thenReturn(Collections.singletonList(application));
        when(numberMapper.selectByApplicationForUpdate(TENANT_ID, application.getId()))
                .thenReturn(Collections.singletonList(number));
        when(counterMapper.next(TENANT_ID, "rule-1", "QC-{SERIAL:3}")).thenReturn(7L);
        when(numberMapper.updateById(number)).thenReturn(1);
 
        service.syncApplicationFlow(FLOW_ID, TASK_ID, 2, DmsFlowEventType.END);
        service.syncApplicationFlow(FLOW_ID, TASK_ID, 2, DmsFlowEventType.END);
 
        assertEquals("QC-007", number.getWenjianBianhao());
        assertEquals("QC-007", number.getShenqingBianhao());
        assertEquals("007", number.getLiushuiHao());
        assertEquals("using", number.getBizStatus());
        org.junit.jupiter.api.Assertions.assertNotNull(number.getBianhaoFafangShijian());
        verify(counterMapper, times(1)).next(TENANT_ID, "rule-1", "QC-{SERIAL:3}");
        verify(numberMapper, times(1)).updateById(number);
    }
 
    @Test
    void usingNumbersReturnsPopupFieldsFromTenantScopedQuery() throws DataException {
        Pagination pagination = new Pagination();
        pagination.setCurrentPage(1);
        pagination.setPageSize(20);
        DmsWenjianBianhaoEntity number = number("number-1", "rule-1", "原文件名称");
        number.setWenjianBianhao("QC-001");
        number.setDangqianWenjianMingcheng("当前文件名称");
        number.setMingchengBanben(2);
        number.setGuizeMingchengKuaizhao("QC-SOP规则");
        number.setShiyongRenId("user-1");
        number.setBianhaoFafangShijian(new Date());
        when(numberMapper.selectUsing(TENANT_ID, "number-1", pagination))
                .thenReturn(Collections.singletonList(number));
        when(permissionService.canViewNumber("operator-1", "number-1")).thenReturn(true);
        UserEntity user = new UserEntity();
        user.setId("user-1");
        user.setRealName("张三");
        when(userApi.getUserName(Collections.singletonList("user-1")))
                .thenReturn(Collections.singletonList(user));
 
        jnpf.dmsEntity.wenjianbianhao.vo.DmsWenjianBianhaoOptionVO result =
                service.usingNumbers(pagination, " number-1 ", "当前").get(0);
 
        assertEquals("number-1", result.getId());
        assertEquals("QC-001", result.getWenjianBianhao());
        assertEquals("当前文件名称", result.getWenjianMingcheng());
        assertEquals(2, result.getMingchengBanben());
        assertEquals("张三", result.getShiyongRen());
        assertEquals("QC-001 / 当前文件名称", result.getBianhaoXianshi());
        assertEquals("当前", pagination.getKeyword());
        verify(permissionService).canViewNumber("operator-1", "number-1");
        verify(numberMapper).selectUsing(TENANT_ID, "number-1", pagination);
    }
 
    @Test
    void usingNumbersExcludeRowsDeniedByPermissionService() throws DataException {
        Pagination pagination = new Pagination();
        pagination.setCurrentPage(1);
        pagination.setPageSize(20);
        DmsWenjianBianhaoEntity denied = number("number-1", "rule-1", "受限文件");
        denied.setWenjianBianhao("QA-001");
        when(numberMapper.selectUsing(TENANT_ID, "", pagination))
                .thenReturn(Collections.singletonList(denied));
        when(permissionService.canViewNumber("operator-1", "number-1")).thenReturn(false);
 
        assertEquals(0, service.usingNumbers(pagination, null, null).size());
 
        verify(permissionService).canViewNumber("operator-1", "number-1");
        verify(userApi, never()).getUserName(any());
    }
 
    @Test
    void recallOnlyRestoresUnissuedNumberToDraft() {
        DmsWenjianBianhaoShenqingEntity application = application(9);
        DmsWenjianBianhaoEntity pending = number("number-1", "rule-1", "文件一");
        pending.setBizStatus("pending_review");
        DmsWenjianBianhaoEntity issued = number("number-2", "rule-1", "文件二");
        issued.setBizStatus("using");
        issued.setWenjianBianhao("QC-001");
        when(applicationMapper.selectByFlowForUpdate(TENANT_ID, FLOW_ID, TASK_ID))
                .thenReturn(Collections.singletonList(application));
        when(numberMapper.selectByApplicationForUpdate(TENANT_ID, application.getId()))
                .thenReturn(Arrays.asList(pending, issued));
        when(numberMapper.updateById(pending)).thenReturn(1);
 
        service.syncApplicationFlow(FLOW_ID, TASK_ID, 9, DmsFlowEventType.RECALL);
 
        assertEquals("draft", pending.getBizStatus());
        assertEquals("using", issued.getBizStatus());
        verify(numberMapper).updateById(pending);
        verify(numberMapper, never()).updateById(issued);
    }
 
    @Test
    void nameChangePassIsVersionCheckedAndIdempotent() {
        DmsWenjianMingchengBiangengShenqingEntity change = new DmsWenjianMingchengBiangengShenqingEntity();
        change.setId("change-1");
        change.setTenantId(TENANT_ID);
        change.setFlowId(FLOW_ID);
        change.setFlowTaskId(TASK_ID);
        change.setFlowState(1);
        change.setBianhaoId("number-1");
        change.setBiangengHouWenjianMingcheng("新文件名称");
        DmsWenjianBianhaoEntity number = number("number-1", "rule-1", "原文件名称");
        number.setBizStatus("using");
        number.setWenjianBianhao("QC-001");
        number.setDangqianWenjianMingcheng("原文件名称");
        number.setMingchengBanben(3);
        when(nameChangeMapper.selectByFlowForUpdate(TENANT_ID, FLOW_ID, TASK_ID))
                .thenReturn(Collections.singletonList(change));
        when(numberMapper.selectActiveByIdForUpdate(TENANT_ID, "number-1")).thenReturn(number);
        when(nameChangeMapper.updateById(change)).thenReturn(1);
        when(numberMapper.updateById(number)).thenReturn(1);
 
        service.syncNameChangeFlow(FLOW_ID, TASK_ID, 1, DmsFlowEventType.START);
        change.setFlowState(2);
        service.syncNameChangeFlow(FLOW_ID, TASK_ID, 2, DmsFlowEventType.END);
        service.syncNameChangeFlow(FLOW_ID, TASK_ID, 2, DmsFlowEventType.END);
 
        assertEquals("原文件名称", change.getYuanWenjianMingcheng());
        assertEquals(3, change.getYuanMingchengBanben());
        assertEquals("新文件名称", number.getDangqianWenjianMingcheng());
        assertEquals(4, number.getMingchengBanben());
        verify(numberMapper, times(1)).updateById(number);
    }
 
    @Test
    void nameChangeStartRejectsAnotherActiveApplicationForTheSameNumber() {
        DmsWenjianMingchengBiangengShenqingEntity change =
                new DmsWenjianMingchengBiangengShenqingEntity();
        change.setId("change-2");
        change.setTenantId(TENANT_ID);
        change.setFlowId(FLOW_ID);
        change.setFlowTaskId(TASK_ID);
        change.setFlowState(1);
        change.setBianhaoId("number-1");
        change.setBiangengHouWenjianMingcheng("另一个新名称");
        DmsWenjianBianhaoEntity number = number("number-1", "rule-1", "原文件名称");
        number.setBizStatus("using");
        number.setWenjianBianhao("QC-001");
        when(nameChangeMapper.selectByFlowForUpdate(TENANT_ID, FLOW_ID, TASK_ID))
                .thenReturn(Collections.singletonList(change));
        when(numberMapper.selectActiveByIdForUpdate(TENANT_ID, "number-1")).thenReturn(number);
        when(nameChangeMapper.countActiveByNumber(TENANT_ID, "number-1", "change-2"))
                .thenReturn(1);
 
        DataException exception = assertThrows(DataException.class,
                () -> service.syncNameChangeFlow(FLOW_ID, TASK_ID, 1, DmsFlowEventType.START));
 
        assertEquals("该文件编号已有进行中的名称变更申请,请勿重复提交", exception.getMessage());
        verify(nameChangeMapper, never()).updateById(change);
    }
 
    private DmsWenjianBianhaoShenqingEntity application(int flowState) {
        DmsWenjianBianhaoShenqingEntity application = new DmsWenjianBianhaoShenqingEntity();
        application.setId("application-1");
        application.setTenantId(TENANT_ID);
        application.setFlowId(FLOW_ID);
        application.setFlowTaskId(TASK_ID);
        application.setFlowState(flowState);
        return application;
    }
 
    private DmsWenjianBianhaoEntity number(String id, String ruleId, String name) {
        DmsWenjianBianhaoEntity number = new DmsWenjianBianhaoEntity();
        number.setId(id);
        number.setForeignId("application-1");
        number.setTenantId(TENANT_ID);
        number.setGuizeId(ruleId);
        number.setYuanWenjianMingcheng(name);
        return number;
    }
 
    private DmsWenjianBianhaoGuizeEntity rule(String id, String name) {
        DmsWenjianBianhaoGuizeEntity rule = new DmsWenjianBianhaoGuizeEntity();
        rule.setId(id);
        rule.setTenantId(TENANT_ID);
        rule.setGuizeMingcheng(name);
        rule.setBizEnabled("enabled");
        return rule;
    }
 
    private DmsWenjianBianhaoGuizeZixiangEntity part(
            String id, String type, String value, String separator) {
        DmsWenjianBianhaoGuizeZixiangEntity part = new DmsWenjianBianhaoGuizeZixiangEntity();
        part.setId(id);
        part.setTenantId(TENANT_ID);
        part.setForeignId("rule-1");
        part.setBianhaoZuheLeixing(type);
        part.setZhi(value);
        part.setFengefu(separator);
        return part;
    }
}