刘光辉
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
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
package jnpf.elnService.template.impl;
 
import jnpf.elnEntity.template.ElnTemplateRepeatGroupEntity;
import jnpf.elnEntity.template.ElnTemplateVos;
import jnpf.exception.DataException;
import org.junit.Test;
 
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
 
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
 
public class ElnTemplateRepeatGroupServiceImplTest {
 
    @Test
    public void createsDefaultsAndRejectsConflictingOrInvalidConstraints() {
        MemoryStore store = new MemoryStore();
        ElnTemplateRepeatGroupServiceImpl service =
                new ElnTemplateRepeatGroupServiceImpl(store, () -> "group-id");
 
        ElnTemplateVos.RepeatGroup created = service.ensureConfigured(
                "tenant-1", "template-1", "results", null, null, "step-1", "user-1");
 
        assertEquals("results", created.getGroupCode());
        assertEquals(Integer.valueOf(0), created.getMinRows());
        assertEquals(Integer.valueOf(100), created.getMaxRows());
        assertEquals("step-1", created.getStepId());
        assertEquals(1, store.groups.size());
        ElnTemplateVos.RepeatGroup retried = service.ensureConfigured(
                "tenant-1", "template-1", "results", 0, 100, "step-1", "user-1");
        assertEquals(created, retried);
 
        expectFailure(() -> service.ensureConfigured(
                "tenant-1", "template-1", "results", 1, 100, "step-1", "user-1"));
        expectFailure(() -> service.ensureConfigured(
                "tenant-1", "template-1", "invalid code", 0, 100, "step-1", "user-1"));
        expectFailure(() -> service.ensureConfigured(
                "tenant-1", "template-1", "other", 5, 4, "step-1", "user-1"));
        expectFailure(() -> service.ensureConfigured(
                "tenant-1", "template-1", "other", 0, 1001, "step-1", "user-1"));
        expectFailure(() -> service.ensureConfigured(
                "tenant-1", "template-1", "results", 0, 100, "step-2", "user-1"));
    }
 
    @Test
    public void copiesVersionScopedConstraintsIdempotently() {
        MemoryStore store = new MemoryStore();
        store.add(group("source-1", "tenant-1", "template-v1", "results", 2, 20));
        store.add(group("source-2", "tenant-1", "template-v1", "samples", 0, 10));
        ElnTemplateRepeatGroupServiceImpl service =
                new ElnTemplateRepeatGroupServiceImpl(store, new SequenceIds());
 
        service.afterBindingsActivated(
                "tenant-1", "template-v1", "template-v2", new LinkedHashMap<>());
        service.afterBindingsActivated(
                "tenant-1", "template-v1", "template-v2", new LinkedHashMap<>());
 
        List<ElnTemplateVos.RepeatGroup> copied = service.list("tenant-1", "template-v2");
        assertEquals(2, copied.size());
        assertEquals("results", copied.get(0).getGroupCode());
        assertEquals(Integer.valueOf(2), copied.get(0).getMinRows());
        assertEquals(Integer.valueOf(20), copied.get(0).getMaxRows());
        assertEquals("samples", copied.get(1).getGroupCode());
        assertEquals(4, store.groups.size());
    }
 
    private static ElnTemplateRepeatGroupEntity group(String id, String tenantId,
            String templateId, String code, int minRows, int maxRows) {
        ElnTemplateRepeatGroupEntity entity = new ElnTemplateRepeatGroupEntity();
        entity.setId(id);
        entity.setFTenantId(tenantId);
        entity.setMubanId(templateId);
        entity.setRepeatGroupCode(code);
        entity.setMinRows(minRows);
        entity.setMaxRows(maxRows);
        entity.setFDeleteMark(0);
        return entity;
    }
 
    private static void expectFailure(Runnable action) {
        try {
            action.run();
            fail("预期重复组配置被拒绝");
        } catch (DataException expected) {
            // expected
        }
    }
 
    private static final class SequenceIds implements java.util.function.Supplier<String> {
        private int next;
        @Override public String get() { return "copied-" + (++next); }
    }
 
    private static final class MemoryStore implements ElnTemplateRepeatGroupServiceImpl.Store {
        private final Map<String, ElnTemplateRepeatGroupEntity> groups = new LinkedHashMap<>();
 
        private void add(ElnTemplateRepeatGroupEntity entity) {
            groups.put(key(entity.getFTenantId(), entity.getMubanId(),
                    entity.getRepeatGroupCode()), entity);
        }
 
        @Override
        public ElnTemplateRepeatGroupEntity find(String tenantId, String templateId,
                String groupCode) {
            return groups.get(key(tenantId, templateId, groupCode));
        }
 
        @Override
        public List<ElnTemplateRepeatGroupEntity> list(String tenantId, String templateId) {
            List<ElnTemplateRepeatGroupEntity> result = new ArrayList<>();
            for (ElnTemplateRepeatGroupEntity entity : groups.values()) {
                if (tenantId.equals(entity.getFTenantId())
                        && templateId.equals(entity.getMubanId())) {
                    result.add(entity);
                }
            }
            return result;
        }
 
        @Override
        public void insert(ElnTemplateRepeatGroupEntity entity) {
            add(entity);
        }
 
        private static String key(String tenantId, String templateId, String groupCode) {
            return tenantId + ":" + templateId + ":" + groupCode;
        }
    }
}