microsoft/teams.net

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
samples/migration-bot

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

404lines · 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 //Assert.Equal("user1", reply.Recipient?.Id);
297 //Assert.Equal("User One", reply.Recipient?.Name);
298 // TODO: review if recipient is required
299 }
300
301 [Fact]
302 public async Task DeserializeAsync()
303 {
304 string json = """
305 {
306 "type": "message",
307 "text": "hello",
308 "from": {
309 "id": "1",
310 "name": "tester",
311 "aadObjectId": "123"
312 }
313 }
314 """;
315 using MemoryStream ms = new(System.Text.Encoding.UTF8.GetBytes(json));
316 CoreActivity? act = await CoreActivity.FromJsonStreamAsync(ms);
317 Assert.NotNull(act);
318 Assert.Equal("message", act.Type);
319 Assert.Equal("hello", act.Properties["text"]?.ToString());
320 Assert.NotNull(act.From);
321 Assert.IsType<ConversationAccount>(act.From);
322 Assert.Equal("1", act.From.Id);
323 Assert.Equal("tester", act.From.Name);
324 Assert.True(act.From.Properties.ContainsKey("aadObjectId"));
325 Assert.Equal("123", act.From.Properties["aadObjectId"]?.ToString());
326 }
327
328
329 [Fact]
330 public async Task DeserializeInvokeWithValueAsync()
331 {
332 string json = """
333 {
334 "type": "invoke",
335 "value": {
336 "key1": "value1",
337 "key2": 2
338 }
339 }
340 """;
341 using MemoryStream ms = new(System.Text.Encoding.UTF8.GetBytes(json));
342 CoreActivity? act = await CoreActivity.FromJsonStreamAsync(ms);
343 Assert.NotNull(act);
344 Assert.Equal("invoke", act.Type);
345 Assert.NotNull(act.Value);
346 Assert.NotNull(act.Value["key1"]);
347 Assert.Equal("value1", act.Value["key1"]?.GetValue<string>());
348 Assert.Equal(2, act.Value["key2"]?.GetValue<int>());
349 }
350
351 [Fact]
352 public void IsTargeted_DefaultsToNull()
353 {
354 ConversationAccount account = new();
355
356 Assert.Null(account.IsTargeted);
357 }
358
359 [Fact]
360 public void IsTargeted_CanBeSetToTrue()
361 {
362 ConversationAccount account = new()
363 {
364 IsTargeted = true
365 };
366
367 Assert.True(account.IsTargeted);
368 }
369
370 [Fact]
371 public void IsTargeted_IsSerializedToJson()
372 {
373 CoreActivity activity = new()
374 {
375 Type = ActivityType.Message,
376 Recipient = new ConversationAccount { Id = "user-123", IsTargeted = true }
377 };
378
379 string json = activity.ToJson();
380
381 // IsTargeted is serialized in the recipient object
382 Assert.Contains("isTargeted", json, StringComparison.OrdinalIgnoreCase);
383 Assert.True(activity.Recipient.IsTargeted);
384 }
385
386 [Fact]
387 public void IsTargeted_DeserializedFromJson()
388 {
389 string json = """
390 {
391 "type": "message",
392 "recipient": {
393 "id": "user-123",
394 "isTargeted": true
395 }
396 }
397 """;
398
399 CoreActivity activity = CoreActivity.FromJsonString(json);
400
401 Assert.NotNull(activity.Recipient);
402 Assert.True(activity.Recipient.IsTargeted);
403 }
404}
405