コード例 #1
0
        public void SetUp()
        {
            _postcodeValidator = new Mock <IPostcodeValidator>();
            _postcodeValidator.Setup(x => x.IsPostcodeValidAsync(It.IsAny <string>())).ReturnsAsync(true);


            _response = new GetNearbyPostcodesResponse()
            {
                Postcodes = new List <GetNearbyPostCodeResponse>()
                {
                    new GetNearbyPostCodeResponse()
                    {
                        Postcode         = "NG1 5FS",
                        AddressDetails   = new List <AddressDetailsResponse>(),
                        DistanceInMetres = 2
                    }
                }
            };

            _mediator = new Mock <IMediator>();

            _mediator.Setup(x => x.Send(It.IsAny <GetNearbyPostcodesRequest>(), It.IsAny <CancellationToken>())).ReturnsAsync(_response);

            _logger = new Mock <ILoggerWrapper <GetNearbyPostcodes> >();
            _logger.SetupAllProperties();
        }
コード例 #2
0
        public async Task GetPostcodes_SortedByDistanceAndLimited()
        {
            GetNearbyPostcodesHandler getNearbyPostcodesHandler = new GetNearbyPostcodesHandler(_nearestPostcodeGetter.Object, _mapper.Object, _postcodeGetter.Object, _addressDetailsSorter.Object);

            GetNearbyPostcodesRequest request = new GetNearbyPostcodesRequest()
            {
                Postcode           = "m11aa",
                RadiusInMetres     = 1604,
                MaxNumberOfResults = 100
            };

            GetNearbyPostcodesResponse result = await getNearbyPostcodesHandler.Handle(request, CancellationToken.None);

            Assert.AreEqual(2, result.Postcodes.Count);

            // check postcodes have been ordered by ascending distance
            Assert.AreEqual(1, result.Postcodes[0].DistanceInMetres);
            Assert.AreEqual("CR2 6XH", result.Postcodes[0].Postcode);
            Assert.AreEqual("friendlyName2", result.Postcodes[0].FriendlyName);

            Assert.AreEqual(2, result.Postcodes[1].DistanceInMetres);
            Assert.AreEqual("M1 1AA", result.Postcodes[1].Postcode);
            Assert.AreEqual("friendlyName1", result.Postcodes[1].FriendlyName);

            _nearestPostcodeGetter.Verify(x => x.GetNearestPostcodesAsync(It.Is <string>(y => y == "M1 1AA"), It.Is <int?>(y => y == 1604), It.Is <int?>(y => y == 100)));

            _postcodeGetter.Verify(x => x.GetPostcodesAsync(It.Is <IEnumerable <string> >(y =>
                                                                                          y.Count() == 3
                                                                                          ), It.IsAny <CancellationToken>()));

            _mapper.Verify(x => x.Map <IEnumerable <PostcodeDto>, IEnumerable <GetNearbyPostCodeResponse> >(It.IsAny <IEnumerable <PostcodeDto> >()));

            _addressDetailsSorter.Verify(x => x.OrderAddressDetailsResponse(It.IsAny <IEnumerable <AddressDetailsResponse> >()));
        }
コード例 #3
0
        public async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", Route = null)]
            [RequestBodyType(typeof(GetNearbyPostcodesRequest), "get nearby postcodes")] GetNearbyPostcodesRequest req,
            CancellationToken cancellationToken)
        {
            try
            {
                NewRelic.Api.Agent.NewRelic.SetTransactionName("AddressService", "GetNearbyPostcodes");
                _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 <GetNearbyPostcodesResponse, AddressServiceErrorCode> .CreateUnsuccessfulResponse(AddressServiceErrorCode.InvalidPostcode, "Invalid postcode"))
                    {
                        StatusCode = 422
                    });
                }

                if (req.IsValid(out var validationResults))
                {
                    GetNearbyPostcodesResponse response = await _mediator.Send(req, cancellationToken);

                    return(new OkObjectResult(ResponseWrapper <GetNearbyPostcodesResponse, AddressServiceErrorCode> .CreateSuccessfulResponse(response)));
                }
                else
                {
                    return(new ObjectResult(ResponseWrapper <GetNearbyPostcodesResponse, AddressServiceErrorCode> .CreateUnsuccessfulResponse(AddressServiceErrorCode.ValidationError, validationResults))
                    {
                        StatusCode = 422
                    });
                }
            }
            catch (Exception ex)
            {
                _logger.LogErrorAndNotifyNewRelic($"Unhandled error in GetNearbyPostcodes", ex, req);
                return(new ObjectResult(ResponseWrapper <GetNearbyPostcodesResponse, AddressServiceErrorCode> .CreateUnsuccessfulResponse(AddressServiceErrorCode.UnhandledError, "Internal Error"))
                {
                    StatusCode = StatusCodes.Status500InternalServerError
                });
            }
        }