public async Task <ActionResult <StoreGiftAidResponse> > Store([FromBody] StoreGiftAidRequest request) { try { var donationId = await _giftAidDonationsService.StoreDonation(request.Name, request.PostCode, request.DonationAmount); if (donationId != null) { var giftAidAmount = _giftAidCalculationService.CalculateGiftAid(request.DonationAmount); var response = new StoreGiftAidResponse { DonationId = donationId.Value, GiftAidAmount = giftAidAmount }; return(response); } return(StatusCode((int)HttpStatusCode.InternalServerError, "Failed to store donation")); } catch (Exception ex) { Log.Debug(ex.Message + Environment.NewLine + ex.StackTrace); return(StatusCode((int)HttpStatusCode.InternalServerError, "Failed to store donation")); } }
[InlineData("TestPerson5", "Postcode5", 1000000)] // Donation amount above upper bound public async Task Store_WhenCalledWithInvalidParameters_ReturnsBadRequest(string name, string postcode, int donationAmount) { // Arrange var data = new StoreGiftAidRequest { Name = name, PostCode = postcode, DonationAmount = donationAmount }; var jsonRequest = JsonConvert.SerializeObject(data); var content = new StringContent(jsonRequest, Encoding.UTF8, "application/json"); // Act var response = await _client.PostAsync("api/GiftAid/Store", content); // Assert Assert.False(response.IsSuccessStatusCode); Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode); }
public async Task Store_WhenCalled_StoresDonationInDatabase(string name, string postcode, int donationAmount, float expectedGiftAid) { // Arrange var data = new StoreGiftAidRequest { Name = name, PostCode = postcode, DonationAmount = donationAmount }; var jsonRequest = JsonConvert.SerializeObject(data); var content = new StringContent(jsonRequest, Encoding.UTF8, "application/json"); // Act var response = await _client.PostAsync("api/GiftAid/Store", content); var deserializedResult = JsonConvert.DeserializeObject <StoreGiftAidResponse>(await response.Content.ReadAsStringAsync()); // Assert response.EnsureSuccessStatusCode(); Assert.NotNull(deserializedResult); Assert.True(deserializedResult.DonationId > 0); Assert.Equal(expectedGiftAid, deserializedResult.GiftAidAmount); }