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