Skip to content

Commit

Permalink
chore: enable nullable in test projects
Browse files Browse the repository at this point in the history
  • Loading branch information
skwasjer committed Jul 10, 2022
1 parent ceb83a4 commit 15f008e
Show file tree
Hide file tree
Showing 19 changed files with 70 additions and 67 deletions.
1 change: 1 addition & 0 deletions Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<LangVersion>latest</LangVersion>
<DisableImplicitNuGetFallbackFolder>true</DisableImplicitNuGetFallbackFolder>
<WarnOnPackingNonPackableProject>false</WarnOnPackingNonPackableProject>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

Expand Down
1 change: 0 additions & 1 deletion src/Correlate.Abstractions/Correlate.Abstractions.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
<PropertyGroup>
<TargetFrameworks>net6.0;netstandard2.0</TargetFrameworks>
<RootNamespace>Correlate</RootNamespace>
<Nullable>enable</Nullable>
</PropertyGroup>

<PropertyGroup>
Expand Down
1 change: 0 additions & 1 deletion src/Correlate.AspNetCore/Correlate.AspNetCore.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
<PropertyGroup>
<TargetFrameworks>net6.0;netcoreapp3.1</TargetFrameworks>
<RootNamespace>Correlate</RootNamespace>
<Nullable>enable</Nullable>
</PropertyGroup>

<PropertyGroup>
Expand Down
1 change: 0 additions & 1 deletion src/Correlate.Core/Correlate.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
<PropertyGroup>
<TargetFrameworks>net6.0;netstandard2.0</TargetFrameworks>
<RootNamespace>Correlate</RootNamespace>
<Nullable>enable</Nullable>
</PropertyGroup>

<PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

<PropertyGroup>
<TargetFrameworks>net6.0;netstandard2.0</TargetFrameworks>
<Nullable>enable</Nullable>
</PropertyGroup>

<PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ namespace Correlate.AspNetCore.Fixtures;

