Skip to content

Commit

Permalink
CON-1654 - Updated Unit Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
VasanthaKasirajan3008 committed Apr 8, 2020
1 parent b7cf270 commit 69d1e90
Show file tree
Hide file tree
Showing 16 changed files with 282 additions and 270 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
using SFA.DAS.EmployerAccounts.Queries.GetOrganisationAgreements;
using Moq;
using NUnit.Framework;
using SFA.DAS.EmployerAccounts.Interfaces;
using SFA.DAS.HashingService;
using SFA.DAS.Validation;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using SFA.DAS.EmployerAccounts.Data;
using SFA.DAS.EmployerAccounts.MarkerInterfaces;
using AutoMapper;
using SFA.DAS.EmployerAccounts.Models.Account;
using SFA.DAS.EmployerAccounts.Dtos;
using SFA.DAS.Common.Domain.Types;

namespace SFA.DAS.EmployerAccounts.UnitTests.Queries.GetOrganisationAgreements
{
public class WhenIGetOrganisationAgreements : QueryBaseTest<GetOrganisationAgreementsQueryHandler, GetOrganisationAgreementsRequest, GetOrganisationAgreementsResponse>
{
public override GetOrganisationAgreementsRequest Query { get; set; }
public override GetOrganisationAgreementsQueryHandler RequestHandler { get; set; }
public override Mock<IValidator<GetOrganisationAgreementsRequest>> RequestValidator { get; set; }
private Mock<IEmployerAgreementRepository> _mockEmployerAgreementRepository;
private Mock<IAccountLegalEntityPublicHashingService> _mockAccountLegalEntityPublicHashingService;
private Mock<IHashingService> _mockhashingService;
private Mock<IReferenceDataService> _mockreferenceDataService;
private Mock<IMapper> _mockmapper;
private long _accountLegelEntityId = 123;
private string hashedId = "ABC123";

[SetUp]
public void Arrange()
{
SetUp();

_mockAccountLegalEntityPublicHashingService = new Mock<IAccountLegalEntityPublicHashingService>();
_mockAccountLegalEntityPublicHashingService.Setup(m => m.DecodeValue(It.IsAny<string>())).Returns(_accountLegelEntityId);

_mockEmployerAgreementRepository = new Mock<IEmployerAgreementRepository>();
_mockEmployerAgreementRepository.Setup(m => m.GetOrganisationAgreement(It.IsAny<long>())).ReturnsAsync
(new AccountLegalEntity {

LegalEntity = new LegalEntity { Id=1, Source= OrganisationType.CompaniesHouse } ,
Agreements = new List<EmployerAgreement>()
{ new EmployerAgreement
{
SignedDate = DateTime.UtcNow,
}

} });

_mockhashingService = new Mock<IHashingService>();
_mockhashingService.Setup(m => m.HashValue(It.IsAny<long>())).Returns(hashedId);

_mockreferenceDataService = new Mock<IReferenceDataService>();
_mockreferenceDataService.Setup(m => m.IsIdentifiableOrganisationType(It.IsAny<OrganisationType>())).ReturnsAsync(true);

var agreements = new List<EmployerAgreementDto> {
new EmployerAgreementDto { Id =1, SignedDate = DateTime.UtcNow , AccountLegalEntity = new AccountLegalEntity { Id = _accountLegelEntityId} },
new EmployerAgreementDto { Id =2, SignedDate = DateTime.UtcNow , AccountLegalEntity = new AccountLegalEntity { Id = _accountLegelEntityId} }};

_mockmapper = new Mock<IMapper>();
_mockmapper.Setup(m => m.Map<ICollection<EmployerAgreement>, ICollection<EmployerAgreementDto>>(It.IsAny<ICollection<EmployerAgreement>>(),
It.IsAny<Action<IMappingOperationOptions<ICollection<EmployerAgreement>, ICollection<EmployerAgreementDto>>>>()))
.Returns(agreements);

RequestHandler = new GetOrganisationAgreementsQueryHandler(RequestValidator.Object, _mockEmployerAgreementRepository.Object, _mockAccountLegalEntityPublicHashingService.Object,
_mockhashingService.Object, _mockreferenceDataService.Object, _mockmapper.Object);

Query = new GetOrganisationAgreementsRequest
{
AccountLegalEntityHashedId = hashedId
};
}


[Test]
public override async Task ThenIfTheMessageIsValidTheRepositoryIsCalled()
{
//Act
await RequestHandler.Handle(Query);

//Assert
_mockEmployerAgreementRepository.Verify(x => x.GetOrganisationAgreement(It.IsAny<long>()), Times.Once);
}

[Test]
public override async Task ThenIfTheMessageIsValidTheValueIsReturnedInTheResponse()
{
//Act
var response = await RequestHandler.Handle(Query);

//Assert
Assert.IsNotNull(response.Agreements);
Assert.IsTrue(response.Agreements.Any());
}


}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using NUnit.Framework;
using SFA.DAS.EmployerAccounts.Queries.GetOrganisationAgreements;
using System.Threading.Tasks;

namespace SFA.DAS.EmployerAccounts.UnitTests.Queries.GetOrganisationAgreements
{
public class WhenIValidateTheRequest
{
private GetOrganisationAgreementsValidator _validator;

[SetUp]
public void Arrange()
{
_validator = new GetOrganisationAgreementsValidator();
}

[Test]
public async Task ThenShouldReturnValidIfRequestIsValid()
{
//Act
var result = await _validator.ValidateAsync(new GetOrganisationAgreementsRequest { AccountLegalEntityHashedId = "Abc123" });

//Assert
Assert.IsTrue(result.IsValid());
}

[Test]
public async Task ThenShouldReturnInValidIfRequestIsNotValid()
{
//Act
var result = await _validator.ValidateAsync(new GetOrganisationAgreementsRequest { AccountLegalEntityHashedId = string.Empty });

//Assert
Assert.IsFalse(result.IsValid());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@
using MediatR;
using Moq;
using NUnit.Framework;
using SFA.DAS.EmployerAccounts.Dtos;
using SFA.DAS.EmployerAccounts.Interfaces;
using SFA.DAS.EmployerAccounts.Models.EmployerAgreement;
using SFA.DAS.EmployerAccounts.Queries.GetOrganisationAgreements;
using SFA.DAS.EmployerAccounts.Web.Orchestrators;
using SFA.DAS.EmployerAccounts.Web.ViewModels;
using SFA.DAS.Validation;
using System.Linq;

namespace SFA.DAS.EmployerAccounts.Web.UnitTests.Orchestrators.EmployerAgreementOrchestratorTests
{
Expand All @@ -20,7 +21,7 @@ public class WhenIGetOrganisationAgreements
private Mock<IMediator> _mediator;
private Mock<IReferenceDataService> _referenceDataService;
private Mock<IMapper> _mapper;
private EmployerAgreementOrchestrator _orchestrator;
private EmployerAgreementOrchestrator _orchestrator;

public string AccountLegalEntityHashedId = "2K7J94";

Expand All @@ -32,21 +33,19 @@ public void Arrange()
_mediator.Setup(x => x.SendAsync(It.IsAny<GetOrganisationAgreementsRequest>()))
.ReturnsAsync(new GetOrganisationAgreementsResponse
{
OrganisationAgreements = new OrganisationAgreement
{
Address = "Address",
Name = "Name"
}
});;

var organisationAgreementViewModel = new OrganisationAgreementViewModel
Agreements = new List<EmployerAgreementDto>()
{
new EmployerAgreementDto { SignedDate = DateTime.UtcNow }
}
});

var EmployerAgreementViewModelV1 = new List<EmployerAgreementViewModelV1>()
{
Address = "Address",
Name = "Name"
new EmployerAgreementViewModelV1 { SignedDate = DateTime.UtcNow }
};

_referenceDataService = new Mock<IReferenceDataService>();
_mapper.Setup(m => m.Map<OrganisationAgreement, OrganisationAgreementViewModel>(It.IsAny<OrganisationAgreement>())).Returns(organisationAgreementViewModel);
_mapper.Setup(m => m.Map<ICollection<EmployerAgreementDto>, ICollection<EmployerAgreementViewModelV1>>(It.IsAny<ICollection<EmployerAgreementDto>>())).Returns(EmployerAgreementViewModelV1);
_orchestrator = new EmployerAgreementOrchestrator(_mediator.Object, _mapper.Object, _referenceDataService.Object);
}

Expand Down Expand Up @@ -95,8 +94,7 @@ public async Task ThenTheValuesAreReturnedInTheResponseFromTheMediatorCall()
var actual = await _orchestrator.GetOrganisationAgreements(AccountLegalEntityHashedId);

//Assert
//TO DO
Assert.IsTrue(actual.Data.Address.Equals("Address"));
Assert.IsNotNull(actual.Data.Any());
}
}
}
17 changes: 17 additions & 0 deletions src/SFA.DAS.EmployerAccounts.Web/Helpers/HtmlHelperExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,24 @@ public static string GetClaimsHashedAccountId(this HtmlHelper htmlHelper)
return (!string.IsNullOrEmpty(hashedAccountId)) ? hashedAccountId : string.Empty;
}

