Skip to content

Commit

Permalink
Merge pull request #2063 from SkillsFundingAgency/CON-1719_Handle-com…
Browse files Browse the repository at this point in the history
…mitments-api-fail

[CON-1719] Should handle add apprentice api call failures gracefully.…
  • Loading branch information
cofaulco authored Apr 16, 2020
2 parents 8510620 + 614201a commit f7235ef
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public void Arrange()
_hashingService = new Mock<IHashingService>();
_hashingService.Setup(x => x.DecodeValue(hashedAccountId)).Returns(_accountId);

RequestHandler = new GetSingleCohortRequestHandler(RequestValidator.Object, _commitmentV2Service.Object, _hashingService.Object);
RequestHandler = new GetSingleCohortRequestHandler(RequestValidator.Object, _commitmentV2Service.Object, _hashingService.Object, Mock.Of<ILog>());

Query = new GetSingleCohortRequest
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -404,5 +404,34 @@ public async Task ThenShouldGetCohortResponse()
//Assert
Assert.AreEqual(1, result.Data.CallToActionViewModel.CohortsCount);
}

[Test]
[TestCase(false, false, false, false, false, Description = "All calls successful")]
[TestCase(true, false, false, false, true, Description = "Vacancy call failed")]
[TestCase(false, true, false, false, true, Description = "Vacancy call failed")]
[TestCase(false, false, true, false, true, Description = "Cohort call failed")]
public async Task ThenShouldSetUnableToDetermineCallToAction(
bool vacancyCallFailed,
bool reservationsCallFailed,
bool cohortCallFailed,
bool apprenticeshipCallFailed,
bool unableToDetermineCallToActionExpected)
{
//Arrange
_mediator.Setup(x => x.SendAsync(It.IsAny<GetVacanciesRequest>())).ReturnsAsync(new GetVacanciesResponse { Vacancies = new List<Vacancy>(), HasFailed = vacancyCallFailed });

_mediator.Setup(x => x.SendAsync(It.IsAny<GetReservationsRequest>())).ReturnsAsync(new GetReservationsResponse { Reservations = new List<Reservation>(), HasFailed = reservationsCallFailed });

_mediator.Setup(x => x.SendAsync(It.IsAny<GetApprenticeshipsRequest>())).ReturnsAsync(new GetApprenticeshipsResponse{ Apprenticeships = new List<Apprenticeship>(), HasFailed = apprenticeshipCallFailed });

var cohort = new Cohort() { Id = 1, NumberOfDraftApprentices = 1, Apprenticeships = new List<Apprenticeship> { new Apprenticeship { FirstName = "FirstName" } } };
_mediator.Setup(x => x.SendAsync(It.IsAny<GetSingleCohortRequest>())).ReturnsAsync(new GetSingleCohortResponse { Cohort = cohort, HasFailed = cohortCallFailed });

//Act
var result = await _orchestrator.GetAccount(HashedAccountId, UserId);

//Assert
Assert.AreEqual(unableToDetermineCallToActionExpected, result.Data.CallToActionViewModel.UnableToDetermineCallToAction);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ public virtual async Task<OrchestratorResponse<AccountDashboardViewModel>> GetAc
{
_mapper.Map<Cohort, CohortViewModel>(accountCohort.Cohort)
},
UnableToDetermineCallToAction = vacanciesResponse.HasFailed || reservationsResponse.HasFailed
UnableToDetermineCallToAction = vacanciesResponse.HasFailed || reservationsResponse.HasFailed || accountCohort.HasFailed || apprenticeshipsResponse.HasFailed
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using SFA.DAS.Validation;
using SFA.DAS.EmployerAccounts.Interfaces;
using SFA.DAS.HashingService;
using System;

namespace SFA.DAS.EmployerAccounts.Queries.GetApprenticeship
{
Expand Down Expand Up @@ -37,10 +38,21 @@ public async Task<GetApprenticeshipsResponse> Handle(GetApprenticeshipsRequest m

long accountId = _hashingService.DecodeValue(message.HashedAccountId);

return new GetApprenticeshipsResponse
try
{
Apprenticeships = await _commitmentV2Service.GetApprenticeships(accountId)
};
return new GetApprenticeshipsResponse
{
Apprenticeships = await _commitmentV2Service.GetApprenticeships(accountId)
};
}
catch(Exception ex)
{
_logger.Error(ex, $"Failed to get Cohorts for {message.HashedAccountId}");
return new GetApprenticeshipsResponse
{
HasFailed = true
};
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ namespace SFA.DAS.EmployerAccounts.Queries.GetApprenticeship
public class GetApprenticeshipsResponse
{
public IEnumerable<Apprenticeship> Apprenticeships { get; set; }
public bool HasFailed { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
using SFA.DAS.EmployerAccounts.Interfaces;
using System.Linq;
using SFA.DAS.HashingService;
using System;
using SFA.DAS.NLog.Logger;

namespace SFA.DAS.EmployerAccounts.Queries.GetSingleCohort
{
Expand All @@ -12,15 +14,18 @@ public class GetSingleCohortRequestHandler : IAsyncRequestHandler<GetSingleCohor
private readonly IValidator<GetSingleCohortRequest> _validator;
private readonly ICommitmentV2Service _commitmentV2Service;
private readonly IHashingService _hashingService;
private readonly ILog _logger;

public GetSingleCohortRequestHandler(
IValidator<GetSingleCohortRequest> validator,
ICommitmentV2Service commitmentV2Service,
IHashingService hashingService)
IHashingService hashingService,
ILog logger)
{
_validator = validator;
_commitmentV2Service = commitmentV2Service;
_hashingService = hashingService;
_logger = logger;
}

public async Task<GetSingleCohortResponse> Handle(GetSingleCohortRequest message)
Expand All @@ -32,22 +37,33 @@ public async Task<GetSingleCohortResponse> Handle(GetSingleCohortRequest message
throw new InvalidRequestException(validationResult.ValidationDictionary);
}

long accountId = _hashingService.DecodeValue(message.HashedAccountId);
long accountId = _hashingService.DecodeValue(message.HashedAccountId);

var cohortsResponse = await _commitmentV2Service.GetCohorts(accountId);
if (cohortsResponse.Count() != 1) return new GetSingleCohortResponse();
try
{
var cohortsResponse = await _commitmentV2Service.GetCohorts(accountId);
if (cohortsResponse.Count() != 1) return new GetSingleCohortResponse();

var singleCohort = cohortsResponse.Single();
var singleCohort = cohortsResponse.Single();

if (singleCohort.NumberOfDraftApprentices > 0)
{
singleCohort.Apprenticeships = await _commitmentV2Service.GetDraftApprenticeships(singleCohort);
}
if (singleCohort.NumberOfDraftApprentices > 0)
{
singleCohort.Apprenticeships = await _commitmentV2Service.GetDraftApprenticeships(singleCohort);
}

return new GetSingleCohortResponse
return new GetSingleCohortResponse
{
Cohort = singleCohort
};
}
catch(Exception ex)
{
Cohort = singleCohort
};
_logger.Error(ex, $"Failed to get Cohorts for {message.HashedAccountId}");
return new GetSingleCohortResponse
{
HasFailed = true
};
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ namespace SFA.DAS.EmployerAccounts.Queries.GetSingleCohort
public class GetSingleCohortResponse
{
public Cohort Cohort { get; set; }
public bool HasFailed { get; set; }
}
}

0 comments on commit f7235ef

Please sign in to comment.