刘光辉
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
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package jnpf.elnService.impl;
 
import jnpf.elnEntity.runtime.ElnRecordEntity;
import jnpf.elnEntity.runtime.ElnRecordFieldValueEntity;
import jnpf.elnEntity.runtime.ElnRecordFormulaCalculationResult;
import jnpf.elnEntity.runtime.ElnRecordRepeatValueEntity;
import jnpf.elnEntity.runtime.ElnRecordForms;
import jnpf.elnEntity.runtime.ElnRecordRevisionEntity;
import jnpf.elnEntity.runtime.ElnRecordVos;
import jnpf.elnMapper.ElnRecordFieldValueMapper;
import jnpf.elnMapper.ElnRecordMapper;
import jnpf.elnMapper.ElnRecordRepeatValueMapper;
import jnpf.elnMapper.ElnRecordRevisionMapper;
import jnpf.elnService.ElnRecordFormulaCalculationService;
import jnpf.lims.api.model.TestWorkItemSnapshotBatch;
import jnpf.lims.api.model.TestWorkItemSnapshot;
import org.junit.Test;
 
import java.lang.reflect.Proxy;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
 
import static org.junit.Assert.assertEquals;
 
public class ElnRecordRecalculationServiceImplTest {
 
    @Test
    public void protectsFullRecalculationWithTaskAccessAndRecordVersion() {
        ElnRecordEntity record = new ElnRecordEntity();
        record.setId("record-1");
        record.setJianyanXiangId("item-1");
        record.setDangqianXiudingId("revision-1");
        record.setFVersion(7);
        ElnRecordRevisionEntity revision = new ElnRecordRevisionEntity();
        revision.setId("revision-1");
        revision.setDianziJiluId("record-1");
        revision.setBizStatus("DRAFT");
        List<ElnRecordFieldValueEntity> values = Arrays.asList(
                calculated("result-a", "SUCCESS"),
                calculated("result-b", "MISSING_INPUT"),
                calculated("result-c", "ERROR"));
        AtomicInteger casCalls = new AtomicInteger();
        AtomicInteger updates = new AtomicInteger();
        AtomicInteger repeatUpdates = new AtomicInteger();
        ElnRecordMapper recordMapper = proxy(ElnRecordMapper.class, (method, args) -> {
            if ("selectOne".equals(method)) return record;
            if ("advanceVersion".equals(method)) {
                assertEquals(7, args[2]);
                casCalls.incrementAndGet();
                return 1;
            }
            return null;
        });
        ElnRecordRevisionMapper revisionMapper = proxy(
                ElnRecordRevisionMapper.class,
                (method, args) -> "selectOne".equals(method) ? revision : null);
        ElnRecordFieldValueMapper valueMapper = proxy(
                ElnRecordFieldValueMapper.class, (method, args) -> {
                    if ("selectList".equals(method)) return new ArrayList<>(values);
                    if ("updateById".equals(method)) {
                        updates.incrementAndGet();
                        return 1;
                    }
                    return null;
                });
        ElnRecordFormulaCalculationService calculation =
                new ElnRecordFormulaCalculationService() {
                    @Override
                    public List<ElnRecordFieldValueEntity> recalculateAffected(
                            String tenantId, ElnRecordEntity record,
                            ElnRecordRevisionEntity revision,
                            List<ElnRecordFieldValueEntity> values,
                            java.util.Set<String> changedBindingIds,
                            String operationCode, String operatorUserId) {
                        return Collections.emptyList();
                    }
 
                    @Override
                    public List<ElnRecordFieldValueEntity> recalculateAll(
                            String tenantId, ElnRecordEntity actualRecord,
                            ElnRecordRevisionEntity actualRevision,
                            List<ElnRecordFieldValueEntity> actualValues,
                            String operationCode, String operatorUserId) {
                        assertEquals("tenant-a", tenantId);
                        assertEquals("operation-1", operationCode);
                        assertEquals("user-1", operatorUserId);
                        return actualValues;
                    }
 
                    @Override
                    public ElnRecordFormulaCalculationResult recalculateAllWithRepeats(
                            String tenantId, ElnRecordEntity actualRecord,
                            ElnRecordRevisionEntity actualRevision,
                            List<ElnRecordFieldValueEntity> actualValues,
                            String operationCode, String operatorUserId) {
                        ElnRecordFormulaCalculationResult result =
                                new ElnRecordFormulaCalculationResult();
                        result.setFieldValues(actualValues);
                        ElnRecordRepeatValueEntity repeat = new ElnRecordRepeatValueEntity();
                        repeat.setJisuanZhuangtai("SUCCESS");
                        result.setRepeatValues(Collections.singletonList(repeat));
                        return result;
                    }
                };
        ElnRecordRepeatValueMapper repeatValueMapper = proxy(
                ElnRecordRepeatValueMapper.class, (method, args) -> {
                    if ("updateById".equals(method)) {
                        repeatUpdates.incrementAndGet();
                        return 1;
                    }
                    return null;
                });
        ElnRecordRecalculationServiceImpl service =
                new ElnRecordRecalculationServiceImpl(
                        recordMapper, revisionMapper, valueMapper, repeatValueMapper,
                        (itemIds, properties) -> successfulTask(), calculation,
                        () -> new ElnRecordRecalculationServiceImpl.UserContext(
                                "tenant-a", "user-1"));
        ElnRecordForms.Recalculate command = new ElnRecordForms.Recalculate();
        command.setExpectedRecordVersion(7);
        command.setOperationCode("operation-1");
 
        ElnRecordVos.Recalculated result = service.recalculateAll("record-1", command);
 
        assertEquals(Integer.valueOf(8), result.getRecordVersion());
        assertEquals(2, result.getSuccessCount());
        assertEquals(1, result.getMissingInputCount());
        assertEquals(1, result.getErrorCount());
        assertEquals(1, casCalls.get());
        assertEquals(3, updates.get());
        assertEquals(1, repeatUpdates.get());
    }
 
    private static ElnRecordFieldValueEntity calculated(String bindingId, String status) {
        ElnRecordFieldValueEntity value = new ElnRecordFieldValueEntity();
        value.setMubanBangdingId(bindingId);
        value.setJisuanZhuangtai(status);
        return value;
    }
 
    private static TestWorkItemSnapshotBatch successfulTask() {
        TestWorkItemSnapshot item = new TestWorkItemSnapshot();
        item.setTestWorkItemId("item-1");
        item.setOutcomeCode("SUCCESS");
        TestWorkItemSnapshotBatch batch = new TestWorkItemSnapshotBatch();
        batch.setItems(Collections.singletonList(item));
        return batch;
    }
 
    @SuppressWarnings("unchecked")
    private static <T> T proxy(Class<T> type, Invocation invocation) {
        return (T) Proxy.newProxyInstance(type.getClassLoader(), new Class<?>[]{type},
                (target, method, args) -> {
                    if (method.getDeclaringClass() == Object.class) {
                        if ("toString".equals(method.getName())) return type.getSimpleName();
                        if ("hashCode".equals(method.getName())) return System.identityHashCode(target);
                        if ("equals".equals(method.getName())) return target == args[0];
                    }
                    Object result = invocation.invoke(method.getName(), args);
                    if (result != null || !method.getReturnType().isPrimitive()) return result;
                    if (method.getReturnType() == boolean.class) return false;
                    return 0;
                });
    }
 
    @FunctionalInterface
    private interface Invocation {
        Object invoke(String method, Object[] args);
    }
}