### Question:
SecurityContext implements AutoCloseable { public void checkPermission(NamedFunction function) throws AccessDeniedException, AuthenticationFailedException { try { if (!accessController.hasPermission(credentials, function)) { throw new AccessDeniedException(String.format("User is not allowed to perform operation '%s'.", function.getName())); } } catch (InvalidCredentialsException ex) { throw new AuthenticationFailedException("Could not authenticate user: " + ex.getMessage()); } } protected  SecurityContext(AccessController accessController, IdentityResolver identityResolver, Credentials credentials); static SecurityContext get(); static SecurityContext set(SecurityContext ctx); static boolean isSet(); static SecurityContext clear(); @Override void close(); Credentials getCredentials(); void checkPermission(NamedFunction function); void checkPermission(NamedFunction function, UUID organizationID); UUID getCurrentUserID(); Set<UUID> getAvailableOrganizationID();  }### Answer:
@Test public void testCheckPermissionWithAccess() throws Exception { when(accessController.hasPermission(credentials, viewThreatIntelFact)).thenReturn(true); context.checkPermission(viewThreatIntelFact); verify(accessController).hasPermission(credentials, viewThreatIntelFact); }
@Test(expected = AccessDeniedException.class) public void testCheckPermissionThrowsAccessDeniedException() throws Exception { when(accessController.hasPermission(credentials, viewThreatIntelFact)).thenReturn(false); context.checkPermission(viewThreatIntelFact); }
@Test(expected = AuthenticationFailedException.class) public void testCheckPermissionThrowsAuthenticationFailedException() throws Exception { when(accessController.hasPermission(credentials, viewThreatIntelFact)).thenThrow(InvalidCredentialsException.class); context.checkPermission(viewThreatIntelFact); }
@Test public void testCheckPermissionForOrganizationWithAccess() throws Exception { UUID organizationID = UUID.randomUUID(); when(identityResolver.resolveOrganizationIdentity(organizationID)).thenReturn(organization); when(accessController.hasPermission(credentials, viewThreatIntelFact, organization)).thenReturn(true); context.checkPermission(viewThreatIntelFact, organizationID); verify(accessController).hasPermission(credentials, viewThreatIntelFact, organization); }
@Test(expected = AccessDeniedException.class) public void testCheckPermissionForOrganizationThrowsAccessDeniedException() throws Exception { UUID organizationID = UUID.randomUUID(); when(identityResolver.resolveOrganizationIdentity(organizationID)).thenReturn(organization); when(accessController.hasPermission(credentials, viewThreatIntelFact, organization)).thenReturn(false); context.checkPermission(viewThreatIntelFact, organizationID); }
@Test(expected = AuthenticationFailedException.class) public void testCheckPermissionForOrganizationThrowsAuthenticationFailedException() throws Exception { UUID organizationID = UUID.randomUUID(); when(identityResolver.resolveOrganizationIdentity(organizationID)).thenReturn(organization); when(accessController.hasPermission(credentials, viewThreatIntelFact, organization)).thenThrow(InvalidCredentialsException.class); context.checkPermission(viewThreatIntelFact, organizationID); }