microsoft/teams.net

Public

mirrored fromhttps://github.com/microsoft/teams.netAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
copilot/sub-pr-338

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

core/test/Microsoft.Teams.Bot.Core.UnitTests/Schema/CoreActivityTests.cs

396lines · modecode

1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT License.
3
4using Microsoft.Teams.Bot.Core.Schema;
5
6namespace Microsoft.Teams.Bot.Core.UnitTests.Schema;
7
8public class CoreCoreActivityTests
9{
10 [Fact]
11 public void Ctor_And_Nulls()
12 {
13 CoreActivity a1 = new();
14 Assert.NotNull(a1);
15 Assert.Equal(ActivityType.Message, a1.Type);
16
17 CoreActivity a2 = new()
18 {
19 Type = "mytype"
20 };
21 Assert.NotNull(a2);
22 Assert.Equal("mytype", a2.Type);
23 }
24
25 [Fact]
26 public void Json_Nulls_Not_Deserialized()
27 {
28 string json = """
29 {
30 "type": "message",
31 "text": null
32 }
33 """;
34 CoreActivity act = CoreActivity.FromJsonString(json);
35 Assert.NotNull(act);
36 Assert.Equal("message", act.Type);
37
38 string json2 = """
39 {
40 "type": "message"
41 }
42 """;
43 CoreActivity act2 = CoreActivity.FromJsonString(json2);
44 Assert.NotNull(act2);
45 Assert.Equal("message", act2.Type);
46
47 }
48
49 [Fact]
50 public void Accept_Unkown_Primitive_Fields()
51 {
52 string json = """
53 {
54 "type": "message",
55 "text": "hello",
56 "unknownString": "some string",
57 "unknownInt": 123,
58 "unknownBool": true,
59 "unknownNull": null
60 }
61 """;
62 CoreActivity act = CoreActivity.FromJsonString(json);
63 Assert.NotNull(act);
64 Assert.Equal("message", act.Type);
65 Assert.True(act.Properties.ContainsKey("unknownString"));
66 Assert.True(act.Properties.ContainsKey("unknownInt"));
67 Assert.True(act.Properties.ContainsKey("unknownBool"));
68 Assert.True(act.Properties.ContainsKey("unknownNull"));
69 Assert.Equal("some string", act.Properties["unknownString"]?.ToString());
70 Assert.Equal(123, ((JsonElement)act.Properties["unknownInt"]!).GetInt32());
71 Assert.True(((JsonElement)act.Properties["unknownBool"]!).GetBoolean());
72 Assert.Null(act.Properties["unknownNull"]);
73 }
74
75 [Fact]
76 public void Serialize_Unkown_Primitive_Fields()
77 {
78 CoreActivity act = new()
79 {
80 Type = ActivityType.Message,
81 };
82 act.Properties["unknownString"] = "some string";
83 act.Properties["unknownInt"] = 123;
84 act.Properties["unknownBool"] = true;
85 act.Properties["unknownNull"] = null;
86 act.Properties["unknownLong"] = 1L;
87 act.Properties["unknownDouble"] = 1.0;
88
89 string json = act.ToJson();
90 Assert.Contains("\"type\": \"message\"", json);
91 Assert.Contains("\"unknownString\": \"some string\"", json);
92 Assert.Contains("\"unknownInt\": 123", json);
93 Assert.Contains("\"unknownBool\": true", json);
94 Assert.Contains("\"unknownNull\": null", json);
95 Assert.Contains("\"unknownLong\": 1", json);
96 Assert.Contains("\"unknownDouble\": 1", json);
97 }
98
99 [Fact]
100 public void Deserialize_Unkown__Fields_In_KnownObjects()
101 {
102 string json = """
103 {
104 "type": "message",
105 "text": "hello",
106 "from": {
107 "id": "1",
108 "name": "tester",
109 "aadObjectId": "123"
110 }
111 }
112 """;
113 CoreActivity act = CoreActivity.FromJsonString(json);
114 Assert.NotNull(act);
115 Assert.Equal("message", act.Type);
116 Assert.NotNull(act.From);
117 Assert.IsType<ConversationAccount>(act.From);
118 Assert.Equal("1", act.From!.Id);
119 Assert.Equal("tester", act.From.Name);
120 Assert.True(act.From.Properties.ContainsKey("aadObjectId"));
121 Assert.Equal("123", act.From.Properties["aadObjectId"]?.ToString());
122 }
123
124 [Fact]
125 public void Deserialize_Serialize_Unkown__Fields_In_KnownObjects()
126 {
127 string json = """
128 {
129 "type": "message",
130 "text": "hello",
131 "from": {
132 "id": "1",
133 "name": "tester",
134 "aadObjectId": "123"
135 }
136 }
137 """;
138 CoreActivity act = CoreActivity.FromJsonString(json);
139 string json2 = act.ToJson();
140 Assert.Contains("\"type\": \"message\"", json2);
141 Assert.Contains("\"text\": \"hello\"", json2);
142 Assert.Contains("\"from\": {", json2);
143 Assert.Contains("\"id\": \"1\"", json2);
144 Assert.Contains("\"name\": \"tester\"", json2);
145 Assert.Contains("\"aadObjectId\": \"123\"", json2);
146 }
147
148 [Fact]
149 public void Deserialize_Serialize_Entities()
150 {
151 string json = """
152 {
153 "type": "message",
154 "text": "hello",
155 "entities": [
156 {
157 "mentioned": {
158 "id": "28:0b6fe6d1-fece-44f7-9a48-56465e2d5ab8",
159 "name": "ridotest"
160 },
161 "text": "\u003Cat\u003Eridotest\u003C/at\u003E",
162 "type": "mention"
163 },
164 {
165 "locale": "en-US",
166 "country": "US",
167 "platform": "Web",
168 "timezone": "America/Los_Angeles",
169 "type": "clientInfo"
170 }
171 ]
172 }
173 """;
174 CoreActivity act = CoreActivity.FromJsonString(json);
175 string json2 = act.ToJson();
176 Assert.Contains("\"type\": \"message\"", json2);
177 Assert.NotNull(act.Entities);
178 Assert.Equal(2, act.Entities!.Count);
179
180 }
181
182
183 [Fact]
184 public void Handling_Nulls_from_default_serializer()
185 {
186 string json = """
187 {
188 "type": "message",
189 "text": null,
190 "unknownString": null
191 }
192 """;
193 CoreActivity? act = JsonSerializer.Deserialize<CoreActivity>(json); //without default options
194 Assert.NotNull(act);
195 Assert.Equal("message", act.Type);
196 Assert.Null(act.Properties["text"]);
197 Assert.Null(act.Properties["unknownString"]!);
198
199 string json2 = JsonSerializer.Serialize(act); //without default options
200 Assert.Contains("\"type\":\"message\"", json2);
201 Assert.Contains("\"text\":null", json2);
202 Assert.Contains("\"unknownString\":null", json2);
203 }
204
205 [Fact]
206 public void Serialize_With_Properties_Initialized()
207 {
208 CoreActivity act = new()
209 {
210 Type = ActivityType.Message,
211 Properties =
212 {
213 { "customField", "customValue" }
214 },
215 ChannelData = new()
216 {
217 Properties =
218 {
219 { "channelCustomField", "channelCustomValue" }
220 }
221 },
222 Conversation = new()
223 {
224 Properties =
225 {
226 { "conversationCustomField", "conversationCustomValue" }
227 }
228 },
229 From = new()
230 {
231 Id = "user1",
232 Properties =
233 {
234 { "fromCustomField", "fromCustomValue" }
235 }
236 },
237 Recipient = new()
238 {
239 Id = "bot1",
240 Properties =
241 {
242 { "recipientCustomField", "recipientCustomValue" }
243 }
244
245 }
246 };
247 string json = act.ToJson();
248 Assert.Contains("\"type\": \"message\"", json);
249 Assert.Contains("\"customField\": \"customValue\"", json);
250 Assert.Contains("\"channelCustomField\": \"channelCustomValue\"", json);
251 Assert.Contains("\"conversationCustomField\": \"conversationCustomValue\"", json);
252 Assert.Contains("\"fromCustomField\": \"fromCustomValue\"", json);
253 Assert.Contains("\"recipientCustomField\": \"recipientCustomValue\"", json);
254 }
255
256
257 [Fact]
258 public void CreateReply()
259 {
260 CoreActivity act = new()
261 {
262 Type = "myActivityType",
263 Id = "CoreActivity1",
264 ChannelId = "channel1",
265 ServiceUrl = new Uri("http://service.url"),
266 From = new ConversationAccount()
267 {
268 Id = "user1",
269 Name = "User One"
270 },
271 Recipient = new ConversationAccount()
272 {
273 Id = "bot1",
274 Name = "Bot One"
275 },
276 Conversation = new Conversation()
277 {
278 Id = "conversation1"
279 }
280 };
281 CoreActivity reply = CoreActivity.CreateBuilder()
282 .WithType(ActivityType.Message)
283 .WithConversationReference(act)
284 .WithProperty("text", "reply")
285 .Build();
286
287 Assert.NotNull(reply);
288 Assert.Equal(ActivityType.Message, reply.Type);
289 Assert.Equal("reply", reply.Properties["text"]);
290 Assert.Equal("channel1", reply.ChannelId);
291 Assert.NotNull(reply.ServiceUrl);
292 Assert.Equal("http://service.url/", reply.ServiceUrl.ToString());
293 Assert.Equal("conversation1", reply.Conversation.Id);
294 Assert.Equal("bot1", reply.From.Id);
295 Assert.Equal("Bot One", reply.From.Name);
296 }
297
298 [Fact]
299 public async Task DeserializeAsync()
300 {
301 string json = """
302 {
303 "type": "message",
304 "text": "hello",
305 "from": {
306 "id": "1",
307 "name": "tester",
308 "aadObjectId": "123"
309 }
310 }
311 """;
312 using MemoryStream ms = new(System.Text.Encoding.UTF8.GetBytes(json));
313 CoreActivity? act = await CoreActivity.FromJsonStreamAsync(ms);
314 Assert.NotNull(act);
315 Assert.Equal("message", act.Type);
316 Assert.Equal("hello", act.Properties["text"]?.ToString());
317 Assert.NotNull(act.From);
318 Assert.IsType<ConversationAccount>(act.From);
319 Assert.Equal("1", act.From.Id);
320 Assert.Equal("tester", act.From.Name);
321 Assert.True(act.From.Properties.ContainsKey("aadObjectId"));
322 Assert.Equal("123", act.From.Properties["aadObjectId"]?.ToString());
323 }
324
325
326 [Fact]
327 public async Task DeserializeInvokeWithValueAsync()
328 {
329 string json = """
330 {
331 "type": "invoke",
332 "value": {
333 "key1": "value1",
334 "key2": 2
335 }
336 }
337 """;
338 using MemoryStream ms = new(System.Text.Encoding.UTF8.GetBytes(json));
339 CoreActivity? act = await CoreActivity.FromJsonStreamAsync(ms);
340 Assert.NotNull(act);
341 Assert.Equal("invoke", act.Type);
342 Assert.NotNull(act.Value);
343 Assert.NotNull(act.Value["key1"]);
344 Assert.Equal("value1", act.Value["key1"]?.GetValue<string>());
345 Assert.Equal(2, act.Value["key2"]?.GetValue<int>());
346 }
347
348 [Fact]
349 public void IsTargeted_DefaultsToFalse()
350 {
351 CoreActivity activity = new();
352
353 Assert.False(activity.IsTargeted);
354 }
355
356 [Fact]
357 public void IsTargeted_CanBeSetToTrue()
358 {
359 CoreActivity activity = new()
360 {
361 IsTargeted = true
362 };
363
364 Assert.True(activity.IsTargeted);
365 }
366
367 [Fact]
368 public void IsTargeted_IsNotSerializedToJson()
369 {
370 CoreActivity activity = new()
371 {
372 Type = ActivityType.Message,
373 IsTargeted = true
374 };
375
376 string json = activity.ToJson();
377
378 Assert.DoesNotContain("isTargeted", json, StringComparison.OrdinalIgnoreCase);
379 Assert.True(activity.IsTargeted); // Property still holds value
380 }
381
382 [Fact]
383 public void IsTargeted_IsNotDeserializedFromJson()
384 {
385 string json = """
386 {
387 "type": "message",
388 "isTargeted": true
389 }
390 """;
391
392 CoreActivity activity = CoreActivity.FromJsonString(json);
393
394 Assert.False(activity.IsTargeted); // Should default to false since JsonIgnore
395 }
396}
397