diff --git a/.classpath b/.classpath index 28af2a9..79174f7 100644 --- a/.classpath +++ b/.classpath @@ -17,6 +17,10 @@ - + + + + + diff --git a/pom.xml b/pom.xml index 32880a7..16fe2ec 100644 --- a/pom.xml +++ b/pom.xml @@ -78,11 +78,6 @@ social-networking-service-model [1.0.0-SNAPSHOT,) - - - - - org.glassfish.jersey.media jersey-media-json-jackson diff --git a/src/main/java/org/gcube/portal/social_networking_client_library/HashTagsClient.java b/src/main/java/org/gcube/portal/social_networking_client_library/HashTagClient.java similarity index 84% rename from src/main/java/org/gcube/portal/social_networking_client_library/HashTagsClient.java rename to src/main/java/org/gcube/portal/social_networking_client_library/HashTagClient.java index 78de3b5..7ca22ce 100644 --- a/src/main/java/org/gcube/portal/social_networking_client_library/HashTagsClient.java +++ b/src/main/java/org/gcube/portal/social_networking_client_library/HashTagClient.java @@ -14,16 +14,16 @@ import org.slf4j.LoggerFactory; * HashTags client. * @author Costantino Perciante at ISTI-CNR (costantino.perciante@isti.cnr.it) */ -public class HashTagsClient extends BaseClient{ - +public class HashTagClient extends BaseClient{ + private static final String SUB_SERVICE_PATH = "2/hashtags/"; - private static Logger logger = LoggerFactory.getLogger(HashTagsClient.class); - - - public HashTagsClient() throws Exception { + private static Logger logger = LoggerFactory.getLogger(HashTagClient.class); + + + public HashTagClient() throws Exception { super(SUB_SERVICE_PATH); } - + /** * Get hashtags and their count * @return a map of type hashtag -> number @@ -34,6 +34,5 @@ public class HashTagsClient extends BaseClient{ String request = getServiceEndpoint() + thisMethodSignature; return HttpClient.get(new GenericType>>(){}, request); } - } diff --git a/src/main/java/org/gcube/portal/social_networking_client_library/MessagesClient.java b/src/main/java/org/gcube/portal/social_networking_client_library/MessageClient.java similarity index 88% rename from src/main/java/org/gcube/portal/social_networking_client_library/MessagesClient.java rename to src/main/java/org/gcube/portal/social_networking_client_library/MessageClient.java index 441dff6..f019da6 100644 --- a/src/main/java/org/gcube/portal/social_networking_client_library/MessagesClient.java +++ b/src/main/java/org/gcube/portal/social_networking_client_library/MessageClient.java @@ -7,7 +7,7 @@ import javax.ws.rs.core.GenericType; import org.apache.commons.lang.Validate; import org.gcube.portal.social_networking_client_library.utils.HttpClient; -import org.gcube.portal.socialnetworking.model.input.Message; +import org.gcube.portal.socialnetworking.model.beans.Message; import org.gcube.portal.socialnetworking.model.output.ResponseBean; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -16,13 +16,13 @@ import org.slf4j.LoggerFactory; * Messages client. * @author Costantino Perciante at ISTI-CNR (costantino.perciante@isti.cnr.it) */ -public class MessagesClient extends BaseClient{ +public class MessageClient extends BaseClient{ private static final String SUB_SERVICE_PATH = "2/messages/"; - private static Logger logger = LoggerFactory.getLogger(MessagesClient.class); + private static Logger logger = LoggerFactory.getLogger(MessageClient.class); - public MessagesClient() throws Exception { + public MessageClient() throws Exception { super(SUB_SERVICE_PATH); } diff --git a/src/main/java/org/gcube/portal/social_networking_client_library/NotificationsClient.java b/src/main/java/org/gcube/portal/social_networking_client_library/NotificationClient.java similarity index 90% rename from src/main/java/org/gcube/portal/social_networking_client_library/NotificationsClient.java rename to src/main/java/org/gcube/portal/social_networking_client_library/NotificationClient.java index 4593f16..fc0d941 100644 --- a/src/main/java/org/gcube/portal/social_networking_client_library/NotificationsClient.java +++ b/src/main/java/org/gcube/portal/social_networking_client_library/NotificationClient.java @@ -8,7 +8,7 @@ import javax.ws.rs.core.GenericType; import org.apache.commons.lang.Validate; import org.gcube.portal.databook.shared.Notification; import org.gcube.portal.social_networking_client_library.utils.HttpClient; -import org.gcube.portal.socialnetworking.model.input.JobNotificationBean; +import org.gcube.portal.socialnetworking.model.beans.JobNotificationBean; import org.gcube.portal.socialnetworking.model.output.ResponseBean; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -17,13 +17,13 @@ import org.slf4j.LoggerFactory; * Notifications client. * @author Costantino Perciante at ISTI-CNR (costantino.perciante@isti.cnr.it) */ -public class NotificationsClient extends BaseClient{ +public class NotificationClient extends BaseClient{ private static final String SUB_SERVICE_PATH = "2/notifications/"; - private static Logger logger = LoggerFactory.getLogger(NotificationsClient.class); + private static Logger logger = LoggerFactory.getLogger(NotificationClient.class); - public NotificationsClient() throws Exception { + public NotificationClient() throws Exception { super(SUB_SERVICE_PATH); } diff --git a/src/main/java/org/gcube/portal/social_networking_client_library/PeopleClient.java b/src/main/java/org/gcube/portal/social_networking_client_library/PeopleClient.java index 492eed1..e994d58 100644 --- a/src/main/java/org/gcube/portal/social_networking_client_library/PeopleClient.java +++ b/src/main/java/org/gcube/portal/social_networking_client_library/PeopleClient.java @@ -3,8 +3,8 @@ package org.gcube.portal.social_networking_client_library; import javax.ws.rs.core.GenericType; import org.gcube.portal.social_networking_client_library.utils.HttpClient; +import org.gcube.portal.socialnetworking.model.beans.UserProfile; import org.gcube.portal.socialnetworking.model.output.ResponseBean; -import org.json.JSONObject; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -15,7 +15,7 @@ import org.slf4j.LoggerFactory; public class PeopleClient extends BaseClient{ private static final String SUB_SERVICE_PATH = "2/people/"; - private static Logger logger = LoggerFactory.getLogger(NotificationsClient.class); + private static Logger logger = LoggerFactory.getLogger(NotificationClient.class); public PeopleClient() throws Exception { @@ -26,12 +26,12 @@ public class PeopleClient extends BaseClient{ * Get profile * @return user's profile */ - public JSONObject getProfile(){ + public UserProfile getProfile(){ logger.debug("Request for getting profile"); String thisMethodSignature = "profile"; String request = getServiceEndpoint() + thisMethodSignature; - return HttpClient.get(new GenericType>(){}, request); + return HttpClient.get(new GenericType>(){}, request); } } diff --git a/src/main/java/org/gcube/portal/social_networking_client_library/PostsClient.java b/src/main/java/org/gcube/portal/social_networking_client_library/PostClient.java similarity index 79% rename from src/main/java/org/gcube/portal/social_networking_client_library/PostsClient.java rename to src/main/java/org/gcube/portal/social_networking_client_library/PostClient.java index 3fac04a..5410ffc 100644 --- a/src/main/java/org/gcube/portal/social_networking_client_library/PostsClient.java +++ b/src/main/java/org/gcube/portal/social_networking_client_library/PostClient.java @@ -1,5 +1,7 @@ package org.gcube.portal.social_networking_client_library; +import java.io.UnsupportedEncodingException; +import java.net.URLEncoder; import java.util.ArrayList; import java.util.List; @@ -8,7 +10,7 @@ import javax.ws.rs.core.GenericType; import org.apache.commons.lang.Validate; import org.gcube.portal.databook.shared.Feed; import org.gcube.portal.social_networking_client_library.utils.HttpClient; -import org.gcube.portal.socialnetworking.model.input.PostInputBean; +import org.gcube.portal.socialnetworking.model.beans.PostInputBean; import org.gcube.portal.socialnetworking.model.output.ResponseBean; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -17,13 +19,13 @@ import org.slf4j.LoggerFactory; * Posts client. * @author Costantino Perciante at ISTI-CNR (costantino.perciante@isti.cnr.it) */ -public class PostsClient extends BaseClient{ +public class PostClient extends BaseClient{ private static final String SUB_SERVICE_PATH = "2/posts/"; - private static Logger logger = LoggerFactory.getLogger(PostsClient.class); + private static Logger logger = LoggerFactory.getLogger(PostClient.class); - public PostsClient() throws Exception { + public PostClient() throws Exception { super(SUB_SERVICE_PATH); } @@ -31,13 +33,13 @@ public class PostsClient extends BaseClient{ * Get posts since date * @return */ - public List getPostsSinceDate(long timeInMillis){ + public List getUserPostsSinceDate(long timeInMillis){ Validate.isTrue(timeInMillis >= 0, "time cannot be negative"); logger.debug("Request for getting posts"); String thisMethodSignature = "get-posts-user-since"; - String request = getServiceEndpoint() + thisMethodSignature; + String request = getServiceEndpoint() + thisMethodSignature + "?time=" + timeInMillis; return HttpClient.get(new GenericType>>(){}, request); } @@ -46,9 +48,7 @@ public class PostsClient extends BaseClient{ * Get all posts * @return */ - public List getAllPosts(long timeInMillis){ - - Validate.isTrue(timeInMillis >= 0, "time cannot be negative"); + public List getAllUserPosts(){ logger.debug("Request for getting posts"); String thisMethodSignature = "get-posts-user"; @@ -61,13 +61,13 @@ public class PostsClient extends BaseClient{ * Get posts quantity * @return */ - public List getPostsQuantity(int quantity){ + public List getUserPostsQuantity(int quantity){ Validate.isTrue(quantity >= 0, "quantity cannot be negative"); logger.debug("Request for getting posts"); String thisMethodSignature = "get-posts-user-quantity"; - String request = getServiceEndpoint() + thisMethodSignature; + String request = getServiceEndpoint() + thisMethodSignature + "?quantity=" + quantity; return HttpClient.get(new GenericType>>(){}, request); } @@ -76,7 +76,7 @@ public class PostsClient extends BaseClient{ * Write post * @return */ - public Feed write(PostInputBean toWrite){ + public Feed writeUserPost(PostInputBean toWrite){ Validate.isTrue(toWrite != null, "Post to write cannot be null"); @@ -131,14 +131,15 @@ public class PostsClient extends BaseClient{ /** * Get posts hashtag * @return + * @throws UnsupportedEncodingException */ - public List getHashtagPosts(String hashtag){ + public List getHashtagPosts(String hashtag) throws UnsupportedEncodingException{ Validate.isTrue(hashtag != null, "hashtag cannot be null"); logger.debug("Request for getting posts with hashtag " + hashtag); String thisMethodSignature = "get-posts-by-hashtag"; - String request = getServiceEndpoint() + thisMethodSignature; + String request = getServiceEndpoint() + thisMethodSignature + "?hashtag=" + URLEncoder.encode(hashtag, "UTF-8"); return HttpClient.get(new GenericType>>(){}, request); } @@ -147,7 +148,7 @@ public class PostsClient extends BaseClient{ * Get liked posts * @return */ - public List getLikedPost(){ + public List getUserLikedPost(){ logger.debug("Request for getting posts liked"); String thisMethodSignature = "get-liked-posts"; @@ -160,7 +161,7 @@ public class PostsClient extends BaseClient{ * Get liked posts ids * @return */ - public List getLikedPostIds(){ + public List getUserLikedPostIds(){ logger.debug("Request for getting posts liked"); String thisMethodSignature = "get-id-liked-posts"; diff --git a/src/main/java/org/gcube/portal/social_networking_client_library/TokensClient.java b/src/main/java/org/gcube/portal/social_networking_client_library/TokenClient.java similarity index 81% rename from src/main/java/org/gcube/portal/social_networking_client_library/TokensClient.java rename to src/main/java/org/gcube/portal/social_networking_client_library/TokenClient.java index 65c2b55..a8659e8 100644 --- a/src/main/java/org/gcube/portal/social_networking_client_library/TokensClient.java +++ b/src/main/java/org/gcube/portal/social_networking_client_library/TokenClient.java @@ -4,7 +4,7 @@ import javax.ws.rs.core.GenericType; import org.apache.commons.lang.Validate; import org.gcube.portal.social_networking_client_library.utils.HttpClient; -import org.gcube.portal.socialnetworking.model.input.ApplicationId; +import org.gcube.portal.socialnetworking.model.beans.ApplicationId; import org.gcube.portal.socialnetworking.model.output.ResponseBean; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -13,13 +13,13 @@ import org.slf4j.LoggerFactory; * Tokens client. * @author Costantino Perciante at ISTI-CNR (costantino.perciante@isti.cnr.it) */ -public class TokensClient extends BaseClient{ +public class TokenClient extends BaseClient{ private static final String SUB_SERVICE_PATH = "2/tokens/"; - private static Logger logger = LoggerFactory.getLogger(TokensClient.class); + private static Logger logger = LoggerFactory.getLogger(TokenClient.class); - public TokensClient() throws Exception { + public TokenClient() throws Exception { super(SUB_SERVICE_PATH); } diff --git a/src/main/java/org/gcube/portal/social_networking_client_library/UsersClient.java b/src/main/java/org/gcube/portal/social_networking_client_library/UserClient.java similarity index 92% rename from src/main/java/org/gcube/portal/social_networking_client_library/UsersClient.java rename to src/main/java/org/gcube/portal/social_networking_client_library/UserClient.java index 5acf5e8..e921da2 100644 --- a/src/main/java/org/gcube/portal/social_networking_client_library/UsersClient.java +++ b/src/main/java/org/gcube/portal/social_networking_client_library/UserClient.java @@ -9,8 +9,8 @@ import javax.ws.rs.core.GenericType; import org.apache.commons.lang.Validate; import org.gcube.portal.social_networking_client_library.utils.HttpClient; +import org.gcube.portal.socialnetworking.model.beans.UserProfileExtended; import org.gcube.portal.socialnetworking.model.output.ResponseBean; -import org.json.JSONObject; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -18,13 +18,13 @@ import org.slf4j.LoggerFactory; * Users client. * @author Costantino Perciante at ISTI-CNR (costantino.perciante@isti.cnr.it) */ -public class UsersClient extends BaseClient{ +public class UserClient extends BaseClient{ private static final String SUB_SERVICE_PATH = "2/users/"; - private static Logger logger = LoggerFactory.getLogger(UsersClient.class); + private static Logger logger = LoggerFactory.getLogger(UserClient.class); - public UsersClient() throws Exception { + public UserClient() throws Exception { super(SUB_SERVICE_PATH); } @@ -73,12 +73,12 @@ public class UsersClient extends BaseClient{ * Get profile * @return */ - public JSONObject getProfile(){ + public UserProfileExtended getProfile(){ logger.debug("Request for getting profile"); String thisMethodSignature = "get-profile"; String request = getServiceEndpoint() + thisMethodSignature; - return HttpClient.get(new GenericType>(){}, request); + return HttpClient.get(new GenericType>(){}, request); } diff --git a/src/main/java/org/gcube/portal/social_networking_client_library/VREsClient.java b/src/main/java/org/gcube/portal/social_networking_client_library/VREClient.java similarity index 62% rename from src/main/java/org/gcube/portal/social_networking_client_library/VREsClient.java rename to src/main/java/org/gcube/portal/social_networking_client_library/VREClient.java index f8050ac..d811f0b 100644 --- a/src/main/java/org/gcube/portal/social_networking_client_library/VREsClient.java +++ b/src/main/java/org/gcube/portal/social_networking_client_library/VREClient.java @@ -1,10 +1,12 @@ package org.gcube.portal.social_networking_client_library; +import java.util.List; + import javax.ws.rs.core.GenericType; import org.gcube.portal.social_networking_client_library.utils.HttpClient; +import org.gcube.portal.socialnetworking.model.beans.MyVRE; import org.gcube.portal.socialnetworking.model.output.ResponseBean; -import org.json.JSONArray; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -12,13 +14,13 @@ import org.slf4j.LoggerFactory; * Vres client. * @author Costantino Perciante at ISTI-CNR (costantino.perciante@isti.cnr.it) */ -public class VREsClient extends BaseClient{ +public class VREClient extends BaseClient{ private static final String SUB_SERVICE_PATH = "2/vres/"; - private static Logger logger = LoggerFactory.getLogger(VREsClient.class); + private static Logger logger = LoggerFactory.getLogger(VREClient.class); - public VREsClient() throws Exception { + public VREClient() throws Exception { super(SUB_SERVICE_PATH); } @@ -26,12 +28,12 @@ public class VREsClient extends BaseClient{ * Get my Vres * @return my vres */ - public JSONArray getMyVRES(){ + public List getMyVRES(boolean retrieveManagers){ logger.debug("Request for writing new message"); String thisMethodSignature = "get-my-vres"; - String request = getServiceEndpoint() + thisMethodSignature + "?"; - return HttpClient.get(new GenericType>(){}, request); + String request = getServiceEndpoint() + thisMethodSignature + "?" + "getManagers=" + retrieveManagers; + return HttpClient.get(new GenericType>>(){}, request); } diff --git a/src/main/java/org/gcube/portal/social_networking_client_library/filter/AuthorizationFilter.java b/src/main/java/org/gcube/portal/social_networking_client_library/filter/AuthorizationFilter.java index 5d64105..6c3376a 100644 --- a/src/main/java/org/gcube/portal/social_networking_client_library/filter/AuthorizationFilter.java +++ b/src/main/java/org/gcube/portal/social_networking_client_library/filter/AuthorizationFilter.java @@ -3,19 +3,24 @@ package org.gcube.portal.social_networking_client_library.filter; import java.io.IOException; import javax.ws.rs.client.ClientRequestContext; +import javax.ws.rs.client.ClientRequestFilter; import org.gcube.common.authorization.library.provider.SecurityTokenProvider; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** * Authorization filter for the jersey client. * @author Costantino Perciante at ISTI-CNR (costantino.perciante@isti.cnr.it) */ -public class AuthorizationFilter { - +public class AuthorizationFilter implements ClientRequestFilter{ private final static String AUTH_TOKEN_PARAMETER = "gcube-token"; + private static Logger logger = LoggerFactory.getLogger(AuthorizationFilter.class); - public void filter(ClientRequestContext clientRequestContext) throws IOException { + @Override + public void filter(ClientRequestContext original) throws IOException { + logger.debug("Adding token to the request " + original.getUri()); String token = SecurityTokenProvider.instance.get(); - clientRequestContext.getHeaders().add(AUTH_TOKEN_PARAMETER, token); + original.getHeaders().add(AUTH_TOKEN_PARAMETER, token); } } diff --git a/src/main/java/org/gcube/portal/social_networking_client_library/utils/HttpClient.java b/src/main/java/org/gcube/portal/social_networking_client_library/utils/HttpClient.java index 3fb3993..84c57bc 100644 --- a/src/main/java/org/gcube/portal/social_networking_client_library/utils/HttpClient.java +++ b/src/main/java/org/gcube/portal/social_networking_client_library/utils/HttpClient.java @@ -7,7 +7,9 @@ import javax.ws.rs.client.Entity; import javax.ws.rs.client.WebTarget; import javax.ws.rs.core.GenericType; import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; +import org.gcube.portal.social.networking.model.providers.CustomObjectMapper; import org.gcube.portal.social_networking_client_library.filter.AuthorizationFilter; import org.gcube.portal.socialnetworking.model.output.ResponseBean; import org.glassfish.jersey.client.ClientConfig; @@ -15,10 +17,8 @@ import org.glassfish.jersey.jackson.JacksonFeature; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import com.fasterxml.jackson.core.JsonProcessingException; - /** - * Http client with post and get methods (jersey is used) + * Jersey client with post and get methods * @author Costantino Perciante at ISTI-CNR (costantino.perciante@isti.cnr.it) */ public class HttpClient{ @@ -27,61 +27,63 @@ public class HttpClient{ /** * Executes a get request - * @param genericType - * @param genericType - * @param genericType * @param returnType * @param requestUrl - * @param token * @return */ public static R get(GenericType> returnType, String requestUrl){ logger.debug("Executing get request at url " + requestUrl); - ClientConfig cc = new ClientConfig().register(new JacksonFeature()).register(new AuthorizationFilter()); + ClientConfig cc = new ClientConfig().register(new JacksonFeature()).register(new AuthorizationFilter()).register(new CustomObjectMapper()); Client client = ClientBuilder.newClient(cc); WebTarget webResourceGet = client.target(requestUrl); - ResponseBean response = webResourceGet.request(MediaType.APPLICATION_JSON).accept(MediaType.APPLICATION_JSON).get(returnType); + Response response = webResourceGet.request(MediaType.APPLICATION_JSON).accept(MediaType.APPLICATION_JSON).get(); if(response == null) throw new WebApplicationException("Response returned by the service is null"); - if (response.getMessage() != null && !response.getMessage().isEmpty()) - throw new WebApplicationException( - "Error message is " + response.getMessage()); + if(response.getStatus() >= 300) + throw new WebApplicationException("There was an error at server side: " + response.getStatusInfo()); - return response.getResult(); + ResponseBean result = response.readEntity(returnType); + + if (!result.isSuccess()) + throw new WebApplicationException( + "Error message is " + result.getMessage()); + + return result.getResult(); } /** * Executes a post request * @param returnType * @param requestUrl - * @param token - * @param body + * @param obj * @return - * @throws JsonProcessingException */ public static R post(GenericType> returnType, String requestUrl, Object obj){ logger.debug("Executing post request at url " + requestUrl); - ClientConfig cc = new ClientConfig().register(new JacksonFeature()).register(new AuthorizationFilter()); + ClientConfig cc = new ClientConfig().register(new JacksonFeature()).register(new AuthorizationFilter()).register(new CustomObjectMapper());; Client client = ClientBuilder.newClient(cc); WebTarget webResourceGet = client.target(requestUrl); - logger.debug("Entity looks like " + Entity.json(obj)); - - ResponseBean response = webResourceGet.request(MediaType.APPLICATION_JSON).accept(MediaType.APPLICATION_JSON).post(Entity.json(obj), returnType); + Response response = webResourceGet.request(MediaType.APPLICATION_JSON).accept(MediaType.APPLICATION_JSON).post(Entity.json(obj)); if(response == null) throw new WebApplicationException("Response returned by the service is null"); - if (response.getMessage() != null && !response.getMessage().isEmpty()) - throw new WebApplicationException( - "Error message is " + response.getMessage()); + if(response.getStatus() >= 300) + throw new WebApplicationException("There was an error at server side: " + response.getStatusInfo()); - return response.getResult(); + ResponseBean result = response.readEntity(returnType); + + if (!result.isSuccess()) + throw new WebApplicationException( + "Error message is " + result.getMessage()); + + return result.getResult(); } } diff --git a/src/main/java/org/gcube/portal/social_networking_client_library/utils/ServiceDiscoverer.java b/src/main/java/org/gcube/portal/social_networking_client_library/utils/ServiceDiscoverer.java index 7432fcb..557d471 100644 --- a/src/main/java/org/gcube/portal/social_networking_client_library/utils/ServiceDiscoverer.java +++ b/src/main/java/org/gcube/portal/social_networking_client_library/utils/ServiceDiscoverer.java @@ -16,6 +16,10 @@ import org.gcube.resources.discovery.client.queries.api.SimpleQuery; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +/** + * Discovers the social networking service endpoint in a given context + * @author Costantino Perciante at ISTI-CNR (costantino.perciante@isti.cnr.it) + */ public class ServiceDiscoverer { private static final String resource = "jersey-servlet"; @@ -38,7 +42,7 @@ public class ServiceDiscoverer { try{ - logger.info("set scope "+currentScope); + logger.debug("set scope "+currentScope); SimpleQuery query = queryFor(GCoreEndpoint.class); query.addCondition(String.format("$resource/Profile/ServiceClass/text() eq '%s'",serviceClass)); diff --git a/src/test/java/org/gcube/portal/social_networking_client_library/TestClientServices.java b/src/test/java/org/gcube/portal/social_networking_client_library/TestClientServices.java index a984faf..5ed1130 100644 --- a/src/test/java/org/gcube/portal/social_networking_client_library/TestClientServices.java +++ b/src/test/java/org/gcube/portal/social_networking_client_library/TestClientServices.java @@ -9,14 +9,17 @@ import java.util.UUID; import org.gcube.common.authorization.library.provider.SecurityTokenProvider; import org.gcube.common.scope.api.ScopeProvider; import org.gcube.portal.databook.shared.EnhancedFeed; +import org.gcube.portal.databook.shared.Feed; import org.gcube.portal.databook.shared.JobStatusType; import org.gcube.portal.databook.shared.Notification; -import org.gcube.portal.socialnetworking.model.input.ApplicationId; -import org.gcube.portal.socialnetworking.model.input.JobNotificationBean; -import org.gcube.portal.socialnetworking.model.input.Message; -import org.gcube.portal.socialnetworking.model.input.Recipient; -import org.json.JSONArray; -import org.json.JSONObject; +import org.gcube.portal.socialnetworking.model.beans.ApplicationId; +import org.gcube.portal.socialnetworking.model.beans.JobNotificationBean; +import org.gcube.portal.socialnetworking.model.beans.Message; +import org.gcube.portal.socialnetworking.model.beans.MyVRE; +import org.gcube.portal.socialnetworking.model.beans.PostInputBean; +import org.gcube.portal.socialnetworking.model.beans.Recipient; +import org.gcube.portal.socialnetworking.model.beans.UserProfile; +import org.gcube.portal.socialnetworking.model.beans.UserProfileExtended; import org.junit.After; import org.junit.Before; import org.slf4j.Logger; @@ -30,7 +33,7 @@ public class TestClientServices { public void setContextAndToken(){ ScopeProvider.instance.set("/gcube/devNext/NextNext"); - SecurityTokenProvider.instance.set("d423aed7-e9e2-424a-b9e7-2bbbd151d9c4-98187548"); + SecurityTokenProvider.instance.set("..."); } @@ -56,14 +59,14 @@ public class TestClientServices { //@Test public void testHashTags() throws Exception { - HashTagsClient hashtags = new HashTagsClient(); + HashTagClient hashtags = new HashTagClient(); Map hashtagsCount = hashtags.getHashtagsCount(); logger.debug("Returned tags " + hashtagsCount); } //@Test public void testMessagesReceived() throws Exception { - MessagesClient messagesClient = new MessagesClient(); + MessageClient messagesClient = new MessageClient(); List receivedMessages = messagesClient.getReceivedMessages(); for (Message workspaceMessage : receivedMessages) { logger.debug("Received message " + workspaceMessage.toString()); @@ -72,7 +75,7 @@ public class TestClientServices { //@Test public void testMessagesSent() throws Exception { - MessagesClient messagesClient = new MessagesClient(); + MessageClient messagesClient = new MessageClient(); List sentMessages = messagesClient.getSentMessages(); for (Message workspaceMessage : sentMessages) { logger.debug("Sent message was " + workspaceMessage); @@ -82,7 +85,7 @@ public class TestClientServices { //@Test public void sendMessage() throws Exception{ logger.debug("Sending message "); - MessagesClient messagesClient = new MessagesClient(); + MessageClient messagesClient = new MessageClient(); List rec = Arrays.asList(new Recipient("andrea.rossi")); Message message = new Message( "Test message", @@ -94,7 +97,7 @@ public class TestClientServices { //@Test public void testNotifications() throws Exception { - NotificationsClient notificationsClient = new NotificationsClient(); + NotificationClient notificationsClient = new NotificationClient(); List latestNotifications = notificationsClient.getNotifications(1, 2); for (Notification notification : latestNotifications) { logger.debug("Notification is " + notification); @@ -103,7 +106,7 @@ public class TestClientServices { //@Test public void sendJobNotification() throws Exception { - NotificationsClient notificationsClient = new NotificationsClient(); + NotificationClient notificationsClient = new NotificationClient(); JobNotificationBean notification = new JobNotificationBean( "costantino.perciante", UUID.randomUUID().toString(), @@ -118,24 +121,198 @@ public class TestClientServices { //@Test public void getProfile() throws Exception { PeopleClient getProfile = new PeopleClient(); - JSONObject profile = getProfile.getProfile(); + UserProfile profile = getProfile.getProfile(); logger.debug("Profile retrieved is " + profile); } //@Test public void generateAppToken() throws Exception { - TokensClient tokenClient = new TokensClient(); - String token = tokenClient.generateApplicationToken(new ApplicationId("test_app")); + TokenClient tokenClient = new TokenClient(); + String token = tokenClient.generateApplicationToken(new ApplicationId("org.gcube.datacatalogue.GRSFNotifier")); logger.debug("Generated token is " + token); } //@Test public void getMyVres() throws Exception{ - VREsClient myVresClient = new VREsClient(); - JSONArray myVres = myVresClient.getMyVRES(); + VREClient myVresClient = new VREClient(); + List myVres = myVresClient.getMyVRES(false); logger.debug("My Vres " + myVres); } + //@Test + public void UsersClientTests() throws Exception{ + + UserClient userClient = new UserClient(); + + // Requires user token bound to the root context + // List administrators = userClient.getAllUsernamesByGlobalRole("administrator"); + // + // assert(administrators != null && !administrators.isEmpty()); + + // logger.debug("Administrators are " + administrators); + + List vreManagers = userClient.getAllUsernamesByLocalRole("vre-manager"); + + assert(vreManagers != null && !vreManagers.isEmpty()); + + logger.debug("Vre Managers are " + vreManagers); + + String myEmail = userClient.getEmail(); + + assert(myEmail != null && !myEmail.isEmpty()); + + logger.debug("My email is " + myEmail); + + String fullname = userClient.getFullName(); + + assert(fullname != null && !fullname.isEmpty()); + + logger.debug("My fullname is " + fullname); + + String industry = userClient.getCustomAttribute("Industry"); + + assert(industry != null && !industry.isEmpty()); + + logger.debug("My Industry is " + industry); + + List usernamesInThisContext = userClient.getAllUsernamesContext(); + + assert(usernamesInThisContext != null && !usernamesInThisContext.isEmpty()); + + logger.debug("List of usernames in this context is " + usernamesInThisContext); + + Map usernamesFullnameInThisContext = userClient.getAllUsernamesFullnamesContext(); + + assert(usernamesFullnameInThisContext != null && !usernamesFullnameInThisContext.isEmpty()); + + logger.debug("List of tuples usernames/fullnames in this context is " + usernamesFullnameInThisContext); + + UserProfileExtended profile = userClient.getProfile(); + + assert(profile != null); + + logger.debug("My wonderful profile is " + profile); + + // Requires application token bound to the root context + // Boolean userExists = userClient.userExists("andrea.rossi"); + // + // assert(userExists != null && userExists == true); + // + // logger.debug("does Andrea Rossi exist? " + userExists); + + } + + //@Test + public void getUserPostsSinceDate() throws Exception{ + + PostClient postClient = new PostClient(); + List sinceLastYearPost = postClient.getUserPostsSinceDate(System.currentTimeMillis() - 1000 * 60 * 60 * 24 * 12); + logger.debug("Posts are " + sinceLastYearPost); + + } + + //@Test + public void getAllUserPosts() throws Exception{ + + PostClient postClient = new PostClient(); + List allposts = postClient.getAllUserPosts(); + logger.debug("All posts are " + allposts); + + } + + + //@Test + public void getUserPostsQuantity() throws Exception{ + + PostClient postClient = new PostClient(); + List quantityPosts = postClient.getUserPostsQuantity(3); + logger.debug("Some posts are " + quantityPosts); + } + + //@Test + public void writeUserPost() throws Exception{ + + PostClient postClient = new PostClient(); + PostInputBean toWrite = new PostInputBean("Testing social networking rest client", null, null, null, null, null, false, null); + Feed written = postClient.writeUserPost(toWrite); + logger.debug("Written post is " + written); + + } + + //@Test + public void getAllApplicationPosts() throws Exception{ + + TokenClient tokenClient = new TokenClient(); + String token = tokenClient.generateApplicationToken(new ApplicationId("org.gcube.datacatalogue.GRSFNotifier")); + logger.debug("Generated token is " + token); + + String currentToken = SecurityTokenProvider.instance.get(); + SecurityTokenProvider.instance.set(token); + + PostClient postClient = new PostClient(); + List applicationPosts = postClient.getAllApplicationPosts(); + logger.debug("Application posts are " + applicationPosts); + + SecurityTokenProvider.instance.set(currentToken); + + } + + //@Test + public void writeApplicationPost() throws Exception{ + + TokenClient tokenClient = new TokenClient(); + String token = tokenClient.generateApplicationToken(new ApplicationId("org.gcube.datacatalogue.GRSFNotifier")); + logger.debug("Generated token is " + token); + + String currentToken = SecurityTokenProvider.instance.get(); + SecurityTokenProvider.instance.set(token); + + PostClient postClient = new PostClient(); + PostInputBean toWrite = new PostInputBean("Testing social networking rest client [via application token] #apptoken #rest #client #java", null, null, null, null, null, false, null); + Feed written = postClient.writeApplicationPost(toWrite); + logger.debug("Written post is " + written); + + SecurityTokenProvider.instance.set(currentToken); + + } + + //@Test + public void getPostsVRE() throws Exception{ + + PostClient postClient = new PostClient(); + List vrePosts = postClient.getPostsVRE(); + logger.debug("VRE posts are " + vrePosts); + + } + + //@Test + public void getHashtagPosts() throws Exception{ + + PostClient postClient = new PostClient(); + List postsWithHashtag = postClient.getHashtagPosts("#connect"); + logger.debug("Posts with hashtag #connect are " + postsWithHashtag); + + } + + //@Test + public void getUserLikedPost() throws Exception{ + + PostClient postClient = new PostClient(); + List postsLiked = postClient.getUserLikedPost(); + logger.debug("Posts liked are " + postsLiked); + + } + + //@Test + public void getUserLikedPostIds() throws Exception{ + + PostClient postClient = new PostClient(); + List postsLikedIds = postClient.getUserLikedPostIds(); + logger.debug("Posts liked's ids are " + postsLikedIds); + + } + + @After public void reset(){ ScopeProvider.instance.reset();