|
2 | 2 | // Licensed under the MIT License. |
3 | 3 |
|
4 | 4 | using System.Collections.Generic; |
| 5 | +using System.IO; |
| 6 | +using System.Linq; |
| 7 | +using System.Text; |
| 8 | +using Newtonsoft.Json; |
5 | 9 | using Newtonsoft.Json.Linq; |
6 | 10 | using Xunit; |
7 | 11 |
|
@@ -98,5 +102,156 @@ public void AttachmentViewInits() |
98 | 102 | Assert.Equal(viewId, attachmentView.ViewId); |
99 | 103 | Assert.Equal(size, attachmentView.Size); |
100 | 104 | } |
| 105 | + |
| 106 | + [Fact] |
| 107 | + public void AttachmentShouldWorkWithoutJsonConverter() |
| 108 | + { |
| 109 | + var text = "Hi!"; |
| 110 | + var activity = new ActivityDummy |
| 111 | + { |
| 112 | + Attachments = new Attachment[] |
| 113 | + { |
| 114 | + new AttachmentDummy { ContentType = "string", Content = text }, |
| 115 | + new AttachmentDummy { ContentType = "string/array", Content = new string[] { text } }, |
| 116 | + new AttachmentDummy { ContentType = "dict", Content = new Dictionary<string, object> { { "firstname", "John" }, { "attachment1", new AttachmentDummy(content: text) }, { "lastname", "Doe" }, { "attachment2", new AttachmentDummy(content: text) }, { "age", 18 } } }, |
| 117 | + new AttachmentDummy { ContentType = "attachment", Content = new AttachmentDummy(content: text) }, |
| 118 | + new AttachmentDummy { ContentType = "attachment/dict", Content = new Dictionary<string, AttachmentDummy> { { "attachment", new AttachmentDummy(content: text) }, { "attachment2", new AttachmentDummy(content: text) } } }, |
| 119 | + new AttachmentDummy { ContentType = "attachment/dict/nested", Content = new Dictionary<string, Dictionary<string, AttachmentDummy>> { { "attachment", new Dictionary<string, AttachmentDummy> { { "content", new AttachmentDummy(content: text) } } } } }, |
| 120 | + new AttachmentDummy { ContentType = "attachment/list", Content = new List<AttachmentDummy> { new AttachmentDummy(content: text), new AttachmentDummy(content: text) } }, |
| 121 | + new AttachmentDummy { ContentType = "attachment/list/nested", Content = new List<List<AttachmentDummy>> { new List<AttachmentDummy> { new AttachmentDummy(content: text) } } }, |
| 122 | + } |
| 123 | + }; |
| 124 | + |
| 125 | + AssertAttachment(activity); |
| 126 | + } |
| 127 | + |
| 128 | + [Fact] |
| 129 | + public void AttachmentShouldWorkWithJsonConverter() |
| 130 | + { |
| 131 | + var text = "Hi!"; |
| 132 | + var activity = new Activity |
| 133 | + { |
| 134 | + Attachments = new Attachment[] |
| 135 | + { |
| 136 | + new Attachment { ContentType = "string", Content = text }, |
| 137 | + new Attachment { ContentType = "string/array", Content = new string[] { text } }, |
| 138 | + new Attachment { ContentType = "dict", Content = new Dictionary<string, object> { { "firstname", "John" }, { "attachment1", new Attachment(content: text) }, { "lastname", "Doe" }, { "attachment2", new Attachment(content: text) }, { "age", 18 } } }, |
| 139 | + new Attachment { ContentType = "attachment", Content = new Attachment(content: text) }, |
| 140 | + new Attachment { ContentType = "attachment/dict", Content = new Dictionary<string, Attachment> { { "attachment", new Attachment(content: text) } } }, |
| 141 | + new Attachment { ContentType = "attachment/dict/nested", Content = new Dictionary<string, Dictionary<string, Attachment>> { { "attachment", new Dictionary<string, Attachment> { { "content", new Attachment(content: text) } } } } }, |
| 142 | + new Attachment { ContentType = "attachment/list", Content = new List<Attachment> { new Attachment(content: text), new Attachment(content: text) } }, |
| 143 | + new Attachment { ContentType = "attachment/list/nested", Content = new List<List<Attachment>> { new List<Attachment> { new Attachment(content: text) } } }, |
| 144 | + } |
| 145 | + }; |
| 146 | + |
| 147 | + AssertAttachment(activity); |
| 148 | + } |
| 149 | + |
| 150 | + [Fact] |
| 151 | + public void MemoryStreamAttachmentShouldWorkWithJsonConverter() |
| 152 | + { |
| 153 | + var text = "Hi!"; |
| 154 | + var buffer = Encoding.UTF8.GetBytes(text); |
| 155 | + var activity = new Activity |
| 156 | + { |
| 157 | + Attachments = new Attachment[] |
| 158 | + { |
| 159 | + new Attachment { ContentType = "stream", Content = new MemoryStream(buffer) }, |
| 160 | + new Attachment { ContentType = "stream/empty", Content = new MemoryStream() }, |
| 161 | + new Attachment { ContentType = "stream/dict", Content = new Dictionary<string, MemoryStream> { { "stream", new MemoryStream(buffer) } } }, |
| 162 | + new Attachment { ContentType = "stream/dict/nested", Content = new Dictionary<string, Dictionary<string, MemoryStream>> { { "stream", new Dictionary<string, MemoryStream> { { "content", new MemoryStream(buffer) } } } } }, |
| 163 | + new Attachment { ContentType = "stream/list", Content = new List<MemoryStream> { new MemoryStream(buffer), new MemoryStream(buffer) } }, |
| 164 | + new Attachment { ContentType = "stream/list/nested", Content = new List<List<MemoryStream>> { new List<MemoryStream> { new MemoryStream(buffer) } } }, |
| 165 | + } |
| 166 | + }; |
| 167 | + |
| 168 | + var serialized = JsonConvert.SerializeObject(activity, new JsonSerializerSettings { MaxDepth = null }); |
| 169 | + var deserialized = JsonConvert.DeserializeObject<Activity>(serialized); |
| 170 | + |
| 171 | + var buffer0 = (GetAttachmentContentByType(deserialized, "stream") as MemoryStream).ToArray(); |
| 172 | + var buffer1 = (GetAttachmentContentByType(deserialized, "stream/empty") as MemoryStream).ToArray(); |
| 173 | + var buffer2 = ((GetAttachmentContentByType(deserialized, "stream/dict") as Dictionary<string, object>)["stream"] as MemoryStream).ToArray(); |
| 174 | + var buffer3 = (((GetAttachmentContentByType(deserialized, "stream/dict/nested") as Dictionary<string, object>)["stream"] as Dictionary<string, object>)["content"] as MemoryStream).ToArray(); |
| 175 | + var buffer4 = ((GetAttachmentContentByType(deserialized, "stream/list") as List<object>)[0] as MemoryStream).ToArray(); |
| 176 | + var buffer4_1 = ((GetAttachmentContentByType(deserialized, "stream/list") as List<object>)[1] as MemoryStream).ToArray(); |
| 177 | + var buffer5 = (((GetAttachmentContentByType(deserialized, "stream/list/nested") as List<object>)[0] as List<object>)[0] as MemoryStream).ToArray(); |
| 178 | + |
| 179 | + Assert.Equal(text, Encoding.UTF8.GetString(buffer0)); |
| 180 | + Assert.Equal(buffer, buffer0); |
| 181 | + Assert.Equal([], buffer1); |
| 182 | + Assert.Equal(buffer, buffer2); |
| 183 | + Assert.Equal(buffer, buffer3); |
| 184 | + Assert.Equal(buffer, buffer4); |
| 185 | + Assert.Equal(buffer, buffer4_1); |
| 186 | + Assert.Equal(buffer, buffer5); |
| 187 | + } |
| 188 | + |
| 189 | + [Fact] |
| 190 | + public void MemoryStreamAttachmentShouldFailWithoutJsonConverter() |
| 191 | + { |
| 192 | + var text = "Hi!"; |
| 193 | + var buffer = Encoding.UTF8.GetBytes(text); |
| 194 | + var activity = new ActivityDummy |
| 195 | + { |
| 196 | + Attachments = new Attachment[] |
| 197 | + { |
| 198 | + new AttachmentDummy { ContentType = "stream", Content = new MemoryStream(buffer) }, |
| 199 | + } |
| 200 | + }; |
| 201 | + |
| 202 | + var ex = Assert.Throws<JsonSerializationException>(() => JsonConvert.SerializeObject(activity, new JsonSerializerSettings { MaxDepth = null })); |
| 203 | + Assert.Contains("ReadTimeout", ex.Message); |
| 204 | + } |
| 205 | + |
| 206 | + private void AssertAttachment<T>(T activity) |
| 207 | + where T : Activity |
| 208 | + { |
| 209 | + var serialized = JsonConvert.SerializeObject(activity, new JsonSerializerSettings { MaxDepth = null }); |
| 210 | + var deserialized = JsonConvert.DeserializeObject<T>(serialized); |
| 211 | + |
| 212 | + var attachment0 = GetAttachmentContentByType(deserialized, "string") as string; |
| 213 | + var attachment1 = (GetAttachmentContentByType(deserialized, "string/array") as JArray).First.Value<string>(); |
| 214 | + var attachment2 = GetAttachmentContentByType(deserialized, "dict") as JObject; |
| 215 | + var attachment3 = (GetAttachmentContentByType(deserialized, "attachment") as JObject).Value<string>("content"); |
| 216 | + var attachment4 = (GetAttachmentContentByType(deserialized, "attachment/dict") as JObject).GetValue("attachment").Value<string>("content"); |
| 217 | + var attachment5 = ((GetAttachmentContentByType(deserialized, "attachment/dict/nested") as JObject).GetValue("attachment") as JObject).GetValue("content").Value<string>("content"); |
| 218 | + var attachment6 = (GetAttachmentContentByType(deserialized, "attachment/list") as JArray)[0].Value<string>("content"); |
| 219 | + var attachment6_1 = (GetAttachmentContentByType(deserialized, "attachment/list") as JArray)[1].Value<string>("content"); |
| 220 | + var attachment7 = (GetAttachmentContentByType(deserialized, "attachment/list/nested") as JArray).First.First.Value<string>("content"); |
| 221 | + |
| 222 | + var expectedString = GetAttachmentContentByType(activity, "string") as string; |
| 223 | + var expectedDict = GetAttachmentContentByType(activity, "dict") as Dictionary<string, object>; |
| 224 | + Assert.Equal(expectedString, attachment0); |
| 225 | + Assert.Equal(expectedString, attachment1); |
| 226 | + Assert.Equal($"{expectedDict["firstname"]} {expectedDict["lastname"]} {expectedDict["age"]}", $"{attachment2["firstname"]} {attachment2["lastname"]} {attachment2["age"]}"); |
| 227 | + Assert.Equal(expectedString, attachment3); |
| 228 | + Assert.Equal(expectedString, attachment4); |
| 229 | + Assert.Equal(expectedString, attachment5); |
| 230 | + Assert.Equal(expectedString, attachment6); |
| 231 | + Assert.Equal(expectedString, attachment6_1); |
| 232 | + Assert.Equal(expectedString, attachment7); |
| 233 | + } |
| 234 | + |
| 235 | + private object GetAttachmentContentByType<T>(T activity, string contenttype) |
| 236 | + where T : Activity |
| 237 | + { |
| 238 | + var attachment = activity.Attachments.First(e => e.ContentType == contenttype); |
| 239 | + return attachment.Content ?? (attachment as AttachmentDummy).Content; |
| 240 | + } |
| 241 | + |
| 242 | + public class ActivityDummy : Activity |
| 243 | + { |
| 244 | + } |
| 245 | + |
| 246 | + public class AttachmentDummy : Attachment |
| 247 | + { |
| 248 | + public AttachmentDummy(object content = default) |
| 249 | + { |
| 250 | + Content = content; |
| 251 | + } |
| 252 | + |
| 253 | + [JsonProperty(PropertyName = "content")] |
| 254 | + public new object Content { get; set; } |
| 255 | + } |
101 | 256 | } |
102 | 257 | } |
0 commit comments