刘光辉
7 小时以前 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
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
package jnpf.limsController;
 
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
import jnpf.base.ActionResult;
import jnpf.base.UserInfo;
import jnpf.lims.api.TestWorkItemSnapshotApi;
import jnpf.lims.api.model.TestWorkItemSnapshotActorRequest;
import jnpf.lims.api.model.TestWorkItemSnapshotBatch;
import jnpf.lims.api.model.TestWorkItemSnapshot;
import jnpf.lims.api.model.TestWorkItemSnapshotProperty;
import jnpf.lims.api.model.TestWorkItemSnapshotRequest;
import jnpf.lims.api.model.TestWorkItemRequestIdentityActorRequest;
import jnpf.lims.api.model.TestWorkItemRequestIdentityBatch;
import jnpf.limsService.LimsTestWorkItemSnapshotService;
import org.junit.Test;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.PostMapping;
 
import java.util.Arrays;
import java.util.concurrent.atomic.AtomicReference;
 
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.fail;
 
public class LimsTestWorkItemSnapshotControllerTest {
 
    @Test
    public void passesAuthenticatedUserAndFallbackTenantToSnapshotService() {
        UserInfo user = new UserInfo();
        user.setUserId("user-a");
        user.setTenantId("");
        TestWorkItemSnapshotBatch expected = new TestWorkItemSnapshotBatch();
        AtomicReference<String> tenant = new AtomicReference<>();
        AtomicReference<String> userId = new AtomicReference<>();
        LimsTestWorkItemSnapshotService service = (request, currentTenant, currentUser) -> {
            tenant.set(currentTenant);
            userId.set(currentUser);
            return expected;
        };
 
        ActionResult<TestWorkItemSnapshotBatch> actual = new LimsTestWorkItemSnapshotController(
                () -> user, service, actor -> expected,
                actor -> new TestWorkItemRequestIdentityBatch())
                .snapshots(new TestWorkItemSnapshotRequest());
 
        assertSame(expected, actual.getData());
        assertEquals("0", tenant.get());
        assertEquals("user-a", userId.get());
    }
 
    @Test
    public void controllerAndFeignShareTheInternalSnapshotPath() throws Exception {
        FeignClient feign = TestWorkItemSnapshotApi.class.getAnnotation(FeignClient.class);
        assertEquals("jnpf-lims", feign.name());
        assertEquals("/Internal/lims/testWorkItems", feign.path());
        assertEquals("/snapshots", TestWorkItemSnapshotApi.class.getMethod(
                "snapshots", TestWorkItemSnapshotRequest.class).getAnnotation(PostMapping.class).value()[0]);
        assertEquals("/snapshots", LimsTestWorkItemSnapshotController.class.getMethod(
                "snapshots", TestWorkItemSnapshotRequest.class).getAnnotation(PostMapping.class).value()[0]);
        assertEquals("/snapshots/asActor", TestWorkItemSnapshotApi.class.getMethod(
                "snapshotsAsActor", TestWorkItemSnapshotActorRequest.class).getAnnotation(PostMapping.class).value()[0]);
        assertEquals("/requestIdentities/asActor", TestWorkItemSnapshotApi.class.getMethod(
                "requestIdentitiesAsActor", TestWorkItemRequestIdentityActorRequest.class)
                .getAnnotation(PostMapping.class).value()[0]);
        assertEquals("/requestIdentities/asActor",
                LimsTestWorkItemSnapshotController.class.getMethod(
                        "requestIdentitiesAsActor", TestWorkItemRequestIdentityActorRequest.class)
                        .getAnnotation(PostMapping.class).value()[0]);
    }
 
    @Test
    public void snapshotContractKeepsLowerCamelJsonAcrossLimsAndFeignMappers() throws Exception {
        TestWorkItemSnapshotRequest request = new TestWorkItemSnapshotRequest();
        request.setTestWorkItemIds(Arrays.asList("item-1"));
        request.setProperties(Arrays.asList("test.basis"));
        TestWorkItemSnapshotActorRequest actor = new TestWorkItemSnapshotActorRequest();
        actor.setTenantId("tenant-1");
        actor.setUserId("user-1");
        actor.setRequest(request);
 
        TestWorkItemSnapshotProperty property = new TestWorkItemSnapshotProperty();
        property.setDataType("TEXT");
        property.setSourceRef("item-1");
        property.setOutcomeCode("SUCCESS");
        TestWorkItemSnapshot item = new TestWorkItemSnapshot();
        item.setTestWorkItemId("item-1");
        item.setTestRequestId("request-1");
        item.setTestDefinitionId("project-1");
        item.setTestDefinitionCode("HD_jyxm_0008");
        item.setTestDefinitionName("pH值");
        item.setTaskStatus("pending_test");
        item.setOutcomeCode("SUCCESS");
        item.getProperties().add(property);
        TestWorkItemSnapshotBatch batch = new TestWorkItemSnapshotBatch();
        batch.getItems().add(item);
 
        ObjectMapper limsMapper = new ObjectMapper()
                .setPropertyNamingStrategy(PropertyNamingStrategies.SNAKE_CASE);
        JsonNode actorJson = limsMapper.readTree(limsMapper.writeValueAsString(actor));
        JsonNode batchJson = limsMapper.readTree(limsMapper.writeValueAsString(batch));
 
        assertEquals("item-1", actorJson.at("/request/testWorkItemIds/0").asText());
        assertEquals("tenant-1", actorJson.path("tenantId").asText());
        assertEquals("item-1", batchJson.at("/items/0/testWorkItemId").asText());
        assertEquals("request-1", batchJson.at("/items/0/testRequestId").asText());
        assertEquals("project-1", batchJson.at("/items/0/testDefinitionId").asText());
        assertEquals("HD_jyxm_0008", batchJson.at("/items/0/testDefinitionCode").asText());
        assertEquals("pH值", batchJson.at("/items/0/testDefinitionName").asText());
        assertEquals("pending_test", batchJson.at("/items/0/taskStatus").asText());
        assertEquals("SUCCESS", batchJson.at("/items/0/outcomeCode").asText());
        assertEquals("TEXT", batchJson.at("/items/0/properties/0/dataType").asText());
        assertEquals("item-1", batchJson.at("/items/0/properties/0/sourceRef").asText());
 
        ObjectMapper feignMapper = new ObjectMapper();
        TestWorkItemSnapshotActorRequest decodedActor = feignMapper.readValue(
                limsMapper.writeValueAsString(actor), TestWorkItemSnapshotActorRequest.class);
        TestWorkItemSnapshotBatch decodedBatch = feignMapper.readValue(
                limsMapper.writeValueAsString(batch), TestWorkItemSnapshotBatch.class);
        assertEquals("item-1", decodedActor.getRequest().getTestWorkItemIds().get(0));
        assertEquals("item-1", decodedBatch.getItems().get(0).getTestWorkItemId());
        assertEquals("request-1", decodedBatch.getItems().get(0).getTestRequestId());
        assertEquals("HD_jyxm_0008", decodedBatch.getItems().get(0).getTestDefinitionCode());
        assertEquals("pH值", decodedBatch.getItems().get(0).getTestDefinitionName());
        assertEquals("SUCCESS", decodedBatch.getItems().get(0).getProperties().get(0).getOutcomeCode());
    }
 
}