public void GetQueryStringParametersTest()
        {
            var request = new LocationGeocodeRequest
            {
                Key      = "key",
                Location = new Coordinate(1, 1)
            };

            var queryStringParameters = request.GetQueryStringParameters();

            var key         = queryStringParameters.SingleOrDefault(x => x.Key == "key");
            var keyExpected = request.Key;

            Assert.IsNotNull(key);
            Assert.AreEqual(keyExpected, key.Value);

            var language         = queryStringParameters.SingleOrDefault(x => x.Key == "language");
            var languageExpected = request.Language.ToCode();

            Assert.IsNotNull(language);
            Assert.AreEqual(languageExpected, language.Value);

            var location         = queryStringParameters.SingleOrDefault(x => x.Key == "latlng");
            var locationExpected = request.Location.ToString();

            Assert.IsNotNull(location);
            Assert.AreEqual(locationExpected, location.Value);
        }
        /* This function was written by Lennart de Waart (563079) */
        /// <summary>
        /// Public asynchronous method for retrieving the address from the latitude and longitude.
        /// GoogleApi source: https://github.com/vivet/GoogleApi
        /// </summary>
        /// <param name="latLng"></param>
        /// <param name="apiKey"></param>
        /// <returns>Filled AddressData class or null</returns>
        public async Task <AddressData> GetAddressFromLatLong(LatLng latLng, string apiKey)
        {
            try
            {
                _logger.Information($"A request has been made to get the addres of latitude {latLng.Latitude} and longitude {latLng.Longitude} from the Google Geocode API.");
                // Create a GeocodeRequest
                LocationGeocodeRequest l = new LocationGeocodeRequest
                {
                    Location = new GoogleApi.Entities.Common.Location(latLng.Latitude, latLng.Longitude),
                    Key      = apiKey
                };
                // POST request to Google Maps API
                GeocodeResponse r = await GoogleMaps.LocationGeocode.QueryAsync(l);

                // Response contains a list of addresses around the location, get the first address and get the address components
                List <AddressComponent> results = r.Results.ToList()[0].AddressComponents.ToList();
                // Google API response results differs in length, so to correctly assign the properties of AddressData in a foreach loop
                AddressData a = AssignAddressDataPropertiesFromGoogle(results);
                // AddressComponent consists of multiple parts of the address, 0 equals the home number, 1 equals the street, 6 equals the postal code and 2 equals the place
                return(a ?? throw new ArgumentNullException($"Dependency failure: Google Geocode API request returned null for lat {latLng.Latitude} and long {latLng.Longitude}."));
            }
            catch (Exception e) // Error handling
            {
                _logger.Error($"ILocationsService says: {e.Message} Exception occured on line {new StackTrace(e, true).GetFrame(0).GetFileLineNumber()}.");
                return(null);
            }
        }
Exemplo n.º 3
0
        public void LocationGeocodeWhenResultTypesTest()
        {
            var request = new LocationGeocodeRequest
            {
                Key         = this.ApiKey,
                Location    = new Entities.Common.Location(40.7141289, -73.9614074),
                ResultTypes = new List <PlaceLocationType> {
                    PlaceLocationType.Premise, PlaceLocationType.Accounting
                }
            };
            var response = GoogleMaps.LocationGeocode.Query(request);
            var result   = response.Results.FirstOrDefault();

            Assert.IsNotNull(response);
            Assert.AreEqual(Status.Ok, response.Status);

            Assert.IsNotNull(result);
            Assert.AreEqual("285 Bedford Ave, Brooklyn, NY 11211, USA", result.FormattedAddress);

            var types = result.Types?.ToArray();

            Assert.IsNotNull(types);
            Assert.IsNotEmpty(types);
            Assert.Contains(PlaceLocationType.Premise, types);
        }
Exemplo n.º 4
0
        public void LocationGeocodeWhenLocationIsNullTest()
        {
            var request   = new LocationGeocodeRequest();
            var exception = Assert.Throws <ArgumentException>(() => GoogleMaps.LocationGeocode.Query(request, TimeSpan.FromMilliseconds(1)));

            Assert.IsNotNull(exception);
            Assert.AreEqual(exception.Message, "Location is required.");
        }
