microsoft/teams.net
Publicmirrored fromhttps://github.com/microsoft/teams.netAvailable
core/test/Microsoft.Teams.Core.UnitTests/Schema/CoreActivityTests.cs
334lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. |
| 2 | // Licensed under the MIT License. |
| 3 | |
| 4 | using System.Text.Json; |
| 5 | using Microsoft.Teams.Core.Schema; |
| 6 | |
| 7 | namespace Microsoft.Teams.Core.UnitTests.Schema; |
| 8 | |
| 9 | public class CoreCoreActivityTests |
| 10 | { |
| 11 | [Fact] |
| 12 | public void Ctor_And_Nulls() |
| 13 | { |
| 14 | CoreActivity a1 = new(); |
| 15 | Assert.NotNull(a1); |
| 16 | Assert.Equal(ActivityType.Message, a1.Type); |
| 17 | |
| 18 | CoreActivity a2 = new() |
| 19 | { |
| 20 | Type = "mytype" |
| 21 | }; |
| 22 | Assert.NotNull(a2); |
| 23 | Assert.Equal("mytype", a2.Type); |
| 24 | } |
| 25 | |
| 26 | [Fact] |
| 27 | public void Json_Nulls_Not_Deserialized() |
| 28 | { |
| 29 | string json = """ |
| 30 | { |
| 31 | "type": "message", |
| 32 | "text": null |
| 33 | } |
| 34 | """; |
| 35 | CoreActivity act = CoreActivity.FromJsonString(json); |
| 36 | Assert.NotNull(act); |
| 37 | Assert.Equal("message", act.Type); |
| 38 | |
| 39 | string json2 = """ |
| 40 | { |
| 41 | "type": "message" |
| 42 | } |
| 43 | """; |
| 44 | CoreActivity act2 = CoreActivity.FromJsonString(json2); |
| 45 | Assert.NotNull(act2); |
| 46 | Assert.Equal("message", act2.Type); |
| 47 | |
| 48 | } |
| 49 | |
| 50 | [Fact] |
| 51 | public void Accept_Unkown_Primitive_Fields() |
| 52 | { |
| 53 | string json = """ |
| 54 | { |
| 55 | "type": "message", |
| 56 | "text": "hello", |
| 57 | "unknownString": "some string", |
| 58 | "unknownInt": 123, |
| 59 | "unknownBool": true, |
| 60 | "unknownNull": null |
| 61 | } |
| 62 | """; |
| 63 | CoreActivity act = CoreActivity.FromJsonString(json); |
| 64 | Assert.NotNull(act); |
| 65 | Assert.Equal("message", act.Type); |
| 66 | Assert.True(act.Properties.ContainsKey("unknownString")); |
| 67 | Assert.True(act.Properties.ContainsKey("unknownInt")); |
| 68 | Assert.True(act.Properties.ContainsKey("unknownBool")); |
| 69 | Assert.True(act.Properties.ContainsKey("unknownNull")); |
| 70 | Assert.Equal("some string", act.Properties["unknownString"]?.ToString()); |
| 71 | Assert.Equal(123, ((JsonElement)act.Properties["unknownInt"]!).GetInt32()); |
| 72 | Assert.True(((JsonElement)act.Properties["unknownBool"]!).GetBoolean()); |
| 73 | Assert.Null(act.Properties["unknownNull"]); |
| 74 | } |
| 75 | |
| 76 | [Fact] |
| 77 | public void Serialize_Unkown_Primitive_Fields() |
| 78 | { |
| 79 | CoreActivity act = new() |
| 80 | { |
| 81 | Type = ActivityType.Message, |
| 82 | }; |
| 83 | act.Properties["unknownString"] = "some string"; |
| 84 | act.Properties["unknownInt"] = 123; |
| 85 | act.Properties["unknownBool"] = true; |
| 86 | act.Properties["unknownNull"] = null; |
| 87 | act.Properties["unknownLong"] = 1L; |
| 88 | act.Properties["unknownDouble"] = 1.0; |
| 89 | |
| 90 | string json = act.ToJson(); |
| 91 | Assert.Contains("\"type\": \"message\"", json); |
| 92 | Assert.Contains("\"unknownString\": \"some string\"", json); |
| 93 | Assert.Contains("\"unknownInt\": 123", json); |
| 94 | Assert.Contains("\"unknownBool\": true", json); |
| 95 | Assert.Contains("\"unknownNull\": null", json); |
| 96 | Assert.Contains("\"unknownLong\": 1", json); |
| 97 | Assert.Contains("\"unknownDouble\": 1", json); |
| 98 | } |
| 99 | |
| 100 | [Fact] |
| 101 | public void Deserialize_Unkown__Fields_In_KnownObjects() |
| 102 | { |
| 103 | string json = """ |
| 104 | { |
| 105 | "type": "message", |
| 106 | "text": "hello", |
| 107 | "from": { |
| 108 | "id": "1", |
| 109 | "name": "tester", |
| 110 | "aadObjectId": "123" |
| 111 | } |
| 112 | } |
| 113 | """; |
| 114 | CoreActivity act = CoreActivity.FromJsonString(json); |
| 115 | Assert.NotNull(act); |
| 116 | Assert.Equal("message", act.Type); |
| 117 | Assert.NotNull(act.From); |
| 118 | Assert.Equal("1", act.From.Id); |
| 119 | Assert.Equal("tester", act.From.Name); |
| 120 | Assert.Equal("123", act.From.Properties["aadObjectId"]?.ToString()); |
| 121 | } |
| 122 | |
| 123 | [Fact] |
| 124 | public void Deserialize_Serialize_Unkown__Fields_In_KnownObjects() |
| 125 | { |
| 126 | string json = """ |
| 127 | { |
| 128 | "type": "message", |
| 129 | "text": "hello", |
| 130 | "from": { |
| 131 | "id": "1", |
| 132 | "name": "tester", |
| 133 | "aadObjectId": "123" |
| 134 | } |
| 135 | } |
| 136 | """; |
| 137 | CoreActivity act = CoreActivity.FromJsonString(json); |
| 138 | string json2 = act.ToJson(); |
| 139 | Assert.Contains("\"type\": \"message\"", json2); |
| 140 | Assert.Contains("\"text\": \"hello\"", json2); |
| 141 | Assert.Contains("\"from\":", json2); |
| 142 | Assert.Contains("\"id\": \"1\"", json2); |
| 143 | Assert.Contains("\"name\": \"tester\"", json2); |
| 144 | Assert.Contains("\"aadObjectId\": \"123\"", json2); |
| 145 | } |
| 146 | |
| 147 | [Fact] |
| 148 | public void Deserialize_Serialize_Entities() |
| 149 | { |
| 150 | string json = """ |
| 151 | { |
| 152 | "type": "message", |
| 153 | "text": "hello", |
| 154 | "entities": [ |
| 155 | { |
| 156 | "mentioned": { |
| 157 | "id": "28:0b6fe6d1-fece-44f7-9a48-56465e2d5ab8", |
| 158 | "name": "ridotest" |
| 159 | }, |
| 160 | "text": "\u003Cat\u003Eridotest\u003C/at\u003E", |
| 161 | "type": "mention" |
| 162 | }, |
| 163 | { |
| 164 | "locale": "en-US", |
| 165 | "country": "US", |
| 166 | "platform": "Web", |
| 167 | "timezone": "America/Los_Angeles", |
| 168 | "type": "clientInfo" |
| 169 | } |
| 170 | ] |
| 171 | } |
| 172 | """; |
| 173 | CoreActivity act = CoreActivity.FromJsonString(json); |
| 174 | string json2 = act.ToJson(); |
| 175 | Assert.Contains("\"type\": \"message\"", json2); |
| 176 | Assert.True(act.Properties.ContainsKey("entities")); |
| 177 | Assert.IsType<JsonElement>(act.Properties["entities"]); |
| 178 | var entitiesElement = (JsonElement)act.Properties["entities"]!; |
| 179 | Assert.Equal(JsonValueKind.Array, entitiesElement.ValueKind); |
| 180 | Assert.Equal(2, entitiesElement.GetArrayLength()); |
| 181 | |
| 182 | } |
| 183 | |
| 184 | |
| 185 | [Fact] |
| 186 | public void Handling_Nulls_from_default_serializer() |
| 187 | { |
| 188 | string json = """ |
| 189 | { |
| 190 | "type": "message", |
| 191 | "text": null, |
| 192 | "unknownString": null |
| 193 | } |
| 194 | """; |
| 195 | CoreActivity? act = JsonSerializer.Deserialize<CoreActivity>(json); //without default options |
| 196 | Assert.NotNull(act); |
| 197 | Assert.Equal("message", act.Type); |
| 198 | Assert.Null(act.Properties["text"]); |
| 199 | Assert.Null(act.Properties["unknownString"]!); |
| 200 | |
| 201 | string json2 = JsonSerializer.Serialize(act); //without default options |
| 202 | Assert.Contains("\"type\":\"message\"", json2); |
| 203 | Assert.Contains("\"text\":null", json2); |
| 204 | Assert.Contains("\"unknownString\":null", json2); |
| 205 | } |
| 206 | |
| 207 | [Fact] |
| 208 | public void Serialize_With_Properties_Initialized() |
| 209 | { |
| 210 | CoreActivity act = new() |
| 211 | { |
| 212 | Type = ActivityType.Message, |
| 213 | From = new ConversationAccount { Id = "user1", Properties = { { "fromCustomField", "fromCustomValue" } } }, |
| 214 | Recipient = new ConversationAccount { Id = "bot1", Properties = { { "recipientCustomField", "recipientCustomValue" } } }, |
| 215 | Properties = |
| 216 | { |
| 217 | { "customField", "customValue" }, |
| 218 | { "channelData", new ChannelData { Properties = { { "channelCustomField", "channelCustomValue" } } } }, |
| 219 | { "conversation", new Conversation { Properties = { { "conversationCustomField", "conversationCustomValue" } } } }, |
| 220 | } |
| 221 | }; |
| 222 | string json = act.ToJson(); |
| 223 | Assert.Contains("\"type\": \"message\"", json); |
| 224 | Assert.Contains("\"customField\": \"customValue\"", json); |
| 225 | Assert.Contains("\"channelCustomField\": \"channelCustomValue\"", json); |
| 226 | Assert.Contains("\"conversationCustomField\": \"conversationCustomValue\"", json); |
| 227 | Assert.Contains("\"fromCustomField\": \"fromCustomValue\"", json); |
| 228 | Assert.Contains("\"recipientCustomField\": \"recipientCustomValue\"", json); |
| 229 | } |
| 230 | |
| 231 | |
| 232 | [Fact] |
| 233 | public async Task DeserializeAsync() |
| 234 | { |
| 235 | string json = """ |
| 236 | { |
| 237 | "type": "message", |
| 238 | "text": "hello", |
| 239 | "from": { |
| 240 | "id": "1", |
| 241 | "name": "tester", |
| 242 | "aadObjectId": "123" |
| 243 | } |
| 244 | } |
| 245 | """; |
| 246 | using MemoryStream ms = new(System.Text.Encoding.UTF8.GetBytes(json)); |
| 247 | CoreActivity? act = await CoreActivity.FromJsonStreamAsync(ms); |
| 248 | Assert.NotNull(act); |
| 249 | Assert.Equal("message", act.Type); |
| 250 | Assert.Equal("hello", act.Properties["text"]?.ToString()); |
| 251 | Assert.NotNull(act.From); |
| 252 | Assert.Equal("1", act.From.Id); |
| 253 | Assert.Equal("tester", act.From.Name); |
| 254 | Assert.Equal("123", act.From.Properties["aadObjectId"]?.ToString()); |
| 255 | } |
| 256 | |
| 257 | |
| 258 | [Fact] |
| 259 | public async Task DeserializeInvokeWithValueAsync() |
| 260 | { |
| 261 | string json = """ |
| 262 | { |
| 263 | "type": "invoke", |
| 264 | "value": { |
| 265 | "key1": "value1", |
| 266 | "key2": 2 |
| 267 | } |
| 268 | } |
| 269 | """; |
| 270 | using MemoryStream ms = new(System.Text.Encoding.UTF8.GetBytes(json)); |
| 271 | CoreActivity? act = await CoreActivity.FromJsonStreamAsync(ms); |
| 272 | Assert.NotNull(act); |
| 273 | Assert.Equal("invoke", act.Type); |
| 274 | // Value is no longer on CoreActivity — it lands in Properties via [JsonExtensionData] |
| 275 | Assert.True(act.Properties.ContainsKey("value")); |
| 276 | var valueElement = Assert.IsType<JsonElement>(act.Properties["value"]); |
| 277 | Assert.Equal("value1", valueElement.GetProperty("key1").GetString()); |
| 278 | Assert.Equal(2, valueElement.GetProperty("key2").GetInt32()); |
| 279 | } |
| 280 | |
| 281 | [Fact] |
| 282 | public void IsTargeted_DefaultsToNull() |
| 283 | { |
| 284 | ConversationAccount account = new(); |
| 285 | |
| 286 | Assert.Null(account.IsTargeted); |
| 287 | } |
| 288 | |
| 289 | [Fact] |
| 290 | public void IsTargeted_CanBeSetToTrue() |
| 291 | { |
| 292 | ConversationAccount account = new() |
| 293 | { |
| 294 | IsTargeted = true |
| 295 | }; |
| 296 | |
| 297 | Assert.True(account.IsTargeted); |
| 298 | } |
| 299 | |
| 300 | [Fact] |
| 301 | public void IsTargeted_IsSerializedToJson() |
| 302 | { |
| 303 | CoreActivity activity = new() |
| 304 | { |
| 305 | Type = ActivityType.Message, |
| 306 | Recipient = new ConversationAccount { Id = "user-123", IsTargeted = true } |
| 307 | }; |
| 308 | |
| 309 | string json = activity.ToJson(); |
| 310 | |
| 311 | // IsTargeted is serialized in the recipient object |
| 312 | Assert.Contains("isTargeted", json, StringComparison.OrdinalIgnoreCase); |
| 313 | } |
| 314 | |
| 315 | [Fact] |
| 316 | public void IsTargeted_DeserializedFromJson() |
| 317 | { |
| 318 | string json = """ |
| 319 | { |
| 320 | "type": "message", |
| 321 | "recipient": { |
| 322 | "id": "user-123", |
| 323 | "isTargeted": true |
| 324 | } |
| 325 | } |
| 326 | """; |
| 327 | |
| 328 | CoreActivity activity = CoreActivity.FromJsonString(json); |
| 329 | |
| 330 | Assert.NotNull(activity.Recipient); |
| 331 | Assert.Equal("user-123", activity.Recipient.Id); |
| 332 | Assert.True(activity.Recipient.IsTargeted); |
| 333 | } |
| 334 | } |
| 335 | |