|
| 1 | +/* |
| 2 | + * Licensed to Elasticsearch under one or more contributor |
| 3 | + * license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright |
| 5 | + * ownership. Elasticsearch licenses this file to you under |
| 6 | + * the Apache License, Version 2.0 (the "License"); you may |
| 7 | + * not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package org.elasticsearch.search.aggregations.metrics.geocentroid; |
| 20 | + |
| 21 | +import org.apache.lucene.geo.GeoEncodingUtils; |
| 22 | +import org.elasticsearch.common.geo.GeoPoint; |
| 23 | +import org.elasticsearch.common.io.stream.Writeable; |
| 24 | +import org.elasticsearch.search.aggregations.InternalAggregationTestCase; |
| 25 | +import org.elasticsearch.search.aggregations.pipeline.PipelineAggregator; |
| 26 | +import org.elasticsearch.test.geo.RandomGeoGenerator; |
| 27 | + |
| 28 | +import java.util.Collections; |
| 29 | +import java.util.List; |
| 30 | +import java.util.Map; |
| 31 | + |
| 32 | +public class InternalGeoCentroidTests extends InternalAggregationTestCase<InternalGeoCentroid> { |
| 33 | + |
| 34 | + @Override |
| 35 | + protected InternalGeoCentroid createTestInstance(String name, List<PipelineAggregator> pipelineAggregators, |
| 36 | + Map<String, Object> metaData) { |
| 37 | + GeoPoint centroid = RandomGeoGenerator.randomPoint(random()); |
| 38 | + |
| 39 | + // Re-encode lat/longs to avoid rounding issue when testing InternalGeoCentroid#hashCode() and |
| 40 | + // InternalGeoCentroid#equals() |
| 41 | + int encodedLon = GeoEncodingUtils.encodeLongitude(centroid.lon()); |
| 42 | + centroid.resetLon(GeoEncodingUtils.decodeLongitude(encodedLon)); |
| 43 | + int encodedLat = GeoEncodingUtils.encodeLatitude(centroid.lat()); |
| 44 | + centroid.resetLat(GeoEncodingUtils.decodeLatitude(encodedLat)); |
| 45 | + |
| 46 | + return new InternalGeoCentroid("_name", centroid, 1, Collections.emptyList(), Collections.emptyMap()); |
| 47 | + } |
| 48 | + |
| 49 | + @Override |
| 50 | + protected Writeable.Reader<InternalGeoCentroid> instanceReader() { |
| 51 | + return InternalGeoCentroid::new; |
| 52 | + } |
| 53 | + |
| 54 | + @Override |
| 55 | + protected void assertReduced(InternalGeoCentroid reduced, List<InternalGeoCentroid> inputs) { |
| 56 | + GeoPoint expected = new GeoPoint(0, 0); |
| 57 | + int i = 0; |
| 58 | + for (InternalGeoCentroid input : inputs) { |
| 59 | + expected.reset(expected.lat() + (input.centroid().lat() - expected.lat()) / (i+1), |
| 60 | + expected.lon() + (input.centroid().lon() - expected.lon()) / (i+1)); |
| 61 | + i++; |
| 62 | + } |
| 63 | + assertEquals(expected.getLat(), reduced.centroid().getLat(), 1E-5D); |
| 64 | + assertEquals(expected.getLon(), reduced.centroid().getLon(), 1E-5D); |
| 65 | + } |
| 66 | +} |
0 commit comments