Skip to content

Commit dabbf5d

Browse files
committed
[TEST] Added unittests for InternalGeoCentroid
Relates to #22278
1 parent 373edee commit dabbf5d

File tree

2 files changed

+87
-0
lines changed

2 files changed

+87
-0
lines changed

core/src/main/java/org/elasticsearch/search/aggregations/metrics/geocentroid/InternalGeoCentroid.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import java.io.IOException;
3131
import java.util.List;
3232
import java.util.Map;
33+
import java.util.Objects;
3334

3435
/**
3536
* Serialization and merge logic for {@link GeoCentroidAggregator}.
@@ -154,4 +155,24 @@ public XContentBuilder doXContentBody(XContentBuilder builder, Params params) th
154155
}
155156
return builder;
156157
}
158+
159+
@Override
160+
public boolean doEquals(Object o) {
161+
InternalGeoCentroid that = (InternalGeoCentroid) o;
162+
return count == that.count &&
163+
Objects.equals(centroid, that.centroid);
164+
}
165+
166+
@Override
167+
protected int doHashCode() {
168+
return Objects.hash(centroid, count);
169+
}
170+
171+
@Override
172+
public String toString() {
173+
return "InternalGeoCentroid{" +
174+
"centroid=" + centroid +
175+
", count=" + count +
176+
'}';
177+
}
157178
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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

Comments
 (0)