Azure / azure-sdk-for-net

This repository is for active development of the Azure SDK for .NET. For consumers of the SDK we recommend visiting our public developer docs at https://learn.microsoft.com/dotnet/azure/ or our versioned developer docs at https://azure.github.io/azure-sdk-for-net.
MIT License
5.49k stars 4.81k forks source link

[QUERY] NotImplementedException when creating EventGridEvent for testing subscription validation event #35901

Open seanjohnston-codurance opened 1 year ago

seanjohnston-codurance commented 1 year ago

Library name and version

Azure.Messaging.EventGrid 4.13.0

Query/Question

Trying to unit test an Azure Function to confirm that it correctly handles a SubscriptionValidationEvent (at least verify that it returns 200), but it seems to be impossible to create a real EventGridEvent with SubscriptionValidationEventData as there is a NotImplemented method Write() when trying to serialize the data during the EventGridEvent constructor.

It seems that the EventGridModelFactory allowing me to create the data model indicates that this sort of thing should be possible, but it doesn't then seem to be set up so that I can actually create a system event from that data.

Is this intended behaviour?

Environment

.NET SDK (reflecting any global.json): Version: 6.0.400 Commit: 7771abd614

Runtime Environment: OS Name: Mac OS X OS Version: 13.3 OS Platform: Darwin RID: osx-arm64 Base Path: /usr/local/share/dotnet/sdk/6.0.400/

global.json file: Not found

Host: Version: 6.0.8 Architecture: arm64 Commit: 55fb7ef977

.NET SDKs installed: 6.0.400 [/usr/local/share/dotnet/sdk]

.NET runtimes installed: Microsoft.AspNetCore.App 6.0.8 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App] Microsoft.NETCore.App 6.0.8 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]

jsquire commented 1 year ago

Thank you for your feedback. Tagging and routing to the team member best able to assist.

JoshLove-msft commented 1 year ago

The model factory is intended to allow creation of all output model types for testing. Because these types are considered output types, the code generator that generates the serialization code does not implement write methods. I agree that this does make it difficult to test the end-to-end scenario of having one of these data models wrapped in an EventGridEvent (or CloudEvent) envelope. We will look into improving the experience here.

enioluwas commented 3 weeks ago

Running into this as well on version 4.21.0. The only workaround I've found for unit testing is to write the JSON for the system event directly:

var eventJson = JsonDocument.Parse("""
{
    "validationCode": "512d38b6-c7b8-40c8-89fe-f46f9e9622b6",
    "validationUrl": "http://test.com"
}
""");

var request = new EventGridEvent(
    subject: "subject",
    eventType: SystemEventNames.EventGridSubscriptionValidation,
    dataVersion: "V3",
    BinaryData.FromObjectAsJson(eventJson.RootElement));

JSON serialization seems to be an issue with most EventGrid models. It makes unit testing code that uses this library a challenge.