Skip to content

Commit

Permalink
Merge pull request #621 from SkillsFundingAgency/CV-517-Get-Approved-…
Browse files Browse the repository at this point in the history
…Cohorts

Cv 517 get approved cohorts
  • Loading branch information
Najamuddin-Muhammad authored Jan 23, 2020
2 parents e4de05a + 5e436de commit 48fd481
Show file tree
Hide file tree
Showing 13 changed files with 379 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,11 @@ public Task<string> SecureProviderCheck()
return _client.Get("api/test/provider");
}

public Task<GetApprovedProvidersResponse> GetApprovedProviders(long accountId, CancellationToken cancellationToken)
{
return _client.Get<GetApprovedProvidersResponse>($"api/accounts/{accountId}/providers/approved", null, cancellationToken);
}

public Task<CreateCohortResponse> CreateCohort(CreateEmptyCohortRequest request, CancellationToken cancellationToken = default)
{
return _client.PostAsJson<CreateEmptyCohortRequest, CreateCohortResponse>("api/cohorts/create-empty-cohort", request, cancellationToken);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public interface ICommitmentsApiClient
Task<GetCohortsResponse> GetCohorts(GetCohortsRequest request, CancellationToken cancellationToken = default);
Task DeleteDraftApprenticeship(long cohortId, long apprenticeshipId, DeleteDraftApprenticeshipRequest request, CancellationToken cancellationToken = default);
Task DeleteCohort(long cohortId, UserInfo userInfo, CancellationToken cancellationToken = default);
Task<GetApprovedProvidersResponse> GetApprovedProviders(long accountId, CancellationToken cancellationToken);
Task<AccountResponse> GetAccount(long accountId, CancellationToken cancellationToken = default);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System.Collections.Generic;
using System.Linq;

namespace SFA.DAS.CommitmentsV2.Api.Types.Responses
{
public class GetApprovedProvidersResponse
{
public long[] ProviderIds { get; }

public GetApprovedProvidersResponse(IEnumerable<long> providerIds)
{
ProviderIds = providerIds.ToArray();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Threading;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using AutoFixture;
using MediatR;
Expand All @@ -8,6 +9,7 @@
using SFA.DAS.CommitmentsV2.Api.Controllers;
using SFA.DAS.CommitmentsV2.Api.Types.Responses;
using SFA.DAS.CommitmentsV2.Application.Queries.GetAccountSummary;
using SFA.DAS.CommitmentsV2.Application.Queries.GetApprovedProviders;

namespace SFA.DAS.CommitmentsV2.Api.UnitTests.Controllers
{
Expand All @@ -29,12 +31,21 @@ public async Task GetAccount_Should_Return_Valid_Result()
_fixture.VerifyResult();
}

[Test]
public async Task GetApprovedProviders_Should_Return_Valid_Result()
{
await _fixture.GetApprovedProviders();
_fixture.VerifyApprovedProviderResponse();
}

private class AccountControllerTestsFixture
{
private AccountController Controller { get; }
private Mock<IMediator> Mediator { get; }
private long AccountId { get; }
private GetAccountSummaryQueryResult MediatorQueryResult { get; }

private GetApprovedProvidersQueryResult ApprovedProviderQueryResponse { get; }
private IActionResult Result { get; set; }

public AccountControllerTestsFixture()
Expand All @@ -46,6 +57,10 @@ public AccountControllerTestsFixture()
Mediator.Setup(x => x.Send(It.IsAny<GetAccountSummaryQuery>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(MediatorQueryResult);

ApprovedProviderQueryResponse = autoFixture.Create<GetApprovedProvidersQueryResult>();
Mediator.Setup(x => x.Send(It.IsAny<GetApprovedProvidersQuery>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(ApprovedProviderQueryResponse);

Controller = new AccountController(Mediator.Object);
AccountId = autoFixture.Create<long>();
}
Expand All @@ -55,6 +70,11 @@ public async Task GetAccount()
Result = await Controller.GetAccount(AccountId);
}

public async Task GetApprovedProviders()
{
Result = await Controller.GetApprovedProviders(AccountId);
}

public void VerifyResult()
{
Assert.AreEqual(typeof(OkObjectResult), Result.GetType());
Expand All @@ -69,6 +89,24 @@ public void VerifyResult()
Assert.AreEqual(MediatorQueryResult.HasApprenticeships, response.HasApprenticeships);
Assert.AreEqual(MediatorQueryResult.HasCohorts, response.HasCohorts);
}

public void VerifyApprovedProviderResponse()
{
Assert.AreEqual(typeof(OkObjectResult), Result.GetType());
var objectResult = (OkObjectResult)Result;
Assert.AreEqual(200, objectResult.StatusCode);

Assert.IsInstanceOf<GetApprovedProvidersResponse>(objectResult.Value);

var response = (GetApprovedProvidersResponse)objectResult.Value;

Assert.AreEqual(3, response.ProviderIds.Length);

foreach (var qr in ApprovedProviderQueryResponse.ProviderIds)
{
Assert.IsTrue(response.ProviderIds.Contains(qr));
}
}
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@
using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using SFA.DAS.CommitmentsV2.Api.Extensions;
using SFA.DAS.CommitmentsV2.Api.Types.Responses;
using SFA.DAS.CommitmentsV2.Application.Queries.GetAccountSummary;
using SFA.DAS.CommitmentsV2.Application.Queries.GetApprovedProviders;

namespace SFA.DAS.CommitmentsV2.Api.Controllers
{
Expand Down Expand Up @@ -37,5 +36,16 @@ public async Task<IActionResult> GetAccount(long accountId)
HasCohorts = employer.HasCohorts
});
}

[HttpGet]
[Route("{AccountId}/providers/approved")]
public async Task<IActionResult> GetApprovedProviders(long accountId)
{
var query = new GetApprovedProvidersQuery(accountId);

var result = await _mediator.Send(query);

return Ok(new GetApprovedProvidersResponse(result.ProviderIds));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public ProviderController(IMediator mediator)
{
_mediator = mediator;
}

[HttpGet]
[Route("{providerId}")]
public async Task<IActionResult> GetProvider(long providerId)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
using MediatR;
using Microsoft.EntityFrameworkCore;
using NUnit.Framework;
using SFA.DAS.CommitmentsV2.Application.Queries.GetApprovedProviders;
using SFA.DAS.CommitmentsV2.Data;
using SFA.DAS.CommitmentsV2.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

namespace SFA.DAS.CommitmentsV2.UnitTests.Application.Queries.GetApprovedProviders
{
[TestFixture]
[Parallelizable]
public class GetApprovedProvidersQueryHandlerTests
{
private GetApprovedProvidersQueryHandlerFixture _fixture;

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

[Test]
public async Task Handle_WhenCohortApprovedByBoth_And_TransferSender_IdIsNull_ThenShouldReturnResult()
{
var result = await _fixture.AddApprovedCohortAndProviderForAccount()
.AddApprovedCohortAndProviderForAccount()
.AddNotApprovedCohortAndProviderForAccount()
.SeedDb().Handle();

Assert.AreEqual(2, result.ProviderIds.Count());
}

[Test]
public async Task Handle_WhenCohortApprovedByBoth_And_TransferSenderNotApproved_ThenShouldNotReturnResult()
{
var result = await _fixture.AddCohortAndProvider_WithTransfserSenderNotApproved()
.SeedDb().Handle();

Assert.AreEqual(0, result.ProviderIds.Count());
}

[Test]
public async Task Handle_WhenCohortApprovedByBoth_And_TransferSenderApproved_ThenShouldReturnResult()
{
var result = await _fixture.AddCohortAndProvider_WithTransferSenderApproved()
.SeedDb().Handle();

Assert.AreEqual(1, result.ProviderIds.Count());
}

[Test]
public async Task Handle_WhenCohortNotFullyApproved_ThenShouldNotReturnResult()
{
var result = await _fixture.AddNotApprovedCohortAndProviderForAccount().SeedDb().Handle();

Assert.AreEqual(0, result.ProviderIds.Count());
}
}

public class GetApprovedProvidersQueryHandlerFixture
{
public GetApprovedProvidersQuery Query { get; set; }
public List<Cohort> Cohorts { get; set; }
public List<Provider> Provider { get; set; }
public ProviderCommitmentsDbContext Db { get; set; }
public IRequestHandler<GetApprovedProvidersQuery, GetApprovedProvidersQueryResult> Handler { get; set; }

public long AccountId => 1;

public GetApprovedProvidersQueryHandlerFixture()
{
Cohorts = new List<Cohort>();
Provider = new List<Provider>();
Query = new GetApprovedProvidersQuery(AccountId);
Db = new ProviderCommitmentsDbContext(new DbContextOptionsBuilder<ProviderCommitmentsDbContext>().UseInMemoryDatabase(Guid.NewGuid().ToString()).Options);
Handler = new GetApprovedProvidersQueryHandler(new Lazy<ProviderCommitmentsDbContext>(() => Db));
}

public GetApprovedProvidersQueryHandlerFixture AddCohortAndProvider_WithTransfserSenderNotApproved()
{
var provider = new Provider(GetNextProviderId(), "Foo", DateTime.UtcNow, DateTime.UtcNow);

Provider.Add(provider);

Cohorts.Add(new Cohort
{
EmployerAccountId = AccountId,
Id = GetNextCohortId(),
EditStatus = 0,
TransferSenderId = 1,
ProviderId = provider.UkPrn,
TransferApprovalStatus = Types.TransferApprovalStatus.Pending
});

return this;
}

public GetApprovedProvidersQueryHandlerFixture AddCohortAndProvider_WithTransferSenderApproved()
{
var provider = new Provider(GetNextProviderId(), "Foo", DateTime.UtcNow, DateTime.UtcNow);

Provider.Add(provider);

Cohorts.Add(new Cohort
{
EmployerAccountId = AccountId,
Id = GetNextCohortId(),
EditStatus = 0,
TransferSenderId = 1,
ProviderId = provider.UkPrn,
TransferApprovalStatus = Types.TransferApprovalStatus.Approved
});

return this;
}

public GetApprovedProvidersQueryHandlerFixture AddApprovedCohortAndProviderForAccount()
{
var provider = new Provider(GetNextProviderId(), "Foo", DateTime.UtcNow, DateTime.UtcNow);

Provider.Add(provider);

Cohorts.Add(new Cohort
{
EmployerAccountId = AccountId,
Id = GetNextCohortId(),
EditStatus = 0,
TransferSenderId = null,
ProviderId = provider.UkPrn
});

return this;
}

public GetApprovedProvidersQueryHandlerFixture AddNotApprovedCohortAndProviderForAccount()
{
Provider.Add(
new Provider(GetNextProviderId(), "Foo", DateTime.UtcNow, DateTime.UtcNow)
);

Cohorts.Add(new Cohort
{
EmployerAccountId = AccountId,
Id = GetNextCohortId(),
EditStatus = Types.EditStatus.EmployerOnly,
TransferSenderId = null,
ProviderId = 1
});

return this;
}

public Task<GetApprovedProvidersQueryResult> Handle()
{
return Handler.Handle(Query, CancellationToken.None);
}

public GetApprovedProvidersQueryHandlerFixture SeedDb()
{
Db.Cohorts.AddRange(Cohorts);
Db.Providers.AddRange(Provider);
Db.SaveChanges();

return this;
}

private long GetNextCohortId() => Cohorts.Count == 0 ? 1 : Cohorts.Max(x => x.Id) + 1;

private long GetNextProviderId() => Provider.Count == 0 ? 1: Provider.Max(x => x.UkPrn) + 1;

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using FluentValidation.Results;
using NUnit.Framework;
using SFA.DAS.CommitmentsV2.Application.Queries.GetApprovedProviders;

namespace SFA.DAS.CommitmentsV2.UnitTests.Application.Queries.GetProvider
{
[TestFixture]
[Parallelizable]
public class GetApprovedProvidersQueryValidatorTests
{
private GetApproveProvidersdQueryValidatorTestsFixture _fixture;

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

[TestCase(-1, false)]
[TestCase( 0, false)]
[TestCase( 1, true)]
public void Validate_WhenValidating_ThenShouldValidate(int accountId, bool isValid)
{
var validationResult = _fixture.Validate(accountId);

Assert.AreEqual(isValid, validationResult.IsValid);
}
}

public class GetApproveProvidersdQueryValidatorTestsFixture
{
public GetApprovedProvidersQueryValidator Validator { get; set; }

public GetApproveProvidersdQueryValidatorTestsFixture()
{
Validator = new GetApprovedProvidersQueryValidator();
}

public ValidationResult Validate(long providerId)
{
return Validator.Validate(new GetApprovedProvidersQuery(providerId));
}
}
}
Loading

0 comments on commit 48fd481

Please sign in to comment.