social-util-library/src/test/java/org/gcube/social_networking/socialutillibrary/MessageParserTest.java

216 lines
10 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package org.gcube.social_networking.socialutillibrary;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.List;
import org.gcube.portlets.widgets.pickitem.shared.ItemBean;
import org.gcube.socialnetworking.socialtoken.SanitizedHashTag;
import org.gcube.socialnetworking.socialtoken.SanitizedURL;
import org.gcube.socialnetworking.socialtoken.SocialMessageParser;
import org.gcube.socialnetworking.socialtoken.URLToken;
import org.junit.Assert;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class MessageParserTest {
private static final Logger logger = LoggerFactory.getLogger(MessageParserTest.class);
public static final String TEST_11 = "Dear all, this is a test to ignore, to select a week for the upcoming 194th #Tcom event, "
+ "hosted by #Apple in #Cupertino, please use this #Doodle: http://Doodle.com/poll/not-existing-poll \n\n"
+ "We're closing the poll next Thursday 16th March.";
public static final String TEST_12 = "Just because I am so happy to have the SPARQL-endpoint available, \n"
+ "sharing some sample SPARQL queries: \n\n" + "* Classes & usage counts: \n"
+ "https://virtuoso.parthenos.d4science.org/sparql?default-graph-uri=&query=%09SELECT+%3Fp+%28COUNT%28%3Fp%29+as+%3FpCount%29++%0D%0A%09%09%09%09%09WHERE+%7B%5B%5D+%3Fp+%5B%5D%7D%0D%0A%09%09%09%09%09GROUP+BY+%3Fp&format=text%2Fhtml&timeout=0&debug=on\n"
+ "\n" + "* properties and usage counts: \n"
+ "https://virtuoso.parthenos.d4science.org/sparql?default-graph-uri=&query=SELECT+%3Ftype+%28COUNT%28%3Ftype%29+as+%3FtypeCount%29++%0D%0A%09%09%09%09%09WHERE+%7B%5B%5D+a+%3Ftype%7D%0D%0A%09%09%09%09%09GROUP+BY+%3Ftype&format=text%2Fhtml&timeout=0&debug=on\n";
public static final String TEST_13 = "Dear members,\n"
+ "The item 'Webinar on Ontology Management using VOCBENCH in the context of AGINFRAPLUS Project' has been just published by Leonardo Candela .\n"
+ "You can find it here: http://data.d4science.org/ctlg/AGINFRAplus/webinar_on_ontology_management_using_vocbench_in_the_context_of_aginfraplus_project \n"
+ "#AGINFRAPLUS #VOCBENCH";
public static final String TEST_LUCA_1 = "Dear members,\n"
+ "The item 'just a test with time fields' has been just published by Francesco Mangiacrapa.\n"
+ "You can find it here: http://data-d.d4science.org/ctlg/NextNext/just_a_test_with_time_fields\n"
+ "#Text_mining #Field_1 #B3";
public static final String TEST_LUCA_2 = "Francesco Mangiacrapa prova <a href=\"#\">Francesco Mangiacrapa</a>";
public static final String TEST_LUCA_3 = "test &nbsp";
public static final String TEST_LUCA_4 = "test &<nbsp <";
public static final String TEST_LUCA_5 = "Accedete a questo link che vi porta ad un post su linkedin. \n" +
"https://www.linkedin.com/feed/update/urn:li:activity:6488779074213801984/\n" +
"I numeri riportati sono veri ed è motivo di orgoglio per tutti noi aver contribuito alla realizzazione della d4s infra che ha questo utilizzo via i diversi gateway. ";
public static final String TEST_LUCA_6 = "(www.google.it)";
public static final String TEST_LUCA_7 = "Hello (https://doodle.com/poll/not-existing-poll)";
public static final String TEST_LUCA_8 = "Hello this link \"https://virtuoso.parthenos.d4science.org/sparql?default-graph-uri=&query=SELECT+%3Ftype+%28COUNT%28%3Ftype%29+as+%3FtypeCount%29++%0D%0A%09%09%09%09%09WHERE+%7B%5B%5D+a+%3Ftype%7D%0D%0A%09%09%09%09%09GROUP+BY+%3Ftype&format=text%2Fhtml&timeout=0&debug=on\" is a SPARQL query ";
public static final String TEST_BUG_18356 = "Dear B-C colleagues,\n"
+ "\n"
+ "Just a kind reminder that we look forward to welcoming all of you during the Projects upcoming Blue-Cloud “Service Exploitation & Sustainability Plan (SE&SP) and Roadmap to 2030” Workshop next Thursday, January 21st (9.30am to 1.30pm). Everyone's welcome for an interactive and productive discussion.\n"
+ "\n"
+ "Please find here a link to the workshop Agenda (https://data.d4science.net/tfXA), including connection details.\n"
+ "\n"
+ "As discussed, the workshop will provide an opportunity to share the high-level results of the initial phase of stakeholder consultations towards the B-C Roadmap and to work within the Partnership to discuss and align some of the underlying, strategic concepts and ideas that will set the direction for the B-C SE&SP and B-C Roadmap to 2030.\n"
+ "\n"
+ "To inform the discussions to be held during the workshop, we have produced a report bringing together all the feedback, messages and recommendations gathered during the initial phase of stakeholder consultations (available here - https://data.d4science.net/JEm7). It is a long document, but we have included a one-page Executive Summary with high-level, key messages & recommendations, in case useful.\n"
+ "\n"
+ "We look forward to a productive discussion with you next Thursday!\n"
+ "\n"
+ "Kind regards,\n"
+ "Julia";
@Test
public void test13() {
SocialMessageParser messageParser = new SocialMessageParser(TEST_13);
logger.debug(messageParser.getParsedMessage());
}
@Test
public void test() {
String message = "Prova #Pippo Pollo http://google) <a href=\"/test\">Luca</a> https://www.linkedin.com/feed/update/urn:li:activity:6488779074213801984 :) ";
SocialMessageParser messageParser = new SocialMessageParser(message);
logger.debug(messageParser.getParsedMessage());
messageParser = new SocialMessageParser(TEST_11);
logger.debug(messageParser.getParsedMessage());
messageParser = new SocialMessageParser(TEST_12);
logger.debug(messageParser.getParsedMessage());
messageParser = new SocialMessageParser(TEST_13);
logger.debug(messageParser.getParsedMessage());
List<ItemBean> mentionedUsers = new ArrayList<>();
ItemBean itemBean = new ItemBean("21150", "francesco.mangiacrapa", "Francesco Mangiacrapa", "");
itemBean.setItemGroup(false);
mentionedUsers.add(itemBean);
messageParser = new SocialMessageParser(TEST_LUCA_1);
logger.debug(messageParser.getParsedMessage(mentionedUsers, "/group/nextnext"));
messageParser = new SocialMessageParser(TEST_LUCA_2);
logger.debug(messageParser.getParsedMessage(mentionedUsers, "/group/nextnext"));
}
@Test
public void anotherTest() {
SocialMessageParser messageParser = new SocialMessageParser(TEST_LUCA_4);
logger.debug(messageParser.getParsedMessage());
messageParser = new SocialMessageParser(TEST_LUCA_5);
logger.debug(messageParser.getParsedMessage());
messageParser = new SocialMessageParser(TEST_LUCA_8);
logger.debug(messageParser.getParsedMessage());
}
@Test
public void urlWithParentesisTest() {
SocialMessageParser messageParser = new SocialMessageParser(TEST_LUCA_6);
logger.debug(messageParser.getParsedMessage());
messageParser = new SocialMessageParser(TEST_LUCA_7);
logger.debug("{}", messageParser.getParsedMessage());
messageParser = new SocialMessageParser(TEST_BUG_18356);
logger.debug("{}", messageParser.getParsedMessage());
}
@Test(expected=MalformedURLException.class)
public void auxTest() throws MalformedURLException {
new SanitizedURL(null);
}
protected String findFirstLink(String message) {
try {
SocialMessageParser messageParser = new SocialMessageParser(message);
List<URLToken> urlTokens = messageParser.getURLTokens();
return urlTokens.get(0).getExtractedURL().toString();
}catch (Exception e) {
return null;
}
}
@Test
public void testTest() {
String text = "Hello (https://doodle.com/poll/not-existing-poll)";
logger.debug(findFirstLink(text));
text = "post \"a text with #hashtag);\"";
SocialMessageParser messageParser = new SocialMessageParser(text);
logger.debug(messageParser.getParsedMessage());
text = "\"#hashtag and #hashtag repeated.";
messageParser = new SocialMessageParser(text);
logger.debug(messageParser.getParsedMessage());
}
@Test
public void checkHashTag7() throws Exception {
String text = "Checking hashtag between quotes #hashtag1; #hashtag1, #hashtag3. \"#hashtag5\" is recognized (#anotherHashtag) #4. #4.12 # ";
SocialMessageParser messageParser = new SocialMessageParser(text);
logger.debug(messageParser.getParsedMessage());
List<String> htags = messageParser.getHashtags();
for (String hTag : htags) {
logger.debug("found hashtag:"+hTag);
}
logger.debug("messageParser.getHashtags().size() should be 7, is: "+ htags.size());
Assert.assertTrue(htags.size() == 7);
}
@Test
public void checkHashTag() throws Exception {
String token = "\"#hashtag\"";
SanitizedHashTag sanitizedHashTag = new SanitizedHashTag(token);
logger.trace("{}", sanitizedHashTag);
Assert.assertTrue(sanitizedHashTag.getPrefix().compareTo("\"")==0);
Assert.assertTrue(sanitizedHashTag.getHashTag().compareTo("#hashtag")==0);
Assert.assertTrue(sanitizedHashTag.getPostfix().compareTo("\"")==0);
}
@Test(expected=IllegalArgumentException.class)
public void hasTagwithURL() throws Exception {
String token = "https://wiki.gcube-system.org/gcube/GCat_Background#GeoSpatial_search_for_datasets:_via_API_or_Search_Widget";
new SanitizedHashTag(token);
}
@Test
public void checkHashTag2() throws Exception {
String token = "\"#hashtag\");";
SanitizedHashTag sanitizedHashTag = new SanitizedHashTag(token);
logger.trace("{}", sanitizedHashTag);
Assert.assertTrue(sanitizedHashTag.getPrefix().compareTo("\"")==0);
Assert.assertTrue(sanitizedHashTag.getHashTag().compareTo("#hashtag")==0);
Assert.assertTrue(sanitizedHashTag.getPostfix().compareTo("\");")==0);
}
@Test
public void checkHashTag3() throws Exception {
String token = ";(\"#hashtag\");";
SanitizedHashTag sanitizedHashTag = new SanitizedHashTag(token);
logger.trace("{}", sanitizedHashTag);
Assert.assertTrue(sanitizedHashTag.getPrefix().compareTo(";(\"")==0);
Assert.assertTrue(sanitizedHashTag.getHashTag().compareTo("#hashtag")==0);
Assert.assertTrue(sanitizedHashTag.getPostfix().compareTo("\");")==0);
}
@Test(expected=IllegalArgumentException.class)
public void checkHashTag4() throws Exception {
String token = ";(\"#hashtag\");]";
new SanitizedHashTag(token);
}
}