Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import org.elasticsearch.common.time.FormatNames;
import org.elasticsearch.common.xcontent.XContentHelper;
import org.elasticsearch.core.Tuple;
import org.elasticsearch.datastreams.logsdb.qa.exceptions.MatcherException;
import org.elasticsearch.datastreams.logsdb.qa.matchers.MatchResult;
import org.elasticsearch.datastreams.logsdb.qa.matchers.Matcher;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.rest.RestStatus;
Expand Down Expand Up @@ -125,7 +125,7 @@ protected static void waitForLogs(RestClient client) throws Exception {
}

@SuppressWarnings("unchecked")
public void testMatchAllQuery() throws IOException, MatcherException {
public void testMatchAllQuery() throws IOException {
final List<XContentBuilder> documents = new ArrayList<>();
int numberOfDocuments = ESTestCase.randomIntBetween(100, 200);
for (int i = 0; i < numberOfDocuments; i++) {
Expand All @@ -137,14 +137,15 @@ public void testMatchAllQuery() throws IOException, MatcherException {
final SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder().query(QueryBuilders.matchAllQuery())
.size(numberOfDocuments);

Matcher.mappings(getContenderMappings(), getBaselineMappings())
final MatchResult matchResult = Matcher.mappings(getContenderMappings(), getBaselineMappings())
.settings(getContenderSettings(), getBaselineSettings())
.expected(getQueryHits(queryBaseline(searchSourceBuilder)))
.ignoringSort(true)
.isEqualTo(getQueryHits(queryContender(searchSourceBuilder)));
assertTrue(matchResult.getMessage(), matchResult.isMatch());
}

public void testTermsQuery() throws IOException, MatcherException {
public void testTermsQuery() throws IOException {
final List<XContentBuilder> documents = new ArrayList<>();
int numberOfDocuments = randomIntBetween(100, 200);
for (int i = 0; i < numberOfDocuments; i++) {
Expand All @@ -156,14 +157,15 @@ public void testTermsQuery() throws IOException, MatcherException {
final SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder().query(QueryBuilders.termQuery("method", "put"))
.size(numberOfDocuments);

Matcher.mappings(getContenderMappings(), getBaselineMappings())
final MatchResult matchResult = Matcher.mappings(getContenderMappings(), getBaselineMappings())
.settings(getContenderSettings(), getBaselineSettings())
.expected(getQueryHits(queryBaseline(searchSourceBuilder)))
.ignoringSort(true)
.isEqualTo(getQueryHits(queryContender(searchSourceBuilder)));
assertTrue(matchResult.getMessage(), matchResult.isMatch());
}

public void testHistogramAggregation() throws IOException, MatcherException {
public void testHistogramAggregation() throws IOException {
final List<XContentBuilder> documents = new ArrayList<>();
int numberOfDocuments = randomIntBetween(100, 200);
for (int i = 0; i < numberOfDocuments; i++) {
Expand All @@ -176,14 +178,15 @@ public void testHistogramAggregation() throws IOException, MatcherException {
.size(numberOfDocuments)
.aggregation(new HistogramAggregationBuilder("agg").field("memory_usage_bytes").interval(100.0D));

Matcher.mappings(getContenderMappings(), getBaselineMappings())
final MatchResult matchResult = Matcher.mappings(getContenderMappings(), getBaselineMappings())
.settings(getContenderSettings(), getBaselineSettings())
.expected(getAggregationBuckets(queryBaseline(searchSourceBuilder), "agg"))
.ignoringSort(true)
.isEqualTo(getAggregationBuckets(queryContender(searchSourceBuilder), "agg"));
assertTrue(matchResult.getMessage(), matchResult.isMatch());
}

public void testTermsAggregation() throws IOException, MatcherException {
public void testTermsAggregation() throws IOException {
final List<XContentBuilder> documents = new ArrayList<>();
int numberOfDocuments = randomIntBetween(100, 200);
for (int i = 0; i < numberOfDocuments; i++) {
Expand All @@ -196,14 +199,15 @@ public void testTermsAggregation() throws IOException, MatcherException {
.size(0)
.aggregation(new TermsAggregationBuilder("agg").field("host.name"));

Matcher.mappings(getContenderMappings(), getBaselineMappings())
final MatchResult matchResult = Matcher.mappings(getContenderMappings(), getBaselineMappings())
.settings(getContenderSettings(), getBaselineSettings())
.expected(getAggregationBuckets(queryBaseline(searchSourceBuilder), "agg"))
.ignoringSort(true)
.isEqualTo(getAggregationBuckets(queryContender(searchSourceBuilder), "agg"));
assertTrue(matchResult.getMessage(), matchResult.isMatch());
}

public void testDateHistogramAggregation() throws IOException, MatcherException {
public void testDateHistogramAggregation() throws IOException {
final List<XContentBuilder> documents = new ArrayList<>();
int numberOfDocuments = randomIntBetween(100, 200);
for (int i = 0; i < numberOfDocuments; i++) {
Expand All @@ -216,11 +220,12 @@ public void testDateHistogramAggregation() throws IOException, MatcherException
.aggregation(AggregationBuilders.dateHistogram("agg").field("@timestamp").calendarInterval(DateHistogramInterval.SECOND))
.size(0);

Matcher.mappings(getContenderMappings(), getBaselineMappings())
final MatchResult matchResult = Matcher.mappings(getContenderMappings(), getBaselineMappings())
.settings(getContenderSettings(), getBaselineSettings())
.expected(getAggregationBuckets(queryBaseline(searchSourceBuilder), "agg"))
.ignoringSort(true)
.isEqualTo(getAggregationBuckets(queryContender(searchSourceBuilder), "agg"));
assertTrue(matchResult.getMessage(), matchResult.isMatch());
}

private static XContentBuilder generateDocument(final Instant timestamp) throws IOException {
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

package org.elasticsearch.datastreams.logsdb.qa.matchers;

import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.xcontent.XContentBuilder;

import java.util.Arrays;
import java.util.List;

class ArrayEqualMatcher extends EqualMatcher<Object[]> {
ArrayEqualMatcher(
final XContentBuilder actualMappings,
final Settings.Builder actualSettings,
final XContentBuilder expectedMappings,
final Settings.Builder expectedSettings,
final Object[] actual,
final Object[] expected,
boolean ignoringSort
) {
super(actualMappings, actualSettings, expectedMappings, expectedSettings, actual, expected, ignoringSort);
}

@Override
public MatchResult match() {
return matchArraysEqual(actual, expected, ignoringSort);
}

private MatchResult matchArraysEqual(final Object[] actualArray, final Object[] expectedArray, boolean ignoreSorting) {
if (actualArray.length != expectedArray.length) {
return MatchResult.noMatch(
formatErrorMessage(actualMappings, actualSettings, expectedMappings, expectedSettings, "Array lengths do no match")
);
}
if (ignoreSorting) {
return matchArraysEqualIgnoringSorting(actualArray, expectedArray)
? MatchResult.match()
: MatchResult.noMatch(
formatErrorMessage(
actualMappings,
actualSettings,
expectedMappings,
expectedSettings,
"Arrays do not match when ignoreing sort order"
)
);
} else {
return matchArraysEqualExact(actualArray, expectedArray)
? MatchResult.match()
: MatchResult.noMatch(
formatErrorMessage(actualMappings, actualSettings, expectedMappings, expectedSettings, "Arrays do not match exactly")
);
}
}

private static boolean matchArraysEqualIgnoringSorting(final Object[] actualArray, final Object[] expectedArray) {
final List<Object> actualList = Arrays.asList(actualArray);
final List<Object> expectedList = Arrays.asList(expectedArray);
return actualList.containsAll(expectedList) && expectedList.containsAll(actualList);
}

private static <T> boolean matchArraysEqualExact(T[] actualArray, T[] expectedArray) {
for (int i = 0; i < actualArray.length; i++) {
boolean isEqual = actualArray[i].equals(expectedArray[i]);
if (isEqual == false) {
return false;
}
}
return true;
}
}
Loading