Ejemplo n.º 1
0
        public void SnapToRoadWhenAsyncTest()
        {
            var request = new SnapToRoadsRequest
            {
                Key  = this.ApiKey,
                Path = new[]
                {
                    new Location(60.170880, 24.942795),
                    new Location(60.170879, 24.942796),
                    new Location(60.170877, 24.942796)
                }
            };
            var result = GoogleMaps.SnapToRoad.QueryAsync(request).Result;

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

            var snappedPoints = result.SnappedPoints?.ToArray();

            Assert.IsNotNull(snappedPoints);
            Assert.AreEqual(3, snappedPoints.Length);

            Assert.AreEqual(0, snappedPoints[0].OriginalIndex);
            Assert.AreEqual(60.170877918672588, snappedPoints[0].Location.Latitude, 0.0001);
            Assert.AreEqual(24.942699821922421, snappedPoints[0].Location.Longitude, 0.0001);

            Assert.AreEqual(1, snappedPoints[1].OriginalIndex);
            Assert.AreEqual(60.170876898776406, snappedPoints[1].Location.Latitude, 0.0001);
            Assert.AreEqual(24.942699912064771, snappedPoints[1].Location.Longitude, 0.0001);

            Assert.AreEqual(2, snappedPoints[2].OriginalIndex);
            Assert.AreEqual(60.170874902634374, snappedPoints[2].Location.Latitude, 0.0001);
            Assert.AreEqual(24.942700088491474, snappedPoints[2].Location.Longitude, 0.0001);
        }
        public void GetQueryStringParametersTest()
        {
            var request = new SnapToRoadsRequest
            {
                Key  = "key",
                Path = new[]
                {
                    new Coordinate(1, 1),
                    new Coordinate(2, 2)
                }
            };

            var queryStringParameters = request.GetQueryStringParameters();

            Assert.IsNotNull(queryStringParameters);

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

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

            var points         = queryStringParameters.FirstOrDefault(x => x.Key == "path");
            var pointsExpected = string.Join("|", request.Path);

            Assert.IsNotNull(points);
            Assert.AreEqual(pointsExpected, points.Value);

            var interpolate         = queryStringParameters.FirstOrDefault(x => x.Key == "interpolate");
            var interpolateExpected = request.Interpolate.ToString().ToLower();

            Assert.IsNotNull(interpolate);
            Assert.AreEqual(interpolateExpected, interpolate.Value);
        }
Ejemplo n.º 3
0
        public void ConstructorDefaultTest()
        {
            var request = new SnapToRoadsRequest();

            Assert.IsTrue(request.IsSsl);
            Assert.IsFalse(request.Interpolate);
        }
Ejemplo n.º 4
0
        public void SnapToRoadWhenPathIsNullTest()
        {
            var request = new SnapToRoadsRequest
            {
                Key = this.ApiKey
            };

            var exception = Assert.Throws <ArgumentException>(() => GoogleMaps.SnapToRoad.Query(request));

            Assert.IsNotNull(exception);
            Assert.AreEqual(exception.Message, "Path is required");
        }
Ejemplo n.º 5
0
        public void SnapToRoadAsyncTest()
        {
            var request = new SnapToRoadsRequest
            {
                Key  = this.ApiKey,
                Path = new[] { new Entities.Maps.Roads.Common.Location(0, 0) }
            };
            var result = GoogleMaps.SnapToRoad.QueryAsync(request).Result;

            Assert.IsNotNull(result);
            Assert.AreEqual(Status.Ok, result.Status);
        }
Ejemplo n.º 6
0
        public void SnapToRoadWhenKeyIsStringEmptyTest()
        {
            var request = new SnapToRoadsRequest
            {
                Key  = string.Empty,
                Path = new[] { new Location(0, 0) }
            };

            var exception = Assert.Throws <ArgumentException>(() => GoogleMaps.SnapToRoad.Query(request));

            Assert.IsNotNull(exception);
            Assert.AreEqual(exception.Message, "Key is required");
        }
