You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@syncope.apache.org by il...@apache.org on 2020/02/04 09:25:29 UTC

[syncope] 01/02: [SYNCOPE-1538] Adjusting realm search to Admin Console needs

This is an automated email from the ASF dual-hosted git repository.

ilgrosso pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/syncope.git

commit 28f31bb8b93cb508110d0fb0a125eb974fcb3df4
Author: Francesco Chicchiriccò <il...@apache.org>
AuthorDate: Tue Feb 4 10:14:52 2020 +0100

    [SYNCOPE-1538] Adjusting realm search to Admin Console needs
---
 .../org/apache/syncope/common/lib/to/RealmTO.java    |  3 ++-
 .../common/rest/api/service/RealmService.java        | 13 ++++++++-----
 .../org/apache/syncope/core/logic/RealmLogic.java    | 20 +++++++++++++-------
 .../core/rest/cxf/service/RealmServiceImpl.java      |  8 ++++++--
 .../apache/syncope/fit/core/MultitenancyITCase.java  | 10 +++++-----
 .../org/apache/syncope/fit/core/RealmITCase.java     |  5 +++--
 .../java/org/apache/syncope/fit/core/UserITCase.java |  2 +-
 .../apache/syncope/fit/core/UserIssuesITCase.java    |  4 ++--
 8 files changed, 40 insertions(+), 25 deletions(-)

diff --git a/common/idrepo/lib/src/main/java/org/apache/syncope/common/lib/to/RealmTO.java b/common/idrepo/lib/src/main/java/org/apache/syncope/common/lib/to/RealmTO.java
index 03a0905..910043f 100644
--- a/common/idrepo/lib/src/main/java/org/apache/syncope/common/lib/to/RealmTO.java
+++ b/common/idrepo/lib/src/main/java/org/apache/syncope/common/lib/to/RealmTO.java
@@ -33,11 +33,12 @@ import javax.xml.bind.annotation.XmlType;
 import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
 import org.apache.commons.lang3.builder.EqualsBuilder;
 import org.apache.commons.lang3.builder.HashCodeBuilder;
+import org.apache.syncope.common.lib.BaseBean;
 import org.apache.syncope.common.lib.jaxb.XmlGenericMapAdapter;
 
 @XmlRootElement(name = "realm")
 @XmlType
