microsoft/teams.net
Publicmirrored from https://github.com/microsoft/teams.netAvailable
Tests/Microsoft.Teams.Api.Tests/Activities/ActivityTests.cs
92lines · modecode
| 1 | using System.Text.Json; |
| 2 | |
| 3 | using Microsoft.Teams.Api.Activities; |
| 4 | |
| 5 | namespace Microsoft.Teams.Api.Tests.Activities; |
| 6 | |
| 7 | public class ActivityTests |
| 8 | { |
| 9 | [Fact] |
| 10 | public void JsonSerialize_Class() |
| 11 | { |
| 12 | var activity = new Activity("unknown") |
| 13 | { |
| 14 | Id = "1", |
| 15 | From = new() |
| 16 | { |
| 17 | Id = "1", |
| 18 | Name = "test", |
| 19 | Role = Role.User |
| 20 | }, |
| 21 | Conversation = new() |
| 22 | { |
| 23 | Id = "1", |
| 24 | Type = ConversationType.Personal |
| 25 | }, |
| 26 | Recipient = new() |
| 27 | { |
| 28 | Id = "2", |
| 29 | Name = "test-bot", |
| 30 | Role = Role.Bot |
| 31 | } |
| 32 | }; |
| 33 | |
| 34 | activity.Properties["hello"] = "world"; |
| 35 | |
| 36 | var json = JsonSerializer.Serialize(activity, new JsonSerializerOptions() |
| 37 | { |
| 38 | WriteIndented = true, |
| 39 | IndentSize = 4, |
| 40 | DefaultIgnoreCondition = System.Text.Json.Serialization.JsonIgnoreCondition.WhenWritingNull |
| 41 | }); |
| 42 | |
| 43 | Assert.Equal(File.ReadAllText( |
| 44 | @"../../../Json/Activity/Activity.json" |
| 45 | ), json); |
| 46 | |
| 47 | var newActivity = JsonSerializer.Deserialize<Activity>(json); |
| 48 | Assert.Equal(newActivity?.ToString(), activity.ToString()); |
| 49 | } |
| 50 | |
| 51 | [Fact] |
| 52 | public void JsonSerialize_Interface() |
| 53 | { |
| 54 | IActivity activity = new Activity("unknown") |
| 55 | { |
| 56 | Id = "1", |
| 57 | From = new() |
| 58 | { |
| 59 | Id = "1", |
| 60 | Name = "test", |
| 61 | Role = Role.User |
| 62 | }, |
| 63 | Conversation = new() |
| 64 | { |
| 65 | Id = "1", |
| 66 | Type = ConversationType.Personal |
| 67 | }, |
| 68 | Recipient = new() |
| 69 | { |
| 70 | Id = "2", |
| 71 | Name = "test-bot", |
| 72 | Role = Role.Bot |
| 73 | } |
| 74 | }; |
| 75 | |
| 76 | activity.Properties["hello"] = "world"; |
| 77 | |
| 78 | var json = JsonSerializer.Serialize(activity, new JsonSerializerOptions() |
| 79 | { |
| 80 | WriteIndented = true, |
| 81 | IndentSize = 4, |
| 82 | DefaultIgnoreCondition = System.Text.Json.Serialization.JsonIgnoreCondition.WhenWritingNull |
| 83 | }); |
| 84 | |
| 85 | Assert.Equal(File.ReadAllText( |
| 86 | @"../../../Json/Activity/Activity.json" |
| 87 | ), json); |
| 88 | |
| 89 | var newActivity = JsonSerializer.Deserialize<IActivity>(json); |
| 90 | Assert.Equal(newActivity?.ToString(), activity.ToString()); |
| 91 | } |
| 92 | } |