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");
|
}
|
}
|
}
|