public async Task QueryObjectIdsAreHonored()
        {
            var gateway = new ArcGISGateway();

            var queryPoint = new Query(@"Earthquakes/EarthquakesFromLastSevenDays/MapServer/0".AsEndpoint())
            {
                ReturnGeometry = false
            };
            var resultPoint = await IntegrationTestFixture.TestPolicy.ExecuteAsync(() =>
            {
                return(gateway.Query <Point>(queryPoint));
            });

            Assert.True(resultPoint.Features.Any());
            Assert.True(resultPoint.Features.All(i => i.Geometry == null));

            var queryPointByOID = new Query(@"Earthquakes/EarthquakesFromLastSevenDays/MapServer/0".AsEndpoint())
            {
                ReturnGeometry = false,
                ObjectIds      = resultPoint.Features.Take(10).Select(f => long.Parse(f.Attributes["objectid"].ToString())).ToList()
            };
            var resultPointByOID = await IntegrationTestFixture.TestPolicy.ExecuteAsync(() =>
            {
                return(gateway.Query <Point>(queryPointByOID));
            });

            Assert.True(resultPointByOID.Features.Any());
            Assert.True(resultPointByOID.Features.All(i => i.Geometry == null));
            Assert.True(resultPoint.Features.Count() > 10);
            Assert.True(resultPointByOID.Features.Count() == 10);
            Assert.False(queryPointByOID.ObjectIds.Except(resultPointByOID.Features.Select(f => f.ObjectID)).Any());
        }
        public async Task QueryOutFieldsAreHonored()
        {
            var gateway = new ArcGISGateway();

            var queryPolyline = new Query(@"Hydrography/Watershed173811/MapServer/1".AsEndpoint())
            {
                OutFields = new List <string> {
                    "lengthkm"
                }, ReturnGeometry = false
            };
            var resultPolyline = await IntegrationTestFixture.TestPolicy.ExecuteAsync(() =>
            {
                return(gateway.Query <Polyline>(queryPolyline));
            });

            Assert.True(resultPolyline.Features.Any());
            Assert.True(resultPolyline.Features.All(i => i.Geometry == null));
            Assert.True(resultPolyline.Features.All(i => i.Attributes != null && i.Attributes.Count == 1));

            var queryPolygon = new Query(@"/Hydrography/Watershed173811/MapServer/0".AsEndpoint())
            {
                Where     = "areasqkm = 0.012",
                OutFields = new List <string> {
                    "areasqkm", "elevation", "resolution", "reachcode"
                }
            };
            var resultPolygon = await IntegrationTestFixture.TestPolicy.ExecuteAsync(() =>
            {
                return(gateway.Query <Polygon>(queryPolygon));
            });

            Assert.True(resultPolygon.Features.Any());
            Assert.True(resultPolygon.Features.All(i => i.Geometry != null));
            Assert.True(resultPolygon.Features.All(i => i.Attributes != null && i.Attributes.Count == 4));
        }
        public async Task QueryCanReturnNoGeometry()
        {
            var gateway = new ArcGISGateway();

            var queryPoint = new Query(@"Earthquakes/EarthquakesFromLastSevenDays/MapServer/0".AsEndpoint())
            {
                ReturnGeometry = false
            };
            var resultPoint = await IntegrationTestFixture.TestPolicy.ExecuteAsync(() =>
            {
                return(gateway.Query <Point>(queryPoint));
            });

            Assert.True(resultPoint.Features.Any());
            Assert.True(resultPoint.Features.All(i => i.Geometry == null));

            var queryPolyline = new Query(@"Hydrography/Watershed173811/MapServer/1".AsEndpoint())
            {
                OutFields = new List <string> {
                    "lengthkm"
                }, ReturnGeometry = false
            };
            var resultPolyline = await IntegrationTestFixture.TestPolicy.ExecuteAsync(() =>
            {
                return(gateway.QueryAsPost <Polyline>(queryPolyline));
            });

            Assert.True(resultPolyline.Features.Any());
            Assert.True(resultPolyline.Features.All(i => i.Geometry == null));
        }
        public async Task GatewayDoesAutoPost()
        {
            var gateway = new ArcGISGateway()
            {
                IncludeHypermediaWithResponse = true
            };

            var longWhere = new StringBuilder("region = '");

            for (var i = 0; i < 3000; i++)
            {
                longWhere.Append(i);
            }

            var query = new Query(@"/Earthquakes/EarthquakesFromLastSevenDays/MapServer/0".AsEndpoint())
            {
                Where = longWhere + "'"
            };

            var result = await IntegrationTestFixture.TestPolicy.ExecuteAsync(() =>
            {
                return(gateway.Query <Point>(query));
            });

            Assert.NotNull(result);
            Assert.Null(result.Error);
            Assert.NotNull(result.SpatialReference);
            Assert.False(result.Features.Any());
            Assert.NotNull(result.Links);
            Assert.Equal("POST", result.Links.First().Method);
        }
        public async Task QueryCanReturnDifferentGeometryTypes()
        {
            var gateway = new ArcGISGateway();

            var queryPoint = new Query(@"Earthquakes/EarthquakesFromLastSevenDays/MapServer/0".AsEndpoint())
            {
                Where = "magnitude > 4.5"
            };
            var resultPoint = await IntegrationTestFixture.TestPolicy.ExecuteAsync(() =>
            {
                return(gateway.QueryAsPost <Point>(queryPoint));
            });

            Assert.True(resultPoint.Features.Any());
            Assert.True(resultPoint.Features.All(i => i.Geometry != null));

            var queryPolyline = new Query(@"Hydrography/Watershed173811/MapServer/1".AsEndpoint())
            {
                OutFields = new List <string> {
                    "lengthkm"
                }
            };
            var resultPolyline = await IntegrationTestFixture.TestPolicy.ExecuteAsync(() =>
            {
                return(gateway.Query <Polyline>(queryPolyline));
            });

            Assert.True(resultPolyline.Features.Any());
            Assert.True(resultPolyline.Features.All(i => i.Geometry != null));

            gateway = new ArcGISGateway(new JsonDotNetSerializer());

            var queryPolygon = new Query(@"/Hydrography/Watershed173811/MapServer/0".AsEndpoint())
            {
                Where = "areasqkm = 0.012", OutFields = new List <string> {
                    "areasqkm"
                }
            };
            var resultPolygon = await IntegrationTestFixture.TestPolicy.ExecuteAsync(() =>
            {
                return(gateway.QueryAsPost <Polygon>(queryPolygon));
            });

            Assert.True(resultPolygon.Features.Any());
            Assert.True(resultPolygon.Features.All(i => i.Geometry != null));
        }
        public async Task QueryCanUseWhereClause()
        {
            var gateway = new ArcGISGateway();

            var queryPoint = new Query(@"Earthquakes/Since_1970/MapServer/0".AsEndpoint())
            {
                ReturnGeometry = false,
                Where          = "UPPER(Name) LIKE UPPER('New Zea%')"
            };

            queryPoint.OutFields.Add("Name");
            var resultPoint = await IntegrationTestFixture.TestPolicy.ExecuteAsync(() =>
            {
                return(gateway.Query <Point>(queryPoint));
            });

            Assert.True(resultPoint.Features.Any());
            Assert.True(resultPoint.Features.All(i => i.Geometry == null));
            Assert.True(resultPoint.Features.All(i => i.Attributes["Name"].ToString().StartsWithIgnoreCase("New Zea")));
        }
        public async Task GatewayDoesAutoPostForMaximumGetRequestLength()
        {
            var gateway = new ArcGISGateway()
            {
                IncludeHypermediaWithResponse = true, MaximumGetRequestLength = 1
            };

            var query = new Query(@"/Earthquakes/EarthquakesFromLastSevenDays/MapServer/0".AsEndpoint());

            var result = await IntegrationTestFixture.TestPolicy.ExecuteAsync(() =>
            {
                return(gateway.Query <Point>(query));
            });

            Assert.NotNull(result);
            Assert.Null(result.Error);
            Assert.NotNull(result.SpatialReference);
            Assert.True(result.Features.Any());
            Assert.NotNull(result.Links);
            Assert.Equal("POST", result.Links.First().Method);
        }
        /// <summary>
        /// Performs unfiltered query, then filters by Extent and Polygon to SE quadrant of globe and verifies both filtered
        /// results contain same number of features as each other, and that both filtered resultsets contain fewer features than unfiltered resultset.
        /// </summary>
        /// <param name="serviceUrl"></param>
        /// <returns></returns>
        public async Task QueryGeometryCriteriaHonored(string serviceUrl)
        {
            int countAllResults     = 0;
            int countExtentResults  = 0;
            int countPolygonResults = 0;

            var gateway = new ArcGISGateway(new JsonDotNetSerializer());

            var queryPointAllResults = new Query(serviceUrl.AsEndpoint());

            var resultPointAllResults = await IntegrationTestFixture.TestPolicy.ExecuteAsync(() =>
            {
                return(gateway.Query <Point>(queryPointAllResults));
            });

            var queryPointExtentResults = new Query(serviceUrl.AsEndpoint())
            {
                Geometry = new Extent {
                    XMin = 0, YMin = 0, XMax = 180, YMax = -90, SpatialReference = SpatialReference.WGS84
                },                                                                                                               // SE quarter of globe
                OutputSpatialReference = SpatialReference.WebMercator
            };
            var resultPointExtentResults = await IntegrationTestFixture.TestPolicy.ExecuteAsync(() =>
            {
                return(gateway.Query <Point>(queryPointExtentResults));
            });

            var rings = new Point[]
            {
                new Point {
                    X = 0, Y = 0
                },
                new Point {
                    X = 180, Y = 0
                },
                new Point {
                    X = 180, Y = -90
                },
                new Point {
                    X = 0, Y = -90
                },
                new Point {
                    X = 0, Y = 0
                }
            }.ToPointCollectionList();

            var queryPointPolygonResults = new Query(serviceUrl.AsEndpoint())
            {
                Geometry = new Polygon {
                    Rings = rings
                }
            };
            var resultPointPolygonResults = await IntegrationTestFixture.TestPolicy.ExecuteAsync(() =>
            {
                return(gateway.Query <Point>(queryPointPolygonResults));
            });

            countAllResults     = resultPointAllResults.Features.Count();
            countExtentResults  = resultPointExtentResults.Features.Count();
            countPolygonResults = resultPointPolygonResults.Features.Count();

            Assert.Equal(resultPointExtentResults.SpatialReference.Wkid, queryPointExtentResults.OutputSpatialReference.Wkid);
            Assert.True(countAllResults > countExtentResults);
            Assert.True(countPolygonResults == countExtentResults);
        }