刘光辉
118 分钟以前 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package jnpf.elnService.template.impl;
 
import jnpf.elnEntity.template.ElnTemplateCheckboxGroupEntity;
import jnpf.elnEntity.template.ElnTemplateVos;
import jnpf.exception.DataException;
import org.junit.Test;
 
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
 
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.fail;
 
public class ElnTemplateCheckboxGroupServiceImplTest {
 
    @Test
    public void createsArbitraryAndUnlimitedTemplateSelectionRules() {
        Fixture fixture = new Fixture();
 
        ElnTemplateVos.CheckboxGroup bounded = fixture.service.ensureConfigured(
                "tenant-1", "template-1", "review_result", 2, 5, "user-1");
        ElnTemplateVos.CheckboxGroup unlimited = fixture.service.ensureConfigured(
                "tenant-1", "template-1", "extra_result", 3, null, "user-1");
 
        assertEquals(Integer.valueOf(2), bounded.getMinSelected());
        assertEquals(Integer.valueOf(5), bounded.getMaxSelected());
        assertEquals(Integer.valueOf(3), unlimited.getMinSelected());
        assertNull(unlimited.getMaxSelected());
        assertEquals(2, fixture.service.list("tenant-1", "template-1").size());
    }
 
    @Test
    public void updatesRulesWithOptimisticVersionAndRejectsInvalidRanges() {
        Fixture fixture = new Fixture();
        fixture.service.ensureConfigured("tenant-1", "template-1", "result", 0, 1, "user-1");
 
        ElnTemplateVos.CheckboxGroup changed = fixture.service.change(
                "tenant-1", "template-1", "result", 1, 4, 0, "user-1");
 
        assertEquals(Integer.valueOf(1), changed.getMinSelected());
        assertEquals(Integer.valueOf(4), changed.getMaxSelected());
        assertEquals(Integer.valueOf(1), changed.getVersion());
        expectDataException(() -> fixture.service.change(
                "tenant-1", "template-1", "result", 2, 3, 0, "user-1"));
        expectDataException(() -> fixture.service.ensureConfigured(
                "tenant-1", "template-1", "invalid", 4, 2, "user-1"));
    }
 
    @Test
    public void copiesRulesWhenTemplateVersionBindingsAreActivated() {
        Fixture fixture = new Fixture();
        fixture.service.ensureConfigured("tenant-1", "source", "result", 1, 7, "user-1");
 
        fixture.service.afterBindingsActivated(
                "tenant-1", "source", "target", new LinkedHashMap<String, String>());
 
        ElnTemplateVos.CheckboxGroup copied = fixture.service.list("tenant-1", "target").get(0);
        assertEquals("result", copied.getGroupCode());
        assertEquals(Integer.valueOf(1), copied.getMinSelected());
        assertEquals(Integer.valueOf(7), copied.getMaxSelected());
    }
 
    @Test
    public void removesRulesWithoutLiveMembers() {
        Fixture fixture = new Fixture();
        fixture.service.ensureConfigured("tenant-1", "template-1", "used", 0, 2, "user-1");
        fixture.service.ensureConfigured("tenant-1", "template-1", "orphan", 1, null, "user-1");
 
        fixture.service.removeUnused("tenant-1", "template-1",
                Collections.singleton("used"), "user-1");
 
        List<ElnTemplateVos.CheckboxGroup> remaining =
                fixture.service.list("tenant-1", "template-1");
        assertEquals(1, remaining.size());
        assertEquals("used", remaining.get(0).getGroupCode());
    }
 
    private static void expectDataException(Runnable action) {
        try {
            action.run();
            fail("预期抛出 DataException");
        } catch (DataException expected) {
            // expected
        }
    }
 
    private static final class Fixture {
        private final MemoryStore store = new MemoryStore();
        private final AtomicInteger ids = new AtomicInteger();
        private final ElnTemplateCheckboxGroupServiceImpl service =
                new ElnTemplateCheckboxGroupServiceImpl(store,
                        () -> "group-" + ids.incrementAndGet());
    }
 
    private static final class MemoryStore implements ElnTemplateCheckboxGroupServiceImpl.Store {
        private final Map<String, ElnTemplateCheckboxGroupEntity> groups = new LinkedHashMap<>();
 
        @Override
        public ElnTemplateCheckboxGroupEntity find(String tenantId, String templateId,
                                                    String groupCode) {
            return groups.get(key(tenantId, templateId, groupCode));
        }
 
        @Override
        public List<ElnTemplateCheckboxGroupEntity> list(String tenantId, String templateId) {
            List<ElnTemplateCheckboxGroupEntity> result = new ArrayList<>();
            for (ElnTemplateCheckboxGroupEntity entity : groups.values()) {
                if (tenantId.equals(entity.getFTenantId())
                        && templateId.equals(entity.getMubanId())) result.add(entity);
            }
            return result;
        }
 
        @Override
        public void insert(ElnTemplateCheckboxGroupEntity entity) {
            groups.put(key(entity.getFTenantId(), entity.getMubanId(), entity.getGroupCode()), entity);
        }
 
        @Override
        public boolean update(ElnTemplateCheckboxGroupEntity entity, int expectedVersion) {
            ElnTemplateCheckboxGroupEntity stored = find(
                    entity.getFTenantId(), entity.getMubanId(), entity.getGroupCode());
            if (stored == null || !Integer.valueOf(expectedVersion).equals(stored.getFVersion())) return false;
            entity.setFVersion(expectedVersion + 1);
            groups.put(key(entity.getFTenantId(), entity.getMubanId(), entity.getGroupCode()), entity);
            return true;
        }
 
        @Override
        public boolean softDelete(ElnTemplateCheckboxGroupEntity entity, int expectedVersion) {
            ElnTemplateCheckboxGroupEntity stored = find(
                    entity.getFTenantId(), entity.getMubanId(), entity.getGroupCode());
            if (stored == null || !Integer.valueOf(expectedVersion).equals(stored.getFVersion())) {
                return false;
            }
            groups.remove(key(entity.getFTenantId(), entity.getMubanId(), entity.getGroupCode()));
            return true;
        }
 
        private static String key(String tenantId, String templateId, String code) {
            return tenantId + ":" + templateId + ":" + code;
        }
    }
}