package jnpf.elnService.template.impl; import jnpf.elnEntity.template.ElnTemplateCheckboxGroupEntity; import jnpf.elnEntity.template.ElnTemplateVos; import jnpf.exception.DataException; import org.junit.Test; import java.util.ArrayList; import java.util.Collections; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.concurrent.atomic.AtomicInteger; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNull; import static org.junit.Assert.fail; public class ElnTemplateCheckboxGroupServiceImplTest { @Test public void createsArbitraryAndUnlimitedTemplateSelectionRules() { Fixture fixture = new Fixture(); ElnTemplateVos.CheckboxGroup bounded = fixture.service.ensureConfigured( "tenant-1", "template-1", "review_result", 2, 5, "user-1"); ElnTemplateVos.CheckboxGroup unlimited = fixture.service.ensureConfigured( "tenant-1", "template-1", "extra_result", 3, null, "user-1"); assertEquals(Integer.valueOf(2), bounded.getMinSelected()); assertEquals(Integer.valueOf(5), bounded.getMaxSelected()); assertEquals(Integer.valueOf(3), unlimited.getMinSelected()); assertNull(unlimited.getMaxSelected()); assertEquals(2, fixture.service.list("tenant-1", "template-1").size()); } @Test public void updatesRulesWithOptimisticVersionAndRejectsInvalidRanges() { Fixture fixture = new Fixture(); fixture.service.ensureConfigured("tenant-1", "template-1", "result", 0, 1, "user-1"); ElnTemplateVos.CheckboxGroup changed = fixture.service.change( "tenant-1", "template-1", "result", 1, 4, 0, "user-1"); assertEquals(Integer.valueOf(1), changed.getMinSelected()); assertEquals(Integer.valueOf(4), changed.getMaxSelected()); assertEquals(Integer.valueOf(1), changed.getVersion()); expectDataException(() -> fixture.service.change( "tenant-1", "template-1", "result", 2, 3, 0, "user-1")); expectDataException(() -> fixture.service.ensureConfigured( "tenant-1", "template-1", "invalid", 4, 2, "user-1")); } @Test public void copiesRulesWhenTemplateVersionBindingsAreActivated() { Fixture fixture = new Fixture(); fixture.service.ensureConfigured("tenant-1", "source", "result", 1, 7, "user-1"); fixture.service.afterBindingsActivated( "tenant-1", "source", "target", new LinkedHashMap()); ElnTemplateVos.CheckboxGroup copied = fixture.service.list("tenant-1", "target").get(0); assertEquals("result", copied.getGroupCode()); assertEquals(Integer.valueOf(1), copied.getMinSelected()); assertEquals(Integer.valueOf(7), copied.getMaxSelected()); } @Test public void removesRulesWithoutLiveMembers() { Fixture fixture = new Fixture(); fixture.service.ensureConfigured("tenant-1", "template-1", "used", 0, 2, "user-1"); fixture.service.ensureConfigured("tenant-1", "template-1", "orphan", 1, null, "user-1"); fixture.service.removeUnused("tenant-1", "template-1", Collections.singleton("used"), "user-1"); List remaining = fixture.service.list("tenant-1", "template-1"); assertEquals(1, remaining.size()); assertEquals("used", remaining.get(0).getGroupCode()); } private static void expectDataException(Runnable action) { try { action.run(); fail("预期抛出 DataException"); } catch (DataException expected) { // expected } } private static final class Fixture { private final MemoryStore store = new MemoryStore(); private final AtomicInteger ids = new AtomicInteger(); private final ElnTemplateCheckboxGroupServiceImpl service = new ElnTemplateCheckboxGroupServiceImpl(store, () -> "group-" + ids.incrementAndGet()); } private static final class MemoryStore implements ElnTemplateCheckboxGroupServiceImpl.Store { private final Map groups = new LinkedHashMap<>(); @Override public ElnTemplateCheckboxGroupEntity 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 (ElnTemplateCheckboxGroupEntity entity : groups.values()) { if (tenantId.equals(entity.getFTenantId()) && templateId.equals(entity.getMubanId())) result.add(entity); } return result; } @Override public void insert(ElnTemplateCheckboxGroupEntity entity) { groups.put(key(entity.getFTenantId(), entity.getMubanId(), entity.getGroupCode()), entity); } @Override public boolean update(ElnTemplateCheckboxGroupEntity entity, int expectedVersion) { ElnTemplateCheckboxGroupEntity stored = find( entity.getFTenantId(), entity.getMubanId(), entity.getGroupCode()); if (stored == null || !Integer.valueOf(expectedVersion).equals(stored.getFVersion())) return false; entity.setFVersion(expectedVersion + 1); groups.put(key(entity.getFTenantId(), entity.getMubanId(), entity.getGroupCode()), entity); return true; } @Override public boolean softDelete(ElnTemplateCheckboxGroupEntity entity, int expectedVersion) { ElnTemplateCheckboxGroupEntity stored = find( entity.getFTenantId(), entity.getMubanId(), entity.getGroupCode()); if (stored == null || !Integer.valueOf(expectedVersion).equals(stored.getFVersion())) { return false; } groups.remove(key(entity.getFTenantId(), entity.getMubanId(), entity.getGroupCode())); return true; } private static String key(String tenantId, String templateId, String code) { return tenantId + ":" + templateId + ":" + code; } } }