刘光辉
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
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
package jnpf.limsService.provider;
 
import org.junit.Test;
import jnpf.exception.DataException;
 
import java.util.Collections;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.OffsetDateTime;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors;
 
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
 
public class LimsElnSourceCatalogTest {
 
    @Test
    public void savesANewSourceDraftWithoutMakingItSelectable() {
        LimsElnSourceCatalog catalog = catalog(newStore());
 
        LimsElnSourceCatalog.Version draft = catalog.createDraft(input("sample.original_batch"));
 
        assertEquals("DRAFT", draft.getStatus());
        assertEquals("sample.original_batch", catalog.detail(draft.getId()).getPropertyCode());
        assertEquals("originalBatch", catalog.definition(draft.getId()).getFieldId());
        assertTrue(catalog.catalog().isEmpty());
    }
 
    @Test
    public void publishesOnlyAfterReadingAnAuthorizedSampleAndFreezesTheDefinition() {
        LimsElnSourceCatalog catalog = new LimsElnSourceCatalog(newStore(),
                () -> new LimsElnSourceCatalog.Actor("0", "field-manager", true),
                (definition, actor, sampleId) -> {
                    assertEquals("work-item-a", sampleId);
                    assertEquals("originalBatch", definition.getFieldId());
                    return new LimsElnSourceCatalog.Preview(Collections.singletonList("ORIGINAL-001"),
                            "{\"root\":\"task\",\"field\":\"pihao_yuanchang\",\"metadataRevision\":1}");
                });
        LimsElnSourceCatalog.Version draft = catalog.createDraft(input("sample.original_batch"));
 
        LimsElnSourceCatalog.Version published = catalog.publish(draft.getId(), 0, "work-item-a");
 
        assertEquals("PUBLISHED", published.getStatus());
        assertEquals(published, catalog.catalog().get(0));
        assertEquals("work-item-a", published.getValidationSampleId());
        assertEquals("field-manager", published.getPublishedBy());
        assertEquals("{\"root\":\"task\",\"field\":\"pihao_yuanchang\",\"metadataRevision\":1}",
                published.getFrozenDefinitionJson());
    }
 
    @Test
    public void doesNotReuseAnExistingPropertyIdentityAsANewSource() {
        LimsElnSourceCatalog catalog = catalog(newStore());
        catalog.createDraft(input("sample.original_batch"));
 
        try {
            catalog.createDraft(input("sample.original_batch"));
            fail("A stable property code must not be reused");
        } catch (DataException expected) {
            assertEquals("Source property already exists", expected.getMessage());
        }
    }
 
    @Test
    public void newPublicationKeepsTheOldDefinitionAndOnlyOffersTheLatestVersion() {
        LimsElnSourceCatalog catalog = publishingCatalog(newStore());
        LimsElnSourceCatalog.Version first = catalog.publish(
                catalog.createDraft(input("sample.original_batch")).getId(), 0, "work-item-a");
        LimsElnSourceCatalog.Draft changed = input("sample.original_batch");
        changed.getDefinition().setFieldId("supplierBatch");
 
        LimsElnSourceCatalog.Version next = catalog.newVersion(first.getId(), changed);
        assertEquals(first, catalog.catalog().get(0));
        LimsElnSourceCatalog.Version second = catalog.publish(next.getId(), 0, "work-item-a");
 
        assertEquals(2, second.getRevision());
        assertEquals(Collections.singletonList(second), catalog.catalog());
        assertEquals(first, catalog.published("sample.original_batch", 1));
        assertEquals("originalBatch", catalog.definition(first.getId()).getFieldId());
        assertEquals("supplierBatch", catalog.definition(second.getId()).getFieldId());
    }
 
    @Test
    public void disablingRemovesNewChoicesButKeepsPublishedVersionsReadable() {
        LimsElnSourceCatalog catalog = publishingCatalog(newStore());
        LimsElnSourceCatalog.Version published = catalog.publish(
                catalog.createDraft(input("sample.original_batch")).getId(), 0, "work-item-a");
 
        catalog.setEnabled("sample.original_batch", false);
 
        assertTrue(catalog.catalog().isEmpty());
        assertEquals(published, catalog.published("sample.original_batch", 1));
        catalog.setEnabled("sample.original_batch", true);
        assertEquals(Collections.singletonList(published), catalog.catalog());
    }
 
