### Question:
CascadingVfsPermissionChecker implements VfsPermissionChecker { @Override public void checkPermission(VfsResourceReference resourceReference) throws VfsException { String scheme = resourceReference.getScheme(); if (HINT.equals(scheme)) { throw new VfsException(String.format("[%s] is a reserved VFS URI scheme and cannot be used.", HINT)); } VfsPermissionChecker resolvedChecker; ComponentManager componentManager = this.componentManagerProvider.get(); try { resolvedChecker = componentManager.getInstance(VfsPermissionChecker.class, scheme); } catch (ComponentLookupException e) { try { resolvedChecker = componentManager.getInstance(VfsPermissionChecker.class); } catch (ComponentLookupException ee) { throw new VfsException(String.format("No VFS Permission Checked has been found in the system. " + "Refusing access to VFS URI scheme [%s]", scheme), ee); } } resolvedChecker.checkPermission(resourceReference); }  @Override void checkPermission(VfsResourceReference resourceReference);  }### Answer:
@Test void checkPermissionWhenReservedScheme() { VfsResourceReference reference = new VfsResourceReference(URI.create("cascading:whatever"), "whatever"); Throwable exception = assertThrows(VfsException.class, () -> this.checker.checkPermission(reference)); assertEquals("[cascading] is a reserved VFS URI scheme and cannot be used.", exception.getMessage()); }
@Test void checkPermissionWithAttachSchemeChecker() throws Exception { VfsResourceReference reference = new VfsResourceReference(URI.create("attach:whatever"), "whatever"); DocumentReference attachmentDocumentReference = new DocumentReference("wiki", "space", "page"); AttachmentReference attachmentReference = new AttachmentReference("file", attachmentDocumentReference); when(this.resolver.resolve("whatever")).thenReturn(attachmentReference); ContextualAuthorizationManager authorizationManager = this.componentManager.registerMockComponent(ContextualAuthorizationManager.class); when(authorizationManager.hasAccess(Right.VIEW, attachmentReference)).thenReturn(true); this.checker.checkPermission(reference); when(authorizationManager.hasAccess(Right.VIEW, attachmentReference)).thenReturn(false); Throwable exception = assertThrows(VfsException.class, () -> this.checker.checkPermission(reference)); assertEquals("No View permission for attachment [Attachment wiki:space.page@file]", exception.getMessage()); }
@Test void checkPermissionWhenNoSpecificSchemeCheckerAndAllowed() throws Exception { VfsResourceReference reference = new VfsResourceReference(URI.create("customscheme:whatever"), "whatever"); AuthorizationManager authorizationManager = this.componentManager.registerMockComponent(AuthorizationManager.class); when(authorizationManager.hasAccess(Right.PROGRAM, this.contextUser, null)).thenReturn(true); this.checker.checkPermission(reference); }
@Test public void checkPermissionWhenNoSpecificSchemeCheckerAndNotAllowed() throws Exception { VfsResourceReference reference = new VfsResourceReference(URI.create("customscheme:whatever"), "whatever"); AuthorizationManager authorizationManager = this.componentManager.registerMockComponent(AuthorizationManager.class); when(authorizationManager.hasAccess(Right.PROGRAM, this.contextUser, null)).thenReturn(false); Throwable exception = assertThrows(VfsException.class, () -> this.checker.checkPermission(reference)); assertEquals("Current logged-in user ([" + this.contextUser + "]) needs to have Programming Rights to use the [customscheme] VFS", exception.getMessage()); }