public static string GetAgreementHistoryTabTitle(this HtmlHelper htmlHelper , int versionNumber)
{
if (versionNumber == 1) return "Published 9 January 2020";
if (versionNumber == 2) return "Published 1 May 2018";
if (versionNumber == 3) return "Published 1 May 2017";

return string.Empty;
}

public static string GetAgreementTabListId(this HtmlHelper htmlHelper, int versionNumber)
{
return $"#v{versionNumber}-agreement";
}

public static string GetAgreementTabPanelId(this HtmlHelper htmlHelper, int versionNumber)
{
return $"v{versionNumber}-agreement";
}

public static bool ViewExists(this HtmlHelper html, string viewName)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using AutoMapper;
using SFA.DAS.EAS.Account.Api.Types;
using SFA.DAS.EmployerAccounts.Dtos;
using SFA.DAS.EmployerAccounts.Models.EmployerAgreement;
using SFA.DAS.EmployerAccounts.Queries.GetEmployerAgreement;
using SFA.DAS.EmployerAccounts.Web.ViewModels;

Expand Down Expand Up @@ -37,14 +36,6 @@ public AgreementMappings()
CreateMap<AccountDetailViewModel, AgreementInfoViewModel>()
.ConvertUsing(new AgreementInfoConverter());

CreateMap<OrganisationAgreement, OrganisationAgreementViewModel>()
.ForMember(dest => dest.AgreementV1SignedDate, opts => opts.Ignore())
.ForMember(dest => dest.AgreementV2SignedDate, opts => opts.Ignore())
.ForMember(dest => dest.AgreementV3SignedDate, opts => opts.Ignore())
.ForMember(dest => dest.AgreementV1Signed, opts => opts.Ignore())
.ForMember(dest => dest.AgreementV2Signed, opts => opts.Ignore())
.ForMember(dest => dest.AgreementV3Signed, opts => opts.Ignore());

