### Question:
QueryCleanupTask extends Task { public static int deleteQueryPermissionsWithMissingRef(MetaStorage storage, Iterable<? extends PermissionOwner<?>> owners) { int countDeleted = 0; for (PermissionOwner<?> owner : owners) { Set<Permission> permissions = owner.getPermissions(); Iterator<Permission> it = permissions.iterator(); while (it.hasNext()) { Permission permission = it.next(); if (!(permission instanceof WildcardPermission)) { log.warn( "Encountered the permission type {} that is not handled by this routine. Permission was: {}", permission.getClass(), permission); continue; } WildcardPermission wpermission = (WildcardPermission) permission; if(!wpermission.getDomains().contains(QueryPermission.DOMAIN.toLowerCase())) { continue; } Set<String> validRef = new HashSet<>(); for (String sId : wpermission.getInstances()) { ManagedExecutionId mId = ManagedExecutionId.Parser.INSTANCE.parse(sId); if (storage.getExecution(mId) != null) { validRef.add(mId.toString()); } } if (!validRef.isEmpty()) { if (wpermission.getInstances().size() == validRef.size()) { continue; } WildcardPermission reducedPermission = new WildcardPermission( List.of(wpermission.getDomains(), wpermission.getAbilities(), validRef), wpermission.getCreationTime()); owner.addPermission(storage, reducedPermission); } owner.removePermission(storage, wpermission); countDeleted++; } } return countDeleted; } QueryCleanupTask(MetaStorage storage, Duration queryExpiration); static boolean isDefaultLabel(String label); @Override void execute(Map<String, List<String>> parameters, PrintWriter output); static int deleteQueryPermissionsWithMissingRef(MetaStorage storage, Iterable<? extends PermissionOwner<?>> owners); static final String EXPIRATION_PARAM; }### Answer:
@Test void doNotDeletePermissionValidReference() { assertThat(storageMock.getAllExecutions()).isEmpty(); final ManagedQuery managedQuery = createManagedQuery(); User user = new User("test", "test"); storageMock.updateUser(user); user.addPermission(storageMock, QueryPermission.onInstance(AbilitySets.QUERY_CREATOR, managedQuery.getId())); QueryCleanupTask.deleteQueryPermissionsWithMissingRef(storageMock, storageMock.getAllUsers()); assertThat(user.getPermissions()).containsOnly(QueryPermission.onInstance(AbilitySets.QUERY_CREATOR, managedQuery.getId())); }
@Test void doDeletePermissionInvalidReference() { assertThat(storageMock.getAllExecutions()).isEmpty(); final ManagedQuery managedQuery = createManagedQuery(); storageMock.removeExecution(managedQuery.getId()); User user = new User("test", "test"); storageMock.updateUser(user); user.addPermission(storageMock, QueryPermission.onInstance(AbilitySets.QUERY_CREATOR, managedQuery.getId())); QueryCleanupTask.deleteQueryPermissionsWithMissingRef(storageMock, storageMock.getAllUsers()); assertThat(user.getPermissions()).isEmpty(); }
@Test void doDeletePartialPermissionWithInvalidReference() { assertThat(storageMock.getAllExecutions()).isEmpty(); final ManagedQuery managedQuery1 = createManagedQuery(); final ManagedQuery managedQuery2 = createManagedQuery(); storageMock.removeExecution(managedQuery2.getId()); User user = new User("test", "test"); storageMock.updateUser(user); user.addPermission( storageMock, new WildcardPermission(List.of( Set.of(QueryPermission.DOMAIN), Set.of(Ability.READ.toString().toLowerCase()), Set.of(managedQuery1.getId().toString(), managedQuery2.getId().toString())), Instant.now())); QueryCleanupTask.deleteQueryPermissionsWithMissingRef(storageMock, storageMock.getAllUsers()); assertThat(user.getPermissions()).containsOnly(QueryPermission.onInstance(Ability.READ, managedQuery1.getId())); }