|
21 | 21 | import java.util.Collections; |
22 | 22 | import java.util.HashMap; |
23 | 23 | import java.util.List; |
| 24 | +import java.util.Locale; |
24 | 25 | import java.util.Map; |
| 26 | +import java.util.stream.Collectors; |
25 | 27 |
|
26 | 28 | import static org.hamcrest.Matchers.equalTo; |
27 | 29 |
|
@@ -214,13 +216,38 @@ public void testMatchTypeTemplate() { |
214 | 216 | } |
215 | 217 |
|
216 | 218 | public void testMatchTypeTemplateRuntime() { |
217 | | - Map<String, Object> templateDef = new HashMap<>(); |
218 | | - templateDef.put("match_mapping_type", "string"); |
219 | | - templateDef.put("runtime", Collections.emptyMap()); |
220 | | - DynamicTemplate template = DynamicTemplate.parse("my_template", templateDef, Version.CURRENT); |
221 | | - assertTrue(template.match(null, "a.b", "b", XContentFieldType.STRING)); |
222 | | - assertFalse(template.match(null, "a.b", "b", XContentFieldType.BOOLEAN)); |
223 | | - assertTrue(template.isRuntimeMapping()); |
| 219 | + List<XContentFieldType> runtimeFieldTypes = |
| 220 | + Arrays.stream(XContentFieldType.values()).filter(XContentFieldType::supportsRuntimeField).collect(Collectors.toList()); |
| 221 | + for (XContentFieldType runtimeFieldType : runtimeFieldTypes) { |
| 222 | + Map<String, Object> templateDef = new HashMap<>(); |
| 223 | + templateDef.put("match_mapping_type", runtimeFieldType.name().toLowerCase(Locale.ROOT)); |
| 224 | + templateDef.put("runtime", Collections.emptyMap()); |
| 225 | + DynamicTemplate template = DynamicTemplate.parse("my_template", templateDef, Version.CURRENT); |
| 226 | + assertTrue(template.isRuntimeMapping()); |
| 227 | + for (XContentFieldType xContentFieldType : XContentFieldType.values()) { |
| 228 | + if (xContentFieldType == runtimeFieldType) { |
| 229 | + assertTrue(template.match(null, "a.b", "b", xContentFieldType)); |
| 230 | + } else { |
| 231 | + assertFalse(template.match(null, "a.b", "b", xContentFieldType)); |
| 232 | + } |
| 233 | + } |
| 234 | + } |
| 235 | + } |
| 236 | + |
| 237 | + public void testMatchTypeTemplateRuntimeUnsupported() { |
| 238 | + List<XContentFieldType> xContentFieldTypes = Arrays.stream(XContentFieldType.values()) |
| 239 | + .filter(xContentFieldType -> xContentFieldType.supportsRuntimeField() == false).collect(Collectors.toList()); |
| 240 | + for (XContentFieldType xContentFieldType : xContentFieldTypes) { |
| 241 | + String fieldType = xContentFieldType.name().toLowerCase(Locale.ROOT); |
| 242 | + Map<String, Object> templateDef = new HashMap<>(); |
| 243 | + templateDef.put("match_mapping_type", fieldType); |
| 244 | + templateDef.put("runtime", Collections.emptyMap()); |
| 245 | + MapperParsingException e = expectThrows(MapperParsingException.class, () -> DynamicTemplate.parse("my_template", templateDef, |
| 246 | + Version.CURRENT)); |
| 247 | + assertEquals("Dynamic template [my_template] defines a runtime field but type [" + |
| 248 | + fieldType + "] is not supported as runtime field", |
| 249 | + e.getMessage()); |
| 250 | + } |
224 | 251 | } |
225 | 252 |
|
226 | 253 | public void testSupportedMatchMappingTypesRuntime() { |
|
0 commit comments