Skip to content

Commit

Permalink
Merge pull request #310 from SkillsFundingAgency/PA-282-EAS-CoC
Browse files Browse the repository at this point in the history
PA-282 EAS CoC
  • Loading branch information
SreekanthBadigenchula authored May 29, 2022
2 parents 5e06a02 + 6c7ad48 commit a034089
Show file tree
Hide file tree
Showing 114 changed files with 4,407 additions and 248 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
using System.Net;
using WireMock.RequestBuilders;
using WireMock.ResponseBuilders;
using WireMock.Server;

namespace SFA.DAS.EmployerCommitmentsV2.Api.FakeServers
{
public class CommitmentsOuterApiBuilder
{
private readonly WireMockServer _server;

public CommitmentsOuterApiBuilder(int port)
{
_server = WireMockServer.StartWithAdminInterface(port, true);
}

public static CommitmentsOuterApiBuilder Create(int port)
{
return new CommitmentsOuterApiBuilder(port);
}

public MockApi Build()
{
return new MockApi(_server);
}

internal CommitmentsOuterApiBuilder WithCourseDeliveryModels()
{
_server
.Given(Request.Create()
.WithPath("/Providers/*/courses/*")
.UsingGet())
.RespondWith(Response.Create()
.WithStatusCode(HttpStatusCode.OK)
.WithHeader("Content-Type", "application/json")
.WithBodyAsJson(new
{
DeliveryModels = new[] { "Regular", "PortableFlexiJob" },
}));

_server
.Given(Request.Create()
.WithPath("/Providers/*/courses/650")
.UsingGet())
.RespondWith(Response.Create()
.WithStatusCode(HttpStatusCode.OK)
.WithHeader("Content-Type", "application/json")
.WithBodyAsJson(new
{
DeliveryModels = new[] { "Regular" },
}));

return this;
}
}
}
38 changes: 38 additions & 0 deletions src/SFA.DAS.EmployerCommitmentsV2.Api.FakeServers/MockApi.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using System;
using WireMock.Server;

namespace SFA.DAS.EmployerCommitmentsV2.Api.FakeServers
{
public class MockApi : IDisposable
{
private readonly WireMockServer _server;

private bool _isDisposed;

public MockApi(WireMockServer server)
{
_server = server;
}

public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}

protected virtual void Dispose(bool disposing)
{
if (_isDisposed) return;

if (disposing)
{
if (_server != null && _server.IsStarted)
_server.Stop();

_server?.Dispose();
}

_isDisposed = true;
}
}
}
19 changes: 19 additions & 0 deletions src/SFA.DAS.EmployerCommitmentsV2.Api.FakeServers/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System;

namespace SFA.DAS.EmployerCommitmentsV2.Api.FakeServers
{
public static class Program
{
public static void Main(string[] args)
{
CommitmentsOuterApiBuilder.Create(44328)
.WithCourseDeliveryModels()
.Build();

Console.WriteLine("Approvals Outer API running on port 44328");
Console.WriteLine("Course Games Developer (650) has a Single DeliveryModel, all other course have multiple");
Console.WriteLine("Press any key to stop the APIs server");
Console.ReadKey();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
<PackageReference Include="WireMock.Net" Version="1.4.20" />
<PackageReference Include="WireMock.Net.StandAlone" Version="1.4.20" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public void SetEditApprenticeViewModel(string version)
};

object serializedModel = JsonConvert.SerializeObject(editApprenticeViewModel);
_tempDataDictionary.Setup(s => s.TryGetValue("EditApprenticeshipRequestViewModel", out serializedModel)).Returns(true);
_tempDataDictionary.Setup(s => s.Peek("EditApprenticeshipRequestViewModel")).Returns(serializedModel);
}

public async Task<IActionResult> ChangeVersion()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
using NUnit.Framework;
using SFA.DAS.EmployerCommitmentsV2.Web.Models.Apprentice;
using System.Threading.Tasks;
using FluentAssertions;
using Newtonsoft.Json;
using SFA.DAS.CommitmentsV2.Types;

namespace SFA.DAS.EmployerCommitmentsV2.Web.UnitTests.Controllers.ApprenticeControllerTests
{
Expand All @@ -17,24 +20,45 @@ public void Arrange()
_fixture = new WhenCallingEditApprenticeshipTestsFixture();
}

[Test]
public async Task ThenTheCorrectViewIsReturned()
{
var result = await _fixture.EditApprenticeship();

_fixture.VerifyViewModel(result as ViewResult);
}

[Test]
public async Task AndWeHaveAnExistingEditViewModel_ThenTheTempModelIsPassedToTheView()
{
var result = await _fixture.WithTempModel().EditApprenticeship();
_fixture.VerifyViewModelIsEquivalentToTempViewModel(result as ViewResult);
}
}

