Added MentionTests and related fixes.

This commit is contained in:
Tracy Boehrer 2019-09-25 08:44:17 -05:00
Родитель 77a7d8fcd7
Коммит bf271d65c9
4 изменённых файлов: 147 добавлений и 4 удалений

Просмотреть файл

@ -3,6 +3,7 @@
package com.microsoft.bot.builder;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.microsoft.bot.connector.Channels;
@ -35,11 +36,10 @@ public class SkypeMentionNormalizeMiddleware implements Middleware {
if (closingBracket != -1) {
int openingBracket = text.indexOf("<", closingBracket);
if (openingBracket != -1) {
String mention = text.substring(closingBracket + 1, openingBracket);
String mention = text.substring(closingBracket + 1, openingBracket).trim();
// create new JsonNode with new mention value
ObjectNode node = JsonNodeFactory.instance.objectNode();
node.put("text", mention);
JsonNode node = JsonNodeFactory.instance.textNode(mention);
entity.setProperties("text", node);
}
}

Просмотреть файл

@ -0,0 +1,131 @@
package com.microsoft.bot.builder;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.microsoft.bot.schema.Activity;
import com.microsoft.bot.schema.Entity;
import org.junit.Assert;
import org.junit.Test;
import java.io.IOException;
public class MentionTests {
static ObjectMapper mapper = new ObjectMapper();
static {
mapper.findAndRegisterModules();
}
@Test
public void Mention_Skype() throws IOException
{
// A Skype mention contains the user mention enclosed in <at> tags. But the activity.getText() (as below)
// does not.
String mentionJson = "{\"mentioned\": {\"id\": \"recipientid\"},\"text\": \"<at id='28: 841caffa-9e92-425d-8d84-b503b3ded285'>botname</at>\"}";
Entity mention = mapper.readValue(mentionJson, Entity.class);
mention.setType("mention");
Activity activity = MessageFactory.text("botname sometext");
activity.setChannelId("skype");
activity.getEntities().add(mention);
// Normalize the Skype mention so that it is in a format RemoveMentionText can handle.
// If SkypeMentionNormalizeMiddleware is added to the adapters Middleware set, this
// will be called on every Skype message.
SkypeMentionNormalizeMiddleware.normalizeSkypMentionText(activity);
// This will remove the Mention.Text from the activity.getText(). This should just leave before/after the
// mention.
activity.removeMentionText("recipientid");
Assert.assertEquals(activity.getText(), "sometext");
}
@Test
public void Mention_Teams() throws IOException
{
String mentionJson = "{\"mentioned\": {\"id\": \"recipientid\"},\"text\": \"<at>botname</at>\"}";
Entity mention = mapper.readValue(mentionJson, Entity.class);
mention.setType("mention");
Activity activity = MessageFactory.text("<at>botname</at> sometext");
activity.getEntities().add(mention);
activity.removeMentionText("recipientid");
Assert.assertEquals(activity.getText(), "sometext");
}
@Test
public void Mention_slack() throws IOException
{
String mentionJson = "{\"mentioned\": {\"id\": \"recipientid\"},\"text\": \"@botname\"}";
Entity mention = mapper.readValue(mentionJson, Entity.class);
mention.setType("mention");
Activity activity = MessageFactory.text("@botname sometext");
activity.getEntities().add(mention);
activity.removeMentionText("recipientid");
Assert.assertEquals(activity.getText(), "sometext");
}
@Test
public void Mention_GroupMe() throws IOException
{
String mentionJson = "{\"mentioned\": {\"id\": \"recipientid\"},\"text\": \"@bot name\"}";
Entity mention = mapper.readValue(mentionJson, Entity.class);
mention.setType("mention");
Activity activity = MessageFactory.text("@bot name sometext");
activity.getEntities().add(mention);
activity.removeMentionText("recipientid");
Assert.assertEquals(activity.getText(), "sometext");
}
@Test
public void Mention_Telegram() throws IOException
{
String mentionJson = "{\"mentioned\": {\"id\": \"recipientid\"},\"text\": \"botname\"}";
Entity mention = mapper.readValue(mentionJson, Entity.class);
mention.setType("mention");
Activity activity = MessageFactory.text("botname sometext");
activity.getEntities().add(mention);
activity.removeMentionText("recipientid");
Assert.assertEquals(activity.getText(), "sometext");
}
@Test
public void Mention_Facebook()
{
// no-op for now: Facebook mentions unknown at this time
}
@Test
public void Mention_Email()
{
// no-op for now: EMail mentions not included in activity.getText()?
}
@Test
public void Mention_Cortana()
{
// no-op for now: Cortana mentions unknown at this time
}
@Test
public void Mention_Kik()
{
// no-op for now: bot mentions in Kik don't get Entity info and not included in activity.getText()
}
@Test
public void Mention_Twilio()
{
// no-op for now: Twilio mentions unknown at this time. Could not determine if they are supported.
}
}

Просмотреть файл

@ -1486,10 +1486,19 @@ public class Activity {
}
String text = activity.getText();
if (StringUtils.isEmpty(text)) {
return text;
}
for (Mention mention : activity.getMentions()) {
if (StringUtils.equals(mention.getMentioned().getId(), id)) {
text = text.replaceAll(mention.getText(), "");
if (StringUtils.isEmpty(mention.getText())) {
text = text.replaceAll("<at>" + mention.getMentioned().getName() + "</at>", "");
} else {
text = text.replaceAll(mention.getText(), "");
}
text = text.trim();
}
}

Просмотреть файл

@ -8,6 +8,7 @@ package com.microsoft.bot.schema;
import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonProcessingException;
@ -105,6 +106,7 @@ public class Entity {
* @param classType Class extended EntitySerialization
* @return Entity converted to type T
*/
@JsonIgnore
public <T extends EntitySerialization> T getAs(Class<T> classType) {
// Serialize
@ -139,6 +141,7 @@ public class Entity {
* @param obj of type T
* @param obj
*/
@JsonIgnore
public <T extends EntitySerialization> Entity setAs(T obj) {
// Serialize
String tempJson;