-public class RealmTO implements NamedEntityTO, TemplatableTO {
+public class RealmTO extends BaseBean implements NamedEntityTO, TemplatableTO {
 
     private static final long serialVersionUID = 516330662956254391L;
 
diff --git a/common/idrepo/rest-api/src/main/java/org/apache/syncope/common/rest/api/service/RealmService.java b/common/idrepo/rest-api/src/main/java/org/apache/syncope/common/rest/api/service/RealmService.java
index 0790b7c..79cc617 100644
--- a/common/idrepo/rest-api/src/main/java/org/apache/syncope/common/rest/api/service/RealmService.java
+++ b/common/idrepo/rest-api/src/main/java/org/apache/syncope/common/rest/api/service/RealmService.java
@@ -42,6 +42,7 @@ import javax.ws.rs.Produces;
 import javax.ws.rs.core.HttpHeaders;
 import javax.ws.rs.core.MediaType;
 import javax.ws.rs.core.Response;
+import org.apache.syncope.common.lib.to.PagedResult;
 import org.apache.syncope.common.lib.to.ProvisioningResult;
 import org.apache.syncope.common.lib.to.RealmTO;
 import org.apache.syncope.common.rest.api.RESTHeaders;
@@ -58,21 +59,23 @@ import org.apache.syncope.common.rest.api.beans.RealmQuery;
 public interface RealmService extends JAXRSService {
 
     /**
-     * Returns a paged list of existing realms matching the given query.
+     * Returns a list of existing realms matching the given query (not including descendant realms) and the total number
+     * of descedant realms.
      *
      * @param query query conditions
-     * @return paged list of existing realms matching the given query
+     * @return list of existing realms matching the given query (not including descendant realms) and the total number
+     * of descedant realms
      */
     @GET
     @Path("search")
     @Produces({ MediaType.APPLICATION_JSON, RESTHeaders.APPLICATION_YAML, MediaType.APPLICATION_XML })
-    List<RealmTO> search(@BeanParam RealmQuery query);
+    PagedResult<RealmTO> search(@BeanParam RealmQuery query);
 
     /**
-     * Returns realms rooted at the given path.
+     * Returns realms rooted at the given path, including descendant realms.
      *
      * @param fullPath full path of the root realm where to read from
-     * @return realms rooted at the given path
+     * @return realms rooted at the given path, including descendant realms
      */
     @GET
     @Path("{fullPath:.*}")
diff --git a/core/idrepo/logic/src/main/java/org/apache/syncope/core/logic/RealmLogic.java b/core/idrepo/logic/src/main/java/org/apache/syncope/core/logic/RealmLogic.java
index a1fd376..bad86d8 100644
--- a/core/idrepo/logic/src/main/java/org/apache/syncope/core/logic/RealmLogic.java
+++ b/core/idrepo/logic/src/main/java/org/apache/syncope/core/logic/RealmLogic.java
@@ -25,6 +25,7 @@ import java.util.Set;
 import java.util.stream.Collectors;
 import org.apache.commons.lang3.ArrayUtils;
 import org.apache.commons.lang3.StringUtils;
+import org.apache.commons.lang3.tuple.Pair;
 import org.apache.syncope.common.lib.SyncopeClientException;
 import org.apache.syncope.common.lib.to.ProvisioningResult;
 import org.apache.syncope.common.lib.to.RealmTO;
@@ -72,16 +73,21 @@ public class RealmLogic extends AbstractTransactionalLogic<RealmTO> {
 
     @PreAuthorize("hasRole('" + IdRepoEntitlement.REALM_LIST + "')")
     @Transactional(readOnly = true)
-    public List<RealmTO> search(final String keyword) {
+    public Pair<Integer, List<RealmTO>> search(final String keyword) {
         Set<String> bases = AuthContextUtils.getAuthorizations().get(IdRepoEntitlement.REALM_LIST);
 
-        return realmDAO.findMatching(keyword).stream().
+        Set<Realm> match = realmDAO.findMatching(keyword).stream().
                 filter(realm -> bases.stream().anyMatch(base -> realm.getFullPath().startsWith(base))).
-                flatMap(realm -> realmDAO.findDescendants(realm).stream()).
-                distinct().
-                map(realm -> binder.getRealmTO(realm, true)).
-                sorted(Comparator.comparing(RealmTO::getFullPath)).
-                collect(Collectors.toList());
+                collect(Collectors.toSet());
+
+        int descendants = Math.toIntExact(
+                match.stream().flatMap(realm -> realmDAO.findDescendants(realm).stream()).distinct().count());
+
+        return Pair.of(
+                descendants,
+                match.stream().map(realm -> binder.getRealmTO(realm, true)).
+                        sorted(Comparator.comparing(RealmTO::getFullPath)).
+                        collect(Collectors.toList()));
     }
 
     @PreAuthorize("isAuthenticated()")
diff --git a/core/idrepo/rest-cxf/src/main/java/org/apache/syncope/core/rest/cxf/service/RealmServiceImpl.java b/core/idrepo/rest-cxf/src/main/java/org/apache/syncope/core/rest/cxf/service/RealmServiceImpl.java
index 5767ad2..c80403e 100644
--- a/core/idrepo/rest-cxf/src/main/java/org/apache/syncope/core/rest/cxf/service/RealmServiceImpl.java
+++ b/core/idrepo/rest-cxf/src/main/java/org/apache/syncope/core/rest/cxf/service/RealmServiceImpl.java
@@ -22,7 +22,9 @@ import java.net.URI;
 import java.util.List;
 import javax.ws.rs.core.Response;
 import org.apache.commons.lang3.StringUtils;
+import org.apache.commons.lang3.tuple.Pair;
 import org.apache.syncope.common.lib.SyncopeConstants;
+import org.apache.syncope.common.lib.to.PagedResult;
 import org.apache.syncope.common.lib.to.ProvisioningResult;
 import org.apache.syncope.common.lib.to.RealmTO;
 import org.apache.syncope.common.rest.api.RESTHeaders;
@@ -39,9 +41,11 @@ public class RealmServiceImpl extends AbstractServiceImpl implements RealmServic
     private RealmLogic logic;
 
     @Override
-    public List<RealmTO> search(final RealmQuery query) {
+    public PagedResult<RealmTO> search(final RealmQuery query) {
         String keyword = query.getKeyword() == null ? null : query.getKeyword().replace('*', '%');
-        return logic.search(keyword);
+
+        Pair<Integer, List<RealmTO>> result = logic.search(keyword);
+        return buildPagedResult(result.getRight(), 1, result.getRight().size(), result.getLeft());
     }
 
     @Override
diff --git a/fit/core-reference/src/test/java/org/apache/syncope/fit/core/MultitenancyITCase.java b/fit/core-reference/src/test/java/org/apache/syncope/fit/core/MultitenancyITCase.java
index f5f16e0..bd8cdde 100644
--- a/fit/core-reference/src/test/java/org/apache/syncope/fit/core/MultitenancyITCase.java
+++ b/fit/core-reference/src/test/java/org/apache/syncope/fit/core/MultitenancyITCase.java
@@ -25,7 +25,6 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
 import static org.junit.jupiter.api.Assertions.fail;
 import static org.junit.jupiter.api.Assumptions.assumeFalse;
 
-import java.util.List;
 import java.util.Locale;
 import javax.ws.rs.ForbiddenException;
 import javax.ws.rs.core.GenericType;
@@ -114,16 +113,17 @@ public class MultitenancyITCase extends AbstractITCase {
 
     @Test
     public void readRealm() {
-        List<RealmTO> realms = adminClient.getService(RealmService.class).
+        PagedResult<RealmTO> realms = adminClient.getService(RealmService.class).
                 search(new RealmQuery.Builder().keyword("*").build());
-        assertEquals(1, realms.size());
-        assertEquals(SyncopeConstants.ROOT_REALM, realms.get(0).getName());
+        assertEquals(1, realms.getTotalCount());
+        assertEquals(1, realms.getResult().size());
+        assertEquals(SyncopeConstants.ROOT_REALM, realms.getResult().get(0).getName());
     }
 
     @Test
     public void createUser() {
         assertNull(adminClient.getService(RealmService.class).
-                search(new RealmQuery.Builder().keyword("*").build()).get(0).getPasswordPolicy());
+                search(new RealmQuery.Builder().keyword("*").build()).getResult().get(0).getPasswordPolicy());
 
         UserCR userCR = new UserCR();
         userCR.setRealm(SyncopeConstants.ROOT_REALM);
diff --git a/fit/core-reference/src/test/java/org/apache/syncope/fit/core/RealmITCase.java b/fit/core-reference/src/test/java/org/apache/syncope/fit/core/RealmITCase.java
index bc86a1b..1d8d5bc 100644
--- a/fit/core-reference/src/test/java/org/apache/syncope/fit/core/RealmITCase.java
+++ b/fit/core-reference/src/test/java/org/apache/syncope/fit/core/RealmITCase.java
@@ -36,6 +36,7 @@ import org.apache.syncope.common.lib.policy.AccountPolicyTO;
 import org.apache.syncope.common.lib.to.RealmTO;
 import org.apache.syncope.common.lib.policy.DefaultAccountRuleConf;
 import org.apache.syncope.common.lib.to.ImplementationTO;
+import org.apache.syncope.common.lib.to.PagedResult;
 import org.apache.syncope.common.lib.to.ProvisioningResult;
 import org.apache.syncope.common.lib.types.ClientExceptionType;
 import org.apache.syncope.common.lib.types.ImplementationEngine;
@@ -58,8 +59,8 @@ public class RealmITCase extends AbstractITCase {
 
     @Test
     public void search() {
-        List<RealmTO> match = realmService.search(new RealmQuery.Builder().keyword("*o*").build());
-        assertTrue(match.stream().allMatch(realm -> realm.getName().contains("o")));
+        PagedResult<RealmTO> match = realmService.search(new RealmQuery.Builder().keyword("*o*").build());
+        assertTrue(match.getResult().stream().allMatch(realm -> realm.getName().contains("o")));
     }
 
     @Test
diff --git a/fit/core-reference/src/test/java/org/apache/syncope/fit/core/UserITCase.java b/fit/core-reference/src/test/java/org/apache/syncope/fit/core/UserITCase.java
index f93a363..76da0ad 100644
--- a/fit/core-reference/src/test/java/org/apache/syncope/fit/core/UserITCase.java
+++ b/fit/core-reference/src/test/java/org/apache/syncope/fit/core/UserITCase.java
@@ -963,7 +963,7 @@ public class UserITCase extends AbstractITCase {
         passwordPolicy = createPolicy(PolicyType.PASSWORD, passwordPolicy);
         assertNotNull(passwordPolicy);
 
-        RealmTO realm = realmService.search(new RealmQuery.Builder().keyword("two").build()).get(0);
+        RealmTO realm = realmService.search(new RealmQuery.Builder().keyword("two").build()).getResult().get(0);
         String oldAccountPolicy = realm.getAccountPolicy();
         realm.setAccountPolicy(accountPolicy.getKey());
         String oldPasswordPolicy = realm.getPasswordPolicy();
diff --git a/fit/core-reference/src/test/java/org/apache/syncope/fit/core/UserIssuesITCase.java b/fit/core-reference/src/test/java/org/apache/syncope/fit/core/UserIssuesITCase.java
index a24107c..818fffa 100644
--- a/fit/core-reference/src/test/java/org/apache/syncope/fit/core/UserIssuesITCase.java
+++ b/fit/core-reference/src/test/java/org/apache/syncope/fit/core/UserIssuesITCase.java
@@ -793,7 +793,7 @@ public class UserIssuesITCase extends AbstractITCase {
         }
         assertNotNull(logicActions);
 
-        RealmTO realm = realmService.search(new RealmQuery.Builder().keyword("two").build()).get(0);
+        RealmTO realm = realmService.search(new RealmQuery.Builder().keyword("two").build()).getResult().get(0);
         assertNotNull(realm);
         realm.getActions().add(logicActions.getKey());
         realmService.update(realm);
@@ -1223,7 +1223,7 @@ public class UserIssuesITCase extends AbstractITCase {
         passwordPolicy = createPolicy(PolicyType.PASSWORD, passwordPolicy);
         assertNotNull(passwordPolicy);
 
-        RealmTO realm = realmService.search(new RealmQuery.Builder().keyword("two").build()).get(0);
+        RealmTO realm = realmService.search(new RealmQuery.Builder().keyword("two").build()).getResult().get(0);
         String oldPasswordPolicy = realm.getPasswordPolicy();
         realm.setPasswordPolicy(passwordPolicy.getKey());
         realmService.update(realm);