From ccdd9bedd26ad58127f3515dc57be6130f71ea7a Mon Sep 17 00:00:00 2001 From: JoshLove-msft <54595583+JoshLove-msft@users.noreply.github.com> Date: Tue, 2 Feb 2021 14:05:37 -0800 Subject: [PATCH 1/5] Update API based on review feedback --- .../Azure.Messaging.EventGrid/README.md | 6 +- ...zure.Messaging.EventGrid.netstandard2.0.cs | 19 +- .../Sample3_ParseAndDeserializeEvents.md | 6 +- .../src/Customization/CloudEvent.cs | 138 +- .../src/Customization/EventGridEvent.cs | 99 +- .../tests/CloudEventTests.cs | 4 +- .../tests/ConsumeEventTests.cs | 1330 ++++++++--------- .../tests/EventGridClientLiveTests.cs | 2 +- .../Sample2_ParseAndDeserializeEvents.cs | 9 +- 9 files changed, 681 insertions(+), 932 deletions(-) diff --git a/sdk/eventgrid/Azure.Messaging.EventGrid/README.md b/sdk/eventgrid/Azure.Messaging.EventGrid/README.md index 24a8c353c40c..c21e3ce52f75 100644 --- a/sdk/eventgrid/Azure.Messaging.EventGrid/README.md +++ b/sdk/eventgrid/Azure.Messaging.EventGrid/README.md @@ -182,7 +182,7 @@ foreach (CloudEvent cloudEvent in cloudEvents) break; case "MyApp.Models.CustomEventType": // One can also specify a custom ObjectSerializer as needed to deserialize the payload correctly - TestPayload testPayload = await cloudEvent.GetDataAsync(myCustomSerializer); + TestPayload testPayload = cloudEvent.GetData(); Console.WriteLine(testPayload.Name); break; case SystemEventNames.StorageBlobDeleted: @@ -198,9 +198,9 @@ Below is an example using the `IsSystemEvent` property along with `AsSystemEvent foreach (EventGridEvent egEvent in egEvents) { // If the event is a system event, AsSystemEventData() should return the correct system event type - if (egEvent.IsSystemEvent) + if (egEvent.TryGetSystemEventData(out object systemEvent)) { - switch (egEvent.AsSystemEventData()) + switch (systemEvent) { case SubscriptionValidationEventData subscriptionValidated: Console.WriteLine(subscriptionValidated.ValidationCode); diff --git a/sdk/eventgrid/Azure.Messaging.EventGrid/api/Azure.Messaging.EventGrid.netstandard2.0.cs b/sdk/eventgrid/Azure.Messaging.EventGrid/api/Azure.Messaging.EventGrid.netstandard2.0.cs index 1c66e0dfc7f1..c34b2a046e6b 100644 --- a/sdk/eventgrid/Azure.Messaging.EventGrid/api/Azure.Messaging.EventGrid.netstandard2.0.cs +++ b/sdk/eventgrid/Azure.Messaging.EventGrid/api/Azure.Messaging.EventGrid.netstandard2.0.cs @@ -7,23 +7,20 @@ public static partial class BinaryDataExtensions } public partial class CloudEvent { - public CloudEvent(string source, string type, object data, string dataContentType = null, System.Type dataSerializationType = null) { } + public CloudEvent(string source, string type, object data, System.Type dataSerializationType = null) { } + public CloudEvent(string source, string type, System.ReadOnlyMemory data, string dataContentType) { } public string DataContentType { get { throw null; } set { } } public string DataSchema { get { throw null; } set { } } public System.Collections.Generic.Dictionary ExtensionAttributes { get { throw null; } } public string Id { get { throw null; } set { } } - public bool IsSystemEvent { get { throw null; } } public string Source { get { throw null; } set { } } public string Subject { get { throw null; } set { } } public System.DateTimeOffset? Time { get { throw null; } set { } } public string Type { get { throw null; } set { } } - public object AsSystemEventData() { throw null; } public System.BinaryData GetData() { throw null; } - public System.Threading.Tasks.Task GetDataAsync() { throw null; } - public System.Threading.Tasks.Task GetDataAsync(Azure.Core.Serialization.ObjectSerializer serializer = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; } - public T GetData(Azure.Core.Serialization.ObjectSerializer serializer = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; } - public static Azure.Messaging.EventGrid.CloudEvent[] Parse(System.BinaryData requestContent) { throw null; } + public T GetData(System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; } public static Azure.Messaging.EventGrid.CloudEvent[] Parse(string requestContent) { throw null; } + public bool TryGetSystemEvent(out object systemEvent) { throw null; } } public partial class EventGridEvent { @@ -32,16 +29,12 @@ public EventGridEvent(string subject, string eventType, string dataVersion, obje public System.DateTimeOffset EventTime { get { throw null; } set { } } public string EventType { get { throw null; } set { } } public string Id { get { throw null; } set { } } - public bool IsSystemEvent { get { throw null; } } public string Subject { get { throw null; } set { } } public string Topic { get { throw null; } set { } } - public object AsSystemEventData() { throw null; } public System.BinaryData GetData() { throw null; } - public System.Threading.Tasks.Task GetDataAsync() { throw null; } - public System.Threading.Tasks.Task GetDataAsync(Azure.Core.Serialization.ObjectSerializer serializer = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; } - public T GetData(Azure.Core.Serialization.ObjectSerializer serializer = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; } - public static Azure.Messaging.EventGrid.EventGridEvent[] Parse(System.BinaryData requestContent) { throw null; } + public T GetData(System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; } public static Azure.Messaging.EventGrid.EventGridEvent[] Parse(string requestContent) { throw null; } + public bool TryGetSystemEventData(out object eventData) { throw null; } } public partial class EventGridPublisherClient { diff --git a/sdk/eventgrid/Azure.Messaging.EventGrid/samples/Sample3_ParseAndDeserializeEvents.md b/sdk/eventgrid/Azure.Messaging.EventGrid/samples/Sample3_ParseAndDeserializeEvents.md index f64dcbe3d17b..536fa15a821e 100644 --- a/sdk/eventgrid/Azure.Messaging.EventGrid/samples/Sample3_ParseAndDeserializeEvents.md +++ b/sdk/eventgrid/Azure.Messaging.EventGrid/samples/Sample3_ParseAndDeserializeEvents.md @@ -40,7 +40,7 @@ foreach (CloudEvent cloudEvent in cloudEvents) break; case "MyApp.Models.CustomEventType": // One can also specify a custom ObjectSerializer as needed to deserialize the payload correctly - TestPayload testPayload = await cloudEvent.GetDataAsync(myCustomSerializer); + TestPayload testPayload = cloudEvent.GetData(); Console.WriteLine(testPayload.Name); break; case SystemEventNames.StorageBlobDeleted: @@ -59,9 +59,9 @@ If expecting mostly system events, it may be cleaner to switch on `AsSytemEventD foreach (EventGridEvent egEvent in egEvents) { // If the event is a system event, AsSystemEventData() should return the correct system event type - if (egEvent.IsSystemEvent) + if (egEvent.TryGetSystemEventData(out object systemEvent)) { - switch (egEvent.AsSystemEventData()) + switch (systemEvent) { case SubscriptionValidationEventData subscriptionValidated: Console.WriteLine(subscriptionValidated.ValidationCode); diff --git a/sdk/eventgrid/Azure.Messaging.EventGrid/src/Customization/CloudEvent.cs b/sdk/eventgrid/Azure.Messaging.EventGrid/src/Customization/CloudEvent.cs index e5b2c0425b89..645fc972b79e 100644 --- a/sdk/eventgrid/Azure.Messaging.EventGrid/src/Customization/CloudEvent.cs +++ b/sdk/eventgrid/Azure.Messaging.EventGrid/src/Customization/CloudEvent.cs @@ -4,7 +4,6 @@ using System; using System.Collections.Generic; using System.IO; -using System.Linq; using System.Text.Json; using System.Threading; using System.Threading.Tasks; @@ -18,40 +17,39 @@ namespace Azure.Messaging.EventGrid /// Properties of an event published to an Event Grid topic using the CloudEvent 1.0 Schema. public class CloudEvent { - /// Initializes a new instance of the class. - /// If the format and encoding of the data is not a JSON value, consider specifying the content type - /// of the payload in . For example, if passing in an XML payload, the - /// consumer can be informed by this attribute being set to "application/xml". - /// If the content type is omitted, then it is implied that the data is a JSON value conforming to the - /// "application/json" media type. + /// Initializes a new instance of the class with an object payload that will + /// be serialized as JSON. /// Identifies the context in which an event happened. The combination of id and source must be unique for each distinct event. /// Type of event related to the originating occurrence. For example, "Contoso.Items.ItemReceived". /// Event data specific to the event type. - /// Content type of the payload. A content type different from "application/json" should be specified if payload is not JSON. /// The type to use when serializing the data. /// If not specified, will be used on . - public CloudEvent(string source, string type, object data, string dataContentType = default, Type dataSerializationType = default) + public CloudEvent(string source, string type, object data, Type dataSerializationType = default) { Argument.AssertNotNull(source, nameof(source)); Argument.AssertNotNull(type, nameof(type)); Source = source; Type = type; - DataContentType = dataContentType; DataSerializationType = dataSerializationType ?? data?.GetType() ?? null; + Data = data; + ExtensionAttributes = new Dictionary(); + } - if (data is IEnumerable enumerable) - { - DataBase64 = enumerable.ToArray(); - } - else if (data is ReadOnlyMemory memory) - { - DataBase64 = memory.ToArray(); - } - else - { - Data = data; - } + /// Initializes a new instance of the class using binary event data. + /// Identifies the context in which an event happened. The combination of id and source must be unique for each distinct event. + /// Type of event related to the originating occurrence. For example, "Contoso.Items.ItemReceived". + /// Binary event data specific to the event type. + /// Content type of the payload. A content type different from "application/json" should be specified if payload is not JSON. + public CloudEvent(string source, string type, ReadOnlyMemory data, string dataContentType) + { + Argument.AssertNotNull(source, nameof(source)); + Argument.AssertNotNull(type, nameof(type)); + + Source = source; + Type = type; + DataContentType = dataContentType; + DataBase64 = data.ToArray(); ExtensionAttributes = new Dictionary(); } @@ -118,18 +116,17 @@ internal CloudEvent(CloudEventInternal cloudEventInternal) private static readonly JsonObjectSerializer s_jsonSerializer = new JsonObjectSerializer(); /// - /// Gets whether or not the event is a System defined event. - /// - public bool IsSystemEvent => - SystemEventExtensions.SystemEventDeserializers.ContainsKey(Type); - - /// - /// Given JSON-encoded events, parses the event envelope and returns an array of CloudEvents. + /// Gets whether or not the event is a System defined event and returns the deserialized + /// system event via out parameter. /// - /// The JSON-encoded representation of either a single event or an array or events, in the CloudEvent schema. - /// A list of . - public static CloudEvent[] Parse(BinaryData requestContent) - => Parse(requestContent.ToString()); + /// If the event is a system event, this will be populated + /// with the deserialized system event. Otherwise, this will be null. + /// Whether or not the event is a system event. + public bool TryGetSystemEvent(out object systemEvent) + { + systemEvent = SystemEventExtensions.AsSystemEventData(Type, SerializedData); + return systemEvent != null; + } /// /// Given JSON-encoded events, parses the event envelope and returns an array of CloudEvents. @@ -138,70 +135,39 @@ public static CloudEvent[] Parse(BinaryData requestContent) /// A list of . public static CloudEvent[] Parse(string requestContent) { - List cloudEventsInternal = new List(); - List cloudEvents = new List(); + Argument.AssertNotNull(requestContent, nameof(requestContent)); + + CloudEvent[] cloudEvents = null; JsonDocument requestDocument = JsonDocument.Parse(requestContent); // Parse JsonElement into separate events, deserialize event envelope properties if (requestDocument.RootElement.ValueKind == JsonValueKind.Object) { - cloudEventsInternal.Add(CloudEventInternal.DeserializeCloudEventInternal(requestDocument.RootElement)); + cloudEvents = new CloudEvent[1]; + cloudEvents[0] = (new CloudEvent(CloudEventInternal.DeserializeCloudEventInternal(requestDocument.RootElement))); } else if (requestDocument.RootElement.ValueKind == JsonValueKind.Array) { + cloudEvents = new CloudEvent[requestDocument.RootElement.GetArrayLength()]; + int i = 0; foreach (JsonElement property in requestDocument.RootElement.EnumerateArray()) { - cloudEventsInternal.Add(CloudEventInternal.DeserializeCloudEventInternal(property)); + cloudEvents[i++] = new CloudEvent(CloudEventInternal.DeserializeCloudEventInternal(property)); } } - - foreach (CloudEventInternal cloudEventInternal in cloudEventsInternal) - { - cloudEvents.Add(new CloudEvent(cloudEventInternal)); - } - - return cloudEvents.ToArray(); + return cloudEvents ?? Array.Empty(); } /// - /// Deserializes the event payload into a specified event type using the provided . + /// Deserializes the event payload into a specified event type using the default serializer, . /// /// Type of event to deserialize to. - /// A custom serializer used to deserialize the payload. If not provided, the - /// will be used. /// The cancellation token to use during deserialization. /// Event was not created from CloudEvent.Parse() method. /// Event payload cannot be cast to the specified event type. /// Deserialized payload of the event, cast to the specified type. - public async Task GetDataAsync(ObjectSerializer serializer = default, CancellationToken cancellationToken = default) - { - if (Data != null && serializer != null) - { - throw new InvalidOperationException("Cannot pass in a custom deserializer if event was not created from CloudEvent.Parse(), " + - "as event data should already be deserialized and the custom deserializer will not be used."); - } - return await GetDataInternal(serializer ?? s_jsonSerializer, true, cancellationToken).ConfigureAwait(false); - } - - /// - /// Deserializes the event payload into a specified event type using the provided . - /// - /// Type of event to deserialize to. - /// A custom serializer used to deserialize the payload. If not provided, the - /// will be used. - /// The cancellation token to use during deserialization. - /// Event was not created from CloudEvent.Parse() method. - /// Event payload cannot be cast to the specified event type. - /// Deserialized payload of the event, cast to the specified type. - public T GetData(ObjectSerializer serializer = default, CancellationToken cancellationToken = default) - { - if (Data != null && serializer != null) - { - throw new InvalidOperationException("Cannot pass in a custom deserializer if event was not created from CloudEvent.Parse(), " + - "as event data should already be deserialized and the custom deserializer will not be used."); - } - return GetDataInternal(serializer ?? s_jsonSerializer, false, cancellationToken).EnsureCompleted(); - } + public T GetData(CancellationToken cancellationToken = default) => + GetDataInternal(s_jsonSerializer, false, cancellationToken).EnsureCompleted(); private async Task GetDataInternal(ObjectSerializer serializer, bool async, CancellationToken cancellationToken = default) { @@ -251,18 +217,6 @@ private async Task GetDataInternal(ObjectSerializer serializer, bool async public BinaryData GetData() => GetDataInternal(); - /// - /// Deserializes the event payload into a system event type or - /// returns the payload of the event wrapped as . Using BinaryData, - /// one can deserialize the payload into rich data, or access the raw JSON data using . - /// - /// - /// Deserialized payload of the event. Returns null if there is no event data. - /// Returns for unknown event types. - /// - public Task GetDataAsync() => - Task.FromResult(GetDataInternal()); - private BinaryData GetDataInternal() { if (Data != null) @@ -299,13 +253,5 @@ private static MemoryStream SerializePayloadToStream(JsonElement payload, Cancel dataStream.Position = 0; return dataStream; } - - /// - /// Deserializes a system event to its system event data payload. This will return null if the event is not a system event. - /// To detect whether an event is a system event, use the property. - /// - /// The rich system model type. - public object AsSystemEventData() => - SystemEventExtensions.AsSystemEventData(Type, SerializedData); } } diff --git a/sdk/eventgrid/Azure.Messaging.EventGrid/src/Customization/EventGridEvent.cs b/sdk/eventgrid/Azure.Messaging.EventGrid/src/Customization/EventGridEvent.cs index 0993ffc40a96..04a8bd52bcf2 100644 --- a/sdk/eventgrid/Azure.Messaging.EventGrid/src/Customization/EventGridEvent.cs +++ b/sdk/eventgrid/Azure.Messaging.EventGrid/src/Customization/EventGridEvent.cs @@ -84,90 +84,59 @@ internal EventGridEvent(EventGridEventInternal eventGridEventInternal) private static readonly JsonObjectSerializer s_jsonSerializer = new JsonObjectSerializer(); /// - /// Gets whether or not the event is a System defined event. + /// Gets whether or not the event is a System defined event and returns the deserialized + /// system event via out parameter. /// - public bool IsSystemEvent => - SystemEventExtensions.SystemEventDeserializers.ContainsKey(EventType); - - /// - /// Given JSON-encoded events, parses the event envelope and returns an array of EventGridEvents. - /// - /// The JSON-encoded representation of either a single event or an array or events, encoded in the EventGridEvent schema. - /// A list of . - public static EventGridEvent[] Parse(BinaryData requestContent) - => Parse(requestContent.ToString()); + /// If the event is a system event, this will be populated + /// with the deserialized system event. Otherwise, this will be null. + /// Whether or not the event is a system event. + public bool TryGetSystemEventData(out object eventData) + { + eventData = SystemEventExtensions.AsSystemEventData(EventType, SerializedData); + return eventData != null; + } /// /// Given JSON-encoded events, parses the event envelope and returns an array of EventGridEvents. /// - /// The JSON-encoded representation of either a single event or an array or events, encoded in the EventGridEvent schema. + /// The JSON-encoded representation of either a single event or an array or events, + /// encoded in the EventGridEvent schema. /// A list of . public static EventGridEvent[] Parse(string requestContent) { - List egEventsInternal = new List(); - List egEvents = new List(); + Argument.AssertNotNull(requestContent, nameof(requestContent)); + + EventGridEvent[] egEvents = null; JsonDocument requestDocument = JsonDocument.Parse(requestContent); // Parse JsonElement into separate events, deserialize event envelope properties if (requestDocument.RootElement.ValueKind == JsonValueKind.Object) { - egEventsInternal.Add(EventGridEventInternal.DeserializeEventGridEventInternal(requestDocument.RootElement)); + egEvents = new EventGridEvent[1]; + egEvents[0] = (new EventGridEvent(EventGridEventInternal.DeserializeEventGridEventInternal(requestDocument.RootElement))); } else if (requestDocument.RootElement.ValueKind == JsonValueKind.Array) { + egEvents = new EventGridEvent[requestDocument.RootElement.GetArrayLength()]; + int i = 0; foreach (JsonElement property in requestDocument.RootElement.EnumerateArray()) { - egEventsInternal.Add(EventGridEventInternal.DeserializeEventGridEventInternal(property)); + egEvents[i++] = new EventGridEvent(EventGridEventInternal.DeserializeEventGridEventInternal(property)); } } - - foreach (EventGridEventInternal egEventInternal in egEventsInternal) - { - egEvents.Add(new EventGridEvent(egEventInternal)); - } - - return egEvents.ToArray(); + return egEvents ?? Array.Empty(); } /// - /// Deserializes the event payload into a specified event type using the provided . + /// Deserializes the event payload into a specified event type using the default serializer, . /// /// Type of event to deserialize to. - /// A custom serializer used to deserialize the payload. If not provided, the - /// will be used. /// The cancellation token to use during deserialization. /// Event was not created from EventGridEvent.Parse() method. /// Event payload cannot be cast to the specified event type. /// Deserialized payload of the event, cast to the specified type. - public async Task GetDataAsync(ObjectSerializer serializer = default, CancellationToken cancellationToken = default) - { - if (Data != null && serializer != null) - { - throw new InvalidOperationException("Cannot pass in a custom deserializer if event was not created from EventGridEvent.Parse(), " + - "as event data should already be deserialized and the custom deserializer will not be used."); - } - return await GetDataInternal(serializer ?? s_jsonSerializer, true, cancellationToken).ConfigureAwait(false); - } - - /// - /// Deserializes the event payload into a specified event type using the provided . - /// - /// Type of event to deserialize to. - /// A custom serializer used to deserialize the payload. If not provided, the - /// will be used. - /// The cancellation token to use during deserialization. - /// Event was not created from EventGridEvent.Parse() method. - /// Event payload cannot be cast to the specified event type. - /// Deserialized payload of the event, cast to the specified type. - public T GetData(ObjectSerializer serializer = default, CancellationToken cancellationToken = default) - { - if (Data != null && serializer != null) - { - throw new InvalidOperationException("Cannot pass in a custom deserializer if event was not created from EventGridEvent.Parse(), " + - "as event data should already be deserialized and the custom deserializer will not be used."); - } - return GetDataInternal(serializer ?? s_jsonSerializer, false, cancellationToken).EnsureCompleted(); - } + public T GetData(CancellationToken cancellationToken = default) => + GetDataInternal(s_jsonSerializer, false, cancellationToken).EnsureCompleted(); private async Task GetDataInternal(ObjectSerializer serializer, bool async, CancellationToken cancellationToken = default) { @@ -209,18 +178,6 @@ private async Task GetDataInternal(ObjectSerializer serializer, bool async public BinaryData GetData() => GetDataInternal(); - /// - /// Deserializes the event payload into a system event type or - /// returns the payload of the event wrapped as . Using BinaryData, - /// one can deserialize the payload into rich data, or access the raw JSON data using . - /// - /// - /// Deserialized payload of the event. - /// Returns for unknown event types. - /// - public Task GetDataAsync() => - Task.FromResult(GetDataInternal()); - private BinaryData GetDataInternal() { if (Data != null) @@ -240,13 +197,5 @@ private static MemoryStream SerializePayloadToStream(JsonElement payload, Cancel dataStream.Position = 0; return dataStream; } - - /// - /// Deserializes a system event to its system event data payload. This will return null if the event is not a system event. - /// To detect whether an event is a system event, use the property. - /// - /// The rich system model type. - public object AsSystemEventData() => - SystemEventExtensions.AsSystemEventData(EventType, SerializedData); } } diff --git a/sdk/eventgrid/Azure.Messaging.EventGrid/tests/CloudEventTests.cs b/sdk/eventgrid/Azure.Messaging.EventGrid/tests/CloudEventTests.cs index 79ffaee252af..33d4d6ff6d7a 100644 --- a/sdk/eventgrid/Azure.Messaging.EventGrid/tests/CloudEventTests.cs +++ b/sdk/eventgrid/Azure.Messaging.EventGrid/tests/CloudEventTests.cs @@ -127,7 +127,6 @@ public async Task SerializesExpectedProperties_BaseType() Age = 10, DerivedProperty = 5 }, - "TestPayload", typeof(TestPayload)); // since the data has not yet been serialized (CloudEvent not constructed from Parse method), GetData returns the passed in instance. @@ -169,8 +168,7 @@ public async Task SerializesExpectedProperties_DerivedType() Name = "name", Age = 10, DerivedProperty = 5 - }, - "TestPayload"); + }); Assert.AreEqual(5, cloudEvent.GetData().DerivedProperty); Assert.AreEqual(5, cloudEvent.GetData().ToObjectFromJson().DerivedProperty); diff --git a/sdk/eventgrid/Azure.Messaging.EventGrid/tests/ConsumeEventTests.cs b/sdk/eventgrid/Azure.Messaging.EventGrid/tests/ConsumeEventTests.cs index 8ca70d610b02..9d58a14fbcf0 100644 --- a/sdk/eventgrid/Azure.Messaging.EventGrid/tests/ConsumeEventTests.cs +++ b/sdk/eventgrid/Azure.Messaging.EventGrid/tests/ConsumeEventTests.cs @@ -44,7 +44,7 @@ public void ConsumeStorageBlobDeletedEventWithExtraProperty() switch (egEvent.EventType) { case SystemEventNames.StorageBlobDeleted: - StorageBlobDeletedEventData blobDeleted = (StorageBlobDeletedEventData)egEvent.AsSystemEventData(); + StorageBlobDeletedEventData blobDeleted = egEvent.GetData(); Assert.AreEqual("https://example.blob.core.windows.net/testcontainer/testfile.txt", blobDeleted.Url); Assert.AreEqual("/subscriptions/id/resourceGroups/Storage/providers/Microsoft.Storage/storageAccounts/xstoretestaccount", egEvent.Topic); break; @@ -65,7 +65,7 @@ public void ConsumeEventNotWrappedInAnArray() switch (egEvent.EventType) { case SystemEventNames.StorageBlobDeleted: - StorageBlobDeletedEventData blobDeleted = (StorageBlobDeletedEventData)egEvent.AsSystemEventData(); + StorageBlobDeletedEventData blobDeleted = egEvent.GetData(); Assert.AreEqual("https://example.blob.core.windows.net/testcontainer/testfile.txt", blobDeleted.Url); break; } @@ -89,11 +89,11 @@ public void ConsumeMultipleEventsInSameBatch() switch (egEvent.EventType) { case SystemEventNames.StorageBlobCreated: - StorageBlobCreatedEventData blobCreated = (StorageBlobCreatedEventData)egEvent.AsSystemEventData(); + StorageBlobCreatedEventData blobCreated = egEvent.GetData(); Assert.AreEqual("https://myaccount.blob.core.windows.net/testcontainer/file1.txt", blobCreated.Url); break; case SystemEventNames.StorageBlobDeleted: - StorageBlobDeletedEventData blobDeleted = (StorageBlobDeletedEventData)egEvent.AsSystemEventData(); + StorageBlobDeletedEventData blobDeleted = egEvent.GetData(); Assert.AreEqual("https://example.blob.core.windows.net/testcontainer/testfile.txt", blobDeleted.Url); break; } @@ -111,48 +111,33 @@ public void ConsumeEventUsingBinaryDataExtensionMethod() switch (egEvent.EventType) { case SystemEventNames.StorageBlobDeleted: - StorageBlobDeletedEventData blobDeleted = (StorageBlobDeletedEventData)egEvent.AsSystemEventData(); + StorageBlobDeletedEventData blobDeleted = egEvent.GetData(); Assert.AreEqual("https://example.blob.core.windows.net/testcontainer/testfile.txt", blobDeleted.Url); break; } } [Test] - public void EGEventAsSystemEventDataThrowsWhenCalledWithoutParse() + public void EGEventParseThrowsIfMissingRequiredProperties() { - var customSerializer = new JsonObjectSerializer( - new JsonSerializerOptions() - { - PropertyNamingPolicy = JsonNamingPolicy.CamelCase - }); - - EventGridEvent egEvent = new EventGridEvent( - "/contoso/items", - "Contoso.Items.ItemReceived", - "1", - new ContosoItemReceivedEventData() - { - ItemSku = "512d38b6-c7b8-40c8-89fe-f46f9e9622b6" - }); - Assert.That(() => egEvent.GetData(customSerializer), - Throws.InstanceOf()); + // missing Id and DataVersion + string requestContent = "[{ \"subject\": \"\", \"data\": { \"itemSku\": \"512d38b6-c7b8-40c8-89fe-f46f9e9622b6\", \"itemUri\": \"https://rp-eastus2.eventgrid.azure.net:553/eventsubscriptions/estest/validate?id=B2E34264-7D71-453A-B5FB-B62D0FDC85EE&t=2018-04-26T20:30:54.4538837Z&apiVersion=2018-05-01-preview&token=1BNqCxBBSSE9OnNSfZM4%2b5H9zDegKMY6uJ%2fO2DFRkwQ%3d\" }, \"eventType\": \"Contoso.Items.ItemReceived\", \"eventTime\": \"2018-01-25T22:12:19.4556811Z\", \"metadataVersion\": \"1\"}]"; - ContosoItemReceivedEventData eventData1 = egEvent.GetData(); - Assert.AreEqual("512d38b6-c7b8-40c8-89fe-f46f9e9622b6", eventData1.ItemSku); - if (egEvent.GetData() is BinaryData binaryEventData) - { - ContosoItemReceivedEventData eventData2 = binaryEventData.ToObjectFromJson(); - Assert.AreEqual("512d38b6-c7b8-40c8-89fe-f46f9e9622b6", eventData1.ItemSku); - } + Assert.That(() => EventGridEvent.Parse(requestContent), + Throws.InstanceOf()); } [Test] - public void EGEventParseThrowsIfMissingRequiredProperties() + public void EGEventParseThrowsOnNullInput() { - // missing Id and DataVersion - string requestContent = "[{ \"subject\": \"\", \"data\": { \"itemSku\": \"512d38b6-c7b8-40c8-89fe-f46f9e9622b6\", \"itemUri\": \"https://rp-eastus2.eventgrid.azure.net:553/eventsubscriptions/estest/validate?id=B2E34264-7D71-453A-B5FB-B62D0FDC85EE&t=2018-04-26T20:30:54.4538837Z&apiVersion=2018-05-01-preview&token=1BNqCxBBSSE9OnNSfZM4%2b5H9zDegKMY6uJ%2fO2DFRkwQ%3d\" }, \"eventType\": \"Contoso.Items.ItemReceived\", \"eventTime\": \"2018-01-25T22:12:19.4556811Z\", \"metadataVersion\": \"1\"}]"; + Assert.That(() => EventGridEvent.Parse(null), + Throws.InstanceOf()); + } - Assert.That(() => EventGridEvent.Parse(requestContent), + [Test] + public void CloudEventParseThrowsOnNullInput() + { + Assert.That(() => CloudEvent.Parse(null), Throws.InstanceOf()); } #endregion @@ -171,7 +156,7 @@ public void ConsumeCustomEvents() { if (egEvent.EventType == "Contoso.Items.ItemReceived") { - ContosoItemReceivedEventData eventData = egEvent.GetData(new JsonObjectSerializer( + ContosoItemReceivedEventData eventData = egEvent.GetData().ToObject(new JsonObjectSerializer( new JsonSerializerOptions() { PropertyNamingPolicy = JsonNamingPolicy.CamelCase @@ -194,7 +179,7 @@ public void ConsumeCustomEventWithArrayData() { if (egEvent.EventType == "Contoso.Items.ItemReceived") { - ContosoItemReceivedEventData[] eventData = egEvent.GetData(new JsonObjectSerializer( + ContosoItemReceivedEventData[] eventData = egEvent.GetData().ToObject(new JsonObjectSerializer( new JsonSerializerOptions() { PropertyNamingPolicy = JsonNamingPolicy.CamelCase @@ -253,9 +238,8 @@ public void ConsumeAppConfigurationKeyValueDeletedEvent() EventGridEvent[] events = EventGridEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].AsSystemEventData() is AppConfigurationKeyValueDeletedEventData); - AppConfigurationKeyValueDeletedEventData eventData = (AppConfigurationKeyValueDeletedEventData)events[0].AsSystemEventData(); - Assert.AreEqual("key1", eventData.Key); + Assert.True(events[0].TryGetSystemEventData(out object eventData)); + Assert.AreEqual("key1", (eventData as AppConfigurationKeyValueDeletedEventData).Key); } [Test] @@ -265,9 +249,8 @@ public void ConsumeAppConfigurationKeyValueModifiedEvent() EventGridEvent[] events = EventGridEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].AsSystemEventData() is AppConfigurationKeyValueModifiedEventData); - AppConfigurationKeyValueModifiedEventData eventData = (AppConfigurationKeyValueModifiedEventData)events[0].AsSystemEventData(); - Assert.AreEqual("key1", eventData.Key); + Assert.True(events[0].TryGetSystemEventData(out object eventData)); + Assert.AreEqual("key1", (eventData as AppConfigurationKeyValueModifiedEventData).Key); } #endregion @@ -280,9 +263,8 @@ public void ConsumeContainerRegistryImagePushedEvent() EventGridEvent[] events = EventGridEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].AsSystemEventData() is ContainerRegistryImagePushedEventData); - ContainerRegistryImagePushedEventData eventData = (ContainerRegistryImagePushedEventData)events[0].AsSystemEventData(); - Assert.AreEqual("127.0.0.1", eventData.Request.Addr); + Assert.True(events[0].TryGetSystemEventData(out object eventData)); + Assert.AreEqual("127.0.0.1", (eventData as ContainerRegistryImagePushedEventData).Request.Addr); } [Test] @@ -293,9 +275,8 @@ public void ConsumeContainerRegistryImageDeletedEvent() EventGridEvent[] events = EventGridEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].AsSystemEventData() is ContainerRegistryImageDeletedEventData); - ContainerRegistryImageDeletedEventData eventData = (ContainerRegistryImageDeletedEventData)events[0].AsSystemEventData(); - Assert.AreEqual("testactor", eventData.Actor.Name); + Assert.True(events[0].TryGetSystemEventData(out object eventData)); + Assert.AreEqual("testactor", (eventData as ContainerRegistryImageDeletedEventData).Actor.Name); } [Test] @@ -306,9 +287,8 @@ public void ConsumeContainerRegistryChartDeletedEvent() EventGridEvent[] events = EventGridEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].AsSystemEventData() is ContainerRegistryChartDeletedEventData); - ContainerRegistryChartDeletedEventData eventData = (ContainerRegistryChartDeletedEventData)events[0].AsSystemEventData(); - Assert.AreEqual("mediatype1", eventData.Target.MediaType); + Assert.True(events[0].TryGetSystemEventData(out object eventData)); + Assert.AreEqual("mediatype1", (eventData as ContainerRegistryChartDeletedEventData).Target.MediaType); } [Test] @@ -319,9 +299,8 @@ public void ConsumeContainerRegistryChartPushedEvent() EventGridEvent[] events = EventGridEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].AsSystemEventData() is ContainerRegistryChartPushedEventData); - ContainerRegistryChartPushedEventData eventData = (ContainerRegistryChartPushedEventData)events[0].AsSystemEventData(); - Assert.AreEqual("mediatype1", eventData.Target.MediaType); + Assert.True(events[0].TryGetSystemEventData(out object eventData)); + Assert.AreEqual("mediatype1", (eventData as ContainerRegistryChartPushedEventData).Target.MediaType); } #endregion @@ -334,9 +313,8 @@ public void ConsumeIoTHubDeviceCreatedEvent() EventGridEvent[] events = EventGridEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].AsSystemEventData() is IotHubDeviceCreatedEventData); - IotHubDeviceCreatedEventData eventData = (IotHubDeviceCreatedEventData)events[0].AsSystemEventData(); - Assert.AreEqual("enabled", eventData.Twin.Status); + Assert.True(events[0].TryGetSystemEventData(out object eventData)); + Assert.AreEqual("enabled", (eventData as IotHubDeviceCreatedEventData).Twin.Status); } [Test] @@ -347,9 +325,8 @@ public void ConsumeIoTHubDeviceDeletedEvent() EventGridEvent[] events = EventGridEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].AsSystemEventData() is IotHubDeviceDeletedEventData); - IotHubDeviceDeletedEventData eventData = (IotHubDeviceDeletedEventData)events[0].AsSystemEventData(); - Assert.AreEqual("AAAAAAAAAAI=", eventData.Twin.Etag); + Assert.True(events[0].TryGetSystemEventData(out object eventData)); + Assert.AreEqual("AAAAAAAAAAI=", (eventData as IotHubDeviceDeletedEventData).Twin.Etag); } [Test] @@ -360,9 +337,8 @@ public void ConsumeIoTHubDeviceConnectedEvent() EventGridEvent[] events = EventGridEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].AsSystemEventData() is IotHubDeviceConnectedEventData); - IotHubDeviceConnectedEventData eventData = (IotHubDeviceConnectedEventData)events[0].AsSystemEventData(); - Assert.AreEqual("EGTESTHUB1", eventData.HubName); + Assert.True(events[0].TryGetSystemEventData(out object eventData)); + Assert.AreEqual("EGTESTHUB1", (eventData as IotHubDeviceConnectedEventData).HubName); } [Test] @@ -373,9 +349,8 @@ public void ConsumeIoTHubDeviceDisconnectedEvent() EventGridEvent[] events = EventGridEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].AsSystemEventData() is IotHubDeviceDisconnectedEventData); - IotHubDeviceDisconnectedEventData eventData = (IotHubDeviceDisconnectedEventData)events[0].AsSystemEventData(); - Assert.AreEqual("000000000000000001D4132452F67CE200000002000000000000000000000002", eventData.DeviceConnectionStateEventInfo.SequenceNumber); + Assert.True(events[0].TryGetSystemEventData(out object eventData)); + Assert.AreEqual("000000000000000001D4132452F67CE200000002000000000000000000000002", (eventData as IotHubDeviceDisconnectedEventData).DeviceConnectionStateEventInfo.SequenceNumber); } [Test] @@ -386,9 +361,8 @@ public void ConsumeIoTHubDeviceTelemetryEvent() EventGridEvent[] events = EventGridEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].AsSystemEventData() is IotHubDeviceTelemetryEventData); - IotHubDeviceTelemetryEventData eventData = (IotHubDeviceTelemetryEventData)events[0].AsSystemEventData(); - Assert.AreEqual("Active", eventData.Properties["Status"]); + Assert.True(events[0].TryGetSystemEventData(out object eventData)); + Assert.AreEqual("Active", (eventData as IotHubDeviceTelemetryEventData).Properties["Status"]); } #endregion @@ -406,10 +380,9 @@ public void ConsumeEventGridSubscriptionValidationEvent() Assert.AreEqual("mySubject", egEvent.Subject); Assert.NotNull(events); - Assert.True(events[0].AsSystemEventData() is SubscriptionValidationEventData); + Assert.True(events[0].TryGetSystemEventData(out object eventData)); Assert.True(events[0].GetData() is SubscriptionValidationEventData); - SubscriptionValidationEventData eventData = (SubscriptionValidationEventData)events[0].AsSystemEventData(); - Assert.AreEqual("512d38b6-c7b8-40c8-89fe-f46f9e9622b6", eventData.ValidationCode); + Assert.AreEqual("512d38b6-c7b8-40c8-89fe-f46f9e9622b6", (eventData as SubscriptionValidationEventData).ValidationCode); } [Test] @@ -420,9 +393,10 @@ public void ConsumeEventGridSubscriptionDeletedEvent() EventGridEvent[] events = EventGridEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].AsSystemEventData() is SubscriptionDeletedEventData); - SubscriptionDeletedEventData eventData = (SubscriptionDeletedEventData)events[0].AsSystemEventData(); - Assert.AreEqual("/subscriptions/id/resourceGroups/rg/providers/Microsoft.EventGrid/topics/topic1/providers/Microsoft.EventGrid/eventSubscriptions/eventsubscription1", eventData.EventSubscriptionId); + Assert.True(events[0].TryGetSystemEventData(out object eventData)); + Assert.AreEqual( + "/subscriptions/id/resourceGroups/rg/providers/Microsoft.EventGrid/topics/topic1/providers/Microsoft.EventGrid/eventSubscriptions/eventsubscription1", + (eventData as SubscriptionDeletedEventData).EventSubscriptionId); } #endregion @@ -435,9 +409,8 @@ public void ConsumeEventHubCaptureFileCreatedEvent() EventGridEvent[] events = EventGridEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].AsSystemEventData() is EventHubCaptureFileCreatedEventData); - EventHubCaptureFileCreatedEventData eventData = (EventHubCaptureFileCreatedEventData)events[0].AsSystemEventData(); - Assert.AreEqual("AzureBlockBlob", eventData.FileType); + Assert.True(events[0].TryGetSystemEventData(out object eventData)); + Assert.AreEqual("AzureBlockBlob", (eventData as EventHubCaptureFileCreatedEventData).FileType); } #endregion @@ -449,17 +422,17 @@ public void ConsumeMachineLearningServicesModelRegisteredEvent() EventGridEvent[] events = EventGridEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].AsSystemEventData() is MachineLearningServicesModelRegisteredEventData); - MachineLearningServicesModelRegisteredEventData eventData = (MachineLearningServicesModelRegisteredEventData)events[0].AsSystemEventData(); - Assert.AreEqual("sklearn_regression_model", eventData.ModelName); - Assert.AreEqual("3", eventData.ModelVersion); + Assert.True(events[0].TryGetSystemEventData(out object eventData)); + var sysData = eventData as MachineLearningServicesModelRegisteredEventData; + Assert.AreEqual("sklearn_regression_model", sysData.ModelName); + Assert.AreEqual("3", sysData.ModelVersion); - Assert.True(eventData.ModelTags is IDictionary); - IDictionary tags = (IDictionary)eventData.ModelTags; + Assert.True(sysData.ModelTags is IDictionary); + IDictionary tags = (IDictionary)sysData.ModelTags; Assert.AreEqual("regression", tags["type"]); - Assert.True(eventData.ModelProperties is IDictionary); - IDictionary properties = (IDictionary)eventData.ModelProperties; + Assert.True(sysData.ModelProperties is IDictionary); + IDictionary properties = (IDictionary)sysData.ModelProperties; Assert.AreEqual("test", properties["area"]); } @@ -470,10 +443,9 @@ public void ConsumeMachineLearningServicesModelDeployedEvent() EventGridEvent[] events = EventGridEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].AsSystemEventData() is MachineLearningServicesModelDeployedEventData); - MachineLearningServicesModelDeployedEventData eventData = (MachineLearningServicesModelDeployedEventData)events[0].AsSystemEventData(); - Assert.AreEqual("aciservice1", eventData.ServiceName); - Assert.AreEqual(2, eventData.ModelIds.Split(',').Length); + Assert.True(events[0].TryGetSystemEventData(out object eventData)); + Assert.AreEqual("aciservice1", (eventData as MachineLearningServicesModelDeployedEventData).ServiceName); + Assert.AreEqual(2, (eventData as MachineLearningServicesModelDeployedEventData).ModelIds.Split(',').Length); } [Test] @@ -483,10 +455,9 @@ public void ConsumeMachineLearningServicesRunCompletedEvent() EventGridEvent[] events = EventGridEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].AsSystemEventData() is MachineLearningServicesRunCompletedEventData); - MachineLearningServicesRunCompletedEventData eventData = (MachineLearningServicesRunCompletedEventData)events[0].AsSystemEventData(); - Assert.AreEqual("AutoML_ad912b2d-6467-4f32-a616-dbe4af6dd8fc", eventData.RunId); - Assert.AreEqual("automl-local-regression", eventData.ExperimentName); + Assert.True(events[0].TryGetSystemEventData(out object eventData)); + Assert.AreEqual("AutoML_ad912b2d-6467-4f32-a616-dbe4af6dd8fc", (eventData as MachineLearningServicesRunCompletedEventData).RunId); + Assert.AreEqual("automl-local-regression", (eventData as MachineLearningServicesRunCompletedEventData).ExperimentName); } [Test] @@ -496,12 +467,11 @@ public void ConsumeMachineLearningServicesRunStatusChangedEvent() EventGridEvent[] events = EventGridEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].AsSystemEventData() is MachineLearningServicesRunStatusChangedEventData); - MachineLearningServicesRunStatusChangedEventData eventData = (MachineLearningServicesRunStatusChangedEventData)events[0].AsSystemEventData(); - Assert.AreEqual("AutoML_ad912b2d-6467-4f32-a616-dbe4af6dd8fc", eventData.RunId); - Assert.AreEqual("automl-local-regression", eventData.ExperimentName); - Assert.AreEqual("Running", eventData.RunStatus); - Assert.AreEqual("automl", eventData.RunType); + Assert.True(events[0].TryGetSystemEventData(out object eventData)); + Assert.AreEqual("AutoML_ad912b2d-6467-4f32-a616-dbe4af6dd8fc", (eventData as MachineLearningServicesRunStatusChangedEventData).RunId); + Assert.AreEqual("automl-local-regression", (eventData as MachineLearningServicesRunStatusChangedEventData).ExperimentName); + Assert.AreEqual("Running", (eventData as MachineLearningServicesRunStatusChangedEventData).RunStatus); + Assert.AreEqual("automl", (eventData as MachineLearningServicesRunStatusChangedEventData).RunType); } [Test] @@ -511,9 +481,8 @@ public void ConsumeMachineLearningServicesDatasetDriftDetectedEvent() EventGridEvent[] events = EventGridEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].AsSystemEventData() is MachineLearningServicesDatasetDriftDetectedEventData); - MachineLearningServicesDatasetDriftDetectedEventData eventData = (MachineLearningServicesDatasetDriftDetectedEventData)events[0].AsSystemEventData(); - Assert.AreEqual("copetersDriftMonitor3", eventData.DataDriftName); + Assert.True(events[0].TryGetSystemEventData(out object eventData)); + Assert.AreEqual("copetersDriftMonitor3", (eventData as MachineLearningServicesDatasetDriftDetectedEventData).DataDriftName); } #endregion @@ -525,9 +494,8 @@ public void ConsumeMapsGeofenceEnteredEvent() EventGridEvent[] events = EventGridEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].AsSystemEventData() is MapsGeofenceEnteredEventData); - MapsGeofenceEnteredEventData eventData = (MapsGeofenceEnteredEventData)events[0].AsSystemEventData(); - Assert.AreEqual(1.0, eventData.Geometries[0].Distance); + Assert.True(events[0].TryGetSystemEventData(out object eventData)); + Assert.AreEqual(1.0, (eventData as MapsGeofenceEnteredEventData).Geometries[0].Distance); } [Test] @@ -537,9 +505,8 @@ public void ConsumeMapsGeofenceExitedEvent() EventGridEvent[] events = EventGridEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].AsSystemEventData() is MapsGeofenceExitedEventData); - MapsGeofenceExitedEventData eventData = (MapsGeofenceExitedEventData)events[0].AsSystemEventData(); - Assert.AreEqual(1.0, eventData.Geometries[0].Distance); + Assert.True(events[0].TryGetSystemEventData(out object eventData)); + Assert.AreEqual(1.0, (eventData as MapsGeofenceExitedEventData).Geometries[0].Distance); } [Test] @@ -549,9 +516,8 @@ public void ConsumeMapsGeofenceResultEvent() EventGridEvent[] events = EventGridEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].AsSystemEventData() is MapsGeofenceResultEventData); - MapsGeofenceResultEventData eventData = (MapsGeofenceResultEventData)events[0].AsSystemEventData(); - Assert.AreEqual(1.0, eventData.Geometries[0].Distance); + Assert.True(events[0].TryGetSystemEventData(out object eventData)); + Assert.AreEqual(1.0, (eventData as MapsGeofenceResultEventData).Geometries[0].Distance); } #endregion @@ -563,10 +529,9 @@ public void ConsumeMediaMediaJobStateChangeEvent() EventGridEvent[] events = EventGridEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].AsSystemEventData() is MediaJobStateChangeEventData); - MediaJobStateChangeEventData eventData = (MediaJobStateChangeEventData)events[0].AsSystemEventData(); - Assert.AreEqual(MediaJobState.Scheduled, eventData.PreviousState); - Assert.AreEqual(MediaJobState.Processing, eventData.State); + Assert.True(events[0].TryGetSystemEventData(out object eventData)); + Assert.AreEqual(MediaJobState.Scheduled, (eventData as MediaJobStateChangeEventData).PreviousState); + Assert.AreEqual(MediaJobState.Processing, (eventData as MediaJobStateChangeEventData).State); } [Test] @@ -577,12 +542,11 @@ public void ConsumeMediaJobOutputStateChangeEvent() EventGridEvent[] events = EventGridEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].AsSystemEventData() is MediaJobOutputStateChangeEventData); - MediaJobOutputStateChangeEventData eventData = (MediaJobOutputStateChangeEventData)events[0].AsSystemEventData(); - Assert.AreEqual(MediaJobState.Scheduled, eventData.PreviousState); - Assert.AreEqual(MediaJobState.Processing, eventData.Output.State); - Assert.True(eventData.Output is MediaJobOutputAsset); - MediaJobOutputAsset outputAsset = (MediaJobOutputAsset)eventData.Output; + Assert.True(events[0].TryGetSystemEventData(out object eventData)); + Assert.AreEqual(MediaJobState.Scheduled, (eventData as MediaJobOutputStateChangeEventData).PreviousState); + Assert.AreEqual(MediaJobState.Processing, (eventData as MediaJobOutputStateChangeEventData).Output.State); + Assert.True((eventData as MediaJobOutputStateChangeEventData).Output is MediaJobOutputAsset); + MediaJobOutputAsset outputAsset = (MediaJobOutputAsset)(eventData as MediaJobOutputStateChangeEventData).Output; Assert.AreEqual("output-2ac2fe75-6557-4de5-ab25-5713b74a6901", outputAsset.AssetName); } @@ -594,10 +558,9 @@ public void ConsumeMediaJobScheduledEvent() EventGridEvent[] events = EventGridEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].AsSystemEventData() is MediaJobScheduledEventData); - MediaJobScheduledEventData eventData = (MediaJobScheduledEventData)events[0].AsSystemEventData(); - Assert.AreEqual(MediaJobState.Queued, eventData.PreviousState); - Assert.AreEqual(MediaJobState.Scheduled, eventData.State); + Assert.True(events[0].TryGetSystemEventData(out object eventData)); + Assert.AreEqual(MediaJobState.Queued, (eventData as MediaJobScheduledEventData).PreviousState); + Assert.AreEqual(MediaJobState.Scheduled, (eventData as MediaJobScheduledEventData).State); } [Test] @@ -608,10 +571,9 @@ public void ConsumeMediaJobProcessingEvent() EventGridEvent[] events = EventGridEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].AsSystemEventData() is MediaJobProcessingEventData); - MediaJobProcessingEventData eventData = (MediaJobProcessingEventData)events[0].AsSystemEventData(); - Assert.AreEqual(MediaJobState.Scheduled, eventData.PreviousState); - Assert.AreEqual(MediaJobState.Processing, eventData.State); + Assert.True(events[0].TryGetSystemEventData(out object eventData)); + Assert.AreEqual(MediaJobState.Scheduled, (eventData as MediaJobProcessingEventData).PreviousState); + Assert.AreEqual(MediaJobState.Processing, (eventData as MediaJobProcessingEventData).State); } [Test] @@ -622,10 +584,9 @@ public void ConsumeMediaJobCancelingEvent() EventGridEvent[] events = EventGridEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].AsSystemEventData() is MediaJobCancelingEventData); - MediaJobCancelingEventData eventData = (MediaJobCancelingEventData)events[0].AsSystemEventData(); - Assert.AreEqual(MediaJobState.Processing, eventData.PreviousState); - Assert.AreEqual(MediaJobState.Canceling, eventData.State); + Assert.True(events[0].TryGetSystemEventData(out object eventData)); + Assert.AreEqual(MediaJobState.Processing, (eventData as MediaJobCancelingEventData).PreviousState); + Assert.AreEqual(MediaJobState.Canceling, (eventData as MediaJobCancelingEventData).State); } [Test] @@ -635,13 +596,12 @@ public void ConsumeMediaJobFinishedEvent() EventGridEvent[] events = EventGridEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].AsSystemEventData() is MediaJobFinishedEventData); - MediaJobFinishedEventData eventData = (MediaJobFinishedEventData)events[0].AsSystemEventData(); - Assert.AreEqual(MediaJobState.Processing, eventData.PreviousState); - Assert.AreEqual(MediaJobState.Finished, eventData.State); - Assert.AreEqual(1, eventData.Outputs.Count); - Assert.True(eventData.Outputs[0] is MediaJobOutputAsset); - MediaJobOutputAsset outputAsset = (MediaJobOutputAsset)eventData.Outputs[0]; + Assert.True(events[0].TryGetSystemEventData(out object eventData)); + Assert.AreEqual(MediaJobState.Processing, (eventData as MediaJobFinishedEventData).PreviousState); + Assert.AreEqual(MediaJobState.Finished, (eventData as MediaJobFinishedEventData).State); + Assert.AreEqual(1, (eventData as MediaJobFinishedEventData).Outputs.Count); + Assert.True((eventData as MediaJobFinishedEventData).Outputs[0] is MediaJobOutputAsset); + MediaJobOutputAsset outputAsset = (MediaJobOutputAsset)(eventData as MediaJobFinishedEventData).Outputs[0]; Assert.AreEqual(MediaJobState.Finished, outputAsset.State); Assert.Null(outputAsset.Error); @@ -657,14 +617,13 @@ public void ConsumeMediaJobCanceledEvent() EventGridEvent[] events = EventGridEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].AsSystemEventData() is MediaJobCanceledEventData); - MediaJobCanceledEventData eventData = (MediaJobCanceledEventData)events[0].AsSystemEventData(); - Assert.AreEqual(MediaJobState.Canceling, eventData.PreviousState); - Assert.AreEqual(MediaJobState.Canceled, eventData.State); - Assert.AreEqual(1, eventData.Outputs.Count); - Assert.True(eventData.Outputs[0] is MediaJobOutputAsset); + Assert.True(events[0].TryGetSystemEventData(out object eventData)); + Assert.AreEqual(MediaJobState.Canceling, (eventData as MediaJobCanceledEventData).PreviousState); + Assert.AreEqual(MediaJobState.Canceled, (eventData as MediaJobCanceledEventData).State); + Assert.AreEqual(1, (eventData as MediaJobCanceledEventData).Outputs.Count); + Assert.True((eventData as MediaJobCanceledEventData).Outputs[0] is MediaJobOutputAsset); - MediaJobOutputAsset outputAsset = (MediaJobOutputAsset)eventData.Outputs[0]; + MediaJobOutputAsset outputAsset = (MediaJobOutputAsset)(eventData as MediaJobCanceledEventData).Outputs[0]; Assert.AreEqual(MediaJobState.Canceled, outputAsset.State); Assert.AreNotEqual(100, outputAsset.Progress); @@ -679,17 +638,16 @@ public void ConsumeMediaJobErroredEvent() EventGridEvent[] events = EventGridEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].AsSystemEventData() is MediaJobErroredEventData); - MediaJobErroredEventData eventData = (MediaJobErroredEventData)events[0].AsSystemEventData(); - Assert.AreEqual(MediaJobState.Processing, eventData.PreviousState); - Assert.AreEqual(MediaJobState.Error, eventData.State); - Assert.AreEqual(1, eventData.Outputs.Count); - Assert.True(eventData.Outputs[0] is MediaJobOutputAsset); + Assert.True(events[0].TryGetSystemEventData(out object eventData)); + Assert.AreEqual(MediaJobState.Processing, (eventData as MediaJobErroredEventData).PreviousState); + Assert.AreEqual(MediaJobState.Error, (eventData as MediaJobErroredEventData).State); + Assert.AreEqual(1, (eventData as MediaJobErroredEventData).Outputs.Count); + Assert.True((eventData as MediaJobErroredEventData).Outputs[0] is MediaJobOutputAsset); - Assert.AreEqual(MediaJobState.Error, eventData.Outputs[0].State); - Assert.NotNull(eventData.Outputs[0].Error); - Assert.AreEqual(MediaJobErrorCategory.Service, eventData.Outputs[0].Error.Category); - Assert.AreEqual(MediaJobErrorCode.ServiceError, eventData.Outputs[0].Error.Code); + Assert.AreEqual(MediaJobState.Error, (eventData as MediaJobErroredEventData).Outputs[0].State); + Assert.NotNull((eventData as MediaJobErroredEventData).Outputs[0].Error); + Assert.AreEqual(MediaJobErrorCategory.Service, (eventData as MediaJobErroredEventData).Outputs[0].Error.Category); + Assert.AreEqual(MediaJobErrorCode.ServiceError, (eventData as MediaJobErroredEventData).Outputs[0].Error.Code); } [Test] @@ -700,11 +658,10 @@ public void ConsumeMediaJobOutputCanceledEvent() EventGridEvent[] events = EventGridEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].AsSystemEventData() is MediaJobOutputCanceledEventData); - MediaJobOutputCanceledEventData eventData = (MediaJobOutputCanceledEventData)events[0].AsSystemEventData(); - Assert.AreEqual(MediaJobState.Canceling, eventData.PreviousState); - Assert.AreEqual(MediaJobState.Canceled, eventData.Output.State); - Assert.True(eventData.Output is MediaJobOutputAsset); + Assert.True(events[0].TryGetSystemEventData(out object eventData)); + Assert.AreEqual(MediaJobState.Canceling, (eventData as MediaJobOutputCanceledEventData).PreviousState); + Assert.AreEqual(MediaJobState.Canceled, (eventData as MediaJobOutputCanceledEventData).Output.State); + Assert.True((eventData as MediaJobOutputCanceledEventData).Output is MediaJobOutputAsset); } [Test] @@ -715,11 +672,10 @@ public void ConsumeMediaJobOutputCancelingEvent() EventGridEvent[] events = EventGridEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].AsSystemEventData() is MediaJobOutputCancelingEventData); - MediaJobOutputCancelingEventData eventData = (MediaJobOutputCancelingEventData)events[0].AsSystemEventData(); - Assert.AreEqual(MediaJobState.Processing, eventData.PreviousState); - Assert.AreEqual(MediaJobState.Canceling, eventData.Output.State); - Assert.True(eventData.Output is MediaJobOutputAsset); + Assert.True(events[0].TryGetSystemEventData(out object eventData)); + Assert.AreEqual(MediaJobState.Processing, (eventData as MediaJobOutputCancelingEventData).PreviousState); + Assert.AreEqual(MediaJobState.Canceling, (eventData as MediaJobOutputCancelingEventData).Output.State); + Assert.True((eventData as MediaJobOutputCancelingEventData).Output is MediaJobOutputAsset); } [Test] @@ -730,14 +686,14 @@ public void ConsumeMediaJobOutputErroredEvent() EventGridEvent[] events = EventGridEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].AsSystemEventData() is MediaJobOutputErroredEventData); - MediaJobOutputErroredEventData eventData = (MediaJobOutputErroredEventData)events[0].AsSystemEventData(); - Assert.AreEqual(MediaJobState.Processing, eventData.PreviousState); - Assert.AreEqual(MediaJobState.Error, eventData.Output.State); - Assert.True(eventData.Output is MediaJobOutputAsset); - Assert.NotNull(eventData.Output.Error); - Assert.AreEqual(MediaJobErrorCategory.Service, eventData.Output.Error.Category); - Assert.AreEqual(MediaJobErrorCode.ServiceError, eventData.Output.Error.Code); + Assert.True(events[0].TryGetSystemEventData(out object eventData)); + MediaJobOutputErroredEventData sysEvent = eventData as MediaJobOutputErroredEventData; + Assert.AreEqual(MediaJobState.Processing, sysEvent.PreviousState); + Assert.AreEqual(MediaJobState.Error, sysEvent.Output.State); + Assert.True(sysEvent.Output is MediaJobOutputAsset); + Assert.NotNull(sysEvent.Output.Error); + Assert.AreEqual(MediaJobErrorCategory.Service, sysEvent.Output.Error.Category); + Assert.AreEqual(MediaJobErrorCode.ServiceError, sysEvent.Output.Error.Code); } [Test] @@ -748,14 +704,15 @@ public void ConsumeMediaJobOutputFinishedEvent() EventGridEvent[] events = EventGridEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].AsSystemEventData() is MediaJobOutputFinishedEventData); - MediaJobOutputFinishedEventData eventData = (MediaJobOutputFinishedEventData)events[0].AsSystemEventData(); - Assert.AreEqual(MediaJobState.Processing, eventData.PreviousState); - Assert.AreEqual(MediaJobState.Finished, eventData.Output.State); - Assert.True(eventData.Output is MediaJobOutputAsset); - Assert.AreEqual(100, eventData.Output.Progress); + Assert.True(events[0].TryGetSystemEventData(out object eventData)); + MediaJobOutputFinishedEventData sysEvent = eventData as MediaJobOutputFinishedEventData; - MediaJobOutputAsset outputAsset = (MediaJobOutputAsset)eventData.Output; + Assert.AreEqual(MediaJobState.Processing, sysEvent.PreviousState); + Assert.AreEqual(MediaJobState.Finished, sysEvent.Output.State); + Assert.True(sysEvent.Output is MediaJobOutputAsset); + Assert.AreEqual(100, sysEvent.Output.Progress); + + MediaJobOutputAsset outputAsset = (MediaJobOutputAsset)sysEvent.Output; Assert.AreEqual("output-2ac2fe75-6557-4de5-ab25-5713b74a6901", outputAsset.AssetName); } @@ -767,11 +724,10 @@ public void ConsumeMediaJobOutputProcessingEvent() EventGridEvent[] events = EventGridEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].AsSystemEventData() is MediaJobOutputProcessingEventData); - MediaJobOutputProcessingEventData eventData = (MediaJobOutputProcessingEventData)events[0].AsSystemEventData(); - Assert.AreEqual(MediaJobState.Scheduled, eventData.PreviousState); - Assert.AreEqual(MediaJobState.Processing, eventData.Output.State); - Assert.True(eventData.Output is MediaJobOutputAsset); + Assert.True(events[0].TryGetSystemEventData(out object eventData)); + Assert.AreEqual(MediaJobState.Scheduled, (eventData as MediaJobOutputProcessingEventData).PreviousState); + Assert.AreEqual(MediaJobState.Processing, (eventData as MediaJobOutputProcessingEventData).Output.State); + Assert.True((eventData as MediaJobOutputProcessingEventData).Output is MediaJobOutputAsset); } [Test] @@ -782,11 +738,10 @@ public void ConsumeMediaJobOutputScheduledEvent() EventGridEvent[] events = EventGridEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].AsSystemEventData() is MediaJobOutputScheduledEventData); - MediaJobOutputScheduledEventData eventData = (MediaJobOutputScheduledEventData)events[0].AsSystemEventData(); - Assert.AreEqual(MediaJobState.Queued, eventData.PreviousState); - Assert.AreEqual(MediaJobState.Scheduled, eventData.Output.State); - Assert.True(eventData.Output is MediaJobOutputAsset); + Assert.True(events[0].TryGetSystemEventData(out object eventData)); + Assert.AreEqual(MediaJobState.Queued, (eventData as MediaJobOutputScheduledEventData).PreviousState); + Assert.AreEqual(MediaJobState.Scheduled, (eventData as MediaJobOutputScheduledEventData).Output.State); + Assert.True((eventData as MediaJobOutputScheduledEventData).Output is MediaJobOutputAsset); } [Test] @@ -797,14 +752,15 @@ public void ConsumeMediaJobOutputProgressEvent() EventGridEvent[] events = EventGridEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].AsSystemEventData() is MediaJobOutputProgressEventData); - MediaJobOutputProgressEventData eventData = (MediaJobOutputProgressEventData)events[0].AsSystemEventData(); - Assert.AreEqual("TestLabel", eventData.Label); - Assert.AreEqual(50, eventData.Progress); - Assert.True(eventData.JobCorrelationData.ContainsKey("Field1")); - Assert.AreEqual("test1", eventData.JobCorrelationData["Field1"]); - Assert.True(eventData.JobCorrelationData.ContainsKey("Field2")); - Assert.AreEqual("test2", eventData.JobCorrelationData["Field2"]); + Assert.True(events[0].TryGetSystemEventData(out object eventData)); + MediaJobOutputProgressEventData sysData = eventData as MediaJobOutputProgressEventData; + + Assert.AreEqual("TestLabel", sysData.Label); + Assert.AreEqual(50, sysData.Progress); + Assert.True(sysData.JobCorrelationData.ContainsKey("Field1")); + Assert.AreEqual("test1", sysData.JobCorrelationData["Field1"]); + Assert.True(sysData.JobCorrelationData.ContainsKey("Field2")); + Assert.AreEqual("test2", sysData.JobCorrelationData["Field2"]); } [Test] @@ -815,12 +771,13 @@ public void ConsumeMediaLiveEventEncoderConnectedEvent() EventGridEvent[] events = EventGridEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].AsSystemEventData() is MediaLiveEventEncoderConnectedEventData); - MediaLiveEventEncoderConnectedEventData eventData = (MediaLiveEventEncoderConnectedEventData)events[0].AsSystemEventData(); - Assert.AreEqual("rtmp://liveevent-ec9d26a8.channel.media.azure.net:1935/live/cb5540b10a5646218c1328be95050c59", eventData.IngestUrl); - Assert.AreEqual("Mystream1", eventData.StreamId); - Assert.AreEqual("", eventData.EncoderIp); - Assert.AreEqual("3557", eventData.EncoderPort); + Assert.True(events[0].TryGetSystemEventData(out object eventData)); + MediaLiveEventEncoderConnectedEventData sysData = eventData as MediaLiveEventEncoderConnectedEventData; + + Assert.AreEqual("rtmp://liveevent-ec9d26a8.channel.media.azure.net:1935/live/cb5540b10a5646218c1328be95050c59", sysData.IngestUrl); + Assert.AreEqual("Mystream1", sysData.StreamId); + Assert.AreEqual("", sysData.EncoderIp); + Assert.AreEqual("3557", sysData.EncoderPort); } [Test] @@ -831,12 +788,13 @@ public void ConsumeMediaLiveEventConnectionRejectedEvent() EventGridEvent[] events = EventGridEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].AsSystemEventData() is MediaLiveEventConnectionRejectedEventData); - MediaLiveEventConnectionRejectedEventData eventData = (MediaLiveEventConnectionRejectedEventData)events[0].AsSystemEventData(); - Assert.AreEqual("rtmp://liveevent-ec9d26a8.channel.media.azure.net:1935/live/cb5540b10a5646218c1328be95050c59", eventData.IngestUrl); - Assert.AreEqual("Mystream1", eventData.StreamId); - Assert.AreEqual("", eventData.EncoderIp); - Assert.AreEqual("3557", eventData.EncoderPort); + Assert.True(events[0].TryGetSystemEventData(out object eventData)); + + var sysData = eventData as MediaLiveEventConnectionRejectedEventData; + Assert.AreEqual("rtmp://liveevent-ec9d26a8.channel.media.azure.net:1935/live/cb5540b10a5646218c1328be95050c59", sysData.IngestUrl); + Assert.AreEqual("Mystream1", sysData.StreamId); + Assert.AreEqual("", sysData.EncoderIp); + Assert.AreEqual("3557", sysData.EncoderPort); } [Test] @@ -846,14 +804,14 @@ public void ConsumeMediaLiveEventEncoderDisconnectedEvent() EventGridEvent[] events = EventGridEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].AsSystemEventData() is MediaLiveEventEncoderDisconnectedEventData); - MediaLiveEventEncoderDisconnectedEventData eventData = (MediaLiveEventEncoderDisconnectedEventData)events[0].AsSystemEventData(); - Assert.AreEqual("MPE_CLIENT_TERMINATED_SESSION", eventData.ResultCode); + Assert.True(events[0].TryGetSystemEventData(out object eventData)); + var sysData = eventData as MediaLiveEventEncoderDisconnectedEventData; - Assert.AreEqual("rtmp://liveevent-ec9d26a8.channel.media.azure.net:1935/live/cb5540b10a5646218c1328be95050c59", eventData.IngestUrl); - Assert.AreEqual("Mystream1", eventData.StreamId); - Assert.AreEqual("", eventData.EncoderIp); - Assert.AreEqual("3557", eventData.EncoderPort); + Assert.AreEqual("MPE_CLIENT_TERMINATED_SESSION", sysData.ResultCode); + Assert.AreEqual("rtmp://liveevent-ec9d26a8.channel.media.azure.net:1935/live/cb5540b10a5646218c1328be95050c59", sysData.IngestUrl); + Assert.AreEqual("Mystream1", sysData.StreamId); + Assert.AreEqual("", sysData.EncoderIp); + Assert.AreEqual("3557", sysData.EncoderPort); } [Test] @@ -864,18 +822,18 @@ public void ConsumeMediaLiveEventIncomingStreamReceivedEvent() EventGridEvent[] events = EventGridEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].AsSystemEventData() is MediaLiveEventIncomingStreamReceivedEventData); - MediaLiveEventIncomingStreamReceivedEventData eventData = (MediaLiveEventIncomingStreamReceivedEventData)events[0].AsSystemEventData(); + Assert.True(events[0].TryGetSystemEventData(out object eventData)); + var sysData = eventData as MediaLiveEventIncomingStreamReceivedEventData; - Assert.AreEqual("rtmp://liveevent-ec9d26a8.channel.media.azure.net:1935/live/cb5540b10a5646218c1328be95050c59", eventData.IngestUrl); - Assert.AreEqual("", eventData.EncoderIp); - Assert.AreEqual("3557", eventData.EncoderPort); - Assert.AreEqual("audio", eventData.TrackType); - Assert.AreEqual("audio_160000", eventData.TrackName); - Assert.AreEqual(160000, eventData.Bitrate); - Assert.AreEqual("66", eventData.Timestamp); - Assert.AreEqual("1950", eventData.Duration); - Assert.AreEqual("1000", eventData.Timescale); + Assert.AreEqual("rtmp://liveevent-ec9d26a8.channel.media.azure.net:1935/live/cb5540b10a5646218c1328be95050c59", sysData.IngestUrl); + Assert.AreEqual("", sysData.EncoderIp); + Assert.AreEqual("3557", sysData.EncoderPort); + Assert.AreEqual("audio", sysData.TrackType); + Assert.AreEqual("audio_160000", sysData.TrackName); + Assert.AreEqual(160000, sysData.Bitrate); + Assert.AreEqual("66", sysData.Timestamp); + Assert.AreEqual("1950", sysData.Duration); + Assert.AreEqual("1000", sysData.Timescale); } [Test] @@ -886,14 +844,15 @@ public void ConsumeMediaLiveEventIncomingStreamsOutOfSyncEvent() EventGridEvent[] events = EventGridEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].AsSystemEventData() is MediaLiveEventIncomingStreamsOutOfSyncEventData); - MediaLiveEventIncomingStreamsOutOfSyncEventData eventData = (MediaLiveEventIncomingStreamsOutOfSyncEventData)events[0].AsSystemEventData(); - Assert.AreEqual("10999", eventData.MinLastTimestamp); - Assert.AreEqual("video", eventData.TypeOfStreamWithMinLastTimestamp); - Assert.AreEqual("100999", eventData.MaxLastTimestamp); - Assert.AreEqual("audio", eventData.TypeOfStreamWithMaxLastTimestamp); - Assert.AreEqual("1000", eventData.TimescaleOfMinLastTimestamp); - Assert.AreEqual("1000", eventData.TimescaleOfMaxLastTimestamp); + Assert.True(events[0].TryGetSystemEventData(out object eventData)); + var sysData = eventData as MediaLiveEventIncomingStreamsOutOfSyncEventData; + + Assert.AreEqual("10999", sysData.MinLastTimestamp); + Assert.AreEqual("video", sysData.TypeOfStreamWithMinLastTimestamp); + Assert.AreEqual("100999", sysData.MaxLastTimestamp); + Assert.AreEqual("audio", sysData.TypeOfStreamWithMaxLastTimestamp); + Assert.AreEqual("1000", sysData.TimescaleOfMinLastTimestamp); + Assert.AreEqual("1000", sysData.TimescaleOfMaxLastTimestamp); } [Test] @@ -904,13 +863,14 @@ public void ConsumeMediaLiveEventIncomingVideoStreamsOutOfSyncEvent() EventGridEvent[] events = EventGridEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].AsSystemEventData() is MediaLiveEventIncomingVideoStreamsOutOfSyncEventData); - MediaLiveEventIncomingVideoStreamsOutOfSyncEventData eventData = (MediaLiveEventIncomingVideoStreamsOutOfSyncEventData)events[0].AsSystemEventData(); - Assert.AreEqual("10999", eventData.FirstTimestamp); - Assert.AreEqual("2000", eventData.FirstDuration); - Assert.AreEqual("100999", eventData.SecondTimestamp); - Assert.AreEqual("2000", eventData.SecondDuration); - Assert.AreEqual("1000", eventData.Timescale); + Assert.True(events[0].TryGetSystemEventData(out object eventData)); + var sysData = eventData as MediaLiveEventIncomingVideoStreamsOutOfSyncEventData; + + Assert.AreEqual("10999", sysData.FirstTimestamp); + Assert.AreEqual("2000", sysData.FirstDuration); + Assert.AreEqual("100999", sysData.SecondTimestamp); + Assert.AreEqual("2000", sysData.SecondDuration); + Assert.AreEqual("1000", sysData.Timescale); } [Test] @@ -921,14 +881,15 @@ public void ConsumeMediaLiveEventIncomingDataChunkDroppedEvent() EventGridEvent[] events = EventGridEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].AsSystemEventData() is MediaLiveEventIncomingDataChunkDroppedEventData); - MediaLiveEventIncomingDataChunkDroppedEventData eventData = (MediaLiveEventIncomingDataChunkDroppedEventData)events[0].AsSystemEventData(); - Assert.AreEqual("8999", eventData.Timestamp); - Assert.AreEqual("video", eventData.TrackType); - Assert.AreEqual("video1", eventData.TrackName); - Assert.AreEqual(2500000, eventData.Bitrate); - Assert.AreEqual("1000", eventData.Timescale); - Assert.AreEqual("FragmentDrop_OverlapTimestamp", eventData.ResultCode); + Assert.True(events[0].TryGetSystemEventData(out object eventData)); + var sysData = eventData as MediaLiveEventIncomingDataChunkDroppedEventData; + + Assert.AreEqual("8999", sysData.Timestamp); + Assert.AreEqual("video", sysData.TrackType); + Assert.AreEqual("video1", sysData.TrackName); + Assert.AreEqual(2500000, sysData.Bitrate); + Assert.AreEqual("1000", sysData.Timescale); + Assert.AreEqual("FragmentDrop_OverlapTimestamp", sysData.ResultCode); } [Test] @@ -939,20 +900,20 @@ public void ConsumeMediaLiveEventIngestHeartbeatEvent() EventGridEvent[] events = EventGridEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].AsSystemEventData() is MediaLiveEventIngestHeartbeatEventData); - MediaLiveEventIngestHeartbeatEventData eventData = (MediaLiveEventIngestHeartbeatEventData)events[0].AsSystemEventData(); - Assert.AreEqual("video", eventData.TrackType); - Assert.AreEqual("video", eventData.TrackName); - Assert.AreEqual(2500000, eventData.Bitrate); - Assert.AreEqual(500726, eventData.IncomingBitrate); - Assert.AreEqual("11999", eventData.LastTimestamp); - Assert.AreEqual("1000", eventData.Timescale); - Assert.AreEqual(0, eventData.OverlapCount); - Assert.AreEqual(0, eventData.DiscontinuityCount); - Assert.AreEqual(0, eventData.NonincreasingCount); - Assert.True(eventData.UnexpectedBitrate); - Assert.AreEqual("Running", eventData.State); - Assert.False(eventData.Healthy); + Assert.True(events[0].TryGetSystemEventData(out object eventData)); + MediaLiveEventIngestHeartbeatEventData sysData = eventData as MediaLiveEventIngestHeartbeatEventData; + Assert.AreEqual("video", sysData.TrackType); + Assert.AreEqual("video", sysData.TrackName); + Assert.AreEqual(2500000, sysData.Bitrate); + Assert.AreEqual(500726, sysData.IncomingBitrate); + Assert.AreEqual("11999", sysData.LastTimestamp); + Assert.AreEqual("1000", sysData.Timescale); + Assert.AreEqual(0, sysData.OverlapCount); + Assert.AreEqual(0, sysData.DiscontinuityCount); + Assert.AreEqual(0, sysData.NonincreasingCount); + Assert.True(sysData.UnexpectedBitrate); + Assert.AreEqual("Running", sysData.State); + Assert.False(sysData.Healthy); } [Test] @@ -963,15 +924,15 @@ public void ConsumeMediaLiveEventTrackDiscontinuityDetectedEvent() EventGridEvent[] events = EventGridEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].AsSystemEventData() is MediaLiveEventTrackDiscontinuityDetectedEventData); - MediaLiveEventTrackDiscontinuityDetectedEventData eventData = (MediaLiveEventTrackDiscontinuityDetectedEventData)events[0].AsSystemEventData(); - Assert.AreEqual("video", eventData.TrackType); - Assert.AreEqual("video", eventData.TrackName); - Assert.AreEqual(2500000, eventData.Bitrate); - Assert.AreEqual("10999", eventData.PreviousTimestamp); - Assert.AreEqual("14999", eventData.NewTimestamp); - Assert.AreEqual("1000", eventData.Timescale); - Assert.AreEqual("4000", eventData.DiscontinuityGap); + Assert.True(events[0].TryGetSystemEventData(out object eventData)); + MediaLiveEventTrackDiscontinuityDetectedEventData sysData = eventData as MediaLiveEventTrackDiscontinuityDetectedEventData; + Assert.AreEqual("video", sysData.TrackType); + Assert.AreEqual("video", sysData.TrackName); + Assert.AreEqual(2500000, sysData.Bitrate); + Assert.AreEqual("10999", sysData.PreviousTimestamp); + Assert.AreEqual("14999", sysData.NewTimestamp); + Assert.AreEqual("1000", sysData.Timescale); + Assert.AreEqual("4000", sysData.DiscontinuityGap); } #endregion @@ -984,9 +945,8 @@ public void ConsumeResourceWriteSuccessEvent() EventGridEvent[] events = EventGridEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].AsSystemEventData() is ResourceWriteSuccessData); - ResourceWriteSuccessData eventData = (ResourceWriteSuccessData)events[0].AsSystemEventData(); - Assert.AreEqual("72f988bf-86f1-41af-91ab-2d7cd011db47", eventData.TenantId); + Assert.True(events[0].TryGetSystemEventData(out object eventData)); + Assert.AreEqual("72f988bf-86f1-41af-91ab-2d7cd011db47", (eventData as ResourceWriteSuccessData).TenantId); } [Test] @@ -997,9 +957,8 @@ public void ConsumeResourceWriteFailureEvent() EventGridEvent[] events = EventGridEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].AsSystemEventData() is ResourceWriteFailureData); - ResourceWriteFailureData eventData = (ResourceWriteFailureData)events[0].AsSystemEventData(); - Assert.AreEqual("72f988bf-86f1-41af-91ab-2d7cd011db47", eventData.TenantId); + Assert.True(events[0].TryGetSystemEventData(out object eventData)); + Assert.AreEqual("72f988bf-86f1-41af-91ab-2d7cd011db47", (eventData as ResourceWriteFailureData).TenantId); } [Test] @@ -1010,9 +969,8 @@ public void ConsumeResourceWriteCancelEvent() EventGridEvent[] events = EventGridEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].AsSystemEventData() is ResourceWriteCancelData); - ResourceWriteCancelData eventData = (ResourceWriteCancelData)events[0].AsSystemEventData(); - Assert.AreEqual("72f988bf-86f1-41af-91ab-2d7cd011db47", eventData.TenantId); + Assert.True(events[0].TryGetSystemEventData(out object eventData)); + Assert.AreEqual("72f988bf-86f1-41af-91ab-2d7cd011db47", (eventData as ResourceWriteCancelData).TenantId); } [Test] @@ -1023,9 +981,8 @@ public void ConsumeResourceDeleteSuccessEvent() EventGridEvent[] events = EventGridEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].AsSystemEventData() is ResourceDeleteSuccessData); - ResourceDeleteSuccessData eventData = (ResourceDeleteSuccessData)events[0].AsSystemEventData(); - Assert.AreEqual("72f988bf-86f1-41af-91ab-2d7cd011db47", eventData.TenantId); + Assert.True(events[0].TryGetSystemEventData(out object eventData)); + Assert.AreEqual("72f988bf-86f1-41af-91ab-2d7cd011db47", (eventData as ResourceDeleteSuccessData).TenantId); } [Test] @@ -1036,9 +993,8 @@ public void ConsumeResourceDeleteFailureEvent() EventGridEvent[] events = EventGridEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].AsSystemEventData() is ResourceDeleteFailureData); - ResourceDeleteFailureData eventData = (ResourceDeleteFailureData)events[0].AsSystemEventData(); - Assert.AreEqual("72f988bf-86f1-41af-91ab-2d7cd011db47", eventData.TenantId); + Assert.True(events[0].TryGetSystemEventData(out object eventData)); + Assert.AreEqual("72f988bf-86f1-41af-91ab-2d7cd011db47", (eventData as ResourceDeleteFailureData).TenantId); } [Test] @@ -1049,9 +1005,8 @@ public void ConsumeResourceDeleteCancelEvent() EventGridEvent[] events = EventGridEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].AsSystemEventData() is ResourceDeleteCancelData); - ResourceDeleteCancelData eventData = (ResourceDeleteCancelData)events[0].AsSystemEventData(); - Assert.AreEqual("72f988bf-86f1-41af-91ab-2d7cd011db47", eventData.TenantId); + Assert.True(events[0].TryGetSystemEventData(out object eventData)); + Assert.AreEqual("72f988bf-86f1-41af-91ab-2d7cd011db47", (eventData as ResourceDeleteCancelData).TenantId); } [Test] @@ -1062,9 +1017,8 @@ public void ConsumeResourceActionSuccessEvent() EventGridEvent[] events = EventGridEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].AsSystemEventData() is ResourceActionSuccessData); - ResourceActionSuccessData eventData = (ResourceActionSuccessData)events[0].AsSystemEventData(); - Assert.AreEqual("72f988bf-86f1-41af-91ab-2d7cd011db47", eventData.TenantId); + Assert.True(events[0].TryGetSystemEventData(out object eventData)); + Assert.AreEqual("72f988bf-86f1-41af-91ab-2d7cd011db47", (eventData as ResourceActionSuccessData).TenantId); } [Test] @@ -1075,9 +1029,8 @@ public void ConsumeResourceActionFailureEvent() EventGridEvent[] events = EventGridEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].AsSystemEventData() is ResourceActionFailureData); - ResourceActionFailureData eventData = (ResourceActionFailureData)events[0].AsSystemEventData(); - Assert.AreEqual("72f988bf-86f1-41af-91ab-2d7cd011db47", eventData.TenantId); + Assert.True(events[0].TryGetSystemEventData(out object eventData)); + Assert.AreEqual("72f988bf-86f1-41af-91ab-2d7cd011db47", (eventData as ResourceActionFailureData).TenantId); } [Test] @@ -1088,9 +1041,8 @@ public void ConsumeResourceActionCancelEvent() EventGridEvent[] events = EventGridEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].AsSystemEventData() is ResourceActionCancelData); - ResourceActionCancelData eventData = (ResourceActionCancelData)events[0].AsSystemEventData(); - Assert.AreEqual("72f988bf-86f1-41af-91ab-2d7cd011db47", eventData.TenantId); + Assert.True(events[0].TryGetSystemEventData(out object eventData)); + Assert.AreEqual("72f988bf-86f1-41af-91ab-2d7cd011db47", (eventData as ResourceActionCancelData).TenantId); } #endregion @@ -1103,9 +1055,8 @@ public void ConsumeServiceBusActiveMessagesAvailableWithNoListenersEvent() EventGridEvent[] events = EventGridEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].AsSystemEventData() is ServiceBusActiveMessagesAvailableWithNoListenersEventData); - ServiceBusActiveMessagesAvailableWithNoListenersEventData eventData = (ServiceBusActiveMessagesAvailableWithNoListenersEventData)events[0].AsSystemEventData(); - Assert.AreEqual("testns1", eventData.NamespaceName); + Assert.True(events[0].TryGetSystemEventData(out object eventData)); + Assert.AreEqual("testns1", (eventData as ServiceBusActiveMessagesAvailableWithNoListenersEventData).NamespaceName); } [Test] @@ -1116,9 +1067,8 @@ public void ConsumeServiceBusDeadletterMessagesAvailableWithNoListenersEvent() EventGridEvent[] events = EventGridEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].AsSystemEventData() is ServiceBusDeadletterMessagesAvailableWithNoListenersEventData); - ServiceBusDeadletterMessagesAvailableWithNoListenersEventData eventData = (ServiceBusDeadletterMessagesAvailableWithNoListenersEventData)events[0].AsSystemEventData(); - Assert.AreEqual("testns1", eventData.NamespaceName); + Assert.True(events[0].TryGetSystemEventData(out object eventData)); + Assert.AreEqual("testns1", (eventData as ServiceBusDeadletterMessagesAvailableWithNoListenersEventData).NamespaceName); } #endregion @@ -1131,9 +1081,8 @@ public void ConsumeStorageBlobCreatedEvent() EventGridEvent[] events = EventGridEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].AsSystemEventData() is StorageBlobCreatedEventData); - StorageBlobCreatedEventData eventData = (StorageBlobCreatedEventData)events[0].AsSystemEventData(); - Assert.AreEqual("https://myaccount.blob.core.windows.net/testcontainer/file1.txt", eventData.Url); + Assert.True(events[0].TryGetSystemEventData(out object eventData)); + Assert.AreEqual("https://myaccount.blob.core.windows.net/testcontainer/file1.txt", (eventData as StorageBlobCreatedEventData).Url); } [Test] @@ -1144,9 +1093,8 @@ public void ConsumeStorageBlobDeletedEvent() EventGridEvent[] events = EventGridEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].AsSystemEventData() is StorageBlobDeletedEventData); - StorageBlobDeletedEventData eventData = (StorageBlobDeletedEventData)events[0].AsSystemEventData(); - Assert.AreEqual("https://example.blob.core.windows.net/testcontainer/testfile.txt", eventData.Url); + Assert.True(events[0].TryGetSystemEventData(out object eventData)); + Assert.AreEqual("https://example.blob.core.windows.net/testcontainer/testfile.txt", (eventData as StorageBlobDeletedEventData).Url); } [Test] @@ -1157,9 +1105,8 @@ public void ConsumeStorageBlobRenamedEvent() EventGridEvent[] events = EventGridEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].AsSystemEventData() is StorageBlobRenamedEventData); - StorageBlobRenamedEventData eventData = (StorageBlobRenamedEventData)events[0].AsSystemEventData(); - Assert.AreEqual("https://myaccount.blob.core.windows.net/testcontainer/testfile.txt", eventData.DestinationUrl); + Assert.True(events[0].TryGetSystemEventData(out object eventData)); + Assert.AreEqual("https://myaccount.blob.core.windows.net/testcontainer/testfile.txt", (eventData as StorageBlobRenamedEventData).DestinationUrl); } [Test] @@ -1170,9 +1117,8 @@ public void ConsumeStorageDirectoryCreatedEvent() EventGridEvent[] events = EventGridEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].AsSystemEventData() is StorageDirectoryCreatedEventData); - StorageDirectoryCreatedEventData eventData = (StorageDirectoryCreatedEventData)events[0].AsSystemEventData(); - Assert.AreEqual("https://myaccount.blob.core.windows.net/testcontainer/testDir", eventData.Url); + Assert.True(events[0].TryGetSystemEventData(out object eventData)); + Assert.AreEqual("https://myaccount.blob.core.windows.net/testcontainer/testDir", (eventData as StorageDirectoryCreatedEventData).Url); } [Test] @@ -1183,9 +1129,8 @@ public void ConsumeStorageDirectoryDeletedEvent() EventGridEvent[] events = EventGridEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].AsSystemEventData() is StorageDirectoryDeletedEventData); - StorageDirectoryDeletedEventData eventData = (StorageDirectoryDeletedEventData)events[0].AsSystemEventData(); - Assert.AreEqual("https://example.blob.core.windows.net/testcontainer/testDir", eventData.Url); + Assert.True(events[0].TryGetSystemEventData(out object eventData)); + Assert.AreEqual("https://example.blob.core.windows.net/testcontainer/testDir", (eventData as StorageDirectoryDeletedEventData).Url); } [Test] @@ -1196,9 +1141,8 @@ public void ConsumeStorageDirectoryRenamedEvent() EventGridEvent[] events = EventGridEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].AsSystemEventData() is StorageDirectoryRenamedEventData); - StorageDirectoryRenamedEventData eventData = (StorageDirectoryRenamedEventData)events[0].AsSystemEventData(); - Assert.AreEqual("https://example.blob.core.windows.net/testcontainer/testDir", eventData.DestinationUrl); + Assert.True(events[0].TryGetSystemEventData(out object eventData)); + Assert.AreEqual("https://example.blob.core.windows.net/testcontainer/testDir", (eventData as StorageDirectoryRenamedEventData).DestinationUrl); } #endregion @@ -1212,9 +1156,8 @@ public void ConsumeWebAppUpdatedEvent() EventGridEvent[] events = EventGridEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].AsSystemEventData() is WebAppUpdatedEventData); - WebAppUpdatedEventData eventData = (WebAppUpdatedEventData)events[0].AsSystemEventData(); - Assert.AreEqual(siteName, eventData.Name); + Assert.True(events[0].TryGetSystemEventData(out object eventData)); + Assert.AreEqual(siteName, (eventData as WebAppUpdatedEventData).Name); } [Test] @@ -1226,9 +1169,8 @@ public void ConsumeWebBackupOperationStartedEvent() EventGridEvent[] events = EventGridEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].AsSystemEventData() is WebBackupOperationStartedEventData); - WebBackupOperationStartedEventData eventData = (WebBackupOperationStartedEventData)events[0].AsSystemEventData(); - Assert.AreEqual(siteName, eventData.Name); + Assert.True(events[0].TryGetSystemEventData(out object eventData)); + Assert.AreEqual(siteName, (eventData as WebBackupOperationStartedEventData).Name); } [Test] @@ -1240,9 +1182,8 @@ public void ConsumeWebBackupOperationCompletedEvent() EventGridEvent[] events = EventGridEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].AsSystemEventData() is WebBackupOperationCompletedEventData); - WebBackupOperationCompletedEventData eventData = (WebBackupOperationCompletedEventData)events[0].AsSystemEventData(); - Assert.AreEqual(siteName, eventData.Name); + Assert.True(events[0].TryGetSystemEventData(out object eventData)); + Assert.AreEqual(siteName, (eventData as WebBackupOperationCompletedEventData).Name); } [Test] @@ -1254,9 +1195,8 @@ public void ConsumeWebBackupOperationFailedEvent() EventGridEvent[] events = EventGridEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].AsSystemEventData() is WebBackupOperationFailedEventData); - WebBackupOperationFailedEventData eventData = (WebBackupOperationFailedEventData)events[0].AsSystemEventData(); - Assert.AreEqual(siteName, eventData.Name); + Assert.True(events[0].TryGetSystemEventData(out object eventData)); + Assert.AreEqual(siteName, (eventData as WebBackupOperationFailedEventData).Name); } [Test] @@ -1268,9 +1208,8 @@ public void ConsumeWebRestoreOperationStartedEvent() EventGridEvent[] events = EventGridEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].AsSystemEventData() is WebRestoreOperationStartedEventData); - WebRestoreOperationStartedEventData eventData = (WebRestoreOperationStartedEventData)events[0].AsSystemEventData(); - Assert.AreEqual(siteName, eventData.Name); + Assert.True(events[0].TryGetSystemEventData(out object eventData)); + Assert.AreEqual(siteName, (eventData as WebRestoreOperationStartedEventData).Name); } [Test] @@ -1282,9 +1221,8 @@ public void ConsumeWebRestoreOperationCompletedEvent() EventGridEvent[] events = EventGridEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].AsSystemEventData() is WebRestoreOperationCompletedEventData); - WebRestoreOperationCompletedEventData eventData = (WebRestoreOperationCompletedEventData)events[0].AsSystemEventData(); - Assert.AreEqual(siteName, eventData.Name); + Assert.True(events[0].TryGetSystemEventData(out object eventData)); + Assert.AreEqual(siteName, (eventData as WebRestoreOperationCompletedEventData).Name); } [Test] @@ -1296,9 +1234,8 @@ public void ConsumeWebRestoreOperationFailedEvent() EventGridEvent[] events = EventGridEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].AsSystemEventData() is WebRestoreOperationFailedEventData); - WebRestoreOperationFailedEventData eventData = (WebRestoreOperationFailedEventData)events[0].AsSystemEventData(); - Assert.AreEqual(siteName, eventData.Name); + Assert.True(events[0].TryGetSystemEventData(out object eventData)); + Assert.AreEqual(siteName, (eventData as WebRestoreOperationFailedEventData).Name); } [Test] @@ -1310,9 +1247,8 @@ public void ConsumeWebSlotSwapStartedEvent() EventGridEvent[] events = EventGridEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].AsSystemEventData() is WebSlotSwapStartedEventData); - WebSlotSwapStartedEventData eventData = (WebSlotSwapStartedEventData)events[0].AsSystemEventData(); - Assert.AreEqual(siteName, eventData.Name); + Assert.True(events[0].TryGetSystemEventData(out object eventData)); + Assert.AreEqual(siteName, (eventData as WebSlotSwapStartedEventData).Name); } [Test] @@ -1324,9 +1260,8 @@ public void ConsumeWebSlotSwapCompletedEvent() EventGridEvent[] events = EventGridEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].AsSystemEventData() is WebSlotSwapCompletedEventData); - WebSlotSwapCompletedEventData eventData = (WebSlotSwapCompletedEventData)events[0].AsSystemEventData(); - Assert.AreEqual(siteName, eventData.Name); + Assert.True(events[0].TryGetSystemEventData(out object eventData)); + Assert.AreEqual(siteName, (eventData as WebSlotSwapCompletedEventData).Name); } [Test] @@ -1338,9 +1273,8 @@ public void ConsumeWebSlotSwapFailedEvent() EventGridEvent[] events = EventGridEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].AsSystemEventData() is WebSlotSwapFailedEventData); - WebSlotSwapFailedEventData eventData = (WebSlotSwapFailedEventData)events[0].AsSystemEventData(); - Assert.AreEqual(siteName, eventData.Name); + Assert.True(events[0].TryGetSystemEventData(out object eventData)); + Assert.AreEqual(siteName, (eventData as WebSlotSwapFailedEventData).Name); } [Test] @@ -1352,9 +1286,8 @@ public void ConsumeWebSlotSwapWithPreviewStartedEvent() EventGridEvent[] events = EventGridEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].AsSystemEventData() is WebSlotSwapWithPreviewStartedEventData); - WebSlotSwapWithPreviewStartedEventData eventData = (WebSlotSwapWithPreviewStartedEventData)events[0].AsSystemEventData(); - Assert.AreEqual(siteName, eventData.Name); + Assert.True(events[0].TryGetSystemEventData(out object eventData)); + Assert.AreEqual(siteName, (eventData as WebSlotSwapWithPreviewStartedEventData).Name); } [Test] @@ -1366,9 +1299,8 @@ public void ConsumeWebSlotSwapWithPreviewCancelledEvent() EventGridEvent[] events = EventGridEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].AsSystemEventData() is WebSlotSwapWithPreviewCancelledEventData); - WebSlotSwapWithPreviewCancelledEventData eventData = (WebSlotSwapWithPreviewCancelledEventData)events[0].AsSystemEventData(); - Assert.AreEqual(siteName, eventData.Name); + Assert.True(events[0].TryGetSystemEventData(out object eventData)); + Assert.AreEqual(siteName, (eventData as WebSlotSwapWithPreviewCancelledEventData).Name); } [Test] @@ -1380,9 +1312,8 @@ public void ConsumeWebAppServicePlanUpdatedEvent() EventGridEvent[] events = EventGridEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].AsSystemEventData() is WebAppServicePlanUpdatedEventData); - WebAppServicePlanUpdatedEventData eventData = (WebAppServicePlanUpdatedEventData)events[0].AsSystemEventData(); - Assert.AreEqual(planName, eventData.Name); + Assert.True(events[0].TryGetSystemEventData(out object eventData)); + Assert.AreEqual(planName, (eventData as WebAppServicePlanUpdatedEventData).Name); } #endregion #endregion @@ -1484,9 +1415,9 @@ public void ConsumeMultipleCloudEventsInSameBatch() Assert.AreEqual(5, events.Length); foreach (CloudEvent cloudEvent in events) { - if (cloudEvent.IsSystemEvent) + if (cloudEvent.TryGetSystemEvent(out object eventData)) { - switch (cloudEvent.AsSystemEventData()) + switch (eventData) { case StorageBlobCreatedEventData blobCreated: Assert.AreEqual("https://myaccount.blob.core.windows.net/testcontainer/file1.txt", blobCreated.Url); @@ -1502,47 +1433,18 @@ public void ConsumeMultipleCloudEventsInSameBatch() { case "BinaryDataType": Assert.AreEqual(Convert.ToBase64String(cloudEvent.GetData().ToArray()), "ZGF0YQ=="); - Assert.IsNull(cloudEvent.AsSystemEventData()); + Assert.IsFalse(cloudEvent.TryGetSystemEvent(out var _)); break; case "Contoso.Items.ItemReceived": - ContosoItemReceivedEventData itemReceived = cloudEvent.GetData(camelCaseSerializer); + ContosoItemReceivedEventData itemReceived = cloudEvent.GetData().ToObject(camelCaseSerializer); Assert.AreEqual("512d38b6-c7b8-40c8-89fe-f46f9e9622b6", itemReceived.ItemSku); - Assert.IsNull(cloudEvent.AsSystemEventData()); + Assert.IsFalse(cloudEvent.TryGetSystemEvent(out var _)); break; } } } } - [Test] - public void CloudEventGetDataThrowsWhenCalledWithoutParse() - { - var customSerializer = new JsonObjectSerializer( - new JsonSerializerOptions() - { - PropertyNamingPolicy = JsonNamingPolicy.CamelCase - }); - - CloudEvent cloudEvent = new CloudEvent( - "/contoso/items", - "Contoso.Items.ItemReceived", - new ContosoItemReceivedEventData() - { - ItemSku = "512d38b6-c7b8-40c8-89fe-f46f9e9622b6" - }); - - Assert.That(() => cloudEvent.GetData(customSerializer), - Throws.InstanceOf()); - - ContosoItemReceivedEventData eventData1 = cloudEvent.GetData(); - Assert.AreEqual("512d38b6-c7b8-40c8-89fe-f46f9e9622b6", eventData1.ItemSku); - if (cloudEvent.GetData() is BinaryData binaryEventData) - { - ContosoItemReceivedEventData eventData2 = binaryEventData.ToObjectFromJson(); - Assert.AreEqual("512d38b6-c7b8-40c8-89fe-f46f9e9622b6", eventData1.ItemSku); - } - } - [Test] public void CloudEventParseDoesNotThrowIfMissingSource() { @@ -1619,7 +1521,7 @@ public void ConsumeCloudEventWithCustomEventPayload() Assert.NotNull(events); - ContosoItemReceivedEventData eventData = events[0].GetData(camelCaseSerializer); + ContosoItemReceivedEventData eventData = events[0].GetData().ToObject(camelCaseSerializer); Assert.AreEqual("512d38b6-c7b8-40c8-89fe-f46f9e9622b6", eventData.ItemSku); } @@ -1638,7 +1540,7 @@ public void ConsumeCloudEventWithArrayDataPayload() Assert.NotNull(events); - ContosoItemReceivedEventData[] eventData = events[0].GetData(camelCaseSerializer); + ContosoItemReceivedEventData[] eventData = events[0].GetData().ToObject(camelCaseSerializer); Assert.AreEqual("512d38b6-c7b8-40c8-89fe-f46f9e9622b6", eventData[0].ItemSku); } #endregion @@ -1708,9 +1610,8 @@ public void ConsumeCloudEventAppConfigurationKeyValueDeletedEvent() CloudEvent[] events = CloudEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].AsSystemEventData() is AppConfigurationKeyValueDeletedEventData); - AppConfigurationKeyValueDeletedEventData eventData = (AppConfigurationKeyValueDeletedEventData)events[0].AsSystemEventData(); - Assert.AreEqual("key1", eventData.Key); + Assert.True(events[0].TryGetSystemEvent(out object eventData)); + Assert.AreEqual("key1", (eventData as AppConfigurationKeyValueDeletedEventData).Key); } [Test] @@ -1720,9 +1621,8 @@ public void ConsumeCloudEventAppConfigurationKeyValueModifiedEvent() CloudEvent[] events = CloudEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].AsSystemEventData() is AppConfigurationKeyValueModifiedEventData); - AppConfigurationKeyValueModifiedEventData eventData = (AppConfigurationKeyValueModifiedEventData)events[0].AsSystemEventData(); - Assert.AreEqual("key1", eventData.Key); + Assert.True(events[0].TryGetSystemEvent(out object eventData)); + Assert.AreEqual("key1", (eventData as AppConfigurationKeyValueModifiedEventData).Key); } #endregion @@ -1735,9 +1635,8 @@ public void ConsumeCloudEventContainerRegistryImagePushedEvent() CloudEvent[] events = CloudEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].AsSystemEventData() is ContainerRegistryImagePushedEventData); - ContainerRegistryImagePushedEventData eventData = (ContainerRegistryImagePushedEventData)events[0].AsSystemEventData(); - Assert.AreEqual("127.0.0.1", eventData.Request.Addr); + Assert.True(events[0].TryGetSystemEvent(out object eventData)); + Assert.AreEqual("127.0.0.1", (eventData as ContainerRegistryImagePushedEventData).Request.Addr); } [Test] @@ -1748,9 +1647,8 @@ public void ConsumeCloudEventContainerRegistryImageDeletedEvent() CloudEvent[] events = CloudEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].AsSystemEventData() is ContainerRegistryImageDeletedEventData); - ContainerRegistryImageDeletedEventData eventData = (ContainerRegistryImageDeletedEventData)events[0].AsSystemEventData(); - Assert.AreEqual("testactor", eventData.Actor.Name); + Assert.True(events[0].TryGetSystemEvent(out object eventData)); + Assert.AreEqual("testactor", (eventData as ContainerRegistryImageDeletedEventData).Actor.Name); } [Test] @@ -1761,9 +1659,8 @@ public void ConsumeCloudEventContainerRegistryChartDeletedEvent() CloudEvent[] events = CloudEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].AsSystemEventData() is ContainerRegistryChartDeletedEventData); - ContainerRegistryChartDeletedEventData eventData = (ContainerRegistryChartDeletedEventData)events[0].AsSystemEventData(); - Assert.AreEqual("mediatype1", eventData.Target.MediaType); + Assert.True(events[0].TryGetSystemEvent(out object eventData)); + Assert.AreEqual("mediatype1", (eventData as ContainerRegistryChartDeletedEventData).Target.MediaType); } [Test] @@ -1774,9 +1671,8 @@ public void ConsumeCloudEventContainerRegistryChartPushedEvent() CloudEvent[] events = CloudEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].AsSystemEventData() is ContainerRegistryChartPushedEventData); - ContainerRegistryChartPushedEventData eventData = (ContainerRegistryChartPushedEventData)events[0].AsSystemEventData(); - Assert.AreEqual("mediatype1", eventData.Target.MediaType); + Assert.True(events[0].TryGetSystemEvent(out object eventData)); + Assert.AreEqual("mediatype1", (eventData as ContainerRegistryChartPushedEventData).Target.MediaType); } #endregion @@ -1789,9 +1685,8 @@ public void ConsumeCloudEventIoTHubDeviceCreatedEvent() CloudEvent[] events = CloudEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].AsSystemEventData() is IotHubDeviceCreatedEventData); - IotHubDeviceCreatedEventData eventData = (IotHubDeviceCreatedEventData)events[0].AsSystemEventData(); - Assert.AreEqual("enabled", eventData.Twin.Status); + Assert.True(events[0].TryGetSystemEvent(out object eventData)); + Assert.AreEqual("enabled", (eventData as IotHubDeviceCreatedEventData).Twin.Status); } [Test] @@ -1802,9 +1697,8 @@ public void ConsumeCloudEventIoTHubDeviceDeletedEvent() CloudEvent[] events = CloudEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].AsSystemEventData() is IotHubDeviceDeletedEventData); - IotHubDeviceDeletedEventData eventData = (IotHubDeviceDeletedEventData)events[0].AsSystemEventData(); - Assert.AreEqual("AAAAAAAAAAI=", eventData.Twin.Etag); + Assert.True(events[0].TryGetSystemEvent(out object eventData)); + Assert.AreEqual("AAAAAAAAAAI=", (eventData as IotHubDeviceDeletedEventData).Twin.Etag); } [Test] @@ -1815,9 +1709,8 @@ public void ConsumeCloudEventIoTHubDeviceConnectedEvent() CloudEvent[] events = CloudEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].AsSystemEventData() is IotHubDeviceConnectedEventData); - IotHubDeviceConnectedEventData eventData = (IotHubDeviceConnectedEventData)events[0].AsSystemEventData(); - Assert.AreEqual("EGTESTHUB1", eventData.HubName); + Assert.True(events[0].TryGetSystemEvent(out object eventData)); + Assert.AreEqual("EGTESTHUB1", (eventData as IotHubDeviceConnectedEventData).HubName); } [Test] @@ -1828,9 +1721,8 @@ public void ConsumeCloudEventIoTHubDeviceDisconnectedEvent() CloudEvent[] events = CloudEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].AsSystemEventData() is IotHubDeviceDisconnectedEventData); - IotHubDeviceDisconnectedEventData eventData = (IotHubDeviceDisconnectedEventData)events[0].AsSystemEventData(); - Assert.AreEqual("000000000000000001D4132452F67CE200000002000000000000000000000002", eventData.DeviceConnectionStateEventInfo.SequenceNumber); + Assert.True(events[0].TryGetSystemEvent(out object eventData)); + Assert.AreEqual("000000000000000001D4132452F67CE200000002000000000000000000000002", (eventData as IotHubDeviceDisconnectedEventData).DeviceConnectionStateEventInfo.SequenceNumber); } [Test] @@ -1841,9 +1733,8 @@ public void ConsumeCloudEventIoTHubDeviceTelemetryEvent() CloudEvent[] events = CloudEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].AsSystemEventData() is IotHubDeviceTelemetryEventData); - IotHubDeviceTelemetryEventData eventData = (IotHubDeviceTelemetryEventData)events[0].AsSystemEventData(); - Assert.AreEqual("Active", eventData.Properties["Status"]); + Assert.True(events[0].TryGetSystemEvent(out object eventData)); + Assert.AreEqual("Active", (eventData as IotHubDeviceTelemetryEventData).Properties["Status"]); } #endregion @@ -1856,9 +1747,8 @@ public void ConsumeCloudEventEventGridSubscriptionValidationEvent() CloudEvent[] events = CloudEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].AsSystemEventData() is SubscriptionValidationEventData); - SubscriptionValidationEventData eventData = (SubscriptionValidationEventData)events[0].AsSystemEventData(); - Assert.AreEqual("512d38b6-c7b8-40c8-89fe-f46f9e9622b6", eventData.ValidationCode); + Assert.True(events[0].TryGetSystemEvent(out object eventData)); + Assert.AreEqual("512d38b6-c7b8-40c8-89fe-f46f9e9622b6", (eventData as SubscriptionValidationEventData).ValidationCode); } [Test] @@ -1869,9 +1759,8 @@ public void ConsumeCloudEventEventGridSubscriptionDeletedEvent() CloudEvent[] events = CloudEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].AsSystemEventData() is SubscriptionDeletedEventData); - SubscriptionDeletedEventData eventData = (SubscriptionDeletedEventData)events[0].AsSystemEventData(); - Assert.AreEqual("/subscriptions/id/resourceGroups/rg/providers/Microsoft.EventGrid/topics/topic1/providers/Microsoft.EventGrid/eventSubscriptions/eventsubscription1", eventData.EventSubscriptionId); + Assert.True(events[0].TryGetSystemEvent(out object eventData)); + Assert.AreEqual("/subscriptions/id/resourceGroups/rg/providers/Microsoft.EventGrid/topics/topic1/providers/Microsoft.EventGrid/eventSubscriptions/eventsubscription1", (eventData as SubscriptionDeletedEventData).EventSubscriptionId); } #endregion @@ -1884,9 +1773,8 @@ public void ConsumeCloudEventEventHubCaptureFileCreatedEvent() CloudEvent[] events = CloudEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].AsSystemEventData() is EventHubCaptureFileCreatedEventData); - EventHubCaptureFileCreatedEventData eventData = (EventHubCaptureFileCreatedEventData)events[0].AsSystemEventData(); - Assert.AreEqual("AzureBlockBlob", eventData.FileType); + Assert.True(events[0].TryGetSystemEvent(out object eventData)); + Assert.AreEqual("AzureBlockBlob", (eventData as EventHubCaptureFileCreatedEventData).FileType); } #endregion @@ -1898,17 +1786,16 @@ public void ConsumeCloudEventMachineLearningServicesModelRegisteredEvent() CloudEvent[] events = CloudEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].AsSystemEventData() is MachineLearningServicesModelRegisteredEventData); - MachineLearningServicesModelRegisteredEventData eventData = (MachineLearningServicesModelRegisteredEventData)events[0].AsSystemEventData(); - Assert.AreEqual("sklearn_regression_model", eventData.ModelName); - Assert.AreEqual("3", eventData.ModelVersion); + Assert.True(events[0].TryGetSystemEvent(out object eventData)); + Assert.AreEqual("sklearn_regression_model", (eventData as MachineLearningServicesModelRegisteredEventData).ModelName); + Assert.AreEqual("3", (eventData as MachineLearningServicesModelRegisteredEventData).ModelVersion); - Assert.True(eventData.ModelTags is IDictionary); - IDictionary tags = (IDictionary)eventData.ModelTags; + Assert.True((eventData as MachineLearningServicesModelRegisteredEventData).ModelTags is IDictionary); + IDictionary tags = (IDictionary)(eventData as MachineLearningServicesModelRegisteredEventData).ModelTags; Assert.AreEqual("regression", tags["type"]); - Assert.True(eventData.ModelProperties is IDictionary); - IDictionary properties = (IDictionary)eventData.ModelProperties; + Assert.True((eventData as MachineLearningServicesModelRegisteredEventData).ModelProperties is IDictionary); + IDictionary properties = (IDictionary)(eventData as MachineLearningServicesModelRegisteredEventData).ModelProperties; Assert.AreEqual("test", properties["area"]); } @@ -1919,10 +1806,9 @@ public void ConsumeCloudEventMachineLearningServicesModelDeployedEvent() CloudEvent[] events = CloudEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].AsSystemEventData() is MachineLearningServicesModelDeployedEventData); - MachineLearningServicesModelDeployedEventData eventData = (MachineLearningServicesModelDeployedEventData)events[0].AsSystemEventData(); - Assert.AreEqual("aciservice1", eventData.ServiceName); - Assert.AreEqual(2, eventData.ModelIds.Split(',').Length); + Assert.True(events[0].TryGetSystemEvent(out object eventData)); + Assert.AreEqual("aciservice1", (eventData as MachineLearningServicesModelDeployedEventData).ServiceName); + Assert.AreEqual(2, (eventData as MachineLearningServicesModelDeployedEventData).ModelIds.Split(',').Length); } [Test] @@ -1932,10 +1818,9 @@ public void ConsumeCloudEventMachineLearningServicesRunCompletedEvent() CloudEvent[] events = CloudEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].AsSystemEventData() is MachineLearningServicesRunCompletedEventData); - MachineLearningServicesRunCompletedEventData eventData = (MachineLearningServicesRunCompletedEventData)events[0].AsSystemEventData(); - Assert.AreEqual("AutoML_ad912b2d-6467-4f32-a616-dbe4af6dd8fc", eventData.RunId); - Assert.AreEqual("automl-local-regression", eventData.ExperimentName); + Assert.True(events[0].TryGetSystemEvent(out object eventData)); + Assert.AreEqual("AutoML_ad912b2d-6467-4f32-a616-dbe4af6dd8fc", (eventData as MachineLearningServicesRunCompletedEventData).RunId); + Assert.AreEqual("automl-local-regression", (eventData as MachineLearningServicesRunCompletedEventData).ExperimentName); } [Test] @@ -1945,12 +1830,12 @@ public void ConsumeCloudEventMachineLearningServicesRunStatusChangedEvent() CloudEvent[] events = CloudEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].AsSystemEventData() is MachineLearningServicesRunStatusChangedEventData); - MachineLearningServicesRunStatusChangedEventData eventData = (MachineLearningServicesRunStatusChangedEventData)events[0].AsSystemEventData(); - Assert.AreEqual("AutoML_ad912b2d-6467-4f32-a616-dbe4af6dd8fc", eventData.RunId); - Assert.AreEqual("automl-local-regression", eventData.ExperimentName); - Assert.AreEqual("Running", eventData.RunStatus); - Assert.AreEqual("automl", eventData.RunType); + Assert.True(events[0].TryGetSystemEvent(out object eventData)); + MachineLearningServicesRunStatusChangedEventData sysData = eventData as MachineLearningServicesRunStatusChangedEventData; + Assert.AreEqual("AutoML_ad912b2d-6467-4f32-a616-dbe4af6dd8fc", sysData.RunId); + Assert.AreEqual("automl-local-regression", sysData.ExperimentName); + Assert.AreEqual("Running", sysData.RunStatus); + Assert.AreEqual("automl", sysData.RunType); } [Test] @@ -1960,9 +1845,8 @@ public void ConsumeCloudEventMachineLearningServicesDatasetDriftDetectedEvent() CloudEvent[] events = CloudEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].AsSystemEventData() is MachineLearningServicesDatasetDriftDetectedEventData); - MachineLearningServicesDatasetDriftDetectedEventData eventData = (MachineLearningServicesDatasetDriftDetectedEventData)events[0].AsSystemEventData(); - Assert.AreEqual("copetersDriftMonitor3", eventData.DataDriftName); + Assert.True(events[0].TryGetSystemEvent(out object eventData)); + Assert.AreEqual("copetersDriftMonitor3", (eventData as MachineLearningServicesDatasetDriftDetectedEventData).DataDriftName); } #endregion @@ -1974,9 +1858,8 @@ public void ConsumeCloudEventMapsGeofenceEnteredEvent() CloudEvent[] events = CloudEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].AsSystemEventData() is MapsGeofenceEnteredEventData); - MapsGeofenceEnteredEventData eventData = (MapsGeofenceEnteredEventData)events[0].AsSystemEventData(); - Assert.AreEqual(1.0, eventData.Geometries[0].Distance); + Assert.True(events[0].TryGetSystemEvent(out object eventData)); + Assert.AreEqual(1.0, (eventData as MapsGeofenceEnteredEventData).Geometries[0].Distance); } [Test] @@ -1986,9 +1869,8 @@ public void ConsumeCloudEventMapsGeofenceExitedEvent() CloudEvent[] events = CloudEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].AsSystemEventData() is MapsGeofenceExitedEventData); - MapsGeofenceExitedEventData eventData = (MapsGeofenceExitedEventData)events[0].AsSystemEventData(); - Assert.AreEqual(1.0, eventData.Geometries[0].Distance); + Assert.True(events[0].TryGetSystemEvent(out object eventData)); + Assert.AreEqual(1.0, (eventData as MapsGeofenceExitedEventData).Geometries[0].Distance); } [Test] @@ -1998,9 +1880,8 @@ public void ConsumeCloudEventMapsGeofenceResultEvent() CloudEvent[] events = CloudEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].AsSystemEventData() is MapsGeofenceResultEventData); - MapsGeofenceResultEventData eventData = (MapsGeofenceResultEventData)events[0].AsSystemEventData(); - Assert.AreEqual(1.0, eventData.Geometries[0].Distance); + Assert.True(events[0].TryGetSystemEvent(out object eventData)); + Assert.AreEqual(1.0, (eventData as MapsGeofenceResultEventData).Geometries[0].Distance); } #endregion @@ -2012,10 +1893,9 @@ public void ConsumeCloudEventMediaMediaJobStateChangeEvent() CloudEvent[] events = CloudEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].AsSystemEventData() is MediaJobStateChangeEventData); - MediaJobStateChangeEventData eventData = (MediaJobStateChangeEventData)events[0].AsSystemEventData(); - Assert.AreEqual(MediaJobState.Scheduled, eventData.PreviousState); - Assert.AreEqual(MediaJobState.Processing, eventData.State); + Assert.True(events[0].TryGetSystemEvent(out object eventData)); + Assert.AreEqual(MediaJobState.Scheduled, (eventData as MediaJobStateChangeEventData).PreviousState); + Assert.AreEqual(MediaJobState.Processing, (eventData as MediaJobStateChangeEventData).State); } [Test] @@ -2026,12 +1906,11 @@ public void ConsumeCloudEventMediaJobOutputStateChangeEvent() CloudEvent[] events = CloudEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].AsSystemEventData() is MediaJobOutputStateChangeEventData); - MediaJobOutputStateChangeEventData eventData = (MediaJobOutputStateChangeEventData)events[0].AsSystemEventData(); - Assert.AreEqual(MediaJobState.Scheduled, eventData.PreviousState); - Assert.AreEqual(MediaJobState.Processing, eventData.Output.State); - Assert.True(eventData.Output is MediaJobOutputAsset); - MediaJobOutputAsset outputAsset = (MediaJobOutputAsset)eventData.Output; + Assert.True(events[0].TryGetSystemEvent(out object eventData)); + Assert.AreEqual(MediaJobState.Scheduled, (eventData as MediaJobOutputStateChangeEventData).PreviousState); + Assert.AreEqual(MediaJobState.Processing, (eventData as MediaJobOutputStateChangeEventData).Output.State); + Assert.True((eventData as MediaJobOutputStateChangeEventData).Output is MediaJobOutputAsset); + MediaJobOutputAsset outputAsset = (MediaJobOutputAsset)(eventData as MediaJobOutputStateChangeEventData).Output; Assert.AreEqual("output-2ac2fe75-6557-4de5-ab25-5713b74a6901", outputAsset.AssetName); } @@ -2043,10 +1922,9 @@ public void ConsumeCloudEventMediaJobScheduledEvent() CloudEvent[] events = CloudEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].AsSystemEventData() is MediaJobScheduledEventData); - MediaJobScheduledEventData eventData = (MediaJobScheduledEventData)events[0].AsSystemEventData(); - Assert.AreEqual(MediaJobState.Queued, eventData.PreviousState); - Assert.AreEqual(MediaJobState.Scheduled, eventData.State); + Assert.True(events[0].TryGetSystemEvent(out object eventData)); + Assert.AreEqual(MediaJobState.Queued, (eventData as MediaJobScheduledEventData).PreviousState); + Assert.AreEqual(MediaJobState.Scheduled, (eventData as MediaJobScheduledEventData).State); } [Test] @@ -2057,10 +1935,9 @@ public void ConsumeCloudEventMediaJobProcessingEvent() CloudEvent[] events = CloudEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].AsSystemEventData() is MediaJobProcessingEventData); - MediaJobProcessingEventData eventData = (MediaJobProcessingEventData)events[0].AsSystemEventData(); - Assert.AreEqual(MediaJobState.Scheduled, eventData.PreviousState); - Assert.AreEqual(MediaJobState.Processing, eventData.State); + Assert.True(events[0].TryGetSystemEvent(out object eventData)); + Assert.AreEqual(MediaJobState.Scheduled, (eventData as MediaJobProcessingEventData).PreviousState); + Assert.AreEqual(MediaJobState.Processing, (eventData as MediaJobProcessingEventData).State); } [Test] @@ -2071,10 +1948,9 @@ public void ConsumeCloudEventMediaJobCancelingEvent() CloudEvent[] events = CloudEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].AsSystemEventData() is MediaJobCancelingEventData); - MediaJobCancelingEventData eventData = (MediaJobCancelingEventData)events[0].AsSystemEventData(); - Assert.AreEqual(MediaJobState.Processing, eventData.PreviousState); - Assert.AreEqual(MediaJobState.Canceling, eventData.State); + Assert.True(events[0].TryGetSystemEvent(out object eventData)); + Assert.AreEqual(MediaJobState.Processing, (eventData as MediaJobCancelingEventData).PreviousState); + Assert.AreEqual(MediaJobState.Canceling, (eventData as MediaJobCancelingEventData).State); } [Test] @@ -2084,13 +1960,13 @@ public void ConsumeCloudEventMediaJobFinishedEvent() CloudEvent[] events = CloudEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].AsSystemEventData() is MediaJobFinishedEventData); - MediaJobFinishedEventData eventData = (MediaJobFinishedEventData)events[0].AsSystemEventData(); - Assert.AreEqual(MediaJobState.Processing, eventData.PreviousState); - Assert.AreEqual(MediaJobState.Finished, eventData.State); - Assert.AreEqual(1, eventData.Outputs.Count); - Assert.True(eventData.Outputs[0] is MediaJobOutputAsset); - MediaJobOutputAsset outputAsset = (MediaJobOutputAsset)eventData.Outputs[0]; + Assert.True(events[0].TryGetSystemEvent(out object eventData)); + MediaJobFinishedEventData sysData = eventData as MediaJobFinishedEventData; + Assert.AreEqual(MediaJobState.Processing, sysData.PreviousState); + Assert.AreEqual(MediaJobState.Finished, sysData.State); + Assert.AreEqual(1, sysData.Outputs.Count); + Assert.True(sysData.Outputs[0] is MediaJobOutputAsset); + MediaJobOutputAsset outputAsset = (MediaJobOutputAsset)sysData.Outputs[0]; Assert.AreEqual(MediaJobState.Finished, outputAsset.State); Assert.Null(outputAsset.Error); @@ -2106,14 +1982,15 @@ public void ConsumeCloudEventMediaJobCanceledEvent() CloudEvent[] events = CloudEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].AsSystemEventData() is MediaJobCanceledEventData); - MediaJobCanceledEventData eventData = (MediaJobCanceledEventData)events[0].AsSystemEventData(); - Assert.AreEqual(MediaJobState.Canceling, eventData.PreviousState); - Assert.AreEqual(MediaJobState.Canceled, eventData.State); - Assert.AreEqual(1, eventData.Outputs.Count); - Assert.True(eventData.Outputs[0] is MediaJobOutputAsset); + Assert.True(events[0].TryGetSystemEvent(out object eventData)); + MediaJobCanceledEventData sysData = eventData as MediaJobCanceledEventData; + + Assert.AreEqual(MediaJobState.Canceling, sysData.PreviousState); + Assert.AreEqual(MediaJobState.Canceled, sysData.State); + Assert.AreEqual(1, sysData.Outputs.Count); + Assert.True(sysData.Outputs[0] is MediaJobOutputAsset); - MediaJobOutputAsset outputAsset = (MediaJobOutputAsset)eventData.Outputs[0]; + MediaJobOutputAsset outputAsset = (MediaJobOutputAsset)sysData.Outputs[0]; Assert.AreEqual(MediaJobState.Canceled, outputAsset.State); Assert.AreNotEqual(100, outputAsset.Progress); @@ -2128,17 +2005,18 @@ public void ConsumeCloudEventMediaJobErroredEvent() CloudEvent[] events = CloudEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].AsSystemEventData() is MediaJobErroredEventData); - MediaJobErroredEventData eventData = (MediaJobErroredEventData)events[0].AsSystemEventData(); - Assert.AreEqual(MediaJobState.Processing, eventData.PreviousState); - Assert.AreEqual(MediaJobState.Error, eventData.State); - Assert.AreEqual(1, eventData.Outputs.Count); - Assert.True(eventData.Outputs[0] is MediaJobOutputAsset); + Assert.True(events[0].TryGetSystemEvent(out object eventData)); + MediaJobErroredEventData sysData = eventData as MediaJobErroredEventData; - Assert.AreEqual(MediaJobState.Error, eventData.Outputs[0].State); - Assert.NotNull(eventData.Outputs[0].Error); - Assert.AreEqual(MediaJobErrorCategory.Service, eventData.Outputs[0].Error.Category); - Assert.AreEqual(MediaJobErrorCode.ServiceError, eventData.Outputs[0].Error.Code); + Assert.AreEqual(MediaJobState.Processing, sysData.PreviousState); + Assert.AreEqual(MediaJobState.Error, sysData.State); + Assert.AreEqual(1, sysData.Outputs.Count); + Assert.True(sysData.Outputs[0] is MediaJobOutputAsset); + + Assert.AreEqual(MediaJobState.Error, sysData.Outputs[0].State); + Assert.NotNull(sysData.Outputs[0].Error); + Assert.AreEqual(MediaJobErrorCategory.Service, sysData.Outputs[0].Error.Category); + Assert.AreEqual(MediaJobErrorCode.ServiceError, sysData.Outputs[0].Error.Code); } [Test] @@ -2149,11 +2027,12 @@ public void ConsumeCloudEventMediaJobOutputCanceledEvent() CloudEvent[] events = CloudEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].AsSystemEventData() is MediaJobOutputCanceledEventData); - MediaJobOutputCanceledEventData eventData = (MediaJobOutputCanceledEventData)events[0].AsSystemEventData(); - Assert.AreEqual(MediaJobState.Canceling, eventData.PreviousState); - Assert.AreEqual(MediaJobState.Canceled, eventData.Output.State); - Assert.True(eventData.Output is MediaJobOutputAsset); + Assert.True(events[0].TryGetSystemEvent(out object eventData)); + MediaJobOutputCanceledEventData sysData = eventData as MediaJobOutputCanceledEventData; + + Assert.AreEqual(MediaJobState.Canceling, sysData.PreviousState); + Assert.AreEqual(MediaJobState.Canceled, sysData.Output.State); + Assert.True(sysData.Output is MediaJobOutputAsset); } [Test] @@ -2164,11 +2043,12 @@ public void ConsumeCloudEventMediaJobOutputCancelingEvent() CloudEvent[] events = CloudEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].AsSystemEventData() is MediaJobOutputCancelingEventData); - MediaJobOutputCancelingEventData eventData = (MediaJobOutputCancelingEventData)events[0].AsSystemEventData(); - Assert.AreEqual(MediaJobState.Processing, eventData.PreviousState); - Assert.AreEqual(MediaJobState.Canceling, eventData.Output.State); - Assert.True(eventData.Output is MediaJobOutputAsset); + Assert.True(events[0].TryGetSystemEvent(out object eventData)); + MediaJobOutputCancelingEventData sysData = eventData as MediaJobOutputCancelingEventData; + + Assert.AreEqual(MediaJobState.Processing, sysData.PreviousState); + Assert.AreEqual(MediaJobState.Canceling, sysData.Output.State); + Assert.True(sysData.Output is MediaJobOutputAsset); } [Test] @@ -2179,14 +2059,15 @@ public void ConsumeCloudEventMediaJobOutputErroredEvent() CloudEvent[] events = CloudEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].AsSystemEventData() is MediaJobOutputErroredEventData); - MediaJobOutputErroredEventData eventData = (MediaJobOutputErroredEventData)events[0].AsSystemEventData(); - Assert.AreEqual(MediaJobState.Processing, eventData.PreviousState); - Assert.AreEqual(MediaJobState.Error, eventData.Output.State); - Assert.True(eventData.Output is MediaJobOutputAsset); - Assert.NotNull(eventData.Output.Error); - Assert.AreEqual(MediaJobErrorCategory.Service, eventData.Output.Error.Category); - Assert.AreEqual(MediaJobErrorCode.ServiceError, eventData.Output.Error.Code); + Assert.True(events[0].TryGetSystemEvent(out object eventData)); + MediaJobOutputErroredEventData sysData = eventData as MediaJobOutputErroredEventData; + + Assert.AreEqual(MediaJobState.Processing, sysData.PreviousState); + Assert.AreEqual(MediaJobState.Error, sysData.Output.State); + Assert.True(sysData.Output is MediaJobOutputAsset); + Assert.NotNull(sysData.Output.Error); + Assert.AreEqual(MediaJobErrorCategory.Service, sysData.Output.Error.Category); + Assert.AreEqual(MediaJobErrorCode.ServiceError, sysData.Output.Error.Code); } [Test] @@ -2197,14 +2078,15 @@ public void ConsumeCloudEventMediaJobOutputFinishedEvent() CloudEvent[] events = CloudEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].AsSystemEventData() is MediaJobOutputFinishedEventData); - MediaJobOutputFinishedEventData eventData = (MediaJobOutputFinishedEventData)events[0].AsSystemEventData(); - Assert.AreEqual(MediaJobState.Processing, eventData.PreviousState); - Assert.AreEqual(MediaJobState.Finished, eventData.Output.State); - Assert.True(eventData.Output is MediaJobOutputAsset); - Assert.AreEqual(100, eventData.Output.Progress); + Assert.True(events[0].TryGetSystemEvent(out object eventData)); + MediaJobOutputFinishedEventData sysData = eventData as MediaJobOutputFinishedEventData; - MediaJobOutputAsset outputAsset = (MediaJobOutputAsset)eventData.Output; + Assert.AreEqual(MediaJobState.Processing, sysData.PreviousState); + Assert.AreEqual(MediaJobState.Finished, sysData.Output.State); + Assert.True(sysData.Output is MediaJobOutputAsset); + Assert.AreEqual(100, sysData.Output.Progress); + + MediaJobOutputAsset outputAsset = (MediaJobOutputAsset)sysData.Output; Assert.AreEqual("output-2ac2fe75-6557-4de5-ab25-5713b74a6901", outputAsset.AssetName); } @@ -2216,11 +2098,12 @@ public void ConsumeCloudEventMediaJobOutputProcessingEvent() CloudEvent[] events = CloudEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].AsSystemEventData() is MediaJobOutputProcessingEventData); - MediaJobOutputProcessingEventData eventData = (MediaJobOutputProcessingEventData)events[0].AsSystemEventData(); - Assert.AreEqual(MediaJobState.Scheduled, eventData.PreviousState); - Assert.AreEqual(MediaJobState.Processing, eventData.Output.State); - Assert.True(eventData.Output is MediaJobOutputAsset); + Assert.True(events[0].TryGetSystemEvent(out object eventData)); + MediaJobOutputProcessingEventData sysData = eventData as MediaJobOutputProcessingEventData; + + Assert.AreEqual(MediaJobState.Scheduled, sysData.PreviousState); + Assert.AreEqual(MediaJobState.Processing, sysData.Output.State); + Assert.True(sysData.Output is MediaJobOutputAsset); } [Test] @@ -2231,11 +2114,10 @@ public void ConsumeCloudEventMediaJobOutputScheduledEvent() CloudEvent[] events = CloudEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].AsSystemEventData() is MediaJobOutputScheduledEventData); - MediaJobOutputScheduledEventData eventData = (MediaJobOutputScheduledEventData)events[0].AsSystemEventData(); - Assert.AreEqual(MediaJobState.Queued, eventData.PreviousState); - Assert.AreEqual(MediaJobState.Scheduled, eventData.Output.State); - Assert.True(eventData.Output is MediaJobOutputAsset); + Assert.True(events[0].TryGetSystemEvent(out object eventData)); + Assert.AreEqual(MediaJobState.Queued, (eventData as MediaJobOutputScheduledEventData).PreviousState); + Assert.AreEqual(MediaJobState.Scheduled, (eventData as MediaJobOutputScheduledEventData).Output.State); + Assert.True((eventData as MediaJobOutputScheduledEventData).Output is MediaJobOutputAsset); } [Test] @@ -2246,14 +2128,15 @@ public void ConsumeCloudEventMediaJobOutputProgressEvent() CloudEvent[] events = CloudEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].AsSystemEventData() is MediaJobOutputProgressEventData); - MediaJobOutputProgressEventData eventData = (MediaJobOutputProgressEventData)events[0].AsSystemEventData(); - Assert.AreEqual("TestLabel", eventData.Label); - Assert.AreEqual(50, eventData.Progress); - Assert.True(eventData.JobCorrelationData.ContainsKey("Field1")); - Assert.AreEqual("test1", eventData.JobCorrelationData["Field1"]); - Assert.True(eventData.JobCorrelationData.ContainsKey("Field2")); - Assert.AreEqual("test2", eventData.JobCorrelationData["Field2"]); + Assert.True(events[0].TryGetSystemEvent(out object eventData)); + MediaJobOutputProgressEventData sysData = eventData as MediaJobOutputProgressEventData; + + Assert.AreEqual("TestLabel", sysData.Label); + Assert.AreEqual(50, sysData.Progress); + Assert.True(sysData.JobCorrelationData.ContainsKey("Field1")); + Assert.AreEqual("test1", sysData.JobCorrelationData["Field1"]); + Assert.True(sysData.JobCorrelationData.ContainsKey("Field2")); + Assert.AreEqual("test2", sysData.JobCorrelationData["Field2"]); } [Test] @@ -2264,12 +2147,13 @@ public void ConsumeCloudEventMediaLiveEventEncoderConnectedEvent() CloudEvent[] events = CloudEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].AsSystemEventData() is MediaLiveEventEncoderConnectedEventData); - MediaLiveEventEncoderConnectedEventData eventData = (MediaLiveEventEncoderConnectedEventData)events[0].AsSystemEventData(); - Assert.AreEqual("rtmp://liveevent-ec9d26a8.channel.media.azure.net:1935/live/cb5540b10a5646218c1328be95050c59", eventData.IngestUrl); - Assert.AreEqual("Mystream1", eventData.StreamId); - Assert.AreEqual("", eventData.EncoderIp); - Assert.AreEqual("3557", eventData.EncoderPort); + Assert.True(events[0].TryGetSystemEvent(out object eventData)); + MediaLiveEventEncoderConnectedEventData sysData = eventData as MediaLiveEventEncoderConnectedEventData; + + Assert.AreEqual("rtmp://liveevent-ec9d26a8.channel.media.azure.net:1935/live/cb5540b10a5646218c1328be95050c59", sysData.IngestUrl); + Assert.AreEqual("Mystream1", sysData.StreamId); + Assert.AreEqual("", sysData.EncoderIp); + Assert.AreEqual("3557", sysData.EncoderPort); } [Test] @@ -2280,12 +2164,13 @@ public void ConsumeCloudEventMediaLiveEventConnectionRejectedEvent() CloudEvent[] events = CloudEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].AsSystemEventData() is MediaLiveEventConnectionRejectedEventData); - MediaLiveEventConnectionRejectedEventData eventData = (MediaLiveEventConnectionRejectedEventData)events[0].AsSystemEventData(); - Assert.AreEqual("rtmp://liveevent-ec9d26a8.channel.media.azure.net:1935/live/cb5540b10a5646218c1328be95050c59", eventData.IngestUrl); - Assert.AreEqual("Mystream1", eventData.StreamId); - Assert.AreEqual("", eventData.EncoderIp); - Assert.AreEqual("3557", eventData.EncoderPort); + Assert.True(events[0].TryGetSystemEvent(out object eventData)); + MediaLiveEventConnectionRejectedEventData sysData = eventData as MediaLiveEventConnectionRejectedEventData; + + Assert.AreEqual("rtmp://liveevent-ec9d26a8.channel.media.azure.net:1935/live/cb5540b10a5646218c1328be95050c59", sysData.IngestUrl); + Assert.AreEqual("Mystream1", sysData.StreamId); + Assert.AreEqual("", sysData.EncoderIp); + Assert.AreEqual("3557", sysData.EncoderPort); } [Test] @@ -2295,14 +2180,15 @@ public void ConsumeCloudEventMediaLiveEventEncoderDisconnectedEvent() CloudEvent[] events = CloudEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].AsSystemEventData() is MediaLiveEventEncoderDisconnectedEventData); - MediaLiveEventEncoderDisconnectedEventData eventData = (MediaLiveEventEncoderDisconnectedEventData)events[0].AsSystemEventData(); - Assert.AreEqual("MPE_CLIENT_TERMINATED_SESSION", eventData.ResultCode); + Assert.True(events[0].TryGetSystemEvent(out object eventData)); + MediaLiveEventEncoderDisconnectedEventData sysData = eventData as MediaLiveEventEncoderDisconnectedEventData; + + Assert.AreEqual("MPE_CLIENT_TERMINATED_SESSION", sysData.ResultCode); - Assert.AreEqual("rtmp://liveevent-ec9d26a8.channel.media.azure.net:1935/live/cb5540b10a5646218c1328be95050c59", eventData.IngestUrl); - Assert.AreEqual("Mystream1", eventData.StreamId); - Assert.AreEqual("", eventData.EncoderIp); - Assert.AreEqual("3557", eventData.EncoderPort); + Assert.AreEqual("rtmp://liveevent-ec9d26a8.channel.media.azure.net:1935/live/cb5540b10a5646218c1328be95050c59", sysData.IngestUrl); + Assert.AreEqual("Mystream1", sysData.StreamId); + Assert.AreEqual("", sysData.EncoderIp); + Assert.AreEqual("3557", sysData.EncoderPort); } [Test] @@ -2313,18 +2199,18 @@ public void ConsumeCloudEventMediaLiveEventIncomingStreamReceivedEvent() CloudEvent[] events = CloudEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].AsSystemEventData() is MediaLiveEventIncomingStreamReceivedEventData); - MediaLiveEventIncomingStreamReceivedEventData eventData = (MediaLiveEventIncomingStreamReceivedEventData)events[0].AsSystemEventData(); + Assert.True(events[0].TryGetSystemEvent(out object eventData)); + MediaLiveEventIncomingStreamReceivedEventData sysData = eventData as MediaLiveEventIncomingStreamReceivedEventData; - Assert.AreEqual("rtmp://liveevent-ec9d26a8.channel.media.azure.net:1935/live/cb5540b10a5646218c1328be95050c59", eventData.IngestUrl); - Assert.AreEqual("", eventData.EncoderIp); - Assert.AreEqual("3557", eventData.EncoderPort); - Assert.AreEqual("audio", eventData.TrackType); - Assert.AreEqual("audio_160000", eventData.TrackName); - Assert.AreEqual(160000, eventData.Bitrate); - Assert.AreEqual("66", eventData.Timestamp); - Assert.AreEqual("1950", eventData.Duration); - Assert.AreEqual("1000", eventData.Timescale); + Assert.AreEqual("rtmp://liveevent-ec9d26a8.channel.media.azure.net:1935/live/cb5540b10a5646218c1328be95050c59", sysData.IngestUrl); + Assert.AreEqual("", sysData.EncoderIp); + Assert.AreEqual("3557", sysData.EncoderPort); + Assert.AreEqual("audio", sysData.TrackType); + Assert.AreEqual("audio_160000", sysData.TrackName); + Assert.AreEqual(160000, sysData.Bitrate); + Assert.AreEqual("66", sysData.Timestamp); + Assert.AreEqual("1950", sysData.Duration); + Assert.AreEqual("1000", sysData.Timescale); } [Test] @@ -2335,14 +2221,15 @@ public void ConsumeCloudEventMediaLiveEventIncomingStreamsOutOfSyncEvent() CloudEvent[] events = CloudEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].AsSystemEventData() is MediaLiveEventIncomingStreamsOutOfSyncEventData); - MediaLiveEventIncomingStreamsOutOfSyncEventData eventData = (MediaLiveEventIncomingStreamsOutOfSyncEventData)events[0].AsSystemEventData(); - Assert.AreEqual("10999", eventData.MinLastTimestamp); - Assert.AreEqual("video", eventData.TypeOfStreamWithMinLastTimestamp); - Assert.AreEqual("100999", eventData.MaxLastTimestamp); - Assert.AreEqual("audio", eventData.TypeOfStreamWithMaxLastTimestamp); - Assert.AreEqual("1000", eventData.TimescaleOfMinLastTimestamp); - Assert.AreEqual("1000", eventData.TimescaleOfMaxLastTimestamp); + Assert.True(events[0].TryGetSystemEvent(out object eventData)); + MediaLiveEventIncomingStreamsOutOfSyncEventData sysData = eventData as MediaLiveEventIncomingStreamsOutOfSyncEventData; + + Assert.AreEqual("10999", sysData.MinLastTimestamp); + Assert.AreEqual("video", sysData.TypeOfStreamWithMinLastTimestamp); + Assert.AreEqual("100999", sysData.MaxLastTimestamp); + Assert.AreEqual("audio", sysData.TypeOfStreamWithMaxLastTimestamp); + Assert.AreEqual("1000", sysData.TimescaleOfMinLastTimestamp); + Assert.AreEqual("1000", sysData.TimescaleOfMaxLastTimestamp); } [Test] @@ -2353,13 +2240,14 @@ public void ConsumeCloudEventMediaLiveEventIncomingVideoStreamsOutOfSyncEvent() CloudEvent[] events = CloudEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].AsSystemEventData() is MediaLiveEventIncomingVideoStreamsOutOfSyncEventData); - MediaLiveEventIncomingVideoStreamsOutOfSyncEventData eventData = (MediaLiveEventIncomingVideoStreamsOutOfSyncEventData)events[0].AsSystemEventData(); - Assert.AreEqual("10999", eventData.FirstTimestamp); - Assert.AreEqual("2000", eventData.FirstDuration); - Assert.AreEqual("100999", eventData.SecondTimestamp); - Assert.AreEqual("2000", eventData.SecondDuration); - Assert.AreEqual("1000", eventData.Timescale); + Assert.True(events[0].TryGetSystemEvent(out object eventData)); + MediaLiveEventIncomingVideoStreamsOutOfSyncEventData sysData = eventData as MediaLiveEventIncomingVideoStreamsOutOfSyncEventData; + + Assert.AreEqual("10999", sysData.FirstTimestamp); + Assert.AreEqual("2000", sysData.FirstDuration); + Assert.AreEqual("100999", sysData.SecondTimestamp); + Assert.AreEqual("2000", sysData.SecondDuration); + Assert.AreEqual("1000", sysData.Timescale); } [Test] @@ -2370,14 +2258,15 @@ public void ConsumeCloudEventMediaLiveEventIncomingDataChunkDroppedEvent() CloudEvent[] events = CloudEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].AsSystemEventData() is MediaLiveEventIncomingDataChunkDroppedEventData); - MediaLiveEventIncomingDataChunkDroppedEventData eventData = (MediaLiveEventIncomingDataChunkDroppedEventData)events[0].AsSystemEventData(); - Assert.AreEqual("8999", eventData.Timestamp); - Assert.AreEqual("video", eventData.TrackType); - Assert.AreEqual("video1", eventData.TrackName); - Assert.AreEqual(2500000, eventData.Bitrate); - Assert.AreEqual("1000", eventData.Timescale); - Assert.AreEqual("FragmentDrop_OverlapTimestamp", eventData.ResultCode); + Assert.True(events[0].TryGetSystemEvent(out object eventData)); + MediaLiveEventIncomingDataChunkDroppedEventData sysData = eventData as MediaLiveEventIncomingDataChunkDroppedEventData; + + Assert.AreEqual("8999", sysData.Timestamp); + Assert.AreEqual("video", sysData.TrackType); + Assert.AreEqual("video1", sysData.TrackName); + Assert.AreEqual(2500000, sysData.Bitrate); + Assert.AreEqual("1000", sysData.Timescale); + Assert.AreEqual("FragmentDrop_OverlapTimestamp", sysData.ResultCode); } [Test] @@ -2388,20 +2277,21 @@ public void ConsumeCloudEventMediaLiveEventIngestHeartbeatEvent() CloudEvent[] events = CloudEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].AsSystemEventData() is MediaLiveEventIngestHeartbeatEventData); - MediaLiveEventIngestHeartbeatEventData eventData = (MediaLiveEventIngestHeartbeatEventData)events[0].AsSystemEventData(); - Assert.AreEqual("video", eventData.TrackType); - Assert.AreEqual("video", eventData.TrackName); - Assert.AreEqual(2500000, eventData.Bitrate); - Assert.AreEqual(500726, eventData.IncomingBitrate); - Assert.AreEqual("11999", eventData.LastTimestamp); - Assert.AreEqual("1000", eventData.Timescale); - Assert.AreEqual(0, eventData.OverlapCount); - Assert.AreEqual(0, eventData.DiscontinuityCount); - Assert.AreEqual(0, eventData.NonincreasingCount); - Assert.True(eventData.UnexpectedBitrate); - Assert.AreEqual("Running", eventData.State); - Assert.False(eventData.Healthy); + Assert.True(events[0].TryGetSystemEvent(out object eventData)); + MediaLiveEventIngestHeartbeatEventData sysData = eventData as MediaLiveEventIngestHeartbeatEventData; + + Assert.AreEqual("video", sysData.TrackType); + Assert.AreEqual("video", sysData.TrackName); + Assert.AreEqual(2500000, sysData.Bitrate); + Assert.AreEqual(500726, sysData.IncomingBitrate); + Assert.AreEqual("11999", sysData.LastTimestamp); + Assert.AreEqual("1000", sysData.Timescale); + Assert.AreEqual(0, sysData.OverlapCount); + Assert.AreEqual(0, sysData.DiscontinuityCount); + Assert.AreEqual(0, sysData.NonincreasingCount); + Assert.True(sysData.UnexpectedBitrate); + Assert.AreEqual("Running", sysData.State); + Assert.False(sysData.Healthy); } [Test] @@ -2412,15 +2302,16 @@ public void ConsumeCloudEventMediaLiveEventTrackDiscontinuityDetectedEvent() CloudEvent[] events = CloudEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].AsSystemEventData() is MediaLiveEventTrackDiscontinuityDetectedEventData); - MediaLiveEventTrackDiscontinuityDetectedEventData eventData = (MediaLiveEventTrackDiscontinuityDetectedEventData)events[0].AsSystemEventData(); - Assert.AreEqual("video", eventData.TrackType); - Assert.AreEqual("video", eventData.TrackName); - Assert.AreEqual(2500000, eventData.Bitrate); - Assert.AreEqual("10999", eventData.PreviousTimestamp); - Assert.AreEqual("14999", eventData.NewTimestamp); - Assert.AreEqual("1000", eventData.Timescale); - Assert.AreEqual("4000", eventData.DiscontinuityGap); + Assert.True(events[0].TryGetSystemEvent(out object eventData)); + MediaLiveEventTrackDiscontinuityDetectedEventData sysData = eventData as MediaLiveEventTrackDiscontinuityDetectedEventData; + + Assert.AreEqual("video", sysData.TrackType); + Assert.AreEqual("video", sysData.TrackName); + Assert.AreEqual(2500000, sysData.Bitrate); + Assert.AreEqual("10999", sysData.PreviousTimestamp); + Assert.AreEqual("14999", sysData.NewTimestamp); + Assert.AreEqual("1000", sysData.Timescale); + Assert.AreEqual("4000", sysData.DiscontinuityGap); } #endregion @@ -2433,9 +2324,10 @@ public void ConsumeCloudEventResourceWriteSuccessEvent() CloudEvent[] events = CloudEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].AsSystemEventData() is ResourceWriteSuccessData); - ResourceWriteSuccessData eventData = (ResourceWriteSuccessData)events[0].AsSystemEventData(); - Assert.AreEqual("72f988bf-86f1-41af-91ab-2d7cd011db47", eventData.TenantId); + Assert.True(events[0].TryGetSystemEvent(out object eventData)); + ResourceWriteSuccessData sysData = eventData as ResourceWriteSuccessData; + + Assert.AreEqual("72f988bf-86f1-41af-91ab-2d7cd011db47", sysData.TenantId); } [Test] @@ -2446,9 +2338,9 @@ public void ConsumeCloudEventResourceWriteFailureEvent() CloudEvent[] events = CloudEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].AsSystemEventData() is ResourceWriteFailureData); - ResourceWriteFailureData eventData = (ResourceWriteFailureData)events[0].AsSystemEventData(); - Assert.AreEqual("72f988bf-86f1-41af-91ab-2d7cd011db47", eventData.TenantId); + Assert.True(events[0].TryGetSystemEvent(out object eventData)); + + Assert.AreEqual("72f988bf-86f1-41af-91ab-2d7cd011db47", (eventData as ResourceWriteFailureData).TenantId); } [Test] @@ -2459,9 +2351,8 @@ public void ConsumeCloudEventResourceWriteCancelEvent() CloudEvent[] events = CloudEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].AsSystemEventData() is ResourceWriteCancelData); - ResourceWriteCancelData eventData = (ResourceWriteCancelData)events[0].AsSystemEventData(); - Assert.AreEqual("72f988bf-86f1-41af-91ab-2d7cd011db47", eventData.TenantId); + Assert.True(events[0].TryGetSystemEvent(out object eventData)); + Assert.AreEqual("72f988bf-86f1-41af-91ab-2d7cd011db47", (eventData as ResourceWriteCancelData).TenantId); } [Test] @@ -2472,9 +2363,8 @@ public void ConsumeCloudEventResourceDeleteSuccessEvent() CloudEvent[] events = CloudEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].AsSystemEventData() is ResourceDeleteSuccessData); - ResourceDeleteSuccessData eventData = (ResourceDeleteSuccessData)events[0].AsSystemEventData(); - Assert.AreEqual("72f988bf-86f1-41af-91ab-2d7cd011db47", eventData.TenantId); + Assert.True(events[0].TryGetSystemEvent(out object eventData)); + Assert.AreEqual("72f988bf-86f1-41af-91ab-2d7cd011db47", (eventData as ResourceDeleteSuccessData).TenantId); } [Test] @@ -2485,9 +2375,8 @@ public void ConsumeCloudEventResourceDeleteFailureEvent() CloudEvent[] events = CloudEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].AsSystemEventData() is ResourceDeleteFailureData); - ResourceDeleteFailureData eventData = (ResourceDeleteFailureData)events[0].AsSystemEventData(); - Assert.AreEqual("72f988bf-86f1-41af-91ab-2d7cd011db47", eventData.TenantId); + Assert.True(events[0].TryGetSystemEvent(out object eventData)); + Assert.AreEqual("72f988bf-86f1-41af-91ab-2d7cd011db47", (eventData as ResourceDeleteFailureData).TenantId); } [Test] @@ -2498,9 +2387,8 @@ public void ConsumeCloudEventResourceDeleteCancelEvent() CloudEvent[] events = CloudEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].AsSystemEventData() is ResourceDeleteCancelData); - ResourceDeleteCancelData eventData = (ResourceDeleteCancelData)events[0].AsSystemEventData(); - Assert.AreEqual("72f988bf-86f1-41af-91ab-2d7cd011db47", eventData.TenantId); + Assert.True(events[0].TryGetSystemEvent(out object eventData)); + Assert.AreEqual("72f988bf-86f1-41af-91ab-2d7cd011db47", (eventData as ResourceDeleteCancelData).TenantId); } [Test] @@ -2511,9 +2399,8 @@ public void ConsumeCloudEventResourceActionSuccessEvent() CloudEvent[] events = CloudEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].AsSystemEventData() is ResourceActionSuccessData); - ResourceActionSuccessData eventData = (ResourceActionSuccessData)events[0].AsSystemEventData(); - Assert.AreEqual("72f988bf-86f1-41af-91ab-2d7cd011db47", eventData.TenantId); + Assert.True(events[0].TryGetSystemEvent(out object eventData)); + Assert.AreEqual("72f988bf-86f1-41af-91ab-2d7cd011db47", (eventData as ResourceActionSuccessData).TenantId); } [Test] @@ -2524,9 +2411,8 @@ public void ConsumeCloudEventResourceActionFailureEvent() CloudEvent[] events = CloudEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].AsSystemEventData() is ResourceActionFailureData); - ResourceActionFailureData eventData = (ResourceActionFailureData)events[0].AsSystemEventData(); - Assert.AreEqual("72f988bf-86f1-41af-91ab-2d7cd011db47", eventData.TenantId); + Assert.True(events[0].TryGetSystemEvent(out object eventData)); + Assert.AreEqual("72f988bf-86f1-41af-91ab-2d7cd011db47", (eventData as ResourceActionFailureData).TenantId); } [Test] @@ -2537,9 +2423,8 @@ public void ConsumeCloudEventResourceActionCancelEvent() CloudEvent[] events = CloudEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].AsSystemEventData() is ResourceActionCancelData); - ResourceActionCancelData eventData = (ResourceActionCancelData)events[0].AsSystemEventData(); - Assert.AreEqual("72f988bf-86f1-41af-91ab-2d7cd011db47", eventData.TenantId); + Assert.True(events[0].TryGetSystemEvent(out object eventData)); + Assert.AreEqual("72f988bf-86f1-41af-91ab-2d7cd011db47", (eventData as ResourceActionCancelData).TenantId); } #endregion @@ -2552,9 +2437,8 @@ public void ConsumeCloudEventServiceBusActiveMessagesAvailableWithNoListenersEve CloudEvent[] events = CloudEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].AsSystemEventData() is ServiceBusActiveMessagesAvailableWithNoListenersEventData); - ServiceBusActiveMessagesAvailableWithNoListenersEventData eventData = (ServiceBusActiveMessagesAvailableWithNoListenersEventData)events[0].AsSystemEventData(); - Assert.AreEqual("testns1", eventData.NamespaceName); + Assert.True(events[0].TryGetSystemEvent(out object eventData)); + Assert.AreEqual("testns1", (eventData as ServiceBusActiveMessagesAvailableWithNoListenersEventData).NamespaceName); } [Test] @@ -2565,9 +2449,8 @@ public void ConsumeCloudEventServiceBusDeadletterMessagesAvailableWithNoListener CloudEvent[] events = CloudEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].AsSystemEventData() is ServiceBusDeadletterMessagesAvailableWithNoListenersEventData); - ServiceBusDeadletterMessagesAvailableWithNoListenersEventData eventData = (ServiceBusDeadletterMessagesAvailableWithNoListenersEventData)events[0].AsSystemEventData(); - Assert.AreEqual("testns1", eventData.NamespaceName); + Assert.True(events[0].TryGetSystemEvent(out object eventData)); + Assert.AreEqual("testns1", (eventData as ServiceBusDeadletterMessagesAvailableWithNoListenersEventData).NamespaceName); } #endregion @@ -2580,9 +2463,8 @@ public void ConsumeCloudEventStorageBlobCreatedEvent() CloudEvent[] events = CloudEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].AsSystemEventData() is StorageBlobCreatedEventData); - StorageBlobCreatedEventData eventData = (StorageBlobCreatedEventData)events[0].AsSystemEventData(); - Assert.AreEqual("https://myaccount.blob.core.windows.net/testcontainer/file1.txt", eventData.Url); + Assert.True(events[0].TryGetSystemEvent(out object eventData)); + Assert.AreEqual("https://myaccount.blob.core.windows.net/testcontainer/file1.txt", (eventData as StorageBlobCreatedEventData).Url); } [Test] @@ -2593,9 +2475,8 @@ public void ConsumeCloudEventStorageBlobDeletedEvent() CloudEvent[] events = CloudEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].AsSystemEventData() is StorageBlobDeletedEventData); - StorageBlobDeletedEventData eventData = (StorageBlobDeletedEventData)events[0].AsSystemEventData(); - Assert.AreEqual("https://example.blob.core.windows.net/testcontainer/testfile.txt", eventData.Url); + Assert.True(events[0].TryGetSystemEvent(out object eventData)); + Assert.AreEqual("https://example.blob.core.windows.net/testcontainer/testfile.txt", (eventData as StorageBlobDeletedEventData).Url); } [Test] @@ -2606,9 +2487,8 @@ public void ConsumeCloudEventStorageBlobRenamedEvent() CloudEvent[] events = CloudEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].AsSystemEventData() is StorageBlobRenamedEventData); - StorageBlobRenamedEventData eventData = (StorageBlobRenamedEventData)events[0].AsSystemEventData(); - Assert.AreEqual("https://myaccount.blob.core.windows.net/testcontainer/testfile.txt", eventData.DestinationUrl); + Assert.True(events[0].TryGetSystemEvent(out object eventData)); + Assert.AreEqual("https://myaccount.blob.core.windows.net/testcontainer/testfile.txt", (eventData as StorageBlobRenamedEventData).DestinationUrl); } [Test] @@ -2619,9 +2499,8 @@ public void ConsumeCloudEventStorageDirectoryCreatedEvent() CloudEvent[] events = CloudEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].AsSystemEventData() is StorageDirectoryCreatedEventData); - StorageDirectoryCreatedEventData eventData = (StorageDirectoryCreatedEventData)events[0].AsSystemEventData(); - Assert.AreEqual("https://myaccount.blob.core.windows.net/testcontainer/testDir", eventData.Url); + Assert.True(events[0].TryGetSystemEvent(out object eventData)); + Assert.AreEqual("https://myaccount.blob.core.windows.net/testcontainer/testDir", (eventData as StorageDirectoryCreatedEventData).Url); } [Test] @@ -2632,9 +2511,8 @@ public void ConsumeCloudEventStorageDirectoryDeletedEvent() CloudEvent[] events = CloudEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].AsSystemEventData() is StorageDirectoryDeletedEventData); - StorageDirectoryDeletedEventData eventData = (StorageDirectoryDeletedEventData)events[0].AsSystemEventData(); - Assert.AreEqual("https://example.blob.core.windows.net/testcontainer/testDir", eventData.Url); + Assert.True(events[0].TryGetSystemEvent(out object eventData)); + Assert.AreEqual("https://example.blob.core.windows.net/testcontainer/testDir", (eventData as StorageDirectoryDeletedEventData).Url); } [Test] @@ -2645,9 +2523,8 @@ public void ConsumeCloudEventStorageDirectoryRenamedEvent() CloudEvent[] events = CloudEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].AsSystemEventData() is StorageDirectoryRenamedEventData); - StorageDirectoryRenamedEventData eventData = (StorageDirectoryRenamedEventData)events[0].AsSystemEventData(); - Assert.AreEqual("https://example.blob.core.windows.net/testcontainer/testDir", eventData.DestinationUrl); + Assert.True(events[0].TryGetSystemEvent(out object eventData)); + Assert.AreEqual("https://example.blob.core.windows.net/testcontainer/testDir", (eventData as StorageDirectoryRenamedEventData).DestinationUrl); } #endregion @@ -2661,9 +2538,8 @@ public void ConsumeCloudEventWebAppUpdatedEvent() CloudEvent[] events = CloudEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].AsSystemEventData() is WebAppUpdatedEventData); - WebAppUpdatedEventData eventData = (WebAppUpdatedEventData)events[0].AsSystemEventData(); - Assert.AreEqual(siteName, eventData.Name); + Assert.True(events[0].TryGetSystemEvent(out object eventData)); + Assert.AreEqual(siteName, (eventData as WebAppUpdatedEventData).Name); } [Test] @@ -2675,9 +2551,8 @@ public void ConsumeCloudEventWebBackupOperationStartedEvent() CloudEvent[] events = CloudEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].AsSystemEventData() is WebBackupOperationStartedEventData); - WebBackupOperationStartedEventData eventData = (WebBackupOperationStartedEventData)events[0].AsSystemEventData(); - Assert.AreEqual(siteName, eventData.Name); + Assert.True(events[0].TryGetSystemEvent(out object eventData)); + Assert.AreEqual(siteName, (eventData as WebBackupOperationStartedEventData).Name); } [Test] @@ -2689,9 +2564,8 @@ public void ConsumeCloudEventWebBackupOperationCompletedEvent() CloudEvent[] events = CloudEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].AsSystemEventData() is WebBackupOperationCompletedEventData); - WebBackupOperationCompletedEventData eventData = (WebBackupOperationCompletedEventData)events[0].AsSystemEventData(); - Assert.AreEqual(siteName, eventData.Name); + Assert.True(events[0].TryGetSystemEvent(out object eventData)); + Assert.AreEqual(siteName, (eventData as WebBackupOperationCompletedEventData).Name); } [Test] @@ -2702,9 +2576,8 @@ public void ConsumeCloudEventWebBackupOperationFailedEvent() CloudEvent[] events = CloudEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].AsSystemEventData() is WebBackupOperationFailedEventData); - WebBackupOperationFailedEventData eventData = (WebBackupOperationFailedEventData)events[0].AsSystemEventData(); - Assert.AreEqual(siteName, eventData.Name); + Assert.True(events[0].TryGetSystemEvent(out object eventData)); + Assert.AreEqual(siteName, (eventData as WebBackupOperationFailedEventData).Name); } [Test] @@ -2716,9 +2589,8 @@ public void ConsumeCloudEventWebRestoreOperationStartedEvent() CloudEvent[] events = CloudEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].AsSystemEventData() is WebRestoreOperationStartedEventData); - WebRestoreOperationStartedEventData eventData = (WebRestoreOperationStartedEventData)events[0].AsSystemEventData(); - Assert.AreEqual(siteName, eventData.Name); + Assert.True(events[0].TryGetSystemEvent(out object eventData)); + Assert.AreEqual(siteName, (eventData as WebRestoreOperationStartedEventData).Name); } [Test] @@ -2730,9 +2602,8 @@ public void ConsumeCloudEventWebRestoreOperationCompletedEvent() CloudEvent[] events = CloudEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].AsSystemEventData() is WebRestoreOperationCompletedEventData); - WebRestoreOperationCompletedEventData eventData = (WebRestoreOperationCompletedEventData)events[0].AsSystemEventData(); - Assert.AreEqual(siteName, eventData.Name); + Assert.True(events[0].TryGetSystemEvent(out object eventData)); + Assert.AreEqual(siteName, (eventData as WebRestoreOperationCompletedEventData).Name); } [Test] @@ -2744,9 +2615,8 @@ public void ConsumeCloudEventWebRestoreOperationFailedEvent() CloudEvent[] events = CloudEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].AsSystemEventData() is WebRestoreOperationFailedEventData); - WebRestoreOperationFailedEventData eventData = (WebRestoreOperationFailedEventData)events[0].AsSystemEventData(); - Assert.AreEqual(siteName, eventData.Name); + Assert.True(events[0].TryGetSystemEvent(out object eventData)); + Assert.AreEqual(siteName, (eventData as WebRestoreOperationFailedEventData).Name); } [Test] @@ -2758,9 +2628,8 @@ public void ConsumeCloudEventWebSlotSwapStartedEvent() CloudEvent[] events = CloudEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].AsSystemEventData() is WebSlotSwapStartedEventData); - WebSlotSwapStartedEventData eventData = (WebSlotSwapStartedEventData)events[0].AsSystemEventData(); - Assert.AreEqual(siteName, eventData.Name); + Assert.True(events[0].TryGetSystemEvent(out object eventData)); + Assert.AreEqual(siteName, (eventData as WebSlotSwapStartedEventData).Name); } [Test] @@ -2772,9 +2641,8 @@ public void ConsumeCloudEventWebSlotSwapCompletedEvent() CloudEvent[] events = CloudEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].AsSystemEventData() is WebSlotSwapCompletedEventData); - WebSlotSwapCompletedEventData eventData = (WebSlotSwapCompletedEventData)events[0].AsSystemEventData(); - Assert.AreEqual(siteName, eventData.Name); + Assert.True(events[0].TryGetSystemEvent(out object eventData)); + Assert.AreEqual(siteName, (eventData as WebSlotSwapCompletedEventData).Name); } [Test] @@ -2786,9 +2654,8 @@ public void ConsumeCloudEventWebSlotSwapFailedEvent() CloudEvent[] events = CloudEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].AsSystemEventData() is WebSlotSwapFailedEventData); - WebSlotSwapFailedEventData eventData = (WebSlotSwapFailedEventData)events[0].AsSystemEventData(); - Assert.AreEqual(siteName, eventData.Name); + Assert.True(events[0].TryGetSystemEvent(out object eventData)); + Assert.AreEqual(siteName, (eventData as WebSlotSwapFailedEventData).Name); } [Test] @@ -2800,9 +2667,8 @@ public void ConsumeCloudEventWebSlotSwapWithPreviewStartedEvent() CloudEvent[] events = CloudEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].AsSystemEventData() is WebSlotSwapWithPreviewStartedEventData); - WebSlotSwapWithPreviewStartedEventData eventData = (WebSlotSwapWithPreviewStartedEventData)events[0].AsSystemEventData(); - Assert.AreEqual(siteName, eventData.Name); + Assert.True(events[0].TryGetSystemEvent(out object eventData)); + Assert.AreEqual(siteName, (eventData as WebSlotSwapWithPreviewStartedEventData).Name); } [Test] @@ -2814,9 +2680,8 @@ public void ConsumeCloudEventWebSlotSwapWithPreviewCancelledEvent() CloudEvent[] events = CloudEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].AsSystemEventData() is WebSlotSwapWithPreviewCancelledEventData); - WebSlotSwapWithPreviewCancelledEventData eventData = (WebSlotSwapWithPreviewCancelledEventData)events[0].AsSystemEventData(); - Assert.AreEqual(siteName, eventData.Name); + Assert.True(events[0].TryGetSystemEvent(out object eventData)); + Assert.AreEqual(siteName, (eventData as WebSlotSwapWithPreviewCancelledEventData).Name); } [Test] @@ -2828,9 +2693,8 @@ public void ConsumeCloudEventWebAppServicePlanUpdatedEvent() CloudEvent[] events = CloudEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].AsSystemEventData() is WebAppServicePlanUpdatedEventData); - WebAppServicePlanUpdatedEventData eventData = (WebAppServicePlanUpdatedEventData)events[0].AsSystemEventData(); - Assert.AreEqual(planName, eventData.Name); + Assert.True(events[0].TryGetSystemEvent(out object eventData)); + Assert.AreEqual(planName, (eventData as WebAppServicePlanUpdatedEventData).Name); } #endregion #endregion diff --git a/sdk/eventgrid/Azure.Messaging.EventGrid/tests/EventGridClientLiveTests.cs b/sdk/eventgrid/Azure.Messaging.EventGrid/tests/EventGridClientLiveTests.cs index ae7b7e330fcb..06203f8be1cf 100644 --- a/sdk/eventgrid/Azure.Messaging.EventGrid/tests/EventGridClientLiveTests.cs +++ b/sdk/eventgrid/Azure.Messaging.EventGrid/tests/EventGridClientLiveTests.cs @@ -174,7 +174,7 @@ public async Task CanPublishCloudEventWithBinaryData() "record", "Microsoft.MockPublisher.TestEvent", // testing IEnumerable - Enumerable.Repeat((byte)1, 1), + Enumerable.Repeat((byte)1, 1).ToArray(), "test/binary") { Id = Recording.Random.NewGuid().ToString(), diff --git a/sdk/eventgrid/Azure.Messaging.EventGrid/tests/Samples/Sample2_ParseAndDeserializeEvents.cs b/sdk/eventgrid/Azure.Messaging.EventGrid/tests/Samples/Sample2_ParseAndDeserializeEvents.cs index 79a126174e5e..de760cbcb575 100644 --- a/sdk/eventgrid/Azure.Messaging.EventGrid/tests/Samples/Sample2_ParseAndDeserializeEvents.cs +++ b/sdk/eventgrid/Azure.Messaging.EventGrid/tests/Samples/Sample2_ParseAndDeserializeEvents.cs @@ -3,7 +3,6 @@ using System; using System.Text.Json; -using System.Threading.Tasks; using Azure.Core.Serialization; using Azure.Core.TestFramework; using Azure.Messaging.EventGrid.SystemEvents; @@ -32,9 +31,9 @@ public void NonGenericReceiveAndDeserializeEventGridEvents() foreach (EventGridEvent egEvent in egEvents) { // If the event is a system event, AsSystemEventData() should return the correct system event type - if (egEvent.IsSystemEvent) + if (egEvent.TryGetSystemEventData(out object systemEvent)) { - switch (egEvent.AsSystemEventData()) + switch (systemEvent) { case SubscriptionValidationEventData subscriptionValidated: Console.WriteLine(subscriptionValidated.ValidationCode); @@ -71,7 +70,7 @@ public void NonGenericReceiveAndDeserializeEventGridEvents() // This sample demonstrates how to parse CloudEvents from JSON and access event data using GetData() [Test] - public async Task GenericReceiveAndDeserializeEventGridEvents() + public void GenericReceiveAndDeserializeEventGridEvents() { // Example of a custom ObjectSerializer used to deserialize the event payload JsonObjectSerializer myCustomSerializer = new JsonObjectSerializer( @@ -98,7 +97,7 @@ public async Task GenericReceiveAndDeserializeEventGridEvents() break; case "MyApp.Models.CustomEventType": // One can also specify a custom ObjectSerializer as needed to deserialize the payload correctly - TestPayload testPayload = await cloudEvent.GetDataAsync(myCustomSerializer); + TestPayload testPayload = cloudEvent.GetData(); Console.WriteLine(testPayload.Name); break; case SystemEventNames.StorageBlobDeleted: From 185214783d96ca6510650c6bafc7fa072325c580 Mon Sep 17 00:00:00 2001 From: JoshLove-msft <54595583+JoshLove-msft@users.noreply.github.com> Date: Tue, 2 Feb 2021 14:59:50 -0800 Subject: [PATCH 2/5] Update samples --- .../samples/Sample1_PublishEventsToTopic.md | 13 ++++++++++--- .../samples/Sample3_ParseAndDeserializeEvents.md | 4 ++-- .../Samples/Sample1_SendEventsToTopicAndDomain.cs | 8 ++++++-- .../Samples/Sample2_ParseAndDeserializeEvents.cs | 2 +- 4 files changed, 19 insertions(+), 8 deletions(-) diff --git a/sdk/eventgrid/Azure.Messaging.EventGrid/samples/Sample1_PublishEventsToTopic.md b/sdk/eventgrid/Azure.Messaging.EventGrid/samples/Sample1_PublishEventsToTopic.md index c888c78c3540..f7eee89931ba 100644 --- a/sdk/eventgrid/Azure.Messaging.EventGrid/samples/Sample1_PublishEventsToTopic.md +++ b/sdk/eventgrid/Azure.Messaging.EventGrid/samples/Sample1_PublishEventsToTopic.md @@ -24,11 +24,18 @@ EventGridPublisherClient client = new EventGridPublisherClient( new AzureKeyCredential(topicAccessKey), clientOptions); ``` -Event Grid also supports authenticating with a shared access signature which allows for providing access to a resource that expires by a certain time without sharing your access key: -```C# Snippet:CreateWithSas +Event Grid also supports authenticating with a shared access signature which allows for providing access to a resource that expires by a certain time without sharing your access key. +Generally, the workflow would be that one application would generate the SAS string and hand off the string to another application that would consume the string. +Generate the SAS: +```C# Snippet:GenerateSas var builder = new EventGridSasBuilder(new Uri(topicEndpoint), DateTimeOffset.Now.AddHours(1)); var keyCredential = new AzureKeyCredential(topicAccessKey); -var sasCredential = new AzureSasCredential(builder.GenerateSas(keyCredential)); +string sasToken = builder.GenerateSas(keyCredential); +``` + +Here is how it would be used from the consumer's perspective: +```C# Snippet:AuthenticateWithSas +var sasCredential = new AzureSasCredential(sasToken); EventGridPublisherClient client = new EventGridPublisherClient( new Uri(topicEndpoint), sasCredential); diff --git a/sdk/eventgrid/Azure.Messaging.EventGrid/samples/Sample3_ParseAndDeserializeEvents.md b/sdk/eventgrid/Azure.Messaging.EventGrid/samples/Sample3_ParseAndDeserializeEvents.md index 536fa15a821e..1d7b54456ea9 100644 --- a/sdk/eventgrid/Azure.Messaging.EventGrid/samples/Sample3_ParseAndDeserializeEvents.md +++ b/sdk/eventgrid/Azure.Messaging.EventGrid/samples/Sample3_ParseAndDeserializeEvents.md @@ -52,8 +52,8 @@ foreach (CloudEvent cloudEvent in cloudEvents) } ``` -### Using `AsSytemEventData()` -If expecting mostly system events, it may be cleaner to switch on `AsSytemEventData()` and use pattern matching to act on the individual events. In the case where there are unrecognized event types, one can use the returned `BinaryData` to examine the event data. +### Using `TryGetSystemEventData()` +If expecting mostly system events, it may be cleaner to switch on `TryGetSystemEventData()` and use pattern matching to act on the individual events. If an event is not a system event, the method will return false and the out parameter will be null. As a caveat, if you are using a custom event type with an EventType value that later gets added as a system event by the service and SDK, the return value of `TryGetSystemEventData` would change from `false` to `true`. This could come up if you are pre-emptively creating your own custom events for events that are already being sent by the service, but have not yet been added to the SDK. In this case, it is better to use the generic `GetData` method so that your code flow doesn't change after upgrading. ```C# Snippet:DeserializePayloadUsingAsSystemEventData foreach (EventGridEvent egEvent in egEvents) diff --git a/sdk/eventgrid/Azure.Messaging.EventGrid/tests/Samples/Sample1_SendEventsToTopicAndDomain.cs b/sdk/eventgrid/Azure.Messaging.EventGrid/tests/Samples/Sample1_SendEventsToTopicAndDomain.cs index 8599c5fac161..283a3cf3d47a 100644 --- a/sdk/eventgrid/Azure.Messaging.EventGrid/tests/Samples/Sample1_SendEventsToTopicAndDomain.cs +++ b/sdk/eventgrid/Azure.Messaging.EventGrid/tests/Samples/Sample1_SendEventsToTopicAndDomain.cs @@ -55,10 +55,14 @@ public async Task AuthenticateWithSasToken() // Create the publisher client using an AzureKeyCredential // Custom topic should be configured to accept events of the Event Grid schema - #region Snippet:CreateWithSas + #region Snippet:GenerateSas var builder = new EventGridSasBuilder(new Uri(topicEndpoint), DateTimeOffset.Now.AddHours(1)); var keyCredential = new AzureKeyCredential(topicAccessKey); - var sasCredential = new AzureSasCredential(builder.GenerateSas(keyCredential)); + string sasToken = builder.GenerateSas(keyCredential); + #endregion + + #region Snippet:AuthenticateWithSas + var sasCredential = new AzureSasCredential(sasToken); EventGridPublisherClient client = new EventGridPublisherClient( new Uri(topicEndpoint), sasCredential); diff --git a/sdk/eventgrid/Azure.Messaging.EventGrid/tests/Samples/Sample2_ParseAndDeserializeEvents.cs b/sdk/eventgrid/Azure.Messaging.EventGrid/tests/Samples/Sample2_ParseAndDeserializeEvents.cs index de760cbcb575..9d5c2cadd134 100644 --- a/sdk/eventgrid/Azure.Messaging.EventGrid/tests/Samples/Sample2_ParseAndDeserializeEvents.cs +++ b/sdk/eventgrid/Azure.Messaging.EventGrid/tests/Samples/Sample2_ParseAndDeserializeEvents.cs @@ -30,7 +30,7 @@ public void NonGenericReceiveAndDeserializeEventGridEvents() #region Snippet:DeserializePayloadUsingAsSystemEventData foreach (EventGridEvent egEvent in egEvents) { - // If the event is a system event, AsSystemEventData() should return the correct system event type + // If the event is a system event, TryGetSystemEventData() will return the deserialized system event if (egEvent.TryGetSystemEventData(out object systemEvent)) { switch (systemEvent) From fd8ccf0c11bdc6a6db6124d435d4b33cea1bd68a Mon Sep 17 00:00:00 2001 From: JoshLove-msft <54595583+JoshLove-msft@users.noreply.github.com> Date: Tue, 2 Feb 2021 15:22:51 -0800 Subject: [PATCH 3/5] fix --- .../Azure.Messaging.EventGrid/README.md | 15 +- ...zure.Messaging.EventGrid.netstandard2.0.cs | 12 +- .../Sample3_ParseAndDeserializeEvents.md | 2 +- .../src/Customization/CloudEvent.cs | 12 +- ...taExtensions.cs => EventGridExtensions.cs} | 2 +- .../tests/ConsumeEventTests.cs | 158 +++++++++--------- 6 files changed, 105 insertions(+), 96 deletions(-) rename sdk/eventgrid/Azure.Messaging.EventGrid/src/Customization/{BinaryDataExtensions.cs => EventGridExtensions.cs} (98%) diff --git a/sdk/eventgrid/Azure.Messaging.EventGrid/README.md b/sdk/eventgrid/Azure.Messaging.EventGrid/README.md index c21e3ce52f75..0aefeb56caff 100644 --- a/sdk/eventgrid/Azure.Messaging.EventGrid/README.md +++ b/sdk/eventgrid/Azure.Messaging.EventGrid/README.md @@ -169,7 +169,12 @@ Using `CloudEvent`: // Parse the JSON payload into a list of events using CloudEvent.Parse CloudEvent[] cloudEvents = CloudEvent.Parse(jsonPayloadSampleTwo); ``` -From here, one can access the event data by deserializing to a specific type using `GetData()` and passing in a custom serializer if necessary. Below is an example calling `GetData()` using CloudEvents. In order to deserialize to the correct type, the `EventType` property (`Type` for CloudEvents) helps distinguish between different events. +From here, one can access the event data by deserializing to a specific type using `GetData()` and passing in a custom serializer if necessary. Calling `GetData()` will either return a deserialized system event (an event generated by an Azure service), or the event data wrapped in `BinaryData`, which represents the serialized JSON event data as bytes. + +Using `GetData()`: + +Below is an example calling `GetData()` for CloudEvents. In order to deserialize to the correct type, the `EventType` property (`Type` for CloudEvents) helps distinguish between different events. Custom event data should be deserialized using the generic method `GetData()`. There is also an overload for `GetData()` that accepts a custom `ObjectSerializer` to deserialize the event data. + ```C# Snippet:DeserializePayloadUsingGenericGetData foreach (CloudEvent cloudEvent in cloudEvents) { @@ -193,11 +198,15 @@ foreach (CloudEvent cloudEvent in cloudEvents) } } ``` -Below is an example using the `IsSystemEvent` property along with `AsSystemEventData()` to deserialize system events. + +Using `TryGetSystemEventData()`: + +If expecting mostly system events, it may be cleaner to switch on `TryGetSystemEventData()` and use pattern matching to act on the individual events. If an event is not a system event, the method will return false and the out parameter will be null. As a caveat, if you are using a custom event type with an EventType value that later gets added as a system event by the service and SDK, the return value of `TryGetSystemEventData` would change from `false` to `true`. This could come up if you are pre-emptively creating your own custom events for events that are already being sent by the service, but have not yet been added to the SDK. In this case, it is better to use the generic `GetData` method so that your code flow doesn't change after upgrading. + ```C# Snippet:DeserializePayloadUsingAsSystemEventData foreach (EventGridEvent egEvent in egEvents) { - // If the event is a system event, AsSystemEventData() should return the correct system event type + // If the event is a system event, TryGetSystemEventData() will return the deserialized system event if (egEvent.TryGetSystemEventData(out object systemEvent)) { switch (systemEvent) diff --git a/sdk/eventgrid/Azure.Messaging.EventGrid/api/Azure.Messaging.EventGrid.netstandard2.0.cs b/sdk/eventgrid/Azure.Messaging.EventGrid/api/Azure.Messaging.EventGrid.netstandard2.0.cs index c34b2a046e6b..ef5589b65aa4 100644 --- a/sdk/eventgrid/Azure.Messaging.EventGrid/api/Azure.Messaging.EventGrid.netstandard2.0.cs +++ b/sdk/eventgrid/Azure.Messaging.EventGrid/api/Azure.Messaging.EventGrid.netstandard2.0.cs @@ -1,10 +1,5 @@ namespace Azure.Messaging.EventGrid { - public static partial class BinaryDataExtensions - { - public static Azure.Messaging.EventGrid.CloudEvent ToCloudEvent(this System.BinaryData binaryData) { throw null; } - public static Azure.Messaging.EventGrid.EventGridEvent ToEventGridEvent(this System.BinaryData binaryData) { throw null; } - } public partial class CloudEvent { public CloudEvent(string source, string type, object data, System.Type dataSerializationType = null) { } @@ -20,7 +15,7 @@ public CloudEvent(string source, string type, System.ReadOnlyMemory data, public System.BinaryData GetData() { throw null; } public T GetData(System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; } public static Azure.Messaging.EventGrid.CloudEvent[] Parse(string requestContent) { throw null; } - public bool TryGetSystemEvent(out object systemEvent) { throw null; } + public bool TryGetSystemEventData(out object eventData) { throw null; } } public partial class EventGridEvent { @@ -36,6 +31,11 @@ public EventGridEvent(string subject, string eventType, string dataVersion, obje public static Azure.Messaging.EventGrid.EventGridEvent[] Parse(string requestContent) { throw null; } public bool TryGetSystemEventData(out object eventData) { throw null; } } + public static partial class EventGridExtensions + { + public static Azure.Messaging.EventGrid.CloudEvent ToCloudEvent(this System.BinaryData binaryData) { throw null; } + public static Azure.Messaging.EventGrid.EventGridEvent ToEventGridEvent(this System.BinaryData binaryData) { throw null; } + } public partial class EventGridPublisherClient { protected EventGridPublisherClient() { } diff --git a/sdk/eventgrid/Azure.Messaging.EventGrid/samples/Sample3_ParseAndDeserializeEvents.md b/sdk/eventgrid/Azure.Messaging.EventGrid/samples/Sample3_ParseAndDeserializeEvents.md index 1d7b54456ea9..949a21e20f62 100644 --- a/sdk/eventgrid/Azure.Messaging.EventGrid/samples/Sample3_ParseAndDeserializeEvents.md +++ b/sdk/eventgrid/Azure.Messaging.EventGrid/samples/Sample3_ParseAndDeserializeEvents.md @@ -58,7 +58,7 @@ If expecting mostly system events, it may be cleaner to switch on `TryGetSystemE ```C# Snippet:DeserializePayloadUsingAsSystemEventData foreach (EventGridEvent egEvent in egEvents) { - // If the event is a system event, AsSystemEventData() should return the correct system event type + // If the event is a system event, TryGetSystemEventData() will return the deserialized system event if (egEvent.TryGetSystemEventData(out object systemEvent)) { switch (systemEvent) diff --git a/sdk/eventgrid/Azure.Messaging.EventGrid/src/Customization/CloudEvent.cs b/sdk/eventgrid/Azure.Messaging.EventGrid/src/Customization/CloudEvent.cs index 645fc972b79e..6b465e4bef4a 100644 --- a/sdk/eventgrid/Azure.Messaging.EventGrid/src/Customization/CloudEvent.cs +++ b/sdk/eventgrid/Azure.Messaging.EventGrid/src/Customization/CloudEvent.cs @@ -117,15 +117,15 @@ internal CloudEvent(CloudEventInternal cloudEventInternal) /// /// Gets whether or not the event is a System defined event and returns the deserialized - /// system event via out parameter. + /// system event data via out parameter. /// - /// If the event is a system event, this will be populated - /// with the deserialized system event. Otherwise, this will be null. + /// If the event is a system event, this will be populated + /// with the deserialized system event data. Otherwise, this will be null. /// Whether or not the event is a system event. - public bool TryGetSystemEvent(out object systemEvent) + public bool TryGetSystemEventData(out object eventData) { - systemEvent = SystemEventExtensions.AsSystemEventData(Type, SerializedData); - return systemEvent != null; + eventData = SystemEventExtensions.AsSystemEventData(Type, SerializedData); + return eventData != null; } /// diff --git a/sdk/eventgrid/Azure.Messaging.EventGrid/src/Customization/BinaryDataExtensions.cs b/sdk/eventgrid/Azure.Messaging.EventGrid/src/Customization/EventGridExtensions.cs similarity index 98% rename from sdk/eventgrid/Azure.Messaging.EventGrid/src/Customization/BinaryDataExtensions.cs rename to sdk/eventgrid/Azure.Messaging.EventGrid/src/Customization/EventGridExtensions.cs index 83692bce0b15..095672cf88cc 100644 --- a/sdk/eventgrid/Azure.Messaging.EventGrid/src/Customization/BinaryDataExtensions.cs +++ b/sdk/eventgrid/Azure.Messaging.EventGrid/src/Customization/EventGridExtensions.cs @@ -12,7 +12,7 @@ namespace Azure.Messaging.EventGrid /// /// Extension methods for BinaryData to use for parsing JSON-encoded events. /// - public static class BinaryDataExtensions + public static class EventGridExtensions { /// /// Given a single JSON-encoded event, parses the event envelope and returns an EventGridEvent. diff --git a/sdk/eventgrid/Azure.Messaging.EventGrid/tests/ConsumeEventTests.cs b/sdk/eventgrid/Azure.Messaging.EventGrid/tests/ConsumeEventTests.cs index 9d58a14fbcf0..0460d59aaefc 100644 --- a/sdk/eventgrid/Azure.Messaging.EventGrid/tests/ConsumeEventTests.cs +++ b/sdk/eventgrid/Azure.Messaging.EventGrid/tests/ConsumeEventTests.cs @@ -1415,7 +1415,7 @@ public void ConsumeMultipleCloudEventsInSameBatch() Assert.AreEqual(5, events.Length); foreach (CloudEvent cloudEvent in events) { - if (cloudEvent.TryGetSystemEvent(out object eventData)) + if (cloudEvent.TryGetSystemEventData(out object eventData)) { switch (eventData) { @@ -1433,12 +1433,12 @@ public void ConsumeMultipleCloudEventsInSameBatch() { case "BinaryDataType": Assert.AreEqual(Convert.ToBase64String(cloudEvent.GetData().ToArray()), "ZGF0YQ=="); - Assert.IsFalse(cloudEvent.TryGetSystemEvent(out var _)); + Assert.IsFalse(cloudEvent.TryGetSystemEventData(out var _)); break; case "Contoso.Items.ItemReceived": ContosoItemReceivedEventData itemReceived = cloudEvent.GetData().ToObject(camelCaseSerializer); Assert.AreEqual("512d38b6-c7b8-40c8-89fe-f46f9e9622b6", itemReceived.ItemSku); - Assert.IsFalse(cloudEvent.TryGetSystemEvent(out var _)); + Assert.IsFalse(cloudEvent.TryGetSystemEventData(out var _)); break; } } @@ -1610,7 +1610,7 @@ public void ConsumeCloudEventAppConfigurationKeyValueDeletedEvent() CloudEvent[] events = CloudEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].TryGetSystemEvent(out object eventData)); + Assert.True(events[0].TryGetSystemEventData(out object eventData)); Assert.AreEqual("key1", (eventData as AppConfigurationKeyValueDeletedEventData).Key); } @@ -1621,7 +1621,7 @@ public void ConsumeCloudEventAppConfigurationKeyValueModifiedEvent() CloudEvent[] events = CloudEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].TryGetSystemEvent(out object eventData)); + Assert.True(events[0].TryGetSystemEventData(out object eventData)); Assert.AreEqual("key1", (eventData as AppConfigurationKeyValueModifiedEventData).Key); } #endregion @@ -1635,7 +1635,7 @@ public void ConsumeCloudEventContainerRegistryImagePushedEvent() CloudEvent[] events = CloudEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].TryGetSystemEvent(out object eventData)); + Assert.True(events[0].TryGetSystemEventData(out object eventData)); Assert.AreEqual("127.0.0.1", (eventData as ContainerRegistryImagePushedEventData).Request.Addr); } @@ -1647,7 +1647,7 @@ public void ConsumeCloudEventContainerRegistryImageDeletedEvent() CloudEvent[] events = CloudEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].TryGetSystemEvent(out object eventData)); + Assert.True(events[0].TryGetSystemEventData(out object eventData)); Assert.AreEqual("testactor", (eventData as ContainerRegistryImageDeletedEventData).Actor.Name); } @@ -1659,7 +1659,7 @@ public void ConsumeCloudEventContainerRegistryChartDeletedEvent() CloudEvent[] events = CloudEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].TryGetSystemEvent(out object eventData)); + Assert.True(events[0].TryGetSystemEventData(out object eventData)); Assert.AreEqual("mediatype1", (eventData as ContainerRegistryChartDeletedEventData).Target.MediaType); } @@ -1671,7 +1671,7 @@ public void ConsumeCloudEventContainerRegistryChartPushedEvent() CloudEvent[] events = CloudEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].TryGetSystemEvent(out object eventData)); + Assert.True(events[0].TryGetSystemEventData(out object eventData)); Assert.AreEqual("mediatype1", (eventData as ContainerRegistryChartPushedEventData).Target.MediaType); } #endregion @@ -1685,7 +1685,7 @@ public void ConsumeCloudEventIoTHubDeviceCreatedEvent() CloudEvent[] events = CloudEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].TryGetSystemEvent(out object eventData)); + Assert.True(events[0].TryGetSystemEventData(out object eventData)); Assert.AreEqual("enabled", (eventData as IotHubDeviceCreatedEventData).Twin.Status); } @@ -1697,7 +1697,7 @@ public void ConsumeCloudEventIoTHubDeviceDeletedEvent() CloudEvent[] events = CloudEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].TryGetSystemEvent(out object eventData)); + Assert.True(events[0].TryGetSystemEventData(out object eventData)); Assert.AreEqual("AAAAAAAAAAI=", (eventData as IotHubDeviceDeletedEventData).Twin.Etag); } @@ -1709,7 +1709,7 @@ public void ConsumeCloudEventIoTHubDeviceConnectedEvent() CloudEvent[] events = CloudEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].TryGetSystemEvent(out object eventData)); + Assert.True(events[0].TryGetSystemEventData(out object eventData)); Assert.AreEqual("EGTESTHUB1", (eventData as IotHubDeviceConnectedEventData).HubName); } @@ -1721,7 +1721,7 @@ public void ConsumeCloudEventIoTHubDeviceDisconnectedEvent() CloudEvent[] events = CloudEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].TryGetSystemEvent(out object eventData)); + Assert.True(events[0].TryGetSystemEventData(out object eventData)); Assert.AreEqual("000000000000000001D4132452F67CE200000002000000000000000000000002", (eventData as IotHubDeviceDisconnectedEventData).DeviceConnectionStateEventInfo.SequenceNumber); } @@ -1733,7 +1733,7 @@ public void ConsumeCloudEventIoTHubDeviceTelemetryEvent() CloudEvent[] events = CloudEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].TryGetSystemEvent(out object eventData)); + Assert.True(events[0].TryGetSystemEventData(out object eventData)); Assert.AreEqual("Active", (eventData as IotHubDeviceTelemetryEventData).Properties["Status"]); } #endregion @@ -1747,7 +1747,7 @@ public void ConsumeCloudEventEventGridSubscriptionValidationEvent() CloudEvent[] events = CloudEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].TryGetSystemEvent(out object eventData)); + Assert.True(events[0].TryGetSystemEventData(out object eventData)); Assert.AreEqual("512d38b6-c7b8-40c8-89fe-f46f9e9622b6", (eventData as SubscriptionValidationEventData).ValidationCode); } @@ -1759,7 +1759,7 @@ public void ConsumeCloudEventEventGridSubscriptionDeletedEvent() CloudEvent[] events = CloudEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].TryGetSystemEvent(out object eventData)); + Assert.True(events[0].TryGetSystemEventData(out object eventData)); Assert.AreEqual("/subscriptions/id/resourceGroups/rg/providers/Microsoft.EventGrid/topics/topic1/providers/Microsoft.EventGrid/eventSubscriptions/eventsubscription1", (eventData as SubscriptionDeletedEventData).EventSubscriptionId); } #endregion @@ -1773,7 +1773,7 @@ public void ConsumeCloudEventEventHubCaptureFileCreatedEvent() CloudEvent[] events = CloudEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].TryGetSystemEvent(out object eventData)); + Assert.True(events[0].TryGetSystemEventData(out object eventData)); Assert.AreEqual("AzureBlockBlob", (eventData as EventHubCaptureFileCreatedEventData).FileType); } #endregion @@ -1786,7 +1786,7 @@ public void ConsumeCloudEventMachineLearningServicesModelRegisteredEvent() CloudEvent[] events = CloudEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].TryGetSystemEvent(out object eventData)); + Assert.True(events[0].TryGetSystemEventData(out object eventData)); Assert.AreEqual("sklearn_regression_model", (eventData as MachineLearningServicesModelRegisteredEventData).ModelName); Assert.AreEqual("3", (eventData as MachineLearningServicesModelRegisteredEventData).ModelVersion); @@ -1806,7 +1806,7 @@ public void ConsumeCloudEventMachineLearningServicesModelDeployedEvent() CloudEvent[] events = CloudEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].TryGetSystemEvent(out object eventData)); + Assert.True(events[0].TryGetSystemEventData(out object eventData)); Assert.AreEqual("aciservice1", (eventData as MachineLearningServicesModelDeployedEventData).ServiceName); Assert.AreEqual(2, (eventData as MachineLearningServicesModelDeployedEventData).ModelIds.Split(',').Length); } @@ -1818,7 +1818,7 @@ public void ConsumeCloudEventMachineLearningServicesRunCompletedEvent() CloudEvent[] events = CloudEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].TryGetSystemEvent(out object eventData)); + Assert.True(events[0].TryGetSystemEventData(out object eventData)); Assert.AreEqual("AutoML_ad912b2d-6467-4f32-a616-dbe4af6dd8fc", (eventData as MachineLearningServicesRunCompletedEventData).RunId); Assert.AreEqual("automl-local-regression", (eventData as MachineLearningServicesRunCompletedEventData).ExperimentName); } @@ -1830,7 +1830,7 @@ public void ConsumeCloudEventMachineLearningServicesRunStatusChangedEvent() CloudEvent[] events = CloudEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].TryGetSystemEvent(out object eventData)); + Assert.True(events[0].TryGetSystemEventData(out object eventData)); MachineLearningServicesRunStatusChangedEventData sysData = eventData as MachineLearningServicesRunStatusChangedEventData; Assert.AreEqual("AutoML_ad912b2d-6467-4f32-a616-dbe4af6dd8fc", sysData.RunId); Assert.AreEqual("automl-local-regression", sysData.ExperimentName); @@ -1845,7 +1845,7 @@ public void ConsumeCloudEventMachineLearningServicesDatasetDriftDetectedEvent() CloudEvent[] events = CloudEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].TryGetSystemEvent(out object eventData)); + Assert.True(events[0].TryGetSystemEventData(out object eventData)); Assert.AreEqual("copetersDriftMonitor3", (eventData as MachineLearningServicesDatasetDriftDetectedEventData).DataDriftName); } #endregion @@ -1858,7 +1858,7 @@ public void ConsumeCloudEventMapsGeofenceEnteredEvent() CloudEvent[] events = CloudEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].TryGetSystemEvent(out object eventData)); + Assert.True(events[0].TryGetSystemEventData(out object eventData)); Assert.AreEqual(1.0, (eventData as MapsGeofenceEnteredEventData).Geometries[0].Distance); } @@ -1869,7 +1869,7 @@ public void ConsumeCloudEventMapsGeofenceExitedEvent() CloudEvent[] events = CloudEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].TryGetSystemEvent(out object eventData)); + Assert.True(events[0].TryGetSystemEventData(out object eventData)); Assert.AreEqual(1.0, (eventData as MapsGeofenceExitedEventData).Geometries[0].Distance); } @@ -1880,7 +1880,7 @@ public void ConsumeCloudEventMapsGeofenceResultEvent() CloudEvent[] events = CloudEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].TryGetSystemEvent(out object eventData)); + Assert.True(events[0].TryGetSystemEventData(out object eventData)); Assert.AreEqual(1.0, (eventData as MapsGeofenceResultEventData).Geometries[0].Distance); } #endregion @@ -1893,7 +1893,7 @@ public void ConsumeCloudEventMediaMediaJobStateChangeEvent() CloudEvent[] events = CloudEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].TryGetSystemEvent(out object eventData)); + Assert.True(events[0].TryGetSystemEventData(out object eventData)); Assert.AreEqual(MediaJobState.Scheduled, (eventData as MediaJobStateChangeEventData).PreviousState); Assert.AreEqual(MediaJobState.Processing, (eventData as MediaJobStateChangeEventData).State); } @@ -1906,7 +1906,7 @@ public void ConsumeCloudEventMediaJobOutputStateChangeEvent() CloudEvent[] events = CloudEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].TryGetSystemEvent(out object eventData)); + Assert.True(events[0].TryGetSystemEventData(out object eventData)); Assert.AreEqual(MediaJobState.Scheduled, (eventData as MediaJobOutputStateChangeEventData).PreviousState); Assert.AreEqual(MediaJobState.Processing, (eventData as MediaJobOutputStateChangeEventData).Output.State); Assert.True((eventData as MediaJobOutputStateChangeEventData).Output is MediaJobOutputAsset); @@ -1922,7 +1922,7 @@ public void ConsumeCloudEventMediaJobScheduledEvent() CloudEvent[] events = CloudEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].TryGetSystemEvent(out object eventData)); + Assert.True(events[0].TryGetSystemEventData(out object eventData)); Assert.AreEqual(MediaJobState.Queued, (eventData as MediaJobScheduledEventData).PreviousState); Assert.AreEqual(MediaJobState.Scheduled, (eventData as MediaJobScheduledEventData).State); } @@ -1935,7 +1935,7 @@ public void ConsumeCloudEventMediaJobProcessingEvent() CloudEvent[] events = CloudEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].TryGetSystemEvent(out object eventData)); + Assert.True(events[0].TryGetSystemEventData(out object eventData)); Assert.AreEqual(MediaJobState.Scheduled, (eventData as MediaJobProcessingEventData).PreviousState); Assert.AreEqual(MediaJobState.Processing, (eventData as MediaJobProcessingEventData).State); } @@ -1948,7 +1948,7 @@ public void ConsumeCloudEventMediaJobCancelingEvent() CloudEvent[] events = CloudEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].TryGetSystemEvent(out object eventData)); + Assert.True(events[0].TryGetSystemEventData(out object eventData)); Assert.AreEqual(MediaJobState.Processing, (eventData as MediaJobCancelingEventData).PreviousState); Assert.AreEqual(MediaJobState.Canceling, (eventData as MediaJobCancelingEventData).State); } @@ -1960,7 +1960,7 @@ public void ConsumeCloudEventMediaJobFinishedEvent() CloudEvent[] events = CloudEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].TryGetSystemEvent(out object eventData)); + Assert.True(events[0].TryGetSystemEventData(out object eventData)); MediaJobFinishedEventData sysData = eventData as MediaJobFinishedEventData; Assert.AreEqual(MediaJobState.Processing, sysData.PreviousState); Assert.AreEqual(MediaJobState.Finished, sysData.State); @@ -1982,7 +1982,7 @@ public void ConsumeCloudEventMediaJobCanceledEvent() CloudEvent[] events = CloudEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].TryGetSystemEvent(out object eventData)); + Assert.True(events[0].TryGetSystemEventData(out object eventData)); MediaJobCanceledEventData sysData = eventData as MediaJobCanceledEventData; Assert.AreEqual(MediaJobState.Canceling, sysData.PreviousState); @@ -2005,7 +2005,7 @@ public void ConsumeCloudEventMediaJobErroredEvent() CloudEvent[] events = CloudEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].TryGetSystemEvent(out object eventData)); + Assert.True(events[0].TryGetSystemEventData(out object eventData)); MediaJobErroredEventData sysData = eventData as MediaJobErroredEventData; Assert.AreEqual(MediaJobState.Processing, sysData.PreviousState); @@ -2027,7 +2027,7 @@ public void ConsumeCloudEventMediaJobOutputCanceledEvent() CloudEvent[] events = CloudEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].TryGetSystemEvent(out object eventData)); + Assert.True(events[0].TryGetSystemEventData(out object eventData)); MediaJobOutputCanceledEventData sysData = eventData as MediaJobOutputCanceledEventData; Assert.AreEqual(MediaJobState.Canceling, sysData.PreviousState); @@ -2043,7 +2043,7 @@ public void ConsumeCloudEventMediaJobOutputCancelingEvent() CloudEvent[] events = CloudEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].TryGetSystemEvent(out object eventData)); + Assert.True(events[0].TryGetSystemEventData(out object eventData)); MediaJobOutputCancelingEventData sysData = eventData as MediaJobOutputCancelingEventData; Assert.AreEqual(MediaJobState.Processing, sysData.PreviousState); @@ -2059,7 +2059,7 @@ public void ConsumeCloudEventMediaJobOutputErroredEvent() CloudEvent[] events = CloudEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].TryGetSystemEvent(out object eventData)); + Assert.True(events[0].TryGetSystemEventData(out object eventData)); MediaJobOutputErroredEventData sysData = eventData as MediaJobOutputErroredEventData; Assert.AreEqual(MediaJobState.Processing, sysData.PreviousState); @@ -2078,7 +2078,7 @@ public void ConsumeCloudEventMediaJobOutputFinishedEvent() CloudEvent[] events = CloudEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].TryGetSystemEvent(out object eventData)); + Assert.True(events[0].TryGetSystemEventData(out object eventData)); MediaJobOutputFinishedEventData sysData = eventData as MediaJobOutputFinishedEventData; Assert.AreEqual(MediaJobState.Processing, sysData.PreviousState); @@ -2098,7 +2098,7 @@ public void ConsumeCloudEventMediaJobOutputProcessingEvent() CloudEvent[] events = CloudEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].TryGetSystemEvent(out object eventData)); + Assert.True(events[0].TryGetSystemEventData(out object eventData)); MediaJobOutputProcessingEventData sysData = eventData as MediaJobOutputProcessingEventData; Assert.AreEqual(MediaJobState.Scheduled, sysData.PreviousState); @@ -2114,7 +2114,7 @@ public void ConsumeCloudEventMediaJobOutputScheduledEvent() CloudEvent[] events = CloudEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].TryGetSystemEvent(out object eventData)); + Assert.True(events[0].TryGetSystemEventData(out object eventData)); Assert.AreEqual(MediaJobState.Queued, (eventData as MediaJobOutputScheduledEventData).PreviousState); Assert.AreEqual(MediaJobState.Scheduled, (eventData as MediaJobOutputScheduledEventData).Output.State); Assert.True((eventData as MediaJobOutputScheduledEventData).Output is MediaJobOutputAsset); @@ -2128,7 +2128,7 @@ public void ConsumeCloudEventMediaJobOutputProgressEvent() CloudEvent[] events = CloudEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].TryGetSystemEvent(out object eventData)); + Assert.True(events[0].TryGetSystemEventData(out object eventData)); MediaJobOutputProgressEventData sysData = eventData as MediaJobOutputProgressEventData; Assert.AreEqual("TestLabel", sysData.Label); @@ -2147,7 +2147,7 @@ public void ConsumeCloudEventMediaLiveEventEncoderConnectedEvent() CloudEvent[] events = CloudEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].TryGetSystemEvent(out object eventData)); + Assert.True(events[0].TryGetSystemEventData(out object eventData)); MediaLiveEventEncoderConnectedEventData sysData = eventData as MediaLiveEventEncoderConnectedEventData; Assert.AreEqual("rtmp://liveevent-ec9d26a8.channel.media.azure.net:1935/live/cb5540b10a5646218c1328be95050c59", sysData.IngestUrl); @@ -2164,7 +2164,7 @@ public void ConsumeCloudEventMediaLiveEventConnectionRejectedEvent() CloudEvent[] events = CloudEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].TryGetSystemEvent(out object eventData)); + Assert.True(events[0].TryGetSystemEventData(out object eventData)); MediaLiveEventConnectionRejectedEventData sysData = eventData as MediaLiveEventConnectionRejectedEventData; Assert.AreEqual("rtmp://liveevent-ec9d26a8.channel.media.azure.net:1935/live/cb5540b10a5646218c1328be95050c59", sysData.IngestUrl); @@ -2180,7 +2180,7 @@ public void ConsumeCloudEventMediaLiveEventEncoderDisconnectedEvent() CloudEvent[] events = CloudEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].TryGetSystemEvent(out object eventData)); + Assert.True(events[0].TryGetSystemEventData(out object eventData)); MediaLiveEventEncoderDisconnectedEventData sysData = eventData as MediaLiveEventEncoderDisconnectedEventData; Assert.AreEqual("MPE_CLIENT_TERMINATED_SESSION", sysData.ResultCode); @@ -2199,7 +2199,7 @@ public void ConsumeCloudEventMediaLiveEventIncomingStreamReceivedEvent() CloudEvent[] events = CloudEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].TryGetSystemEvent(out object eventData)); + Assert.True(events[0].TryGetSystemEventData(out object eventData)); MediaLiveEventIncomingStreamReceivedEventData sysData = eventData as MediaLiveEventIncomingStreamReceivedEventData; Assert.AreEqual("rtmp://liveevent-ec9d26a8.channel.media.azure.net:1935/live/cb5540b10a5646218c1328be95050c59", sysData.IngestUrl); @@ -2221,7 +2221,7 @@ public void ConsumeCloudEventMediaLiveEventIncomingStreamsOutOfSyncEvent() CloudEvent[] events = CloudEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].TryGetSystemEvent(out object eventData)); + Assert.True(events[0].TryGetSystemEventData(out object eventData)); MediaLiveEventIncomingStreamsOutOfSyncEventData sysData = eventData as MediaLiveEventIncomingStreamsOutOfSyncEventData; Assert.AreEqual("10999", sysData.MinLastTimestamp); @@ -2240,7 +2240,7 @@ public void ConsumeCloudEventMediaLiveEventIncomingVideoStreamsOutOfSyncEvent() CloudEvent[] events = CloudEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].TryGetSystemEvent(out object eventData)); + Assert.True(events[0].TryGetSystemEventData(out object eventData)); MediaLiveEventIncomingVideoStreamsOutOfSyncEventData sysData = eventData as MediaLiveEventIncomingVideoStreamsOutOfSyncEventData; Assert.AreEqual("10999", sysData.FirstTimestamp); @@ -2258,7 +2258,7 @@ public void ConsumeCloudEventMediaLiveEventIncomingDataChunkDroppedEvent() CloudEvent[] events = CloudEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].TryGetSystemEvent(out object eventData)); + Assert.True(events[0].TryGetSystemEventData(out object eventData)); MediaLiveEventIncomingDataChunkDroppedEventData sysData = eventData as MediaLiveEventIncomingDataChunkDroppedEventData; Assert.AreEqual("8999", sysData.Timestamp); @@ -2277,7 +2277,7 @@ public void ConsumeCloudEventMediaLiveEventIngestHeartbeatEvent() CloudEvent[] events = CloudEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].TryGetSystemEvent(out object eventData)); + Assert.True(events[0].TryGetSystemEventData(out object eventData)); MediaLiveEventIngestHeartbeatEventData sysData = eventData as MediaLiveEventIngestHeartbeatEventData; Assert.AreEqual("video", sysData.TrackType); @@ -2302,7 +2302,7 @@ public void ConsumeCloudEventMediaLiveEventTrackDiscontinuityDetectedEvent() CloudEvent[] events = CloudEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].TryGetSystemEvent(out object eventData)); + Assert.True(events[0].TryGetSystemEventData(out object eventData)); MediaLiveEventTrackDiscontinuityDetectedEventData sysData = eventData as MediaLiveEventTrackDiscontinuityDetectedEventData; Assert.AreEqual("video", sysData.TrackType); @@ -2324,7 +2324,7 @@ public void ConsumeCloudEventResourceWriteSuccessEvent() CloudEvent[] events = CloudEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].TryGetSystemEvent(out object eventData)); + Assert.True(events[0].TryGetSystemEventData(out object eventData)); ResourceWriteSuccessData sysData = eventData as ResourceWriteSuccessData; Assert.AreEqual("72f988bf-86f1-41af-91ab-2d7cd011db47", sysData.TenantId); @@ -2338,7 +2338,7 @@ public void ConsumeCloudEventResourceWriteFailureEvent() CloudEvent[] events = CloudEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].TryGetSystemEvent(out object eventData)); + Assert.True(events[0].TryGetSystemEventData(out object eventData)); Assert.AreEqual("72f988bf-86f1-41af-91ab-2d7cd011db47", (eventData as ResourceWriteFailureData).TenantId); } @@ -2351,7 +2351,7 @@ public void ConsumeCloudEventResourceWriteCancelEvent() CloudEvent[] events = CloudEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].TryGetSystemEvent(out object eventData)); + Assert.True(events[0].TryGetSystemEventData(out object eventData)); Assert.AreEqual("72f988bf-86f1-41af-91ab-2d7cd011db47", (eventData as ResourceWriteCancelData).TenantId); } @@ -2363,7 +2363,7 @@ public void ConsumeCloudEventResourceDeleteSuccessEvent() CloudEvent[] events = CloudEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].TryGetSystemEvent(out object eventData)); + Assert.True(events[0].TryGetSystemEventData(out object eventData)); Assert.AreEqual("72f988bf-86f1-41af-91ab-2d7cd011db47", (eventData as ResourceDeleteSuccessData).TenantId); } @@ -2375,7 +2375,7 @@ public void ConsumeCloudEventResourceDeleteFailureEvent() CloudEvent[] events = CloudEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].TryGetSystemEvent(out object eventData)); + Assert.True(events[0].TryGetSystemEventData(out object eventData)); Assert.AreEqual("72f988bf-86f1-41af-91ab-2d7cd011db47", (eventData as ResourceDeleteFailureData).TenantId); } @@ -2387,7 +2387,7 @@ public void ConsumeCloudEventResourceDeleteCancelEvent() CloudEvent[] events = CloudEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].TryGetSystemEvent(out object eventData)); + Assert.True(events[0].TryGetSystemEventData(out object eventData)); Assert.AreEqual("72f988bf-86f1-41af-91ab-2d7cd011db47", (eventData as ResourceDeleteCancelData).TenantId); } @@ -2399,7 +2399,7 @@ public void ConsumeCloudEventResourceActionSuccessEvent() CloudEvent[] events = CloudEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].TryGetSystemEvent(out object eventData)); + Assert.True(events[0].TryGetSystemEventData(out object eventData)); Assert.AreEqual("72f988bf-86f1-41af-91ab-2d7cd011db47", (eventData as ResourceActionSuccessData).TenantId); } @@ -2411,7 +2411,7 @@ public void ConsumeCloudEventResourceActionFailureEvent() CloudEvent[] events = CloudEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].TryGetSystemEvent(out object eventData)); + Assert.True(events[0].TryGetSystemEventData(out object eventData)); Assert.AreEqual("72f988bf-86f1-41af-91ab-2d7cd011db47", (eventData as ResourceActionFailureData).TenantId); } @@ -2423,7 +2423,7 @@ public void ConsumeCloudEventResourceActionCancelEvent() CloudEvent[] events = CloudEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].TryGetSystemEvent(out object eventData)); + Assert.True(events[0].TryGetSystemEventData(out object eventData)); Assert.AreEqual("72f988bf-86f1-41af-91ab-2d7cd011db47", (eventData as ResourceActionCancelData).TenantId); } #endregion @@ -2437,7 +2437,7 @@ public void ConsumeCloudEventServiceBusActiveMessagesAvailableWithNoListenersEve CloudEvent[] events = CloudEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].TryGetSystemEvent(out object eventData)); + Assert.True(events[0].TryGetSystemEventData(out object eventData)); Assert.AreEqual("testns1", (eventData as ServiceBusActiveMessagesAvailableWithNoListenersEventData).NamespaceName); } @@ -2449,7 +2449,7 @@ public void ConsumeCloudEventServiceBusDeadletterMessagesAvailableWithNoListener CloudEvent[] events = CloudEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].TryGetSystemEvent(out object eventData)); + Assert.True(events[0].TryGetSystemEventData(out object eventData)); Assert.AreEqual("testns1", (eventData as ServiceBusDeadletterMessagesAvailableWithNoListenersEventData).NamespaceName); } #endregion @@ -2463,7 +2463,7 @@ public void ConsumeCloudEventStorageBlobCreatedEvent() CloudEvent[] events = CloudEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].TryGetSystemEvent(out object eventData)); + Assert.True(events[0].TryGetSystemEventData(out object eventData)); Assert.AreEqual("https://myaccount.blob.core.windows.net/testcontainer/file1.txt", (eventData as StorageBlobCreatedEventData).Url); } @@ -2475,7 +2475,7 @@ public void ConsumeCloudEventStorageBlobDeletedEvent() CloudEvent[] events = CloudEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].TryGetSystemEvent(out object eventData)); + Assert.True(events[0].TryGetSystemEventData(out object eventData)); Assert.AreEqual("https://example.blob.core.windows.net/testcontainer/testfile.txt", (eventData as StorageBlobDeletedEventData).Url); } @@ -2487,7 +2487,7 @@ public void ConsumeCloudEventStorageBlobRenamedEvent() CloudEvent[] events = CloudEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].TryGetSystemEvent(out object eventData)); + Assert.True(events[0].TryGetSystemEventData(out object eventData)); Assert.AreEqual("https://myaccount.blob.core.windows.net/testcontainer/testfile.txt", (eventData as StorageBlobRenamedEventData).DestinationUrl); } @@ -2499,7 +2499,7 @@ public void ConsumeCloudEventStorageDirectoryCreatedEvent() CloudEvent[] events = CloudEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].TryGetSystemEvent(out object eventData)); + Assert.True(events[0].TryGetSystemEventData(out object eventData)); Assert.AreEqual("https://myaccount.blob.core.windows.net/testcontainer/testDir", (eventData as StorageDirectoryCreatedEventData).Url); } @@ -2511,7 +2511,7 @@ public void ConsumeCloudEventStorageDirectoryDeletedEvent() CloudEvent[] events = CloudEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].TryGetSystemEvent(out object eventData)); + Assert.True(events[0].TryGetSystemEventData(out object eventData)); Assert.AreEqual("https://example.blob.core.windows.net/testcontainer/testDir", (eventData as StorageDirectoryDeletedEventData).Url); } @@ -2523,7 +2523,7 @@ public void ConsumeCloudEventStorageDirectoryRenamedEvent() CloudEvent[] events = CloudEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].TryGetSystemEvent(out object eventData)); + Assert.True(events[0].TryGetSystemEventData(out object eventData)); Assert.AreEqual("https://example.blob.core.windows.net/testcontainer/testDir", (eventData as StorageDirectoryRenamedEventData).DestinationUrl); } #endregion @@ -2538,7 +2538,7 @@ public void ConsumeCloudEventWebAppUpdatedEvent() CloudEvent[] events = CloudEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].TryGetSystemEvent(out object eventData)); + Assert.True(events[0].TryGetSystemEventData(out object eventData)); Assert.AreEqual(siteName, (eventData as WebAppUpdatedEventData).Name); } @@ -2551,7 +2551,7 @@ public void ConsumeCloudEventWebBackupOperationStartedEvent() CloudEvent[] events = CloudEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].TryGetSystemEvent(out object eventData)); + Assert.True(events[0].TryGetSystemEventData(out object eventData)); Assert.AreEqual(siteName, (eventData as WebBackupOperationStartedEventData).Name); } @@ -2564,7 +2564,7 @@ public void ConsumeCloudEventWebBackupOperationCompletedEvent() CloudEvent[] events = CloudEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].TryGetSystemEvent(out object eventData)); + Assert.True(events[0].TryGetSystemEventData(out object eventData)); Assert.AreEqual(siteName, (eventData as WebBackupOperationCompletedEventData).Name); } @@ -2576,7 +2576,7 @@ public void ConsumeCloudEventWebBackupOperationFailedEvent() CloudEvent[] events = CloudEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].TryGetSystemEvent(out object eventData)); + Assert.True(events[0].TryGetSystemEventData(out object eventData)); Assert.AreEqual(siteName, (eventData as WebBackupOperationFailedEventData).Name); } @@ -2589,7 +2589,7 @@ public void ConsumeCloudEventWebRestoreOperationStartedEvent() CloudEvent[] events = CloudEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].TryGetSystemEvent(out object eventData)); + Assert.True(events[0].TryGetSystemEventData(out object eventData)); Assert.AreEqual(siteName, (eventData as WebRestoreOperationStartedEventData).Name); } @@ -2602,7 +2602,7 @@ public void ConsumeCloudEventWebRestoreOperationCompletedEvent() CloudEvent[] events = CloudEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].TryGetSystemEvent(out object eventData)); + Assert.True(events[0].TryGetSystemEventData(out object eventData)); Assert.AreEqual(siteName, (eventData as WebRestoreOperationCompletedEventData).Name); } @@ -2615,7 +2615,7 @@ public void ConsumeCloudEventWebRestoreOperationFailedEvent() CloudEvent[] events = CloudEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].TryGetSystemEvent(out object eventData)); + Assert.True(events[0].TryGetSystemEventData(out object eventData)); Assert.AreEqual(siteName, (eventData as WebRestoreOperationFailedEventData).Name); } @@ -2628,7 +2628,7 @@ public void ConsumeCloudEventWebSlotSwapStartedEvent() CloudEvent[] events = CloudEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].TryGetSystemEvent(out object eventData)); + Assert.True(events[0].TryGetSystemEventData(out object eventData)); Assert.AreEqual(siteName, (eventData as WebSlotSwapStartedEventData).Name); } @@ -2641,7 +2641,7 @@ public void ConsumeCloudEventWebSlotSwapCompletedEvent() CloudEvent[] events = CloudEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].TryGetSystemEvent(out object eventData)); + Assert.True(events[0].TryGetSystemEventData(out object eventData)); Assert.AreEqual(siteName, (eventData as WebSlotSwapCompletedEventData).Name); } @@ -2654,7 +2654,7 @@ public void ConsumeCloudEventWebSlotSwapFailedEvent() CloudEvent[] events = CloudEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].TryGetSystemEvent(out object eventData)); + Assert.True(events[0].TryGetSystemEventData(out object eventData)); Assert.AreEqual(siteName, (eventData as WebSlotSwapFailedEventData).Name); } @@ -2667,7 +2667,7 @@ public void ConsumeCloudEventWebSlotSwapWithPreviewStartedEvent() CloudEvent[] events = CloudEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].TryGetSystemEvent(out object eventData)); + Assert.True(events[0].TryGetSystemEventData(out object eventData)); Assert.AreEqual(siteName, (eventData as WebSlotSwapWithPreviewStartedEventData).Name); } @@ -2680,7 +2680,7 @@ public void ConsumeCloudEventWebSlotSwapWithPreviewCancelledEvent() CloudEvent[] events = CloudEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].TryGetSystemEvent(out object eventData)); + Assert.True(events[0].TryGetSystemEventData(out object eventData)); Assert.AreEqual(siteName, (eventData as WebSlotSwapWithPreviewCancelledEventData).Name); } @@ -2693,7 +2693,7 @@ public void ConsumeCloudEventWebAppServicePlanUpdatedEvent() CloudEvent[] events = CloudEvent.Parse(requestContent); Assert.NotNull(events); - Assert.True(events[0].TryGetSystemEvent(out object eventData)); + Assert.True(events[0].TryGetSystemEventData(out object eventData)); Assert.AreEqual(planName, (eventData as WebAppServicePlanUpdatedEventData).Name); } #endregion From 967b8bcfc48c5e563f43c51160503645059a3904 Mon Sep 17 00:00:00 2001 From: JoshLove-msft <54595583+JoshLove-msft@users.noreply.github.com> Date: Tue, 2 Feb 2021 15:54:20 -0800 Subject: [PATCH 4/5] Update wording --- sdk/eventgrid/Azure.Messaging.EventGrid/README.md | 6 ++++-- .../samples/Sample3_ParseAndDeserializeEvents.md | 4 +++- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/sdk/eventgrid/Azure.Messaging.EventGrid/README.md b/sdk/eventgrid/Azure.Messaging.EventGrid/README.md index 0aefeb56caff..71ab330111b1 100644 --- a/sdk/eventgrid/Azure.Messaging.EventGrid/README.md +++ b/sdk/eventgrid/Azure.Messaging.EventGrid/README.md @@ -169,7 +169,7 @@ Using `CloudEvent`: // Parse the JSON payload into a list of events using CloudEvent.Parse CloudEvent[] cloudEvents = CloudEvent.Parse(jsonPayloadSampleTwo); ``` -From here, one can access the event data by deserializing to a specific type using `GetData()` and passing in a custom serializer if necessary. Calling `GetData()` will either return a deserialized system event (an event generated by an Azure service), or the event data wrapped in `BinaryData`, which represents the serialized JSON event data as bytes. +From here, one can access the event data by deserializing to a specific type using `GetData()`. Calling `GetData()` will either return the event data wrapped in `BinaryData`, which represents the serialized JSON event data as bytes. Using `GetData()`: @@ -201,7 +201,9 @@ foreach (CloudEvent cloudEvent in cloudEvents) Using `TryGetSystemEventData()`: -If expecting mostly system events, it may be cleaner to switch on `TryGetSystemEventData()` and use pattern matching to act on the individual events. If an event is not a system event, the method will return false and the out parameter will be null. As a caveat, if you are using a custom event type with an EventType value that later gets added as a system event by the service and SDK, the return value of `TryGetSystemEventData` would change from `false` to `true`. This could come up if you are pre-emptively creating your own custom events for events that are already being sent by the service, but have not yet been added to the SDK. In this case, it is better to use the generic `GetData` method so that your code flow doesn't change after upgrading. +If expecting mostly system events, it may be cleaner to switch on `TryGetSystemEventData()` and use pattern matching to act on the individual events. If an event is not a system event, the method will return false and the out parameter will be null. + +*As a caveat, if you are using a custom event type with an EventType value that later gets added as a system event by the service and SDK, the return value of `TryGetSystemEventData` would change from `false` to `true`. This could come up if you are pre-emptively creating your own custom events for events that are already being sent by the service, but have not yet been added to the SDK. In this case, it is better to use the generic `GetData` method so that your code flow doesn't change automatically after upgrading (of course, you may still want to modify your code to consume the newly released system event model as opposed to your custom model).* ```C# Snippet:DeserializePayloadUsingAsSystemEventData foreach (EventGridEvent egEvent in egEvents) diff --git a/sdk/eventgrid/Azure.Messaging.EventGrid/samples/Sample3_ParseAndDeserializeEvents.md b/sdk/eventgrid/Azure.Messaging.EventGrid/samples/Sample3_ParseAndDeserializeEvents.md index 949a21e20f62..4c12b03d1630 100644 --- a/sdk/eventgrid/Azure.Messaging.EventGrid/samples/Sample3_ParseAndDeserializeEvents.md +++ b/sdk/eventgrid/Azure.Messaging.EventGrid/samples/Sample3_ParseAndDeserializeEvents.md @@ -53,7 +53,9 @@ foreach (CloudEvent cloudEvent in cloudEvents) ``` ### Using `TryGetSystemEventData()` -If expecting mostly system events, it may be cleaner to switch on `TryGetSystemEventData()` and use pattern matching to act on the individual events. If an event is not a system event, the method will return false and the out parameter will be null. As a caveat, if you are using a custom event type with an EventType value that later gets added as a system event by the service and SDK, the return value of `TryGetSystemEventData` would change from `false` to `true`. This could come up if you are pre-emptively creating your own custom events for events that are already being sent by the service, but have not yet been added to the SDK. In this case, it is better to use the generic `GetData` method so that your code flow doesn't change after upgrading. +If expecting mostly system events, it may be cleaner to switch on `TryGetSystemEventData()` and use pattern matching to act on the individual events. If an event is not a system event, the method will return false and the out parameter will be null. + +*As a caveat, if you are using a custom event type with an EventType value that later gets added as a system event by the service and SDK, the return value of `TryGetSystemEventData` would change from `false` to `true`. This could come up if you are pre-emptively creating your own custom events for events that are already being sent by the service, but have not yet been added to the SDK. In this case, it is better to use the generic `GetData` method so that your code flow doesn't change automatically after upgrading (of course, you may still want to modify your code to consume the newly released system event model as opposed to your custom model).* ```C# Snippet:DeserializePayloadUsingAsSystemEventData foreach (EventGridEvent egEvent in egEvents) From 8b6e0449c2b09ac0b85ca6652443106a3618fb0c Mon Sep 17 00:00:00 2001 From: JoshLove-msft <54595583+JoshLove-msft@users.noreply.github.com> Date: Tue, 2 Feb 2021 16:48:30 -0800 Subject: [PATCH 5/5] Fix sample --- sdk/eventgrid/Azure.Messaging.EventGrid/README.md | 2 +- .../samples/Sample3_ParseAndDeserializeEvents.md | 2 +- .../tests/Samples/Sample2_ParseAndDeserializeEvents.cs | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/sdk/eventgrid/Azure.Messaging.EventGrid/README.md b/sdk/eventgrid/Azure.Messaging.EventGrid/README.md index 71ab330111b1..daa588163ffe 100644 --- a/sdk/eventgrid/Azure.Messaging.EventGrid/README.md +++ b/sdk/eventgrid/Azure.Messaging.EventGrid/README.md @@ -187,7 +187,7 @@ foreach (CloudEvent cloudEvent in cloudEvents) break; case "MyApp.Models.CustomEventType": // One can also specify a custom ObjectSerializer as needed to deserialize the payload correctly - TestPayload testPayload = cloudEvent.GetData(); + TestPayload testPayload = cloudEvent.GetData().ToObject(myCustomSerializer); Console.WriteLine(testPayload.Name); break; case SystemEventNames.StorageBlobDeleted: diff --git a/sdk/eventgrid/Azure.Messaging.EventGrid/samples/Sample3_ParseAndDeserializeEvents.md b/sdk/eventgrid/Azure.Messaging.EventGrid/samples/Sample3_ParseAndDeserializeEvents.md index 4c12b03d1630..b9d1c9718ac0 100644 --- a/sdk/eventgrid/Azure.Messaging.EventGrid/samples/Sample3_ParseAndDeserializeEvents.md +++ b/sdk/eventgrid/Azure.Messaging.EventGrid/samples/Sample3_ParseAndDeserializeEvents.md @@ -40,7 +40,7 @@ foreach (CloudEvent cloudEvent in cloudEvents) break; case "MyApp.Models.CustomEventType": // One can also specify a custom ObjectSerializer as needed to deserialize the payload correctly - TestPayload testPayload = cloudEvent.GetData(); + TestPayload testPayload = cloudEvent.GetData().ToObject(myCustomSerializer); Console.WriteLine(testPayload.Name); break; case SystemEventNames.StorageBlobDeleted: diff --git a/sdk/eventgrid/Azure.Messaging.EventGrid/tests/Samples/Sample2_ParseAndDeserializeEvents.cs b/sdk/eventgrid/Azure.Messaging.EventGrid/tests/Samples/Sample2_ParseAndDeserializeEvents.cs index 9d5c2cadd134..5611c383c45a 100644 --- a/sdk/eventgrid/Azure.Messaging.EventGrid/tests/Samples/Sample2_ParseAndDeserializeEvents.cs +++ b/sdk/eventgrid/Azure.Messaging.EventGrid/tests/Samples/Sample2_ParseAndDeserializeEvents.cs @@ -97,7 +97,7 @@ public void GenericReceiveAndDeserializeEventGridEvents() break; case "MyApp.Models.CustomEventType": // One can also specify a custom ObjectSerializer as needed to deserialize the payload correctly - TestPayload testPayload = cloudEvent.GetData(); + TestPayload testPayload = cloudEvent.GetData().ToObject(myCustomSerializer); Console.WriteLine(testPayload.Name); break; case SystemEventNames.StorageBlobDeleted: