public void Setup() { _mockSearchProvider = new Mock <IProviderSearchProvider>(); _mockPostCodeLookup = new Mock <ILookupLocations>(); _validQuery = new GetClosestLocationsQuery { ApprenticeshipId = "123", Ukprn = 12345678, PostCode = "AB12 3CD" }; _postCodeResponse = new CoordinateResponse { Coordinate = new Coordinate { Lat = 50, Lon = 1 }, ResponseCode = LocationLookupResponse.Ok }; _mockPostCodeLookup.Setup(x => x.GetLatLongFromPostCode(It.IsAny <string>())).ReturnsAsync(_postCodeResponse); _handler = new GetClosestLocationsHandler( new NullLogger <GetClosestLocationsHandler>(), new GetClosestLocationsQueryValidator(new Validation()), _mockSearchProvider.Object, _mockPostCodeLookup.Object); }
public async Task <CoordinateResponse> GetLatLongFromPostCode(string postcode) { var coordinates = new Coordinate(); var uri = new Uri(_applicationSettings.PostcodeUrl, postcode.Replace(" ", string.Empty)); try { var stopwatch = Stopwatch.StartNew(); var response = await _retryService.RetryWeb(() => MakeRequestAsync(uri.ToString()), CouldntConnect); stopwatch.Stop(); var responseTime = stopwatch.ElapsedMilliseconds; if (response.IsSuccessStatusCode) { var value = await response.Content.ReadAsStringAsync(); var result = JsonConvert.DeserializeObject <PostCodeResponse>(value); if (!result.Result.Latitude.HasValue || !result.Result.Longitude.HasValue) { return(new CoordinateResponse { Coordinate = null, ResponseCode = LocationLookupResponse.MissingCoordinates }); } coordinates.Lat = result.Result.Latitude.Value; coordinates.Lon = result.Result.Longitude.Value; SendDependencyLog(response.StatusCode, uri, responseTime); var coordinateResponse = new CoordinateResponse { Coordinate = coordinates, ResponseCode = LocationLookupResponse.Ok }; return(coordinateResponse); } if (response.StatusCode == HttpStatusCode.InternalServerError) { LogInformation(postcode, uri, response, responseTime, "Postcodes.IO-ServerError", "Server error trying to find postcode"); return(new CoordinateResponse { Coordinate = null, ResponseCode = LocationLookupResponse.ServerError }); } LogInformation(postcode, uri, response, responseTime, "Postcodes.IO-PostCodeNotFound", "Unable to find coordinates for postcode"); return(new CoordinateResponse { Coordinate = null, ResponseCode = LocationLookupResponse.WrongPostcode }); } catch (Exception ex) { _logger.Error(ex, $"Unable to connect to Postcode lookup servce. Url: {uri}"); throw new SearchException("Unable to connect to Post Code Lookup service", ex); } }