package jnpf.limsService.impl; import org.junit.Test; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import static org.junit.Assert.assertTrue; /** Locks the submit-result freeze gate and rollback boundary in source order. */ public class LimsElnFreezeWiringContractTest { @Test public void freezeGateRunsBeforeTheFirstLimsUpdate() throws Exception { String source = read("jnpf-lims-biz/src/main/java/jnpf/limsService/impl/" + "LimsJianceTaskServiceImpl.java"); String method = method(source, "public LimsSubmitResultOutcome submitJianyanResult(", "\n @Override"); int gate = method.indexOf("elnRecordFreezeGate.prepareAndReleaseAfterCompletion"); int update = method.indexOf("limsJianyanXiangMapper.submitResult("); assertTrue("ELN freeze gate must be present in submit-result", gate >= 0); assertTrue("ELN freeze gate must run before the first LIMS update", update >= 0 && gate < update); assertTrue("assigned recorder and external-test routing must reach the result update", method.contains("item.getId(), currentUserId, isWaijian,")); assertTrue("ELN ready values must be resolved through the freeze gate", method.contains("elnSubmissionValues.get(item.getId())")); assertTrue("LIMS result update must use the ELN-ready value", method.contains("submissionValues.jianyanJieguo")); assertTrue("LIMS conclusion update must use the ELN-ready value", method.contains("submissionValues.jianyanJielun")); } @Test public void controllerMarksBusinessFailureForRollback() throws Exception { String source = read("jnpf-lims-controller/src/main/java/jnpf/limsController/" + "LimsJianceTaskController.java"); String method = method(source, "public R submitResult(", "\n /**"); int serviceCall = method.indexOf("limsJianceTaskService.submitJianyanResult"); int rollback = method.indexOf( "TransactionAspectSupport.currentTransactionStatus().setRollbackOnly()"); int error = method.indexOf("return R.error(e.getMessage())", serviceCall); assertTrue("submit-result must call the transactional service", serviceCall >= 0); assertTrue("caught business failures must mark the transaction rollback-only", rollback > serviceCall && error > rollback); } private static String method(String source, String signature, String nextMarker) { int start = source.indexOf(signature); int end = source.indexOf(nextMarker, start + signature.length()); assertTrue("method signature missing: " + signature, start >= 0); return source.substring(start, end > start ? end : source.length()); } private static String read(String relative) throws Exception { Path module = Paths.get(System.getProperty("user.dir")).toAbsolutePath(); Path lims = "jnpf-lims-biz".equals(module.getFileName().toString()) ? module.getParent() : module.resolve("jnpf-lims"); return new String(Files.readAllBytes(lims.resolve(relative)), StandardCharsets.UTF_8); } }