Skip to content

Commit

Permalink
Merge pull request #2 from msx752/development
Browse files Browse the repository at this point in the history
bugfix & package version upgrades and new XUnit Tests
  • Loading branch information
msx752 authored Feb 25, 2023
2 parents eda7cdb + 67d7390 commit 3421834
Show file tree
Hide file tree
Showing 17 changed files with 428 additions and 8 deletions.
32 changes: 32 additions & 0 deletions .github/workflows/xunit-tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# This workflow will build a .NET project
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-net
name: xunit-tests
on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]
jobs:
build:

runs-on: ubuntu-latest
strategy:
matrix:
dotnet-version: [ '7.0.x' ]
permissions:
actions: read
contents: read
security-events: write

steps:
- uses: actions/checkout@v3
- name: Setup .NET Core SDK ${{ matrix.dotnet-version }}
uses: actions/setup-dotnet@v3
with:
dotnet-version: ${{ matrix.dotnet-version }}

- name: Install dependencies
run: dotnet restore

- name: Test
run: dotnet test test/SampleDotnet.Result.Tests/SampleDotnet.Result.Tests.csproj --no-restore --verbosity normal
2 changes: 1 addition & 1 deletion src/SampleDotnet.Result/BadRequestResponse.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace SampleDotnet.Result;