    @Test
    public void updatesDraftsWithOptimisticLockingAndNeverUpdatesPublishedDefinitions() {
        LimsElnSourceCatalog catalog = publishingCatalog(newStore());
        LimsElnSourceCatalog.Version draft = catalog.createDraft(input("sample.original_batch"));
        LimsElnSourceCatalog.Draft changed = input("sample.original_batch");
        changed.getDefinition().setFieldId("supplierBatch");
 
        LimsElnSourceCatalog.Version updated = catalog.updateDraft(draft.getId(), 0, changed);
 
        assertEquals(1, updated.getEditVersion());
        assertEquals("supplierBatch", catalog.definition(draft.getId()).getFieldId());
        try {
            catalog.updateDraft(draft.getId(), 0, input("sample.original_batch"));
            fail("Stale editors must not overwrite a draft");
        } catch (DataException expected) {
            assertEquals("Source version has changed", expected.getMessage());
        }
        LimsElnSourceCatalog.Version published = catalog.publish(draft.getId(), updated.getEditVersion(), "work-item-a");
        try {
            catalog.updateDraft(draft.getId(), published.getEditVersion(), changed);
            fail("A published source must not be edited in place");
        } catch (DataException expected) {
            assertEquals("Published source is immutable", expected.getMessage());
        }
        assertEquals(published, catalog.published("sample.original_batch", 1));
    }
 
    @Test
    public void recordCallersCanResolveFixedVersionsWithoutReceivingMaintenancePermission() {
        LimsElnSourceCatalog.Store store = newStore();
        LimsElnSourceCatalog manager = publishingCatalog(store);
        LimsElnSourceCatalog.Version published = manager.publish(
                manager.createDraft(input("sample.original_batch")).getId(), 0, "work-item-a");
        LimsElnSourceCatalog recordCaller = new LimsElnSourceCatalog(store,
                () -> new LimsElnSourceCatalog.Actor("0", "inspector", false));
 
        assertEquals(published, recordCaller.published("sample.original_batch", 1));
 
        try {
            recordCaller.createDraft(input("sample.other"));
            fail("A record caller must not gain field management permission");
        } catch (DataException expected) {
            assertEquals("Field management permission required", expected.getMessage());
        }
    }
 
    @Test
    public void validatesNativeScalarTypesWithoutReinterpretingTextOrRoundingNumbers() {
        Map<String, Object> samples = new LinkedHashMap<>();
        samples.put("DECIMAL", new BigDecimal("1.234567890123456789"));
        samples.put("DATE", LocalDate.of(2026, 9, 5));
        samples.put("DATETIME", OffsetDateTime.parse("2026-09-05T12:30:00+08:00"));
        samples.put("BOOLEAN", Boolean.TRUE);
        for (Map.Entry<String, Object> sample : samples.entrySet()) {
            LimsElnSourceCatalog catalog = new LimsElnSourceCatalog(newStore(),
                    () -> new LimsElnSourceCatalog.Actor("0", "field-manager", true),
                    (definition, actor, sampleId) -> new LimsElnSourceCatalog.Preview(
                            Collections.singletonList(sample.getValue()), "{\"metadataRevision\":1}"));
            LimsElnSourceCatalog.Draft input = input("sample.typed_value_" + sample.getKey());
            LimsElnSourceCatalog.Version draft = catalog.createDraft(new LimsElnSourceCatalog.Draft(
                    input.getPropertyCode(), input.getDisplayName(), sample.getKey(), input.getDefinition()));
 
            assertEquals(sample.getKey(), catalog.publish(draft.getId(), 0, "work-item-a").getDataType());
        }
    }
 
