刘光辉
3 小时以前 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
package jnpf.dmsService;
 
import jnpf.dmsEntity.storage.approval.DmsDisableRequest;
import jnpf.dmsEntity.storage.approval.DmsApprovedBatch;
import jnpf.dmsMapper.*;
import jnpf.flowable.WorkFlowApi;
import jnpf.flowable.entity.TaskEntity;
import jnpf.exception.DataException;
import org.junit.jupiter.api.*;
import java.util.*;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;
import org.springframework.test.util.ReflectionTestUtils;
 
class DmsApprovedDisableServiceTest {
    DmsApprovedDisableMapper mapper=mock(DmsApprovedDisableMapper.class);
    DmsWenjianZiyuanMapper resources=mock(DmsWenjianZiyuanMapper.class);
    WorkFlowApi workflow=mock(WorkFlowApi.class);
    DmsApprovedBatchMapper batches=mock(DmsApprovedBatchMapper.class);
    DmsApprovedDisableService service=new DmsApprovedDisableService(mapper,resources,workflow,batches);
    Date approvedAt=new Date(System.currentTimeMillis()-60000);
    @BeforeEach void enable(){ReflectionTestUtils.setField(service,"enabled",true);}
    DmsDisableRequest row() {
        DmsDisableRequest r=new DmsDisableRequest(); r.setId(UUID.randomUUID());r.setTenantId("tenant");
        r.setFlowId("flow");r.setTaskId("task");r.setOperation("yishixiao");r.setApplicationId("application");
        r.setSourceRowId("child");r.setFileId("main");r.setBusinessVersionId("version");
        r.setDocumentId(UUID.randomUUID());r.setFileVersionId(UUID.randomUUID());r.setFolderId(UUID.randomUUID());
        r.setSourceFileId("new");r.setSourceVersionId("newVersion");r.setApprovedAt(approvedAt);
        r.setActorId("persisted-actor");r.setScheduledAt(approvedAt);r.setStatus("WAITING");r.setLeaseToken("lease");return r;
    }
    void approved() {
        TaskEntity task=new TaskEntity();task.setId("task");task.setFlowId("flow");task.setStatus(2);
        task.setEndTime(approvedAt);when(workflow.getInfoSubmit("task")).thenReturn(task);
    }
    @Test void noReceiptWithoutRealApproval() {
        assertThrows(DataException.class,()->service.enqueue("tenant","flow","task","yishixiao"));
        verifyNoInteractions(mapper,resources);
    }
    @Test void enqueueUsesPersistedActorWhenWorkflowProjectionOmitsActor() {
        approved();
        for (String operation : List.of("yichexiao", "yishixiao")) {
            DmsDisableRequest r=row();r.setOperation(operation);
            when(mapper.nativeRows("tenant","flow","task",operation)).thenReturn(List.of(r));
            when(mapper.validateTarget(r)).thenReturn(1);when(mapper.candidateExists(r)).thenReturn(1);
            DmsApprovedBatch batch=service.enqueue("tenant","flow","task",operation);
            assertEquals("persisted-actor",batch.getActorId());
            assertEquals("persisted-actor",r.getActorId());verify(mapper).enqueue(r);
        }
    }
    @Test void missingPersistedActorFailsBeforeWritingAnyProgress() {
        approved();DmsDisableRequest r=row();r.setActorId(null);
        when(mapper.nativeRows("tenant","flow","task","yishixiao")).thenReturn(List.of(r));
        assertThrows(DataException.class,()->service.enqueue("tenant","flow","task","yishixiao"));
        verifyNoInteractions(batches,resources);verify(mapper,never()).enqueue(any());
    }
    @Test void datesAreIndependentAndNeverEarlierThanApproval() {
        approved();DmsDisableRequest a=row(),b=row();b.setSourceRowId("child2");b.setFileId("main2");
        b.setRequestedAt(new Date(approvedAt.getTime()+3*86400000L));
        when(mapper.nativeRows("tenant","flow","task","yishixiao")).thenReturn(List.of(a,b));
        when(mapper.validateTarget(any())).thenReturn(1);when(mapper.candidateExists(any())).thenReturn(1);
        DmsApprovedBatch batch=service.enqueue("tenant","flow","task","yishixiao");
        assertEquals(batch.getId(),a.getBatchId());assertEquals(batch.getId(),b.getBatchId());
        assertEquals(approvedAt,batch.getScheduledAt());verify(batches).insertBatch(batch);
        assertEquals(approvedAt,a.getScheduledAt());assertTrue(b.getScheduledAt().after(approvedAt));
        verify(mapper).enqueue(a);verify(mapper).enqueue(b);
    }
    @Test void duplicateCallbackReusesTaskButChangedOrRemovedRowsAreRejected() {
        approved();DmsDisableRequest a=row(),b=row();b.setSourceRowId("child2");b.setFileId("main2");
        when(mapper.nativeRows("tenant","flow","task","yishixiao")).thenReturn(List.of(a,b));
        when(mapper.validateTarget(any())).thenReturn(1);when(mapper.candidateExists(any())).thenReturn(1);
        DmsApprovedBatch batch=service.enqueue("tenant","flow","task","yishixiao");
        when(batches.find("tenant","task","shixiao_shenqing")).thenReturn(batch);
        when(mapper.nativeRows("tenant","flow","task","yishixiao")).thenReturn(List.of(b,a));
        assertSame(batch,service.enqueue("tenant","flow","task","yishixiao"));
        verify(batches,times(1)).insertBatch(any());verify(mapper,times(2)).validateTarget(any());
        when(mapper.nativeRows("tenant","flow","task","yishixiao")).thenReturn(List.of(a));
        assertThrows(DataException.class,()->service.enqueue("tenant","flow","task","yishixiao"));
    }
    @Test void changedDateRejectsDuplicateCallback() {
        approved();DmsDisableRequest a=row();
        when(mapper.nativeRows("tenant","flow","task","yishixiao")).thenReturn(List.of(a));
        when(mapper.validateTarget(any())).thenReturn(1);when(mapper.candidateExists(any())).thenReturn(1);
        DmsApprovedBatch batch=service.enqueue("tenant","flow","task","yishixiao");
        when(batches.find("tenant","task","shixiao_shenqing")).thenReturn(batch);
        a.setRequestedAt(new Date());
        assertThrows(DataException.class,()->service.enqueue("tenant","flow","task","yishixiao"));
    }
}