package jnpf.elnService.template.impl; import jnpf.elnEntity.template.ElnTemplateVersionEntity; import org.junit.Test; import java.util.concurrent.atomic.AtomicReference; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; public class ElnTemplateEditabilityServiceImplTest { @Test public void draftEnabledExactTenantTemplateAndFileMatchIsEditable() { AtomicReference queriedTenant = new AtomicReference<>(); AtomicReference queriedTemplate = new AtomicReference<>(); TestTenantRoute route = new TestTenantRoute(); ElnTemplateEditabilityServiceImpl service = new ElnTemplateEditabilityServiceImpl( (tenantId, templateId) -> { queriedTenant.set(tenantId); queriedTemplate.set(templateId); return template("tenant-a", "template-1", "DRAFT", "enabled", 0, "[{\"fileId\":\"file-1\",\"fileExtension\":\"docx\"}]"); }, route); assertTrue(service.isEditable("tenant-a", "template-1", "file-1")); assertEquals("tenant-a", queriedTenant.get()); assertEquals("template-1", queriedTemplate.get()); assertNull(route.currentTenant); assertEquals(1, route.clearCount); } @Test public void publishedDisabledDeletedFileMismatchAndTenantMismatchFailClosed() { assertFalse(probe(template("tenant-a", "template-1", "PUBLISHED", "enabled", 0, docx("file-1")), "tenant-a", "template-1", "file-1")); assertFalse(probe(template("tenant-a", "template-1", "DRAFT", "disabled", 0, docx("file-1")), "tenant-a", "template-1", "file-1")); assertFalse(probe(template("tenant-a", "template-1", "DRAFT", "enabled", 1, docx("file-1")), "tenant-a", "template-1", "file-1")); assertFalse(probe(template("tenant-a", "template-1", "DRAFT", "enabled", 0, docx("file-2")), "tenant-a", "template-1", "file-1")); assertFalse(probe(template("tenant-b", "template-1", "DRAFT", "enabled", 0, docx("file-1")), "tenant-a", "template-1", "file-1")); assertFalse(probe(template("tenant-a", "template-2", "DRAFT", "enabled", 0, docx("file-1")), "tenant-a", "template-1", "file-1")); } @Test public void malformedOrAmbiguousAttachmentsFailClosedAndClearTenant() { assertFalse(probe(template("tenant-a", "template-1", "DRAFT", "enabled", 0, "not-json"), "tenant-a", "template-1", "file-1")); assertFalse(probe(template("tenant-a", "template-1", "DRAFT", "enabled", 0, "[]"), "tenant-a", "template-1", "file-1")); assertFalse(probe(template("tenant-a", "template-1", "DRAFT", "enabled", 0, "[{\"fileId\":\"file-1\",\"fileExtension\":\"docx\"}," + "{\"fileId\":\"file-2\",\"fileExtension\":\"DOCX\"}]"), "tenant-a", "template-1", "file-1")); } @Test public void missingParametersQueryAndTenantRoutingFailuresFailClosedAndAlwaysClear() { TestTenantRoute missingRoute = new TestTenantRoute(); ElnTemplateEditabilityServiceImpl missingService = new ElnTemplateEditabilityServiceImpl( (tenantId, templateId) -> { throw new AssertionError("missing parameters must not query"); }, missingRoute); assertFalse(missingService.isEditable(" ", "template-1", "file-1")); assertEquals(1, missingRoute.clearCount); TestTenantRoute queryRoute = new TestTenantRoute(); ElnTemplateEditabilityServiceImpl queryFailure = new ElnTemplateEditabilityServiceImpl( (tenantId, templateId) -> { throw new IllegalStateException("query failed"); }, queryRoute); assertFalse(queryFailure.isEditable("tenant-a", "template-1", "file-1")); assertNull(queryRoute.currentTenant); assertEquals(1, queryRoute.clearCount); TestTenantRoute switchFailureRoute = new TestTenantRoute(); switchFailureRoute.switchFailure = new IllegalStateException("route failed"); ElnTemplateEditabilityServiceImpl switchFailure = new ElnTemplateEditabilityServiceImpl( (tenantId, templateId) -> { throw new AssertionError("failed routing must not query"); }, switchFailureRoute); assertFalse(switchFailure.isEditable("tenant-a", "template-1", "file-1")); assertNull(switchFailureRoute.currentTenant); assertEquals(1, switchFailureRoute.clearCount); } private boolean probe(ElnTemplateVersionEntity template, String tenantId, String templateId, String fileId) { TestTenantRoute route = new TestTenantRoute(); ElnTemplateEditabilityServiceImpl service = new ElnTemplateEditabilityServiceImpl( (ignoredTenant, ignoredTemplate) -> template, route); boolean result = service.isEditable(tenantId, templateId, fileId); assertNull(route.currentTenant); assertEquals(1, route.clearCount); return result; } private static String docx(String fileId) { return "[{\"fileId\":\"" + fileId + "\",\"fileExtension\":\"docx\"}]"; } private static ElnTemplateVersionEntity template(String tenantId, String templateId, String status, String enabled, Integer deleteMark, String files) { ElnTemplateVersionEntity template = new ElnTemplateVersionEntity(); template.setFTenantId(tenantId); template.setId(templateId); template.setBizStatus(status); template.setBizEnabled(enabled); template.setFDeleteMark(deleteMark); template.setMubanFile(files); return template; } private static final class TestTenantRoute implements ElnTemplateEditabilityServiceImpl.TenantRoute { private String currentTenant; private int clearCount; private RuntimeException switchFailure; @Override public void switchTo(String tenantId) { if (switchFailure != null) { throw switchFailure; } currentTenant = tenantId; } @Override public void clear() { currentTenant = null; clearCount++; } } }