Exemplo n.º 5
0
        public void GetQueryStringParametersTest()
        {
            var request = new LocationGeocodeRequest
            {
                Key      = "abc",
                Location = new Entities.Common.Location(1, 1)
            };

            Assert.DoesNotThrow(() => request.GetQueryStringParameters());
        }
        public void GetQueryStringParametersWhenKeyIsEmptyTest()
        {
            var request = new LocationGeocodeRequest
            {
                Key = string.Empty
            };

            var exception = Assert.Throws <ArgumentException>(() => request.GetQueryStringParameters());

            Assert.AreEqual("'Key' is required", exception.Message);
        }
Exemplo n.º 7
0
        public void LocationGeocodeWhenAsyncTest()
        {
            var request = new LocationGeocodeRequest
            {
                Location = new Entities.Common.Location(40.7141289, -73.9614074)
            };
            var result = GoogleMaps.LocationGeocode.QueryAsync(request).Result;

            Assert.IsNotNull(result);
            Assert.AreEqual(Status.Ok, result.Status);
        }
Exemplo n.º 8
0
        public void LocationGeocodeWhenAsyncTest()
        {
            var request = new LocationGeocodeRequest
            {
                Key      = this.ApiKey,
                Location = new Coordinate(40.7141289, -73.9614074)
            };
            var result = GoogleMaps.LocationGeocode.QueryAsync(request).Result;

            Assert.IsNotNull(result);
            Assert.AreEqual(Status.Ok, result.Status);
        }
Exemplo n.º 9
0
        public string getFormattedAddress(Location location)
        {
            LocationGeocodeRequest request = new LocationGeocodeRequest();

            request.Key      = this.GoogleApiKey;
            request.Location = location;
            var response_geocode = GoogleApi.GoogleMaps.LocationGeocode.Query(request);

            string address = response_geocode.Results.First().FormattedAddress.ToString();

            return(address);
        }
Exemplo n.º 10
0
        public void LocationGeocodeWhenNoLocalCodeTest()
        {
            var request = new LocationGeocodeRequest
            {
                Key      = this.ApiKey,
                Location = new Coordinate(27.0675, -40.808)
            };

            var response = GoogleMaps.LocationGeocode.Query(request);

            Assert.IsNotNull(response);
            Assert.AreEqual(Status.Ok, response.Status);
        }
Exemplo n.º 11
0
        public void GetUriTest()
        {
            var request = new LocationGeocodeRequest
            {
                Key      = "abc",
                Location = new Entities.Common.Location(1, 1)
            };

            var uri = request.GetUri();

            Assert.IsNotNull(uri);
            Assert.AreEqual($"/maps/api/geocode/json?key={request.Key}&language={request.Language.ToCode()}&latlng={Uri.EscapeDataString(request.Location.ToString())}", uri.PathAndQuery);
        }
Exemplo n.º 12
0
        public void LocationGeocodeTest()
        {
            var request = new LocationGeocodeRequest
            {
                Key      = this.ApiKey,
                Location = new Coordinate(40.7141289, -73.9614074)
            };

            var response = GoogleMaps.LocationGeocode.Query(request);

            Assert.IsNotNull(response);
            Assert.AreEqual(Status.Ok, response.Status);
        }
Exemplo n.º 13
0
        public void LocationGeocodeWhenLocationIsNullTest()
        {
            var request = new LocationGeocodeRequest();

            var exception = Assert.Throws <AggregateException>(() => GoogleMaps.LocationGeocode.QueryAsync(request).Wait());

            Assert.IsNotNull(exception);

            var innerException = exception.InnerException;

            Assert.IsNotNull(innerException);
            Assert.AreEqual(typeof(GoogleApiException), innerException.GetType());
            Assert.AreEqual(innerException.Message, "Location is required");
        }
