Skip to content

Commit

Permalink
[SYNCOPE-1806] Fixing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ilgrosso committed Mar 6, 2024
1 parent 5145c09 commit cfc59b6
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,19 +43,19 @@ public interface GroupRepoBase extends GroupDAO {
@Override
List<Group> findOwnedByGroup(@Param("groupKey") String groupKey);

@Query("SELECT e.leftEnd.id FROM JPAAMembership e WHERE e.rightEnd.id = :groupKey")
@Query("SELECT DISTINCT e.leftEnd.id FROM JPAAMembership e WHERE e.rightEnd.id = :groupKey")
@Override
List<String> findAMembers(@Param("groupKey") String groupKey);

@Query("SELECT e.leftEnd.id FROM JPAUMembership e WHERE e.rightEnd.id = :groupKey")
@Query("SELECT DISTINCT e.leftEnd.id FROM JPAUMembership e WHERE e.rightEnd.id = :groupKey")
@Override
List<String> findUMembers(@Param("groupKey") String groupKey);

@Query("SELECT COUNT(e.leftEnd.id) FROM JPAAMembership e WHERE e.rightEnd.id = :groupKey")
@Query("SELECT COUNT(DISTINCT e.leftEnd.id) FROM JPAAMembership e WHERE e.rightEnd.id = :groupKey")
@Override
long countAMembers(@Param("groupKey") String groupKey);

@Query("SELECT COUNT(e.leftEnd.id) FROM JPAUMembership e WHERE e.rightEnd.id = :groupKey")
@Query("SELECT COUNT(DISTINCT e.leftEnd.id) FROM JPAUMembership e WHERE e.rightEnd.id = :groupKey")
@Override
long countUMembers(@Param("groupKey") String groupKey);
}
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ public List<TypeExtension> findTypeExtensions(final AnyTypeClass anyTypeClass) {
@Override
public long countADynMembers(final Group group) {
Query query = entityManager.createNativeQuery(
"SELECT COUNT(any_id) FROM " + ADYNMEMB_TABLE + " WHERE group_id=?");
"SELECT COUNT(DISTINCT any_id) FROM " + ADYNMEMB_TABLE + " WHERE group_id=?");
query.setParameter(1, group.getKey());

return ((Number) query.getSingleResult()).longValue();
Expand All @@ -370,7 +370,7 @@ public long countUDynMembers(final Group group) {
}

Query query = entityManager.createNativeQuery(
"SELECT COUNT(any_id) FROM " + UDYNMEMB_TABLE + " WHERE group_id=?");
"SELECT COUNT(DISTINCT any_id) FROM " + UDYNMEMB_TABLE + " WHERE group_id=?");
query.setParameter(1, group.getKey());

return ((Number) query.getSingleResult()).longValue();
Expand All @@ -383,7 +383,7 @@ public List<String> findADynMembers(final Group group) {

group.getADynMemberships().forEach(memb -> {
Query query = entityManager.createNativeQuery(
"SELECT any_id FROM " + ADYNMEMB_TABLE + " WHERE group_id=? AND anyType_id=?");
"SELECT DISTINCT any_id FROM " + ADYNMEMB_TABLE + " WHERE group_id=? AND anyType_id=?");
query.setParameter(1, group.getKey());
query.setParameter(2, memb.getAnyType().getKey());

Expand All @@ -404,7 +404,8 @@ public List<String> findUDynMembers(final Group group) {
return List.of();
}

Query query = entityManager.createNativeQuery("SELECT any_id FROM " + UDYNMEMB_TABLE + " WHERE group_id=?");
Query query = entityManager.createNativeQuery(
"SELECT DISTINCT any_id FROM " + UDYNMEMB_TABLE + " WHERE group_id=?");
query.setParameter(1, group.getKey());

@SuppressWarnings("unchecked")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,14 @@ public interface GroupRepo
@Query("MATCH (a:" + Neo4jAnyObject.NODE + ")-[]-"
+ "(n:" + Neo4jAMembership.NODE + ")-[]-"
+ "(g:" + Neo4jGroup.NODE + " {id: $groupKey}) "
+ "RETURN COUNT(a)")
+ "RETURN COUNT(DISTINCT a)")
@Override
long countAMembers(@Param("groupKey") String groupKey);

@Query("MATCH (u:" + Neo4jUser.NODE + ")-[]-"
+ "(n:" + Neo4jUMembership.NODE + ")-[]-"
+ "(g:" + Neo4jGroup.NODE + " {id: $groupKey}) "
+ "RETURN COUNT(u)")
+ "RETURN COUNT(DISTINCT u)")
@Override
long countUMembers(@Param("groupKey") String groupKey);
}
Original file line number Diff line number Diff line change
Expand Up @@ -386,23 +386,23 @@ public long countADynMembers(final Group group) {
return neo4jTemplate.count(
"MATCH (n)-[:" + DYN_GROUP_ANY_OBJECT_MEMBERSHIP_REL + "]-"
+ "(p:" + Neo4jGroup.NODE + " {id: $id}) "
+ "RETURN COUNT(n.id)", Map.of("id", group.getKey()));
+ "RETURN COUNT(DISTINCT n.id)", Map.of("id", group.getKey()));
}

