microsoft/teams.net
Publicmirrored from https://github.com/microsoft/teams.netAvailable
Tests/Microsoft.Teams.Api.Tests/Activities/Conversation/ConversationUpdateActivityTests.cs
136lines · modecode
| 1 | |
| 2 | |
| 3 | using System.Text.Json; |
| 4 | |
| 5 | using Microsoft.Teams.Api.Activities; |
| 6 | |
| 7 | namespace Microsoft.Teams.Api.Tests.Activities.Conversation; |
| 8 | |
| 9 | public class ConversationUpdateActivityTests |
| 10 | { |
| 11 | private ConversationUpdateActivity SetupConversationUpdateActivity() |
| 12 | { |
| 13 | return new ConversationUpdateActivity() |
| 14 | { |
| 15 | TopicName = "topicName", |
| 16 | MembersAdded = |
| 17 | [ |
| 18 | new Account { Id = "userId1", Name = "User One" }, |
| 19 | new Account { Id = "userId2", Name = "User Two" } |
| 20 | ], |
| 21 | MembersRemoved = |
| 22 | [ |
| 23 | new Account { Id = "userId3", Name = "User Three" }, |
| 24 | |
| 25 | ], |
| 26 | ChannelId = new ChannelId("msteams"), |
| 27 | Conversation = new Api.Conversation() |
| 28 | { |
| 29 | Type = new ConversationType("channel"), |
| 30 | Id = "someguid", |
| 31 | TenantId = "tenantId", |
| 32 | Name = "channelName", |
| 33 | IsGroup = false, |
| 34 | |
| 35 | }, |
| 36 | From = new Account() |
| 37 | { |
| 38 | Id = "botId", |
| 39 | Name = "Bot user", |
| 40 | Role = new Role("bot"), |
| 41 | AadObjectId = "aadObjectId", |
| 42 | Properties = new Dictionary<string, object>() |
| 43 | { |
| 44 | { "key1", "value1" }, |
| 45 | { "key2", "value2" }, |
| 46 | }, |
| 47 | }, |
| 48 | Recipient = new Account() |
| 49 | { |
| 50 | Id = "userId1", |
| 51 | Name = "User One" |
| 52 | }, |
| 53 | }; |
| 54 | } |
| 55 | [Fact] |
| 56 | public void ConversationUpdateActivity_Props() |
| 57 | { |
| 58 | var activity = SetupConversationUpdateActivity(); |
| 59 | |
| 60 | var expectedSubmitException = "Unable to cast object of type 'Microsoft.Teams.Api.Activities.ConversationUpdateActivity' to type 'Microsoft.Teams.Api.Activities.EndOfConversationActivity'."; |
| 61 | |
| 62 | Assert.NotNull(activity.ToConversationUpdate()); |
| 63 | |
| 64 | var ex = Assert.Throws<System.InvalidCastException>(() => activity.ToEndOfConversation()); |
| 65 | Assert.Equal(expectedSubmitException, ex.Message); |
| 66 | } |
| 67 | |
| 68 | private static readonly JsonSerializerOptions CachedJsonSerializerOptions = new JsonSerializerOptions |
| 69 | { |
| 70 | WriteIndented = true, |
| 71 | DefaultIgnoreCondition = System.Text.Json.Serialization.JsonIgnoreCondition.WhenWritingNull |
| 72 | }; |
| 73 | |
| 74 | [Fact] |
| 75 | public void ConversationUpdateActivity_JsonSerialize() |
| 76 | { |
| 77 | var activity = SetupConversationUpdateActivity(); |
| 78 | |
| 79 | var json = JsonSerializer.Serialize(activity, CachedJsonSerializerOptions); |
| 80 | |
| 81 | Assert.Equal(File.ReadAllText( |
| 82 | @"../../../Json/Activity/Conversation/ConversationUpdateActivity.json" |
| 83 | ), json); |
| 84 | } |
| 85 | |
| 86 | [Fact] |
| 87 | public void ConversationUpdateActivity_JsonSerialize_Derived_From_Class() |
| 88 | { |
| 89 | Activity activity = SetupConversationUpdateActivity(); |
| 90 | |
| 91 | var json = JsonSerializer.Serialize(activity, CachedJsonSerializerOptions); |
| 92 | |
| 93 | Assert.Equal(File.ReadAllText( |
| 94 | @"../../../Json/Activity/Conversation/ConversationUpdateActivity.json" |
| 95 | ), json); |
| 96 | } |
| 97 | |
| 98 | [Fact] |
| 99 | public void ConversationUpdateActivity_JsonSerialize_Derived_From_Interface() |
| 100 | { |
| 101 | IActivity activity = SetupConversationUpdateActivity(); |
| 102 | |
| 103 | var json = JsonSerializer.Serialize(activity, CachedJsonSerializerOptions); |
| 104 | |
| 105 | Assert.Equal(File.ReadAllText( |
| 106 | @"../../../Json/Activity/Conversation/ConversationUpdateActivity.json" |
| 107 | ), json); |
| 108 | } |
| 109 | |
| 110 | |
| 111 | [Fact] |
| 112 | public void ConversationUpdateActivity_JsonDeserialize() |
| 113 | { |
| 114 | var json = File.ReadAllText(@"../../../Json/Activity/Conversation/ConversationUpdateActivity.json"); |
| 115 | var activity = JsonSerializer.Deserialize<ConversationUpdateActivity>(json); |
| 116 | var expected = SetupConversationUpdateActivity(); |
| 117 | |
| 118 | Assert.Equal(expected.ToString(), activity!.ToString()); |
| 119 | Assert.NotNull(activity.ToConversationUpdate()); |
| 120 | } |
| 121 | |
| 122 | |
| 123 | [Fact] |
| 124 | public void ConversationUpdateActivity_JsonDeserialize_Derived() |
| 125 | { |
| 126 | var json = File.ReadAllText(@"../../../Json/Activity/Conversation/ConversationUpdateActivity.json"); |
| 127 | var activity = JsonSerializer.Deserialize<Activity>(json); |
| 128 | var expected = SetupConversationUpdateActivity(); |
| 129 | |
| 130 | Assert.Equal(expected.ToString(), activity!.ToString()); |
| 131 | Assert.NotNull(activity.ToConversationUpdate()); |
| 132 | var expectedSubmitException = "Unable to cast object of type 'Microsoft.Teams.Api.Activities.ConversationUpdateActivity' to type 'Microsoft.Teams.Api.Activities.InstallUpdateActivity'."; |
| 133 | var ex = Assert.Throws<System.InvalidCastException>(() => activity.ToInstallUpdate()); |
| 134 | Assert.Equal(expectedSubmitException, ex.Message); |
| 135 | } |
| 136 | } |