Skip to content

Commit

Permalink
Migrate to .net 5.0 (#23)
Browse files Browse the repository at this point in the history
* Target .net 5.0
* Added auth header validation
* Added request catcher example
* Some cleanup in preparation for more unit tests
* Use asp.net ApiVersion handling instead of manual checking
* Added support for basic integration tests
* Add a warning about serilog config when required 'using' section is missing
* Upgrade Azure.Messaging.EventGrid library to 4.1.0
  • Loading branch information
pm7y committed Mar 24, 2021
1 parent c989388 commit a7e5c56
Show file tree
Hide file tree
Showing 45 changed files with 911 additions and 544 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -345,3 +345,6 @@ build-linux-64

# vscode
src/AzureEventGridSimulator/.config/

/src/AzureEventGridSimulator/appsettings.debug.txt
/**/*/log_*.txt
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ ENTRYPOINT ["AzureEventGridSimulator.exe"]

#### Customizable Run command
Alternatively, you can specify the configuration file, you have to map you config file
${PWD} - your curretn folder
${PWD} - your current folder
C:\temp\ - folder inside the container
`docker run -it --rm -v ${PWD}:C:\temp\ {TAG_NAME} --entrypoint AzureEventGridSimulator.exe --ConfigFile=C:\temp\{NAME OF YOUR CONFIGURATION FILE}`

Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<TargetFramework>net5.0</TargetFramework>
<TargetLatestRuntimePatch>true</TargetLatestRuntimePatch>
<IsPackable>false</IsPackable>

<RootNamespace>UnitTests</RootNamespace>
<LangVersion>default</LangVersion>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Azure.Messaging.EventGrid" Version="4.1.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="5.0.3" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.8.0" />
<PackageReference Include="Shouldly" Version="4.0.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.9.1" />
<PackageReference Include="Shouldly" Version="4.0.3" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<PrivateAssets>all</PrivateAssets>
Expand Down
49 changes: 49 additions & 0 deletions src/AzureEventGridSimulator.Tests/Integration/BasicTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using System;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using Azure.Messaging.EventGrid;
using AzureEventGridSimulator.Domain;
using Microsoft.AspNetCore.Mvc.Testing;
using Newtonsoft.Json;
using Shouldly;
using Xunit;

namespace AzureEventGridSimulator.Tests.Integration
{
public class BasicTests
: IClassFixture<TestContextFixture>
{
private readonly TestContextFixture _factory;

public BasicTests(TestContextFixture factory)
{
_factory = factory;
}

[Fact]
public async Task GivenAValidEvent_WhenPublished_ThenItShouldBeAccepted()
{
// Arrange
var client = _factory.CreateClient(new WebApplicationFactoryClientOptions
{
BaseAddress = new Uri("https://localhost:60101")
});

client.DefaultRequestHeaders.Add(Constants.AegSasKeyHeader, "TheLocal+DevelopmentKey=");
client.DefaultRequestHeaders.Add(Constants.AegEventTypeHeader, Constants.NotificationEventType);

var testEvent = new EventGridEvent("subject", "eventType", "1.0", new { Blah = 1 });
var json = JsonConvert.SerializeObject(new[] { testEvent }, Formatting.Indented);

// Act
var jsonContent = new StringContent(json, Encoding.UTF8, "application/json");
var response = await client.PostAsync("/api/events", jsonContent);

// Assert
response.EnsureSuccessStatusCode();
response.StatusCode.ShouldBe(HttpStatusCode.OK);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc.Testing;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Xunit;

namespace AzureEventGridSimulator.Tests.Integration
{
// ReSharper disable once ClassNeverInstantiated.Global
public class TestContextFixture : WebApplicationFactory<Startup>, IAsyncLifetime
{
public Task InitializeAsync()
{
return Task.CompletedTask;
}

public Task DisposeAsync()
{
Dispose();
return Task.CompletedTask;
}

protected override void ConfigureWebHost(IWebHostBuilder builder)
{
builder.UseEnvironment(Environments.Development);

builder.ConfigureAppConfiguration((_, configurationBuilder) =>
{
configurationBuilder
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.tests.json", false, true);
});

builder.ConfigureLogging(logging =>
{
logging.ClearProviders();
logging.AddConsole();
});
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
using Shouldly;
using Xunit;

namespace UnitTests
namespace AzureEventGridSimulator.Tests.Unit
{
public class ConfigurationLoadingTests
{
[Fact]
public void TestConfigurationLoad()
{
var json = @"
const string json = @"
{
""topics"": [{
""name"": ""MyAwesomeTopic"",
Expand Down Expand Up @@ -51,9 +51,9 @@ public void TestConfigurationLoad()
settings.ShouldNotBeNull();
settings.Topics.ShouldNotBeNull();
settings.Topics.ShouldAllBe(t =>
t.Subscribers.All(s => s.Filter != null) &&
t.Subscribers.All(s => s.Filter.AdvancedFilters != null)
);
t.Subscribers.All(s => s.Filter != null) &&
t.Subscribers.All(s => s.Filter.AdvancedFilters != null)
);

Should.NotThrow(() => { settings.Validate(); });
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
using Shouldly;
using Xunit;

namespace UnitTests.Filtering
namespace AzureEventGridSimulator.Tests.Unit.Filtering
{
public class AdvancedFilterEventAcceptanceTests
{
private static readonly EventGridEvent GridEvent = new EventGridEvent
private static readonly EventGridEvent _gridEvent = new()
{
Id = "EventId",
Data = new { NumberValue = 1, IsTrue = true, Name = "StringValue", DoubleValue = 0.12345d, NumberMaxValue = ulong.MaxValue, SubObject = new { Id = 1, Name = "Test" } },
Expand All @@ -27,7 +27,7 @@ public void TestAdvancedFilteringSuccess(AdvancedFilterSetting filter)
{
var filterConfig = new FilterSetting { AdvancedFilters = new[] { filter } };

filterConfig.AcceptsEvent(GridEvent).ShouldBeTrue($"{filter.Key} - {filter.OperatorType} - {filter.Value} - {filter.Values.Separate() }");
filterConfig.AcceptsEvent(_gridEvent).ShouldBeTrue($"{filter.Key} - {filter.OperatorType} - {filter.Value} - {filter.Values.Separate()}");
}

[Theory]
Expand All @@ -36,16 +36,17 @@ public void TestAdvancedFilteringFailure(AdvancedFilterSetting filter)
{
var filterConfig = new FilterSetting { AdvancedFilters = new[] { filter } };

filterConfig.AcceptsEvent(GridEvent).ShouldBeFalse($"{filter.Key} - {filter.OperatorType} - {filter.Value} - {filter.Values.Separate() }");
filterConfig.AcceptsEvent(_gridEvent).ShouldBeFalse($"{filter.Key} - {filter.OperatorType} - {filter.Value} - {filter.Values.Separate()}");
}

[Fact]
public void TestSimpleEventDataFilteringSuccess()
{
var filterConfig = new FilterSetting
{
AdvancedFilters = new[] {
new AdvancedFilterSetting { Key = "Data", OperatorType = AdvancedFilterSetting.OperatorTypeEnum.NumberIn, Values = new object[]{ 1 } }
AdvancedFilters = new[]
{
new AdvancedFilterSetting { Key = "Data", OperatorType = AdvancedFilterSetting.OperatorTypeEnum.NumberIn, Values = new object[] { 1 } }
}
};
var gridEvent = new EventGridEvent { Data = 1 };
Expand All @@ -58,7 +59,8 @@ public void TestSimpleEventDataFilteringUsingValueSuccess()
{
var filterConfig = new FilterSetting
{
AdvancedFilters = new[] {
AdvancedFilters = new[]
{
new AdvancedFilterSetting { Key = "Data", OperatorType = AdvancedFilterSetting.OperatorTypeEnum.NumberGreaterThanOrEquals, Value = 1 },
new AdvancedFilterSetting { Key = "Data", OperatorType = AdvancedFilterSetting.OperatorTypeEnum.NumberLessThanOrEquals, Value = 1 }
}
Expand All @@ -73,7 +75,8 @@ public void TestSimpleEventDataFilteringFailure()
{
var filterConfig = new FilterSetting
{
AdvancedFilters = new[] {
AdvancedFilters = new[]
{
new AdvancedFilterSetting { Key = "Data", OperatorType = AdvancedFilterSetting.OperatorTypeEnum.NumberIn, Value = 1 }
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
using Shouldly;
using Xunit;

namespace UnitTests.Filtering
namespace AzureEventGridSimulator.Tests.Unit.Filtering
{
public class AdvancedFilterValidationTests
{
private SimulatorSettings GetValidSimulatorSettings(AdvancedFilterSetting advancedFilter)
private static SimulatorSettings GetValidSimulatorSettings(AdvancedFilterSetting advancedFilter)
{
return new SimulatorSettings
return new()
{
Topics = new[]
{
Expand All @@ -22,10 +22,10 @@ private SimulatorSettings GetValidSimulatorSettings(AdvancedFilterSetting advanc
Port = 12345,
Subscribers = new List<SubscriptionSettings>
{
new SubscriptionSettings
new()
{
Name = "SubscriberName",
Filter = new FilterSetting{ AdvancedFilters = new[]{ advancedFilter } }
Filter = new FilterSetting { AdvancedFilters = new[] { advancedFilter } }
}
}.ToArray()
}
Expand Down Expand Up @@ -142,7 +142,11 @@ public void TestFilterValidationWithSixValues()
foreach (AdvancedFilterSetting.OperatorTypeEnum operatorType in Enum.GetValues(typeof(AdvancedFilterSetting.OperatorTypeEnum)))
{
var filterConfig = new AdvancedFilterSetting { Key = "Data", Values = new object[6], OperatorType = operatorType };
if (new[] { AdvancedFilterSetting.OperatorTypeEnum.NumberIn, AdvancedFilterSetting.OperatorTypeEnum.NumberNotIn, AdvancedFilterSetting.OperatorTypeEnum.StringIn, AdvancedFilterSetting.OperatorTypeEnum.StringNotIn }.Contains(operatorType))
if (new[]
{
AdvancedFilterSetting.OperatorTypeEnum.NumberIn, AdvancedFilterSetting.OperatorTypeEnum.NumberNotIn, AdvancedFilterSetting.OperatorTypeEnum.StringIn,
AdvancedFilterSetting.OperatorTypeEnum.StringNotIn
}.Contains(operatorType))
{
var exception = Should.Throw<ArgumentOutOfRangeException>(() => GetValidSimulatorSettings(filterConfig).Validate());

Expand All @@ -169,7 +173,7 @@ public void TestFilterValidationWithSingleDepthKey()
[Fact]
public void TestFilterValidationWithGrandchildKey()
{
// following the announcement here https://azure.microsoft.com/en-us/updates/advanced-filtering-generally-available-in-event-grid/ this should now work
// following the announcement here https://azure.microsoft.com/en-us/updates/advanced-filtering-generally-available-in-event-grid/ this should now work
Should.NotThrow(() =>
{
var filterConfig = new AdvancedFilterSetting { Key = "Data.Key1.SubKey", Value = "SomeValue" };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
using Shouldly;
using Xunit;

namespace UnitTests.Filtering
namespace AzureEventGridSimulator.Tests.Unit.Filtering
{
public class FilterSettingsValidationTests
{
private SimulatorSettings GetValidSimulatorSettings(FilterSetting filter)
private static SimulatorSettings GetValidSimulatorSettings(FilterSetting filter)
{
return new SimulatorSettings
return new()
{
Topics = new[]
{
Expand All @@ -21,7 +21,7 @@ private SimulatorSettings GetValidSimulatorSettings(FilterSetting filter)
Port = 12345,
Subscribers = new List<SubscriptionSettings>
{
new SubscriptionSettings
new()
{
Name = "SubscriberName",
Filter = filter
Expand All @@ -32,9 +32,9 @@ private SimulatorSettings GetValidSimulatorSettings(FilterSetting filter)
};
}

private AdvancedFilterSetting GetValidAdvancedFilter()
private static AdvancedFilterSetting GetValidAdvancedFilter()
{
return new AdvancedFilterSetting
return new()
{
Key = "key",
OperatorType = AdvancedFilterSetting.OperatorTypeEnum.BoolEquals,
Expand Down
Loading

0 comments on commit a7e5c56

Please sign in to comment.