social-service-client/src/test/java/org/gcube/portal/social_networking_client_li.../TestClientServices.java

334 lines
13 KiB
Java

package org.gcube.portal.social_networking_client_library;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import org.gcube.common.authorization.library.provider.AccessTokenProvider;
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.social_networking.social_networking_client_library.FullTextSearchClient;
import org.gcube.social_networking.social_networking_client_library.HashTagClient;
import org.gcube.social_networking.social_networking_client_library.MessageClient;
import org.gcube.social_networking.social_networking_client_library.NotificationClient;
import org.gcube.social_networking.social_networking_client_library.PeopleClient;
import org.gcube.social_networking.social_networking_client_library.PostClient;
import org.gcube.social_networking.social_networking_client_library.TokenClient;
import org.gcube.social_networking.social_networking_client_library.UserClient;
import org.gcube.social_networking.social_networking_client_library.VREClient;
import org.gcube.social_networking.socialnetworking.model.beans.ApplicationId;
import org.gcube.social_networking.socialnetworking.model.beans.JobNotificationBean;
import org.gcube.social_networking.socialnetworking.model.beans.Message;
import org.gcube.social_networking.socialnetworking.model.beans.MessageInbox;
import org.gcube.social_networking.socialnetworking.model.beans.MyVRE;
import org.gcube.social_networking.socialnetworking.model.beans.PostInputBean;
import org.gcube.social_networking.socialnetworking.model.beans.Recipient;
import org.gcube.social_networking.socialnetworking.model.beans.UserProfile;
import org.gcube.social_networking.socialnetworking.model.beans.UserProfileExtended;
import org.junit.Before;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class TestClientServices {
private static Logger logger = LoggerFactory.getLogger(TestClientServices.class);
@Before
public void setContextAndToken(){
ScopeProvider.instance.set("/gcube/devsec/devVRE");
AccessTokenProvider.instance.set("eyJhbGciOiJSUzI1NiIsInR5cCIgOiAiSldUIiw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iIsImxvY2FsZSI6ImVuIiwiZmFtaWx5X25hbWUiOiJBc3NhbnRlIiwiZW1haWwiOiJtYXNzaW1pbGlhbm8uYXNzYW50ZUBpc3RpLmNuci5pdCJ9.MFee4w8XAgchOHGiqMYnmYCpclNOmC8xiZMkoqeg3UnaN9iEslwJJtAjeZiEdCBlnypw9Y1a8Fisc5p6f2IBBMaDtbE_omYTStBSDT84ZJMrspHIjstAinax0sY0h2tG-DBhdK6AI0D-MFiOL07FW4iQgizXh4qnpzVNL0vxIUQMYo99R4DYsZzcsrxnUL0qzhNHfN4PqdMRWpt-eRyKO15FvA7_HExAemOzYEFbRrw8JSpsXj6BNcRg9ZwRV7815WhbqhV-Cq3_PDjyiSw3mVGjeh1QmNTOSbBKniGYJo2nWBng3tP_HS0yoS1xRCS9d0PrjBEVdbRn-HlM4M43DA");
//SecurityTokenProvider.instance.set("");
}
@Test
public void testSearch() throws Exception {
FullTextSearchClient search = new FullTextSearchClient();
ArrayList<EnhancedFeed> result = search.search("looking for test", 0, 10);
for (EnhancedFeed enhancedPost : result) {
logger.debug("Returned post " + enhancedPost);
}
}
//@Test
public void testSearchEmptyList() throws Exception {
FullTextSearchClient search = new FullTextSearchClient();
ArrayList<EnhancedFeed> result = search.search("tipiterotipirè", 0, 10);
for (EnhancedFeed enhancedFeed : result) {
logger.debug("Returned feed " + enhancedFeed);
}
}
@Test
public void testHashTags() throws Exception {
HashTagClient hashtags = new HashTagClient();
Map<String, Integer> hashtagsCount = hashtags.getHashtagsCount();
logger.debug("Returned tags " + hashtagsCount);
}
@Test
public void testMessagesReceived() throws Exception {
MessageClient messagesClient = new MessageClient();
List<MessageInbox> receivedMessages = messagesClient.getReceivedMessages();
System.out.println("Messages found # " + receivedMessages.size());
for (MessageInbox workspaceMessage : receivedMessages) {
logger.debug("Received message " + workspaceMessage.toString());
}
}
@Test
public void testMessagesSent() throws Exception {
MessageClient messagesClient = new MessageClient();
List<MessageInbox> sentMessages = messagesClient.getSentMessages();
for (MessageInbox workspaceMessage : sentMessages) {
logger.debug("Sent message was " + workspaceMessage);
}
}
@Test
public void sendMessage() throws Exception{
logger.debug("Sending message ");
MessageClient messagesClient = new MessageClient();
List<Recipient> rec = Arrays.asList(new Recipient("andrea.rossi"));
Message message = new Message(
"Test message",
"Sending message via client " + System.currentTimeMillis(),
new ArrayList<Recipient>(rec));
String idMessage = messagesClient.writeMessage(message);
assert(idMessage != null);
}
//@Test
public void testNotifications() throws Exception {
NotificationClient notificationsClient = new NotificationClient();
List<Notification> latestNotifications = notificationsClient.getNotifications(1, 2);
for (Notification notification : latestNotifications) {
logger.debug("Notification is " + notification);
}
}
@Test
public void sendJobNotification() throws Exception {
NotificationClient notificationsClient = new NotificationClient();
JobNotificationBean notification = new JobNotificationBean(
"massimiliano.assante",
UUID.randomUUID().toString(),
"SmartExecutor Social Indexer",
"Workspace Application",
JobStatusType.SUCCEEDED,
"all ok");
notificationsClient.sendJobNotification(notification);
logger.debug("Sent job notification ");
}
@Test
public void getProfile() throws Exception {
PeopleClient getProfile = new PeopleClient();
UserProfile profile = getProfile.getProfile();
logger.debug("Profile retrieved is " + profile);
}
//@Test
public void generateAppToken() throws Exception {
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{
VREClient myVresClient = new VREClient();
List<MyVRE> 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<String> administrators = userClient.getAllUsernamesByGlobalRole("administrator");
//
// assert(administrators != null && !administrators.isEmpty());
// logger.debug("Administrators are " + administrators);
List<String> 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<String> usernamesInThisContext = userClient.getAllUsernamesContext();
assert(usernamesInThisContext != null && !usernamesInThisContext.isEmpty());
logger.debug("List of usernames in this context is " + usernamesInThisContext);
Map<String, String> 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<Feed> 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<Feed> allposts = postClient.getAllUserPosts();
logger.debug("All posts are " + allposts);
}
//@Test
public void getUserPostsQuantity() throws Exception{
PostClient postClient = new PostClient();
List<Feed> 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<Feed> 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<Feed> vrePosts = postClient.getPostsVRE();
logger.debug("VRE posts are " + vrePosts);
}
//@Test
public void getHashtagPosts() throws Exception{
PostClient postClient = new PostClient();
List<Feed> postsWithHashtag = postClient.getHashtagPosts("#connect");
logger.debug("Posts with hashtag #connect are " + postsWithHashtag);
}
//@Test
public void getUserLikedPost() throws Exception{
PostClient postClient = new PostClient();
List<Feed> postsLiked = postClient.getUserLikedPost();
logger.debug("Posts liked are " + postsLiked);
}
//@Test
public void getUserLikedPostIds() throws Exception{
PostClient postClient = new PostClient();
List<String> postsLikedIds = postClient.getUserLikedPostIds();
logger.debug("Posts liked's ids are " + postsLikedIds);
}
//@After
public void reset(){
ScopeProvider.instance.reset();
SecurityTokenProvider.instance.reset();
}
}