### Question:
TenantBasedPermissionEvaluator implements PermissionEvaluator { @Override public boolean hasPermission(Authentication authentication, Object target, Object permission) { if (!authentication.isAuthenticated()) { return true; } if (target instanceof Optional) { target = ((Optional<?>) target).orElse(null); } if (target == null) { return false; } CloudbreakUser cloudbreakUser = restRequestThreadLocalService.getCloudbreakUser(); Collection<?> targets = target instanceof Collection ? (Collection<?>) target : Collections.singleton(target); return targets.stream().allMatch(t -> { if (!(t instanceof Clustered)) { return true; } Cluster cluster = ((Clustered) t).getCluster(); if (cluster == null || !cloudbreakUser.getTenant().contentEquals(cluster.getClusterPertain().getTenant())) { return false; } cloudbreakAuthorizationService.hasAccess(cluster.getStackCrn(), cloudbreakUser.getUserId(), cloudbreakUser.getTenant(), permission.toString()); return true; }); }  @Override boolean hasPermission(Authentication authentication, Object target, Object permission); @Override boolean hasPermission(Authentication authentication, Serializable targetId, String targetType, Object permission);  }### Answer:
@Test public void testRequestIsNotAuthorized() { when(authentication.isAuthenticated()).thenReturn(false); boolean result = underTest.hasPermission(authentication, null, ""); Assert.assertTrue(result); }
@Test public void testTargetIsNull() { boolean result = underTest.hasPermission(authentication, null, ""); Assert.assertFalse(result); }
@Test public void testTargetNotInstanceOfClustered() { boolean result = underTest.hasPermission(authentication, new Object(), ""); Assert.assertTrue(result); }
@Test public void testClusterIsNull() { when(clusteredTarget.getCluster()).thenReturn(null); boolean result = underTest.hasPermission(authentication, clusteredTarget, ""); Assert.assertFalse(result); }
@Test public void testTenantNotMatch() { when(clusterPertain.getTenant()).thenReturn("tenant_a"); boolean result = underTest.hasPermission(authentication, clusteredTarget, ""); Assert.assertFalse(result); }
@Test public void testHasAccess() { boolean result = underTest.hasPermission(authentication, clusteredTarget, ""); Assert.assertTrue(result); }
@Test public void testIsOptional() { boolean result = underTest.hasPermission(authentication, Optional.of(clusteredTarget), ""); Assert.assertTrue(result); }
@Test public void testIsCollection() { boolean result = underTest.hasPermission(authentication, Collections.singleton(clusteredTarget), ""); Assert.assertTrue(result); }