public class Startup
{
public static ITestCorrelatorContext LastRequestContext { get; private set; }
public static ITestCorrelatorContext LastRequestContext { get; private set; } = default!;

public void ConfigureServices(IServiceCollection services)
{
services.AddCorrelate();
services.AddCorrelate(opts => opts.IncludeInResponse = true);

services
.AddHttpClient<TestController>(client => client.BaseAddress = new Uri("http://0.0.0.0"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public TestController(ILogger<TestController> logger, ICorrelationContextAccesso
[HttpGet]
public IActionResult Get()
{
_logger.LogInformation("controller action: ok - {Id}", _correlationContextAccessor.CorrelationContext.CorrelationId);
_logger.LogInformation("controller action: ok - {Id}", _correlationContextAccessor.CorrelationContext?.CorrelationId);

return Ok("ok");
}
Expand All @@ -34,7 +34,7 @@ public IActionResult Get()
[HttpGet("correlate_client_request")]
public async Task<IActionResult> CorrelateClientRequest()
{
_logger.LogInformation("controller action: ok - {Id}", _correlationContextAccessor.CorrelationContext.CorrelationId);
_logger.LogInformation("controller action: ok - {Id}", _correlationContextAccessor.CorrelationContext?.CorrelationId);

HttpResponseMessage response = await _httpClient.GetAsync("correlated_external_call");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,10 @@ public CorrelateMiddlewareTests(TestAppFactory<Startup> factory)

public void Dispose()
{
// ReSharper disable ConditionalAccessQualifierIsNonNullableAccordingToAPIContract
_factory?.Dispose();
_mockHttp?.Dispose();
// ReSharper restore ConditionalAccessQualifierIsNonNullableAccordingToAPIContract
GC.SuppressFinalize(this);
}

Expand Down Expand Up @@ -160,7 +162,7 @@ public async Task When_calling_external_service_in_microservice_should_forward_c
HttpResponseMessage response = await client.SendAsync(request);

// Assert
string errorMessage = null;
string? errorMessage = null;
if (!response.IsSuccessStatusCode)
{
errorMessage = await response.Content.ReadAsStringAsync();
Expand All @@ -184,11 +186,11 @@ public async Task When_executing_multiple_requests_the_response_should_contain_n
var responses = (await Task.WhenAll(requestTasks)).ToList();

// Assert
string[] correlationIds = responses
string?[] correlationIds = responses
.Select(r => r.Headers.SingleOrDefault(h => h.Key == CorrelationHttpHeaders.CorrelationId).Value.FirstOrDefault())
.ToArray();

var distinctCorrelationIds = new HashSet<string>(correlationIds);
var distinctCorrelationIds = correlationIds.ToHashSet();
correlationIds
.Should()
.HaveCount(distinctCorrelationIds.Count)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace Correlate.AspNetCore;
internal class TestResponseFeature : IHttpResponseFeature
{
private readonly HeaderDictionary _headers = new();
private string _reasonPhrase;
private string? _reasonPhrase;
private Func<Task> _responseCompletedAsync = () => Task.FromResult(true);
private Func<Task> _responseStartingAsync = () => Task.FromResult(true);
private int _statusCode;
Expand Down Expand Up @@ -42,7 +42,7 @@ public int StatusCode
}
}

public string ReasonPhrase
public string? ReasonPhrase
{
get => _reasonPhrase;
set
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ private async Task<string> RunChildTask(string id, int level = 0)
await Task.Run(() => Task.Delay(_rnd.Next(100)));

// Do nested run.
string childId = null;
string? childId = null;
if (level < recursiveRuns)
{
childId = await RunChildTask(id, level + 1);
Expand Down
40 changes: 20 additions & 20 deletions test/Correlate.Core.Tests/CorrelationManagerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -294,13 +294,13 @@ public Task When_starting_correlationContext_inside_running_context_with_same_id
{
return _sut.CorrelateAsync(async () =>
{
CorrelationContext parentContext = _correlationContextAccessor.CorrelationContext;
CorrelationContext? parentContext = _correlationContextAccessor.CorrelationContext;
parentContext.Should().NotBeNull();

await _sut.CorrelateAsync(parentContext?.CorrelationId,
() =>
{
CorrelationContext innerContext = _correlationContextAccessor.CorrelationContext;
CorrelationContext? innerContext = _correlationContextAccessor.CorrelationContext;
innerContext.Should()
.NotBe(parentContext)
.And.BeEquivalentTo(parentContext);
Expand All @@ -315,11 +315,11 @@ public Task When_starting_correlationContext_inside_running_context_without_spec
{
return _sut.CorrelateAsync(async () =>
{
CorrelationContext parentContext = _correlationContextAccessor.CorrelationContext;
CorrelationContext? parentContext = _correlationContextAccessor.CorrelationContext;

await _sut.CorrelateAsync(() =>
{
CorrelationContext innerContext = _correlationContextAccessor.CorrelationContext;
CorrelationContext? innerContext = _correlationContextAccessor.CorrelationContext;
innerContext.Should()
.NotBe(parentContext)
.And.BeEquivalentTo(parentContext);
Expand All @@ -338,14 +338,14 @@ public Task When_starting_correlationContext_when_another_context_is_active_shou
return _sut.CorrelateAsync(parentContextId,
async () =>
{
CorrelationContext parentContext = _correlationContextAccessor.CorrelationContext;
CorrelationContext? parentContext = _correlationContextAccessor.CorrelationContext;
parentContext.Should().NotBeNull();
parentContext?.CorrelationId.Should().Be(parentContextId);

await _sut.CorrelateAsync(innerContextId,
() =>
{
CorrelationContext innerContext = _correlationContextAccessor.CorrelationContext;
CorrelationContext? innerContext = _correlationContextAccessor.CorrelationContext;
innerContext.Should().NotBeNull();
innerContext.Should().NotBe(parentContext);
innerContext?.CorrelationId.Should().Be(innerContextId);
Expand Down Expand Up @@ -600,13 +600,13 @@ public void When_starting_correlationContext_inside_running_context_with_same_id
{
_sut.Correlate(() =>
{
CorrelationContext parentContext = _correlationContextAccessor.CorrelationContext;
CorrelationContext? parentContext = _correlationContextAccessor.CorrelationContext;
parentContext.Should().NotBeNull();

_sut.Correlate(parentContext?.CorrelationId,
() =>
{
CorrelationContext innerContext = _correlationContextAccessor.CorrelationContext;
CorrelationContext? innerContext = _correlationContextAccessor.CorrelationContext;
innerContext.Should()
.NotBe(parentContext)
.And.BeEquivalentTo(parentContext);
Expand All @@ -619,11 +619,11 @@ public void When_starting_correlationContext_inside_running_context_without_spec
{
_sut.Correlate(() =>
{
CorrelationContext parentContext = _correlationContextAccessor.CorrelationContext;
CorrelationContext? parentContext = _correlationContextAccessor.CorrelationContext;

_sut.Correlate(() =>
{
CorrelationContext innerContext = _correlationContextAccessor.CorrelationContext;
CorrelationContext? innerContext = _correlationContextAccessor.CorrelationContext;
innerContext.Should()
.NotBe(parentContext)
.And.BeEquivalentTo(parentContext);
Expand All @@ -640,14 +640,14 @@ public void When_starting_correlationContext_when_another_context_is_active_shou
_sut.Correlate(parentContextId,
() =>
{
CorrelationContext parentContext = _correlationContextAccessor.CorrelationContext;
CorrelationContext? parentContext = _correlationContextAccessor.CorrelationContext;
parentContext.Should().NotBeNull();
parentContext?.CorrelationId.Should().Be(parentContextId);

_sut.Correlate(innerContextId,
() =>
{
CorrelationContext innerContext = _correlationContextAccessor.CorrelationContext;
CorrelationContext? innerContext = _correlationContextAccessor.CorrelationContext;
innerContext.Should().NotBeNull();
innerContext.Should().NotBe(parentContext);
innerContext?.CorrelationId.Should().Be(innerContextId);
Expand Down Expand Up @@ -701,19 +701,19 @@ static int ReturningCorrelatedFunc()
return new[]
{
// Instance members
DelegateTestCase.Create(instance.CorrelateAsync, (string)null, (Func<Task>)CorrelatedTask, (OnException)null),
DelegateTestCase.Create(instance.CorrelateAsync, (string)null, (Func<Task<int>>)ReturningCorrelatedTask, (OnException<int>)null),
DelegateTestCase.Create(instance.Correlate, (string)null, (Action)CorrelatedAction, (OnException)null),
DelegateTestCase.Create(instance.Correlate, (string)null, (Func<int>)ReturningCorrelatedFunc, (OnException<int>)null),
DelegateTestCase.Create(instance.CorrelateAsync, (string?)null, (Func<Task>)CorrelatedTask, (OnException?)null),
DelegateTestCase.Create(instance.CorrelateAsync, (string?)null, (Func<Task<int>>)ReturningCorrelatedTask, (OnException<int>?)null),
DelegateTestCase.Create(instance.Correlate, (string?)null, (Action)CorrelatedAction, (OnException?)null),
DelegateTestCase.Create(instance.Correlate, (string?)null, (Func<int>)ReturningCorrelatedFunc, (OnException<int>?)null),
// Extensions
DelegateTestCase.Create(AsyncCorrelationManagerExtensions.CorrelateAsync, instance, (Func<Task>)CorrelatedTask),
DelegateTestCase.Create(AsyncCorrelationManagerExtensions.CorrelateAsync, instance, (Func<Task<int>>)ReturningCorrelatedTask),
DelegateTestCase.Create(CorrelationManagerExtensions.Correlate, instance, (Action)CorrelatedAction),
DelegateTestCase.Create(CorrelationManagerExtensions.Correlate, instance, (Func<int>)ReturningCorrelatedFunc),
DelegateTestCase.Create(AsyncCorrelationManagerExtensions.CorrelateAsync, instance, (Func<Task>)CorrelatedTask, (OnException)null),
DelegateTestCase.Create(AsyncCorrelationManagerExtensions.CorrelateAsync, instance, (Func<Task<int>>)ReturningCorrelatedTask, (OnException<int>)null),
DelegateTestCase.Create(CorrelationManagerExtensions.Correlate, instance, (Action)CorrelatedAction, (OnException)null),
DelegateTestCase.Create(CorrelationManagerExtensions.Correlate, instance, (Func<int>)ReturningCorrelatedFunc, (OnException<int>)null)
DelegateTestCase.Create(AsyncCorrelationManagerExtensions.CorrelateAsync, instance, (Func<Task>)CorrelatedTask, (OnException?)null),
DelegateTestCase.Create(AsyncCorrelationManagerExtensions.CorrelateAsync, instance, (Func<Task<int>>)ReturningCorrelatedTask, (OnException<int>?)null),
DelegateTestCase.Create(CorrelationManagerExtensions.Correlate, instance, (Action)CorrelatedAction, (OnException?)null),
DelegateTestCase.Create(CorrelationManagerExtensions.Correlate, instance, (Func<int>)ReturningCorrelatedFunc, (OnException<int>?)null)
}
.SelectMany(tc => tc.GetNullArgumentTestCases());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,18 @@ public CorrelatingMessageHandlerTests()

public void Dispose()
{
// ReSharper disable ConditionalAccessQualifierIsNonNullableAccordingToAPIContract
_sut?.Dispose();
_httpClient?.Dispose();
_mockHttp?.Dispose();
// ReSharper restore ConditionalAccessQualifierIsNonNullableAccordingToAPIContract
}

[Fact]
public async Task Given_a_correlation_context_should_add()
{
_contextAccessor.CorrelationContext?.CorrelationId.Should().NotBeNull();
string correlationId = _contextAccessor.CorrelationContext!.CorrelationId;
string? correlationId = _contextAccessor.CorrelationContext?.CorrelationId;

_mockHttp
.When(matching => matching
Expand Down Expand Up @@ -87,7 +89,7 @@ public async Task Given_headerName_is_overridden_should_not_use_default_headerNa
{
_correlateClientOptions.RequestHeader = "custom-header";
_contextAccessor.CorrelationContext?.CorrelationId.Should().NotBeNull();
string correlationId = _contextAccessor.CorrelationContext!.CorrelationId;
string? correlationId = _contextAccessor.CorrelationContext?.CorrelationId;

_mockHttp
.When(matching => matching
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public void Given_list_of_accepted_headers_when_request_contains_one_of_the_head
_sut.Add(requestHeaderKey, new StringValues(CorrelationId));

// Act
KeyValuePair<string, string> header = _sut.GetCorrelationIdHeader(new[] { "first-header", "second-header", "third-header" });
KeyValuePair<string, string?> header = _sut.GetCorrelationIdHeader(new[] { "first-header", "second-header", "third-header" });

// Assert
header.Should().BeEquivalentTo(new KeyValuePair<string, string>(requestHeaderKey, CorrelationId));
Expand All @@ -37,7 +37,7 @@ public void When_getting_by_custom_header_name_should_get_value()
_sut.Add(TestHeaderName, new StringValues(CorrelationId));

// Act
KeyValuePair<string, string> header = _sut.GetCorrelationIdHeader(new[] { TestHeaderName });
KeyValuePair<string, string?> header = _sut.GetCorrelationIdHeader(new[] { TestHeaderName });

// Assert
header.Should().BeEquivalentTo(new KeyValuePair<string, string>(TestHeaderName, CorrelationId));
Expand All @@ -49,7 +49,7 @@ public void When_getting_by_name_it_should_get_value()
_sut.Add(CorrelationHttpHeaders.CorrelationId, new StringValues(CorrelationId));

// Act
KeyValuePair<string, string> header = _sut.GetCorrelationIdHeader(new[] { CorrelationHttpHeaders.CorrelationId });
KeyValuePair<string, string?> header = _sut.GetCorrelationIdHeader(new[] { CorrelationHttpHeaders.CorrelationId });

// Assert
header.Should().BeEquivalentTo(new KeyValuePair<string, string>(CorrelationHttpHeaders.CorrelationId, CorrelationId));
Expand All @@ -59,20 +59,20 @@ public void When_getting_by_name_it_should_get_value()
public void When_header_is_not_found_should_return_preferred_header_without_value()
{
// Act
KeyValuePair<string, string> header = _sut.GetCorrelationIdHeader(new[] { TestHeaderName });
KeyValuePair<string, string?> header = _sut.GetCorrelationIdHeader(new[] { TestHeaderName });

// Assert
header.Should().BeEquivalentTo(new KeyValuePair<string, string>(TestHeaderName, null));
header.Should().BeEquivalentTo(new KeyValuePair<string, string?>(TestHeaderName, null));
}

[Fact]
public void When_using_empty_accepted_headers_should_throw()
{
_sut.Add(CorrelationHttpHeaders.CorrelationId, new StringValues(CorrelationId));
var expectedHeader = new KeyValuePair<string, string>(CorrelationHttpHeaders.CorrelationId, null);
var expectedHeader = new KeyValuePair<string, string?>(CorrelationHttpHeaders.CorrelationId, null);

// Act
KeyValuePair<string, string> header = _sut.GetCorrelationIdHeader(Array.Empty<string>());
KeyValuePair<string, string?> header = _sut.GetCorrelationIdHeader(Array.Empty<string>());

// Assert
header.Should().BeEquivalentTo(expectedHeader, "it should not take correlation id from header dictionary but still return header key");
Expand Down
Loading

0 comments on commit 15f008e

Please sign in to comment.