forked from NCIOCPL/r4r-api
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a59d4a1
commit 19988ef
Showing
6 changed files
with
176 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
Feature: The system is able to report whether it is a healthy condition. | ||
|
||
Background: | ||
* url apiHost | ||
|
||
Scenario: The index has been loaded. | ||
|
||
Given path 'HealthCheck', 'status' | ||
When method get | ||
Then status 200 | ||
And match response == 'alive!' |
68 changes: 68 additions & 0 deletions
68
src/NCI.OCPL.Api.ResourcesForResearchers/Controllers/HealthCheckController.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
using System; | ||
using System.Threading.Tasks; | ||
|
||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.Extensions.Logging; | ||
|
||
using NCI.OCPL.Api.Common; | ||
|
||
|
||
namespace NCI.OCPL.Api.ResourcesForResearchers.Controllers | ||
{ | ||
|
||
/// <summary> | ||
/// Controller for reporting on system health. | ||
/// </summary> | ||
[Route("HealthCheck")] | ||
public class HealthCheckController : ControllerBase | ||
{ | ||
|
||
/// <summary> | ||
/// Message to return for a "healthy" status. | ||
/// </summary> | ||
public const string HEALTHY_STATUS = "alive!"; | ||
|
||
/// <summary> | ||
/// Message to return for an "unhealthy" status. | ||
/// </summary> | ||
public const string UNHEALTHY_STATUS = "Service not healthy."; | ||
|
||
private readonly IHealthCheckService _healthSvc; | ||
private readonly ILogger _logger; | ||
|
||
/// <summary> | ||
/// Constructor. | ||
/// </summary> | ||
/// <param name="logger">The logger.</param> | ||
/// <param name="healthCheckSvc">Elasticsearch health check instance.</param> | ||
/// <returns></returns> | ||
public HealthCheckController(ILogger<HealthCheckController> logger, IHealthCheckService healthCheckSvc) | ||
=> (_logger, _healthSvc) = (logger, healthCheckSvc); | ||
|
||
|
||
/// <summary> | ||
/// Provides an endpoint for checking that the glossary API and underlying Elasticsearch instance | ||
/// are in a good operational state. | ||
/// </summary> | ||
/// <returns>Status 200 and the message "alive!" if the service is healthy. | ||
/// | ||
/// Status 500 and the message "Service not healthy." otherwise.</returns> | ||
[HttpGet("status")] | ||
public async Task<string> IsHealthy() | ||
{ | ||
try | ||
{ | ||
bool isHealthy = await _healthSvc.IndexIsHealthy(); ; | ||
if (isHealthy) | ||
return HEALTHY_STATUS; | ||
else | ||
throw new APIErrorException(500, UNHEALTHY_STATUS); | ||
} | ||
catch (Exception ex) | ||
{ | ||
_logger.LogError(ex, "Error checking health."); | ||
throw new APIErrorException(500, UNHEALTHY_STATUS); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
...CI.OCPL.Api.ResourcesForResearchers.Tests/Tests/Controllers/HealthCheckControllerTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
using System; | ||
|
||
using Microsoft.Extensions.Logging.Testing; | ||
|
||
using Moq; | ||
using Xunit; | ||
|
||
using NCI.OCPL.Api.Common; | ||
using NCI.OCPL.Api.ResourcesForResearchers.Controllers; | ||
|
||
|
||
namespace NCI.OCPL.Api.Glossary.Tests | ||
{ | ||
public class HealthCheckControllerTests | ||
{ | ||
/// <summary> | ||
/// Handle the healthcheck reporting an unhealthy status. | ||
/// </summary> | ||
[Fact] | ||
public async void IsUnhealthy() | ||
{ | ||
var logger = NullLogger<HealthCheckController>.Instance; | ||
var healthcheckService = new Mock<IHealthCheckService>(); | ||
healthcheckService.Setup( | ||
svc => svc.IndexIsHealthy() | ||
) | ||
.ReturnsAsync(false); | ||
|
||
var controller = new HealthCheckController(logger, healthcheckService.Object); | ||
|
||
var exception = await Assert.ThrowsAsync<APIErrorException>(() => controller.IsHealthy()); | ||
|
||
Assert.Equal(500, exception.HttpStatusCode); | ||
Assert.Equal(HealthCheckController.UNHEALTHY_STATUS, exception.Message); | ||
} | ||
|
||
|
||
/// <summary> | ||
/// Handle the health service failing. | ||
/// </summary> | ||
[Fact] | ||
public async void IsVeryUnhealthy() | ||
{ | ||
var logger = NullLogger<HealthCheckController>.Instance; | ||
var healthcheckService = new Mock<IHealthCheckService>(); | ||
healthcheckService.Setup( | ||
svc => svc.IndexIsHealthy() | ||
) | ||
.ThrowsAsync(new Exception("kaboom!")); | ||
|
||
|
||
var controller = new HealthCheckController(logger, healthcheckService.Object); | ||
|
||
var exception = await Assert.ThrowsAsync<APIErrorException>(() => controller.IsHealthy()); | ||
|
||
Assert.Equal(500, exception.HttpStatusCode); | ||
Assert.Equal(HealthCheckController.UNHEALTHY_STATUS, exception.Message); | ||
} | ||
|
||
|
||
/// <summary> | ||
/// Handle the healthcheck reporting a healthy status. | ||
/// </summary> | ||
[Fact] | ||
public async void IsHealthy() | ||
{ | ||
var logger = NullLogger<HealthCheckController>.Instance; | ||
var healthcheckService = new Mock<IHealthCheckService>(); | ||
healthcheckService.Setup( | ||
healthSvc => healthSvc.IndexIsHealthy() | ||
) | ||
.ReturnsAsync(true); | ||
|
||
var controller = new HealthCheckController(logger, healthcheckService.Object); | ||
|
||
string actual = await controller.IsHealthy(); | ||
|
||
healthcheckService.Verify( | ||
svc => svc.IndexIsHealthy(), | ||
Times.Once | ||
); | ||
|
||
Assert.Equal(HealthCheckController.HEALTHY_STATUS, actual); | ||
} | ||
|
||
} | ||
} |