Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update EventGrid SDK to T2 #18

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Azure.Messaging.EventGrid" Version="4.10.0" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
<PackageReference Include="System.Memory.Data" Version="6.0.0" />

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This package will get pulled in by Azure.Messaging.EventGrid. Unless we need 6.0.0, I'd just remove this.

</ItemGroup>

</Project>
3 changes: 2 additions & 1 deletion src/Blazor.EventGridViewer.Core/Models/CloudEvent.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Newtonsoft.Json;
using System;

namespace Blazor.EventGridViewer.Core.Models
{
Expand All @@ -21,7 +22,7 @@ public class CloudEvent
public string Id { get; set; }

[JsonProperty("time")]
public string Time { get; set; }
public DateTimeOffset Time { get; set; }

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


[JsonProperty("data")]
public object Data { get; set; }
Expand Down
12 changes: 9 additions & 3 deletions src/Blazor.EventGridViewer.Core/Models/EventGridEventModel.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace Blazor.EventGridViewer.Core.Models
using System;

namespace Blazor.EventGridViewer.Core.Models
{
/// <summary>
/// Class used as a model for EventGrid Events
Expand All @@ -24,10 +26,14 @@ public class EventGridEventModel
/// <summary>
/// EventGrid Data
/// </summary>
public object EventData { get; set; }
public BinaryData EventData { get; set; }
/// <summary>
/// EventGrid Event Time
/// </summary>
public string EventTime { get; set; }
public DateTimeOffset EventTime { get; set; }
/// <summary>
/// Data Version
/// </summary>
public string DataVersion { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class EventGridViewerEventModel
/// <summary>
/// EventGrid Event Time
/// </summary>
public string EventTime { get; set; }
public DateTimeOffset EventTime { get; set; }
/// <summary>
/// Unique Id for EventGridViewer Event
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
Expand All @@ -7,9 +7,9 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Azure.Messaging.EventGrid" Version="4.10.0" />
<PackageReference Include="BlazorStrap" Version="1.3.2" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.AzureAD.UI" Version="3.1.5" />
<PackageReference Include="Microsoft.Azure.EventGrid" Version="3.2.0" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.EventGrid;
using Microsoft.Azure.EventGrid.Models;
using Newtonsoft.Json.Linq;
using Azure.Messaging.EventGrid;
using Azure.Messaging.EventGrid.Models;
using Azure.Messaging.EventGrid.SystemEvents;

namespace Blazor.EventGridViewer.ServerApp.Controllers
{
Expand Down Expand Up @@ -57,10 +58,11 @@ public async Task<IActionResult> Post()

foreach (EventGridEventModel model in eventGridEventModels)
{
EventGridEvent eventGrid = new EventGridEvent(model.Subject, model.EventType, model.DataVersion, model.EventData);
// EventGrid validation message
if (model.EventType == EventTypes.EventGridSubscriptionValidationEvent)
if (eventGrid.TryGetSystemEventData(out object systemEvent))

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The systemEvent we get back would still need to be checked to make sure it is the correct type, i.e. SubscriptionValidationEventData. Or we can continue checking the event type, and then do eventGrid.Data.ToObjectFromJson().

{
var eventData = ((JObject)(model.EventData)).ToObject<SubscriptionValidationEventData>();
var eventData = ((JObject)systemEvent).ToObject<SubscriptionValidationEventData>();
var responseData = new SubscriptionValidationResponse()
{
ValidationResponse = eventData.ValidationCode
Expand All @@ -73,7 +75,7 @@ public async Task<IActionResult> Post()
}
}
}
catch (Exception ex)
catch (Exception)
{
result = new StatusCodeResult(StatusCodes.Status500InternalServerError);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Blazor.EventGridViewer.ServerApp/Pages/Index.razor
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
<BSCollapse>
<BSCard CardType="CardType.Body">
<BSCard CardType="CardType.Text"><pre><code class="language-json">@model.Data</code></pre></BSCard>
<span role="button" title="Copy Data"><i class="fa fa-copy fa-3x eventgrid-viewer-blazor-button" aria-hidden="true" @onclick="@(() => onCopyClick(model.Data))"></i></span>
<span role="button" title="Copy Data"><i class="fa fa-copy fa-3x eventgrid-viewer-blazor-button" aria-hidden="true" @onclick="@(() => onCopyClick(model.Data.ToString()))"></i></span>
</BSCard>
</BSCollapse>
</BSCard>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ protected override async Task OnInitializedAsync()
{
await BootstrapCss.SetBootstrapCss(BOOTSTRAP_VERSION);
}
catch (Exception ex)
catch (Exception)
{
_isServerSide = true;
}
Expand Down
1 change: 0 additions & 1 deletion src/Blazor.EventGridViewer.ServerApp/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
using Microsoft.AspNetCore.Mvc.Authorization;
using Microsoft.AspNetCore.Authentication;
using Blazor.EventGridViewer.Services.Adapters;
using Microsoft.Azure.EventGrid.Models;
using Blazor.EventGridViewer.Core.Models;
using System.Collections.Generic;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
using Blazor.EventGridViewer.Core;
using Blazor.EventGridViewer.Core.Models;
using Blazor.EventGridViewer.Services.Interfaces;
using Microsoft.Azure.EventGrid.Models;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using Azure.Messaging.EventGrid;

namespace Blazor.EventGridViewer.Services.Adapters
{
Expand Down Expand Up @@ -56,7 +56,7 @@ private List<EventGridEventModel> AdaptCloudEvent(string t)
EventType = cloudEvent.Type,
Subject = string.IsNullOrEmpty(cloudEvent.Subject) ? cloudEvent.Type : cloudEvent.Subject,
Data = json,
EventData = cloudEvent.Data,
EventData = new BinaryData(cloudEvent.Data),
EventTime = cloudEvent.Time
};
models.Add(model);
Expand All @@ -72,7 +72,7 @@ private List<EventGridEventModel> AdaptCloudEvent(string t)
private List<EventGridEventModel> AdaptEventGridEvent(string t)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need EventGridEventModel - can we just use EventGridEvent?

{
List<EventGridEventModel> models = new List<EventGridEventModel>();
var eventGridEvents = JsonConvert.DeserializeObject<List<EventGridEvent>>(t);
var eventGridEvents = EventGridEvent.ParseMany(new BinaryData(t));

foreach (var eventGridEvent in eventGridEvents)
{
Expand All @@ -84,7 +84,7 @@ private List<EventGridEventModel> AdaptEventGridEvent(string t)
Subject = string.IsNullOrEmpty(eventGridEvent.Subject) ? eventGridEvent.EventType : eventGridEvent.Subject,
Data = json,
EventData = eventGridEvent.Data,
EventTime = eventGridEvent.EventTime.ToString("o") // https://docs.microsoft.com/en-us/dotnet/api/system.datetime.tostring?view=netcore-3.1
EventTime = eventGridEvent.EventTime // https://docs.microsoft.com/en-us/dotnet/api/system.datetime.tostring?view=netcore-3.1
};

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we remove this?

models.Add(model);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
Expand All @@ -9,7 +9,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Azure.EventGrid" Version="3.2.0" />
<PackageReference Include="Azure.Messaging.EventGrid" Version="4.10.0" />

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
<PackageReference Include="Azure.Messaging.EventGrid" Version="4.10.0" />
<PackageReference Include="Azure.Messaging.EventGrid" Version="4.11.0" />

</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Blazor.EventGridViewer.Core.CustomEventArgs;
using Azure.Messaging.EventGrid;
using Blazor.EventGridViewer.Core.CustomEventArgs;
using Blazor.EventGridViewer.Core.Models;
using System;

Expand Down
58 changes: 7 additions & 51 deletions src/Blazor.EventGridViewer.Unit.Tests/Data.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Newtonsoft.Json.Linq;
using System;

namespace Blazor.EventGridViewer.Unit.Tests
{
Expand All @@ -14,24 +15,8 @@ public static class Data
public static string GetMockEventGridEventJson()
{
// Note: guids were generated with an online guid generator
JArray jarray = new JArray();
jarray.Add(
new JObject
{
["id"] = "efdae305-d50f-49f6-a6ec-bd632d52bb3b",
["topic"] = "subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
["subject"] = "EventGridSubscriptionValidation",
["data"] = new JObject
{
["validationCode"] = "595c123c-0b91-4fa1-8b40-36f2e44194c9"
},
["eventType"] = "Microsoft.EventGrid.SubscriptionValidationEvent",
["eventTime"] = "2018-01-25T22:12:19.4556811Z",
["metadataVersion"] = "1",
["dataVersion"] = "1"
});

return jarray.ToString();
string mockEventGridEvent = "[{\"id\": \"efdae305-d50f-49f6-a6ec-bd632d52bb3b\",\"topic\":\"subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx\",\"subject\":\"EventGridSubscriptionValidation\",\"data\": {\"validationCode\": \"595c123c-0b91-4fa1-8b40-36f2e44194c9\"},\"eventType\": \"Microsoft.EventGrid.SubscriptionValidationEvent\",\"eventTime\": \"2018-01-25T22:12:19.4556811Z\",\"metadataVersion\": \"1\",\"dataVersion\": \"1\"}]";
return mockEventGridEvent;
}

/// <summary>
Expand All @@ -41,22 +26,8 @@ public static string GetMockEventGridEventJson()
public static string GetMockCloudEventJson()
{
// Note: guids were generated with an online guid generator
JObject jobject = new JObject
{
["specversion"] = "1.0",
["type"] = "com.github.pull.create",
["source"] = "https://github.com/cloudevents/spec/pull",
["subject"] = "123",
["id"] = "efdae305-d50f-49f6-a6ec-bd632d52bb3b",
["time"] = "2018-01-25T22:12:19.4556811Z",
["data"] = new JObject
{
["make"] = "Ducati",
["model"] = "Monster"
}
};

return jobject.ToString();
string mockCloudEvent = "{\"specversion\":\"1.0\",\"type\":\"com.github.pull.create\",\"source\":\"https://github.com/cloudevents/spec/pull\",\"subject\": \"123\",\"id\": \"efdae305-d50f-49f6-a6ec-bd632d52bb3b\",\"time\": \"2018-01-25T22:12:19.4556811Z\",\"data\": {\"make\": \"Ducati\",\"model\":\"Monster\"}}";
return mockCloudEvent;
}

/// <summary>
Expand All @@ -66,23 +37,8 @@ public static string GetMockCloudEventJson()
public static string GetMockCloudEventExtraPropertiesJson()
{
// Note: guids were generated with an online guid generator
JObject jobject = new JObject
{
["specversion"] = "1.0",
["type"] = "com.github.pull.create",
["source"] = "https://github.com/cloudevents/spec/pull",
["subject"] = "123",
["id"] = "efdae305-d50f-49f6-a6ec-bd632d52bb3b",
["time"] = "2018-01-25T22:12:19.4556811Z",
["comexampleothervalue"] = "5",
["data"] = new JObject
{
["make"] = "Ducati",
["model"] = "Monster"
}
};

return jobject.ToString();
string mockCloudEventExtraProperties = "[{\"specversion\":\"1.0\",\"type\":\"com.github.pull.create\",\"source\":\"https://github.com/cloudevents/spec/pull\",\"subject\":\"123\",\"id\":\"efdae305-d50f-49f6-a6ec-bd632d52bb3b\",\"time\": \"2018-01-25T22:12:19.4556811Z\",\"comexampleothervalue\":\"5\",\"data\": {\"make\": \"Ducati\",\"model\":\"Monster\"}}]";
return mockCloudEventExtraProperties;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Blazor.EventGridViewer.Core.Models;
using Blazor.EventGridViewer.Services.Adapters;
using Blazor.EventGridViewer.Services.Interfaces;
using System;
using Xunit;

namespace Blazor.EventGridViewer.Unit.Tests
Expand Down Expand Up @@ -41,7 +42,7 @@ public void EventGridEventModelAdapterConvertCanConvertEventGridEventModel()

// Assert
Assert.True(eventGridEventModel.Id == eventGridViewerEventModel.Id && eventGridEventModel.Subject == eventGridViewerEventModel.Subject &&
eventGridEventModel.EventType == eventGridViewerEventModel.EventType && eventGridEventModel.Data == eventGridViewerEventModel.Data &&
eventGridEventModel.EventType == eventGridViewerEventModel.EventType && eventGridEventModel.Data == eventGridViewerEventModel.Data.ToString() &&
eventGridEventModel.EventTime == eventGridViewerEventModel.EventTime);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
using Blazor.EventGridViewer.Core.Models;
using Blazor.EventGridViewer.Services.Adapters;
using Blazor.EventGridViewer.Services.Interfaces;
using Microsoft.Azure.EventGrid.Models;
using Moq;
using Newtonsoft.Json;
using System.Collections.Generic;
using System.Linq;
using Xunit;
using Azure.Messaging.EventGrid;
using Newtonsoft.Json.Linq;

namespace Blazor.EventGridViewer.Unit.Tests
{
Expand Down Expand Up @@ -44,17 +45,17 @@ public void EventGridSchemaAdapterConvertEventGridTest()
Mock<IEventGridIdentifySchemaService> mockEventGridIdentifySchemaService = new Mock<IEventGridIdentifySchemaService>();
mockEventGridIdentifySchemaService.Setup(s => s.Identify(json)).Returns(Core.EventGridSchemaType.EventGrid);
IAdapter<string, List<EventGridEventModel>> adapter = new EventGridSchemaAdapter(mockEventGridIdentifySchemaService.Object);
var mockModel = JsonConvert.DeserializeObject<List<EventGridEvent>>(json).FirstOrDefault();
var mockModel = EventGridEvent.ParseMany(new System.BinaryData(json)).FirstOrDefault();

// Act
var model = adapter.Convert(Data.GetMockEventGridEventJson()).FirstOrDefault();

// Assert
Assert.True(model.Id == mockModel.Id && model.Subject == mockModel.Subject &&
model.EventType == mockModel.EventType && model.EventTime == mockModel.EventTime.ToString("o"));
model.EventType == mockModel.EventType && model.EventTime == mockModel.EventTime);

var data = JsonConvert.SerializeObject(mockModel, Formatting.Indented);
Assert.Equal(data, model.Data);
Assert.Equal(data, model.Data.ToString());
}

/// <summary>
Expand All @@ -78,7 +79,7 @@ public void EventGridSchemaAdapterConvertCloudEventTest()
model.EventType == mockModel.Type && model.EventTime == mockModel.Time);

var data = JsonConvert.SerializeObject(mockModel, Formatting.Indented);
Assert.Equal(data, model.Data);
Assert.Equal(data, model.Data.ToString());
}
}
}