Esempio n. 1
0
        public void AddModelExceptionErrors_WhenExistingStateErrors_TheNewErrorsMessagesShouldBeAppended()
        {
            var exception = new CommitmentsApiModelException(_fixture.Add2ExceptionErrors().Errors);

            _fixture.Add1ModelStateErrors().ModelState.AddModelExceptionErrors(exception);

            Assert.AreEqual(3, _fixture.ModelState.ErrorCount);
        }
Esempio n. 2
0
        public void AddModelExceptionErrors_WhenThereIsAnEmptyList_NoMessagesShouldBeAdded()
        {
            var exception = new CommitmentsApiModelException(_fixture.Errors);

            _fixture.ModelState.AddModelExceptionErrors(exception);

            Assert.AreEqual(0, _fixture.ModelState.ErrorCount);
        }
Esempio n. 3
0
        public void AddModelExceptionErrors_WhenThereAreErrorsAndWeAreMappingfieldNames_TheFieldNamesWillBeMapped()
        {
            var exception = new CommitmentsApiModelException(_fixture.Add2ExceptionErrors().Errors);

            _fixture.ModelState.AddModelExceptionErrors(exception, field => "xxx" + field);

            Assert.IsTrue(_fixture.ModelState.ContainsKey("xxxfield1"));
            Assert.IsTrue(_fixture.ModelState.ContainsKey("xxxfield2"));
        }
