@Test
public void updateSecurityRulesTest() {

    
    // Create a group
    UserGroup ug = new UserGroup();
    ug.setGroupName("usergroupTest1");
    long gid = geoStoreClient.insertUserGroup(ug);
    
    UserGroup ug2 = new UserGroup();
    ug2.setGroupName("unusedGroup");
    long gid2 = geoStoreClient.insertUserGroup(ug2);
    
    UserGroup anotherGroup = new UserGroup();
    anotherGroup.setGroupName("anotherGroup");
    long anotherGid = geoStoreClient.insertUserGroup(anotherGroup);
    
    Set<UserGroup> ugroups = new HashSet<>();
    ugroups.add(ug);
    ugroups.add(anotherGroup);
    
    // Create 2 user
    createUser("u1", Role.USER, "u1");
    createUser("u2", Role.USER, "u2", ug, anotherGroup);
            
    GeoStoreClient u1Client = createUserClient("u1", "u1");
    GeoStoreClient u2Client = createUserClient("u2", "u2");
    
    // Create a resource with the user "user". So user will be the owner
    createUser("user", Role.USER, "user");
    createDefaultCategory();
    ShortResource sr = createAResource();
    ShortResource sr2 = createAResource();
    
    
    // Since "user" is the owner the users
    // "u1" and "u2" should not be allowed to READ or WRITE, because they are not the owner of the reosource, it belong to at least one group and the resource doesn't have any groups rule security
    try{
        u1Client.getResource(sr.getId(), true);
        fail("Untrapped exception");
    }
    catch(UniformInterfaceException e){
        assertEquals(403, e.getResponse().getStatus());
    }
    try{
        u2Client.getResource(sr.getId(), true);
        fail("Untrapped exception");
    }
    catch(UniformInterfaceException e){
        assertEquals(403, e.getResponse().getStatus());
    }
    try{
        u1Client.updateResource(sr.getId(), new RESTResource());
        fail("Untrapped exception");
    }
    catch(UniformInterfaceException e){
        assertEquals(403, e.getResponse().getStatus());
    }
    try{
        u2Client.updateResource(sr.getId(), new RESTResource());
        fail("Untrapped exception");
    }
    catch(UniformInterfaceException e){
        assertEquals(403, e.getResponse().getStatus());
    }
    
    // Update permissions for users belong to "usergroupTest1" ("u1") group
    // The effect of this service call will be: 
    // 1) The resource will be added with a group rule on usergroupTest1
    // 2) "u2" will have WRITE permission but not READ permissions
    // 3) Nothing will change for "u1"
    List<ShortResource> srl = new ArrayList<ShortResource>();
    srl.add(sr);
    ShortResourceList srll = new ShortResourceList(srl);
    
    ShortResourceList srlf = geoStoreClient.getAllShortResource(0, 1000);
    assertEquals(2, srlf.getList().size());
    geoStoreClient.updateSecurityRules(srll, gid, false, true);
    geoStoreClient.updateSecurityRules(srll, gid2, false, false);
    srlf = geoStoreClient.getAllShortResource(0, 1000);
    assertEquals(2, srlf.getList().size());
    
    // READ shouldn't allowed, WRITE allowed
    try{
        u1Client.getResource(sr.getId(), true);
    }
    catch(UniformInterfaceException e){
        assertEquals(403, e.getResponse().getStatus());
    }
    try{
        u2Client.getResource(sr.getId(), true);
    }
    catch(UniformInterfaceException e){
        assertEquals(403, e.getResponse().getStatus());
    }
    try{
        u1Client.updateResource(sr.getId(), new RESTResource());
    }
    catch(UniformInterfaceException e){
        assertEquals(403, e.getResponse().getStatus());
    }
    u2Client.updateResource(sr.getId(), new RESTResource());
}