public class WhenCallingEditApprenticeshipTestsFixture : ApprenticeControllerTestFixtureBase
{
private readonly EditApprenticeshipRequest _request;
private readonly EditApprenticeshipRequestViewModel _viewModel;
private readonly EditApprenticeshipRequestViewModel _tempViewModel;
private object _tempViewModelAsString;


public WhenCallingEditApprenticeshipTestsFixture() : base()
{
_request = _autoFixture.Create<EditApprenticeshipRequest>();
_viewModel = _autoFixture.Create<EditApprenticeshipRequestViewModel>();

_viewModel = _autoFixture.Build<EditApprenticeshipRequestViewModel>()
.Without(x => x.StartDate).Without(x => x.StartMonth).Without(x => x.StartYear)
.Without(x => x.EndDate).Without(x => x.EndMonth).Without(x => x.EndYear)
.Without(x => x.EmploymentEndDate).Without(x => x.EmploymentEndMonth).Without(x => x.EmploymentEndYear)
.Create();

_tempViewModel = _autoFixture.Build<EditApprenticeshipRequestViewModel>()
.Without(x => x.StartDate).Without(x => x.StartMonth).Without(x => x.StartYear)
.Without(x => x.EndDate).Without(x => x.EndMonth).Without(x => x.EndYear)
.Without(x => x.EmploymentEndDate).Without(x => x.EmploymentEndMonth).Without(x => x.EmploymentEndYear)
.Create(); ;
_tempViewModelAsString = JsonConvert.SerializeObject(_tempViewModel);

_mockMapper.Setup(m => m.Map<EditApprenticeshipRequestViewModel>(_request))
.ReturnsAsync(_viewModel);
Expand All @@ -45,12 +69,27 @@ public async Task<IActionResult> EditApprenticeship()
return await _controller.EditApprenticeship(_request);
}

public WhenCallingEditApprenticeshipTestsFixture WithTempModel()
{
_tempDataDictionary.Setup(x => x.TryGetValue("ViewModelForEdit", out _tempViewModelAsString));
_viewModel.DeliveryModel = DeliveryModel.PortableFlexiJob;
return this;
}

public void VerifyViewModel(ViewResult viewResult)
{
var viewModel = viewResult.Model as ApprenticeshipDetailsRequestViewModel;
var viewModel = viewResult.Model as EditApprenticeshipRequestViewModel;

Assert.IsInstanceOf<ApprenticeshipDetailsRequestViewModel>(viewModel);
Assert.IsInstanceOf<EditApprenticeshipRequestViewModel>(viewModel);
Assert.AreEqual(_viewModel, viewModel);
}

public void VerifyViewModelIsEquivalentToTempViewModel(ViewResult viewResult)
{
var viewModel = viewResult.Model as EditApprenticeshipRequestViewModel;

Assert.IsInstanceOf<EditApprenticeshipRequestViewModel>(viewModel);
_tempViewModel.Should().BeEquivalentTo(viewModel);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,13 @@ public void Arrange()
var baseDate = DateTime.Now;
var startDate = new MonthYearModel(baseDate.ToString("MMyyyy"));
var endDate = new MonthYearModel(baseDate.AddYears(2).ToString("MMyyyy"));
var employmentEndDate = new MonthYearModel(baseDate.AddYears(1).ToString("MMyyyy"));
var dateOfBirth = new MonthYearModel(baseDate.AddYears(-18).ToString("MMyyyy"));

_editRequestViewModel = autoFixture.Build<EditApprenticeshipRequestViewModel>()
.With(x => x.StartDate, startDate)
.With(x => x.EndDate, endDate)
.With(x => x.EmploymentEndDate, employmentEndDate)
.With(x => x.DateOfBirth, dateOfBirth)
.Create();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ public void Arrange()
_viewModel = _autoFixture.Build<EditApprenticeshipRequestViewModel>()
.Without(x => x.StartDate)
.Without(x => x.EndDate)
.Without(x => x.EmploymentEndDate)
.Without(x => x.DateOfBirth)
.With(x => x.CourseCode, _apprenticeshipResponse.CourseCode)
.Create();
Expand Down Expand Up @@ -151,6 +152,21 @@ public async Task VerifyMapperIsCalled()
await _fixture.EditApprenticeship(_viewModel);
_fixture.VerifyMapperIsCalled();
}

[Test]
public async Task AndSelectCourseIsToBeChangedThenTheUserIsRedirectedToSelectCoursePage()
{
var result = await _fixture.EditChangingCourse(_viewModel);
_fixture.VerifyRedirectedTo(result, "SelectCourseForEdit");
}

[Test]
public async Task AndSelectDeliveryModelIsToBeChangedThenTheUserIsRedirectedToSelectDeliveryModelPage()
{
var result = await _fixture.EditChangingDeliveryModel(_viewModel);
_fixture.VerifyRedirectedTo(result, "SelectDeliveryModelForEdit");
}

}

public class WhenPostingEditApprenticeshipDetailsFixture : ApprenticeControllerTestFixtureBase
Expand All @@ -162,8 +178,18 @@ public WhenPostingEditApprenticeshipDetailsFixture() : base ()

public async Task<IActionResult> EditApprenticeship(EditApprenticeshipRequestViewModel viewModel)
{
return await _controller.EditApprenticeship(viewModel);
}
return await _controller.EditApprenticeship(null, null, viewModel);
}

public async Task<IActionResult> EditChangingCourse(EditApprenticeshipRequestViewModel viewModel)
{
return await _controller.EditApprenticeship("Edit", null, viewModel);
}

public async Task<IActionResult> EditChangingDeliveryModel(EditApprenticeshipRequestViewModel viewModel)
{
return await _controller.EditApprenticeship(null, "Edit", viewModel);
}

public void SetUpGetApprenticeship(GetApprenticeshipResponse response)
{
Expand Down Expand Up @@ -217,5 +243,10 @@ public void VerifyRedirectToChangeOption(RedirectToActionResult result)
{
result.ActionName.Should().Be("ChangeOption");
}

public void VerifyRedirectedTo(IActionResult actionResult, string actionName)
{
actionResult.VerifyReturnsRedirectToActionResult().WithActionName(actionName);
}
}
}
Loading

0 comments on commit a034089

Please sign in to comment.