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 @@ -188,7 +188,8 @@ public void testMatchAllQuery() throws IOException {
final SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder().query(QueryBuilders.matchAllQuery())
.size(numberOfDocuments);

final MatchResult matchResult = Matcher.mappings(getContenderMappings(), getBaselineMappings())
final MatchResult matchResult = Matcher.matchSource()
.mappings(getContenderMappings(), getBaselineMappings())
.settings(getContenderSettings(), getBaselineSettings())
.expected(getQueryHits(queryBaseline(searchSourceBuilder)))
.ignoringSort(true)
Expand All @@ -208,7 +209,8 @@ public void testTermsQuery() throws IOException {
final SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder().query(QueryBuilders.termQuery("method", "put"))
.size(numberOfDocuments);

final MatchResult matchResult = Matcher.mappings(getContenderMappings(), getBaselineMappings())
final MatchResult matchResult = Matcher.matchSource()
.mappings(getContenderMappings(), getBaselineMappings())
.settings(getContenderSettings(), getBaselineSettings())
.expected(getQueryHits(queryBaseline(searchSourceBuilder)))
.ignoringSort(true)
Expand Down Expand Up @@ -324,5 +326,4 @@ private void assertDocumentIndexing(List<XContentBuilder> documents) throws IOEx
var contenderResponseBody = entityAsMap(tuple.v2());
assertThat("errors in contender bulk response:\n " + contenderResponseBody, contenderResponseBody.get("errors"), equalTo(false));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import java.io.IOException;
import java.time.Instant;
import java.util.List;
import java.util.function.Function;

/**
* Challenge test (see {@link StandardVersusLogsIndexModeChallengeRestIT}) that uses randomly generated
Expand All @@ -42,26 +41,7 @@ public StandardVersusLogsIndexModeRandomDataChallengeRestIT() {
DataGeneratorSpecification.builder()
// Nested fields don't work with subobjects: false.
.withNestedFieldsLimit(0)
// TODO increase depth of objects
// Currently matching fails because in synthetic source all fields are flat (given that we have subobjects: false)
// but stored source is identical to original document which has nested structure.
.withMaxObjectDepth(0)
.withDataSourceHandlers(List.of(new DataSourceHandler() {
// TODO enable null values
// Matcher does not handle nulls currently
@Override
public DataSourceResponse.NullWrapper handle(DataSourceRequest.NullWrapper request) {
return new DataSourceResponse.NullWrapper(Function.identity());
}

// TODO enable arrays
// List matcher currently does not apply matching logic recursively
// and equality check fails because arrays are sorted in synthetic source.
@Override
public DataSourceResponse.ArrayWrapper handle(DataSourceRequest.ArrayWrapper request) {
return new DataSourceResponse.ArrayWrapper(Function.identity());
}

// TODO enable scaled_float fields
// There a difference in synthetic source (precision loss)
// specific to this fields which matcher can't handle.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@
import java.util.Arrays;
import java.util.List;

class ArrayEqualMatcher extends EqualMatcher<Object[]> {
import static org.elasticsearch.datastreams.logsdb.qa.matchers.Messages.formatErrorMessage;
import static org.elasticsearch.datastreams.logsdb.qa.matchers.Messages.prettyPrintArrays;

class ArrayEqualMatcher extends GenericEqualsMatcher<Object[]> {
ArrayEqualMatcher(
final XContentBuilder actualMappings,
final Settings.Builder actualSettings,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@

import java.util.List;

class EqualMatcher<T> extends Matcher {
import static org.elasticsearch.datastreams.logsdb.qa.matchers.Messages.formatErrorMessage;

public class GenericEqualsMatcher<T> extends Matcher {
protected final XContentBuilder actualMappings;
protected final Settings.Builder actualSettings;
protected final XContentBuilder expectedMappings;
Expand All @@ -22,7 +24,7 @@ class EqualMatcher<T> extends Matcher {
protected final T expected;
protected final boolean ignoringSort;

EqualMatcher(
protected GenericEqualsMatcher(
XContentBuilder actualMappings,
Settings.Builder actualSettings,
XContentBuilder expectedMappings,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@

import java.util.List;

class ListEqualMatcher extends EqualMatcher<List<?>> {
ListEqualMatcher(
import static org.elasticsearch.datastreams.logsdb.qa.matchers.Messages.formatErrorMessage;
import static org.elasticsearch.datastreams.logsdb.qa.matchers.Messages.prettyPrintCollections;

public class ListEqualMatcher extends GenericEqualsMatcher<List<?>> {
public ListEqualMatcher(
final XContentBuilder actualMappings,
final Settings.Builder actualSettings,
final XContentBuilder expectedMappings,
Expand All @@ -40,7 +43,7 @@ private MatchResult matchListEquals(final List<Object> actualList, final List<Ob
actualSettings,
expectedMappings,
expectedSettings,
"List lengths do no match, " + prettyPrintLists(actualList, expectedList)
"List lengths do no match, " + prettyPrintCollections(actualList, expectedList)
)
);
}
Expand All @@ -53,7 +56,7 @@ private MatchResult matchListEquals(final List<Object> actualList, final List<Ob
actualSettings,
expectedMappings,
expectedSettings,
"Lists do not match when ignoring sort order, " + prettyPrintLists(actualList, expectedList)
"Lists do not match when ignoring sort order, " + prettyPrintCollections(actualList, expectedList)
)
);
} else {
Expand All @@ -65,7 +68,7 @@ private MatchResult matchListEquals(final List<Object> actualList, final List<Ob
actualSettings,
expectedMappings,
expectedSettings,
"Lists do not match exactly, " + prettyPrintLists(actualList, expectedList)
"Lists do not match exactly, " + prettyPrintCollections(actualList, expectedList)
)
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,12 @@

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

import org.elasticsearch.common.Strings;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.datastreams.logsdb.qa.matchers.source.SourceMatcher;
import org.elasticsearch.xcontent.XContentBuilder;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.Map;

/**
* A base class to be used for the matching logic when comparing query results.
Expand All @@ -25,6 +24,14 @@ public static <T> SettingsStep<T> mappings(final XContentBuilder actualMappings,
return new Builder<>(expectedMappings, actualMappings);
}

public static MappingsStep<List<Map<String, Object>>> matchSource() {
return new SourceMatcherBuilder();
}

public interface MappingsStep<T> {
SettingsStep<T> mappings(XContentBuilder actualMappings, XContentBuilder expectedMappings);
}

public interface SettingsStep<T> {
ExpectedStep<T> settings(Settings.Builder actualSettings, Settings.Builder expectedSettings);
}
Expand All @@ -40,7 +47,6 @@ public interface CompareStep<T> {
}

private static class Builder<T> implements SettingsStep<T>, CompareStep<T>, ExpectedStep<T> {

private final XContentBuilder expectedMappings;
private final XContentBuilder actualMappings;
private Settings.Builder expectedSettings;
Expand All @@ -67,8 +73,15 @@ private Builder(

@Override
public MatchResult isEqualTo(T actual) {
return new EqualMatcher<>(actualMappings, actualSettings, expectedMappings, expectedSettings, actual, expected, ignoringSort)
.match();
return new GenericEqualsMatcher<>(
actualMappings,
actualSettings,
expectedMappings,
expectedSettings,
actual,
expected,
ignoringSort
).match();
}

@Override
Expand All @@ -84,39 +97,54 @@ public CompareStep<T> 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())
+ "] ";
}
private static class SourceMatcherBuilder
implements
MappingsStep<List<Map<String, Object>>>,
SettingsStep<List<Map<String, Object>>>,
CompareStep<List<Map<String, Object>>>,
ExpectedStep<List<Map<String, Object>>> {
private XContentBuilder expectedMappings;
private XContentBuilder actualMappings;
private Settings.Builder expectedSettings;
private Settings.Builder actualSettings;
private List<Map<String, Object>> expected;
private boolean ignoringSort;

protected static String prettyPrintArrays(final Object[] actualArray, final Object[] expectedArray) {
return "actual: " + prettyPrintList(Arrays.asList(actualArray)) + ", expected: " + prettyPrintList(Arrays.asList(expectedArray));
}
@Override
public ExpectedStep<List<Map<String, Object>>> settings(Settings.Builder actualSettings, Settings.Builder expectedSettings) {
this.actualSettings = actualSettings;
this.expectedSettings = expectedSettings;
return this;
}

protected static String prettyPrintLists(final List<Object> actualList, final List<Object> expectedList) {
return "actual: " + prettyPrintList(actualList) + ", expected: " + prettyPrintList(expectedList);
}
private SourceMatcherBuilder() {}

private static String prettyPrintList(final List<Object> list) {
return "[" + list.stream().map(Object::toString).collect(Collectors.joining(", ")) + "]";
public SettingsStep<List<Map<String, Object>>> mappings(
final XContentBuilder actualMappings,
final XContentBuilder expectedMappings
) {
this.actualMappings = actualMappings;
this.expectedMappings = expectedMappings;

return this;
}

@Override
public MatchResult isEqualTo(List<Map<String, Object>> actual) {
return new SourceMatcher(actualMappings, actualSettings, expectedMappings, expectedSettings, actual, expected, ignoringSort)
.match();
}

@Override
public CompareStep<List<Map<String, Object>>> ignoringSort(boolean ignoringSort) {
this.ignoringSort = ignoringSort;
return this;
}

@Override
public CompareStep<List<Map<String, Object>>> expected(List<Map<String, Object>> expected) {
this.expected = expected;
return this;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* 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.Strings;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.xcontent.XContentBuilder;

import java.util.Arrays;
import java.util.Collection;
import java.util.stream.Collectors;

public class Messages {
public 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())
+ "] ";
}

public static String prettyPrintArrays(final Object[] actualArray, final Object[] expectedArray) {
return "actual: "
+ prettyPrintCollection(Arrays.asList(actualArray))
+ ", expected: "
+ prettyPrintCollection(Arrays.asList(expectedArray));
}

public static <T> String prettyPrintCollections(final Collection<T> actualList, final Collection<T> expectedList) {
return "actual: " + prettyPrintCollection(actualList) + ", expected: " + prettyPrintCollection(expectedList);
}

private static <T> String prettyPrintCollection(final Collection<T> list) {
return "[" + list.stream().map(Object::toString).collect(Collectors.joining(", ")) + "]";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.xcontent.XContentBuilder;

public class ObjectMatcher extends EqualMatcher<Object> {
import static org.elasticsearch.datastreams.logsdb.qa.matchers.Messages.formatErrorMessage;

public class ObjectMatcher extends GenericEqualsMatcher<Object> {
ObjectMatcher(
final XContentBuilder actualMappings,
final Settings.Builder actualSettings,
Expand Down
Loading