Ejemplo n.º 7
0
        public void SnapToRoadWhenPathIsEmptyTest()
        {
            var request = new SnapToRoadsRequest
            {
                Key  = this.ApiKey,
                Path = new Entities.Maps.Roads.Common.Location[0]
            };

            var exception = Assert.Throws <ArgumentException>(() => GoogleMaps.SnapToRoad.Query(request));

            Assert.IsNotNull(exception);
            Assert.AreEqual(exception.Message, "Path is required.");
        }
Ejemplo n.º 8
0
        public void SnapToRoadWhenKeyIsNullTest()
        {
            var request = new SnapToRoadsRequest
            {
                Key  = null,
                Path = new[] { new Entities.Maps.Roads.Common.Location(0, 0) }
            };

            var exception = Assert.Throws <ArgumentException>(() => GoogleMaps.SnapToRoad.Query(request));

            Assert.IsNotNull(exception);
            Assert.AreEqual(exception.Message, "Key is required.");
        }
Ejemplo n.º 9
0
        public void GetQueryStringParametersTest()
        {
            var request = new SnapToRoadsRequest
            {
                Key  = "abc",
                Path = new[]
                {
                    new Location(1, 1),
                    new Location(2, 2)
                }
            };

            Assert.DoesNotThrow(() => request.GetQueryStringParameters());
        }
Ejemplo n.º 10
0
        public void GetQueryStringParametersWhenKeyIsStringEmptyTest()
        {
            var request = new SnapToRoadsRequest
            {
                Key = string.Empty
            };

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

            Assert.IsNotNull(exception);
            Assert.AreEqual(exception.Message, "'Key' is required");
        }
Ejemplo n.º 11
0
        public void GetQueryStringParametersWhenPathIsNullTest()
        {
            var request = new SnapToRoadsRequest
            {
                Key = this.ApiKey
            };

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

            Assert.IsNotNull(exception);
            Assert.AreEqual(exception.Message, "Path is required");
        }
Ejemplo n.º 12
0
        public void GetUriTest()
        {
            var request = new SnapToRoadsRequest
            {
                Key  = "abc",
                Path = new[]
                {
                    new Location(1, 1),
                    new Location(2, 2)
                }
            };

            var uri = request.GetUri();

            Assert.IsNotNull(uri);
            Assert.AreEqual($"/v1/snapToRoads?key={request.Key}&path={Uri.EscapeDataString(string.Join("|", request.Path))}&interpolate={request.Interpolate.ToString().ToLower()}", uri.PathAndQuery);
        }
Ejemplo n.º 13
0
        public void SnapToRoadWhenAsyncTest()
        {
            var request = new SnapToRoadsRequest
            {
                Key  = this.ApiKey,
                Path = new[]
                {
                    new Coordinate(60.170880, 24.942795),
                    new Coordinate(60.170879, 24.942796),
                    new Coordinate(60.170877, 24.942796)
                }
            };
            var result = GoogleMaps.SnapToRoad.QueryAsync(request).Result;

            Assert.IsNotNull(result);
            Assert.AreEqual(Status.Ok, result.Status);
        }
Ejemplo n.º 14
0
        public void SnapToRoadWhenPathIsNullTest()
        {
            var request = new SnapToRoadsRequest
            {
                Key = this.ApiKey
            };

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

            Assert.IsNotNull(exception);

            var innerException = exception.InnerException;

            Assert.IsNotNull(innerException);
            Assert.AreEqual(typeof(GoogleApiException), innerException.GetType());
            Assert.AreEqual(innerException.Message, "Path is required");
        }
Ejemplo n.º 15
0
        public void GetQueryStringParametersWhenPathCotaninsMoreThanHundredLocationsTest()
        {
            var request = new SnapToRoadsRequest
            {
                Key  = "abc",
                Path = new Coordinate[101]
            };

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

            Assert.IsNotNull(exception);
            Assert.AreEqual(exception.Message, "'Path' must contain equal or less than 100 coordinates");
        }
Ejemplo n.º 16
0
        public void SnapToRoadWhenAsyncAndCancelledTest()
        {
            var request = new SnapToRoadsRequest
            {
                Key  = this.ApiKey,
                Path = new[] { new Location(0, 0) }
            };
            var cancellationTokenSource = new CancellationTokenSource();
            var task = GoogleMaps.SnapToRoad.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.");
        }
