Skip to content

Commit

Permalink
Merge pull request #159 from SkillsFundingAgency/MF-281_SelectCourseC…
Browse files Browse the repository at this point in the history
…ontent

Mf 281 select course content
  • Loading branch information
SijiOdun authored Oct 3, 2019
2 parents 62b1631 + 3092756 commit 7ae85c4
Show file tree
Hide file tree
Showing 18 changed files with 295 additions and 311 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -168,14 +168,14 @@ public async Task Then_Gets_Selected_Course(CacheReservationCourseCommand comman
await _commandHandler.Handle(command, CancellationToken.None);

//Assert
_mockCourseService.Verify(cs => cs.GetCourse(command.CourseId), Times.Once);
_mockCourseService.Verify(cs => cs.GetCourse(command.SelectedCourseId), Times.Once);
}

[Test, AutoData]
public async Task Then_Calls_Cache_Service_To_Save_Reservation(CacheReservationCourseCommand command)
{
//Assign
command.CourseId = _expectedCourse.Id;
command.SelectedCourseId = _expectedCourse.Id;

//Act
await _commandHandler.Handle(command, CancellationToken.None);
Expand All @@ -192,7 +192,7 @@ public async Task Then_Calls_Cache_Service_To_Save_Reservation(CacheReservationC
public async Task Then_Caches_Course_Choice_If_Not_Selected(CacheReservationCourseCommand command)
{
//Assign
command.CourseId = null;
command.SelectedCourseId = null;

//Act
await _commandHandler.Handle(command, CancellationToken.None);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public async Task Then_If_ReservationId_Is_Invalid_Then_Fail()
var command = new CacheReservationCourseCommand
{
Id = Guid.Empty,
CourseId = "1"
SelectedCourseId = "1"
};

var result = await _validator.ValidateAsync(command);
Expand All @@ -49,16 +49,16 @@ public async Task Then_If_CourseId_Not_Set_Then_Fail()
var command = new CacheReservationCourseCommand
{
Id = Guid.NewGuid(),
CourseId = ""
SelectedCourseId = ""
};

var result = await _validator.ValidateAsync(command);

result.IsValid().Should().BeFalse();
result.ValidationDictionary.Count.Should().Be(1);
result.ValidationDictionary
.Should().ContainKey(nameof( CacheReservationCourseCommand.CourseId))
.WhichValue.Should().Be("Select a course");
.Should().ContainKey(nameof(CacheReservationCourseCommand.SelectedCourseId))
.WhichValue.Should().Be("Select which apprenticeship training your apprentice will take");
}


Expand All @@ -70,32 +70,32 @@ public async Task Then_If_CourseId_Is_Invalid_Then_Fail()
var command = new CacheReservationCourseCommand
{
Id = Guid.NewGuid(),
CourseId = "123"
SelectedCourseId = "123"
};

var result = await _validator.ValidateAsync(command);

result.IsValid().Should().BeFalse();
result.ValidationDictionary.Count.Should().Be(1);
result.ValidationDictionary
.Should().ContainKey(nameof( CacheReservationCourseCommand.CourseId))
.WhichValue.Should().Be("Course is invalid");
.Should().ContainKey(nameof( CacheReservationCourseCommand.SelectedCourseId))
.WhichValue.Should().Be("Selected course does not exist");
}

[Test]
public async Task And_All_Fields_Invalid_Then_Returns_All_Errors()
{
_courseService.Setup(s => s.CourseExists(It.IsAny<string>())).ReturnsAsync(false);

var command = new CacheReservationCourseCommand{ CourseId = "INVALID" };
var command = new CacheReservationCourseCommand{ SelectedCourseId = "INVALID" };

var result = await _validator.ValidateAsync(command);

result.IsValid().Should().BeFalse();
result.ValidationDictionary.Count.Should().Be(2);
result.ValidationDictionary
.Should().ContainKey(nameof( CacheReservationCourseCommand.Id))
.And.ContainKey(nameof( CacheReservationCourseCommand.CourseId));
.And.ContainKey(nameof( CacheReservationCourseCommand.SelectedCourseId));
}

[Test]
Expand All @@ -104,7 +104,7 @@ public async Task And_All_Fields_Valid_Then_Valid()
var command = new CacheReservationCourseCommand
{
Id = Guid.NewGuid(),
CourseId = "1"
SelectedCourseId = "1"
};

var result = await _validator.ValidateAsync(command);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public class CacheReservationCourseCommand: IRequest<Unit>
{
public Guid Id { get; set; }

public string CourseId { get; set; }
public string SelectedCourseId { get; set; }
public uint? UkPrn { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,15 @@ public async Task<Unit> Handle(CacheReservationCourseCommand command, Cancellati
throw new CachedReservationNotFoundException(command.Id);
}

if (string.IsNullOrEmpty(command.CourseId))
if (string.IsNullOrEmpty(command.SelectedCourseId))
{
var course = new Course(null,null,0);
cachedReservation.CourseId = course.Id;
cachedReservation.CourseDescription = course.CourseDescription;
}
else
{
var course = await _courseService.GetCourse(command.CourseId);
var course = await _courseService.GetCourse(command.SelectedCourseId);

cachedReservation.CourseId = course.Id;
cachedReservation.CourseDescription = course.CourseDescription;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ public async Task<ValidationResult> ValidateAsync(CacheReservationCourseCommand
result.AddError(nameof(command.Id), $"{nameof( CacheReservationCourseCommand.Id)} has not been supplied");
}

if(string.IsNullOrEmpty(command.CourseId))
if(string.IsNullOrEmpty(command.SelectedCourseId))
{
result.AddError(nameof(command.CourseId), "Select a course");
result.AddError(nameof(command.SelectedCourseId), "Select which apprenticeship training your apprentice will take");
}
else if(!await _courseService.CourseExists(command.CourseId))
else if(!await _courseService.CourseExists(command.SelectedCourseId))
{
result.AddError(nameof(command.CourseId), "Course is invalid");
result.AddError(nameof(command.SelectedCourseId), "Selected course does not exist");
}

return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,13 @@ public void GivenIAmANonLevyEmployer()
public void GivenIHaveChosenACourse()
{
var controller = Services.GetService<EmployerReservationsController>();
var postSelectCourseViewModel = new PostSelectCourseViewModel
{
SelectedCourseId = TestData.Course.Id,
ApprenticeTrainingKnown = true
};

var result = controller.PostSelectCourse(TestData.ReservationRouteModel, TestData.Course.Id)
var result = controller.PostSelectCourse(TestData.ReservationRouteModel, postSelectCourseViewModel)
.Result as RedirectToRouteResult;

Assert.IsNotNull(result);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,43 +64,51 @@ public void Arrange()
}

[Test, MoqAutoData]
public async Task Then_Caches_Draft_Reservation(ReservationsRouteModel routeModel)
public async Task Then_Caches_Draft_Reservation(
ReservationsRouteModel routeModel,
PostSelectCourseViewModel postSelectCourseViewModel)
{
//Assign
var selectedCourse = _course.Id;
postSelectCourseViewModel.SelectedCourseId = _course.Id;
postSelectCourseViewModel.ApprenticeTrainingKnown = true;

//Act
await _controller.PostSelectCourse(routeModel, selectedCourse);
await _controller.PostSelectCourse(routeModel, postSelectCourseViewModel);

//Assert
_mediator.Verify(mediator =>
mediator.Send(It.Is<CacheReservationCourseCommand>(command =>
command.CourseId.Equals(_course.Id)), It.IsAny<CancellationToken>()));
command.SelectedCourseId.Equals(_course.Id)), It.IsAny<CancellationToken>()));
}

[Test, MoqAutoData]
public async Task And_No_Course_Then_Caches_Draft_Reservation(ReservationsRouteModel routeModel)
public async Task And_No_Course_Then_Caches_Draft_Reservation(
ReservationsRouteModel routeModel,
PostSelectCourseViewModel postSelectCourseViewModel)
{
_mediator.Setup(mediator => mediator.Send(
It.IsAny<GetCachedReservationQuery>(),
It.IsAny<CancellationToken>()))
.ReturnsAsync(() => _cachedReservationResult);

await _controller.PostSelectCourse(routeModel, null);
postSelectCourseViewModel.SelectedCourseId = null;
postSelectCourseViewModel.ApprenticeTrainingKnown = true;
await _controller.PostSelectCourse(routeModel, postSelectCourseViewModel);

_mediator.Verify(mediator =>
mediator.Send(It.IsAny<CacheReservationCourseCommand>(), It.IsAny<CancellationToken>()),
Times.Once);
}

[Test, MoqAutoData]
public async Task Then_Adds_Guid_To_RouteModel(ReservationsRouteModel routeModel)
public async Task Then_Adds_Guid_To_RouteModel(
ReservationsRouteModel routeModel,
PostSelectCourseViewModel postSelectCourseViewModel)
{
//Assign
var selectedCourse = _course.Id;
postSelectCourseViewModel.SelectedCourseId = _course.Id;

//Act
var result = await _controller.PostSelectCourse(routeModel, selectedCourse) as RedirectToRouteResult;
var result = await _controller.PostSelectCourse(routeModel, postSelectCourseViewModel) as RedirectToRouteResult;

//Assert
Assert.IsNotNull(result);
Expand All @@ -110,16 +118,19 @@ public async Task Then_Adds_Guid_To_RouteModel(ReservationsRouteModel routeModel
}

[Test, AutoData]//note cannot use moqautodata to construct controller here due to modelmetadata usage.
public async Task And_Validation_Error_Then_Returns_Validation_Error_Details(ReservationsRouteModel routeModel)
public async Task And_Validation_Error_Then_Returns_Validation_Error_Details(
ReservationsRouteModel routeModel,
PostSelectCourseViewModel postSelectCourseViewModel)
{
//Assign
var selectedCourse = _course.Id;
postSelectCourseViewModel.SelectedCourseId = _course.Id;
postSelectCourseViewModel.ApprenticeTrainingKnown = true;

_mediator.Setup(mediator => mediator.Send(It.IsAny<CacheReservationCourseCommand>(), It.IsAny<CancellationToken>()))
.ThrowsAsync(new ValidationException(new ValidationResult("Failed", new List<string> { "Course|The Course field is not valid." }), null, null));

//Act
var result = await _controller.PostSelectCourse(routeModel, selectedCourse);
var result = await _controller.PostSelectCourse(routeModel, postSelectCourseViewModel);

//Assert
Assert.IsNotNull(result);
Expand All @@ -132,44 +143,48 @@ public async Task And_Validation_Error_Then_Returns_Validation_Error_Details(Res
[Test, MoqAutoData]
public async Task Then_The_BackLink_Is_Set_To_Return_To_CohortDetails_From_ValidationError_If_There_Is_A_Cohort_Ref(
ReservationsRouteModel routeModel,
string cohortUrl
string cohortUrl,
PostSelectCourseViewModel postSelectCourseViewModel
)
{
//Arrange
routeModel.CohortReference = "ABC123";
_cachedReservationResult.CohortRef = "ABC123";
_mediator.Setup(mediator => mediator.Send(It.IsAny<CacheReservationCourseCommand>(), It.IsAny<CancellationToken>()))
.ThrowsAsync(new ValidationException(new ValidationResult("Failed", new List<string> { "Course|The Course field is not valid." }), null, null));
var selectedCourse = _course.Id;
postSelectCourseViewModel.SelectedCourseId = string.Empty;
postSelectCourseViewModel.ApprenticeTrainingKnown = true;
_externalUrlHelper
.Setup(x => x.GenerateCohortDetailsUrl(null, routeModel.EmployerAccountId,routeModel.CohortReference))
.Setup(x => x.GenerateCohortDetailsUrl(null, routeModel.EmployerAccountId, _cachedReservationResult.CohortRef))
.Returns(cohortUrl);

//Act
var result = await _controller.PostSelectCourse(routeModel, selectedCourse) as ViewResult;
var result = await _controller.PostSelectCourse(routeModel, postSelectCourseViewModel) as ViewResult;

var viewModel = result?.Model as EmployerSelectCourseViewModel;
Assert.IsNotNull(viewModel);
Assert.AreEqual(cohortUrl, viewModel.BackLink);
Assert.AreEqual(routeModel.CohortReference, viewModel.CohortReference);
Assert.AreEqual(_cachedReservationResult.CohortRef, viewModel.CohortReference);
}

[Test, MoqAutoData]
public async Task Then_The_BackLink_Is_Set_To_Return_To_ReviewPage_If_There_Is_FromReview_Flag(
ICollection<Course> courses,
[Frozen] Mock<IMediator> mockMediator,
ReservationsRouteModel routeModel,
EmployerReservationsController controller)
EmployerReservationsController controller,
PostSelectCourseViewModel postSelectCourseViewModel)
{
//Arrange
_cachedReservationResult.CohortRef = "";
routeModel.CohortReference = "";
routeModel.FromReview = true;
_mediator.Setup(mediator => mediator.Send(It.IsAny<CacheReservationCourseCommand>(), It.IsAny<CancellationToken>()))
.ThrowsAsync(new ValidationException(new ValidationResult("Failed", new List<string> { "Course|The Course field is not valid." }), null, null));
var selectedCourse = _course.Id;
postSelectCourseViewModel.SelectedCourseId = _course.Id;


//Act
var result = await _controller.PostSelectCourse(routeModel, selectedCourse) as ViewResult;
var result = await _controller.PostSelectCourse(routeModel, postSelectCourseViewModel) as ViewResult;

var viewModel = result?.Model as EmployerSelectCourseViewModel;
Assert.IsNotNull(viewModel);
Expand All @@ -183,23 +198,67 @@ public async Task Then_The_BackLink_Is_Set_To_Return_To_SelectLegalEntityView(
ICollection<Course> courses,
[Frozen] Mock<IMediator> mockMediator,
ReservationsRouteModel routeModel,
EmployerReservationsController controller)
EmployerReservationsController controller,
PostSelectCourseViewModel postSelectCourseViewModel)
{
//Arrange
_cachedReservationResult.CohortRef = "";
_cachedReservationResult.EmployerHasSingleLegalEntity = false;
routeModel.CohortReference = "";
routeModel.FromReview = false;
_mediator.Setup(mediator => mediator.Send(It.IsAny<CacheReservationCourseCommand>(), It.IsAny<CancellationToken>()))
.ThrowsAsync(new ValidationException(new ValidationResult("Failed", new List<string> { "Course|The Course field is not valid." }), null, null));
var selectedCourse = _course.Id;
postSelectCourseViewModel.SelectedCourseId= _course.Id;


//Act
var result = await _controller.PostSelectCourse(routeModel, selectedCourse) as ViewResult;
var result = await _controller.PostSelectCourse(routeModel, postSelectCourseViewModel) as ViewResult;

var viewModel = result?.Model as EmployerSelectCourseViewModel;
Assert.IsNotNull(viewModel);
Assert.AreEqual(RouteNames.EmployerSelectLegalEntity, viewModel.BackLink);
Assert.AreEqual(routeModel.CohortReference, viewModel.CohortReference);
}

[Test, MoqAutoData]
public async Task WhenApprenticeshipTrainingNotKnown_ThenRedirectsToGuidancePage(
ReservationsRouteModel routeModel,
EmployerReservationsController controller,
PostSelectCourseViewModel postSelectCourseViewModel)
{
//Arrange
postSelectCourseViewModel.ApprenticeTrainingKnown = false;
var expectedRouteName = RouteNames.EmployerCourseGuidance;

//Act
var result = await controller.PostSelectCourse(routeModel, postSelectCourseViewModel) as RedirectToRouteResult;

//Assert
Assert.NotNull(result);
Assert.AreEqual(expectedRouteName, result.RouteName);
}

[Test, MoqAutoData]
public async Task WhenApprenticeshipTrainingIsNull_ThenRedirectsToSelectCourse(
ReservationsRouteModel routeModel,
PostSelectCourseViewModel postSelectCourseViewModel,
[Frozen] Mock<IMediator> mockMediator,
EmployerReservationsController controller)
{
//Arrange
postSelectCourseViewModel.ApprenticeTrainingKnown = null;
var expectedViewName = "SelectCourse";
_mediator.Setup(mediator => mediator.Send(It.IsAny<CacheReservationCourseCommand>(), It.IsAny<CancellationToken>()))
.ThrowsAsync(new ValidationException(new ValidationResult("Failed", new List<string> { "Course|The Course field is not valid." }), null, null));

//Act
var result = await _controller.PostSelectCourse(routeModel, postSelectCourseViewModel) as ViewResult;

//Assert
Assert.NotNull(result);
Assert.AreEqual(expectedViewName, result.ViewName);


}
}
}
Loading

0 comments on commit 7ae85c4

Please sign in to comment.