刘光辉
5 小时以前 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
package jnpf.elnService;
 
import org.junit.Test;
 
import java.util.Arrays;
 
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertNotNull;
 
public class ElnFieldProviderCatalogTest {
 
    @Test
    public void failedRemoteCatalogDoesNotFallBackToLegacyProperties() {
        jnpf.lims.api.ElnSourceCatalogApi remote = org.mockito.Mockito.mock(jnpf.lims.api.ElnSourceCatalogApi.class);
        ElnFieldProviderCatalog catalog = new DefaultElnFieldProviderCatalog(remote);
        org.mockito.Mockito.when(remote.references()).thenReturn(null);
        assertThrows(jnpf.exception.DataException.class, () -> catalog.list("LIMS_TASK"));
        assertThrows(jnpf.exception.DataException.class,
                () -> catalog.reference("LIMS_TASK", "test.basis", 1));
        org.mockito.Mockito.when(remote.references()).thenReturn(jnpf.base.ActionResult.fail("Unavailable"));
        org.mockito.Mockito.when(remote.reference("test.basis", 1)).thenReturn(jnpf.base.ActionResult.fail("Unavailable"));
        assertThrows(jnpf.exception.DataException.class, () -> catalog.list("LIMS_TASK"));
        assertThrows(jnpf.exception.DataException.class,
                () -> catalog.reference("LIMS_TASK", "test.basis", 1));
        org.mockito.Mockito.when(remote.reference("test.basis", 1)).thenThrow(new IllegalStateException("Offline"));
        assertThrows(IllegalStateException.class,
                () -> catalog.reference("LIMS_TASK", "test.basis", 1));
        assertEquals(4, catalog.list("SYSTEM").size());
    }
 
    @Test
    public void rejectsRemoteReferencesForAnotherPropertyOrRevision() {
        jnpf.lims.api.ElnSourceCatalogApi remote = org.mockito.Mockito.mock(jnpf.lims.api.ElnSourceCatalogApi.class);
        ElnFieldProviderCatalog catalog = new DefaultElnFieldProviderCatalog(remote);
        jnpf.lims.api.model.ElnSourceReference wrong = new jnpf.lims.api.model.ElnSourceReference();
        wrong.setPropertyCode("test.basis");
        wrong.setRevision(2);
        wrong.setDataType("TEXT");
        wrong.setEnabled(true);
        org.mockito.Mockito.when(remote.reference("test.basis", 1)).thenReturn(jnpf.base.ActionResult.success("OK", wrong));
        assertThrows(jnpf.exception.DataException.class,
                () -> catalog.reference("LIMS_TASK", "test.basis", 1));
        wrong.setPropertyCode("sample.name");
        wrong.setRevision(1);
        assertThrows(jnpf.exception.DataException.class,
                () -> catalog.reference("LIMS_TASK", "test.basis", 1));
    }
 
    private static void assertThrows(Class<? extends RuntimeException> type, Runnable operation) {
        try {
            operation.run();
            org.junit.Assert.fail("Invalid remote catalog response must be rejected");
        } catch (RuntimeException expected) {
            assertTrue(expected.toString(), type.isInstance(expected));
        }
    }
 
    @Test
    public void describesThePinnedDisabledVersionWithoutReplacingItWithCurrentChoices() {
        jnpf.lims.api.ElnSourceCatalogApi remote = org.mockito.Mockito.mock(jnpf.lims.api.ElnSourceCatalogApi.class);
        jnpf.lims.api.model.ElnSourceReference pinned = new jnpf.lims.api.model.ElnSourceReference();
        pinned.setPropertyCode("sample.received_at");
        pinned.setRevision(1);
        pinned.setDisplayName("Received at V1");
        pinned.setDataType("DATETIME");
        pinned.setEnabled(false);
        org.mockito.Mockito.when(remote.references()).thenReturn(jnpf.base.ActionResult.success("OK", java.util.Collections.emptyList()));
        org.mockito.Mockito.when(remote.reference("sample.received_at", 1)).thenReturn(jnpf.base.ActionResult.success("OK", pinned));
        ElnFieldProviderCatalog catalog = new DefaultElnFieldProviderCatalog(remote);
 
        ElnFieldProviderCatalog.Property property = catalog.reference("LIMS_TASK", "sample.received_at", 1);
 
        assertNotNull(property);
        assertEquals("Received at V1", property.getLabel());
        assertEquals(Integer.valueOf(1), property.getPropertyRevision());
        assertFalse(property.isEnabled());
        assertEquals(0, catalog.list("LIMS_TASK").size());
        assertNotNull(catalog.reference("LIMS_TASK", "test.basis", null));
    }
 
    @Test
    public void loadsConfigurableLimsChoicesWhileKeepingSystemPropertiesLocal() {
        jnpf.lims.api.ElnSourceCatalogApi remote = org.mockito.Mockito.mock(jnpf.lims.api.ElnSourceCatalogApi.class);
        jnpf.lims.api.model.ElnSourceReference configured = new jnpf.lims.api.model.ElnSourceReference();
        configured.setPropertyCode("sample.received_at");
        configured.setRevision(2);
        configured.setDisplayName("Received at");
        configured.setDataType("DATETIME");
        configured.setEnabled(true);
        org.mockito.Mockito.when(remote.references()).thenReturn(jnpf.base.ActionResult.success("OK",
                java.util.Collections.singletonList(configured)));
        try (org.springframework.context.annotation.AnnotationConfigApplicationContext context =
                     new org.springframework.context.annotation.AnnotationConfigApplicationContext()) {
            context.registerBean(jnpf.lims.api.ElnSourceCatalogApi.class, () -> remote);
            context.register(DefaultElnFieldProviderCatalog.class);
            context.refresh();
            ElnFieldProviderCatalog catalog = context.getBean(ElnFieldProviderCatalog.class);
            assertEquals(1, catalog.list("LIMS_TASK").size());
            ElnFieldProviderCatalog.Property item = catalog.list("LIMS_TASK").get(0);
            assertEquals("sample.received_at", item.getProperty());
            assertEquals(Integer.valueOf(2), item.getPropertyRevision());
            assertTrue(item.isEnabled());
            assertTrue(catalog.contains("SYSTEM", "current_date"));
            org.mockito.Mockito.when(remote.references()).thenThrow(new IllegalStateException("Source offline"));
            assertEquals(4, catalog.list("SYSTEM").size());
        }
    }
 
    @Test
    public void limsTaskCatalogContainsConfirmedTaskProperties() {
        ElnFieldProviderCatalog catalog = new DefaultElnFieldProviderCatalog();
 
        assertEquals(Arrays.asList("test.basis", "sample.name", "sample.batch_no", "sample.production_date",
                        "quality_standard.source", "sample.specification", "test.type"), catalog.list("LIMS_TASK").stream()
                .map(ElnFieldProviderCatalog.Property::getProperty)
                .collect(java.util.stream.Collectors.toList()));
        assertFalse(catalog.contains("LIMS_TASK", "current_date"));
        assertEquals(true, catalog.contains("SYSTEM", "current_date"));
    }
}