@Override
public long countUDynMembers(final Group group) {
return neo4jTemplate.count(
"MATCH (n)-[:" + DYN_GROUP_USER_MEMBERSHIP_REL + "]-"
+ "(p:" + Neo4jGroup.NODE + " {id: $id}) "
+ "RETURN COUNT(n.id)", Map.of("id", group.getKey()));
+ "RETURN COUNT(DISTNCT n.id)", Map.of("id", group.getKey()));
}

@Override
public List<String> findADynMembers(final Group group) {
return neo4jClient.query(
"MATCH (n)-[:" + DYN_GROUP_ANY_OBJECT_MEMBERSHIP_REL + "]-"
+ "(p:" + Neo4jGroup.NODE + " {id: $id}) "
+ "RETURN n.id").bindAll(Map.of("id", group.getKey())).fetch().all().stream().
+ "RETURN DISTINCT n.id").bindAll(Map.of("id", group.getKey())).fetch().all().stream().
map(found -> found.get("n.id").toString()).toList();
}

Expand All @@ -411,7 +411,7 @@ public List<String> findUDynMembers(final Group group) {
return neo4jClient.query(
"MATCH (n)-[:" + DYN_GROUP_USER_MEMBERSHIP_REL + "]-"
+ "(p:" + Neo4jGroup.NODE + " {id: $id}) "
+ "RETURN n.id").bindAll(Map.of("id", group.getKey())).fetch().all().stream().
+ "RETURN DISTINCT n.id").bindAll(Map.of("id", group.getKey())).fetch().all().stream().
map(found -> found.get("n.id").toString()).toList();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,27 @@

public class DynRealmITCase extends AbstractITCase {

private static ArrayNode fetchDynRealmsFromElasticsearch(final String userKey) throws Exception {
String body =
'{'
+ " \"query\": {"
+ " \"match\": {\"_id\": \"" + userKey + "\"}"
+ " }"
+ '}';

HttpClient client = HttpClient.newHttpClient();
HttpResponse<String> response = client.send(
HttpRequest.newBuilder(URI.create("http://localhost:9200/master_user/_search")).
header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON).
method("GET", BodyPublishers.ofString(body)).
build(),
BodyHandlers.ofString());
assertEquals(Response.Status.OK.getStatusCode(), response.statusCode());

return (ArrayNode) JSON_MAPPER.readTree(response.body()).
get("hits").get("hits").get(0).get("_source").get("dynRealms");
}

@Test
public void misc() {
DynRealmTO dynRealm = null;
Expand Down Expand Up @@ -222,27 +243,6 @@ public void delegatedAdmin() {
}
}

private static ArrayNode fetchDynRealmsFromElasticsearch(final String userKey) throws Exception {
String body =
'{'
+ " \"query\": {"
+ " \"match\": {\"_id\": \"" + userKey + "\"}"
+ " }"
+ '}';

HttpClient client = HttpClient.newHttpClient();
HttpResponse<String> response = client.send(
HttpRequest.newBuilder(URI.create("http://localhost:9200/master_user/_search")).
header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON).
method("GET", BodyPublishers.ofString(body)).
build(),
BodyHandlers.ofString());
assertEquals(Response.Status.OK.getStatusCode(), response.statusCode());

return (ArrayNode) JSON_MAPPER.readTree(response.body()).
get("hits").get("hits").get(0).get("_source").get("dynRealms");
}

