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