Skip to content

Commit

Permalink
Merge branch 'master' into CV-251-add-reservations-to-bulk-upload
Browse files Browse the repository at this point in the history
# Conflicts:
#	src/CommitmentsV2/SFA.DAS.ReservationsV2.Api.Client.UnitTests/WhenCallingTheValidateReservationEndpoint.cs
#	src/CommitmentsV2/SFA.DAS.ReservationsV2.Api.Client/ReservationsApiClient.cs
#	src/SFA.DAS.Reservations.API.Types/IReservationsApiClient.cs
#	src/SFA.DAS.Reservations.Api.Client/HttpHelper.cs
#	src/SFA.DAS.Reservations.Api.Types.UnitTests/ReservationsHelper/WhenCallingTheValidateReservationEndpoint.cs
#	src/SFA.DAS.Reservations.Api.Types/IReservationHelper.cs
#	src/SFA.DAS.Reservations.Api.Types/ReservationsHelper.cs
  • Loading branch information
Pauljgraham committed Sep 19, 2019
2 parents 522f170 + 9f85df9 commit d11188c
Show file tree
Hide file tree
Showing 18 changed files with 286 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,6 @@ public Task CallPingEndpoint_ThenShouldReturnOkResponse()
{
return TestAsync(f => f.Client.GetAsync("/api/ping"), (f, r) => r.StatusCode.Should().Be(HttpStatusCode.OK));
}

[Test]
public Task CallHealthEndpoint_ThenShouldReturnOkResponse()
{
return TestAsync(f => f.Client.GetAsync("/health"), (f, r) => r.StatusCode.Should().Be(HttpStatusCode.OK));
}
}

public class HealthCheckFixture
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Diagnostics.HealthChecks;
using Moq;
using NUnit.Framework;
using SFA.DAS.Apprenticeships.Api.Types.Providers;
using SFA.DAS.CommitmentsV2.Api.HealthChecks;
using SFA.DAS.Providers.Api.Client;

