|
43 | 43 | import static org.hamcrest.Matchers.equalTo;
|
44 | 44 | import static org.hamcrest.Matchers.hasSize;
|
45 | 45 | import static org.hamcrest.Matchers.instanceOf;
|
| 46 | +import static org.hamcrest.Matchers.nullValue; |
46 | 47 |
|
47 | 48 | public class ObjectParserTests extends ESTestCase {
|
48 | 49 |
|
@@ -757,6 +758,89 @@ public void testConsumeUnknownFields() throws IOException {
|
757 | 758 | assertThat(o.fields.get("test_nested"), instanceOf(Map.class));
|
758 | 759 | }
|
759 | 760 |
|
| 761 | + public void testRequiredFieldSet() throws IOException { |
| 762 | + class TestStruct { |
| 763 | + private Long a; |
| 764 | + private Long b; |
| 765 | + |
| 766 | + private void setA(long value) { |
| 767 | + this.a = value; |
| 768 | + } |
| 769 | + |
| 770 | + private void setB(long value) { |
| 771 | + this.b = value; |
| 772 | + } |
| 773 | + } |
| 774 | + |
| 775 | + XContentParser parser = createParser(JsonXContent.jsonXContent, "{\"a\": \"123\"}"); |
| 776 | + ObjectParser<TestStruct, Void> objectParser = new ObjectParser<>("foo", true, TestStruct::new); |
| 777 | + objectParser.declareLong(TestStruct::setA, new ParseField("a")); |
| 778 | + objectParser.declareLong(TestStruct::setB, new ParseField("b")); |
| 779 | + objectParser.declareRequiredFieldSet(new String[]{"a", "b"}); |
| 780 | + |
| 781 | + TestStruct obj = objectParser.apply(parser, null); |
| 782 | + assertThat(obj.a, equalTo(123L)); |
| 783 | + assertThat(obj.b, nullValue()); |
| 784 | + |
| 785 | + parser = createParser(JsonXContent.jsonXContent, "{\"b\": \"123\"}"); |
| 786 | + objectParser = new ObjectParser<>("foo", true, TestStruct::new); |
| 787 | + objectParser.declareLong(TestStruct::setA, new ParseField("a")); |
| 788 | + objectParser.declareLong(TestStruct::setB, new ParseField("b")); |
| 789 | + objectParser.declareRequiredFieldSet(new String[]{"a", "b"}); |
| 790 | + |
| 791 | + obj = objectParser.apply(parser, null); |
| 792 | + assertThat(obj.a, nullValue()); |
| 793 | + assertThat(obj.b, equalTo(123L)); |
| 794 | + |
| 795 | + parser = createParser(JsonXContent.jsonXContent, "{\"a\": \"123\", \"b\": \"456\"}"); |
| 796 | + objectParser = new ObjectParser<>("foo", true, TestStruct::new); |
| 797 | + objectParser.declareLong(TestStruct::setA, new ParseField("a")); |
| 798 | + objectParser.declareLong(TestStruct::setB, new ParseField("b")); |
| 799 | + objectParser.declareRequiredFieldSet(new String[]{"a", "b"}); |
| 800 | + |
| 801 | + obj = objectParser.apply(parser, null); |
| 802 | + assertThat(obj.a, equalTo(123L)); |
| 803 | + assertThat(obj.b, equalTo(456L)); |
| 804 | + } |
| 805 | + |
| 806 | + public void testMultipleRequiredFieldSet() throws IOException { |
| 807 | + class TestStruct { |
| 808 | + private Long a; |
| 809 | + private Long b; |
| 810 | + private Long c; |
| 811 | + private Long d; |
| 812 | + |
| 813 | + private void setA(long value) { |
| 814 | + this.a = value; |
| 815 | + } |
| 816 | + |
| 817 | + private void setB(long value) { |
| 818 | + this.b = value; |
| 819 | + } |
| 820 | + |
| 821 | + private void setC(long value) { |
| 822 | + this.c = value; |
| 823 | + } |
| 824 | + |
| 825 | + private void setD(long value) { |
| 826 | + this.d = value; |
| 827 | + } |
| 828 | + } |
| 829 | + |
| 830 | + XContentParser parser = createParser(JsonXContent.jsonXContent, "{\"unrelated\": \"123\"}"); |
| 831 | + ObjectParser<TestStruct, Void> objectParser = new ObjectParser<>("foo", true, TestStruct::new); |
| 832 | + objectParser.declareLong(TestStruct::setA, new ParseField("a")); |
| 833 | + objectParser.declareLong(TestStruct::setB, new ParseField("b")); |
| 834 | + objectParser.declareLong(TestStruct::setC, new ParseField("c")); |
| 835 | + objectParser.declareLong(TestStruct::setD, new ParseField("d")); |
| 836 | + objectParser.declareRequiredFieldSet(new String[]{"a", "b"}); |
| 837 | + objectParser.declareRequiredFieldSet(new String[]{"c", "d"}); |
| 838 | + |
| 839 | + IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> objectParser.apply(parser, null)); |
| 840 | + assertThat(e.getMessage(), equalTo("Required one of fields [a, b], but none were specified. " + |
| 841 | + "Required one of fields [c, d], but none were specified. ")); |
| 842 | + } |
| 843 | + |
760 | 844 | @Override
|
761 | 845 | protected NamedXContentRegistry xContentRegistry() {
|
762 | 846 | return new NamedXContentRegistry(Arrays.asList(
|
|
0 commit comments