刘光辉
4 小时以前 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
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
package jnpf.elnService.impl;
 
import jnpf.base.ActionResult;
import jnpf.exception.DataException;
import jnpf.lims.api.TestWorkItemSnapshotApi;
import jnpf.lims.api.model.TestWorkItemRequestIdentityActorRequest;
import jnpf.lims.api.model.TestWorkItemRequestIdentityBatch;
import jnpf.lims.api.model.TestWorkItemRequestIdentity;
import jnpf.lims.api.model.TestWorkItemSnapshotActorRequest;
import jnpf.lims.api.model.TestWorkItemSnapshotBatch;
import jnpf.lims.api.model.TestWorkItemSnapshotRequest;
import org.junit.Test;
 
import java.util.Arrays;
import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;
 
import static org.junit.Assert.assertEquals;
 
public class LimsTestRequestIdentityLookupAdapterTest {
 
    @Test
    public void mapsOnlySuccessfulStableIdentitiesAndKeepsTheTenantBoundary() {
        AtomicReference<TestWorkItemRequestIdentityActorRequest> captured =
                new AtomicReference<>();
        TestWorkItemRequestIdentityBatch batch = new TestWorkItemRequestIdentityBatch();
        batch.getItems().add(identity("item-1", "request-1", "SUCCESS"));
        batch.getItems().add(identity("item-2", null, "NOT_FOUND"));
        TestWorkItemSnapshotApi api = api(actor -> {
            captured.set(actor);
            return ActionResult.success("ok", batch);
        });
 
        Map<String, jnpf.elnService.ElnTestRequestIdentityLookup.Identity> result =
                new LimsTestRequestIdentityLookupAdapter(api)
                .resolve("tenant-a", Arrays.asList("item-1", "item-2"));
 
        assertEquals("tenant-a", captured.get().getTenantId());
        assertEquals(Arrays.asList("item-1", "item-2"),
                captured.get().getRequest().getTestWorkItemIds());
        assertEquals(1, result.size());
        assertEquals("request-1", result.get("item-1").getTestRequestId());
        assertEquals("pending_audit", result.get("item-1").getTaskStatus());
    }
 
    @Test(expected = DataException.class)
    public void rejectsRemoteFailureInsteadOfReturningNoMatches() {
        TestWorkItemSnapshotApi api = api(actor -> ActionResult.fail("identity unavailable"));
        new LimsTestRequestIdentityLookupAdapter(api)
                .resolve("tenant-a", Arrays.asList("item-1"));
    }
 
    @Test(expected = DataException.class)
    public void reportsMissingStableIdentityInsteadOfConvertingItToNotFound() {
        TestWorkItemRequestIdentityBatch batch = new TestWorkItemRequestIdentityBatch();
        batch.getItems().add(identity("item-1", null, "TEST_REQUEST_INVALID"));
        TestWorkItemSnapshotApi api = api(actor -> ActionResult.success("ok", batch));
        new LimsTestRequestIdentityLookupAdapter(api)
                .resolve("tenant-a", Arrays.asList("item-1"));
    }
 
    private static TestWorkItemRequestIdentity identity(
            String itemId, String requestId, String outcome) {
        TestWorkItemRequestIdentity item = new TestWorkItemRequestIdentity();
        item.setTestWorkItemId(itemId);
        item.setTestRequestId(requestId);
        item.setTaskStatus("pending_audit");
        item.setOutcomeCode(outcome);
        return item;
    }
 
    private static TestWorkItemSnapshotApi api(IdentityResponder responder) {
        return new TestWorkItemSnapshotApi() {
            @Override
            public ActionResult<TestWorkItemSnapshotBatch> snapshots(
                    TestWorkItemSnapshotRequest request) {
                return ActionResult.fail("unused");
            }
 
            @Override
            public ActionResult<TestWorkItemSnapshotBatch> snapshotsAsActor(
                    TestWorkItemSnapshotActorRequest request) {
                return ActionResult.fail("unused");
            }
 
            @Override
            public ActionResult<TestWorkItemRequestIdentityBatch> requestIdentitiesAsActor(
                    TestWorkItemRequestIdentityActorRequest request) {
                return responder.respond(request);
            }
        };
    }
 
    @FunctionalInterface
    private interface IdentityResponder {
        ActionResult<TestWorkItemRequestIdentityBatch> respond(
                TestWorkItemRequestIdentityActorRequest request);
    }
}