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