Program: 60434
Project: 60434
Buggy source:
@Test public void delegatedAdmin(){
  DynRealmTO dynRealm=null;
  RoleTO role=null;
  try {
    dynRealm=new DynRealmTO();
    dynRealm.setKey("String_Node_Str" + getUUIDString());
    dynRealm.getDynMembershipConds().put(AnyTypeKind.USER.name(),"String_Node_Str");
    dynRealm.getDynMembershipConds().put(AnyTypeKind.GROUP.name(),"String_Node_Str");
    Response response=dynRealmService.create(dynRealm);
    dynRealm=getObject(response.getLocation(),DynRealmService.class,DynRealmTO.class);
    assertNotNull(dynRealm);
    role=new RoleTO();
    role.setKey("String_Node_Str" + getUUIDString());
    role.getEntitlements().add(StandardEntitlement.USER_SEARCH);
    role.getEntitlements().add(StandardEntitlement.USER_READ);
    role.getEntitlements().add(StandardEntitlement.USER_UPDATE);
    role.getEntitlements().add(StandardEntitlement.GROUP_READ);
    role.getEntitlements().add(StandardEntitlement.GROUP_UPDATE);
    role.getDynRealms().add(dynRealm.getKey());
    role=createRole(role);
    assertNotNull(role);
    UserTO dynRealmAdmin=UserITCase.getUniqueSampleTO("String_Node_Str");
    dynRealmAdmin.setPassword("String_Node_Str");
    dynRealmAdmin.getRoles().add(role.getKey());
    dynRealmAdmin=createUser(dynRealmAdmin).getEntity();
    assertNotNull(dynRealmAdmin);
    UserTO user=UserITCase.getUniqueSampleTO("String_Node_Str");
    user.setRealm("String_Node_Str");
    user.getResources().clear();
    user.getResources().add(RESOURCE_NAME_LDAP);
    user=createUser(user).getEntity();
    assertNotNull(user);
    final String userKey=user.getKey();
    GroupTO group=GroupITCase.getSampleTO("String_Node_Str");
    group.setRealm("String_Node_Str");
    group.getResources().clear();
    group.getResources().add(RESOURCE_NAME_LDAP);
    group=createGroup(group).getEntity();
    assertNotNull(group);
    final String groupKey=group.getKey();
    PagedResult<UserTO> matchingUsers=userService.search(new AnyQuery.Builder().realm("String_Node_Str").fiql(SyncopeClient.getUserSearchConditionBuilder().inDynRealms(dynRealm.getKey()).query()).build());
    assertTrue(matchingUsers.getResult().stream().anyMatch(object -> object.getKey().equals(userKey)));
    PagedResult<GroupTO> matchingGroups=groupService.search(new AnyQuery.Builder().realm("String_Node_Str").fiql(SyncopeClient.getGroupSearchConditionBuilder().inDynRealms(dynRealm.getKey()).query()).build());
    assertTrue(matchingGroups.getResult().stream().anyMatch(object -> object.getKey().equals(groupKey)));
    SyncopeClient delegatedClient=clientFactory.create(dynRealmAdmin.getUsername(),"String_Node_Str");
    UserService delegatedUserService=delegatedClient.getService(UserService.class);
    GroupService delegatedGroupService=delegatedClient.getService(GroupService.class);
    assertNotNull(delegatedUserService.read(userKey));
    assertNotNull(delegatedGroupService.read(groupKey));
    matchingUsers=delegatedUserService.search(new AnyQuery.Builder().realm("String_Node_Str").build());
    assertTrue(matchingUsers.getResult().stream().anyMatch(object -> object.getKey().equals(userKey)));
    UserPatch userPatch=new UserPatch();
    userPatch.setKey(userKey);
    userPatch.getResources().add(new StringPatchItem.Builder().value(RESOURCE_NAME_LDAP).operation(PatchOperation.DELETE).build());
    try {
      delegatedUserService.update(userPatch);
      fail();
    }
 catch (    SyncopeClientException e) {
      assertEquals(ClientExceptionType.DelegatedAdministration,e.getType());
    }
    userPatch.getResources().clear();
    userPatch.getResources().add(new StringPatchItem.Builder().value(RESOURCE_NAME_NOPROPAGATION).build());
    user=delegatedUserService.update(userPatch).readEntity(new GenericType<ProvisioningResult<UserTO>>(){
    }
).getEntity();
    assertNotNull(user);
    assertTrue(user.getResources().contains(RESOURCE_NAME_NOPROPAGATION));
    GroupPatch groupPatch=new GroupPatch();
    groupPatch.setKey(groupKey);
    groupPatch.getPlainAttrs().add(new AttrPatch.Builder().attrTO(attrTO("String_Node_Str","String_Node_Str")).build());
    group=delegatedGroupService.update(groupPatch).readEntity(new GenericType<ProvisioningResult<GroupTO>>(){
    }
).getEntity();
    assertNotNull(group);
    assertEquals("String_Node_Str",group.getPlainAttr("String_Node_Str").get().getValues().get(0));
  }
  finally {
    if (role != null) {
      roleService.delete(role.getKey());
    }
    if (dynRealm != null) {
      dynRealmService.delete(dynRealm.getKey());
    }
  }
}

