diff --git a/sdk/eventgrid/Azure.Messaging.EventGrid/src/Customization/BinaryDataExtensions.cs b/sdk/eventgrid/Azure.Messaging.EventGrid/src/Customization/BinaryDataExtensions.cs index 12912abd56f3d..ea9149f20ba67 100644 --- a/sdk/eventgrid/Azure.Messaging.EventGrid/src/Customization/BinaryDataExtensions.cs +++ b/sdk/eventgrid/Azure.Messaging.EventGrid/src/Customization/BinaryDataExtensions.cs @@ -25,15 +25,13 @@ public static EventGridEvent ToEventGridEvent(this BinaryData binaryData) JsonDocument requestDocument = JsonDocument.Parse(binaryData.ToBytes()); EventGridEventInternal egEventInternal = EventGridEventInternal.DeserializeEventGridEventInternal(requestDocument.RootElement); - EventGridEvent egEvent = new EventGridEvent() - { - Subject = egEventInternal.Subject, - EventType = egEventInternal.EventType, - DataVersion = egEventInternal.DataVersion, - Id = egEventInternal.Id, - EventTime = egEventInternal.EventTime, - SerializedData = egEventInternal.Data - }; + EventGridEvent egEvent = new EventGridEvent( + egEventInternal.Data, + egEventInternal.Subject, + egEventInternal.EventType, + egEventInternal.DataVersion, + egEventInternal.EventTime, + egEventInternal.Id); return egEvent; } @@ -56,17 +54,15 @@ public static CloudEvent ToCloudEvent(this BinaryData binaryData) } CloudEvent cloudEvent = new CloudEvent( - cloudEventInternal.Source, - cloudEventInternal.Type) - { - Id = cloudEventInternal.Id, - Time = cloudEventInternal.Time, - DataBase64 = cloudEventInternal.DataBase64, - DataSchema = cloudEventInternal.Dataschema, - DataContentType = cloudEventInternal.Datacontenttype, - Subject = cloudEventInternal.Subject, - SerializedData = cloudEventInternal.Data - }; + cloudEventInternal.Id, + cloudEventInternal.Source, + cloudEventInternal.Type, + cloudEventInternal.Time, + cloudEventInternal.Dataschema, + cloudEventInternal.Datacontenttype, + cloudEventInternal.Subject, + cloudEventInternal.Data, + cloudEventInternal.DataBase64); if (cloudEventInternal.AdditionalProperties != null) { diff --git a/sdk/eventgrid/Azure.Messaging.EventGrid/src/Customization/CloudEvent.cs b/sdk/eventgrid/Azure.Messaging.EventGrid/src/Customization/CloudEvent.cs index 9bbf679ee5ca1..4ff4849d25563 100644 --- a/sdk/eventgrid/Azure.Messaging.EventGrid/src/Customization/CloudEvent.cs +++ b/sdk/eventgrid/Azure.Messaging.EventGrid/src/Customization/CloudEvent.cs @@ -85,6 +85,24 @@ public CloudEvent(string source, string type, BinaryData data, string dataConten ExtensionAttributes = new Dictionary(); } + internal CloudEvent(string id, string source, string type, DateTimeOffset? time, string dataSchema, string dataContentType, string subject, JsonElement? serializedData, byte[] dataBase64) + { + Argument.AssertNotNull(id, nameof(id)); + Argument.AssertNotNull(source, nameof(source)); + Argument.AssertNotNull(type, nameof(type)); + + Id = id; + Source = source; + Type = type; + Time = time; + DataSchema = dataSchema; + DataContentType = dataContentType; + Subject = subject; + SerializedData = serializedData; + DataBase64 = dataBase64; + ExtensionAttributes = new Dictionary(); + } + /// An identifier for the event. The combination of id and source must be unique for each distinct event. public string Id { get; set; } = Guid.NewGuid().ToString(); @@ -163,17 +181,15 @@ public static CloudEvent[] Parse(string requestContent) } CloudEvent cloudEvent = new CloudEvent( + cloudEventInternal.Id, cloudEventInternal.Source, - cloudEventInternal.Type) - { - Id = cloudEventInternal.Id, - Time = cloudEventInternal.Time, - DataBase64 = cloudEventInternal.DataBase64, - DataSchema = cloudEventInternal.Dataschema, - DataContentType = cloudEventInternal.Datacontenttype, - Subject = cloudEventInternal.Subject, - SerializedData = cloudEventInternal.Data - }; + cloudEventInternal.Type, + cloudEventInternal.Time, + cloudEventInternal.Dataschema, + cloudEventInternal.Datacontenttype, + cloudEventInternal.Subject, + cloudEventInternal.Data, + cloudEventInternal.DataBase64); if (cloudEventInternal.AdditionalProperties != null) { diff --git a/sdk/eventgrid/Azure.Messaging.EventGrid/src/Customization/EventGridEvent.cs b/sdk/eventgrid/Azure.Messaging.EventGrid/src/Customization/EventGridEvent.cs index 1a4be936d65c3..e95a2ff99ca93 100644 --- a/sdk/eventgrid/Azure.Messaging.EventGrid/src/Customization/EventGridEvent.cs +++ b/sdk/eventgrid/Azure.Messaging.EventGrid/src/Customization/EventGridEvent.cs @@ -36,8 +36,19 @@ public EventGridEvent(object data, string subject, string eventType, string data DataVersion = dataVersion; } - internal EventGridEvent() + internal EventGridEvent(JsonElement serializedData, string subject, string eventType, string dataVersion, DateTimeOffset eventTime, string id) { + Argument.AssertNotNull(subject, nameof(subject)); + Argument.AssertNotNull(eventType, nameof(eventType)); + Argument.AssertNotNull(dataVersion, nameof(dataVersion)); + Argument.AssertNotNull(id, nameof(id)); + + Subject = subject; + SerializedData = serializedData; + EventType = eventType; + DataVersion = dataVersion; + EventTime = eventTime; + Id = id; } /// An unique identifier for the event. @@ -100,15 +111,13 @@ public static EventGridEvent[] Parse(string requestContent) foreach (EventGridEventInternal egEventInternal in egEventsInternal) { - EventGridEvent egEvent = new EventGridEvent() - { - Subject = egEventInternal.Subject, - EventType = egEventInternal.EventType, - DataVersion = egEventInternal.DataVersion, - Id = egEventInternal.Id, - EventTime = egEventInternal.EventTime, - SerializedData = egEventInternal.Data - }; + EventGridEvent egEvent = new EventGridEvent( + egEventInternal.Data, + egEventInternal.Subject, + egEventInternal.EventType, + egEventInternal.DataVersion, + egEventInternal.EventTime, + egEventInternal.Id); egEvents.Add(egEvent); } diff --git a/sdk/eventgrid/Azure.Messaging.EventGrid/tests/ConsumeEventTests.cs b/sdk/eventgrid/Azure.Messaging.EventGrid/tests/ConsumeEventTests.cs index c14e3c9928e89..83e8a721b4383 100644 --- a/sdk/eventgrid/Azure.Messaging.EventGrid/tests/ConsumeEventTests.cs +++ b/sdk/eventgrid/Azure.Messaging.EventGrid/tests/ConsumeEventTests.cs @@ -129,6 +129,16 @@ public void EGEventGetDataThrowsWhenCalledWithoutParse() Assert.AreEqual("512d38b6-c7b8-40c8-89fe-f46f9e9622b6", eventData1.ItemSku); } } + + [Test] + public void EGEventParseThrowsIfMissingRequiredProperties() + { + // 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(requestContent), + Throws.InstanceOf()); + } #endregion #region Custom event tests @@ -1479,6 +1489,16 @@ public void CloudEventGetDataThrowsWhenCalledWithoutParse() Assert.AreEqual("512d38b6-c7b8-40c8-89fe-f46f9e9622b6", eventData1.ItemSku); } } + + [Test] + public void CloudEventParseThrowsIfMissingRequiredProperties() + { + // missing Id and Source + 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\" }, \"type\": \"Contoso.Items.ItemReceived\"}]"; + + Assert.That(() => CloudEvent.Parse(requestContent), + Throws.InstanceOf()); + } #endregion #region Custom event tests