Ejemplo n.º 1
0
        public void ShouldReturnValidationErrorWhenSavingDonorWithInvalidData()
        {
            int    validationErrorCode    = 1;
            string validationErrorMessage = "test error";

            GiftAidDonorRequest giftAidDonorRequest = this.GetGiftAidDonorRequest();

            giftAidDonorRequest.DonationAmount = 1.99m;

            var validationError = new ValidationError(validationErrorCode, validationErrorMessage);
            var errors          = new List <ValidationError> {
                validationError
            };

            this._giftAidCalculatorServiceMock.Setup(_ => _.Calculate(giftAidDonorRequest.DonationAmount)).Verifiable();
            this._giftAidDonorServiceMock.Setup(_ => _.SaveDonorDetails(giftAidDonorRequest)).Verifiable();
            this._giftAidValidatorMock.Setup(_ => _.IsValid(giftAidDonorRequest.DonationAmount)).Returns(false);
            this._giftAidValidatorMock.Setup(_ => _.Errors).Returns(errors);

            var response     = this._controller.Post(giftAidDonorRequest) as BadRequestObjectResult;
            var resultErrors = response.Value as List <ValidationError>;

            this._giftAidCalculatorServiceMock.Verify(_ => _.Calculate(It.IsAny <decimal>()), Times.Never);
            this._giftAidDonorServiceMock.Verify(_ => _.SaveDonorDetails(giftAidDonorRequest), Times.Never);
            Assert.That(response.StatusCode, Is.EqualTo(StatusCodes.Status400BadRequest));
            Assert.That(resultErrors.Count, Is.EqualTo(1));
            Assert.That(resultErrors, Is.EqualTo(errors));
        }
Ejemplo n.º 2
0
        public int SaveGiftAidDonor(GiftAidDonorRequest donor)
        {
            int id = this._database.Count + 1;

            this._database.Add(1, donor);

            return(id);
        }
Ejemplo n.º 3
0
        public void ShouldReturnInternalServerErrorWhenSavingDonor()
        {
            GiftAidDonorRequest giftAidDonorRequest = this.GetGiftAidDonorRequest();

            this._giftAidValidatorMock.Setup(_ => _.IsValid(giftAidDonorRequest.DonationAmount)).Returns(true);
            this._giftAidDonorServiceMock.Setup(_ => _.SaveDonorDetails(giftAidDonorRequest)).Throws <Exception>();

            var response = this._controller.Post(giftAidDonorRequest) as StatusCodeResult;

            Assert.That(response.StatusCode, Is.EqualTo(StatusCodes.Status500InternalServerError));
        }
Ejemplo n.º 4
0
        public void ShouldReturnUniqueId()
        {
            var giftAidDonorRepository = new GiftAidDonorRepository();

            var giftAidDonorRequest = new GiftAidDonorRequest
            {
                DonationAmount = 100m,
                Name           = "Joe Bloggs",
                Postcode       = "WC2N 5DU"
            };

            int id = giftAidDonorRepository.SaveGiftAidDonor(giftAidDonorRequest);

            Assert.That(id, Is.GreaterThan(0));
        }
Ejemplo n.º 5
0
        public GiftAidDonorResponse SaveDonorDetails(GiftAidDonorRequest request)
        {
            decimal giftAidAmount = this._giftAidCalculatorService.Calculate(request.DonationAmount);

            int declarationId = this._giftAidDonorRepository.SaveGiftAidDonor(request);

            return(new GiftAidDonorResponse
            {
                DeclarationId = declarationId,
                DonationAmount = request.DonationAmount,
                GiftAidAmount = giftAidAmount,
                Name = request.Name,
                Postcode = request.Postcode
            });
        }
Ejemplo n.º 6
0
        public IActionResult Post([FromBody] GiftAidDonorRequest request)
        {
            return(this.ExecuteAndHandleErrors(() =>
            {
                if (ModelState.IsValid == false)
                {
                    return BadRequest(this.GetErrorsFromModelState());
                }

                if (this._giftAidValidator.IsValid(request.DonationAmount))
                {
                    return Ok(this._giftAidDonorService.SaveDonorDetails(request));
                }

                return BadRequest(this._giftAidValidator.Errors);
            }));
        }
Ejemplo n.º 7
0
        public void ShouldReturnBadRequestWhenSuppliedModelIsInvalid()
        {
            GiftAidDonorRequest giftAidDonorRequest = this.GetGiftAidDonorRequest();

            this._giftAidCalculatorServiceMock.Setup(_ => _.Calculate(giftAidDonorRequest.DonationAmount)).Verifiable();
            this._giftAidDonorServiceMock.Setup(_ => _.SaveDonorDetails(giftAidDonorRequest)).Verifiable();
            this._giftAidValidatorMock.Setup(_ => _.IsValid(giftAidDonorRequest.DonationAmount)).Verifiable();

            this._controller.ModelState.AddModelError("test-error", "Test error");
            var response     = this._controller.Post(giftAidDonorRequest) as BadRequestObjectResult;
            var resultErrors = response.Value as List <ValidationError>;

            this._giftAidCalculatorServiceMock.Verify(_ => _.Calculate(It.IsAny <decimal>()), Times.Never);
            this._giftAidDonorServiceMock.Verify(_ => _.SaveDonorDetails(giftAidDonorRequest), Times.Never);
            this._giftAidValidatorMock.Verify(_ => _.IsValid(giftAidDonorRequest.DonationAmount), Times.Never);

            Assert.That(resultErrors.Count, Is.EqualTo(1));
        }
        public void ShouldReturnAGiftAidDonorResponseWithAllTheDetails()
        {
            var giftAidDonorRequest = new GiftAidDonorRequest
            {
                DonationAmount = 100m,
                Name           = "Joe Bloggs",
                Postcode       = "WC2N 5DU"
            };
            var expectedGiftAid = 25m;
            int expectedId      = 1;

            this._giftAidCalculatorServiceMock.Setup(_ => _.Calculate(giftAidDonorRequest.DonationAmount)).Returns(expectedGiftAid);
            this._giftAidDonorRepositoryMock.Setup(_ => _.SaveGiftAidDonor(giftAidDonorRequest)).Returns(expectedId);

            GiftAidDonorResponse response = this._giftAidDonorService.SaveDonorDetails(giftAidDonorRequest);

            Assert.That(response.DeclarationId, Is.EqualTo(expectedId));
            Assert.That(response.GiftAidAmount, Is.EqualTo(expectedGiftAid));
            Assert.That(response.DonationAmount, Is.EqualTo(giftAidDonorRequest.DonationAmount));
            Assert.That(response.Name, Is.EqualTo(giftAidDonorRequest.Name));
            Assert.That(response.Postcode, Is.EqualTo(giftAidDonorRequest.Postcode));
        }