Exemplo n.º 14
0
        public void LocationGeocodeWhenResultTypesWhenNoResultsTest()
        {
            var request = new LocationGeocodeRequest
            {
                Key         = this.ApiKey,
                Location    = new Entities.Common.Location(40.7141289, -73.9614074),
                ResultTypes = new List <PlaceLocationType> {
                    PlaceLocationType.Accounting
                }
            };
            var response = GoogleMaps.LocationGeocode.Query(request);

            Assert.IsNotNull(response);
            Assert.AreEqual(Status.ZeroResults, response.Status);
        }
        public void GetQueryStringParametersWhenLocationIsNullTest()
        {
            var request = new LocationGeocodeRequest
            {
                Key = "key"
            };

            var exception = Assert.Throws <ArgumentException>(() =>
            {
                var parameters = request.GetQueryStringParameters();
                Assert.IsNull(parameters);
            });

            Assert.IsNotNull(exception);
            Assert.AreEqual(exception.Message, "'Location' is required");
        }
Exemplo n.º 16
0
        public void LocationGeocodeWhenAsyncAndCancelledTest()
        {
            var request = new LocationGeocodeRequest
            {
                Location = new Entities.Common.Location(40.7141289, -73.9614074)
            };
            var cancellationTokenSource = new CancellationTokenSource();
            var task = GoogleMaps.LocationGeocode.QueryAsync(request, cancellationTokenSource.Token);

            cancellationTokenSource.Cancel();

            var exception = Assert.Throws <OperationCanceledException>(() => task.Wait(cancellationTokenSource.Token));

            Assert.IsNotNull(exception);
            Assert.AreEqual(exception.Message, "The operation was canceled.");
        }
Exemplo n.º 17
0
        public void GetUriWhenLocationTypeTest()
        {
            var request = new LocationGeocodeRequest
            {
                Key           = "abc",
                Location      = new Entities.Common.Location(1, 1),
                LocationTypes = new[]
                {
                    GeometryLocationType.Rooftop
                }
            };

            var uri = request.GetUri();

            Assert.IsNotNull(uri);
            Assert.AreEqual($"/maps/api/geocode/json?key={request.Key}&language={request.Language.ToCode()}&latlng={Uri.EscapeDataString(request.Location.ToString())}&location_type={Uri.EscapeDataString(string.Join("|", request.LocationTypes.Select(x => x.ToString().ToUpper()).AsEnumerable()))}", uri.PathAndQuery);
        }
Exemplo n.º 18
0
        public void LocationGeocodeWhenLoncationTypesTest()
        {
            var request = new LocationGeocodeRequest
            {
                Key           = this.ApiKey,
                Location      = new Coordinate(40.7141289, -73.9614074),
                LocationTypes = new List <GeometryLocationType>
                {
                    GeometryLocationType.Rooftop
                }
            };

            var response = GoogleMaps.LocationGeocode.Query(request);

            Assert.IsNotNull(response);
            Assert.AreEqual(Status.Ok, response.Status);
        }
Exemplo n.º 19
0
        public void LocationGeocodeWhenResultTypesTest()
        {
            var request = new LocationGeocodeRequest
            {
                Key         = this.ApiKey,
                Location    = new Coordinate(40.7141289, -73.9614074),
                ResultTypes = new List <PlaceLocationType>
                {
                    PlaceLocationType.Premise,
                    PlaceLocationType.Accounting
                }
            };
            var response = GoogleMaps.LocationGeocode.Query(request);

            Assert.IsNotNull(response);
            Assert.AreEqual(Status.Ok, response.Status);
        }
Exemplo n.º 20
0
        public void GetUriWhenResultTypeTest()
        {
            var request = new LocationGeocodeRequest
            {
                Key         = "abc",
                Location    = new Entities.Common.Location(1, 1),
                ResultTypes = new[]
                {
                    PlaceLocationType.Accounting,
                    PlaceLocationType.Administrative_Area_Level_1
                }
            };

            var uri = request.GetUri();

            Assert.IsNotNull(uri);
            Assert.AreEqual($"/maps/api/geocode/json?key={request.Key}&language={request.Language.ToCode()}&latlng={Uri.EscapeDataString(request.Location.ToString())}&result_type={Uri.EscapeDataString(string.Join("|", request.ResultTypes.Select(x => x.ToString().ToLower()).AsEnumerable()))}", uri.PathAndQuery);
        }
        public void GetQueryStringParametersWhenChannelTest()
        {
            var request = new LocationGeocodeRequest
            {
                Key      = "key",
                Location = new Coordinate(1, 1),
                Channel  = "channel"
            };

            var queryStringParameters = request.GetQueryStringParameters();

            Assert.IsNotNull(queryStringParameters);

            var channel         = queryStringParameters.FirstOrDefault(x => x.Key == "channel");
            var channelExpected = request.Channel;

            Assert.IsNotNull(channel);
            Assert.AreEqual(channelExpected, channel.Value);
        }
