ResourcePermissionService { public void revokeAuthorizedEnvironmentsAndEnvironmentTypesPerApplication(AbstractSecurityEnabledResource resource, String[] applicationsToDelete, String[] environmentsToDelete, String[] environmentTypesToDelete) { IResourceSaver noSave = null; if (ArrayUtils.isNotEmpty(applicationsToDelete)) { revokePermission(resource, noSave, Subject.APPLICATION, applicationsToDelete); } if (ArrayUtils.isNotEmpty(environmentsToDelete)) { revokePermission(resource, noSave, Subject.ENVIRONMENT, environmentsToDelete); } if (ArrayUtils.isNotEmpty(environmentTypesToDelete)) { revokePermission(resource, noSave, Subject.ENVIRONMENT_TYPE, environmentTypesToDelete); } alienDAO.save(resource); }  void grantPermission(ISecurityEnabledResource resource, Subject subjectType, String... subjects); void grantPermission(ISecurityEnabledResource resource, IResourceSaver saver, Subject subjectType, String... subjects); void revokePermission(ISecurityEnabledResource resource, Subject subjectType, String... subjects); void revokePermission(ISecurityEnabledResource resource, IResourceSaver saver, Subject subjectType, String... subjects); boolean anyHasPermission(ISecurityEnabledResource resource, Map<Subject, Set<String>> subjects); boolean allHavePermission(ISecurityEnabledResource resource, Map<Subject, Set<String>> subjects); List<User> getAuthorizedUsers(AbstractSecurityEnabledResource resource); List<Group> getAuthorizedGroups(AbstractSecurityEnabledResource resource); void revokeAuthorizedEnvironmentsAndEnvironmentTypesPerApplication(AbstractSecurityEnabledResource resource, String[] applicationsToDelete,
                                                                              String[] environmentsToDelete, String[] environmentTypesToDelete); void grantAuthorizedEnvironmentsAndEnvTypesPerApplication(AbstractSecurityEnabledResource resource, String[] applicationsToAdd, String[] environmentsToAdd,
                                                                     String[] environmentTypesToAdd);  }
@Test public void when_many_app_env_permission_are_revoked_the_resource_is_saved_only_one_time() throws Exception { resourceSecured = new AbstractSecurityEnabledResource() { @Override public String getId() { return "id"; } }; HashSet<Permission> permissions = new HashSet<>(); permissions.add(Permission.ADMIN); resourceSecured.addPermissions(Subject.APPLICATION, "subject1", new HashSet<>(permissions)); resourceSecured.addPermissions(Subject.ENVIRONMENT, "subject1_1", new HashSet<>(permissions)); resourceSecured.addPermissions(Subject.ENVIRONMENT, "subject1_2", new HashSet<>(permissions)); resourceSecured.addPermissions(Subject.ENVIRONMENT_TYPE, "subject_env_1_1", new HashSet<>(permissions)); ApplicationEnvironment ae1 = new ApplicationEnvironment(); ae1.setId("subject1_1"); ApplicationEnvironment ae2 = new ApplicationEnvironment(); ae2.setId("subject1_2"); Mockito.when(applicationEnvironmentService.getByApplicationId("subject1")).thenReturn(new ApplicationEnvironment[] { ae1, ae2 }); service.revokeAuthorizedEnvironmentsAndEnvironmentTypesPerApplication((AbstractSecurityEnabledResource) resourceSecured, new String[] { "subject1" }, new String[] { "subject1_1", "subject1_2" }, new String[] {"subject_env_1_1" }); verify(alienDAO).save(resourceSecuredCaptor.capture()); }