Fixed version (ground truth):
@Test public void delegatedAdmin(){
  DynRealmTO dynRealm=null;
  RoleTO role=null;
  try {
    dynRealm=new DynRealmTO();
    dynRealm.setKey("String_Node_Str" + getUUIDString());
    dynRealm.getDynMembershipConds().put(AnyTypeKind.USER.name(),"String_Node_Str");
    dynRealm.getDynMembershipConds().put(AnyTypeKind.GROUP.name(),"String_Node_Str");
    Response response=dynRealmService.create(dynRealm);
    dynRealm=getObject(response.getLocation(),DynRealmService.class,DynRealmTO.class);
    assertNotNull(dynRealm);
    role=new RoleTO();
    role.setKey("String_Node_Str" + getUUIDString());
    role.getEntitlements().add(StandardEntitlement.USER_SEARCH);
    role.getEntitlements().add(StandardEntitlement.USER_READ);
    role.getEntitlements().add(StandardEntitlement.USER_UPDATE);
    role.getEntitlements().add(StandardEntitlement.GROUP_READ);
    role.getEntitlements().add(StandardEntitlement.GROUP_UPDATE);
    role.getDynRealms().add(dynRealm.getKey());
    role=createRole(role);
    assertNotNull(role);
    UserTO dynRealmAdmin=UserITCase.getUniqueSampleTO("String_Node_Str");
    dynRealmAdmin.setPassword("String_Node_Str");
    dynRealmAdmin.getRoles().add(role.getKey());
    dynRealmAdmin=createUser(dynRealmAdmin).getEntity();
    assertNotNull(dynRealmAdmin);
    UserTO user=UserITCase.getUniqueSampleTO("String_Node_Str");
    user.setRealm("String_Node_Str");
    user.getResources().clear();
    user.getResources().add(RESOURCE_NAME_LDAP);
    user=createUser(user).getEntity();
    assertNotNull(user);
    final String userKey=user.getKey();
    GroupTO group=GroupITCase.getSampleTO("String_Node_Str");
    group.setRealm("String_Node_Str");
    group.getResources().clear();
    group.getResources().add(RESOURCE_NAME_LDAP);
    group=createGroup(group).getEntity();
    assertNotNull(group);
    final String groupKey=group.getKey();
    if (ElasticsearchDetector.isElasticSearchEnabled(syncopeService)) {
      try {
        Thread.sleep(2000);
      }
 catch (      InterruptedException ex) {
      }
    }
    PagedResult<UserTO> matchingUsers=userService.search(new AnyQuery.Builder().realm("String_Node_Str").fiql(SyncopeClient.getUserSearchConditionBuilder().inDynRealms(dynRealm.getKey()).query()).build());
    assertTrue(matchingUsers.getResult().stream().anyMatch(object -> object.getKey().equals(userKey)));
    PagedResult<GroupTO> matchingGroups=groupService.search(new AnyQuery.Builder().realm("String_Node_Str").fiql(SyncopeClient.getGroupSearchConditionBuilder().inDynRealms(dynRealm.getKey()).query()).build());
    assertTrue(matchingGroups.getResult().stream().anyMatch(object -> object.getKey().equals(groupKey)));
    SyncopeClient delegatedClient=clientFactory.create(dynRealmAdmin.getUsername(),"String_Node_Str");
    UserService delegatedUserService=delegatedClient.getService(UserService.class);
    GroupService delegatedGroupService=delegatedClient.getService(GroupService.class);
    assertNotNull(delegatedUserService.read(userKey));
    assertNotNull(delegatedGroupService.read(groupKey));
    matchingUsers=delegatedUserService.search(new AnyQuery.Builder().realm("String_Node_Str").build());
    assertTrue(matchingUsers.getResult().stream().anyMatch(object -> object.getKey().equals(userKey)));
    UserPatch userPatch=new UserPatch();
    userPatch.setKey(userKey);
    userPatch.getResources().add(new StringPatchItem.Builder().value(RESOURCE_NAME_LDAP).operation(PatchOperation.DELETE).build());
    try {
      delegatedUserService.update(userPatch);
      fail();
    }
 catch (    SyncopeClientException e) {
      assertEquals(ClientExceptionType.DelegatedAdministration,e.getType());
    }
    userPatch.getResources().clear();
    userPatch.getResources().add(new StringPatchItem.Builder().value(RESOURCE_NAME_NOPROPAGATION).build());
    user=delegatedUserService.update(userPatch).readEntity(new GenericType<ProvisioningResult<UserTO>>(){
    }
).getEntity();
    assertNotNull(user);
    assertTrue(user.getResources().contains(RESOURCE_NAME_NOPROPAGATION));
    GroupPatch groupPatch=new GroupPatch();
    groupPatch.setKey(groupKey);
    groupPatch.getPlainAttrs().add(new AttrPatch.Builder().attrTO(attrTO("String_Node_Str","String_Node_Str")).build());
    group=delegatedGroupService.update(groupPatch).readEntity(new GenericType<ProvisioningResult<GroupTO>>(){
    }
).getEntity();
    assertNotNull(group);
    assertEquals("String_Node_Str",group.getPlainAttr("String_Node_Str").get().getValues().get(0));
  }
  finally {
    if (role != null) {
      roleService.delete(role.getKey());
    }
    if (dynRealm != null) {
      dynRealmService.delete(dynRealm.getKey());
    }
  }
}