Ejemplo n.º 17
0
        public void GetQueryStringParametersWhenPathCotaninsMoreThan100LocationsTest()
        {
            var request = new SnapToRoadsRequest
            {
                Key  = this.ApiKey,
                Path = new Location[101]
            };

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

            Assert.IsNotNull(exception);
            Assert.AreEqual(exception.Message, "Path must contain less than 100 locations");
        }
Ejemplo n.º 18
0
        public void GetQueryStringParametersWhenKeyIsNullTest()
        {
            var request = new SnapToRoadsRequest
            {
                Key  = null,
                Path = new[] { new Location(0, 0) }
            };

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

            Assert.IsNotNull(exception);
            Assert.AreEqual(exception.Message, "Key is required");
        }
Ejemplo n.º 19
0
        protected override async Task <List <Coordinate>?> ExecuteRequestAsync(
            List <Coordinate> coordinates,
            CancellationToken cancellationToken)
        {
            var request = new SnapToRoadsRequest
            {
                Interpolate = true,
                Key         = APIKey,
                Path        = coordinates.Select(c => new GoogleApi.Entities.Maps.Roads.Common.Coordinate(c.Latitude, c.Longitude))
            };

            SnapToRoadsResponse?result = null;

            try
            {
                result = await GoogleMaps.SnapToRoad.QueryAsync(request, cancellationToken);
            }
            catch (Exception e)
            {
                Logger?.Error <string>("Snap to road request failed. Message was '{0}'", e.Message);
                return(null);
            }

            if (result == null)
            {
                Logger?.Error("Snap to road request failed");
                return(null);
            }

            var errors = result.Errors?.ToList() ?? new List <Error>();

            if (errors.Count <= 0)
            {
                return(result.SnappedPoints
                       .Select(p => new Coordinate(p.Location.Latitude, p.Location.Longitude))
                       .ToList());
            }

            foreach (var error in errors)
            {
                Logger?.Error <string>("Snap to road error: {0}", error.ErrorMessage);
            }

            return(null);
        }
Ejemplo n.º 20
0
        public void SnapToRoadWhenKeyIsStringEmptyTest()
        {
            var request = new SnapToRoadsRequest
            {
                Key  = string.Empty,
                Path = new[] { new Location(0, 0) }
            };

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

            Assert.IsNotNull(exception);

            var innerException = exception.InnerException;

            Assert.IsNotNull(innerException);
            Assert.AreEqual(typeof(GoogleApiException), innerException.GetType());
            Assert.AreEqual(innerException.Message, "Key is required");
        }
Ejemplo n.º 21
0
        public void SnapToRoadWhenPathIsEmptyTest()
        {
            var request = new SnapToRoadsRequest
            {
                Key  = this.ApiKey,
                Path = new Location[0]
            };

            var exception = Assert.Throws <AggregateException>(() => GoogleMaps.SnapToRoad.Query(request));

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

            var innerException = exception.InnerException;

            Assert.IsNotNull(innerException);
            Assert.AreEqual(typeof(GoogleApiException), innerException.GetType());
            Assert.AreEqual(innerException.Message, "Path is required");
        }
Ejemplo n.º 22
0
        public void SnapToRoadWhenAsyncAndTimeoutTest()
        {
            var request = new SnapToRoadsRequest
            {
                Key  = this.ApiKey,
                Path = new[] { new Location(0, 0) }
            };
            var exception = Assert.Throws <AggregateException>(() =>
            {
                var result = GoogleMaps.SnapToRoad.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.");
        }
Ejemplo n.º 23
0
        public void SnapToRoadWhenInvalidKeyTest()
        {
            var request = new SnapToRoadsRequest
            {
                Key  = "test",
                Path = new[]
                {
                    new Location(60.170880, 24.942795),
                    new Location(60.170879, 24.942796),
                    new Location(60.170877, 24.942796)
                }
            };

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

            Assert.IsNotNull(exception);

            var innerException = exception.InnerExceptions.FirstOrDefault();

            Assert.IsNotNull(innerException);
            Assert.AreEqual(typeof(GoogleApiException).ToString(), innerException.GetType().ToString());
            Assert.AreEqual("Response status code does not indicate success: 400 (Bad Request).", innerException.Message);
        }