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