public void SetUp() { GetPostcodeResponse getNearbyGetPostcodesResponse = new GetPostcodeResponse() { Postcode = "NG1 5FS" }; _mapper = new Mock <IMapper>(); _mapper.Setup(x => x.Map <PostcodeDto, GetPostcodeResponse>(It.IsAny <PostcodeDto>())).Returns(getNearbyGetPostcodesResponse); _postcodeGetter = new Mock <IPostcodeAndAddressGetter>(); _postcodeGetter.SetupAllProperties(); _addressDetailsSorter = new Mock <IAddressDetailsSorter>(); _addressDetailsSorter.SetupAllProperties(); }
public async Task GetPostcode() { GetPostcodeHandler getPostcodeHandler = new GetPostcodeHandler(_mapper.Object, _postcodeGetter.Object, _addressDetailsSorter.Object); GetPostcodeRequest request = new GetPostcodeRequest() { Postcode = "ng15fs" }; GetPostcodeResponse result = await getPostcodeHandler.Handle(request, CancellationToken.None); Assert.AreEqual("NG1 5FS", result.Postcode); _postcodeGetter.Verify(x => x.GetPostcodeAsync(It.Is <string>(y => y == "NG1 5FS"), It.IsAny <CancellationToken>())); _mapper.Verify(x => x.Map <PostcodeDto, GetPostcodeResponse>(It.IsAny <PostcodeDto>())); _addressDetailsSorter.Verify(x => x.OrderAddressDetailsResponse(It.IsAny <IEnumerable <AddressDetailsResponse> >())); }
public void SetUp() { _postcodeValidator = new Mock <IPostcodeValidator>(); _postcodeValidator.Setup(x => x.IsPostcodeValidAsync(It.IsAny <string>())).ReturnsAsync(true); _response = new GetPostcodeResponse() { AddressDetails = new List <AddressDetailsResponse>(), Postcode = "NG1 5FS" }; _mediator = new Mock <IMediator>(); _mediator.Setup(x => x.Send(It.IsAny <GetPostcodeRequest>(), It.IsAny <CancellationToken>())).ReturnsAsync(_response); _logger = new Mock <ILoggerWrapper <GetPostcode> >(); _logger.SetupAllProperties(); }
public async Task <IActionResult> Run( [HttpTrigger(AuthorizationLevel.Function, "get", Route = null)] [RequestBodyType(typeof(GetPostcodeRequest), "Get Postcode")] GetPostcodeRequest req, CancellationToken cancellationToken) { try { NewRelic.Api.Agent.NewRelic.SetTransactionName("AddressService", "GetPostcode"); _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 <GetPostcodeResponse, AddressServiceErrorCode> .CreateUnsuccessfulResponse(AddressServiceErrorCode.InvalidPostcode, "Invalid postcode")) { StatusCode = 422 }); } if (req.IsValid(out var validationResults)) { GetPostcodeResponse response = await _mediator.Send(req, cancellationToken); return(new OkObjectResult(ResponseWrapper <GetPostcodeResponse, AddressServiceErrorCode> .CreateSuccessfulResponse(response))); } else { return(new ObjectResult(ResponseWrapper <GetPostcodeResponse, AddressServiceErrorCode> .CreateUnsuccessfulResponse(AddressServiceErrorCode.ValidationError, validationResults)) { StatusCode = 422 }); } } catch (Exception ex) { _logger.LogErrorAndNotifyNewRelic($"Unhandled error in GetPostcode", ex, req); return(new ObjectResult(ResponseWrapper <GetPostcodeResponse, AddressServiceErrorCode> .CreateUnsuccessfulResponse(AddressServiceErrorCode.UnhandledError, "Internal Error")) { StatusCode = StatusCodes.Status500InternalServerError }); } }