public async Task Test_SerializationAsync()
        {
            // Get the client.
            IGeocodingClient client = Fixture.GetRequiredService <IGeocodingClient>();

            // Set up the request.
            var request = new GeocodeRequest(
                "1600 Pennsylvania Avenue, Washington D.C., United States"
                );

            // Make the request.
            var results = await client
                          .GeocodeAsync(request, default)
                          .ConfigureAwait(false);

            // Create the container.
            var expected = new Container <GeocodeResponse> {
                Value = results
            };

            // Serialize to a string.
            string json = JsonSerializer.Serialize(expected);

            // Serialize again.
            var actual = JsonSerializer.Deserialize <Container <GeocodeResponse> >(json);

            // Compare.
            Assert.Equal(expected.Value.Status, actual.Value.Status);
            Assert.Equal(expected.Value.Results.Count, actual.Value.Results.Count);
        }
        public async Task Successful()
        {
            _restClientMock.Setup(mock => mock.GetAsync <GeocodingResponse>(It.IsAny <string>())).ReturnsAsync(CreateResponseOk(_mockLocation));
            var address = new Address
            {
                Row1     = "Regeringsgatan 67",
                PostCode = "11156",
                Country  = "Sweden"
            };
            var response = await _client.GeocodeAsync(address);

            Assert.IsNotNull(response?.results);
            Assert.AreEqual("OK", response.status);
            var result   = response?.results?.FirstOrDefault();
            var location = result?.geometry?.location;

            Assert.IsNotNull(location);
            Assert.AreEqual(_mockLocation.lng, location.lng);
            Assert.AreEqual(_mockLocation.lat, location.lat);
        }
Exemple #3
0
        public async Task <Location> GeocodeAsync(Address address, bool mustBeUnique)
        {
            InternalContract.RequireNotNull(address, nameof(address));
            InternalContract.RequireValidated(address, nameof(address));

            var response = await _client.GeocodeAsync(Mapping.ToStorage(address));

            FulcrumAssert.IsNotNull(response, $"{Namespace}: 9410F4F3-FCFB-4B82-9E9D-5061F41EB31B");
            switch (response.status)
            {
            case "OK":
                FulcrumAssert.AreEqual(1, response.results.Length, $"{Namespace}: D7A969EE-B513-4093-ACB1-4D3B8168B4A6");
                var firstResult = response.results?.FirstOrDefault();
                FulcrumAssert.IsNotNull(firstResult?.geometry?.location, $"{Namespace}: D7A969EE-B513-4093-ACB1-4D3B8168B4A6", $"Google geocoding returned an unexpected result: {JObject.FromObject(response).ToString(Formatting.Indented)}");
                if (mustBeUnique && response.results.Length > 1)
                {
                    throw new FulcrumContractException($"The geoconding was not unique. There were ({response.results.Length}) results in the response.");
                }
                var location = Mapping.ToService(firstResult?.geometry?.location);
                FulcrumAssert.IsNotNull(location, $"{Namespace}: F21B761B-0CD4-4063-8780-70F2921301D1");
                FulcrumAssert.IsValidated(location, $"{Namespace}: 5DB577C2-C3EC-4C0A-AFF1-F018635CF8AF");
                return(location);

            case "ZERO_RESULTS":
                throw new FulcrumNotFoundException(response.error_message ?? $"Geocoding of Address {address} returned no results.");

            case "OVER_QUERY_LIMIT":
                throw new FulcrumForbiddenAccessException(response.error_message ?? "Google geocoding returned a status that says that the current API key is over the query limit.");

            case "REQUEST_DENIED":
                throw new FulcrumForbiddenAccessException(response.error_message ?? "Google geocoding returned a status that says that the request was denied. Invalid API key?");

            case "INVALID_REQUEST":
                FulcrumAssert.Fail($"{Namespace}: D2268351-8B84-4CCE-B5AB-793A9C4C0366", response.error_message ?? "Google geocoding returned a status that generally indicates that the query (address, components or latlng) is missing");
                throw new ApplicationException("Assertion failed to stop execution.");

            case "UNKNOWN_ERROR":
                throw new FulcrumTryAgainException(response.error_message ?? "Google geocoding returned a status that indicates that the request could not be processed due to a server error. The request may succeed if you try again.");

            default:
                FulcrumAssert.Fail($"{Namespace}: 96D375F3-2D5A-42B3-9168-145AD0D68CA7", response.error_message ?? $"Google geocoding returned a an unkown status code ({response.status})");
                throw new ApplicationException("Assertion failed to stop execution.");
            }
        }
        public async Task Test_GeocodeAsync()
        {
            // Get the client.
            IGeocodingClient client = Fixture.GetRequiredService <IGeocodingClient>();

            // Set up the request.
            var request = new GeocodeRequest(
                "1600 Pennsylvania Avenue, Washington D.C., United States"
                );

            // Make the request.
            var actual = await client
                         .GeocodeAsync(request, default)
                         .ConfigureAwait(false);

            // Validate response.
            Assert.Equal("OK", actual.Status);
            Assert.Single(actual.Results);
            Assert.Contains(actual.Results.Single().AddressComponents, c => c.LongName == "1600");
        }