Skip to content

Commit fec7860

Browse files
author
Christoph Büscher
authored
[Tests] Fix edge case in ScriptedMetricAggregatorTests (#31357)
An expected exception is only thrown when there are documents in the index created in the test setup. Fixed the test by making sure there is at least one. Closes #31307
1 parent 8453ca6 commit fec7860

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

server/src/test/java/org/elasticsearch/search/aggregations/metrics/scripted/ScriptedMetricAggregatorTests.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ public static void initMockScripts() {
118118
SCRIPTS.put("initScriptParams", params -> {
119119
Map<String, Object> agg = (Map<String, Object>) params.get("_agg");
120120
Integer initialValue = (Integer)params.get("initialValue");
121-
ArrayList<Integer> collector = new ArrayList();
121+
ArrayList<Integer> collector = new ArrayList<>();
122122
collector.add(initialValue);
123123
agg.put("collector", collector);
124124
return agg;
@@ -175,7 +175,6 @@ public void testNoDocs() throws IOException {
175175
/**
176176
* without combine script, the "_aggs" map should contain a list of the size of the number of documents matched
177177
*/
178-
@SuppressWarnings("unchecked")
179178
public void testScriptedMetricWithoutCombine() throws IOException {
180179
try (Directory directory = newDirectory()) {
181180
int numDocs = randomInt(100);
@@ -190,8 +189,11 @@ public void testScriptedMetricWithoutCombine() throws IOException {
190189
ScriptedMetric scriptedMetric = search(newSearcher(indexReader, true, true), new MatchAllDocsQuery(), aggregationBuilder);
191190
assertEquals(AGG_NAME, scriptedMetric.getName());
192191
assertNotNull(scriptedMetric.aggregation());
192+
@SuppressWarnings("unchecked")
193193
Map<String, Object> agg = (Map<String, Object>) scriptedMetric.aggregation();
194-
assertEquals(numDocs, ((List<Integer>) agg.get("collector")).size());
194+
@SuppressWarnings("unchecked")
195+
List<Integer> list = (List<Integer>) agg.get("collector");
196+
assertEquals(numDocs, list.size());
195197
}
196198
}
197199
}
@@ -300,10 +302,9 @@ public void testSelfReferencingAggStateAfterInit() throws IOException {
300302
}
301303
}
302304

303-
@AwaitsFix(bugUrl = "https://github.com/elastic/elasticsearch/issues/31307")
304305
public void testSelfReferencingAggStateAfterMap() throws IOException {
305306
try (Directory directory = newDirectory()) {
306-
Integer numDocs = randomInt(100);
307+
Integer numDocs = randomIntBetween(1, 100);
307308
try (RandomIndexWriter indexWriter = new RandomIndexWriter(random(), directory)) {
308309
for (int i = 0; i < numDocs; i++) {
309310
indexWriter.addDocument(singleton(new SortedNumericDocValuesField("number", i)));

0 commit comments

Comments
 (0)