刘光辉
14 小时以前 34981c30a78e8bbd7791131059a9210f9928b62c
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
package jnpf.bizcommon.audit.service.value;
 
import jnpf.bizcommon.audit.entity.model.AuditFieldDiffViewVO;
 
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
 
/** dictionaryType 无内联 options 时的批量解析与租户边界探针。 */
public final class JnpfDictionaryFieldValueResolverProbe {
 
    private static int passed;
    private static int failed;
 
    public static void main(String[] args) {
        CountingLookup lookup = new CountingLookup();
        JnpfDictionaryFieldValueResolver resolver = new JnpfDictionaryFieldValueResolver(lookup);
        AuditFieldDiffViewVO status = field("biz_status", "", "created");
        AuditFieldDiffViewVO method = field("jiance_fangshi", "", "quanjian");
        AuditFieldDiffViewVO snapshot = field("snapshot", "", "created");
        snapshot.setNewDisplay("事件快照");
        List<AuditFieldDiffViewVO> fields = Arrays.asList(status, method, snapshot);
        Map<String, String> dictionaryTypes = new LinkedHashMap<>();
        dictionaryTypes.put("biz_status", "bizStatus");
        dictionaryTypes.put("jiance_fangshi", "jianceFangshi");
        dictionaryTypes.put("snapshot", "bizStatus");
 
        resolver.resolve(new AuditFieldValueContext(
                "lims_qingyandan", "tenant-a", null, dictionaryTypes), fields);
 
        check("多个 dictionaryType 合并成一次批量查询", lookup.calls == 1,
                String.valueOf(lookup.calls));
        check("字典类型和值整批收集", lookup.typeCount == 2 && lookup.valueCount == 2,
                lookup.typeCount + "/" + lookup.valueCount);
        check("无内联 options 的字典值解析展示", "已创建".equals(status.getNewDisplay())
                && "全检".equals(method.getNewDisplay()),
                status.getNewDisplay() + "/" + method.getNewDisplay());
        check("事件发生时展示快照优先", "事件快照".equals(snapshot.getNewDisplay()),
                snapshot.getNewDisplay());
 
        resolver.resolve(new AuditFieldValueContext(
                        "lims_qingyandan", "", null, dictionaryTypes),
                Collections.singletonList(field("biz_status", "", "created")));
        check("空租户字典解析 fail-closed", lookup.calls == 1, String.valueOf(lookup.calls));
 
        System.out.println("=== JNPF 字典批量解析断言:" + passed + " 通过 / " + failed + " 失败 ===");
        System.exit(failed == 0 ? 0 : 1);
    }
 
    private static AuditFieldDiffViewVO field(String name, String oldData, String newData) {
        AuditFieldDiffViewVO field = new AuditFieldDiffViewVO();
        field.setField(name);
        field.setValueType("enum");
        field.setComponentType("select");
        field.setOldData(oldData);
        field.setNewData(newData);
        return field;
    }
 
    private static final class CountingLookup
            implements JnpfDictionaryFieldValueResolver.DictionaryLookup {
        private int calls;
        private int typeCount;
        private int valueCount;
 
        @Override
        public Map<String, Map<String, String>> labels(
                String tenantId, Set<String> dictionaryTypes, Set<String> values) {
            calls++;
            typeCount = dictionaryTypes.size();
            valueCount = values.size();
            Map<String, Map<String, String>> result = new LinkedHashMap<>();
            result.put("bizStatus", Collections.singletonMap("created", "已创建"));
            result.put("jianceFangshi", Collections.singletonMap("quanjian", "全检"));
            return result;
        }
    }
 
    private static void check(String name, boolean ok, String detail) {
        if (ok) {
            passed++;
            System.out.println("  [PASS] " + name);
        } else {
            failed++;
            System.out.println("  [FAIL] " + name + " -- " + detail);
        }
    }
 
    private JnpfDictionaryFieldValueResolverProbe() {
    }
}