Skip to content

Commit 5fd223b

Browse files
Add unit tests (#5)
1 parent 32abad3 commit 5fd223b

File tree

12 files changed

+376
-164
lines changed

12 files changed

+376
-164
lines changed

.github/workflows/ci-cd.yml

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,59 +13,74 @@ env:
1313

1414
jobs:
1515

16-
build:
16+
build-test:
1717
runs-on: ubuntu-latest
18-
name: Build
18+
name: Build and test
1919
steps:
20-
- uses: actions/checkout@v2
20+
- name: Checkout code
21+
uses: actions/checkout@v2
22+
2123
- name: Setup
2224
uses: actions/setup-dotnet@v1
2325
with:
2426
dotnet-version: 5.0.300
27+
2528
- name: Build
2629
run: dotnet build --configuration ${{ env.CONFIGURATION }}
2730

31+
- name: Test
32+
run: dotnet test --configuration ${{ env.CONFIGURATION }}
33+
2834
github-package-deploy:
2935
if: ${{ github.event_name == 'push' }}
3036
name: GitHub package deploy
3137
runs-on: ubuntu-latest
32-
needs: build
38+
needs: build-test
3339
steps:
3440
- name: Checkout code
35-
uses: actions/checkout@v2
41+
uses: actions/checkout@v2
42+
3643
- name: Setup .NET
3744
uses: actions/setup-dotnet@v1
3845
with:
39-
dotnet-version: 5.0.101
46+
dotnet-version: 5.0.300
47+
4048
- name: Publish
4149
run: dotnet publish --configuration ${{ env.CONFIGURATION }} -o 'HttpHealthCheckDashboard/bin/publish'
50+
4251
- name: Docker build
43-
run: docker build -f 'HttpHealthCheckDashboard/Dockerfile' -t docker.pkg.github.com/arnab-developer/httphealthcheckdashboard/httphealthcheckdashboard:1.0.5 .
52+
run: docker build -f 'HttpHealthCheckDashboard/Dockerfile' -t docker.pkg.github.com/arnab-developer/httphealthcheckdashboard/httphealthcheckdashboard:1.0.6 .
53+
4454
- name: Docker push
4555
run: |
4656
echo "${{ secrets.GITHUB_TOKEN }}" | docker login https://docker.pkg.github.com -u Arnab-Developer --password-stdin
47-
docker push docker.pkg.github.com/arnab-developer/httphealthcheckdashboard/httphealthcheckdashboard:1.0.5
57+
docker push docker.pkg.github.com/arnab-developer/httphealthcheckdashboard/httphealthcheckdashboard:1.0.6
4858
4959
dockerhub-deploy:
5060
if: ${{ github.event_name == 'release' }}
5161
name: Dockerhub deploy
5262
runs-on: ubuntu-latest
53-
needs: build
63+
needs: build-test
5464
environment:
5565
name: Dockerhub
5666
url: https://hub.docker.com/r/45862391/httphealthcheckdashboard
5767
steps:
5868
- name: Checkout code
59-
uses: actions/checkout@v2
69+
uses: actions/checkout@v2
70+
6071
- name: Setup .NET
6172
uses: actions/setup-dotnet@v1
73+
6274
with:
63-
dotnet-version: 5.0.101
75+
dotnet-version: 5.0.300
76+
6477
- name: Publish
6578
run: dotnet publish --configuration ${{ env.CONFIGURATION }} -o 'HttpHealthCheckDashboard/bin/publish'
79+
6680
- name: Docker build
67-
run: docker build -f 'HttpHealthCheckDashboard/Dockerfile' -t 45862391/httphealthcheckdashboard:1.0.5 .
81+
run: docker build -f 'HttpHealthCheckDashboard/Dockerfile' -t 45862391/httphealthcheckdashboard:1.0.6 .
82+
6883
- name: Docker push
6984
run: |
7085
echo "${{ secrets.DOCKERHUB_PWD }}" | docker login -u 45862391 --password-stdin
71-
docker push 45862391/httphealthcheckdashboard:1.0.5
86+
docker push 45862391/httphealthcheckdashboard:1.0.6

HttpHealthCheckDashboard.sln

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
1212
README.md = README.md
1313
EndProjectSection
1414
EndProject
15-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HttpHealthCheckDashboardLib", "HttpHealthCheckDashboardLib\HttpHealthCheckDashboardLib.csproj", "{C5F20680-076B-4361-AF25-FE52DEFE98FE}"
15+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HttpHealthCheckDashboardLib", "HttpHealthCheckDashboardLib\HttpHealthCheckDashboardLib.csproj", "{C5F20680-076B-4361-AF25-FE52DEFE98FE}"
16+
EndProject
17+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HttpHealthCheckDashboardLibTests", "HttpHealthCheckDashboardLibTests\HttpHealthCheckDashboardLibTests.csproj", "{753A1256-517F-4921-BA95-9CA4F9375BCA}"
1618
EndProject
1719
Global
1820
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -28,6 +30,10 @@ Global
2830
{C5F20680-076B-4361-AF25-FE52DEFE98FE}.Debug|Any CPU.Build.0 = Debug|Any CPU
2931
{C5F20680-076B-4361-AF25-FE52DEFE98FE}.Release|Any CPU.ActiveCfg = Release|Any CPU
3032
{C5F20680-076B-4361-AF25-FE52DEFE98FE}.Release|Any CPU.Build.0 = Release|Any CPU
33+
{753A1256-517F-4921-BA95-9CA4F9375BCA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
34+
{753A1256-517F-4921-BA95-9CA4F9375BCA}.Debug|Any CPU.Build.0 = Debug|Any CPU
35+
{753A1256-517F-4921-BA95-9CA4F9375BCA}.Release|Any CPU.ActiveCfg = Release|Any CPU
36+
{753A1256-517F-4921-BA95-9CA4F9375BCA}.Release|Any CPU.Build.0 = Release|Any CPU
3137
EndGlobalSection
3238
GlobalSection(SolutionProperties) = preSolution
3339
HideSolutionNode = FALSE
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using ArnabDeveloper.HttpHealthCheck;
2+
using Microsoft.Extensions.Diagnostics.HealthChecks;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.Threading;
6+
using System.Threading.Tasks;
7+
8+
namespace HttpHealthCheckDashboardLib
9+
{
10+
public abstract class BaseHealthCheck
11+
: Microsoft.Extensions.Diagnostics.HealthChecks.IHealthCheck
12+
{
13+
private readonly IEnumerable<ApiDetail> _urlDetails;
14+
private readonly ICommonHealthCheck _commonHealthCheck;
15+
16+
public BaseHealthCheck(IEnumerable<ApiDetail> urlDetails,
17+
ICommonHealthCheck commonHealthCheck)
18+
{
19+
_urlDetails = urlDetails;
20+
_commonHealthCheck = commonHealthCheck;
21+
}
22+
23+
Task<HealthCheckResult> Microsoft.Extensions.Diagnostics.HealthChecks.IHealthCheck.CheckHealthAsync(
24+
HealthCheckContext context, CancellationToken cancellationToken)
25+
{
26+
string apiNameToTest = GetType().Name.Substring(0, GetType().Name.IndexOf("HealthCheck"));
27+
ApiDetail? apiDetail = _urlDetails.FirstOrDefault(u => u.Name == apiNameToTest && u.IsEnable);
28+
29+
return _commonHealthCheck.IsApiHealthy(apiDetail)
30+
? Task.FromResult(HealthCheckResult.Healthy())
31+
: Task.FromResult(HealthCheckResult.Unhealthy());
32+
}
33+
}
34+
}
Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,13 @@
11
using ArnabDeveloper.HttpHealthCheck;
2-
using Microsoft.Extensions.Diagnostics.HealthChecks;
32
using System.Collections.Generic;
4-
using System.Linq;
5-
using System.Threading;
6-
using System.Threading.Tasks;
73

84
namespace HttpHealthCheckDashboardLib.HealthChecks
95
{
10-
public class BlogHealthCheck
11-
: Microsoft.Extensions.Diagnostics.HealthChecks.IHealthCheck
6+
public class BlogHealthCheck : BaseHealthCheck
127
{
13-
private readonly IEnumerable<ApiDetail> _urlDetails;
14-
private readonly ICommonHealthCheck _commonHealthCheck;
15-
16-
public BlogHealthCheck(IEnumerable<ApiDetail> urlDetails,
17-
ICommonHealthCheck commonHealthCheck)
8+
public BlogHealthCheck(IEnumerable<ApiDetail> urlDetails, ICommonHealthCheck commonHealthCheck)
9+
: base(urlDetails, commonHealthCheck)
1810
{
19-
_urlDetails = urlDetails;
20-
_commonHealthCheck = commonHealthCheck;
21-
}
22-
23-
Task<HealthCheckResult> Microsoft.Extensions.Diagnostics.HealthChecks.IHealthCheck.CheckHealthAsync(
24-
HealthCheckContext context, CancellationToken cancellationToken)
25-
{
26-
string apiNameToTest = nameof(BlogHealthCheck).Substring(
27-
0, nameof(BlogHealthCheck).IndexOf("HealthCheck"));
28-
ApiDetail? apiDetail = _urlDetails.FirstOrDefault(u => u.Name == apiNameToTest && u.IsEnable);
29-
30-
return _commonHealthCheck.IsApiHealthy(apiDetail)
31-
? Task.FromResult(HealthCheckResult.Healthy())
32-
: Task.FromResult(HealthCheckResult.Unhealthy());
3311
}
3412
}
3513
}
Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,13 @@
11
using ArnabDeveloper.HttpHealthCheck;
2-
using Microsoft.Extensions.Diagnostics.HealthChecks;
32
using System.Collections.Generic;
4-
using System.Linq;
5-
using System.Threading;
6-
using System.Threading.Tasks;
73

84
namespace HttpHealthCheckDashboardLib.HealthChecks
95
{
10-
public class GitHubHealthCheck
11-
: Microsoft.Extensions.Diagnostics.HealthChecks.IHealthCheck
6+
public class GitHubHealthCheck : BaseHealthCheck
127
{
13-
private readonly IEnumerable<ApiDetail> _urlDetails;
14-
private readonly ICommonHealthCheck _commonHealthCheck;
15-
16-
public GitHubHealthCheck(IEnumerable<ApiDetail> urlDetails,
17-
ICommonHealthCheck commonHealthCheck)
8+
public GitHubHealthCheck(IEnumerable<ApiDetail> urlDetails, ICommonHealthCheck commonHealthCheck)
9+
: base(urlDetails, commonHealthCheck)
1810
{
19-
_urlDetails = urlDetails;
20-
_commonHealthCheck = commonHealthCheck;
21-
}
22-
23-
Task<HealthCheckResult> Microsoft.Extensions.Diagnostics.HealthChecks.IHealthCheck.CheckHealthAsync(
24-
HealthCheckContext context, CancellationToken cancellationToken)
25-
{
26-
string apiNameToTest = nameof(GitHubHealthCheck).Substring(
27-
0, nameof(GitHubHealthCheck).IndexOf("HealthCheck"));
28-
ApiDetail? apiDetail = _urlDetails.FirstOrDefault(u => u.Name == apiNameToTest && u.IsEnable);
29-
30-
return _commonHealthCheck.IsApiHealthy(apiDetail)
31-
? Task.FromResult(HealthCheckResult.Healthy())
32-
: Task.FromResult(HealthCheckResult.Unhealthy());
3311
}
3412
}
3513
}
Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,13 @@
11
using ArnabDeveloper.HttpHealthCheck;
2-
using Microsoft.Extensions.Diagnostics.HealthChecks;
32
using System.Collections.Generic;
4-
using System.Linq;
5-
using System.Threading;
6-
using System.Threading.Tasks;
73

84
namespace HttpHealthCheckDashboardLib.HealthChecks
95
{
10-
public class InactiveUrlHealthCheck
11-
: Microsoft.Extensions.Diagnostics.HealthChecks.IHealthCheck
6+
public class InactiveUrlHealthCheck : BaseHealthCheck
127
{
13-
private readonly IEnumerable<ApiDetail> _urlDetails;
14-
private readonly ICommonHealthCheck _commonHealthCheck;
15-
16-
public InactiveUrlHealthCheck(IEnumerable<ApiDetail> urlDetails,
17-
ICommonHealthCheck commonHealthCheck)
8+
public InactiveUrlHealthCheck(IEnumerable<ApiDetail> urlDetails, ICommonHealthCheck commonHealthCheck)
9+
: base(urlDetails, commonHealthCheck)
1810
{
19-
_urlDetails = urlDetails;
20-
_commonHealthCheck = commonHealthCheck;
21-
}
22-
23-
Task<HealthCheckResult> Microsoft.Extensions.Diagnostics.HealthChecks.IHealthCheck.CheckHealthAsync(
24-
HealthCheckContext context, CancellationToken cancellationToken)
25-
{
26-
string apiNameToTest = nameof(InactiveUrlHealthCheck).Substring(
27-
0, nameof(InactiveUrlHealthCheck).IndexOf("HealthCheck"));
28-
ApiDetail? apiDetail = _urlDetails.FirstOrDefault(u => u.Name == apiNameToTest && u.IsEnable);
29-
30-
return _commonHealthCheck.IsApiHealthy(apiDetail)
31-
? Task.FromResult(HealthCheckResult.Healthy())
32-
: Task.FromResult(HealthCheckResult.Unhealthy());
3311
}
3412
}
3513
}
Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,13 @@
11
using ArnabDeveloper.HttpHealthCheck;
2-
using Microsoft.Extensions.Diagnostics.HealthChecks;
32
using System.Collections.Generic;
4-
using System.Linq;
5-
using System.Threading;
6-
using System.Threading.Tasks;
73

84
namespace HttpHealthCheckDashboardLib.HealthChecks
95
{
10-
public class InstagramHealthCheck
11-
: Microsoft.Extensions.Diagnostics.HealthChecks.IHealthCheck
6+
public class InstagramHealthCheck : BaseHealthCheck
127
{
13-
private readonly IEnumerable<ApiDetail> _urlDetails;
14-
private readonly ICommonHealthCheck _commonHealthCheck;
15-
16-
public InstagramHealthCheck(IEnumerable<ApiDetail> urlDetails,
17-
ICommonHealthCheck commonHealthCheck)
8+
public InstagramHealthCheck(IEnumerable<ApiDetail> urlDetails, ICommonHealthCheck commonHealthCheck)
9+
: base(urlDetails, commonHealthCheck)
1810
{
19-
_urlDetails = urlDetails;
20-
_commonHealthCheck = commonHealthCheck;
21-
}
22-
23-
Task<HealthCheckResult> Microsoft.Extensions.Diagnostics.HealthChecks.IHealthCheck.CheckHealthAsync(
24-
HealthCheckContext context, CancellationToken cancellationToken)
25-
{
26-
string apiNameToTest = nameof(InstagramHealthCheck).Substring(
27-
0, nameof(InstagramHealthCheck).IndexOf("HealthCheck"));
28-
ApiDetail? apiDetail = _urlDetails.FirstOrDefault(u => u.Name == apiNameToTest && u.IsEnable);
29-
30-
return _commonHealthCheck.IsApiHealthy(apiDetail)
31-
? Task.FromResult(HealthCheckResult.Healthy())
32-
: Task.FromResult(HealthCheckResult.Unhealthy());
3311
}
3412
}
3513
}
Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,13 @@
11
using ArnabDeveloper.HttpHealthCheck;
2-
using Microsoft.Extensions.Diagnostics.HealthChecks;
32
using System.Collections.Generic;
4-
using System.Linq;
5-
using System.Threading;
6-
using System.Threading.Tasks;
73

84
namespace HttpHealthCheckDashboardLib.HealthChecks
95
{
10-
public class InvalidUrlHealthCheck
11-
: Microsoft.Extensions.Diagnostics.HealthChecks.IHealthCheck
6+
public class InvalidUrlHealthCheck : BaseHealthCheck
127
{
13-
private readonly IEnumerable<ApiDetail> _urlDetails;
14-
private readonly ICommonHealthCheck _commonHealthCheck;
15-
16-
public InvalidUrlHealthCheck(IEnumerable<ApiDetail> urlDetails,
17-
ICommonHealthCheck commonHealthCheck)
8+
public InvalidUrlHealthCheck(IEnumerable<ApiDetail> urlDetails, ICommonHealthCheck commonHealthCheck)
9+
: base(urlDetails, commonHealthCheck)
1810
{
19-
_urlDetails = urlDetails;
20-
_commonHealthCheck = commonHealthCheck;
21-
}
22-
23-
Task<HealthCheckResult> Microsoft.Extensions.Diagnostics.HealthChecks.IHealthCheck.CheckHealthAsync(
24-
HealthCheckContext context, CancellationToken cancellationToken)
25-
{
26-
string apiNameToTest = nameof(InvalidUrlHealthCheck).Substring(
27-
0, nameof(InvalidUrlHealthCheck).IndexOf("HealthCheck"));
28-
ApiDetail? apiDetail = _urlDetails.FirstOrDefault(u => u.Name == apiNameToTest && u.IsEnable);
29-
30-
return _commonHealthCheck.IsApiHealthy(apiDetail)
31-
? Task.FromResult(HealthCheckResult.Healthy())
32-
: Task.FromResult(HealthCheckResult.Unhealthy());
3311
}
3412
}
3513
}
Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,13 @@
11
using ArnabDeveloper.HttpHealthCheck;
2-
using Microsoft.Extensions.Diagnostics.HealthChecks;
32
using System.Collections.Generic;
4-
using System.Linq;
5-
using System.Threading;
6-
using System.Threading.Tasks;
73

84
namespace HttpHealthCheckDashboardLib.HealthChecks
95
{
10-
public class TwitterHealthCheck
11-
: Microsoft.Extensions.Diagnostics.HealthChecks.IHealthCheck
6+
public class TwitterHealthCheck : BaseHealthCheck
127
{
13-
private readonly IEnumerable<ApiDetail> _urlDetails;
14-
private readonly ICommonHealthCheck _commonHealthCheck;
15-
16-
public TwitterHealthCheck(IEnumerable<ApiDetail> urlDetails,
17-
ICommonHealthCheck commonHealthCheck)
8+
public TwitterHealthCheck(IEnumerable<ApiDetail> urlDetails, ICommonHealthCheck commonHealthCheck)
9+
: base(urlDetails, commonHealthCheck)
1810
{
19-
_urlDetails = urlDetails;
20-
_commonHealthCheck = commonHealthCheck;
21-
}
22-
23-
Task<HealthCheckResult> Microsoft.Extensions.Diagnostics.HealthChecks.IHealthCheck.CheckHealthAsync(
24-
HealthCheckContext context, CancellationToken cancellationToken)
25-
{
26-
string apiNameToTest = nameof(TwitterHealthCheck).Substring(
27-
0, nameof(TwitterHealthCheck).IndexOf("HealthCheck"));
28-
ApiDetail? apiDetail = _urlDetails.FirstOrDefault(u => u.Name == apiNameToTest && u.IsEnable);
29-
30-
return _commonHealthCheck.IsApiHealthy(apiDetail)
31-
? Task.FromResult(HealthCheckResult.Healthy())
32-
: Task.FromResult(HealthCheckResult.Unhealthy());
3311
}
3412
}
3513
}

0 commit comments

Comments
 (0)