From 2c4def3e928e6e4af6578693cda7043e858edf11 Mon Sep 17 00:00:00 2001 From: Ali HAMDI Date: Fri, 8 Nov 2019 11:51:13 +0100 Subject: [PATCH] PLF-8554 : Suggestions should be continuously filled from last registered users and shared connections (#411) * PLF-8554 : Suggestions should be continuously filled from last registered users and shared connections --- .../PeopleRestServices.java | 24 +++++++++---------- .../TestPeopleRestServices.java | 24 +++++++++++++++++-- 2 files changed, 33 insertions(+), 15 deletions(-) diff --git a/component/common/src/main/java/org/exoplatform/platform/common/rest/services/SuggestPeoplePortlet/PeopleRestServices.java b/component/common/src/main/java/org/exoplatform/platform/common/rest/services/SuggestPeoplePortlet/PeopleRestServices.java index 86215d28d2..7277d6b844 100644 --- a/component/common/src/main/java/org/exoplatform/platform/common/rest/services/SuggestPeoplePortlet/PeopleRestServices.java +++ b/component/common/src/main/java/org/exoplatform/platform/common/rest/services/SuggestPeoplePortlet/PeopleRestServices.java @@ -27,16 +27,14 @@ import javax.ws.rs.core.SecurityContext; import javax.ws.rs.core.UriInfo; import javax.ws.rs.ext.RuntimeDelegate; -import java.util.Collections; -import java.util.LinkedHashMap; -import java.util.List; -import java.util.Map; +import java.util.*; import java.util.Map.Entry; @Path("/homepage/intranet/people/") @Produces("application/json") public class PeopleRestServices implements ResourceContainer { + private static final int NUMBER_OF_SUGGESTIONS = 10; private static Log log = ExoLogger.getLogger(PeopleRestServices.class); private static final CacheControl cacheControl; @@ -232,27 +230,27 @@ public Response getSuggestions(@Context SecurityContext sc, @Context UriInfo uri ListAccess connectionList = relationshipManager.getConnections(identity); int size = connectionList.getSize(); - Map suggestions; + Map connectionsSuggestions; if (size > 0) { - suggestions = relationshipManager.getSuggestions(identity, 20, 50, 10); - if (suggestions.size() == 1 && suggestions.keySet().iterator().next().getRemoteId().equals(userACL.getSuperUser())) { + connectionsSuggestions = relationshipManager.getSuggestions(identity, 20, 50, 10); + if (connectionsSuggestions.size() == 1 && connectionsSuggestions.keySet().iterator().next().getRemoteId().equals(userACL.getSuperUser())) { // The only suggestion is the super user so we clear the suggestion list - suggestions = Collections.emptyMap(); + connectionsSuggestions = Collections.emptyMap(); } } else { - suggestions = Collections.emptyMap(); + connectionsSuggestions = Collections.emptyMap(); } JSONObject jsonGlobal = new JSONObject(); JSONArray jsonArray = new JSONArray(); - if (suggestions.isEmpty()) { + Map suggestions = new HashMap<>(connectionsSuggestions); + if (connectionsSuggestions.size() < NUMBER_OF_SUGGESTIONS) { // Returns the last users - List identities = identityManager.getLastIdentities(10); - suggestions = new LinkedHashMap(); + List identities = identityManager.getLastIdentities(NUMBER_OF_SUGGESTIONS - suggestions.size()); for (Identity id : identities) { if (identity.equals(id) || relationshipManager.get(identity, id) != null) continue; - suggestions.put(id, new Integer(0)); + suggestions.put(id, 0); } } for (Entry suggestion : suggestions.entrySet()) { diff --git a/component/common/src/test/java/org/exoplatform/platform/common/rest/services/SuggestPeoplePortlet/TestPeopleRestServices.java b/component/common/src/test/java/org/exoplatform/platform/common/rest/services/SuggestPeoplePortlet/TestPeopleRestServices.java index cd2f27845e..58f47c1909 100644 --- a/component/common/src/test/java/org/exoplatform/platform/common/rest/services/SuggestPeoplePortlet/TestPeopleRestServices.java +++ b/component/common/src/test/java/org/exoplatform/platform/common/rest/services/SuggestPeoplePortlet/TestPeopleRestServices.java @@ -47,6 +47,10 @@ public void testSuggestions() throws Exception { idFoo.setId("foo"); Identity idBar = new Identity(OrganizationIdentityProvider.NAME, "bar"); idBar.setId("bar"); + Identity idBaz = new Identity(OrganizationIdentityProvider.NAME, "baz"); + idBaz.setId("baz"); + Identity idQux = new Identity(OrganizationIdentityProvider.NAME, "qux"); + idBaz.setId("qux"); envctx.put(SecurityContext.class, new MockSecurityContext(idFoo.getRemoteId())); Map imResults = new HashMap(); @@ -84,7 +88,7 @@ public void testSuggestions() throws Exception { assertTrue(json.has("items")); assertTrue(json.getJSONArray("items").length() == 0); - // The only suggestion is demo + // The only suggestion is bar rmResults.put("getConnections", new MockListAccess(new Identity[]{idBar})); rmResults.put("getSuggestions", Collections.singletonMap(idRoot, 1)); rmResults.remove("get"); @@ -94,9 +98,25 @@ public void testSuggestions() throws Exception { assertEquals("application/json", resp.getContentType().toString()); json = new JSONObject(resp.getEntity().toString()); assertTrue(json.has("items")); - assertTrue(json.getJSONArray("items").length() == 1); + assertEquals(1, json.getJSONArray("items").length()); assertEquals(json.getJSONArray("items").getJSONObject(0).getString("username"), idBar.getRemoteId()); + // There is one suggestion and one connection to a contact + // bar is the suggestion since he is a connection + // baz is a new suggestion as he is a new registered user + Map suggestions = new HashMap<>(); + suggestions.put(idRoot, 1); + suggestions.put(idBar, 2); + imResults.put("getLastIdentities", Arrays.asList(idRoot, idFoo, idBaz)); + rmResults.put("getConnections", new MockListAccess(new Identity[]{idQux})); + rmResults.put("getSuggestions", suggestions); + resp = launcher.service("GET", path, "", null, null, envctx); + assertEquals(200, resp.getStatus()); + assertEquals("application/json", resp.getContentType().toString()); + json = new JSONObject(resp.getEntity().toString()); + assertTrue(json.has("items")); + assertEquals(2, json.getJSONArray("items").length()); + getContainer().unregisterComponent("UserACL"); getContainer().unregisterComponent("RelationshipManager"); getContainer().unregisterComponent("IdentityManager");