    @Test
    public void blankSampleValuesDoNotCountAsSuccessfulPublicationValidation() {
        LimsElnSourceCatalog catalog = new LimsElnSourceCatalog(newStore(),
                () -> new LimsElnSourceCatalog.Actor("0", "field-manager", true),
                (definition, actor, sampleId) -> new LimsElnSourceCatalog.Preview(
                        Collections.singletonList("  "), "{\"metadataRevision\":1}"));
        LimsElnSourceCatalog.Version draft = catalog.createDraft(input("sample.original_batch"));
 
        try {
            catalog.publish(draft.getId(), 0, "work-item-a");
            fail("Blank data is not a successful sample");
        } catch (DataException expected) {
            assertEquals("Source preview must resolve one value", expected.getMessage());
        }
        assertEquals(draft, catalog.detail(draft.getId()));
        assertTrue(catalog.catalog().isEmpty());
    }
 
    @Test
    public void anOldEditorCannotPublishSomeoneElsesUpdatedDraft() {
        LimsElnSourceCatalog catalog = publishingCatalog(newStore());
        LimsElnSourceCatalog.Version draft = catalog.createDraft(input("sample.original_batch"));
        LimsElnSourceCatalog.Draft changed = input("sample.original_batch");
        changed.getDefinition().setFieldId("supplierBatch");
        LimsElnSourceCatalog.Version updated = catalog.updateDraft(draft.getId(), 0, changed);
 
        try {
            catalog.publish(draft.getId(), 0, "work-item-a");
            fail("Publication must check the editor version before reading data");
        } catch (DataException expected) {
            assertEquals("Source version has changed", expected.getMessage());
        }
        assertEquals(updated, catalog.detail(draft.getId()));
    }
 
    @Test
    public void failedOrAmbiguousPreviewsNeverPublishTheDraft() {
        List<LimsElnSourceCatalog.SourceReader> readers = Arrays.asList(
                (definition, actor, sampleId) -> { throw new DataException("Sample is not authorized"); },
                (definition, actor, sampleId) -> new LimsElnSourceCatalog.Preview(
                        Arrays.asList("same-value", "same-value"), "{\"metadataRevision\":1}"),
                (definition, actor, sampleId) -> new LimsElnSourceCatalog.Preview(
                        Collections.singletonList(123), "{\"metadataRevision\":1}"),
                (definition, actor, sampleId) -> new LimsElnSourceCatalog.Preview(
                        Collections.singletonList("value"), " "),
                (definition, actor, sampleId) -> new LimsElnSourceCatalog.Preview(
                        Collections.singletonList("value"), "{}"));
        for (int i = 0; i < readers.size(); i++) {
            LimsElnSourceCatalog catalog = new LimsElnSourceCatalog(newStore(),
                    () -> new LimsElnSourceCatalog.Actor("0", "field-manager", true), readers.get(i));
            LimsElnSourceCatalog.Version draft = catalog.createDraft(input("sample.invalid_" + i));
            try {
                catalog.publish(draft.getId(), 0, "work-item-a");
                fail("Unsuccessful validation must never publish a source");
            } catch (DataException expected) {
                assertEquals(draft, catalog.detail(draft.getId()));
            }
            assertTrue(catalog.catalog().isEmpty());
        }
    }
 
    @Test
    public void draftMetadataMustHaveABusinessNameAndASupportedScalarType() {
        LimsElnSourceCatalog catalog = catalog(newStore());
        LimsElnSourceCatalog.Draft source = input("sample.original_batch");
        for (LimsElnSourceCatalog.Draft invalid : Arrays.asList(
                new LimsElnSourceCatalog.Draft(source.getPropertyCode(), " ", "TEXT", source.getDefinition()),
                new LimsElnSourceCatalog.Draft(source.getPropertyCode(), "Name", "SCRIPT", source.getDefinition()))) {
            try {
                catalog.createDraft(invalid);
                fail("Invalid metadata must be rejected before persistence");
            } catch (DataException expected) {
                assertTrue(catalog.catalog().isEmpty());
            }
        }
        assertEquals("DRAFT", catalog.createDraft(source).getStatus());
    }
 
