Skip to content

Commit

Permalink
Ingest: move IngestDocument test ctor usage to static builder (#87770)
Browse files Browse the repository at this point in the history
`IngestDocument` has a test constructor that takes a `sourceAndMetadata` and `ingestMetadata` map.

However, that test constructor was also used from wire deserialization in `WriteableIngestDocument` 
and during `XContent` parsing.

This change moves test usage into test static builders and the wire usage into another static builder to
make the callers intent clear.

`IngestDocument`s constructed for test usage will not need metadata
validation performed on construction when that is added in a future commit.
  • Loading branch information
stu-elastic committed Jun 21, 2022
1 parent 0d98910 commit e0ced8a
Show file tree
Hide file tree
Showing 31 changed files with 202 additions and 154 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
package org.elasticsearch.ingest.common;

import org.elasticsearch.ingest.IngestDocument;
import org.elasticsearch.ingest.TestIngestDocument;
import org.elasticsearch.test.ESTestCase;
import org.junit.Before;

Expand Down Expand Up @@ -350,7 +351,7 @@ private void testCommunityIdProcessor(Map<String, Object> source, int seed, Stri
ignoreMissing
);

IngestDocument input = new IngestDocument(source, Map.of());
IngestDocument input = TestIngestDocument.ofSourceAndMetadata(source);
IngestDocument output = processor.execute(input);

String hash = output.getFieldValue(DEFAULT_TARGET, String.class, ignoreMissing);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.elasticsearch.ingest.IngestDocument;
import org.elasticsearch.ingest.Processor;
import org.elasticsearch.ingest.RandomDocumentPicks;
import org.elasticsearch.ingest.TestIngestDocument;
import org.elasticsearch.test.ESTestCase;

import java.util.ArrayList;
Expand Down Expand Up @@ -585,7 +586,7 @@ public void testAutoConvertMatchFloat() throws Exception {
}

public void testTargetField() throws Exception {
IngestDocument ingestDocument = new IngestDocument(new HashMap<>(), new HashMap<>());
IngestDocument ingestDocument = TestIngestDocument.emptyIngestDocument();
int randomInt = randomInt();
String fieldName = RandomDocumentPicks.addRandomField(random(), ingestDocument, String.valueOf(randomInt));
String targetField = fieldName + randomAlphaOfLength(5);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import org.elasticsearch.ingest.IngestDocument;
import org.elasticsearch.ingest.Processor;
import org.elasticsearch.ingest.TestIngestDocument;
import org.elasticsearch.ingest.TestTemplateService;
import org.elasticsearch.test.ESTestCase;

Expand All @@ -25,15 +26,15 @@ public class DotExpanderProcessorTests extends ESTestCase {
public void testEscapeFields() throws Exception {
Map<String, Object> source = new HashMap<>();
source.put("foo.bar", "baz1");
IngestDocument document = new IngestDocument(source, Collections.emptyMap());
IngestDocument document = TestIngestDocument.ofSourceAndMetadata(source);
DotExpanderProcessor processor = new DotExpanderProcessor("_tag", null, null, "foo.bar");
processor.execute(document);
assertThat(document.getFieldValue("foo", Map.class).size(), equalTo(1));
assertThat(document.getFieldValue("foo.bar", String.class), equalTo("baz1"));

source = new HashMap<>();
source.put("foo.bar.baz", "value");
document = new IngestDocument(source, Collections.emptyMap());
document = TestIngestDocument.ofSourceAndMetadata(source);
processor = new DotExpanderProcessor("_tag", null, null, "foo.bar.baz");
processor.execute(document);
assertThat(document.getFieldValue("foo", Map.class).size(), equalTo(1));
Expand All @@ -43,7 +44,7 @@ public void testEscapeFields() throws Exception {
source = new HashMap<>();
source.put("foo.bar", "baz1");
source.put("foo", new HashMap<>(Collections.singletonMap("bar", "baz2")));
document = new IngestDocument(source, Collections.emptyMap());
document = TestIngestDocument.ofSourceAndMetadata(source);
processor = new DotExpanderProcessor("_tag", null, null, "foo.bar");
processor.execute(document);
assertThat(document.getSourceAndMetadata().size(), equalTo(1));
Expand All @@ -54,7 +55,7 @@ public void testEscapeFields() throws Exception {
source = new HashMap<>();
source.put("foo.bar", "2");
source.put("foo", new HashMap<>(Collections.singletonMap("bar", 1)));
document = new IngestDocument(source, Collections.emptyMap());
document = TestIngestDocument.ofSourceAndMetadata(source);
processor = new DotExpanderProcessor("_tag", null, null, "foo.bar");
processor.execute(document);
assertThat(document.getSourceAndMetadata().size(), equalTo(1));
Expand All @@ -67,15 +68,15 @@ public void testEscapeFields_valueField() throws Exception {
Map<String, Object> source = new HashMap<>();
source.put("foo.bar", "baz1");
source.put("foo", "baz2");
IngestDocument document1 = new IngestDocument(source, Collections.emptyMap());
IngestDocument document1 = TestIngestDocument.ofSourceAndMetadata(source);
Processor processor1 = new DotExpanderProcessor("_tag", null, null, "foo.bar");
// foo already exists and if a leaf field and therefor can't be replaced by a map field:
Exception e = expectThrows(IllegalArgumentException.class, () -> processor1.execute(document1));
assertThat(e.getMessage(), equalTo("cannot expand [foo.bar], because [foo] is not an object field, but a value field"));

// so because foo is no branch field but a value field the `foo.bar` field can't be expanded
// into [foo].[bar], so foo should be renamed first into `[foo].[bar]:
IngestDocument document = new IngestDocument(source, Collections.emptyMap());
IngestDocument document = TestIngestDocument.ofSourceAndMetadata(source);
Processor processor = new RenameProcessor(
"_tag",
null,
Expand All @@ -92,7 +93,7 @@ public void testEscapeFields_valueField() throws Exception {

source = new HashMap<>();
source.put("foo.bar", "baz1");
document = new IngestDocument(source, Collections.emptyMap());
document = TestIngestDocument.ofSourceAndMetadata(source);
processor = new DotExpanderProcessor("_tag", null, null, "foo.bar");
processor.execute(document);
assertThat(document.getFieldValue("foo", Map.class).size(), equalTo(1));
Expand All @@ -101,7 +102,7 @@ public void testEscapeFields_valueField() throws Exception {
source = new HashMap<>();
source.put("foo.bar.baz", "baz1");
source.put("foo", new HashMap<>(Collections.singletonMap("bar", new HashMap<>())));
document = new IngestDocument(source, Collections.emptyMap());
document = TestIngestDocument.ofSourceAndMetadata(source);
processor = new DotExpanderProcessor("_tag", null, null, "foo.bar.baz");
processor.execute(document);
assertThat(document.getFieldValue("foo", Map.class).size(), equalTo(1));
Expand All @@ -111,7 +112,7 @@ public void testEscapeFields_valueField() throws Exception {
source = new HashMap<>();
source.put("foo.bar.baz", "baz1");
source.put("foo", new HashMap<>(Collections.singletonMap("bar", "baz2")));
IngestDocument document2 = new IngestDocument(source, Collections.emptyMap());
IngestDocument document2 = TestIngestDocument.ofSourceAndMetadata(source);
Processor processor2 = new DotExpanderProcessor("_tag", null, null, "foo.bar.baz");
e = expectThrows(IllegalArgumentException.class, () -> processor2.execute(document2));
assertThat(e.getMessage(), equalTo("cannot expand [foo.bar.baz], because [foo.bar] is not an object field, but a value field"));
Expand All @@ -120,7 +121,7 @@ public void testEscapeFields_valueField() throws Exception {
public void testEscapeFields_path() throws Exception {
Map<String, Object> source = new HashMap<>();
source.put("foo", new HashMap<>(Collections.singletonMap("bar.baz", "value")));
IngestDocument document = new IngestDocument(source, Collections.emptyMap());
IngestDocument document = TestIngestDocument.ofSourceAndMetadata(source);
DotExpanderProcessor processor = new DotExpanderProcessor("_tag", null, "foo", "bar.baz");
processor.execute(document);
assertThat(document.getFieldValue("foo", Map.class).size(), equalTo(1));
Expand All @@ -129,7 +130,7 @@ public void testEscapeFields_path() throws Exception {

source = new HashMap<>();
source.put("field", new HashMap<>(Collections.singletonMap("foo.bar.baz", "value")));
document = new IngestDocument(source, Collections.emptyMap());
document = TestIngestDocument.ofSourceAndMetadata(source);
processor = new DotExpanderProcessor("_tag", null, "field", "foo.bar.baz");
processor.execute(document);
assertThat(document.getFieldValue("field.foo", Map.class).size(), equalTo(1));
Expand All @@ -141,7 +142,7 @@ public void testEscapeFields_doNothingIfFieldNotInSourceDoc() throws Exception {
// asking to expand a (literal) field that is not present in the source document
Map<String, Object> source = new HashMap<>();
source.put("foo.bar", "baz1");
IngestDocument document = new IngestDocument(source, Collections.emptyMap());
IngestDocument document = TestIngestDocument.ofSourceAndMetadata(source);
// abc.def does not exist in source, so don't mutate document
DotExpanderProcessor processor = new DotExpanderProcessor("_tag", null, null, "abc.def");
processor.execute(document);
Expand All @@ -159,7 +160,7 @@ public void testEscapeFields_doNothingIfFieldNotInSourceDoc() throws Exception {
Map<String, Object> inner = new HashMap<>();
inner.put("bar", "baz1");
source.put("foo", inner);
document = new IngestDocument(source, Collections.emptyMap());
document = TestIngestDocument.ofSourceAndMetadata(source);
// foo.bar, the literal value (as opposed to nested value) does not exist in source, so don't mutate document
processor = new DotExpanderProcessor("_tag", null, null, "foo.bar");
processor.execute(document);
Expand All @@ -177,7 +178,7 @@ public void testOverride() throws Exception {
inner.put("qux", "quux");
source.put("foo", inner);
source.put("foo.bar", "baz2");
IngestDocument document = new IngestDocument(source, Map.of());
IngestDocument document = TestIngestDocument.ofSourceAndMetadata(source);
DotExpanderProcessor processor = new DotExpanderProcessor("_tag", null, null, "foo.bar", true);
processor.execute(document);
assertThat(document.getFieldValue("foo", Map.class).size(), equalTo(2));
Expand All @@ -189,7 +190,7 @@ public void testWildcard() throws Exception {
Map<String, Object> source = new HashMap<>();
source.put("foo.bar", "baz");
source.put("qux.quux", "corge");
IngestDocument document = new IngestDocument(source, Map.of());
IngestDocument document = TestIngestDocument.ofSourceAndMetadata(source);
DotExpanderProcessor processor = new DotExpanderProcessor("_tag", null, null, "*");
processor.execute(document);
assertThat(document.getFieldValue("foo", Map.class).size(), equalTo(1));
Expand All @@ -201,7 +202,7 @@ public void testWildcard() throws Exception {
Map<String, Object> inner = new HashMap<>();
inner.put("bar.baz", "qux");
source.put("foo", inner);
document = new IngestDocument(source, Map.of());
document = TestIngestDocument.ofSourceAndMetadata(source);
processor = new DotExpanderProcessor("_tag", null, "foo", "*");
processor.execute(document);
assertThat(document.getFieldValue("foo", Map.class).size(), equalTo(1));
Expand All @@ -212,7 +213,7 @@ public void testWildcard() throws Exception {
inner = new HashMap<>();
inner.put("bar.baz", "qux");
source.put("foo", inner);
document = new IngestDocument(source, Map.of());
document = TestIngestDocument.ofSourceAndMetadata(source);
processor = new DotExpanderProcessor("_tag", null, null, "*");
processor.execute(document);
assertThat(document.getFieldValue("foo", Map.class).size(), equalTo(1));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

package org.elasticsearch.ingest.common;

import org.elasticsearch.ingest.IngestDocument;
import org.elasticsearch.ingest.TestIngestDocument;
import org.elasticsearch.test.ESTestCase;

import java.security.MessageDigest;
Expand Down Expand Up @@ -227,7 +227,7 @@ private String doTestFingerprint(
MessageDigest md = MessageDigest.getInstance(FingerprintProcessor.Factory.DEFAULT_METHOD);
expectedBytes = md.digest(expectedBytes);

var input = new IngestDocument(inputMap, Map.of());
var input = TestIngestDocument.ofSourceAndMetadata(inputMap);
var output = fp.execute(input);
assertTrue(output.hasField("fingerprint"));
String fingerprint = output.getFieldValue("fingerprint", String.class);
Expand Down Expand Up @@ -257,7 +257,7 @@ public void testMethod() throws Exception {
config.put("method", FingerprintProcessor.Factory.SUPPORTED_DIGESTS[k]);

FingerprintProcessor fp = factory.create(null, randomAlphaOfLength(10), null, config);
var input = new IngestDocument(inputMap, Map.of());
var input = TestIngestDocument.ofSourceAndMetadata(inputMap);
var output = fp.execute(input);
assertTrue(output.hasField("fingerprint"));
String fingerprint = output.getFieldValue("fingerprint", String.class);
Expand Down Expand Up @@ -394,7 +394,7 @@ private void doTestObjectTraversal(Map<String, Object> inputMap, List<String> fi
expectedBytes = concatBytes(expectedBytes, toBytes(value));
}

var input = new IngestDocument(inputMap, Map.of());
var input = TestIngestDocument.ofSourceAndMetadata(inputMap);
var output = fp.execute(input);
var hasher = (TestHasher) threadLocalHasher.get();
assertThat(hasher.getBytesSeen(), equalTo(expectedBytes));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.elasticsearch.grok.MatcherWatchdog;
import org.elasticsearch.ingest.IngestDocument;
import org.elasticsearch.ingest.RandomDocumentPicks;
import org.elasticsearch.ingest.TestIngestDocument;
import org.elasticsearch.test.ESTestCase;

import java.util.Arrays;
Expand Down Expand Up @@ -99,7 +100,7 @@ public void testNoMatchingPatternName() {

public void testMatchWithoutCaptures() throws Exception {
String fieldName = "value";
IngestDocument originalDoc = new IngestDocument(new HashMap<>(), new HashMap<>());
IngestDocument originalDoc = TestIngestDocument.emptyIngestDocument();
originalDoc.setFieldValue(fieldName, fieldName);
IngestDocument doc = new IngestDocument(originalDoc);
GrokProcessor processor = new GrokProcessor(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import org.elasticsearch.ElasticsearchParseException;
import org.elasticsearch.ingest.IngestDocument;
import org.elasticsearch.ingest.TestIngestDocument;
import org.elasticsearch.ingest.TestTemplateService;
import org.elasticsearch.test.ESTestCase;

Expand Down Expand Up @@ -149,7 +150,7 @@ public void testReadFromField() throws Exception {
null,
config
);
IngestDocument input = new IngestDocument(source, Map.of());
IngestDocument input = TestIngestDocument.ofSourceAndMetadata(source);
IngestDocument output = processor.execute(input);
String hash = output.getFieldValue(DEFAULT_TARGET, String.class);
assertThat(hash, equalTo("external"));
Expand Down Expand Up @@ -195,7 +196,7 @@ private void testNetworkDirectionProcessor(
config
);

IngestDocument input = new IngestDocument(source, Map.of());
IngestDocument input = TestIngestDocument.ofSourceAndMetadata(source);
IngestDocument output = processor.execute(input);

String hash = output.getFieldValue(DEFAULT_TARGET, String.class, ignoreMissing);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
package org.elasticsearch.ingest.common;

import org.elasticsearch.ingest.IngestDocument;
import org.elasticsearch.ingest.TestIngestDocument;
import org.elasticsearch.test.ESTestCase;

import java.util.HashMap;
Expand Down Expand Up @@ -74,7 +75,7 @@ public void testUseRoot() throws Exception {

var processor = new RegisteredDomainProcessor(null, null, "domain", "", false);

IngestDocument input = new IngestDocument(source, Map.of());
IngestDocument input = TestIngestDocument.ofSourceAndMetadata(source);
IngestDocument output = processor.execute(input);

String domain = output.getFieldValue(domainField, String.class);
Expand Down Expand Up @@ -125,7 +126,7 @@ private void testRegisteredDomainProcessor(

var processor = new RegisteredDomainProcessor(null, null, "domain", "url", ignoreMissing);

IngestDocument input = new IngestDocument(source, Map.of());
IngestDocument input = TestIngestDocument.ofSourceAndMetadata(source);
IngestDocument output = processor.execute(input);

String domain = output.getFieldValue(domainField, String.class, expectedDomain == null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.elasticsearch.ingest.IngestDocument;
import org.elasticsearch.ingest.Processor;
import org.elasticsearch.ingest.RandomDocumentPicks;
import org.elasticsearch.ingest.TestIngestDocument;
import org.elasticsearch.ingest.TestTemplateService;
import org.elasticsearch.test.ESTestCase;

Expand Down Expand Up @@ -147,7 +148,7 @@ public Object put(String key, Object value) {
};
source.put("list", Collections.singletonList("item"));

IngestDocument ingestDocument = new IngestDocument(source, Collections.emptyMap());
IngestDocument ingestDocument = TestIngestDocument.ofSourceAndMetadata(source);
Processor processor = createRenameProcessor("list", "new_field", false);
try {
processor.execute(ingestDocument);
Expand All @@ -171,7 +172,7 @@ public Object remove(Object key) {
};
source.put("list", Collections.singletonList("item"));

IngestDocument ingestDocument = new IngestDocument(source, Collections.emptyMap());
IngestDocument ingestDocument = TestIngestDocument.ofSourceAndMetadata(source);
Processor processor = createRenameProcessor("list", "new_field", false);
try {
processor.execute(ingestDocument);
Expand All @@ -186,7 +187,7 @@ public Object remove(Object key) {
public void testRenameLeafIntoBranch() throws Exception {
Map<String, Object> source = new HashMap<>();
source.put("foo", "bar");
IngestDocument ingestDocument = new IngestDocument(source, Collections.emptyMap());
IngestDocument ingestDocument = TestIngestDocument.ofSourceAndMetadata(source);
Processor processor1 = createRenameProcessor("foo", "foo.bar", false);
processor1.execute(ingestDocument);
assertThat(ingestDocument.getFieldValue("foo", Map.class), equalTo(Collections.singletonMap("bar", "bar")));
Expand Down

0 comments on commit e0ced8a

Please sign in to comment.