Exemplo n.º 22
0
        public void LocationGeocodeWhenLoncationTypesTest()
        {
            var request = new LocationGeocodeRequest
            {
                Key           = this.ApiKey,
                Location      = new Entities.Common.Location(40.7141289, -73.9614074),
                LocationTypes = new List <GeometryLocationType> {
                    GeometryLocationType.Rooftop
                }
            };
            var response = GoogleMaps.LocationGeocode.Query(request);
            var result   = response.Results.FirstOrDefault();

            Assert.IsNotNull(response);
            Assert.AreEqual(Status.Ok, response.Status);

            Assert.IsNotNull(result);
            Assert.AreEqual("285 Bedford Ave, Brooklyn, NY 11211, USA", result.FormattedAddress);
            Assert.AreEqual(GeometryLocationType.Rooftop, result.Geometry.LocationType);
        }
Exemplo n.º 23
0
        public void LocationGeocodeWhenAsyncAndTimeoutTest()
        {
            var request = new LocationGeocodeRequest
            {
                Location = new Entities.Common.Location(40.7141289, -73.9614074)
            };
            var exception = Assert.Throws <AggregateException>(() =>
            {
                var result = GoogleMaps.LocationGeocode.QueryAsync(request, TimeSpan.FromMilliseconds(1)).Result;
                Assert.IsNull(result);
            });

            Assert.IsNotNull(exception);
            Assert.AreEqual(exception.Message, "One or more errors occurred.");

            var innerException = exception.InnerException;

            Assert.IsNotNull(innerException);
            Assert.AreEqual(innerException.GetType(), typeof(TaskCanceledException));
            Assert.AreEqual(innerException.Message, "A task was canceled.");
        }
        public void GetQueryStringParametersWhenLocationTypeTest()
        {
            var request = new LocationGeocodeRequest
            {
                Key           = "key",
                Location      = new Coordinate(1, 1),
                LocationTypes = new[]
                {
                    GeometryLocationType.Rooftop
                }
            };

            var queryStringParameters = request.GetQueryStringParameters();

            Assert.IsNotNull(queryStringParameters);

            var locationType         = queryStringParameters.SingleOrDefault(x => x.Key == "location_type");
            var locationTypeExpected = string.Join("|", request.LocationTypes.Select(x => x.ToString().ToUpper()));

            Assert.IsNotNull(locationType);
            Assert.AreEqual(locationTypeExpected, locationType.Value);
        }
        public void GetQueryStringParametersWhenResultTypeTest()
        {
            var request = new LocationGeocodeRequest
            {
                Key         = "key",
                Location    = new Coordinate(1, 1),
                ResultTypes = new[]
                {
                    PlaceLocationType.Accounting,
                    PlaceLocationType.Administrative_Area_Level_1
                }
            };

            var queryStringParameters = request.GetQueryStringParameters();

            Assert.IsNotNull(queryStringParameters);

            var resultType         = queryStringParameters.SingleOrDefault(x => x.Key == "result_type");
            var resultTypeExpected = string.Join("|", request.ResultTypes.Select(x => x.ToString().ToLower()));

            Assert.IsNotNull(resultType);
            Assert.AreEqual(resultTypeExpected, resultType.Value);
        }
Exemplo n.º 26
0
        public void ConstructorDefaultTest()
        {
            var request = new LocationGeocodeRequest();

            Assert.IsTrue(request.IsSsl);
        }
        public void ConstructorDefaultTest()
        {
            var request = new LocationGeocodeRequest();

            Assert.AreEqual(Language.English, request.Language);
        }