CreateMap<EmployerAgreementDto, EmployerAgreementViewModelV1>()
.ForMember(dest => dest.SignedDateText, opt => opt.MapFrom(src => src.SignedDate.HasValue ? src.SignedDate.Value.ToString("dd MMMM yyyy") : ""))
.ForMember(dest => dest.AccountLegalEntityPublicHashedId, opts => opts.Ignore());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -369,31 +369,31 @@ public virtual async Task<OrchestratorResponse<ConfirmLegalAgreementToRemoveView
return response;
}

public virtual async Task<OrchestratorResponse<OrganisationAgreementViewModel>> GetOrganisationAgreements(string accountLegalEntityHashedId)
public virtual async Task<OrchestratorResponse<ICollection<EmployerAgreementViewModelV1>>> GetOrganisationAgreements(string accountLegalEntityHashedId)
{
var response = new OrchestratorResponse<OrganisationAgreementViewModel>();
var response = new OrchestratorResponse<ICollection<EmployerAgreementViewModelV1>>();

try
{
var result = await _mediator.SendAsync(new GetOrganisationAgreementsRequest
{
AccountLegalEntityHashedId = accountLegalEntityHashedId
});

response.Data = _mapper.Map<OrganisationAgreement, OrganisationAgreementViewModel>(result.OrganisationAgreements);
response.Data = _mapper.Map<ICollection<EmployerAgreementDto>, ICollection<EmployerAgreementViewModelV1>>(result.Agreements);
}
catch (InvalidRequestException ex)
{
return new OrchestratorResponse<OrganisationAgreementViewModel>
return new OrchestratorResponse<ICollection<EmployerAgreementViewModelV1>>
{
Status = HttpStatusCode.BadRequest,
Data = new OrganisationAgreementViewModel(),
Data = new List<EmployerAgreementViewModelV1>(),
Exception = ex
};
}
catch (UnauthorizedAccessException)
{
return new OrchestratorResponse<OrganisationAgreementViewModel>
return new OrchestratorResponse<ICollection<EmployerAgreementViewModelV1>>
{
Status = HttpStatusCode.Unauthorized
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,6 @@
<Compile Include="ViewModels\CohortViewModel.cs" />
<Compile Include="ViewModels\CreateUserAccountViewModel.cs" />
<Compile Include="ViewModels\EmployerAgreementViewModelV1.cs" />
<Compile Include="ViewModels\OrganisationAgreementViewModel.cs" />
<Compile Include="ViewModels\SignEmployerAgreementViewModel.cs" />
<Compile Include="ViewModels\PensionRegulatorDetailsViewModel.cs" />
<Compile Include="ViewModels\PanelViewModel.cs" />
Expand Down

This file was deleted.

Loading

0 comments on commit 69d1e90

Please sign in to comment.