Skip to content

Commit

Permalink
CON-1640-fix code review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
shomavg committed Apr 21, 2020
1 parent a70231c commit 1c179eb
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 286 deletions.
Original file line number Diff line number Diff line change
@@ -1,69 +1,67 @@
using SFA.DAS.CommitmentsV2.Api.Client;
using System;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using AutoMapper;
using Moq;
using NUnit.Framework;
using SFA.DAS.EmployerAccounts.Services;
using SFA.DAS.CommitmentsV2.Api.Types.Requests;
using System.Threading;
using SFA.DAS.CommitmentsV2.Api.Types.Responses;
using SFA.DAS.CommitmentsV2.Types;
using SFA.DAS.CommitmentsV2.Types.Dtos;
using System.Linq;
using Polly;
using Polly.Registry;
using Polly.Timeout;
using SFA.DAS.EmployerAccounts.Interfaces;
using SFA.DAS.EmployerAccounts.Models.CommitmentsV2;
using SFA.DAS.Encoding;

namespace SFA.DAS.EmployerAccounts.UnitTests.Services.AccountCohorts
{
public class WhenIGetAccountCohortsWithTimeout
{
private Mock<ICommitmentsApiClient> _mockCommitmentsApiClient;
private Mock<ICommitmentV2Service> _mockCommitmentsV2Service;
private CommitmentsV2ServiceWithTimeout _commitmentsV2ServiceWithTimeout;
private Mock<IEncodingService> _mockEncodingService;
private Mock<IMapper> _mockMapper;
private long _accountId = 123;
private IAsyncPolicy _policy;
private List<Apprenticeship> _apprenticeships;
private List<Cohort> _cohorts;
private Cohort _cohort;

[SetUp]
public void Arrange()
{
_mockMapper = new Mock<IMapper>();
_mockCommitmentsApiClient = new Mock<ICommitmentsApiClient>();
_mockEncodingService = new Mock<IEncodingService>();
_cohort=new Cohort(){Id = 123};
_cohorts = new List<Cohort>
{
new Cohort() {Id = 123}
};

_apprenticeships = new List<Apprenticeship>
{
new Apprenticeship {ApprenticeshipStatus = ApprenticeshipStatus.Approved, FirstName = "FirstName", LastName = "LastName"}
};

_mockCommitmentsV2Service = new Mock<ICommitmentV2Service>();
_policy = Policy.NoOpAsync();
var registryPolicy = new PolicyRegistry();
registryPolicy.Add(Constants.DefaultServiceTimeout, _policy);
_mockCommitmentsApiClient
.Setup(c => c.GetApprenticeships(It.Is<GetApprenticeshipsRequest>(r => r.AccountId == _accountId), CancellationToken.None))
.Returns(Task.FromResult(CreateApprenticeshipResponse()));

_mockCommitmentsApiClient.Setup(c => c.GetCohorts(It.Is<GetCohortsRequest>(r => r.AccountId == _accountId), CancellationToken.None))
.Returns(Task.FromResult(GetCohortsResponseForWithTrainingProviderStaus()));
_mockCommitmentsV2Service
.Setup(c => c.GetApprenticeships(_accountId))
.ReturnsAsync(_apprenticeships);

_mockCommitmentsV2Service
.Setup(c => c.GetCohorts(_accountId))
.ReturnsAsync(_cohorts);

_mockCommitmentsApiClient.Setup(c => c.GetDraftApprenticeships(123, It.IsAny<CancellationToken>())).Returns(Task.FromResult(GetDraftApprenticeshipsResponse()));
_mockCommitmentsV2Service
.Setup(c => c.GetDraftApprenticeships(_cohort))
.ReturnsAsync(_apprenticeships);

_commitmentsV2ServiceWithTimeout = new CommitmentsV2ServiceWithTimeout(_mockCommitmentsApiClient.Object, _mockMapper.Object, _mockEncodingService.Object, registryPolicy);
_commitmentsV2ServiceWithTimeout = new CommitmentsV2ServiceWithTimeout(_mockCommitmentsV2Service.Object, registryPolicy);
}

[Test]
public async Task ThenGetApprenticeshipsResponse()
{
//Arrange
var apprenticeships = new List<Apprenticeship> { new Apprenticeship { ApprenticeshipStatus = EmployerAccounts.Models.CommitmentsV2.ApprenticeshipStatus.Approved, FirstName ="FirstName" , LastName = "LastName" } };

_mockMapper
.Setup(m => m.Map
(It.IsAny<IEnumerable<GetApprenticeshipsResponse.ApprenticeshipDetailsResponse>>(),
It.IsAny<Action<IMappingOperationOptions<IEnumerable<GetApprenticeshipsResponse.ApprenticeshipDetailsResponse>, IEnumerable<Apprenticeship>>>>()))
.Returns(apprenticeships);

//Act
var result =await _commitmentsV2ServiceWithTimeout.GetApprenticeships(_accountId);
var result = await _commitmentsV2ServiceWithTimeout.GetApprenticeships(_accountId);

//Assert
Assert.IsNotNull(result);
Expand All @@ -73,18 +71,6 @@ public async Task ThenGetApprenticeshipsResponse()
[Test]
public async Task ThenGetCohortsResponse()
{
//Arrange
_mockEncodingService.Setup(x => x.Encode(It.IsAny<long>(), EncodingType.CohortReference)).Returns((long y, EncodingType z) => y + "_Encoded");

var cohorts = new List<Cohort>()
{
new Cohort { Id = 1 }
};

_mockMapper.Setup(m => m.Map<IEnumerable<CohortSummary>, IEnumerable<Cohort>>(It.IsAny<CohortSummary[]>(),
It.IsAny<Action<IMappingOperationOptions<IEnumerable<CohortSummary>, IEnumerable<Cohort>>>>()))
.Returns(cohorts);

//Act
var result = await _commitmentsV2ServiceWithTimeout.GetCohorts(_accountId);

Expand All @@ -96,29 +82,52 @@ public async Task ThenGetCohortsResponse()
[Test]
public async Task ThenGetDraftApprenticeshipsResponse()
{
//Arrange
var apprenticeships = new List<Apprenticeship> { new Apprenticeship { FirstName = "FirstName", LastName = "LastName" } };
_mockMapper.Setup(m => m.Map<IEnumerable<DraftApprenticeshipDto>, IEnumerable<Apprenticeship>>(It.IsAny<IReadOnlyCollection<DraftApprenticeshipDto>>(),
It.IsAny<Action<IMappingOperationOptions<IEnumerable<DraftApprenticeshipDto>, IEnumerable<Apprenticeship>>>>()))
.Returns(apprenticeships);


//Act
var result = await _commitmentsV2ServiceWithTimeout.GetDraftApprenticeships(new Cohort {Id = 123});
var result = await _commitmentsV2ServiceWithTimeout.GetDraftApprenticeships(_cohort);

//Assert
Assert.IsNotNull(result);
Assert.IsTrue(result.Count().Equals(1));
}

[Test]
public async Task ThenGetApprenticeshipsResponseReturnsTheSameApprenticeship()
{
//act
var apprenticeshipsResult = await _commitmentsV2ServiceWithTimeout.GetApprenticeships(_accountId);

// assert
Assert.AreSame(apprenticeshipsResult, _apprenticeships);
}

[Test]
public async Task ThenGetCohortsResponseReturnsTheSameCohorts()
{
//act
var cohortsResult = await _commitmentsV2ServiceWithTimeout.GetCohorts(_accountId);

// assert
Assert.AreSame(cohortsResult, _cohorts);
}

[Test]
public async Task ThenGetDraftApprenticeshipsResponseReturnsTheSameApprenticeship()
{
//act
var apprenticeshipsResult = await _commitmentsV2ServiceWithTimeout.GetDraftApprenticeships(_cohort);

// assert
Assert.AreSame(apprenticeshipsResult, _apprenticeships);
}

[Test]
public async Task ThenGetApprenticeshipsIsCalled()
{
//act
await _commitmentsV2ServiceWithTimeout.GetApprenticeships(_accountId);

// assert
_mockCommitmentsApiClient.Verify(rs => rs.GetApprenticeships(It.Is<GetApprenticeshipsRequest>(r => r.AccountId == _accountId), CancellationToken.None), Times.Once());
_mockCommitmentsV2Service.Verify(rs => rs.GetApprenticeships(_accountId), Times.Once());
}

[Test]
Expand All @@ -128,17 +137,17 @@ public async Task ThenTheGetCohortsIsCalled()
await _commitmentsV2ServiceWithTimeout.GetCohorts(_accountId);

// assert
_mockCommitmentsApiClient.Verify(rs => rs.GetCohorts(It.Is<GetCohortsRequest>(r => r.AccountId == _accountId), CancellationToken.None), Times.AtLeastOnce);
_mockCommitmentsV2Service.Verify(rs => rs.GetCohorts(_accountId), Times.Once);
}

[Test]
public async Task ThenTheGetDraftApprenticeshipsIsCalled()
{
//act
await _commitmentsV2ServiceWithTimeout.GetDraftApprenticeships(new Cohort { Id = 123 });
await _commitmentsV2ServiceWithTimeout.GetDraftApprenticeships(_cohort);

// assert
_mockCommitmentsApiClient.Verify(rs => rs.GetDraftApprenticeships(It.IsAny<long>(),CancellationToken.None), Times.AtLeastOnce);
_mockCommitmentsV2Service.Verify(rs => rs.GetDraftApprenticeships(_cohort), Times.Once);
}

[Test]
Expand All @@ -151,7 +160,8 @@ public async Task ThenThrowTimeoutException_GetApprenticeships()

try
{
_mockCommitmentsApiClient.Setup(p => p.GetApprenticeships(It.Is<GetApprenticeshipsRequest>(r => r.AccountId == _accountId), CancellationToken.None))
_mockCommitmentsV2Service
.Setup(c => c.GetApprenticeships(_accountId))
.Throws<TimeoutRejectedException>();
await _commitmentsV2ServiceWithTimeout.GetApprenticeships(_accountId);
}
Expand All @@ -175,7 +185,8 @@ public async Task ThenThrowTimeoutException_GetCohorts()

try
{
_mockCommitmentsApiClient.Setup(p => p.GetCohorts(It.Is<GetCohortsRequest>(r => r.AccountId == _accountId), CancellationToken.None))
_mockCommitmentsV2Service
.Setup(c => c.GetCohorts(_accountId))
.Throws<TimeoutRejectedException>();
await _commitmentsV2ServiceWithTimeout.GetCohorts(_accountId);
}
Expand All @@ -199,7 +210,8 @@ public async Task ThenThrowTimeoutException_GetDraftApprenticeships()

try
{
_mockCommitmentsApiClient.Setup(p => p.GetDraftApprenticeships(It.IsAny<long>(), CancellationToken.None))
_mockCommitmentsV2Service
.Setup(c => c.GetDraftApprenticeships(It.IsAny<Cohort>()))
.Throws<TimeoutRejectedException>();
await _commitmentsV2ServiceWithTimeout.GetDraftApprenticeships(new Cohort { Id = 123 });
}
Expand All @@ -212,71 +224,5 @@ public async Task ThenThrowTimeoutException_GetDraftApprenticeships()
Assert.AreEqual(actualException.InnerException?.Message, innerException);
Assert.AreEqual(actualException.Message, message);
}


private GetApprenticeshipsResponse CreateApprenticeshipResponse()
{
IEnumerable<GetApprenticeshipsResponse.ApprenticeshipDetailsResponse> apprenticeships = new List<GetApprenticeshipsResponse.ApprenticeshipDetailsResponse>()
{
new GetApprenticeshipsResponse.ApprenticeshipDetailsResponse
{
Id = 1,
FirstName = "FirstName",
LastName = "LastName",
Uln = "Uln",
EmployerName = "EmployerName",
CourseName = "CourseName",
StartDate = new DateTime(2020, 5, 1),
EndDate = new DateTime(2022, 5, 1),
ApprenticeshipStatus = CommitmentsV2.Types.ApprenticeshipStatus.Live,
ProviderName = "ProviderName"
}
};

return new GetApprenticeshipsResponse() { Apprenticeships = apprenticeships, TotalApprenticeships = 1, TotalApprenticeshipsFound = 1, TotalApprenticeshipsWithAlertsFound = 0 };
}


private GetCohortsResponse GetCohortsResponseForWithTrainingProviderStaus()
{
IEnumerable<CohortSummary> cohorts = new List<CohortSummary>()
{
new CohortSummary
{
CohortId = 4,
AccountId = 1,
ProviderId = 4,
ProviderName = "Provider4",
NumberOfDraftApprentices = 1,
IsDraft = false,
WithParty = Party.Provider,
CreatedOn = DateTime.Now
},
};

return new GetCohortsResponse(cohorts);
}

private GetDraftApprenticeshipsResponse GetDraftApprenticeshipsResponse()
{
IReadOnlyCollection<DraftApprenticeshipDto> draftApprenticeships = new List<DraftApprenticeshipDto>()
{
new DraftApprenticeshipDto
{
Id = 1,
FirstName = "FirstName",
LastName = "LastName",
DateOfBirth = new DateTime(2000, 1 ,1 ),
Cost = 100,
StartDate = new DateTime(2020, 5, 1),
EndDate = new DateTime(2022, 5, 1),
CourseCode = "CourseCode",
CourseName = "CourseName"
}
};

var draftApprenticeshipsResponse = new GetDraftApprenticeshipsResponse() { DraftApprenticeships = draftApprenticeships };
return draftApprenticeshipsResponse;
}
}
}
Loading

0 comments on commit 1c179eb

Please sign in to comment.