microsoft/teams.net
Publicmirrored fromhttps://github.com/microsoft/teams.netAvailable
core/test/Microsoft.Teams.Bot.Core.UnitTests/Schema/EntitiesTest.cs
122lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. |
| 2 | // Licensed under the MIT License. |
| 3 | |
| 4 | using System.Text.Json.Nodes; |
| 5 | using Microsoft.Teams.Bot.Core.Schema; |
| 6 | |
| 7 | namespace Microsoft.Teams.Bot.Core.UnitTests.Schema; |
| 8 | |
| 9 | public class EntitiesTest |
| 10 | { |
| 11 | [Fact] |
| 12 | public void Test_Entity_Deserialization() |
| 13 | { |
| 14 | string json = """ |
| 15 | { |
| 16 | "type": "message", |
| 17 | "entities": [ |
| 18 | { |
| 19 | "type": "mention", |
| 20 | "mentioned": { |
| 21 | "id": "user1", |
| 22 | "name": "User One" |
| 23 | }, |
| 24 | "text": "<at>User One</at>" |
| 25 | } |
| 26 | ] |
| 27 | } |
| 28 | """; |
| 29 | CoreActivity activity = CoreActivity.FromJsonString(json); |
| 30 | Assert.NotNull(activity); |
| 31 | Assert.NotNull(activity.Entities); |
| 32 | Assert.Single(activity.Entities); |
| 33 | JsonNode? e1 = activity.Entities[0]; |
| 34 | Assert.NotNull(e1); |
| 35 | Assert.Equal("mention", e1["type"]?.ToString()); |
| 36 | Assert.NotNull(e1["mentioned"]); |
| 37 | Assert.True(e1["mentioned"]?.AsObject().ContainsKey("id")); |
| 38 | Assert.NotNull(e1["mentioned"]?["id"]); |
| 39 | Assert.Equal("user1", e1["mentioned"]?["id"]?.ToString()); |
| 40 | Assert.Equal("User One", e1["mentioned"]?["name"]?.ToString()); |
| 41 | Assert.Equal("<at>User One</at>", e1["text"]?.ToString()); |
| 42 | } |
| 43 | |
| 44 | [Fact] |
| 45 | public void Entitiy_Serialization() |
| 46 | { |
| 47 | JsonNodeOptions nops = new() |
| 48 | { |
| 49 | PropertyNameCaseInsensitive = false |
| 50 | }; |
| 51 | |
| 52 | CoreActivity activity = new(ActivityType.Message); |
| 53 | JsonObject mentionEntity = new() |
| 54 | { |
| 55 | ["type"] = "mention", |
| 56 | ["mentioned"] = new JsonObject |
| 57 | { |
| 58 | ["id"] = "user1", |
| 59 | ["name"] = "UserOne" |
| 60 | }, |
| 61 | ["text"] = "<at>User One</at>" |
| 62 | }; |
| 63 | activity.Entities = new JsonArray(nops, mentionEntity); |
| 64 | string json = activity.ToJson(); |
| 65 | Assert.NotNull(json); |
| 66 | Assert.Contains("\"type\": \"mention\"", json); |
| 67 | Assert.Contains("\"id\": \"user1\"", json); |
| 68 | Assert.Contains("\"name\": \"UserOne\"", json); |
| 69 | Assert.Contains("\"text\": \"\\u003Cat\\u003EUser One\\u003C/at\\u003E\"", json); |
| 70 | } |
| 71 | |
| 72 | [Fact] |
| 73 | public void Entity_RoundTrip() |
| 74 | { |
| 75 | string json = """ |
| 76 | { |
| 77 | "type": "message", |
| 78 | "entities": [ |
| 79 | { |
| 80 | "type": "mention", |
| 81 | "mentioned": { |
| 82 | "id": "user1", |
| 83 | "name": "User One" |
| 84 | }, |
| 85 | "text": "<at>User One</at>" |
| 86 | } |
| 87 | ] |
| 88 | } |
| 89 | """; |
| 90 | CoreActivity activity = CoreActivity.FromJsonString(json); |
| 91 | string serialized = activity.ToJson(); |
| 92 | Assert.NotNull(serialized); |
| 93 | Assert.Contains("\"type\": \"mention\"", serialized); |
| 94 | Assert.Contains("\"id\": \"user1\"", serialized); |
| 95 | Assert.Contains("\"name\": \"User One\"", serialized); |
| 96 | Assert.Contains("\"text\": \"\\u003Cat\\u003EUser One\\u003C/at\\u003E\"", serialized); |
| 97 | } |
| 98 | |
| 99 | [Fact] |
| 100 | public void Test_Unknown_Entity() |
| 101 | { |
| 102 | string json = """ |
| 103 | { |
| 104 | "type": "message", |
| 105 | "entities": [ |
| 106 | { |
| 107 | "type": "unknownEntityType", |
| 108 | "someProperty": "someValue" |
| 109 | } |
| 110 | ] |
| 111 | } |
| 112 | """; |
| 113 | CoreActivity activity = CoreActivity.FromJsonString(json); |
| 114 | Assert.NotNull(activity); |
| 115 | Assert.NotNull(activity.Entities); |
| 116 | Assert.Single(activity.Entities); |
| 117 | JsonNode? e1 = activity.Entities[0]; |
| 118 | Assert.NotNull(e1); |
| 119 | Assert.Equal("unknownEntityType", e1["type"]?.ToString()); |
| 120 | Assert.Equal("someValue", e1["someProperty"]?.ToString()); |
| 121 | } |
| 122 | } |
| 123 | |