microsoft/teams.net

Public

mirrored from https://github.com/microsoft/teams.netAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
copilot/move-activity-implementations

Branches

Tags

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

Clone

HTTPS

Download ZIP

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

298lines · modecode

1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT License.
3
4using Microsoft.Bot.Core.Schema;
5
6namespace Microsoft.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(ActivityTypes.Message, a1.Type);
16 Assert.Null(a1.Text);
17
18 CoreActivity a2 = new()
19 {
20 Type = "mytype"
21 };
22 Assert.NotNull(a2);
23 Assert.Equal("mytype", a2.Type);
24 Assert.Null(a2.Text);
25 }
26
27 [Fact]
28 public void Json_Nulls_Not_Deserialized()
29 {
30 string json = """
31 {
32 "type": "message",
33 "text": null
34 }
35 """;
36 CoreActivity act = CoreActivity.FromJsonString(json);
37 Assert.NotNull(act);
38 Assert.Equal("message", act.Type);
39 Assert.Null(act.Text);
40
41 string json2 = """
42 {
43 "type": "message"
44 }
45 """;
46 CoreActivity act2 = CoreActivity.FromJsonString(json2);
47 Assert.NotNull(act2);
48 Assert.Equal("message", act2.Type);
49 Assert.Null(act2.Text);
50
51 }
52
53 [Fact]
54 public void Accept_Unkown_Primitive_Fields()
55 {
56 string json = """
57 {
58 "type": "message",
59 "text": "hello",
60 "unknownString": "some string",
61 "unknownInt": 123,
62 "unknownBool": true,
63 "unknownNull": null
64 }
65 """;
66 CoreActivity act = CoreActivity.FromJsonString(json);
67 Assert.NotNull(act);
68 Assert.Equal("message", act.Type);
69 Assert.Equal("hello", act.Text);
70 Assert.True(act.Properties.ContainsKey("unknownString"));
71 Assert.True(act.Properties.ContainsKey("unknownInt"));
72 Assert.True(act.Properties.ContainsKey("unknownBool"));
73 Assert.True(act.Properties.ContainsKey("unknownNull"));
74 Assert.Equal("some string", act.Properties["unknownString"]?.ToString());
75 Assert.Equal(123, ((JsonElement)act.Properties["unknownInt"]!).GetInt32());
76 Assert.True(((JsonElement)act.Properties["unknownBool"]!).GetBoolean());
77 Assert.Null(act.Properties["unknownNull"]);
78 }
79
80 [Fact]
81 public void Serialize_Unkown_Primitive_Fields()
82 {
83 CoreActivity act = new()
84 {
85 Type = ActivityTypes.Message,
86 Text = "hello",
87 };
88 act.Properties["unknownString"] = "some string";
89 act.Properties["unknownInt"] = 123;
90 act.Properties["unknownBool"] = true;
91 act.Properties["unknownNull"] = null;
92 act.Properties["unknownLong"] = 1L;
93 act.Properties["unknownDouble"] = 1.0;
94
95 string json = act.ToJson();
96 Assert.Contains("\"type\": \"message\"", json);
97 Assert.Contains("\"text\": \"hello\"", json);
98 Assert.Contains("\"unknownString\": \"some string\"", json);
99 Assert.Contains("\"unknownInt\": 123", json);
100 Assert.Contains("\"unknownBool\": true", json);
101 Assert.Contains("\"unknownNull\": null", json);
102 Assert.Contains("\"unknownLong\": 1", json);
103 Assert.Contains("\"unknownDouble\": 1", json);
104 }
105
106 [Fact]
107 public void Deserialize_Unkown__Fields_In_KnownObjects()
108 {
109 string json = """
110 {
111 "type": "message",
112 "text": "hello",
113 "from": {
114 "id": "1",
115 "name": "tester",
116 "aadObjectId": "123"
117 }
118 }
119 """;
120 CoreActivity act = CoreActivity.FromJsonString(json);
121 Assert.NotNull(act);
122 Assert.Equal("message", act.Type);
123 Assert.Equal("hello", act.Text);
124 Assert.NotNull(act.From);
125 Assert.IsType<ConversationAccount>(act.From);
126 Assert.Equal("1", act.From!.Id);
127 Assert.Equal("tester", act.From.Name);
128 Assert.True(act.From.Properties.ContainsKey("aadObjectId"));
129 Assert.Equal("123", act.From.Properties["aadObjectId"]?.ToString());
130 }
131
132 [Fact]
133 public void Deserialize_Serialize_Unkown__Fields_In_KnownObjects()
134 {
135 string json = """
136 {
137 "type": "message",
138 "text": "hello",
139 "from": {
140 "id": "1",
141 "name": "tester",
142 "aadObjectId": "123"
143 }
144 }
145 """;
146 CoreActivity act = CoreActivity.FromJsonString(json);
147 act.Text = "updated";
148 string json2 = act.ToJson();
149 Assert.Contains("\"type\": \"message\"", json2);
150 Assert.Contains("\"text\": \"updated\"", json2);
151 Assert.Contains("\"from\": {", json2);
152 Assert.Contains("\"id\": \"1\"", json2);
153 Assert.Contains("\"name\": \"tester\"", json2);
154 Assert.Contains("\"aadObjectId\": \"123\"", json2);
155 }
156
157 [Fact]
158 public void Handling_Nulls_from_default_serializer()
159 {
160 string json = """
161 {
162 "type": "message",
163 "text": null,
164 "unknownString": null
165 }
166 """;
167 CoreActivity? act = JsonSerializer.Deserialize<CoreActivity>(json); //without default options
168 Assert.NotNull(act);
169 Assert.Equal("message", act.Type);
170 Assert.Null(act.Text);
171 Assert.Null(act.Properties["unknownString"]!);
172
173 string json2 = JsonSerializer.Serialize(act); //without default options
174 Assert.Contains("\"type\":\"message\"", json2);
175 Assert.Contains("\"text\":null", json2);
176 Assert.Contains("\"unknownString\":null", json2);
177 }
178
179 [Fact]
180 public void Serialize_With_Properties_Initialized()
181 {
182 CoreActivity act = new()
183 {
184 Type = ActivityTypes.Message,
185 Text = "hello",
186 Properties =
187 {
188 { "customField", "customValue" }
189 },
190 ChannelData = new()
191 {
192 Properties =
193 {
194 { "channelCustomField", "channelCustomValue" }
195 }
196 },
197 Conversation = new()
198 {
199 Properties =
200 {
201 { "conversationCustomField", "conversationCustomValue" }
202 }
203 },
204 From = new()
205 {
206 Id = "user1",
207 Properties =
208 {
209 { "fromCustomField", "fromCustomValue" }
210 }
211 },
212 Recipient = new()
213 {
214 Id = "bot1",
215 Properties =
216 {
217 { "recipientCustomField", "recipientCustomValue" }
218 }
219
220 }
221 };
222 string json = act.ToJson();
223 Assert.Contains("\"type\": \"message\"", json);
224 Assert.Contains("\"text\": \"hello\"", json);
225 Assert.Contains("\"customField\": \"customValue\"", json);
226 Assert.Contains("\"channelCustomField\": \"channelCustomValue\"", json);
227 Assert.Contains("\"conversationCustomField\": \"conversationCustomValue\"", json);
228 Assert.Contains("\"fromCustomField\": \"fromCustomValue\"", json);
229 Assert.Contains("\"recipientCustomField\": \"recipientCustomValue\"", json);
230 }
231
232
233 [Fact]
234 public void CreateReply()
235 {
236 CoreActivity act = new()
237 {
238 Type = "myActivityType",
239 Text = "hello",
240 Id = "CoreActivity1",
241 ChannelId = "channel1",
242 ServiceUrl = new Uri("http://service.url"),
243 From = new ConversationAccount()
244 {
245 Id = "user1",
246 Name = "User One"
247 },
248 Recipient = new ConversationAccount()
249 {
250 Id = "bot1",
251 Name = "Bot One"
252 },
253 Conversation = new Conversation()
254 {
255 Id = "conversation1"
256 }
257 };
258 CoreActivity reply = act.CreateReplyMessageActivity("reply");
259 Assert.NotNull(reply);
260 Assert.Equal(ActivityTypes.Message, reply.Type);
261 Assert.Equal("reply", reply.Text);
262 Assert.Equal("channel1", reply.ChannelId);
263 Assert.NotNull(reply.ServiceUrl);
264 Assert.Equal("http://service.url/", reply.ServiceUrl.ToString());
265 Assert.Equal("conversation1", reply.Conversation.Id);
266 Assert.Equal("bot1", reply.From.Id);
267 Assert.Equal("Bot One", reply.From.Name);
268 Assert.Equal("user1", reply.Recipient.Id);
269 Assert.Equal("User One", reply.Recipient.Name);
270 }
271
272 [Fact]
273 public async Task DeserializeAsync()
274 {
275 string json = """
276 {
277 "type": "message",
278 "text": "hello",
279 "from": {
280 "id": "1",
281 "name": "tester",
282 "aadObjectId": "123"
283 }
284 }
285 """;
286 using MemoryStream ms = new(System.Text.Encoding.UTF8.GetBytes(json));
287 CoreActivity? act = await CoreActivity.FromJsonStreamAsync(ms);
288 Assert.NotNull(act);
289 Assert.Equal("message", act.Type);
290 Assert.Equal("hello", act.Text);
291 Assert.NotNull(act.From);
292 Assert.IsType<ConversationAccount>(act.From);
293 Assert.Equal("1", act.From.Id);
294 Assert.Equal("tester", act.From.Name);
295 Assert.True(act.From.Properties.ContainsKey("aadObjectId"));
296 Assert.Equal("123", act.From.Properties["aadObjectId"]?.ToString());
297 }
298}
299