|
38 | 38 | import java.util.Arrays; |
39 | 39 | import java.util.Collections; |
40 | 40 | import java.util.List; |
| 41 | +import java.util.Map; |
41 | 42 | import java.util.function.Supplier; |
42 | 43 |
|
43 | 44 | import static org.elasticsearch.common.xcontent.XContentHelper.toXContent; |
@@ -101,29 +102,43 @@ private static DocumentField mutateDocumentField(DocumentField documentField) { |
101 | 102 | } |
102 | 103 |
|
103 | 104 | public static Tuple<DocumentField, DocumentField> randomDocumentField(XContentType xContentType) { |
104 | | - if (randomBoolean()) { |
105 | | - String metaField = randomValueOtherThanMany(field -> field.equals(TypeFieldMapper.NAME) |
106 | | - || field.equals(IndexFieldMapper.NAME) || field.equals(IdFieldMapper.NAME), |
107 | | - () -> randomFrom(MapperService.getAllMetaFields())); |
108 | | - DocumentField documentField; |
109 | | - if (metaField.equals(IgnoredFieldMapper.NAME)) { |
110 | | - int numValues = randomIntBetween(1, 3); |
111 | | - List<Object> ignoredFields = new ArrayList<>(numValues); |
112 | | - for (int i = 0; i < numValues; i++) { |
113 | | - ignoredFields.add(randomAlphaOfLengthBetween(3, 10)); |
| 105 | + switch(randomIntBetween(0, 3)) { |
| 106 | + case 0: |
| 107 | + String metaField = randomValueOtherThanMany(field -> field.equals(TypeFieldMapper.NAME) |
| 108 | + || field.equals(IndexFieldMapper.NAME) || field.equals(IdFieldMapper.NAME), |
| 109 | + () -> randomFrom(MapperService.getAllMetaFields())); |
| 110 | + DocumentField documentField; |
| 111 | + if (metaField.equals(IgnoredFieldMapper.NAME)) { |
| 112 | + int numValues = randomIntBetween(1, 3); |
| 113 | + List<Object> ignoredFields = new ArrayList<>(numValues); |
| 114 | + for (int i = 0; i < numValues; i++) { |
| 115 | + ignoredFields.add(randomAlphaOfLengthBetween(3, 10)); |
| 116 | + } |
| 117 | + documentField = new DocumentField(metaField, ignoredFields); |
| 118 | + } else { |
| 119 | + //meta fields are single value only, besides _ignored |
| 120 | + documentField = new DocumentField(metaField, Collections.singletonList(randomAlphaOfLengthBetween(3, 10))); |
114 | 121 | } |
115 | | - documentField = new DocumentField(metaField, ignoredFields); |
116 | | - } else { |
117 | | - //meta fields are single value only, besides _ignored |
118 | | - documentField = new DocumentField(metaField, Collections.singletonList(randomAlphaOfLengthBetween(3, 10))); |
119 | | - } |
120 | | - return Tuple.tuple(documentField, documentField); |
121 | | - } else { |
122 | | - String fieldName = randomAlphaOfLengthBetween(3, 10); |
123 | | - Tuple<List<Object>, List<Object>> tuple = RandomObjects.randomStoredFieldValues(random(), xContentType); |
124 | | - DocumentField input = new DocumentField(fieldName, tuple.v1()); |
125 | | - DocumentField expected = new DocumentField(fieldName, tuple.v2()); |
126 | | - return Tuple.tuple(input, expected); |
| 122 | + return Tuple.tuple(documentField, documentField); |
| 123 | + case 1: |
| 124 | + String fieldName = randomAlphaOfLengthBetween(3, 10); |
| 125 | + Tuple<List<Object>, List<Object>> tuple = RandomObjects.randomStoredFieldValues(random(), xContentType); |
| 126 | + DocumentField input = new DocumentField(fieldName, tuple.v1()); |
| 127 | + DocumentField expected = new DocumentField(fieldName, tuple.v2()); |
| 128 | + return Tuple.tuple(input, expected); |
| 129 | + case 2: |
| 130 | + List<Object> listValues = randomList(1, 5, () -> randomList(1, 5, ESTestCase::randomInt)); |
| 131 | + DocumentField listField = new DocumentField(randomAlphaOfLength(5), listValues); |
| 132 | + return Tuple.tuple(listField, listField); |
| 133 | + case 3: |
| 134 | + List<Object> objectValues = randomList(1, 5, () -> |
| 135 | + Map.of(randomAlphaOfLength(5), randomInt(), |
| 136 | + randomAlphaOfLength(5), randomBoolean(), |
| 137 | + randomAlphaOfLength(5), randomAlphaOfLength(10))); |
| 138 | + DocumentField objectField = new DocumentField(randomAlphaOfLength(5), objectValues); |
| 139 | + return Tuple.tuple(objectField, objectField); |
| 140 | + default: |
| 141 | + throw new IllegalStateException(); |
127 | 142 | } |
128 | 143 | } |
129 | 144 | } |
0 commit comments