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"));
|
}
|
}
|