namespace SFA.DAS.CommitmentsV2.Api.UnitTests.HealthChecks
{
[TestFixture]
[Parallelizable]
public class ApprenticeshipInfoServiceHealthCheckTests
{
private ApprenticeshipInfoServiceHealthCheckTestsFixture _fixture;

[SetUp]
public void SetUp()
{
_fixture = new ApprenticeshipInfoServiceHealthCheckTestsFixture();
}

[Test]
public async Task CheckHealthAsync_WhenFindAllAsyncSucceeds_ThenShouldReturnHealthyStatus()
{
var healthCheckResult = await _fixture.SetFindAllAsyncSuccess().CheckHealthAsync();

Assert.AreEqual(HealthStatus.Healthy, healthCheckResult.Status);
}

[Test]
public async Task CheckHealthAsync_WhenFindAllAsyncFails_ThenShouldReturnDegradedStatus()
{
var healthCheckResult = await _fixture.SetFindAllAsyncFailure().CheckHealthAsync();

Assert.AreEqual(HealthStatus.Degraded, healthCheckResult.Status);
Assert.AreEqual(_fixture.Exception.Message, healthCheckResult.Description);
}

private class ApprenticeshipInfoServiceHealthCheckTestsFixture
{
public HealthCheckContext HealthCheckContext { get; set; }
public CancellationToken CancellationToken { get; set; }
public Mock<IProviderApiClient> ProviderApiClient { get; set; }
public ApprenticeshipInfoServiceApiHealthCheck HealthCheck { get; set; }
public Exception Exception { get; set; }

public ApprenticeshipInfoServiceHealthCheckTestsFixture()
{
HealthCheckContext = new HealthCheckContext
{
Registration = new HealthCheckRegistration("Foo", Mock.Of<IHealthCheck>(), null, null)
};

ProviderApiClient = new Mock<IProviderApiClient>();
HealthCheck = new ApprenticeshipInfoServiceApiHealthCheck(ProviderApiClient.Object);
Exception = new Exception("Foobar");
}

public Task<HealthCheckResult> CheckHealthAsync()
{
return HealthCheck.CheckHealthAsync(HealthCheckContext, CancellationToken);
}

public ApprenticeshipInfoServiceHealthCheckTestsFixture SetFindAllAsyncSuccess()
{
ProviderApiClient.Setup(c => c.FindAllAsync()).ReturnsAsync(new List<ProviderSummary>());

return this;
}

public ApprenticeshipInfoServiceHealthCheckTestsFixture SetFindAllAsyncFailure()
{
ProviderApiClient.Setup(c => c.FindAllAsync()).ThrowsAsync(Exception);

return this;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Diagnostics.HealthChecks;
using Moq;
using NUnit.Framework;
using SFA.DAS.CommitmentsV2.Api.HealthChecks;
using SFA.DAS.Reservations.Api.Types;

namespace SFA.DAS.CommitmentsV2.Api.UnitTests.HealthChecks
{
[TestFixture]
[Parallelizable]
public class ReservationsApiHealthCheckTests
{
private ReservationsApiHealthCheckTestsFixture _fixture;

[SetUp]
public void SetUp()
{
_fixture = new ReservationsApiHealthCheckTestsFixture();
}

[Test]
public async Task CheckHealthAsync_WhenPingSucceeds_ThenShouldReturnHealthyStatus()
{
var healthCheckResult = await _fixture.SetPingSuccess().CheckHealthAsync();

Assert.AreEqual(HealthStatus.Healthy, healthCheckResult.Status);
}

[Test]
public async Task CheckHealthAsync_WhenPingFails_ThenShouldReturnDegradedStatus()
{
var healthCheckResult = await _fixture.SetPingFailure().CheckHealthAsync();

Assert.AreEqual(HealthStatus.Degraded, healthCheckResult.Status);
Assert.AreEqual(_fixture.Exception.Message, healthCheckResult.Description);
}

private class ReservationsApiHealthCheckTestsFixture
{
public HealthCheckContext HealthCheckContext { get; set; }
public CancellationToken CancellationToken { get; set; }
public Mock<IReservationsApiClient> ReservationsApiClient { get; set; }
public ReservationsApiHealthCheck HealthCheck { get; set; }
public Exception Exception { get; set; }

public ReservationsApiHealthCheckTestsFixture()
{
HealthCheckContext = new HealthCheckContext
{
Registration = new HealthCheckRegistration("Foo", Mock.Of<IHealthCheck>(), null, null)
};

ReservationsApiClient = new Mock<IReservationsApiClient>();
HealthCheck = new ReservationsApiHealthCheck(ReservationsApiClient.Object);
Exception = new Exception("Foobar");
}

public Task<HealthCheckResult> CheckHealthAsync()
{
return HealthCheck.CheckHealthAsync(HealthCheckContext, CancellationToken);
}

public ReservationsApiHealthCheckTestsFixture SetPingSuccess()
{
ReservationsApiClient.Setup(c => c.Ping(CancellationToken)).Returns(Task.CompletedTask);

return this;
}

public ReservationsApiHealthCheckTestsFixture SetPingFailure()
{
ReservationsApiClient.Setup(c => c.Ping(CancellationToken)).ThrowsAsync(Exception);

return this;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
<PackageReference Include="NUnit3TestAdapter" Version="3.12.0" />
<PackageReference Include="SFA.DAS.Apprenticeships.Api.Client" Version="0.11.174" />
<PackageReference Include="SFA.DAS.Apprenticeships.Api.Types" Version="0.11.174" />
<PackageReference Include="SFA.DAS.Providers.Api.Client" Version="0.11.98" />
<PackageReference Include="SFA.DAS.Testing" Version="3.0.22" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public static class IoC
public static void Initialize(Registry registry)
{
registry.IncludeRegistry<AcademicYearDateProviderRegistry>();
registry.IncludeRegistry<ApprenticeshipInfoServiceRegistry>();
registry.IncludeRegistry<AuthorizationRegistry>();
registry.IncludeRegistry<ConfigurationRegistry>();
registry.IncludeRegistry<CurrentDateTimeRegistry>();
Expand All @@ -28,7 +29,6 @@ public static void Initialize(Registry registry)
registry.IncludeRegistry<NServiceBusClientUnitOfWorkRegistry>();
registry.IncludeRegistry<NServiceBusUnitOfWorkRegistry>();
registry.IncludeRegistry<ReservationsApiClientRegistry>();
registry.IncludeRegistry<TrainingProgrammeRegistry>();
registry.IncludeRegistry<DefaultRegistry>();
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Diagnostics.HealthChecks;
using SFA.DAS.Providers.Api.Client;

namespace SFA.DAS.CommitmentsV2.Api.HealthChecks
{
public class ApprenticeshipInfoServiceApiHealthCheck : IHealthCheck
{
private readonly IProviderApiClient _providerApiClient;

public ApprenticeshipInfoServiceApiHealthCheck(IProviderApiClient providerApiClient)
{
_providerApiClient = providerApiClient;
}

public async Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default)
{
try
{
await _providerApiClient.FindAllAsync();

return HealthCheckResult.Healthy();
}
catch (Exception exception)
{
return HealthCheckResult.Degraded(exception.Message);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public NServiceBusHealthCheck(IMessageSession messageSession, IDistributedCache
_distributedCache = distributedCache;
}

public async Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = new CancellationToken())
public async Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default)
{
var messageId = Guid.NewGuid();
var data = new Dictionary<string, object> { ["MessageId"] = messageId };
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Diagnostics.HealthChecks;
using SFA.DAS.Reservations.Api.Types;

namespace SFA.DAS.CommitmentsV2.Api.HealthChecks
{
public class ReservationsApiHealthCheck : IHealthCheck
{
private readonly IReservationsApiClient _reservationsApiClient;

public ReservationsApiHealthCheck(IReservationsApiClient reservationsApiClient)
{
_reservationsApiClient = reservationsApiClient;
}

public async Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default)
{
try
{
await _reservationsApiClient.Ping(cancellationToken);

return HealthCheckResult.Healthy();
}
catch (Exception exception)
{
return HealthCheckResult.Degraded(exception.Message);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ public static IServiceCollection AddDasHealthChecks(this IServiceCollection serv
var databaseConnectionString = configuration.GetValue<string>(CommitmentsConfigurationKeys.DatabaseConnectionString);

services.AddHealthChecks()
.AddCheck<ApprenticeshipInfoServiceApiHealthCheck>("Apprenticeship Info Service API Health Check")
.AddCheck<NServiceBusHealthCheck>("Service Bus Health Check")
.AddCheck<ReservationsApiHealthCheck>("Reservations API Health Check")
.AddSqlServer(databaseConnectionString, name: "Commitments DB Health Check");

return services;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
using SFA.DAS.CommitmentsV2.Configuration;
using SFA.DAS.CommitmentsV2.Data;
using SFA.DAS.CommitmentsV2.Data;
using SFA.DAS.CommitmentsV2.Jobs.ScheduledJobs;
using SFA.DAS.Providers.Api.Client;
using StructureMap;

namespace SFA.DAS.CommitmentsV2.Jobs.DependencyResolution
Expand All @@ -12,7 +10,6 @@ public DefaultRegistry()
{
For<ImportProvidersJobs>();
For<IDbContextFactory>().Use<DbContextFactory>();
For<IProviderApiClient>().Use(c => new ProviderApiClient(c.GetInstance<ApprenticeshipInfoServiceConfiguration>().BaseUrl));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ public static class IoC
{
public static void Initialize(Registry registry)
{
registry.IncludeRegistry<ApprenticeshipInfoServiceRegistry>();
registry.IncludeRegistry<ConfigurationRegistry>();
registry.IncludeRegistry<DataRegistry>();
registry.IncludeRegistry<DefaultRegistry>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
<PackageReference Include="NLog.Extensions.Logging" Version="1.4.0" />
<PackageReference Include="SFA.DAS.Configuration.AzureTableStorage" Version="3.0.1" />
<PackageReference Include="SFA.DAS.NLog.Targets.Redis" Version="1.2.1" />
<PackageReference Include="SFA.DAS.Providers.Api.Client" Version="0.11.98" />
<PackageReference Include="StructureMap" Version="4.7.0" />
<PackageReference Include="StructureMap.Microsoft.DependencyInjection" Version="2.0.0" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,19 @@
using MediatR;
using SFA.DAS.Apprenticeships.Api.Client;
using SFA.DAS.Apprenticeships.Api.Client;
using SFA.DAS.CommitmentsV2.Configuration;
using SFA.DAS.CommitmentsV2.Domain.Interfaces;
using SFA.DAS.CommitmentsV2.Mapping;
using SFA.DAS.CommitmentsV2.Services;
using SFA.DAS.Providers.Api.Client;
using StructureMap;
using StructureMap.Pipeline;

namespace SFA.DAS.CommitmentsV2.DependencyResolution
{
public class TrainingProgrammeRegistry : Registry
public class ApprenticeshipInfoServiceRegistry : Registry
{
private const string ServiceName = "SFA.DAS.CommitmentsV2";

public TrainingProgrammeRegistry()
public ApprenticeshipInfoServiceRegistry()
{
// You'll also need to use the call AddMemoryCache in MVC startup to make IMemoryCache available
For<IStandardApiClient>().Use<StandardApiClient>().Ctor<string>("baseUrl").Is(ctx => ctx.GetInstance<ApprenticeshipInfoServiceConfiguration>().BaseUrl);
For<IFrameworkApiClient>().Use<FrameworkApiClient>().Ctor<string>("baseUrl").Is(ctx => ctx.GetInstance<ApprenticeshipInfoServiceConfiguration>().BaseUrl);

For<IProviderApiClient>().Use<ProviderApiClient>().Ctor<string>("baseUrl").Is(ctx => ctx.GetInstance<ApprenticeshipInfoServiceConfiguration>().BaseUrl);
For<IStandardApiClient>().Use<StandardApiClient>().Ctor<string>("baseUrl").Is(ctx => ctx.GetInstance<ApprenticeshipInfoServiceConfiguration>().BaseUrl);
For<ITrainingProgrammeApiClient>().Use<TrainingProgrammeApiClient>().Singleton();
For<ITrainingProgrammeLookup>().Use<TrainingProgrammeLookup>().Singleton();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
<PackageReference Include="SFA.DAS.Learners" Version="1.1.181" />
<PackageReference Include="SFA.DAS.NServiceBus" Version="15.0.8" />
<PackageReference Include="SFA.DAS.NServiceBus.SqlServer" Version="15.0.8" />
<PackageReference Include="SFA.DAS.Providers.Api.Client" Version="0.11.98" />
<PackageReference Include="SFA.DAS.UnitOfWork" Version="9.0.8" />
<PackageReference Include="SFA.DAS.Configuration.AzureTableStorage" Version="3.0.1" />
<PackageReference Include="StructureMap" Version="4.7.0" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp2.2</TargetFramework>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ namespace SFA.DAS.Reservations.Api.Types
{
public interface IReservationsApiClient
{
Task Ping(CancellationToken cancellationToken);
Task<ReservationValidationResult> ValidateReservation(ReservationValidationMessage request, CancellationToken cancellationToken);
Task<ReservationAllocationStatusResult> GetReservationAllocationStatus(ReservationAllocationStatusMessage request, CancellationToken cancellationToken);
Task<BulkCreateReservationsResult> BulkCreateReservations(long accountLegalEntity, BulkCreateReservationsRequest request, CancellationToken cancellationToken);
Expand Down
Loading

0 comments on commit d11188c

Please sign in to comment.