|
2 | 2 | // Licensed under the Apache 2.0 license.
|
3 | 3 | // See LICENSE file in the project root for full license information.
|
4 | 4 |
|
| 5 | +using CloudNative.CloudEvents.Core; |
5 | 6 | using CloudNative.CloudEvents.UnitTests;
|
6 | 7 | using System;
|
7 | 8 | using System.Collections.Generic;
|
|
15 | 16 | using System.Threading.Tasks;
|
16 | 17 | using Xunit;
|
17 | 18 | using static CloudNative.CloudEvents.UnitTests.TestHelpers;
|
| 19 | +using JArray = Newtonsoft.Json.Linq.JArray; |
18 | 20 | // JObject is a really handy way of creating JSON which we can then parse with System.Text.Json
|
19 | 21 | using JObject = Newtonsoft.Json.Linq.JObject;
|
20 |
| -using JArray = Newtonsoft.Json.Linq.JArray; |
21 |
| -using CloudNative.CloudEvents.Core; |
22 | 22 |
|
23 | 23 | namespace CloudNative.CloudEvents.SystemTextJson.UnitTests
|
24 | 24 | {
|
@@ -1102,6 +1102,53 @@ public void EncodeStructured_IndentationSettings()
|
1102 | 1102 | Assert.Equal(expected, json);
|
1103 | 1103 | }
|
1104 | 1104 |
|
| 1105 | + [Fact] |
| 1106 | + public void ConvertToJsonElement() |
| 1107 | + { |
| 1108 | + var cloudEvent = new CloudEvent |
| 1109 | + { |
| 1110 | + Data = SampleBinaryData |
| 1111 | + }.PopulateRequiredAttributes(); |
| 1112 | + |
| 1113 | + JsonElement element = new JsonEventFormatter().ConvertToJsonElement(cloudEvent); |
| 1114 | + var asserter = new JsonElementAsserter |
| 1115 | + { |
| 1116 | + { "data_base64", JsonValueKind.String, SampleBinaryDataBase64 }, |
| 1117 | + { "id", JsonValueKind.String, "test-id" }, |
| 1118 | + { "source", JsonValueKind.String, "//test" }, |
| 1119 | + { "specversion", JsonValueKind.String, "1.0" }, |
| 1120 | + { "type", JsonValueKind.String, "test-type" } |
| 1121 | + }; |
| 1122 | + asserter.AssertProperties(element, assertCount: true); |
| 1123 | + } |
| 1124 | + |
| 1125 | + [Fact] |
| 1126 | + public void ConvertFromJsonElement() |
| 1127 | + { |
| 1128 | + var obj = new JObject |
| 1129 | + { |
| 1130 | + ["specversion"] = "1.0", |
| 1131 | + ["type"] = "test-type", |
| 1132 | + ["id"] = "test-id", |
| 1133 | + ["data"] = "text", // Just so that it's reasonable to have a DataContentType, |
| 1134 | + ["datacontenttype"] = "text/plain", |
| 1135 | + ["dataschema"] = "https://data-schema", |
| 1136 | + ["subject"] = "event-subject", |
| 1137 | + ["source"] = "//event-source", |
| 1138 | + ["time"] = SampleTimestampText |
| 1139 | + }; |
| 1140 | + using var document = JsonDocument.Parse(obj.ToString()); |
| 1141 | + var cloudEvent = new JsonEventFormatter().ConvertFromJsonElement(document.RootElement, extensionAttributes: null); |
| 1142 | + Assert.Equal(CloudEventsSpecVersion.V1_0, cloudEvent.SpecVersion); |
| 1143 | + Assert.Equal("test-type", cloudEvent.Type); |
| 1144 | + Assert.Equal("test-id", cloudEvent.Id); |
| 1145 | + Assert.Equal("text/plain", cloudEvent.DataContentType); |
| 1146 | + Assert.Equal(new Uri("https://data-schema"), cloudEvent.DataSchema); |
| 1147 | + Assert.Equal("event-subject", cloudEvent.Subject); |
| 1148 | + Assert.Equal(new Uri("//event-source", UriKind.RelativeOrAbsolute), cloudEvent.Source); |
| 1149 | + AssertTimestampsEqual(SampleTimestamp, cloudEvent.Time); |
| 1150 | + } |
| 1151 | + |
1105 | 1152 | // Utility methods
|
1106 | 1153 | private static object DecodeBinaryModeEventData(byte[] bytes, string contentType)
|
1107 | 1154 | {
|
|
0 commit comments