microsoft/teams.net
Publicmirrored from https://github.com/microsoft/teams.netAvailable
Tests/Microsoft.Teams.Api.Tests/Activities/Events/ReadReceiptActivityTests.cs
207lines · modecode
| 1 | using System.Text.Json; |
| 2 | |
| 3 | using Microsoft.Teams.Api.Activities; |
| 4 | using Microsoft.Teams.Api.Activities.Events; |
| 5 | using Microsoft.Teams.Api.Entities; |
| 6 | |
| 7 | namespace Microsoft.Teams.Api.Tests.Activities.Events; |
| 8 | |
| 9 | public class ReadReceiptActivityTests |
| 10 | { |
| 11 | private static readonly JsonSerializerOptions CachedJsonSerializerOptions = new JsonSerializerOptions() |
| 12 | { |
| 13 | WriteIndented = true, |
| 14 | IndentSize = 2, |
| 15 | DefaultIgnoreCondition = System.Text.Json.Serialization.JsonIgnoreCondition.WhenWritingNull |
| 16 | }; |
| 17 | |
| 18 | public ReadReceiptActivity SetupReadReceiptActivity() |
| 19 | { |
| 20 | return new ReadReceiptActivity() |
| 21 | { |
| 22 | Id = "readReceiptId", |
| 23 | ChannelData = new ChannelData() |
| 24 | { |
| 25 | EventType = "readReceipt", |
| 26 | Notification = new Notification(), |
| 27 | Channel = new Channel() |
| 28 | { |
| 29 | Id = "channelId", |
| 30 | Name = "channelName", |
| 31 | Type = new ChannelType("standard"), |
| 32 | }, |
| 33 | Settings = new ChannelDataSettings() |
| 34 | { |
| 35 | SelectedChannel = new Channel() |
| 36 | { |
| 37 | Id = "selectedChannelId", |
| 38 | Name = "selectedChannelName", |
| 39 | Type = new ChannelType("standard"), |
| 40 | }, |
| 41 | Properties = new Dictionary<string, object?>() |
| 42 | { |
| 43 | { "channelDataSettingskey1", "value1" }, |
| 44 | { "channelDataSettingskey2", "value2" }, |
| 45 | } |
| 46 | }, |
| 47 | Team = new Team() |
| 48 | { |
| 49 | Id = "teamId", |
| 50 | Name = "teamName", |
| 51 | Type = new TeamType("standard"), |
| 52 | }, |
| 53 | Tenant = new Tenant() |
| 54 | { |
| 55 | Id = "tenantId", |
| 56 | }, |
| 57 | App = new App() |
| 58 | { |
| 59 | Id = "appId", |
| 60 | Properties = new Dictionary<string, object?>() |
| 61 | { |
| 62 | { "appPropKey1", "value1" }, |
| 63 | { "appPropKey2", "value2" }, |
| 64 | } |
| 65 | }, |
| 66 | FeedbackLoopEnabled = true, |
| 67 | Properties = new Dictionary<string, object?>() |
| 68 | { |
| 69 | { "key1", "value1" }, |
| 70 | { "key2", "value2" }, |
| 71 | }, |
| 72 | StreamId = "streamId", |
| 73 | StreamSequence = 3, |
| 74 | StreamType = new StreamType("streaming"), |
| 75 | }, |
| 76 | Conversation = new Api.Conversation() |
| 77 | { |
| 78 | Id = "conversationId", |
| 79 | Name = "conversationName", |
| 80 | Type = new ConversationType("group"), |
| 81 | }, |
| 82 | ChannelId = new ChannelId("webchat"), |
| 83 | Recipient = new Account() |
| 84 | { |
| 85 | Id = "recipientId", |
| 86 | Name = "recipientName", |
| 87 | }, |
| 88 | ReplyToId = "replyToId", |
| 89 | }; |
| 90 | } |
| 91 | |
| 92 | [Fact] |
| 93 | public void ReadReceiptActivity_Props() |
| 94 | { |
| 95 | var activity = SetupReadReceiptActivity(); |
| 96 | |
| 97 | Assert.NotNull(activity.ToReadReceipt()); |
| 98 | ActivityType expectedEventType = new ActivityType("event"); |
| 99 | Assert.Equal(expectedEventType.ToString(), activity.Type.Value); |
| 100 | Assert.True(activity.Name.IsReadReceipt); |
| 101 | Assert.False(activity.IsStreaming); |
| 102 | var expectedSubmitException = "Unable to cast object of type 'Microsoft.Teams.Api.Activities.Events.ReadReceiptActivity' to type 'Microsoft.Teams.Api.Activities.MessageActivity'."; |
| 103 | var ex = Assert.Throws<System.InvalidCastException>(() => activity.ToMessage()); |
| 104 | Assert.Equal(expectedSubmitException, ex.Message); |
| 105 | } |
| 106 | |
| 107 | [Fact] |
| 108 | public void ReadReceiptActivity_JsonSerialize() |
| 109 | { |
| 110 | var activity = SetupReadReceiptActivity(); |
| 111 | |
| 112 | var json = JsonSerializer.Serialize(activity, CachedJsonSerializerOptions); |
| 113 | |
| 114 | Assert.Equal(File.ReadAllText( |
| 115 | @"../../../Json/Activity/Events/ReadReceiptActivity.json" |
| 116 | ), json); |
| 117 | } |
| 118 | |
| 119 | |
| 120 | [Fact] |
| 121 | public void ReadReceiptActivity_JsonSerialize_Object() |
| 122 | { |
| 123 | ReadReceiptActivity activity = SetupReadReceiptActivity(); |
| 124 | |
| 125 | var json = JsonSerializer.Serialize(activity, CachedJsonSerializerOptions); |
| 126 | |
| 127 | string expectedPath = "Activity.Event.Application/vnd.microsoft.readReceipt"; |
| 128 | Assert.Equal(expectedPath, activity.GetPath()); |
| 129 | Assert.Equal(File.ReadAllText( |
| 130 | @"../../../Json/Activity/Events/ReadReceiptActivity.json" |
| 131 | ), json); |
| 132 | } |
| 133 | |
| 134 | [Fact] |
| 135 | public void ReadReceiptActivity_JsonSerialize_Derived_From_Class() |
| 136 | { |
| 137 | EventActivity activity = SetupReadReceiptActivity(); |
| 138 | |
| 139 | var json = JsonSerializer.Serialize(activity, CachedJsonSerializerOptions); |
| 140 | |
| 141 | string expectedPath = "Activity.Event.Application/vnd.microsoft.readReceipt"; |
| 142 | Assert.Equal(expectedPath, activity.GetPath()); |
| 143 | Assert.Equal(typeof(ReadReceiptActivity), activity.Name.ToType()); |
| 144 | Assert.True(activity.Name.IsReadReceipt); |
| 145 | Assert.False(activity.Name.IsMeetingStart); |
| 146 | Assert.Equal(File.ReadAllText( |
| 147 | @"../../../Json/Activity/Events/ReadReceiptActivity.json" |
| 148 | ), json); |
| 149 | } |
| 150 | |
| 151 | [Fact] |
| 152 | public void ReadReceiptActivity_JsonSerialize_Derived_From_Interface() |
| 153 | { |
| 154 | IActivity activity = SetupReadReceiptActivity(); |
| 155 | |
| 156 | var json = JsonSerializer.Serialize(activity, CachedJsonSerializerOptions); |
| 157 | |
| 158 | string expectedPath = "Activity.Event.Application/vnd.microsoft.readReceipt"; |
| 159 | Assert.Equal(expectedPath, activity.GetPath()); |
| 160 | Assert.Equal(File.ReadAllText( |
| 161 | @"../../../Json/Activity/Events/ReadReceiptActivity.json" |
| 162 | ), json); |
| 163 | } |
| 164 | |
| 165 | [Fact] |
| 166 | public void ReadReceiptActivity_JsonDeserialize() |
| 167 | { |
| 168 | var json = File.ReadAllText(@"../../../Json/Activity/Events/ReadReceiptActivity.json"); |
| 169 | var activity = JsonSerializer.Deserialize<ReadReceiptActivity>(json); |
| 170 | var expected = SetupReadReceiptActivity(); |
| 171 | |
| 172 | Assert.Equal(expected.ToString(), activity!.ToString()); |
| 173 | Assert.Equal(typeof(ReadReceiptActivity), activity.Name.ToType()); |
| 174 | Assert.Equal("Application/vnd.microsoft.readReceipt", activity.Name.ToPrettyString()); |
| 175 | Assert.NotNull(activity.ToReadReceipt()); |
| 176 | } |
| 177 | |
| 178 | |
| 179 | [Fact] |
| 180 | public void ReadReceiptActivity_JsonDeserialize_Derived() |
| 181 | { |
| 182 | var json = File.ReadAllText(@"../../../Json/Activity/Events/ReadReceiptActivity.json"); |
| 183 | var activity = JsonSerializer.Deserialize<EventActivity>(json); |
| 184 | var expected = SetupReadReceiptActivity(); |
| 185 | |
| 186 | Assert.Equal(expected.ToString(), activity!.ToString()); |
| 187 | Assert.NotNull(activity.ToEvent()); |
| 188 | Assert.Equal(typeof(ReadReceiptActivity), activity.Name.ToType()); |
| 189 | var expectedSubmitException = "Unable to cast object of type 'Microsoft.Teams.Api.Activities.Events.ReadReceiptActivity' to type 'Microsoft.Teams.Api.Activities.InstallUpdateActivity'."; |
| 190 | var ex = Assert.Throws<System.InvalidCastException>(() => activity.ToInstallUpdate()); |
| 191 | Assert.Equal(expectedSubmitException, ex.Message); |
| 192 | } |
| 193 | |
| 194 | [Fact] |
| 195 | public void ReadReceiptActivity_JsonDeserialize_Activity_Derived() |
| 196 | { |
| 197 | var json = File.ReadAllText(@"../../../Json/Activity/Events/ReadReceiptActivity.json"); |
| 198 | var activity = JsonSerializer.Deserialize<Activity>(json); |
| 199 | var expected = SetupReadReceiptActivity(); |
| 200 | |
| 201 | Assert.Equal(expected.ToString(), activity!.ToString()); |
| 202 | Assert.NotNull(activity.ToEvent()); |
| 203 | var expectedSubmitException = "Unable to cast object of type 'Microsoft.Teams.Api.Activities.Events.ReadReceiptActivity' to type 'Microsoft.Teams.Api.Activities.InstallUpdateActivity'."; |
| 204 | var ex = Assert.Throws<System.InvalidCastException>(() => activity.ToInstallUpdate()); |
| 205 | Assert.Equal(expectedSubmitException, ex.Message); |
| 206 | } |
| 207 | } |