刘光辉
6 小时以前 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
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<String> 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);
    }
}