diff --git a/modules/data-streams/src/javaRestTest/java/org/elasticsearch/datastreams/logsdb/qa/StandardVersusLogsIndexModeChallengeRestIT.java b/modules/data-streams/src/javaRestTest/java/org/elasticsearch/datastreams/logsdb/qa/StandardVersusLogsIndexModeChallengeRestIT.java index 79e96b7446042..63ce59fb19785 100644 --- a/modules/data-streams/src/javaRestTest/java/org/elasticsearch/datastreams/logsdb/qa/StandardVersusLogsIndexModeChallengeRestIT.java +++ b/modules/data-streams/src/javaRestTest/java/org/elasticsearch/datastreams/logsdb/qa/StandardVersusLogsIndexModeChallengeRestIT.java @@ -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; @@ -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 documents = new ArrayList<>(); int numberOfDocuments = ESTestCase.randomIntBetween(100, 200); for (int i = 0; i < numberOfDocuments; i++) { @@ -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 documents = new ArrayList<>(); int numberOfDocuments = randomIntBetween(100, 200); for (int i = 0; i < numberOfDocuments; i++) { @@ -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 documents = new ArrayList<>(); int numberOfDocuments = randomIntBetween(100, 200); for (int i = 0; i < numberOfDocuments; i++) { @@ -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 documents = new ArrayList<>(); int numberOfDocuments = randomIntBetween(100, 200); for (int i = 0; i < numberOfDocuments; i++) { @@ -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 documents = new ArrayList<>(); int numberOfDocuments = randomIntBetween(100, 200); for (int i = 0; i < numberOfDocuments; i++) { @@ -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 { diff --git a/modules/data-streams/src/javaRestTest/java/org/elasticsearch/datastreams/logsdb/qa/exceptions/MatcherException.java b/modules/data-streams/src/javaRestTest/java/org/elasticsearch/datastreams/logsdb/qa/exceptions/MatcherException.java deleted file mode 100644 index 442e09f67ac1f..0000000000000 --- a/modules/data-streams/src/javaRestTest/java/org/elasticsearch/datastreams/logsdb/qa/exceptions/MatcherException.java +++ /dev/null @@ -1,53 +0,0 @@ -/* - * 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.exceptions; - -import org.elasticsearch.common.Strings; -import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.xcontent.XContentBuilder; - -/** - * Generic base class for all types of mismatch errors. - */ -public class MatcherException extends Exception { - - public MatcherException( - final XContentBuilder actualMappings, - final Settings.Builder actualSettings, - final XContentBuilder expectedMappings, - final Settings.Builder expectedSettings, - final String errorMessage - ) { - super(errorMessage(actualMappings, actualSettings, expectedMappings, expectedSettings, errorMessage)); - } - - private static String errorMessage( - final XContentBuilder actualMappings, - final Settings.Builder actualSettings, - final XContentBuilder expectedMappings, - final Settings.Builder expectedSettings, - final String errorMessage - ) { - return "Error [" - + errorMessage - + "] " - + "actual mappings [" - + Strings.toString(actualMappings) - + "] " - + "actual settings [" - + Strings.toString(actualSettings.build()) - + "] " - + "expected mappings [" - + Strings.toString(expectedMappings) - + "] " - + "expected settings [" - + Strings.toString(expectedSettings.build()) - + "] "; - } -} diff --git a/modules/data-streams/src/javaRestTest/java/org/elasticsearch/datastreams/logsdb/qa/exceptions/MismatchTypeMatcherException.java b/modules/data-streams/src/javaRestTest/java/org/elasticsearch/datastreams/logsdb/qa/exceptions/MismatchTypeMatcherException.java deleted file mode 100644 index d0c9b5f544500..0000000000000 --- a/modules/data-streams/src/javaRestTest/java/org/elasticsearch/datastreams/logsdb/qa/exceptions/MismatchTypeMatcherException.java +++ /dev/null @@ -1,24 +0,0 @@ -/* - * 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.exceptions; - -import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.xcontent.XContentBuilder; - -public class MismatchTypeMatcherException extends MatcherException { - public MismatchTypeMatcherException( - XContentBuilder actualMappings, - Settings.Builder actualSettings, - XContentBuilder expectedMappings, - Settings.Builder expectedSettings, - String errorMessage - ) { - super(actualMappings, actualSettings, expectedMappings, expectedSettings, errorMessage); - } -} diff --git a/modules/data-streams/src/javaRestTest/java/org/elasticsearch/datastreams/logsdb/qa/exceptions/NotEqualMatcherException.java b/modules/data-streams/src/javaRestTest/java/org/elasticsearch/datastreams/logsdb/qa/exceptions/NotEqualMatcherException.java deleted file mode 100644 index 8cd9946e7f065..0000000000000 --- a/modules/data-streams/src/javaRestTest/java/org/elasticsearch/datastreams/logsdb/qa/exceptions/NotEqualMatcherException.java +++ /dev/null @@ -1,24 +0,0 @@ -/* - * 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.exceptions; - -import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.xcontent.XContentBuilder; - -public class NotEqualMatcherException extends MatcherException { - public NotEqualMatcherException( - XContentBuilder actualMappings, - Settings.Builder actualSettings, - XContentBuilder expectedMappings, - Settings.Builder expectedSettings, - String errorMessage - ) { - super(actualMappings, actualSettings, expectedMappings, expectedSettings, errorMessage); - } -} diff --git a/modules/data-streams/src/javaRestTest/java/org/elasticsearch/datastreams/logsdb/qa/exceptions/UncomparableMatcherException.java b/modules/data-streams/src/javaRestTest/java/org/elasticsearch/datastreams/logsdb/qa/exceptions/UncomparableMatcherException.java deleted file mode 100644 index 44455ec2783f6..0000000000000 --- a/modules/data-streams/src/javaRestTest/java/org/elasticsearch/datastreams/logsdb/qa/exceptions/UncomparableMatcherException.java +++ /dev/null @@ -1,24 +0,0 @@ -/* - * 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.exceptions; - -import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.xcontent.XContentBuilder; - -public class UncomparableMatcherException extends MatcherException { - public UncomparableMatcherException( - XContentBuilder actualMappings, - Settings.Builder actualSettings, - XContentBuilder expectedMappings, - Settings.Builder expectedSettings, - String errorMessage - ) { - super(actualMappings, actualSettings, expectedMappings, expectedSettings, errorMessage); - } -} diff --git a/modules/data-streams/src/javaRestTest/java/org/elasticsearch/datastreams/logsdb/qa/matchers/ArrayEqualMatcher.java b/modules/data-streams/src/javaRestTest/java/org/elasticsearch/datastreams/logsdb/qa/matchers/ArrayEqualMatcher.java new file mode 100644 index 0000000000000..25e6dc8ef31c9 --- /dev/null +++ b/modules/data-streams/src/javaRestTest/java/org/elasticsearch/datastreams/logsdb/qa/matchers/ArrayEqualMatcher.java @@ -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 { + 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 actualList = Arrays.asList(actualArray); + final List expectedList = Arrays.asList(expectedArray); + return actualList.containsAll(expectedList) && expectedList.containsAll(actualList); + } + + private static 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; + } +} diff --git a/modules/data-streams/src/javaRestTest/java/org/elasticsearch/datastreams/logsdb/qa/matchers/EqualMatcher.java b/modules/data-streams/src/javaRestTest/java/org/elasticsearch/datastreams/logsdb/qa/matchers/EqualMatcher.java index b3621f2596ae4..b6c5704e0677d 100644 --- a/modules/data-streams/src/javaRestTest/java/org/elasticsearch/datastreams/logsdb/qa/matchers/EqualMatcher.java +++ b/modules/data-streams/src/javaRestTest/java/org/elasticsearch/datastreams/logsdb/qa/matchers/EqualMatcher.java @@ -9,22 +9,18 @@ package org.elasticsearch.datastreams.logsdb.qa.matchers; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.datastreams.logsdb.qa.exceptions.MatcherException; -import org.elasticsearch.datastreams.logsdb.qa.exceptions.MismatchTypeMatcherException; -import org.elasticsearch.datastreams.logsdb.qa.exceptions.UncomparableMatcherException; import org.elasticsearch.xcontent.XContentBuilder; -import java.util.Arrays; import java.util.List; class EqualMatcher extends Matcher { - private final XContentBuilder actualMappings; - private final Settings.Builder actualSettings; - private final XContentBuilder expectedMappings; - private final Settings.Builder expectedSettings; - private final T actual; - private final T expected; - private final boolean ignoringSort; + protected final XContentBuilder actualMappings; + protected final Settings.Builder actualSettings; + protected final XContentBuilder expectedMappings; + protected final Settings.Builder expectedSettings; + protected final T actual; + protected final T expected; + protected final boolean ignoringSort; EqualMatcher( XContentBuilder actualMappings, @@ -44,65 +40,54 @@ class EqualMatcher extends Matcher { this.ignoringSort = ignoringSort; } - @SuppressWarnings("unchecked") - public boolean match() throws MatcherException { + public MatchResult match() { if (actual == null) { if (expected == null) { - throw new UncomparableMatcherException( - actualMappings, - actualSettings, - expectedMappings, - expectedSettings, - "Both 'actual' and 'expected' are null" + + return MatchResult.noMatch( + formatErrorMessage( + actualMappings, + actualSettings, + expectedMappings, + expectedSettings, + "Both 'actual' and 'expected' are null" + ) ); } - return false; + return MatchResult.noMatch( + formatErrorMessage(actualMappings, actualSettings, expectedMappings, expectedSettings, "Expected is null but actual is not") + ); } if (expected == null) { - return false; + return MatchResult.noMatch( + formatErrorMessage(actualMappings, actualSettings, expectedMappings, expectedSettings, "Actual is null but expected is not") + ); } if (actual.getClass().equals(expected.getClass()) == false) { - throw new MismatchTypeMatcherException( + return MatchResult.noMatch( + formatErrorMessage( + actualMappings, + actualSettings, + expectedMappings, + expectedSettings, + "Unable to match " + actual.getClass().getSimpleName() + " to " + expected.getClass().getSimpleName() + ) + ); + } + if (actual.getClass().isArray()) { + return new ArrayEqualMatcher( actualMappings, actualSettings, expectedMappings, expectedSettings, - "Unable to match " + actual.getClass().getSimpleName() + " to " + expected.getClass().getSimpleName() - ); - } - if (actual.getClass().isArray()) { - return matchArraysEqual((T[]) actual, (T[]) expected, ignoringSort); + (Object[]) actual, + (Object[]) expected, + ignoringSort + ).match(); } if (actual instanceof List act && expected instanceof List exp) { - return matchArraysEqual((T[]) (act).toArray(), (T[]) (exp).toArray(), ignoringSort); - } - return actual.equals(expected); - } - - private boolean matchArraysEqual(final T[] actualArray, final T[] expectedArray, boolean ignoreSorting) { - if (actualArray.length != expectedArray.length) { - return false; - } - if (ignoreSorting) { - return matchArraysEqualIgnoringSorting(actualArray, expectedArray) != false; - } else { - return matchArraysEqualExact(actualArray, expectedArray) != false; - } - } - - private static boolean matchArraysEqualIgnoringSorting(T[] actualArray, T[] expectedArray) { - final List actualList = Arrays.asList(actualArray); - final List expectedList = Arrays.asList(expectedArray); - return actualList.containsAll(expectedList) && expectedList.containsAll(actualList); - } - - private static 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 true; - } + return new ListEqualMatcher(actualMappings, actualSettings, expectedMappings, expectedSettings, act, exp, ignoringSort).match(); } - return false; + return new ObjectMatcher(actualMappings, actualSettings, expectedMappings, expectedSettings, actual, expected).match(); } } diff --git a/modules/data-streams/src/javaRestTest/java/org/elasticsearch/datastreams/logsdb/qa/matchers/ListEqualMatcher.java b/modules/data-streams/src/javaRestTest/java/org/elasticsearch/datastreams/logsdb/qa/matchers/ListEqualMatcher.java new file mode 100644 index 0000000000000..56c24712f635c --- /dev/null +++ b/modules/data-streams/src/javaRestTest/java/org/elasticsearch/datastreams/logsdb/qa/matchers/ListEqualMatcher.java @@ -0,0 +1,75 @@ +/* + * 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.List; + +class ListEqualMatcher extends EqualMatcher> { + ListEqualMatcher( + final XContentBuilder actualMappings, + final Settings.Builder actualSettings, + final XContentBuilder expectedMappings, + final Settings.Builder expectedSettings, + final List actual, + final List expected, + final boolean ignoringSort + ) { + super(actualMappings, actualSettings, expectedMappings, expectedSettings, actual, expected, ignoringSort); + } + + @Override + @SuppressWarnings("unchecked") + public MatchResult match() { + return matchListEquals((List) actual, (List) expected, ignoringSort); + } + + private MatchResult matchListEquals(final List actualList, final List expectedList, boolean ignoreSorting) { + if (actualList.size() != expectedList.size()) { + return MatchResult.noMatch( + formatErrorMessage(actualMappings, actualSettings, expectedMappings, expectedSettings, "List lengths do no match") + ); + } + if (ignoreSorting) { + return matchListsEqualIgnoringSorting(actualList, expectedList) + ? MatchResult.match() + : MatchResult.noMatch( + formatErrorMessage( + actualMappings, + actualSettings, + expectedMappings, + expectedSettings, + "Lists do not match when ignoreing sort order" + ) + ); + } else { + return matchListsEqualExact(actualList, expectedList) + ? MatchResult.match() + : MatchResult.noMatch( + formatErrorMessage(actualMappings, actualSettings, expectedMappings, expectedSettings, "Lists do not match exactly") + ); + } + } + + private static boolean matchListsEqualIgnoringSorting(final List actualList, final List expectedList) { + return actualList.containsAll(expectedList) && expectedList.containsAll(actualList); + } + + private static boolean matchListsEqualExact(List actualList, List expectedList) { + for (int i = 0; i < actualList.size(); i++) { + boolean isEqual = actualList.get(i).equals(expectedList.get(i)); + if (isEqual == false) { + return false; + } + } + return true; + } +} diff --git a/modules/data-streams/src/javaRestTest/java/org/elasticsearch/datastreams/logsdb/qa/matchers/MatchResult.java b/modules/data-streams/src/javaRestTest/java/org/elasticsearch/datastreams/logsdb/qa/matchers/MatchResult.java new file mode 100644 index 0000000000000..07a57dcca3b71 --- /dev/null +++ b/modules/data-streams/src/javaRestTest/java/org/elasticsearch/datastreams/logsdb/qa/matchers/MatchResult.java @@ -0,0 +1,55 @@ +/* + * 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 java.util.Objects; + +public class MatchResult { + private final boolean isMatch; + private final String message; + + private MatchResult(boolean isMatch, String message) { + this.isMatch = isMatch; + this.message = message; + } + + public static MatchResult match() { + return new MatchResult(true, "Match successful"); + } + + public static MatchResult noMatch(final String reason) { + return new MatchResult(false, reason); + } + + public boolean isMatch() { + return isMatch; + } + + public String getMessage() { + return message; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + MatchResult that = (MatchResult) o; + return isMatch == that.isMatch && Objects.equals(message, that.message); + } + + @Override + public int hashCode() { + return Objects.hash(isMatch, message); + } + + @Override + public String toString() { + return "MatchResult{" + "isMatch=" + isMatch + ", message='" + message + '\'' + '}'; + } +} diff --git a/modules/data-streams/src/javaRestTest/java/org/elasticsearch/datastreams/logsdb/qa/matchers/Matcher.java b/modules/data-streams/src/javaRestTest/java/org/elasticsearch/datastreams/logsdb/qa/matchers/Matcher.java index 50afda072c4d5..7faa6cbff5456 100644 --- a/modules/data-streams/src/javaRestTest/java/org/elasticsearch/datastreams/logsdb/qa/matchers/Matcher.java +++ b/modules/data-streams/src/javaRestTest/java/org/elasticsearch/datastreams/logsdb/qa/matchers/Matcher.java @@ -8,9 +8,8 @@ package org.elasticsearch.datastreams.logsdb.qa.matchers; +import org.elasticsearch.common.Strings; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.datastreams.logsdb.qa.exceptions.MatcherException; -import org.elasticsearch.datastreams.logsdb.qa.exceptions.NotEqualMatcherException; import org.elasticsearch.xcontent.XContentBuilder; /** @@ -31,7 +30,7 @@ public interface ExpectedStep { } public interface CompareStep { - void isEqualTo(T actual) throws MatcherException; + MatchResult isEqualTo(T actual); CompareStep ignoringSort(boolean ignoringSort); } @@ -63,25 +62,9 @@ private Builder( } @Override - public void isEqualTo(T actual) throws MatcherException { - boolean match = new EqualMatcher<>( - actualMappings, - actualSettings, - expectedMappings, - expectedSettings, - actual, - expected, - ignoringSort - ).match(); - if (match == false) { - throw new NotEqualMatcherException( - actualMappings, - actualSettings, - expectedMappings, - expectedSettings, - "actual [" + actual + "] not equal to [" + expected + "]" - ); - } + public MatchResult isEqualTo(T actual) { + return new EqualMatcher<>(actualMappings, actualSettings, expectedMappings, expectedSettings, actual, expected, ignoringSort) + .match(); } @Override @@ -97,4 +80,28 @@ public CompareStep expected(T expected) { } } + protected static String formatErrorMessage( + final XContentBuilder actualMappings, + final Settings.Builder actualSettings, + final XContentBuilder expectedMappings, + final Settings.Builder expectedSettings, + final String errorMessage + ) { + return "Error [" + + errorMessage + + "] " + + "actual mappings [" + + Strings.toString(actualMappings) + + "] " + + "actual settings [" + + Strings.toString(actualSettings.build()) + + "] " + + "expected mappings [" + + Strings.toString(expectedMappings) + + "] " + + "expected settings [" + + Strings.toString(expectedSettings.build()) + + "] "; + } + } diff --git a/modules/data-streams/src/javaRestTest/java/org/elasticsearch/datastreams/logsdb/qa/matchers/ObjectMatcher.java b/modules/data-streams/src/javaRestTest/java/org/elasticsearch/datastreams/logsdb/qa/matchers/ObjectMatcher.java new file mode 100644 index 0000000000000..ebc50e8a7e89c --- /dev/null +++ b/modules/data-streams/src/javaRestTest/java/org/elasticsearch/datastreams/logsdb/qa/matchers/ObjectMatcher.java @@ -0,0 +1,34 @@ +/* + * 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; + +public class ObjectMatcher extends EqualMatcher { + ObjectMatcher( + final XContentBuilder actualMappings, + final Settings.Builder actualSettings, + final XContentBuilder expectedMappings, + final Settings.Builder expectedSettings, + final Object actual, + final Object expected + ) { + super(actualMappings, actualSettings, expectedMappings, expectedSettings, actual, expected, true); + } + + @Override + public MatchResult match() { + return actual.equals(expected) + ? MatchResult.match() + : MatchResult.noMatch( + formatErrorMessage(actualMappings, actualSettings, expectedMappings, expectedSettings, "Actual does not equal expected") + ); + } +}