microsoft/teams.net
Publicmirrored from https://github.com/microsoft/teams.netAvailable
Tests/Microsoft.Teams.Api.Tests/Activities/Invokes/MessageExtensions/QueryMEActivityTests.cs
167lines · modecode
| 1 | using System.Text.Json; |
| 2 | |
| 3 | using Microsoft.Teams.Api.Activities; |
| 4 | using Microsoft.Teams.Api.Activities.Invokes; |
| 5 | using Microsoft.Teams.Api.Entities; |
| 6 | using Microsoft.Teams.Api.MessageExtensions; |
| 7 | |
| 8 | using static Microsoft.Teams.Api.Activities.Invokes.MessageExtensions; |
| 9 | |
| 10 | namespace Microsoft.Teams.Api.Tests.Activities.Invokes; |
| 11 | |
| 12 | public class QueryMEActivityTests |
| 13 | { |
| 14 | private QueryActivity SetupQueryActivity() |
| 15 | { |
| 16 | IList<IEntity> _entityList = |
| 17 | [ |
| 18 | new ClientInfoEntity() |
| 19 | { |
| 20 | Platform = "Windows", |
| 21 | Locale = "en-US", |
| 22 | Country = "US", |
| 23 | Timezone = "GMT-8", |
| 24 | } |
| 25 | ]; |
| 26 | |
| 27 | return new QueryActivity() |
| 28 | { |
| 29 | Value = new Query() |
| 30 | { |
| 31 | CommandId = "searchCmd", |
| 32 | Parameters = new List<Parameter>() |
| 33 | { |
| 34 | new Parameter() |
| 35 | { |
| 36 | Name = "Somelist", |
| 37 | Value = "Toronto" |
| 38 | } |
| 39 | }, |
| 40 | }, |
| 41 | Conversation = new Api.Conversation() |
| 42 | { |
| 43 | Id = "convId", |
| 44 | Type = ConversationType.Personal |
| 45 | }, |
| 46 | Id = "id:data", |
| 47 | ServiceUrl = "https://me-url", |
| 48 | From = new Account() |
| 49 | { |
| 50 | Id = "botId", |
| 51 | Name = "User Name", |
| 52 | AadObjectId = "aadObjectId" |
| 53 | }, |
| 54 | Recipient = new Account() |
| 55 | { |
| 56 | Id = "recipientId", |
| 57 | Name = "Recipient Name", |
| 58 | }, |
| 59 | Entities = _entityList, |
| 60 | }; |
| 61 | } |
| 62 | |
| 63 | [Fact] |
| 64 | public void QueryMEActivity_JsonSerialize() |
| 65 | { |
| 66 | var activity = SetupQueryActivity(); |
| 67 | |
| 68 | var json = JsonSerializer.Serialize(activity, new JsonSerializerOptions() |
| 69 | { |
| 70 | WriteIndented = true, |
| 71 | IndentSize = 2, |
| 72 | DefaultIgnoreCondition = System.Text.Json.Serialization.JsonIgnoreCondition.WhenWritingNull |
| 73 | }); |
| 74 | |
| 75 | string expectedPath = "Activity.Invoke.ComposeExtension/query"; |
| 76 | Assert.Equal(expectedPath, activity.GetPath()); |
| 77 | Assert.Equal(File.ReadAllText( |
| 78 | @"../../../Json/Activity/Invokes/QueryMEActivity.json" |
| 79 | ), json); |
| 80 | } |
| 81 | |
| 82 | [Fact] |
| 83 | public void QueryMEActivity_JsonSerialize_Derived() |
| 84 | { |
| 85 | MessageExtensionActivity activity = SetupQueryActivity(); |
| 86 | |
| 87 | var json = JsonSerializer.Serialize(activity, new JsonSerializerOptions() |
| 88 | { |
| 89 | WriteIndented = true, |
| 90 | IndentSize = 2, |
| 91 | DefaultIgnoreCondition = System.Text.Json.Serialization.JsonIgnoreCondition.WhenWritingNull |
| 92 | }); |
| 93 | |
| 94 | string expectedPath = "Activity.Invoke.ComposeExtension/query"; |
| 95 | Assert.Equal(expectedPath, activity.GetPath()); |
| 96 | Assert.Equal(File.ReadAllText( |
| 97 | @"../../../Json/Activity/Invokes/QueryMEActivity.json" |
| 98 | ), json); |
| 99 | } |
| 100 | |
| 101 | [Fact] |
| 102 | public void QueryMEActivity_JsonSerialize_Derived_Interface() |
| 103 | { |
| 104 | InvokeActivity activity = SetupQueryActivity(); |
| 105 | |
| 106 | var json = JsonSerializer.Serialize(activity, new JsonSerializerOptions() |
| 107 | { |
| 108 | WriteIndented = true, |
| 109 | IndentSize = 2, |
| 110 | DefaultIgnoreCondition = System.Text.Json.Serialization.JsonIgnoreCondition.WhenWritingNull |
| 111 | }); |
| 112 | |
| 113 | string expectedPath = "Activity.Invoke.ComposeExtension/query"; |
| 114 | Assert.Equal(expectedPath, activity.GetPath()); |
| 115 | Assert.Equal(File.ReadAllText( |
| 116 | @"../../../Json/Activity/Invokes/QueryMEActivity.json" |
| 117 | ), json); |
| 118 | } |
| 119 | |
| 120 | [Fact] |
| 121 | public void QueryMEActivity_JsonDeserialize() |
| 122 | { |
| 123 | var json = File.ReadAllText(@"../../../Json/Activity/Invokes/QueryMEActivity.json"); |
| 124 | var activity = JsonSerializer.Deserialize<QueryActivity>(json); |
| 125 | var expected = SetupQueryActivity(); |
| 126 | |
| 127 | Assert.Equal(expected.ToString(), activity!.ToString()); |
| 128 | Assert.NotNull(activity.ToMessageExtension()); |
| 129 | } |
| 130 | |
| 131 | [Fact] |
| 132 | public void QueryMEActivity_JsonDeserialize_Derived() |
| 133 | { |
| 134 | var json = File.ReadAllText(@"../../../Json/Activity/Invokes/QueryMEActivity.json"); |
| 135 | var activity = JsonSerializer.Deserialize<MessageExtensionActivity>(json); |
| 136 | var expected = SetupQueryActivity(); |
| 137 | |
| 138 | Assert.Equal(expected.ToString(), activity!.ToString()); |
| 139 | Assert.NotNull(activity.ToMessageExtension()); |
| 140 | var expectedSubmitException = "Unable to cast object of type 'QueryActivity' to type 'Microsoft.Teams.Api.Activities.Invokes.TaskActivity'."; |
| 141 | var ex = Assert.Throws<System.InvalidCastException>(() => activity.ToTask()); |
| 142 | Assert.Equal(expectedSubmitException, ex.Message); |
| 143 | } |
| 144 | |
| 145 | [Fact] |
| 146 | public void QueryMEActivity_JsonDeserialize_Derived_Interface() |
| 147 | { |
| 148 | var json = File.ReadAllText(@"../../../Json/Activity/Invokes/QueryMEActivity.json"); |
| 149 | var activity = JsonSerializer.Deserialize<InvokeActivity>(json); |
| 150 | var expected = SetupQueryActivity(); |
| 151 | |
| 152 | Assert.NotNull(activity); |
| 153 | Assert.Equal(expected.ToString(), activity.ToString()); |
| 154 | Assert.NotNull(activity.ToMessageExtension()); |
| 155 | } |
| 156 | |
| 157 | [Fact] |
| 158 | public void QueryMEActivity_JsonDeserialize_Derived_Activity_Interface() |
| 159 | { |
| 160 | var json = File.ReadAllText(@"../../../Json/Activity/Invokes/QueryMEActivity.json"); |
| 161 | var activity = JsonSerializer.Deserialize<Activity>(json); |
| 162 | var expected = SetupQueryActivity(); |
| 163 | |
| 164 | Assert.NotNull(activity); |
| 165 | Assert.Equal(expected.ToString(), activity.ToString()); |
| 166 | } |
| 167 | } |