ResourceServiceImpl implements ResourceService { @Override public void updateSecurityRules(long id, List<SecurityRule> rules) throws BadRequestServiceEx, InternalErrorServiceEx, NotFoundServiceEx { Resource resource = resourceDAO.find(id); if (resource != null) { Search searchCriteria = new Search(); searchCriteria.addFilterEqual("resource.id", id); List<SecurityRule> resourceRules = this.securityDAO.search(searchCriteria); for (SecurityRule rule : resourceRules) { securityDAO.remove(rule); } for (SecurityRule rule : rules) { rule.setResource(resource); if (rule.getGroup() != null) { UserGroup ug = userGroupDAO.find(rule.getGroup().getId()); if (ug != null) { rule.setGroup(ug); } } securityDAO.persist(rule); } } else { throw new NotFoundServiceEx("Resource not found " + id); } }  void setSecurityDAO(SecurityDAO securityDAO); void setStoredDataDAO(StoredDataDAO storedDataDAO); void setResourceDAO(ResourceDAO resourceDAO); void setAttributeDAO(AttributeDAO attributeDAO); void setCategoryDAO(CategoryDAO categoryDAO); void setUserGroupDAO(UserGroupDAO userGroupDAO); @Override long insert(Resource resource); @Override long update(Resource resource); @Override void updateAttributes(long id, List<Attribute> attributes); @Override Resource get(long id); @Override boolean delete(long id); @Override void deleteResources(SearchFilter filter); @Override List<ShortResource> getList(String nameLike, Integer page, Integer entries, User authUser); @Override List<ShortResource> getAll(Integer page, Integer entries, User authUser); @Override long getCount(String nameLike); @Override long getCountByFilter(SearchFilter filter); static List<String> extratcGroupNames(Set<UserGroup> groups); @Override List<ShortAttribute> getAttributes(long id); @Override ShortAttribute getAttribute(long id, String name); @Override long updateAttribute(long id, String name, String value); @Override long insertAttribute(long id, String name, String value, DataType type); @Override List<ShortResource> getResources(SearchFilter filter, User authUser); List<Resource> getResources(SearchFilter filter, Integer page, Integer entries,
            boolean includeAttributes, boolean includeData, User authUser); @Override List<ShortResource> getResources(SearchFilter filter, Integer page, Integer entries,
            User authUser); @Override List<Resource> getResourcesFull(SearchFilter filter, User authUser); @Override List<SecurityRule> getUserSecurityRule(String userName, long resourceId); @Override List<SecurityRule> getGroupSecurityRule(List<String> groupNames, long resourceId); @Override List<SecurityRule> getSecurityRules(long id); @Override void updateSecurityRules(long id, List<SecurityRule> rules); long getCountByFilterAndUser(SearchFilter filter, User user); long getCountByFilterAndUser(String nameLike, User user);  }
@Test public void testUpdateSecurityRules() throws Exception { long resourceId = createResource("name1", "description1", "MAP"); List<SecurityRule> writtenRules = resourceService.getSecurityRules(resourceId); assertEquals(0, writtenRules.size()); List<SecurityRule> rules = new ArrayList<SecurityRule>(); long userId = createUser("user1", Role.USER, "password"); User user = new User(); user.setId(userId); long groupId = createGroup("group1"); UserGroup group = new UserGroup(); group.setId(groupId); long otherGroupId = createGroup("group2"); UserGroup othergroup = new UserGroup(); othergroup.setId(otherGroupId); SecurityRule rule = new SecurityRule(); rule.setUser(user); rule.setCanRead(true); rules.add(rule); rule = new SecurityRule(); rule.setCanRead(true); rule.setCanWrite(true); rule.setGroup(group); rules.add(rule); resourceService.updateSecurityRules(resourceId, rules); writtenRules = resourceService.getSecurityRules(resourceId); assertEquals(2, writtenRules.size()); rules.clear(); rule = new SecurityRule(); rule.setUser(user); rule.setCanRead(true); rules.add(rule); rule = new SecurityRule(); rule.setCanRead(true); rule.setCanWrite(true); rule.setGroup(group); rules.add(rule); rule = new SecurityRule(); rule.setCanRead(true); rule.setCanWrite(true); rule.setGroup(othergroup); rules.add(rule); resourceService.updateSecurityRules(resourceId, rules); writtenRules = resourceService.getSecurityRules(resourceId); assertEquals(3, writtenRules.size()); }