3939import static org .elasticsearch .test .MapMatcher .assertMap ;
4040import static org .elasticsearch .test .MapMatcher .matchesMap ;
4141import static org .elasticsearch .xpack .esql .ccq .Clusters .REMOTE_CLUSTER_NAME ;
42- import static org .hamcrest .Matchers .any ;
43- import static org .hamcrest .Matchers .equalTo ;
44- import static org .hamcrest .Matchers .greaterThanOrEqualTo ;
45- import static org .hamcrest .Matchers .hasItems ;
46- import static org .hamcrest .Matchers .hasKey ;
42+ import static org .hamcrest .Matchers .*;
4743
4844@ ThreadLeakFilters (filters = TestClustersThreadFilter .class )
4945public class MultiClustersIT extends ESRestTestCase {
@@ -160,42 +156,33 @@ private Map<String, Object> runEsql(RestEsqlTestCase.RequestObjectBuilder reques
160156 }
161157 }
162158
159+ private <C , V > void assertResultMap (boolean includeCCSMetadata , Map <String , Object > result , C columns , V values , boolean remoteOnly ) {
160+ MapMatcher mapMatcher = getResultMatcher (ccsMetadataAvailable (), result .containsKey ("is_partial" ));
161+ if (includeCCSMetadata ) {
162+ mapMatcher = mapMatcher .entry ("_clusters" , any (Map .class ));
163+ }
164+ assertMap (result , mapMatcher .entry ("columns" , columns ).entry ("values" , values ));
165+ if (includeCCSMetadata ) {
166+ assertClusterDetailsMap (result , remoteOnly );
167+ }
168+ }
169+
163170 public void testCount () throws Exception {
164171 {
165172 boolean includeCCSMetadata = includeCCSMetadata ();
166173 Map <String , Object > result = run ("FROM test-local-index,*:test-remote-index | STATS c = COUNT(*)" , includeCCSMetadata );
167174 var columns = List .of (Map .of ("name" , "c" , "type" , "long" ));
168175 var values = List .of (List .of (localDocs .size () + remoteDocs .size ()));
169176
170- MapMatcher mapMatcher = matchesMap ();
171- if (includeCCSMetadata ) {
172- mapMatcher = mapMatcher .entry ("_clusters" , any (Map .class ));
173- }
174- if (ccsMetadataAvailable ()) {
175- mapMatcher = mapMatcher .entry ("took" , greaterThanOrEqualTo (0 ));
176- }
177- assertMap (result , mapMatcher .entry ("columns" , columns ).entry ("values" , values ));
178- if (includeCCSMetadata ) {
179- assertClusterDetailsMap (result , false );
180- }
177+ assertResultMap (includeCCSMetadata , result , columns , values , false );
181178 }
182179 {
183180 boolean includeCCSMetadata = includeCCSMetadata ();
184181 Map <String , Object > result = run ("FROM *:test-remote-index | STATS c = COUNT(*)" , includeCCSMetadata );
185182 var columns = List .of (Map .of ("name" , "c" , "type" , "long" ));
186183 var values = List .of (List .of (remoteDocs .size ()));
187184
188- MapMatcher mapMatcher = matchesMap ();
189- if (includeCCSMetadata ) {
190- mapMatcher = mapMatcher .entry ("_clusters" , any (Map .class ));
191- }
192- if (ccsMetadataAvailable ()) {
193- mapMatcher = mapMatcher .entry ("took" , greaterThanOrEqualTo (0 ));
194- }
195- assertMap (result , mapMatcher .entry ("columns" , columns ).entry ("values" , values ));
196- if (includeCCSMetadata ) {
197- assertClusterDetailsMap (result , true );
198- }
185+ assertResultMap (includeCCSMetadata , result , columns , values , true );
199186 }
200187 }
201188
@@ -208,17 +195,7 @@ public void testUngroupedAggs() throws Exception {
208195 var values = List .of (List .of (Math .toIntExact (sum )));
209196
210197 // check all sections of map except _cluster/details
211- MapMatcher mapMatcher = matchesMap ();
212- if (includeCCSMetadata ) {
213- mapMatcher = mapMatcher .entry ("_clusters" , any (Map .class ));
214- }
215- if (ccsMetadataAvailable ()) {
216- mapMatcher = mapMatcher .entry ("took" , greaterThanOrEqualTo (0 ));
217- }
218- assertMap (result , mapMatcher .entry ("columns" , columns ).entry ("values" , values ));
219- if (includeCCSMetadata ) {
220- assertClusterDetailsMap (result , false );
221- }
198+ assertResultMap (includeCCSMetadata , result , columns , values , false );
222199 }
223200 {
224201 boolean includeCCSMetadata = includeCCSMetadata ();
@@ -227,17 +204,7 @@ public void testUngroupedAggs() throws Exception {
227204 long sum = remoteDocs .stream ().mapToLong (d -> d .data ).sum ();
228205 var values = List .of (List .of (Math .toIntExact (sum )));
229206
230- MapMatcher mapMatcher = matchesMap ();
231- if (includeCCSMetadata ) {
232- mapMatcher = mapMatcher .entry ("_clusters" , any (Map .class ));
233- }
234- if (ccsMetadataAvailable ()) {
235- mapMatcher = mapMatcher .entry ("took" , greaterThanOrEqualTo (0 ));
236- }
237- assertMap (result , mapMatcher .entry ("columns" , columns ).entry ("values" , values ));
238- if (includeCCSMetadata ) {
239- assertClusterDetailsMap (result , true );
240- }
207+ assertResultMap (includeCCSMetadata , result , columns , values , true );
241208 }
242209 {
243210 assumeTrue ("requires ccs metadata" , ccsMetadataAvailable ());
@@ -246,23 +213,15 @@ public void testUngroupedAggs() throws Exception {
246213 long sum = remoteDocs .stream ().mapToLong (d -> d .data ).sum ();
247214 var values = List .of (List .of (Math .toIntExact (sum )));
248215
249- MapMatcher mapMatcher = matchesMap ();
250- assertMap (
251- result ,
252- mapMatcher .entry ("columns" , columns )
253- .entry ("values" , values )
254- .entry ("took" , greaterThanOrEqualTo (0 ))
255- .entry ("_clusters" , any (Map .class ))
256- );
257- assertClusterDetailsMap (result , true );
216+ assertResultMap (true , result , columns , values , true );
258217 }
259218 }
260219
261220 private void assertClusterDetailsMap (Map <String , Object > result , boolean remoteOnly ) {
262221 @ SuppressWarnings ("unchecked" )
263222 Map <String , Object > clusters = (Map <String , Object >) result .get ("_clusters" );
264- assertThat (clusters .size (), greaterThanOrEqualTo (7 ));
265- assertThat (clusters .keySet (), hasItems ( "total" , "successful" , "running" , "skipped" , "partial" , "failed" , "details" ));
223+ assertThat (clusters .size (), equalTo (7 ));
224+ assertThat (clusters .keySet (), equalTo ( Set . of ( "total" , "successful" , "running" , "skipped" , "partial" , "failed" , "details" ) ));
266225 int expectedNumClusters = remoteOnly ? 1 : 2 ;
267226 Set <String > expectedClusterAliases = remoteOnly ? Set .of ("remote_cluster" ) : Set .of ("remote_cluster" , "(local)" );
268227
@@ -272,10 +231,6 @@ private void assertClusterDetailsMap(Map<String, Object> result, boolean remoteO
272231 assertThat (clusters .get ("skipped" ), equalTo (0 ));
273232 assertThat (clusters .get ("partial" ), equalTo (0 ));
274233 assertThat (clusters .get ("failed" ), equalTo (0 ));
275- if (result .containsKey ("is_partial" )) {
276- // for some BWC tests, the is_partial key may not be present
277- assertThat (result .get ("is_partial" ), equalTo (false ));
278- }
279234
280235 @ SuppressWarnings ("unchecked" )
281236 Map <String , Object > details = (Map <String , Object >) clusters .get ("details" );
@@ -330,17 +285,7 @@ public void testGroupedAggs() throws Exception {
330285 .map (e -> List .of (Math .toIntExact (e .getValue ()), e .getKey ()))
331286 .toList ();
332287
333- MapMatcher mapMatcher = matchesMap ();
334- if (includeCCSMetadata ) {
335- mapMatcher = mapMatcher .entry ("_clusters" , any (Map .class ));
336- }
337- if (ccsMetadataAvailable ()) {
338- mapMatcher = mapMatcher .entry ("took" , greaterThanOrEqualTo (0 ));
339- }
340- assertMap (result , mapMatcher .entry ("columns" , columns ).entry ("values" , values ));
341- if (includeCCSMetadata ) {
342- assertClusterDetailsMap (result , false );
343- }
288+ assertResultMap (includeCCSMetadata , result , columns , values , false );
344289 }
345290 {
346291 boolean includeCCSMetadata = includeCCSMetadata ();
@@ -358,17 +303,7 @@ public void testGroupedAggs() throws Exception {
358303 .toList ();
359304
360305 // check all sections of map except _clusters/details
361- MapMatcher mapMatcher = matchesMap ();
362- if (includeCCSMetadata ) {
363- mapMatcher = mapMatcher .entry ("_clusters" , any (Map .class ));
364- }
365- if (ccsMetadataAvailable ()) {
366- mapMatcher = mapMatcher .entry ("took" , greaterThanOrEqualTo (0 ));
367- }
368- assertMap (result , mapMatcher .entry ("columns" , columns ).entry ("values" , values ));
369- if (includeCCSMetadata ) {
370- assertClusterDetailsMap (result , true );
371- }
306+ assertResultMap (includeCCSMetadata , result , columns , values , true );
372307 }
373308 }
374309
@@ -383,23 +318,16 @@ public void testIndexPattern() throws Exception {
383318 Map <String , Object > result = run ("FROM " + indexPattern + " | STATS c = COUNT(*)" , false );
384319 var columns = List .of (Map .of ("name" , "c" , "type" , "long" ));
385320 var values = List .of (List .of (localDocs .size () + remoteDocs .size ()));
386- MapMatcher mapMatcher = matchesMap ();
387- if (ccsMetadataAvailable ()) {
388- mapMatcher = mapMatcher .entry ("took" , greaterThanOrEqualTo (0 ));
389- }
390- assertMap (result , mapMatcher .entry ("columns" , columns ).entry ("values" , values ));
321+
322+ assertResultMap (false , result , columns , values , false );
391323 }
392324 {
393325 String indexPattern = randomFrom ("*:test-remote-index" , "*:test-remote-*" , "*:test-*" );
394326 Map <String , Object > result = run ("FROM " + indexPattern + " | STATS c = COUNT(*)" , false );
395327 var columns = List .of (Map .of ("name" , "c" , "type" , "long" ));
396328 var values = List .of (List .of (remoteDocs .size ()));
397329
398- MapMatcher mapMatcher = matchesMap ();
399- if (ccsMetadataAvailable ()) {
400- mapMatcher = mapMatcher .entry ("took" , greaterThanOrEqualTo (0 ));
401- }
402- assertMap (result , mapMatcher .entry ("columns" , columns ).entry ("values" , values ));
330+ assertResultMap (false , result , columns , values , false );
403331 }
404332 }
405333
0 commit comments