Skip to content

Commit 914c57f

Browse files
committed
cleanup
1 parent f35f858 commit 914c57f

File tree

2 files changed

+26
-30
lines changed

2 files changed

+26
-30
lines changed

test/framework/src/main/java/org/elasticsearch/logsdb/datageneration/matchers/source/DynamicFieldMatcher.java

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
package org.elasticsearch.logsdb.datageneration.matchers.source;
1111

1212
import org.elasticsearch.common.settings.Settings;
13+
import org.elasticsearch.logsdb.datageneration.matchers.ListEqualMatcher;
1314
import org.elasticsearch.logsdb.datageneration.matchers.MatchResult;
1415
import org.elasticsearch.xcontent.XContentBuilder;
1516

@@ -46,9 +47,12 @@ class DynamicFieldMatcher {
4647
* @return {#{@link MatchResult}} if field values need special treatment by this matcher.
4748
* If field values can be matched using generic mapper, returns {@link Optional#empty()}.
4849
*/
49-
public Optional<MatchResult> match(List<Object> actual, List<Object> expected) {
50+
public MatchResult match(List<Object> actual, List<Object> expected) {
5051
if (expected == null) {
51-
return Optional.empty();
52+
expected = List.of();
53+
}
54+
if (actual == null) {
55+
actual = List.of();
5256
}
5357

5458
// Floating point values are always mapped as float with dynamic mapping.
@@ -59,7 +63,7 @@ public Optional<MatchResult> match(List<Object> actual, List<Object> expected) {
5963
var normalizedActual = normalizeDoubles(actual);
6064
var normalizedExpected = normalizeDoubles(expected);
6165

62-
var matchResult = normalizedActual.equals(normalizedExpected)
66+
return normalizedActual.equals(normalizedExpected)
6367
? MatchResult.match()
6468
: MatchResult.noMatch(
6569
formatErrorMessage(
@@ -71,10 +75,9 @@ public Optional<MatchResult> match(List<Object> actual, List<Object> expected) {
7175
+ prettyPrintCollections(normalizedActual, normalizedExpected)
7276
)
7377
);
74-
return Optional.of(matchResult);
7578
}
7679

77-
return Optional.empty();
80+
return matchWithGenericMatcher(actual, expected);
7881
}
7982

8083
private static Set<Float> normalizeDoubles(List<Object> values) {
@@ -85,4 +88,18 @@ private static Set<Float> normalizeDoubles(List<Object> values) {
8588
Function<Object, Float> toFloat = (o) -> o instanceof Number n ? n.floatValue() : Float.parseFloat((String) o);
8689
return values.stream().filter(Objects::nonNull).map(toFloat).collect(Collectors.toSet());
8790
}
91+
92+
private MatchResult matchWithGenericMatcher(List<Object> actualValues, List<Object> expectedValues) {
93+
var genericListMatcher = new ListEqualMatcher(
94+
actualMappings,
95+
actualSettings,
96+
expectedMappings,
97+
expectedSettings,
98+
SourceTransforms.normalizeValues(actualValues),
99+
SourceTransforms.normalizeValues(expectedValues),
100+
true
101+
);
102+
103+
return genericListMatcher.match();
104+
}
88105
}

test/framework/src/main/java/org/elasticsearch/logsdb/datageneration/matchers/source/SourceMatcher.java

Lines changed: 4 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,13 @@
1313
import org.elasticsearch.common.settings.Settings;
1414
import org.elasticsearch.common.xcontent.XContentHelper;
1515
import org.elasticsearch.logsdb.datageneration.matchers.GenericEqualsMatcher;
16-
import org.elasticsearch.logsdb.datageneration.matchers.ListEqualMatcher;
1716
import org.elasticsearch.logsdb.datageneration.matchers.MatchResult;
1817
import org.elasticsearch.xcontent.XContentBuilder;
1918

2019
import java.util.HashMap;
2120
import java.util.List;
2221
import java.util.Map;
2322
import java.util.Objects;
24-
import java.util.Optional;
2523

2624
import static org.elasticsearch.logsdb.datageneration.matchers.Messages.formatErrorMessage;
2725
import static org.elasticsearch.logsdb.datageneration.matchers.Messages.prettyPrintCollections;
@@ -138,9 +136,7 @@ private MatchResult compareSource(Map<String, List<Object>> actual, Map<String,
138136
var actualValues = actual.get(name);
139137
var expectedValues = expectedFieldEntry.getValue();
140138

141-
var matchIncludingFieldSpecificMatchers = matchWithFieldSpecificMatcher(name, actualValues, expectedValues).orElseGet(
142-
() -> matchWithGenericMatcher(actualValues, expectedValues)
143-
);
139+
var matchIncludingFieldSpecificMatchers = matchWithFieldSpecificMatcher(name, actualValues, expectedValues);
144140
if (matchIncludingFieldSpecificMatchers.isMatch() == false) {
145141
var message = "Source documents don't match for field [" + name + "]: " + matchIncludingFieldSpecificMatchers.getMessage();
146142
return MatchResult.noMatch(message);
@@ -149,7 +145,7 @@ private MatchResult compareSource(Map<String, List<Object>> actual, Map<String,
149145
return MatchResult.match();
150146
}
151147

152-
private Optional<MatchResult> matchWithFieldSpecificMatcher(String fieldName, List<Object> actualValues, List<Object> expectedValues) {
148+
private MatchResult matchWithFieldSpecificMatcher(String fieldName, List<Object> actualValues, List<Object> expectedValues) {
153149
var actualFieldMapping = actualNormalizedMapping.get(fieldName);
154150
if (actualFieldMapping == null) {
155151
if (expectedNormalizedMapping.get(fieldName) != null
@@ -189,30 +185,13 @@ private Optional<MatchResult> matchWithFieldSpecificMatcher(String fieldName, Li
189185
}
190186

191187
var fieldSpecificMatcher = fieldSpecificMatchers.get(actualFieldType);
192-
if (fieldSpecificMatcher == null) {
193-
return Optional.empty();
194-
}
188+
assert fieldSpecificMatcher != null : "Missing matcher for field type [" + actualFieldType + "]";
195189

196-
MatchResult matched = fieldSpecificMatcher.match(
190+
return fieldSpecificMatcher.match(
197191
actualValues,
198192
expectedValues,
199193
actualFieldMapping.mappingParameters(),
200194
expectedFieldMapping.mappingParameters()
201195
);
202-
return Optional.of(matched);
203-
}
204-
205-
private MatchResult matchWithGenericMatcher(List<Object> actualValues, List<Object> expectedValues) {
206-
var genericListMatcher = new ListEqualMatcher(
207-
actualMappings,
208-
actualSettings,
209-
expectedMappings,
210-
expectedSettings,
211-
SourceTransforms.normalizeValues(actualValues),
212-
SourceTransforms.normalizeValues(expectedValues),
213-
true
214-
);
215-
216-
return genericListMatcher.match();
217196
}
218197
}

0 commit comments

Comments
 (0)