package jnpf.elnService.template.impl; import jnpf.elnEntity.template.ElnTemplateRepeatGroupEntity; import jnpf.elnEntity.template.ElnTemplateVos; import jnpf.exception.DataException; import org.junit.Test; import java.util.ArrayList; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import static org.junit.Assert.assertEquals; import static org.junit.Assert.fail; public class ElnTemplateRepeatGroupServiceImplTest { @Test public void createsDefaultsAndRejectsConflictingOrInvalidConstraints() { MemoryStore store = new MemoryStore(); ElnTemplateRepeatGroupServiceImpl service = new ElnTemplateRepeatGroupServiceImpl(store, () -> "group-id"); ElnTemplateVos.RepeatGroup created = service.ensureConfigured( "tenant-1", "template-1", "results", null, null, "step-1", "user-1"); assertEquals("results", created.getGroupCode()); assertEquals(Integer.valueOf(0), created.getMinRows()); assertEquals(Integer.valueOf(100), created.getMaxRows()); assertEquals("step-1", created.getStepId()); assertEquals(1, store.groups.size()); ElnTemplateVos.RepeatGroup retried = service.ensureConfigured( "tenant-1", "template-1", "results", 0, 100, "step-1", "user-1"); assertEquals(created, retried); expectFailure(() -> service.ensureConfigured( "tenant-1", "template-1", "results", 1, 100, "step-1", "user-1")); expectFailure(() -> service.ensureConfigured( "tenant-1", "template-1", "invalid code", 0, 100, "step-1", "user-1")); expectFailure(() -> service.ensureConfigured( "tenant-1", "template-1", "other", 5, 4, "step-1", "user-1")); expectFailure(() -> service.ensureConfigured( "tenant-1", "template-1", "other", 0, 1001, "step-1", "user-1")); expectFailure(() -> service.ensureConfigured( "tenant-1", "template-1", "results", 0, 100, "step-2", "user-1")); } @Test public void copiesVersionScopedConstraintsIdempotently() { MemoryStore store = new MemoryStore(); store.add(group("source-1", "tenant-1", "template-v1", "results", 2, 20)); store.add(group("source-2", "tenant-1", "template-v1", "samples", 0, 10)); ElnTemplateRepeatGroupServiceImpl service = new ElnTemplateRepeatGroupServiceImpl(store, new SequenceIds()); service.afterBindingsActivated( "tenant-1", "template-v1", "template-v2", new LinkedHashMap<>()); service.afterBindingsActivated( "tenant-1", "template-v1", "template-v2", new LinkedHashMap<>()); List copied = service.list("tenant-1", "template-v2"); assertEquals(2, copied.size()); assertEquals("results", copied.get(0).getGroupCode()); assertEquals(Integer.valueOf(2), copied.get(0).getMinRows()); assertEquals(Integer.valueOf(20), copied.get(0).getMaxRows()); assertEquals("samples", copied.get(1).getGroupCode()); assertEquals(4, store.groups.size()); } private static ElnTemplateRepeatGroupEntity group(String id, String tenantId, String templateId, String code, int minRows, int maxRows) { ElnTemplateRepeatGroupEntity entity = new ElnTemplateRepeatGroupEntity(); entity.setId(id); entity.setFTenantId(tenantId); entity.setMubanId(templateId); entity.setRepeatGroupCode(code); entity.setMinRows(minRows); entity.setMaxRows(maxRows); entity.setFDeleteMark(0); return entity; } private static void expectFailure(Runnable action) { try { action.run(); fail("预期重复组配置被拒绝"); } catch (DataException expected) { // expected } } private static final class SequenceIds implements java.util.function.Supplier { private int next; @Override public String get() { return "copied-" + (++next); } } private static final class MemoryStore implements ElnTemplateRepeatGroupServiceImpl.Store { private final Map groups = new LinkedHashMap<>(); private void add(ElnTemplateRepeatGroupEntity entity) { groups.put(key(entity.getFTenantId(), entity.getMubanId(), entity.getRepeatGroupCode()), entity); } @Override public ElnTemplateRepeatGroupEntity find(String tenantId, String templateId, String groupCode) { return groups.get(key(tenantId, templateId, groupCode)); } @Override public List list(String tenantId, String templateId) { List result = new ArrayList<>(); for (ElnTemplateRepeatGroupEntity entity : groups.values()) { if (tenantId.equals(entity.getFTenantId()) && templateId.equals(entity.getMubanId())) { result.add(entity); } } return result; } @Override public void insert(ElnTemplateRepeatGroupEntity entity) { add(entity); } private static String key(String tenantId, String templateId, String groupCode) { return tenantId + ":" + templateId + ":" + groupCode; } } }