    @Test
    public void editingDuringPreviewInvalidatesThePendingPublication() {
        AtomicReference<LimsElnSourceCatalog> reference = new AtomicReference<>();
        AtomicReference<String> draftId = new AtomicReference<>();
        LimsElnSourceCatalog catalog = new LimsElnSourceCatalog(newStore(),
                () -> new LimsElnSourceCatalog.Actor("0", "field-manager", true),
                (definition, actor, sampleId) -> {
                    LimsElnSourceCatalog.Draft changed = input("sample.original_batch");
                    changed.getDefinition().setFieldId("supplierBatch");
                    reference.get().updateDraft(draftId.get(), 0, changed);
                    return new LimsElnSourceCatalog.Preview(
                            Collections.singletonList("ORIGINAL-001"), "{\"metadataRevision\":1}");
                });
        reference.set(catalog);
        draftId.set(catalog.createDraft(input("sample.original_batch")).getId());
 
        try {
            catalog.publish(draftId.get(), 0, "work-item-a");
            fail("Validation of an old definition must not publish an updated draft");
        } catch (DataException expected) {
            assertEquals("Source version has changed", expected.getMessage());
        }
        assertEquals("supplierBatch", catalog.definition(draftId.get()).getFieldId());
        assertTrue(catalog.catalog().isEmpty());
    }
 
    private static LimsElnSourceCatalog publishingCatalog(LimsElnSourceCatalog.Store store) {
        return new LimsElnSourceCatalog(store,
                () -> new LimsElnSourceCatalog.Actor("0", "field-manager", true),
                (definition, actor, sampleId) -> new LimsElnSourceCatalog.Preview(
                        Collections.singletonList("ORIGINAL-001"), "{\"metadataRevision\":1}"));
    }
 
    private static LimsElnSourceCatalog catalog(LimsElnSourceCatalog.Store store) {
        return new LimsElnSourceCatalog(store,
                () -> new LimsElnSourceCatalog.Actor("0", "field-manager", true));
    }
 
    private static LimsElnSourceCatalog.Draft input(String code) {
        LimsElnSourceCatalog.Definition definition = new LimsElnSourceCatalog.Definition();
        definition.setObjectId("testRequest");
        definition.setFieldId("originalBatch");
        return new LimsElnSourceCatalog.Draft(code, "Original batch", "TEXT", definition);
    }
 
    protected LimsElnSourceCatalog.Store newStore() {
        return new MemoryStore();
    }
 
    private static final class MemoryStore implements LimsElnSourceCatalog.Store {
        private final Map<String, LimsElnSourceCatalog.Version> versions = new LinkedHashMap<>();
        private final Map<String, Boolean> enabled = new LinkedHashMap<>();
 
        @Override
        public boolean enabled(String tenantId, String code) {
            return enabled.getOrDefault(tenantId + ":" + code, true);
        }
 
        @Override
        public Map<String, Boolean> enabledStates(String tenantId) {
            Map<String, Boolean> result = new LinkedHashMap<>();
            versions.values().stream().filter(version -> tenantId.equals(version.getTenantId()))
                    .forEach(version -> result.put(version.getPropertyCode(), enabled(tenantId, version.getPropertyCode())));
            return result;
        }
 
        @Override
        public void setEnabled(String tenantId, String code, boolean value) {
            enabled.put(tenantId + ":" + code, value);
        }
 
        @Override
        public void insert(LimsElnSourceCatalog.Version version) {
            versions.put(version.getId(), version);
        }
 
        @Override
        public boolean replaceDraft(LimsElnSourceCatalog.Version expected,
                                    LimsElnSourceCatalog.Version replacement) {
            return versions.replace(expected.getId(), expected, replacement);
        }
 
        @Override
        public LimsElnSourceCatalog.Version find(String tenantId, String id) {
            LimsElnSourceCatalog.Version version = versions.get(id);
            return version != null && tenantId.equals(version.getTenantId()) ? version : null;
        }
 
        @Override
        public List<LimsElnSourceCatalog.Version> list(String tenantId) {
            return versions.values().stream().filter(v -> tenantId.equals(v.getTenantId()))
                    .collect(Collectors.toList());
        }
    }
}