public async Task HappyPath() { GetNearbyPostcodesWithoutAddressesRequest req = new GetNearbyPostcodesWithoutAddressesRequest() { Postcode = "NG1 5FS" }; GetNearbyPostcodesWithoutAddresses getNearbyPostcodes = new GetNearbyPostcodesWithoutAddresses(_mediator.Object, _postcodeValidator.Object, _logger.Object); IActionResult result = await getNearbyPostcodes.Run(req, CancellationToken.None); OkObjectResult objectResult = result as OkObjectResult; Assert.IsNotNull(objectResult); Assert.AreEqual(200, objectResult.StatusCode); ResponseWrapper <GetNearbyPostcodesWithoutAddressesResponse, AddressServiceErrorCode> deserialisedResponse = objectResult.Value as ResponseWrapper <GetNearbyPostcodesWithoutAddressesResponse, AddressServiceErrorCode>; Assert.IsNotNull(deserialisedResponse); Assert.IsTrue(deserialisedResponse.HasContent); Assert.IsTrue(deserialisedResponse.IsSuccessful); Assert.AreEqual(0, deserialisedResponse.Errors.Count()); Assert.AreEqual("NG1 5FS", deserialisedResponse.Content.NearestPostcodes.FirstOrDefault().Postcode); _mediator.Verify(x => x.Send(It.IsAny <GetNearbyPostcodesWithoutAddressesRequest>(), It.IsAny <CancellationToken>())); }
public async Task InvalidPostcode() { _postcodeValidator.Setup(x => x.IsPostcodeValidAsync(It.IsAny <string>())).ReturnsAsync(false); GetNearbyPostcodesWithoutAddressesRequest req = new GetNearbyPostcodesWithoutAddressesRequest() { Postcode = "NG1 5FS" }; GetNearbyPostcodesWithoutAddresses getNearbyPostcodes = new GetNearbyPostcodesWithoutAddresses(_mediator.Object, _postcodeValidator.Object, _logger.Object); IActionResult result = await getNearbyPostcodes.Run(req, CancellationToken.None); ObjectResult objectResult = result as ObjectResult; Assert.IsNotNull(objectResult); Assert.AreEqual(422, objectResult.StatusCode); ResponseWrapper <GetNearbyPostcodesWithoutAddressesResponse, AddressServiceErrorCode> deserialisedResponse = objectResult.Value as ResponseWrapper <GetNearbyPostcodesWithoutAddressesResponse, AddressServiceErrorCode>; Assert.IsNotNull(deserialisedResponse); Assert.IsFalse(deserialisedResponse.HasContent); Assert.IsFalse(deserialisedResponse.IsSuccessful); Assert.AreEqual(1, deserialisedResponse.Errors.Count()); Assert.AreEqual(AddressServiceErrorCode.InvalidPostcode, deserialisedResponse.Errors[0].ErrorCode); _mediator.Verify(x => x.Send(It.IsAny <GetNearbyPostcodesWithoutAddressesRequest>(), It.IsAny <CancellationToken>()), Times.Never); }
public async Task ErrorThrown() { _mediator.Setup(x => x.Send(It.IsAny <GetNearbyPostcodesWithoutAddressesRequest>(), It.IsAny <CancellationToken>())).Throws <Exception>(); GetNearbyPostcodesWithoutAddressesRequest req = new GetNearbyPostcodesWithoutAddressesRequest() { Postcode = "NG1 5FS" }; GetNearbyPostcodesWithoutAddresses getNearbyPostcodes = new GetNearbyPostcodesWithoutAddresses(_mediator.Object, _postcodeValidator.Object, _logger.Object); IActionResult result = await getNearbyPostcodes.Run(req, CancellationToken.None); ObjectResult objectResult = result as ObjectResult; Assert.IsNotNull(objectResult); ResponseWrapper <GetNearbyPostcodesWithoutAddressesResponse, AddressServiceErrorCode> deserialisedResponse = objectResult.Value as ResponseWrapper <GetNearbyPostcodesWithoutAddressesResponse, AddressServiceErrorCode>; Assert.IsNotNull(deserialisedResponse); Assert.AreEqual(500, objectResult.StatusCode);; Assert.IsFalse(deserialisedResponse.HasContent); Assert.IsFalse(deserialisedResponse.IsSuccessful); Assert.AreEqual(1, deserialisedResponse.Errors.Count()); Assert.AreEqual(AddressServiceErrorCode.UnhandledError, deserialisedResponse.Errors[0].ErrorCode); _mediator.Verify(x => x.Send(It.IsAny <GetNearbyPostcodesWithoutAddressesRequest>(), It.IsAny <CancellationToken>())); _logger.Verify(x => x.LogErrorAndNotifyNewRelic(It.Is <string>(y => y.Contains("Unhandled error")), It.IsAny <Exception>(), It.IsAny <GetNearbyPostcodesWithoutAddressesRequest>())); }
public async Task <IActionResult> Run( [HttpTrigger(AuthorizationLevel.Function, "get", Route = null)] [RequestBodyType(typeof(GetNearbyPostcodesWithoutAddressesRequest), "get nearby postcodes without addresses")] GetNearbyPostcodesWithoutAddressesRequest req, CancellationToken cancellationToken) { try { NewRelic.Api.Agent.NewRelic.SetTransactionName("AddressService", "GetNearbyPostcodesWithoutAddresses"); _logger.LogInformation("C# HTTP trigger function processed a request."); // This validation logic belongs in a custom validation attribute on the Postcode property. However, validationContext.GetService<IExternalService> always returned null in the validation attribute (despite DI working fine elsewhere). I didn't want to spend a lot of time finding out why when there is lots to do so I've put the postcode validation logic here for now. if (!await _postcodeValidator.IsPostcodeValidAsync(req.Postcode)) { return(new ObjectResult(ResponseWrapper <GetNearbyPostcodesWithoutAddressesResponse, AddressServiceErrorCode> .CreateUnsuccessfulResponse(AddressServiceErrorCode.InvalidPostcode, "Invalid postcode")) { StatusCode = 422 }); } if (req.IsValid(out var validationResults)) { GetNearbyPostcodesWithoutAddressesResponse response = await _mediator.Send(req, cancellationToken); return(new OkObjectResult(ResponseWrapper <GetNearbyPostcodesWithoutAddressesResponse, AddressServiceErrorCode> .CreateSuccessfulResponse(response))); } else { return(new ObjectResult(ResponseWrapper <GetNearbyPostcodesWithoutAddressesResponse, AddressServiceErrorCode> .CreateUnsuccessfulResponse(AddressServiceErrorCode.ValidationError, validationResults)) { StatusCode = 422 }); } } catch (Exception ex) { _logger.LogErrorAndNotifyNewRelic($"Unhandled error in GetNearbyPostcodesWithoutAddresses", ex, req); return(new ObjectResult(ResponseWrapper <GetNearbyPostcodesWithoutAddressesResponse, AddressServiceErrorCode> .CreateUnsuccessfulResponse(AddressServiceErrorCode.UnhandledError, "Internal Error")) { StatusCode = StatusCodes.Status500InternalServerError }); } }
public async Task GetNumberOfAddressesPerPostcodeInBoundaryResponse() { GetNearbyPostcodesWithoutAddressesRequest request = new GetNearbyPostcodesWithoutAddressesRequest() { Postcode = "NG 2AA", MaxNumberOfResults = 1, RadiusInMetres = 200 }; GetNearbyPostcodesWithoutAddressesHandler getNearbyPostcodesWithoutAddressesHandler = new GetNearbyPostcodesWithoutAddressesHandler(_mapper.Object, _nearestPostcodeGetter.Object); GetNearbyPostcodesWithoutAddressesResponse result = await getNearbyPostcodesWithoutAddressesHandler.Handle(request, CancellationToken.None); Assert.AreEqual(1, result.NearestPostcodes.Count); Assert.AreEqual("NG1 1AA", result.NearestPostcodes.FirstOrDefault().Postcode); Assert.AreEqual(99, result.NearestPostcodes.FirstOrDefault().DistanceInMetres); _nearestPostcodeGetter.Setup(x => x.GetNearestPostcodesAsync(It.Is <string>(y => y == "NG 2AA"), It.Is <int?>(y => y == 200), It.Is <int?>(y => y == 1))); _mapper.Verify(x => x.Map <IEnumerable <NearestPostcodeDto>, IEnumerable <NearestPostcodeWithoutAddress> >(It.Is <IEnumerable <NearestPostcodeDto> >(y => y.Count() == 1 && y.Any(z => z.Postcode == "NG1 1AA"))), Times.Once); }