刘光辉
4 小时以前 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
package jnpf.elnService.field;
 
import jnpf.elnEntity.field.ElnFieldDefEntity;
import jnpf.elnEntity.field.ElnFieldVersionEntity;
import jnpf.elnMapper.ElnFieldDefMapper;
import jnpf.elnMapper.ElnFieldVersionMapper;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
 
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
 
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
 
public class ElnTestResultFieldCatalogImplTest {
 
    @Test
    public void createsPublishedStableFieldsWithConflictSafeMappers() {
        ElnFieldDefMapper definitions = mock(ElnFieldDefMapper.class);
        ElnFieldVersionMapper versions = mock(ElnFieldVersionMapper.class);
        when(definitions.insertIgnoreCode(any())).thenReturn(1);
        when(versions.insertIgnoreVersionNo(any())).thenReturn(1);
        when(definitions.updateById(any(ElnFieldDefEntity.class))).thenReturn(1);
 
        new ElnTestResultFieldCatalogImpl(definitions, versions).ensure("tenant-1", "user-1");
 
        ArgumentCaptor<ElnFieldDefEntity> definitionsCaptor = ArgumentCaptor.forClass(ElnFieldDefEntity.class);
        verify(definitions, times(2)).insertIgnoreCode(definitionsCaptor.capture());
        assertEquals(ElnTestResultFields.RESULT, definitionsCaptor.getAllValues().get(0).getZiduanBianma());
        assertEquals(ElnTestResultFields.CONCLUSION, definitionsCaptor.getAllValues().get(1).getZiduanBianma());
        ArgumentCaptor<ElnFieldVersionEntity> versionsCaptor = ArgumentCaptor.forClass(ElnFieldVersionEntity.class);
        verify(versions, times(2)).insertIgnoreVersionNo(versionsCaptor.capture());
        List<ElnFieldVersionEntity> created = versionsCaptor.getAllValues();
        assertEquals("TEXT", created.get(0).getShujuLeixing());
        assertEquals("SELECT", created.get(1).getShujuLeixing());
        assertTrue(created.get(1).getKongjianPeizhi().contains("non_compliant"));
    }
 
    @Test
    public void secondEnsureReusesCurrentPublishedDefinitionsAndVersions() {
        ElnFieldDefMapper definitions = mock(ElnFieldDefMapper.class);
        ElnFieldVersionMapper versions = mock(ElnFieldVersionMapper.class);
        List<ElnFieldDefEntity> createdDefinitions = new ArrayList<>();
        List<ElnFieldVersionEntity> createdVersions = new ArrayList<>();
        AtomicInteger definitionReads = new AtomicInteger();
        AtomicInteger versionReads = new AtomicInteger();
        when(definitions.insertIgnoreCode(any())).thenAnswer(invocation -> {
            createdDefinitions.add(invocation.getArgument(0));
            return 1;
        });
        when(versions.insertIgnoreVersionNo(any())).thenAnswer(invocation -> {
            createdVersions.add(invocation.getArgument(0));
            return 1;
        });
        when(definitions.updateById(any(ElnFieldDefEntity.class))).thenReturn(1);
        when(definitions.selectOne(any())).thenAnswer(invocation -> {
            int read = definitionReads.getAndIncrement();
            return read < 2 ? null : createdDefinitions.get(read - 2);
        });
        when(versions.selectOne(any())).thenAnswer(invocation ->
                createdVersions.get(versionReads.getAndIncrement()));
 
        ElnTestResultFieldCatalogImpl catalog = new ElnTestResultFieldCatalogImpl(definitions, versions);
        catalog.ensure("tenant-1", "user-1");
        catalog.ensure("tenant-1", "user-1");
 
        verify(definitions, times(2)).insertIgnoreCode(any(ElnFieldDefEntity.class));
        verify(versions, times(2)).insertIgnoreVersionNo(any(ElnFieldVersionEntity.class));
        verify(definitions, times(2)).updateById(any(ElnFieldDefEntity.class));
    }
}