刘光辉
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
package jnpf.elnService.impl;
 
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import jnpf.elnEntity.runtime.ElnRecordVos;
import jnpf.elnEntity.runtime.ElnSharedFillOperationEntity;
import jnpf.elnMapper.ElnSharedFillOperationMapper;
import jnpf.util.JsonUtil;
import org.junit.Test;
 
import java.util.Collections;
 
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
 
public class ElnRecordSourceSyncOperationServiceTest {
 
    @Test
    public void persistsPendingDocumentResultAndConfirmsItIdempotently() {
        ElnSharedFillOperationMapper operations = mock(ElnSharedFillOperationMapper.class);
        ElnSharedFillOperationEntity open = operation("OPEN", null);
        when(operations.insertIgnore(any(ElnSharedFillOperationEntity.class))).thenReturn(1);
        when(operations.selectOne(any(Wrapper.class))).thenReturn(open);
        when(operations.prepareResult(any(String.class), any(String.class), any(String.class),
                any(String.class), any(String.class))).thenReturn(1);
        when(operations.confirmPrepared(any(String.class), any(String.class),
                any(String.class))).thenReturn(1, 0);
        ElnRecordSourceSyncOperationService service =
                new ElnRecordSourceSyncOperationService(operations);
        ElnRecordVos.SourceSynchronized result = new ElnRecordVos.SourceSynchronized();
        result.setOperationCode("sync-1");
        result.setChangedFieldCodes(Collections.singletonList("source"));
        result.setRecordId("record-1");
 
        assertNull(service.begin("tenant-1", "user-1", "sync-1", "digest-1"));
        assertSame(result, service.prepare(
                "tenant-1", "user-1", "sync-1", "digest-1", result));
 
        ElnSharedFillOperationEntity pending = operation(
                "OPEN", JsonUtil.getObjectToString(result));
        when(operations.insertIgnore(any(ElnSharedFillOperationEntity.class))).thenReturn(0);
        when(operations.selectOne(any(Wrapper.class))).thenReturn(pending);
        when(operations.selectList(any(Wrapper.class)))
                .thenReturn(Collections.singletonList(pending));
        assertEquals(Collections.singletonList("source"), service.begin(
                "tenant-1", "user-1", "sync-1", "digest-1").getChangedFieldCodes());
        assertEquals("sync-1", service.pending(
                "tenant-1", "user-1", "record-1").getOperationCode());
        assertTrue(service.hasPendingDocumentConfirmation("tenant-1", "record-1"));
        assertTrue(service.confirm("tenant-1", "user-1", "sync-1"));
 
        ElnSharedFillOperationEntity completed = operation(
                "COMPLETED", JsonUtil.getObjectToString(result));
        when(operations.selectOne(any(Wrapper.class))).thenReturn(completed);
        assertFalse(service.confirm("tenant-1", "user-1", "sync-1"));
    }
 
    private static ElnSharedFillOperationEntity operation(String status, String resultJson) {
        ElnSharedFillOperationEntity value = new ElnSharedFillOperationEntity();
        value.setOperationCode("sync-1");
        value.setRequestDigest("digest-1");
        value.setOperationStatus(status);
        value.setResultJson(resultJson);
        value.setOperatorUserId("user-1");
        value.setFTenantId("tenant-1");
        return value;
    }
}