Skip to content

Commit

Permalink
Code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
jezzsantos committed Sep 19, 2023
1 parent 815bc4f commit 2357688
Show file tree
Hide file tree
Showing 27 changed files with 371 additions and 115 deletions.
6 changes: 3 additions & 3 deletions README_PROJECT.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

You will need the following development tools to build, run, and test this project:

* Jetbrains Rider - obtain a license key for Jetbrains dotUltimate
* Jetbrains Rider - obtain a license key for Jetbrains dotUltimate

* Install the .NET7.0 SDK. Available for [download here](https://dotnet.microsoft.com/en-us/download/dotnet/7.0)

Expand Down Expand Up @@ -120,7 +120,7 @@ When pushed, all branches will be built and tested with GitHub actions
2. Run these tests:

In Rider, run all C# tests with Category= `Unit`, `Unit.Architecture` and `Integration.Web`
In Rider, run all C# tests with Category= `Unit`, `Unit.Architecture` and `Integration.Web`

> Note: Use `Group By > Category` in Rider's unit test explorer to view these three categories easily.
Expand Down Expand Up @@ -182,7 +182,7 @@ This is what we modify and how:

We run this API production DI code in-process in a Kestrel web server in the same process as your Integration Tests (e.g. in the process of `xunit.console.exe`, not as a separate process). We replace the `appsettings.json` file with the one in your integration testing project

> Which should never ever contain ANY production settings or secrets!
> Which should never ever contain ANY production settings or secrets!
We then manipulate the DI container (seen in the `TestStartup.cs` of your integration testing project) and replace certain dependencies (e.g. the 3rd party adapters like: `INotificationsService,` etc) with their respective hardcoded Stubbed equivalents (found in the `Stubs` directory your integration testing project).

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
#if TESTINGONLY
using FluentValidation;
using Infrastructure.WebApi.Interfaces.Operations.TestingOnly;
using JetBrains.Annotations;

namespace ApiHost1.Apis.TestingOnly;

public class GetTestingOnlyValidatedRequestValidator : AbstractValidator<GetTestingOnlyValidatedRequest>
[UsedImplicitly]
public class GetWithValidatorTestingOnlyRequestValidator : AbstractValidator<GetWithValidatorTestingOnlyRequest>
{
public GetTestingOnlyValidatedRequestValidator()
public GetWithValidatorTestingOnlyRequestValidator()
{
RuleFor(req => req.Id)
.NotEmpty()
Expand Down
12 changes: 6 additions & 6 deletions src/ApiHost1/Apis/TestingOnly/TestingWebApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,21 @@ namespace ApiHost1.Apis.TestingOnly;
public class TestingWebApi : IWebApiService
{
[WebApiRoute("/testingonly/{id}/unvalidated", WebApiOperation.Get, true)]
public async Task<IResult> Get(GetTestingOnlyUnvalidatedRequest request, CancellationToken cancellationToken)
public async Task<IResult> Get(GetWithoutValidatorTestingOnlyRequest request, CancellationToken cancellationToken)
{
await Task.CompletedTask;
return Results.Ok(new GetTestingOnlyResponse { Message = $"amessage{request.Id}" });
return Results.Ok(new StringMessageTestingOnlyResponse { Message = $"amessage{request.Id}" });
}

[WebApiRoute("/testingonly/{id}/validated", WebApiOperation.Get, true)]
public async Task<IResult> Get(GetTestingOnlyValidatedRequest request, CancellationToken cancellationToken)
public async Task<IResult> Get(GetWithValidatorTestingOnlyRequest request, CancellationToken cancellationToken)
{
await Task.CompletedTask;
return Results.Ok(new GetTestingOnlyResponse { Message = $"amessage{request.Field1}" });
return Results.Ok(new StringMessageTestingOnlyResponse { Message = $"amessage{request.Field1}" });
}

[WebApiRoute("/testingonly/exception", WebApiOperation.Get, true)]
public async Task<IResult> Get(GetTestingOnlyExceptionRequest request, CancellationToken cancellationToken)
[WebApiRoute("/testingonly/throws", WebApiOperation.Get, true)]
public async Task<IResult> Get(ThrowsExceptionTestingOnlyRequest request, CancellationToken cancellationToken)
{
await Task.CompletedTask;
throw new InvalidOperationException("amessage");
Expand Down
4 changes: 3 additions & 1 deletion src/ApiHost1/HostedModules.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ public static SubDomainModules Get()
// EXTEND: Add the sub domain of each API, to host in this project.
// NOTE: The order of these registrations will matter for some dependencies
var modules = new SubDomainModules();
modules.Register(new CarsApiModule());
#if TESTINGONLY
modules.Register(new TestingOnlyApiModule());
#endif
modules.Register(new CarsApiModule());

return modules;
}
Expand Down
3 changes: 2 additions & 1 deletion src/ApiHost1/Resources.resx
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>

<root>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" id="root"
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata"
id="root"
xmlns="">
<xsd:element name="root" msdata:IsDataSet="true">

Expand Down
7 changes: 5 additions & 2 deletions src/ApiHost1/TestingOnlyApiModule.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#if TESTINGONLY
using System.Reflection;
using ApiHost1.Apis.TestingOnly;
using Infrastructure.WebApi.Common;

namespace ApiHost1;
Expand All @@ -15,5 +17,6 @@ public Action<ConfigurationManager, IServiceCollection> RegisterServicesFunction
get { return (_, _) => { }; }
}

public Assembly ApiAssembly => typeof(Program).Assembly;
}
public Assembly ApiAssembly => typeof(TestingWebApi).Assembly;
}
#endif
2 changes: 2 additions & 0 deletions src/CarsApi/Apis/Cars/GetCarRequestValidator.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using FluentValidation;
using Infrastructure.WebApi.Interfaces.Operations.Cars;
using JetBrains.Annotations;

namespace CarsApi.Apis.Cars;

[UsedImplicitly]
public class GetCarRequestValidator : AbstractValidator<GetCarRequest>
{
public GetCarRequestValidator()
Expand Down
8 changes: 4 additions & 4 deletions src/Common.UnitTests/Common.UnitTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.7.2" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Common\Common.csproj" />
<ProjectReference Include="..\UnitTesting.Common\UnitTesting.Common.csproj" />
<ProjectReference Include="..\Common\Common.csproj" />
<ProjectReference Include="..\UnitTesting.Common\UnitTesting.Common.csproj" />
</ItemGroup>

</Project>
101 changes: 52 additions & 49 deletions src/Common/Annotations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -149,10 +149,10 @@ public sealed class ItemCanBeNullAttribute : Attribute
[Conditional("JETBRAINS_ANNOTATIONS")]
public sealed class StringFormatMethodAttribute : Attribute
{
/// <param name="formatParameterName">
/// Specifies which parameter of an annotated method should be treated as the format string.
/// </param>
public StringFormatMethodAttribute([NotNull] string formatParameterName)
/// <param name="formatParameterName">
/// Specifies which parameter of an annotated method should be treated as the format string.
/// </param>
public StringFormatMethodAttribute([NotNull] string formatParameterName)
{
FormatParameterName = formatParameterName;
}
Expand Down Expand Up @@ -244,9 +244,6 @@ public ValueProviderAttribute([NotNull] string name)
[Conditional("JETBRAINS_ANNOTATIONS")]
public sealed class ValueRangeAttribute : Attribute
{
public object From { get; }
public object To { get; }

public ValueRangeAttribute(long from, long to)
{
From = from;
Expand All @@ -268,6 +265,10 @@ public ValueRangeAttribute(ulong value)
{
From = To = value;
}

public object From { get; }

public object To { get; }
}

/// <summary>
Expand Down Expand Up @@ -706,12 +707,12 @@ public PublicAPIAttribute([NotNull] string comment)
[Conditional("JETBRAINS_ANNOTATIONS")]
public sealed class InstantHandleAttribute : Attribute
{
/// <summary>
/// Require the method invocation to be used under the 'await' expression for this attribute to take effect on the code
/// analysis engine.
/// Can be used for delegate/enumerable parameters of 'async' methods.
/// </summary>
public bool RequireAwait { get; set; }
/// <summary>
/// Require the method invocation to be used under the 'await' expression for this attribute to take effect on the code
/// analysis engine.
/// Can be used for delegate/enumerable parameters of 'async' methods.
/// </summary>
public bool RequireAwait { get; set; }
}

/// <summary>
Expand Down Expand Up @@ -887,28 +888,28 @@ public sealed class SourceTemplateAttribute : Attribute
[Conditional("JETBRAINS_ANNOTATIONS")]
public sealed class MacroAttribute : Attribute
{
/// <summary>
/// Allows specifying a macro that will be executed for a <see cref="SourceTemplateAttribute">source template</see>
/// parameter when the template is expanded.
/// </summary>
[CanBeNull]
/// <summary>
/// Allows specifying a macro that will be executed for a <see cref="SourceTemplateAttribute">source template</see>
/// parameter when the template is expanded.
/// </summary>
[CanBeNull]
public string Expression { get; set; }

/// <summary>
/// Allows specifying which occurrence of the target parameter becomes editable when the template is deployed.
/// </summary>
/// <remarks>
/// If the target parameter is used several times in the template, only one occurrence becomes editable;
/// other occurrences are changed synchronously. To specify the zero-based index of the editable occurrence,
/// use values >= 0. To make the parameter non-editable when the template is expanded, use -1.
/// </remarks>
public int Editable { get; set; }

/// <summary>
/// Identifies the target parameter of a <see cref="SourceTemplateAttribute">source template</see> if the
/// <see cref="MacroAttribute" /> is applied on a template method.
/// </summary>
[CanBeNull]
/// <summary>
/// Allows specifying which occurrence of the target parameter becomes editable when the template is deployed.
/// </summary>
/// <remarks>
/// If the target parameter is used several times in the template, only one occurrence becomes editable;
/// other occurrences are changed synchronously. To specify the zero-based index of the editable occurrence,
/// use values >= 0. To make the parameter non-editable when the template is expanded, use -1.
/// </remarks>
public int Editable { get; set; }

/// <summary>
/// Identifies the target parameter of a <see cref="SourceTemplateAttribute">source template</see> if the
/// <see cref="MacroAttribute" /> is applied on a template method.
/// </summary>
[CanBeNull]
public string Target { get; set; }
}

Expand Down Expand Up @@ -1464,12 +1465,12 @@ public AspRequiredAttributeAttribute([NotNull] string attribute)
[Conditional("JETBRAINS_ANNOTATIONS")]
public sealed class AspTypePropertyAttribute : Attribute
{
public bool CreateConstructorReferences { get; }

public AspTypePropertyAttribute(bool createConstructorReferences)
{
CreateConstructorReferences = createConstructorReferences;
}

public bool CreateConstructorReferences { get; }
}

#endregion
Expand Down Expand Up @@ -1798,13 +1799,14 @@ public sealed class RouteTemplateAttribute : Attribute
[Conditional("JETBRAINS_ANNOTATIONS")]
public sealed class RouteParameterConstraintAttribute : Attribute
{
[NotNull] public string ConstraintName { get; }
[CanBeNull] public Type ProposedType { get; set; }

public RouteParameterConstraintAttribute([NotNull] string constraintName)
{
ConstraintName = constraintName;
}

[NotNull] public string ConstraintName { get; }

[CanBeNull] public Type ProposedType { get; set; }
}

/// <summary>
Expand Down Expand Up @@ -2033,6 +2035,7 @@ public RazorPageBaseTypeAttribute([NotNull] string baseType, string pageName)
}

[NotNull] public string BaseType { get; }

[CanBeNull] public string PageName { get; }
}

Expand Down Expand Up @@ -2154,20 +2157,20 @@ public sealed class XamlTwoWayBindingModeByDefaultAttribute : Attribute
[Conditional("JETBRAINS_ANNOTATIONS")]
public sealed class TestSubjectAttribute : Attribute
{
/// <summary>
/// Gets the type of the subject being tested.
/// </summary>
[NotNull]
public Type Subject { get; }

/// <summary>
/// Initializes a new instance of the <see cref="TestSubjectAttribute" /> class with the specified subject type.
/// </summary>
/// <param name="subject">The type of the subject being tested.</param>
public TestSubjectAttribute([NotNull] Type subject)
/// <summary>
/// Initializes a new instance of the <see cref="TestSubjectAttribute" /> class with the specified subject type.
/// </summary>
/// <param name="subject">The type of the subject being tested.</param>
public TestSubjectAttribute([NotNull] Type subject)
{
Subject = subject;
}

/// <summary>
/// Gets the type of the subject being tested.
/// </summary>
[NotNull]
public Type Subject { get; }
}

/// <summary>
Expand Down
Loading

0 comments on commit 2357688

Please sign in to comment.