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

332 lines
12 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.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");
AccessTokenProvider.instance.set("eyJhbGciOiJSUzI1NiIsInR5cCIgOiAiSldUIiwia2lkIiA6ICJSSklZNEpoNF9qdDdvNmREY0NlUDFfS1l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0aS5jbnIuaXQifQ.aGkLfIk1wS5KQFdds1o4Lshh8P1Ni1ox7tu_bM_dzrYEfB7A9dnL3ye-pU3Rh7a-Z4P_mw0vT1D0tmKRHoKE5Pc4D-QsL_smTztXPDpG-bQBWcFvl7oMtYHA2Vg0Tcz_VIgxMz89PEsoRoPjyOvjGwXLmuq7Z13_YMov-R7BtGSigoXUhlbwNXaSjHrXarndn8X8zz2XUIPVvmHQerceSL0rU8oMdbvEv9wuPuPw1veaYJYCd3LaI29e0emRSqQVAb0Qs_J0u12DW7BUjgpjUGVjq6BzqUZgD7gkzqdjQ-IqbYKfOgjXdj_t2ekXL5yULgpH2M6ATW7x1NsE7J68ug");
}
//@Test
public void testSearch() throws Exception {
FullTextSearchClient search = new FullTextSearchClient();
ArrayList<EnhancedFeed> result = search.search("looking for test", 0, 10);
for (EnhancedFeed enhancedFeed : result) {
logger.debug("Returned feed " + enhancedFeed);
}
}
//@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<Message> receivedMessages = messagesClient.getReceivedMessages();
for (Message workspaceMessage : receivedMessages) {
logger.debug("Received message " + workspaceMessage.toString());
}
}
//@Test
public void testMessagesSent() throws Exception {
MessageClient messagesClient = new MessageClient();
List<Message> sentMessages = messagesClient.getSentMessages();
for (Message 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"), new Recipient("luca.frosini"));
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(
"costantino.perciante",
UUID.randomUUID().toString(),
"SmartExecutor Social Indexer",
"SmartExecutor",
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();
}
}