package jnpf.elnService.template.impl; import jnpf.elnService.template.ElnTemplateAnnotationAccessService; import jnpf.permission.model.authorize.UserPermissionProbe; import org.junit.Test; import java.util.concurrent.atomic.AtomicReference; import java.util.concurrent.atomic.AtomicInteger; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; public class ElnTemplateAnnotationAccessServiceImplTest { @Test public void editableTemplateAndGrantedMenuAllowAnnotation() { AtomicReference captured = new AtomicReference<>(); ElnTemplateAnnotationAccessService service = new ElnTemplateAnnotationAccessServiceImpl( (tenantId, templateId, fileId) -> true, probe -> { captured.set(probe); return new UserPermissionProbe.Result(true, "ALLOWED"); }); boolean allowed = service.canAnnotate("tenant-a", "user-1", "template-1", "file-1"); assertTrue(allowed); assertEquals("tenant-a", captured.get().getTenantId()); assertEquals("user-1", captured.get().getUserId()); assertEquals("LIMS", captured.get().getSystemCode()); assertEquals("dianziJiluMubanGuanli", captured.get().getModuleCode()); assertEquals("Web", captured.get().getModuleCategory()); assertEquals("", captured.get().getButtonCode()); } @Test public void nonEditableTemplateRejectsWithoutPermissionProbe() { AtomicInteger permissionCalls = new AtomicInteger(); ElnTemplateAnnotationAccessService service = new ElnTemplateAnnotationAccessServiceImpl( (tenantId, templateId, fileId) -> false, probe -> { permissionCalls.incrementAndGet(); return new UserPermissionProbe.Result(true, "ALLOWED"); }); assertFalse(service.canAnnotate("tenant-a", "user-1", "template-1", "file-1")); assertEquals(0, permissionCalls.get()); } @Test public void deniedMenuPermissionRejectsAnnotation() { ElnTemplateAnnotationAccessService service = new ElnTemplateAnnotationAccessServiceImpl( (tenantId, templateId, fileId) -> true, probe -> new UserPermissionProbe.Result(false, "MODULE_DENIED")); assertFalse(service.canAnnotate("tenant-a", "user-1", "template-1", "file-1")); } @Test public void dependencyFailureRejectsAnnotation() { ElnTemplateAnnotationAccessService service = new ElnTemplateAnnotationAccessServiceImpl( (tenantId, templateId, fileId) -> true, probe -> { throw new IllegalStateException("permission unavailable"); }); assertFalse(service.canAnnotate("tenant-a", "user-1", "template-1", "file-1")); } }