public sealed class BadRequestResponse
public class BadRequestResponse
: BaseJsonResult
{
public BadRequestResponse()
Expand Down
2 changes: 1 addition & 1 deletion src/SampleDotnet.Result/InternalServerErrorResponse.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace SampleDotnet.Result;

public sealed class InternalServerErrorResponse
public class InternalServerErrorResponse
: BaseJsonResult
{
public InternalServerErrorResponse()
Expand Down
4 changes: 2 additions & 2 deletions src/SampleDotnet.Result/NotFoundResponse.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace SampleDotnet.Result;

public sealed class NotFoundResponse
public class NotFoundResponse
: BaseJsonResult
{
public NotFoundResponse()
Expand All @@ -9,7 +9,7 @@ public NotFoundResponse()
}

public NotFoundResponse(string message)
: base(StatusCodes.Status400BadRequest, message)
: base(StatusCodes.Status404NotFound, message)
{
}
}
2 changes: 1 addition & 1 deletion src/SampleDotnet.Result/OkResponse.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace SampleDotnet.Result;

public sealed class OkResponse
public class OkResponse
: BaseJsonResult
{
public OkResponse()
Expand Down
26 changes: 26 additions & 0 deletions src/SampleDotnet.Result/StatusCodeResponse.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
namespace SampleDotnet.Result
{
public class StatusCodeResponse
: BaseJsonResult
{
public StatusCodeResponse(int statusCode) : base(statusCode)
{
}

public StatusCodeResponse(int statusCode, IEnumerable<string> errorMessages) : base(statusCode, errorMessages)
{
}

public StatusCodeResponse(int statusCode, string errorMessages) : base(statusCode, errorMessages)
{
}

public StatusCodeResponse(int statusCode, object body) : base(statusCode, body)
{
}

public StatusCodeResponse(int statusCode, IEnumerable<object> body) : base(statusCode, body)
{
}
}
}
2 changes: 1 addition & 1 deletion src/SampleDotnet.Result/UnauthorizedResponse.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace SampleDotnet.Result;

public sealed class UnauthorizedResponse
public class UnauthorizedResponse
: BaseJsonResult
{
public UnauthorizedResponse()
Expand Down
71 changes: 71 additions & 0 deletions test/SampleDotnet.Result.Tests/BadRequestResponseTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
namespace SampleDotnet.Result.Tests;

public class BadRequestResponseTests
{
private const int statusCode_BADREQUEST = 400;

[Fact]
public void BadRequestResponse_Default()
{
BadRequestResponse response = new BadRequestResponse();

response.Model.Errors.ShouldBeNull();
response.ShouldBeDefaultResponseModel(new ResponseModel());
}

[Fact]
public void BadRequestResponse_Ctor1()
{
string responseObject = "error message1";
BadRequestResponse response = new BadRequestResponse(responseObject);

response.ShouldValidatetCommons(statusCode_BADREQUEST);
response.Model.ShouldNotBeNull();
response.Model.GetType().ShouldBeEquivalentTo(typeof(ResponseModel));

ResponseModel responseModel = new ResponseModel(responseObject);
response.ShouldSerializeAndDeserialize(responseModel);
}

[Fact]
public void BadRequestResponse_Ctor2()
{
IEnumerable<string> responseObjects = new List<string> { "error message1", "error message2" };
BadRequestResponse response = new BadRequestResponse(responseObjects);

response.ShouldValidatetCommons(statusCode_BADREQUEST);
response.Model.ShouldNotBeNull();
response.Model.GetType().ShouldBeEquivalentTo(typeof(ResponseModel));

ResponseModel responseModel = new ResponseModel(responseObjects);
response.ShouldSerializeAndDeserialize(responseModel);
}

[Fact]
public void StatusCodeResponse_Ctor1()
{
string responseObject = "error message1";
StatusCodeResponse response = new StatusCodeResponse(statusCode_BADREQUEST, responseObject);

response.ShouldValidatetCommons(statusCode_BADREQUEST);
response.Model.ShouldNotBeNull();
response.Model.GetType().ShouldBeEquivalentTo(typeof(ResponseModel));

ResponseModel responseModel = new ResponseModel(responseObject);
response.ShouldSerializeAndDeserialize(responseModel);
}

[Fact]
public void StatusCodeResponse_Ctor2()
{
IEnumerable<string> responseObjects = new List<string> { "error message1", "error message2" };
StatusCodeResponse response = new StatusCodeResponse(statusCode_BADREQUEST, responseObjects);

response.ShouldValidatetCommons(statusCode_BADREQUEST);
response.Model.ShouldNotBeNull();
response.Model.GetType().ShouldBeEquivalentTo(typeof(ResponseModel));

ResponseModel responseModel = new ResponseModel(responseObjects);
response.ShouldSerializeAndDeserialize(responseModel);
}
}
10 changes: 10 additions & 0 deletions test/SampleDotnet.Result.Tests/ExecuteTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace SampleDotnet.Result.Tests;

public class ExecuteTests
{
[Fact]
public void Execute_Stats1()
{
//TODO: initialize demo app with WebApplicationFactory then exetuce ActionContext
}
}
43 changes: 43 additions & 0 deletions test/SampleDotnet.Result.Tests/InternalServerErrorResponseTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
namespace SampleDotnet.Result.Tests;

public class InternalServerErrorResponseTests
{
private const int statusCode_INTERNALSERVERERROR = 500;

[Fact]
public void InternalServerErrorResponse_Default()
{
InternalServerErrorResponse response = new InternalServerErrorResponse();

response.Model.Errors.ShouldBeNull();
response.ShouldBeDefaultResponseModel(new ResponseModel());
}

[Fact]
public void InternalServerErrorResponse_Ctor1()
{
string responseObject = "User Friendly Message1";
InternalServerErrorResponse response = new InternalServerErrorResponse(responseObject);

response.ShouldValidatetCommons(statusCode_INTERNALSERVERERROR);
response.Model.ShouldNotBeNull();
response.Model.GetType().ShouldBeEquivalentTo(typeof(ResponseModel));

ResponseModel responseModel = new ResponseModel(responseObject);
response.ShouldSerializeAndDeserialize(responseModel);
}

[Fact]
public void InternalServerErrorResponse_Ctor2()
{
IEnumerable<string> responseObjects = new List<string> { "User Friendly Message1", "User Friendly Message2" };
InternalServerErrorResponse response = new InternalServerErrorResponse(responseObjects);

response.ShouldValidatetCommons(statusCode_INTERNALSERVERERROR);
response.Model.ShouldNotBeNull();
response.Model.GetType().ShouldBeEquivalentTo(typeof(ResponseModel));

ResponseModel responseModel = new ResponseModel(responseObjects);
response.ShouldSerializeAndDeserialize(responseModel);
}
}
29 changes: 29 additions & 0 deletions test/SampleDotnet.Result.Tests/NotFoundResponseTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
namespace SampleDotnet.Result.Tests;

public class NotFoundResponseTests
{
private const int statusCode_NOTFOUND = 404;

[Fact]
public void NotFoundResponse_Default()
{
NotFoundResponse response = new NotFoundResponse();

response.Model.Errors.ShouldBeNull();
response.ShouldBeDefaultResponseModel(new ResponseModel());
}

[Fact]
public void NotFoundResponse_Ctor1()
{
string responseObject = "NotFound error message1";
NotFoundResponse response = new NotFoundResponse(responseObject);

response.ShouldValidatetCommons(statusCode_NOTFOUND);
response.Model.ShouldNotBeNull();
response.Model.GetType().ShouldBeEquivalentTo(typeof(ResponseModel));

ResponseModel responseModel = new ResponseModel(responseObject);
response.ShouldSerializeAndDeserialize(responseModel);
}
}
78 changes: 78 additions & 0 deletions test/SampleDotnet.Result.Tests/OkResponseTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
namespace SampleDotnet.Result.Tests;

public class OkResponseTests
{
private const int statusCode_OK = 200;

[Fact]
public void OkResponse_Default()
{
OkResponse response = new OkResponse();

response.Model.Errors.ShouldBeNull();
response.ShouldValidatetCommons(statusCode_OK);
response.ShouldBeDefaultResponseModel(new ResponseModel());
}

[Fact]
public void OkResponse_Ctor1()
{
OkResponseTestData responseObject = new OkResponseTestData { strval1 = "valueOfName" };
OkResponse response = new OkResponse(responseObject);

response.ShouldValidatetCommons(statusCode_OK);
response.Model.Errors.ShouldBeNull();
response.Model.GetType().ShouldBeEquivalentTo(typeof(ResponseModel<object>));

ResponseModel<OkResponseTestData> responseModel = new ResponseModel<OkResponseTestData>(responseObject);
response.ShouldSerializeAndDeserialize(responseModel);
}

[Fact]
public void OkResponse_Ctor2()
{
IEnumerable<OkResponseTestData> responseObjects = new List<OkResponseTestData> { new OkResponseTestData() { strval1 = "valueOfName1", intval1 = 1 }, new OkResponseTestData { strval1 = "valueOfName2", intval1 = 2 } };
OkResponse response = new OkResponse(responseObjects);

response.ShouldValidatetCommons(statusCode_OK);
response.Model.Errors.ShouldBeNull();
response.Model.GetType().ShouldBeEquivalentTo(typeof(ResponseModel<object>));

ResponseModel<OkResponseTestData> responseModel = new ResponseModel<OkResponseTestData>(responseObjects);
response.ShouldSerializeAndDeserialize(responseModel);
}

[Fact]
public void StatusCodeResponse_Ctor1()
{
OkResponseTestData responseObject = new OkResponseTestData { strval1 = "valueOfName" };
StatusCodeResponse response = new StatusCodeResponse(statusCode_OK, responseObject);

response.ShouldValidatetCommons(statusCode_OK);
response.Model.Errors.ShouldBeNull();
response.Model.GetType().ShouldBeEquivalentTo(typeof(ResponseModel<object>));

ResponseModel<OkResponseTestData> responseModel = new ResponseModel<OkResponseTestData>(responseObject);
response.ShouldSerializeAndDeserialize(responseModel);
}

[Fact]
public void StatusCodeResponse_Ctor2()
{
IEnumerable<OkResponseTestData> responseObjects = new List<OkResponseTestData> { new OkResponseTestData() { strval1 = "valueOfName1", intval1 = 1 }, new OkResponseTestData { strval1 = "valueOfName2", intval1 = 2 } };
StatusCodeResponse response = new StatusCodeResponse(statusCode_OK, responseObjects);

response.ShouldValidatetCommons(statusCode_OK);
response.Model.Errors.ShouldBeNull();
response.Model.GetType().ShouldBeEquivalentTo(typeof(ResponseModel<object>));

ResponseModel<OkResponseTestData> responseModel = new ResponseModel<OkResponseTestData>(responseObjects);
response.ShouldSerializeAndDeserialize(responseModel);
}
}

internal class OkResponseTestData
{
public int intval1 { get; set; }
public string strval1 { get; set; }
}
24 changes: 22 additions & 2 deletions test/SampleDotnet.Result.Tests/SampleDotnet.Result.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
Expand All @@ -15,6 +15,26 @@
</Content>
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Mvc.Abstractions" Version="2.2.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="7.0.3" />
<PackageReference Include="Microsoft.AspNetCore.TestHost" Version="7.0.3" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.5.0" />
<PackageReference Include="Shouldly" Version="4.1.0" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="3.2.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="System.Net.Http" Version="4.3.4" />
<PackageReference Include="System.Diagnostics.TraceSource" Version="4.3.0" />
<PackageReference Include="Moq" Version="4.18.4" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.2" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\SampleDotnet.Result\SampleDotnet.Result.csproj" />
</ItemGroup>
Expand Down
Loading

0 comments on commit 3421834

Please sign in to comment.