|
34 | 34 | import static org.hamcrest.Matchers.equalTo;
|
35 | 35 | import static org.hamcrest.Matchers.greaterThanOrEqualTo;
|
36 | 36 | import static org.hamcrest.Matchers.instanceOf;
|
| 37 | +import static org.hamcrest.Matchers.is; |
37 | 38 | import static org.hamcrest.Matchers.lessThanOrEqualTo;
|
38 | 39 | import static org.hamcrest.Matchers.not;
|
39 | 40 | import static org.hamcrest.Matchers.notNullValue;
|
@@ -103,6 +104,25 @@ public void testSimpleGetFieldValue() {
|
103 | 104 | assertThat(document.getFieldValue("_source._ingest.timestamp", ZonedDateTime.class), equalTo(BOGUS_TIMESTAMP));
|
104 | 105 | }
|
105 | 106 |
|
| 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 | + |
106 | 126 | public void testGetSourceObject() {
|
107 | 127 | try {
|
108 | 128 | document.getFieldValue("_source", Object.class);
|
@@ -152,6 +172,22 @@ public void testSimpleGetFieldValueTypeMismatch() {
|
152 | 172 | }
|
153 | 173 | }
|
154 | 174 |
|
| 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 | + |
155 | 191 | public void testNestedGetFieldValue() {
|
156 | 192 | assertThat(document.getFieldValue("fizz.buzz", String.class), equalTo("hello world"));
|
157 | 193 | assertThat(document.getFieldValue("fizz.1", String.class), equalTo("bar"));
|
|
0 commit comments