Skip to content

Commit

Permalink
fix up tests
Browse files Browse the repository at this point in the history
Signed-off-by: Neil South <[email protected]>
  • Loading branch information
neildsouth committed Jul 24, 2024
1 parent 9127120 commit dfa94b5
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 20 deletions.
7 changes: 2 additions & 5 deletions src/InformaticsGateway/Repositories/MonaiServiceLocator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,11 @@ public Dictionary<string, ServiceStatus> GetServiceStatus()
{
Guard.Against.Null(type, nameof(type));

var hostedServices = _serviceProvider.GetServices<IHostedService>().ToList();
var t = (hostedServices[2].GetType()).Name;

var TypeInterface = type.GetInterfaces().FirstOrDefault(i => i.Name == $"I{type.Name}");
if (TypeInterface is null)
{
var te = _serviceProvider.GetServices<IHostedService>().FirstOrDefault(i => i.GetType().Name == type.Name);
return te as IMonaiService;
var service = _serviceProvider.GetServices<IHostedService>()?.FirstOrDefault(i => i.GetType().Name == type.Name);
return service as IMonaiService;
}
return (_serviceProvider.GetService(TypeInterface) as IMonaiService);

Expand Down
34 changes: 22 additions & 12 deletions src/InformaticsGateway/Test/Repositories/MonaiServiceLocatorTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
*/

using System;
using System.Collections.Generic;
using DotNext.Collections.Generic;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Monai.Deploy.InformaticsGateway.Api.Rest;
using Monai.Deploy.InformaticsGateway.Repositories;
using Monai.Deploy.InformaticsGateway.Services.Common;
Expand All @@ -37,26 +41,32 @@ public MonaiServiceLocatorTest()
mock.SetupGet(p => p.ServiceName).Returns(type.Name);
return mock.Object;
});

_serviceProvider.Setup(sp => sp.GetService(typeof(IEnumerable<IHostedService>)))
.Returns((Type type) =>
{
var mock = new Mock<IHostedService>();
return new List<IHostedService> { mock.Object };
});
}

[Fact(DisplayName = "GetMonaiServices")]
public void GetMonaiServices()
{
var hosted = new List<IHostedService>()
{
new Mock<IHostedService>().Object
};



var serviceLocator = new MonaiServiceLocator(_serviceProvider.Object);
var result = serviceLocator.GetMonaiServices();

Assert.Collection(result,
items => items.ServiceName.Equals("DataRetrievalService"),
items => items.ServiceName.Equals("ScpService"),
items => items.ServiceName.Equals("ScuService"),
items => items.ServiceName.Equals("ExtAppScuService"),
items => items.ServiceName.Equals("SpaceReclaimerService"),
items => items.ServiceName.Equals("DicomWebExportService"),
items => items.ServiceName.Equals("ScuExportService"),
items => items.ServiceName.Equals("PayloadNotificationService"),
items => items.ServiceName.Equals("HL7 Service"),
items => items.ServiceName.Equals("ExtAppScuExportService"),
items => items.ServiceName.Equals("Hl7ExportService"));
items => items.ServiceName.Equals("IMllpService"),
items => items.ServiceName.Equals("IPayloadService"));

}

[Fact(DisplayName = "GetServiceStatus")]
Expand All @@ -65,7 +75,7 @@ public void GetServiceStatus()
var serviceLocator = new MonaiServiceLocator(_serviceProvider.Object);
var result = serviceLocator.GetServiceStatus();

Assert.Equal(11, result.Count);
Assert.Equal(2, result.Count);
foreach (var svc in result.Keys)
{
Assert.Equal(ServiceStatus.Running, result[svc]);
Expand Down
9 changes: 6 additions & 3 deletions src/InformaticsGateway/Test/Services/Http/MonaiHealthCheckTest.cs
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.Extensions.Diagnostics.HealthChecks;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using Monai.Deploy.InformaticsGateway.Repositories;
using Monai.Deploy.InformaticsGateway.Services.Http;
using Moq;
Expand All @@ -27,6 +29,7 @@ namespace Monai.Deploy.InformaticsGateway.Test.Services.Http
public class MonaiHealthCheckTest
{
private readonly Mock<IMonaiServiceLocator> _monaiServiceLocator;
private readonly ILogger<MonaiHealthCheck> _logger = new NullLogger<MonaiHealthCheck>();

public MonaiHealthCheckTest()
{
Expand All @@ -43,7 +46,7 @@ public async Task GivenAllServicesRunning_WhenCheckHealthAsyncIsCalled_ReturnsHe
{ "C", Api.Rest.ServiceStatus.Running },
});

var svc = new MonaiHealthCheck(_monaiServiceLocator.Object);
var svc = new MonaiHealthCheck(_monaiServiceLocator.Object, _logger);
var result = await svc.CheckHealthAsync(null);
Assert.Equal(HealthStatus.Healthy, result.Status);
}
Expand All @@ -58,7 +61,7 @@ public async Task GivenSomeServicesNotRunning_WhenCheckHealthAsyncIsCalled_Retur
{ "C", Api.Rest.ServiceStatus.Stopped },
});

var svc = new MonaiHealthCheck(_monaiServiceLocator.Object);
var svc = new MonaiHealthCheck(_monaiServiceLocator.Object, _logger);
var result = await svc.CheckHealthAsync(null);
Assert.Equal(HealthStatus.Degraded, result.Status);
Assert.Equal(Api.Rest.ServiceStatus.Cancelled, result.Data["B"]);
Expand All @@ -75,7 +78,7 @@ public async Task GivenAllServicesNotRunning_WhenCheckHealthAsyncIsCalled_Return
{ "C", Api.Rest.ServiceStatus.Stopped },
});

var svc = new MonaiHealthCheck(_monaiServiceLocator.Object);
var svc = new MonaiHealthCheck(_monaiServiceLocator.Object, _logger);
var result = await svc.CheckHealthAsync(null);

Assert.Equal(HealthStatus.Unhealthy, result.Status);
Expand Down

0 comments on commit dfa94b5

Please sign in to comment.