@Test
public void issueSYNCOPE1480() throws Exception {
String ctype = getUUIDString();
Expand Down Expand Up @@ -333,6 +333,14 @@ public void issueSYNCOPE1806() {
assertNotNull(realm2);

// 2. verify that dynamic members are the same
if (IS_EXT_SEARCH_ENABLED) {
try {
Thread.sleep(2000);
} catch (InterruptedException ex) {
// ignore
}
}

PagedResult<UserTO> matching1 = USER_SERVICE.search(new AnyQuery.Builder().realm("/").fiql(
SyncopeClient.getUserSearchConditionBuilder().inDynRealms(realm1.getKey()).query()).build());
PagedResult<UserTO> matching2 = USER_SERVICE.search(new AnyQuery.Builder().realm("/").fiql(
Expand All @@ -350,6 +358,14 @@ public void issueSYNCOPE1806() {
updateUser(userUR);

// 4. verify that dynamic members are still the same
if (IS_EXT_SEARCH_ENABLED) {
try {
Thread.sleep(2000);
} catch (InterruptedException ex) {
// ignore
}
}

matching1 = USER_SERVICE.search(new AnyQuery.Builder().realm("/").fiql(
SyncopeClient.getUserSearchConditionBuilder().inDynRealms(realm1.getKey()).query()).build());
matching2 = USER_SERVICE.search(new AnyQuery.Builder().realm("/").fiql(
Expand All @@ -367,6 +383,11 @@ public void issueSYNCOPE1806() {
if (realm2 != null) {
DYN_REALM_SERVICE.delete(realm2.getKey());
}
UserUR userUR = new UserUR();
userUR.setKey("823074dc-d280-436d-a7dd-07399fae48ec");
userUR.getPlainAttrs().add(new AttrPatch.Builder(new Attr.Builder("cool").build()).
operation(PatchOperation.DELETE).build());
updateUser(userUR);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -679,17 +679,26 @@ public void uDynMembership() {

GroupCR groupCR = getBasicSample("uDynMembership");
groupCR.setUDynMembershipCond("cool==true");
GroupTO group = createGroup(groupCR).getEntity();
assertNotNull(group);
GroupTO group = null;
try {
group = createGroup(groupCR).getEntity();
assertNotNull(group);
String groupKey = group.getKey();

List<MembershipTO> memberships = USER_SERVICE.read("c9b2dec2-00a7-4855-97c0-d854842b4b24").getDynMemberships();
assertTrue(memberships.stream().anyMatch(m -> m.getGroupKey().equals(group.getKey())));
assertEquals(1, GROUP_SERVICE.read(group.getKey()).getDynamicUserMembershipCount());
List<MembershipTO> memberships =
USER_SERVICE.read("c9b2dec2-00a7-4855-97c0-d854842b4b24").getDynMemberships();
assertTrue(memberships.stream().anyMatch(m -> m.getGroupKey().equals(groupKey)));
assertEquals(1, GROUP_SERVICE.read(group.getKey()).getDynamicUserMembershipCount());

GROUP_SERVICE.update(new GroupUR.Builder(group.getKey()).udynMembershipCond("cool==false").build());
GROUP_SERVICE.update(new GroupUR.Builder(group.getKey()).udynMembershipCond("cool==false").build());

assertTrue(USER_SERVICE.read("c9b2dec2-00a7-4855-97c0-d854842b4b24").getDynMemberships().isEmpty());
assertEquals(0, GROUP_SERVICE.read(group.getKey()).getDynamicUserMembershipCount());
assertTrue(USER_SERVICE.read("c9b2dec2-00a7-4855-97c0-d854842b4b24").getDynMemberships().isEmpty());
assertEquals(0, GROUP_SERVICE.read(group.getKey()).getDynamicUserMembershipCount());
} finally {
if (group != null) {
GROUP_SERVICE.delete(group.getKey());
}
}
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,23 +138,30 @@ public void dynMembership() {
assertTrue(bellini.getDynRoles().isEmpty());
assertTrue(bellini.getPrivileges().isEmpty());

RoleTO role = getSampleRoleTO("dynMembership");
role.getPrivileges().add("getMighty");
role.setDynMembershipCond("cool==true");
Response response = ROLE_SERVICE.create(role);
role = getObject(response.getLocation(), RoleService.class, RoleTO.class);
assertNotNull(role);

bellini = USER_SERVICE.read("bellini");
assertTrue(bellini.getDynRoles().contains(role.getKey()));
assertTrue(bellini.getPrivileges().contains("getMighty"));

role.setDynMembershipCond("cool==false");
ROLE_SERVICE.update(role);

bellini = USER_SERVICE.read("bellini");
assertTrue(bellini.getDynMemberships().isEmpty());
assertTrue(bellini.getPrivileges().isEmpty());
RoleTO role = null;
try {
role = getSampleRoleTO("dynMembership");
role.getPrivileges().add("getMighty");
role.setDynMembershipCond("cool==true");
Response response = ROLE_SERVICE.create(role);
role = getObject(response.getLocation(), RoleService.class, RoleTO.class);
assertNotNull(role);

bellini = USER_SERVICE.read("bellini");
assertTrue(bellini.getDynRoles().contains(role.getKey()));
assertTrue(bellini.getPrivileges().contains("getMighty"));

role.setDynMembershipCond("cool==false");
ROLE_SERVICE.update(role);

bellini = USER_SERVICE.read("bellini");
assertTrue(bellini.getDynMemberships().isEmpty());
assertTrue(bellini.getPrivileges().isEmpty());
} finally {
if (role != null) {
ROLE_SERVICE.delete(role.getKey());
}
}
}

@Test
Expand Down

0 comments on commit cfc59b6

Please sign in to comment.