刘光辉
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
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
package jnpf.limsService.impl;
 
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
import jnpf.limsEntity.BizStatus;
import jnpf.limsEntity.LimsQingyandanEntity;
import jnpf.limsEntity.LimsSxtQuyangZhongzhiShenqingZixiangEntity;
import jnpf.limsMapper.LimsQingyandanMapper;
import org.apache.ibatis.builder.MapperBuilderAssistant;
import org.apache.ibatis.session.Configuration;
 
import java.lang.reflect.Field;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;
 
/**
 * 取样终止审批通过时请验单状态更新的可执行回归检查,无需数据库。
 */
public class LimsSxtQuyangZhongzhiShenqingExecutionChecks {
 
    public static void main(String[] args) throws Exception {
        updatesOnlyActivePendingSxtQingyandanByDirectLink();
    }
 
    private static void updatesOnlyActivePendingSxtQingyandanByDirectLink() throws Exception {
        initializeQingyandanTableInfo();
        AtomicInteger updateCalls = new AtomicInteger();
        AtomicReference<LambdaUpdateWrapper<LimsQingyandanEntity>> captured = new AtomicReference<>();
        LimsQingyandanMapper mapper = mapperProxy(updateCalls, captured);
 
        LimsSxtJihuaServiceImpl service = new LimsSxtJihuaServiceImpl(event -> {
        });
        setField(service, "qingyandanMapper", mapper);
 
        LimsSxtQuyangZhongzhiShenqingZixiangEntity child = new LimsSxtQuyangZhongzhiShenqingZixiangEntity();
        child.setQingyandanId("qingyandan-1");
        child.setFTenantId("tenant-1");
 
        assertTrue(invokeTerminate(service, child));
        assertFalse(invokeTerminate(service, child));
        assertEquals(2, updateCalls.get());
 
        LambdaUpdateWrapper<LimsQingyandanEntity> wrapper = captured.get();
        assertContains(wrapper.getSqlSegment(), "f_id");
        assertContains(wrapper.getSqlSegment(), "f_tenant_id");
        assertContains(wrapper.getSqlSegment(), "biz_type");
        assertContains(wrapper.getSqlSegment(), "quyang_status");
        assertContains(wrapper.getSqlSegment(), "f_delete_mark");
 
        Map<String, Object> values = wrapper.getParamNameValuePairs();
        assertContainsValue(values, "qingyandan-1");
        assertContainsValue(values, "tenant-1");
        assertContainsValue(values, "SXT");
        assertContainsValue(values, BizStatus.PENDING_FETCH.getCode());
        assertContainsValue(values, BizStatus.DELETED.getCode());
        assertContainsValue(values, 1);
    }
 
    private static void initializeQingyandanTableInfo() {
        if (TableInfoHelper.getTableInfo(LimsQingyandanEntity.class) == null) {
            TableInfoHelper.initTableInfo(new MapperBuilderAssistant(new Configuration(), ""),
                    LimsQingyandanEntity.class);
        }
    }
 
    @SuppressWarnings("unchecked")
    private static LimsQingyandanMapper mapperProxy(AtomicInteger updateCalls,
                                                     AtomicReference<LambdaUpdateWrapper<LimsQingyandanEntity>> captured) {
        InvocationHandler handler = (proxy, method, args) -> {
            if ("update".equals(method.getName())) {
                captured.set((LambdaUpdateWrapper<LimsQingyandanEntity>) args[1]);
                return updateCalls.getAndIncrement() == 0 ? 1 : 0;
            }
            if ("toString".equals(method.getName())) {
                return "LimsQingyandanMapperProxy";
            }
            if ("hashCode".equals(method.getName())) {
                return System.identityHashCode(proxy);
            }
            if ("equals".equals(method.getName())) {
                return proxy == args[0];
            }
            throw new UnsupportedOperationException(method.getName());
        };
        return (LimsQingyandanMapper) Proxy.newProxyInstance(
                LimsQingyandanMapper.class.getClassLoader(), new Class<?>[]{LimsQingyandanMapper.class}, handler);
    }
 
    private static boolean invokeTerminate(LimsSxtJihuaServiceImpl service,
                                           LimsSxtQuyangZhongzhiShenqingZixiangEntity child) throws Exception {
        Method method = LimsSxtJihuaServiceImpl.class.getDeclaredMethod("terminatePendingQingyandan",
                LimsSxtQuyangZhongzhiShenqingZixiangEntity.class);
        method.setAccessible(true);
        return (Boolean) method.invoke(service, child);
    }
 
    private static void setField(Object target, String name, Object value) throws Exception {
        Field field = target.getClass().getDeclaredField(name);
        field.setAccessible(true);
        field.set(target, value);
    }
 
    private static void assertContains(String source, String expected) {
        if (!source.contains(expected)) {
            throw new AssertionError("expected SQL to contain " + expected + ", actual=" + source);
        }
    }
 
    private static void assertContainsValue(Map<String, Object> values, Object expected) {
        if (!values.containsValue(expected)) {
            throw new AssertionError("expected parameters to contain " + expected + ", actual=" + values);
        }
    }
 
    private static void assertEquals(Object expected, Object actual) {
        if (!expected.equals(actual)) {
            throw new AssertionError("expected=" + expected + ", actual=" + actual);
        }
    }
 
    private static void assertTrue(boolean value) {
        if (!value) {
            throw new AssertionError("expected true");
        }
    }
 
    private static void assertFalse(boolean value) {
        if (value) {
            throw new AssertionError("expected false");
        }
    }
}