Skip to content

Commit 3ed83bb

Browse files
committed
Add some more IngestDocument tests
1 parent 1c0bb76 commit 3ed83bb

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

server/src/test/java/org/elasticsearch/ingest/IngestDocumentTests.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import static org.hamcrest.Matchers.equalTo;
3535
import static org.hamcrest.Matchers.greaterThanOrEqualTo;
3636
import static org.hamcrest.Matchers.instanceOf;
37+
import static org.hamcrest.Matchers.is;
3738
import static org.hamcrest.Matchers.lessThanOrEqualTo;
3839
import static org.hamcrest.Matchers.not;
3940
import static org.hamcrest.Matchers.notNullValue;
@@ -103,6 +104,25 @@ public void testSimpleGetFieldValue() {
103104
assertThat(document.getFieldValue("_source._ingest.timestamp", ZonedDateTime.class), equalTo(BOGUS_TIMESTAMP));
104105
}
105106

107+
public void testGetFieldValueIgnoreMissing() {
108+
assertThat(document.getFieldValue("foo", String.class, randomBoolean()), equalTo("bar"));
109+
assertThat(document.getFieldValue("int", Integer.class, randomBoolean()), equalTo(123));
110+
111+
// if ignoreMissing is true, we just return nulls for values that aren't found
112+
assertThat(document.getFieldValue("nonsense", Integer.class, true), nullValue());
113+
assertThat(document.getFieldValue("some.nonsense", Integer.class, true), nullValue());
114+
assertThat(document.getFieldValue("fizz.some.nonsense", Integer.class, true), nullValue());
115+
116+
// if ignoreMissing is false, we throw an exception for values that aren't found
117+
IllegalArgumentException e;
118+
e = expectThrows(IllegalArgumentException.class, () -> document.getFieldValue("fizz.some.nonsense", Integer.class, false));
119+
assertThat(e.getMessage(), is("field [some] not present as part of path [fizz.some.nonsense]"));
120+
121+
// if ignoreMissing is true, and the object is present-and-of-the-wrong-type, then we also throw an exception
122+
e = expectThrows(IllegalArgumentException.class, () -> document.getFieldValue("int", Boolean.class, true));
123+
assertThat(e.getMessage(), is("field [int] of type [java.lang.Integer] cannot be cast to [java.lang.Boolean]"));
124+
}
125+
106126
public void testGetSourceObject() {
107127
try {
108128
document.getFieldValue("_source", Object.class);
@@ -152,6 +172,22 @@ public void testSimpleGetFieldValueTypeMismatch() {
152172
}
153173
}
154174

175+
public void testSimpleGetFieldValueIgnoreMissingAndTypeMismatch() {
176+
try {
177+
document.getFieldValue("int", String.class, randomBoolean());
178+
fail("getFieldValue should have failed");
179+
} catch (IllegalArgumentException e) {
180+
assertThat(e.getMessage(), equalTo("field [int] of type [java.lang.Integer] cannot be cast to [java.lang.String]"));
181+
}
182+
183+
try {
184+
document.getFieldValue("foo", Integer.class, randomBoolean());
185+
fail("getFieldValue should have failed");
186+
} catch (IllegalArgumentException e) {
187+
assertThat(e.getMessage(), equalTo("field [foo] of type [java.lang.String] cannot be cast to [java.lang.Integer]"));
188+
}
189+
}
190+
155191
public void testNestedGetFieldValue() {
156192
assertThat(document.getFieldValue("fizz.buzz", String.class), equalTo("hello world"));
157193
assertThat(document.getFieldValue("fizz.1", String.class), equalTo("bar"));

0 commit comments

Comments
 (0)