Esempio n. 4
0
        private Exception CreateApiModelException(HttpResponseMessage httpResponseMessage, string content)
        {
            if (string.IsNullOrWhiteSpace(content))
            {
                _logger.LogWarning($"{httpResponseMessage.RequestMessage.RequestUri} has returned an empty string when an array of error responses was expected.");
                return(new CommitmentsApiModelException(new List <ErrorDetail>()));
            }

            var errors = new CommitmentsApiModelException(JsonConvert.DeserializeObject <ErrorResponse>(content).Errors);

            var errorDetails = string.Join(";", errors.Errors.Select(e => $"{e.Field} ({e.Message})"));

            _logger.Log(errors.Errors.Count == 0 ? LogLevel.Warning : LogLevel.Debug, $"{httpResponseMessage.RequestMessage.RequestUri} has returned {errors.Errors.Count} errors: {errorDetails}");

            return(errors);
        }
        public DraftApprenticeshipControllerTestFixture()
        {
            var autoFixture = new Fixture();

            _cohortId = autoFixture.Create <long>();
            _draftApprenticeshipId       = autoFixture.Create <long>();
            _providerId                  = autoFixture.Create <int>();
            _cohortReference             = autoFixture.Create <string>();
            _draftApprenticeshipHashedId = autoFixture.Create <string>();

            _editDraftApprenticeshipRequest = autoFixture.Build <EditDraftApprenticeshipRequest>()
                                              .With(x => x.CohortId, _cohortId)
                                              .With(x => x.DraftApprenticeshipId, _draftApprenticeshipId)
                                              .Create();

            _editDraftApprenticeshipDetails = autoFixture.Build <GetDraftApprenticeshipResponse>()
                                              .Create();

            _createAddDraftApprenticeshipRequest = new AddDraftApprenticeshipRequest();
            _updateDraftApprenticeshipRequest    = new UpdateDraftApprenticeshipRequest();

            _reservationsAddDraftApprenticeshipRequest = autoFixture.Build <ReservationsAddDraftApprenticeshipRequest>()
                                                         .With(x => x.ProviderId, _providerId)
                                                         .With(x => x.CohortId, _cohortId)
                                                         .With(x => x.CohortReference, _cohortReference)
                                                         .With(x => x.StartMonthYear, "012019")
                                                         .Create();

            _courseResponse = new GetTrainingCoursesQueryResponse
            {
                TrainingCourses = new TrainingProgramme[0]
            };

            _addModel = new AddDraftApprenticeshipViewModel
            {
                ProviderId      = _providerId,
                CohortId        = _cohortId,
                CohortReference = _cohortReference
            };

            _editModel = new EditDraftApprenticeshipViewModel
            {
                ProviderId                  = _providerId,
                CohortId                    = _cohortId,
                CohortReference             = _cohortReference,
                DraftApprenticeshipId       = _draftApprenticeshipId,
                DraftApprenticeshipHashedId = _draftApprenticeshipHashedId
            };

            _cohortResponse = autoFixture.Build <GetCohortResponse>()
                              .With(x => x.LevyStatus, ApprenticeshipEmployerType.Levy)
                              .With(x => x.ChangeOfPartyRequestId, default(long?))
                              .Create();

            _apiModelException = new CommitmentsApiModelException(new List <ErrorDetail>()
            {
                new ErrorDetail("Name", "Cannot be more than...")
            });

            _mediator = new Mock <IMediator>();
            _mediator.Setup(x => x.Send(It.IsAny <GetTrainingCoursesQueryRequest>(), It.IsAny <CancellationToken>()))
            .ReturnsAsync(_courseResponse);

            _modelMapper = new Mock <IModelMapper>();
            _modelMapper.Setup(x => x.Map <AddDraftApprenticeshipRequest>(It.IsAny <AddDraftApprenticeshipViewModel>()))
            .ReturnsAsync(_createAddDraftApprenticeshipRequest);

            _modelMapper.Setup(x => x.Map <EditDraftApprenticeshipViewModel>(It.IsAny <EditDraftApprenticeshipRequest>()))
            .ReturnsAsync(_editModel);

            _modelMapper.Setup(x => x.Map <UpdateDraftApprenticeshipRequest>(It.IsAny <EditDraftApprenticeshipViewModel>()))
            .ReturnsAsync(_updateDraftApprenticeshipRequest);

            _linkGenerator = new Mock <ILinkGenerator>();
            _linkGenerator.Setup(x => x.ProviderApprenticeshipServiceLink(It.IsAny <string>()))
            .Returns <string>(input => input);

            _commitmentsApiClient = new Mock <ICommitmentsApiClient>();
            _commitmentsApiClient.Setup(x => x.GetCohort(It.IsAny <long>(), It.IsAny <CancellationToken>()))
            .ReturnsAsync(_cohortResponse);

            _controller = new DraftApprenticeshipController(_mediator.Object,
                                                            _linkGenerator.Object, _commitmentsApiClient.Object, _modelMapper.Object);
        }
        public static void AddModelExceptionErrors(this ModelStateDictionary modelState, CommitmentsApiModelException exception, Func <string, string> fieldNameMapper = null)
        {
            if (exception?.Errors == null)
            {
                return;
            }

            foreach (var error in exception.Errors)
            {
                var field = fieldNameMapper == null ? error.Field : fieldNameMapper(error.Field);

                modelState.AddModelError(field, error.Message);
            }
        }
        public AddDraftApprenticeshipTestsFixture()
        {
            Cohort = new CohortDetails
            {
                CohortId     = 1,
                ProviderName = "Foobar",
                WithParty    = Party.Employer
            };

            Request = new AddDraftApprenticeshipRequest
            {
                AccountHashedId            = "AAA000",
                CohortReference            = "BBB111",
                CohortId                   = Cohort.CohortId,
                AccountLegalEntityHashedId = "CCC222",
                AccountLegalEntityId       = 2,
                ReservationId              = Guid.NewGuid(),
                StartMonthYear             = "092019",
                CourseCode                 = "DDD333"
            };

            ViewModel = new AddDraftApprenticeshipViewModel
            {
                AccountHashedId            = Request.AccountHashedId,
                CohortReference            = Request.CohortReference,
                CohortId                   = Request.CohortId,
                AccountLegalEntityHashedId = Request.AccountLegalEntityHashedId,
                AccountLegalEntityId       = Request.AccountLegalEntityId,
                ReservationId              = Request.ReservationId,
                StartDate                  = new MonthYearModel(Request.StartMonthYear),
                CourseCode                 = Request.CourseCode
            };

            AddDraftApprenticeshipRequest = new CommitmentsV2.Api.Types.Requests.AddDraftApprenticeshipRequest();
            StandardCourses              = new List <TrainingProgramme>();
            Courses                      = new List <TrainingProgramme>();
            CohortDetailsUrl             = $"accounts/{Request.AccountHashedId}/apprentices/{Request.CohortReference}/details";
            CommitmentsApiModelException = new CommitmentsApiModelException(new List <ErrorDetail> {
                new ErrorDetail("Foo", "Bar")
            });
            CommitmentsApiClient = new Mock <ICommitmentsApiClient>();
            ModelMapper          = new Mock <IModelMapper>();
            LinkGenerator        = new Mock <ILinkGenerator>();
            AuthorizationService = new Mock <IAuthorizationService>();
            AuthorizationService.Setup(x => x.IsAuthorized(EmployerFeature.EnhancedApproval)).Returns(false);

            Controller = new DraftApprenticeshipController(
                ModelMapper.Object,
                CommitmentsApiClient.Object,
                AuthorizationService.Object
                );

            CommitmentsApiClient.Setup(c => c.GetAllTrainingProgrammes(CancellationToken.None)).ReturnsAsync(new GetAllTrainingProgrammesResponse {
                TrainingProgrammes = Courses
            });
            CommitmentsApiClient.Setup(c => c.GetAllTrainingProgrammeStandards(CancellationToken.None)).ReturnsAsync(new GetAllTrainingProgrammeStandardsResponse {
                TrainingProgrammes = StandardCourses
            });
            ModelMapper.Setup(m => m.Map <CommitmentsV2.Api.Types.Requests.AddDraftApprenticeshipRequest>(ViewModel)).Returns(Task.FromResult(AddDraftApprenticeshipRequest));

            ModelMapper.Setup(m => m.Map <AddDraftApprenticeshipViewModel>(It.IsAny <AddDraftApprenticeshipRequest>())).ReturnsAsync(ViewModel);
        }