diff --git a/server/src/test/java/org/elasticsearch/index/mapper/DateFieldMapperTests.java b/server/src/test/java/org/elasticsearch/index/mapper/DateFieldMapperTests.java index 9ba7e0d33db..73ec5cc5157 100644 --- a/server/src/test/java/org/elasticsearch/index/mapper/DateFieldMapperTests.java +++ b/server/src/test/java/org/elasticsearch/index/mapper/DateFieldMapperTests.java @@ -24,67 +24,34 @@ import org.apache.lucene.index.IndexableField; import org.elasticsearch.Version; import org.elasticsearch.bootstrap.JavaVersion; import org.elasticsearch.cluster.metadata.IndexMetadata; -import org.elasticsearch.common.Strings; -import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.collect.List; -import org.elasticsearch.common.compress.CompressedXContent; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.time.DateFormatter; -import org.elasticsearch.common.xcontent.XContentFactory; -import org.elasticsearch.common.xcontent.XContentType; -import org.elasticsearch.index.IndexService; +import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.index.mapper.DateFieldMapper.Resolution; -import org.elasticsearch.index.mapper.MapperService.MergeReason; import org.elasticsearch.index.termvectors.TermVectorsService; -import org.elasticsearch.plugins.Plugin; -import org.elasticsearch.test.ESSingleNodeTestCase; -import org.elasticsearch.test.InternalSettingsPlugin; -import org.junit.Before; import java.io.IOException; import java.time.ZoneId; import java.time.ZoneOffset; import java.time.ZonedDateTime; -import java.util.Collection; -import java.util.Collections; import java.util.HashMap; import java.util.Map; -import static org.elasticsearch.index.mapper.FieldMapperTestCase.fetchSourceValue; import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.notNullValue; -public class DateFieldMapperTests extends ESSingleNodeTestCase { - - IndexService indexService; - DocumentMapperParser parser; - - @Before - public void setup() { - indexService = createIndex("test"); - parser = indexService.mapperService().documentMapperParser(); - } +public class DateFieldMapperTests extends MapperTestCase { @Override - protected Collection> getPlugins() { - return pluginList(InternalSettingsPlugin.class); + protected void minimalMapping(XContentBuilder b) throws IOException { + b.field("type", "date"); } public void testDefaults() throws Exception { - String mapping = Strings.toString(XContentFactory.jsonBuilder().startObject().startObject("type") - .startObject("properties").startObject("field").field("type", "date").endObject().endObject() - .endObject().endObject()); - DocumentMapper mapper = parser.parse("type", new CompressedXContent(mapping)); - - assertEquals(mapping, mapper.mappingSource().toString()); - - ParsedDocument doc = mapper.parse(new SourceToParse("test", "type", "1", BytesReference - .bytes(XContentFactory.jsonBuilder() - .startObject() - .field("field", "2016-03-11") - .endObject()), - XContentType.JSON)); + DocumentMapper mapper = createDocumentMapper(fieldMapping(this::minimalMapping)); + ParsedDocument doc = mapper.parse(source(b -> b.field("field", "2016-03-11"))); IndexableField[] fields = doc.rootDoc().getFields("field"); assertEquals(2, fields.length); @@ -100,20 +67,12 @@ public class DateFieldMapperTests extends ESSingleNodeTestCase { } public void testNotIndexed() throws Exception { - String mapping = Strings.toString(XContentFactory.jsonBuilder().startObject().startObject("type") - .startObject("properties").startObject("field").field("type", "date").field("index", false).endObject().endObject() - .endObject().endObject()); - DocumentMapper mapper = parser.parse("type", new CompressedXContent(mapping)); + DocumentMapper mapper = createDocumentMapper(fieldMapping(b -> b + .field("type", "date") + .field("index", false))); - assertEquals(mapping, mapper.mappingSource().toString()); - - ParsedDocument doc = mapper.parse(new SourceToParse("test", "type", "1", BytesReference - .bytes(XContentFactory.jsonBuilder() - .startObject() - .field("field", "2016-03-11") - .endObject()), - XContentType.JSON)); + ParsedDocument doc = mapper.parse(source(b -> b.field("field", "2016-03-11"))); IndexableField[] fields = doc.rootDoc().getFields("field"); assertEquals(1, fields.length); @@ -122,20 +81,12 @@ public class DateFieldMapperTests extends ESSingleNodeTestCase { } public void testNoDocValues() throws Exception { - String mapping = Strings.toString(XContentFactory.jsonBuilder().startObject().startObject("type") - .startObject("properties").startObject("field").field("type", "date").field("doc_values", false).endObject().endObject() - .endObject().endObject()); - DocumentMapper mapper = parser.parse("type", new CompressedXContent(mapping)); + DocumentMapper mapper = createDocumentMapper(fieldMapping(b -> b + .field("type", "date") + .field("doc_values", false))); - assertEquals(mapping, mapper.mappingSource().toString()); - - ParsedDocument doc = mapper.parse(new SourceToParse("test", "type", "1", BytesReference - .bytes(XContentFactory.jsonBuilder() - .startObject() - .field("field", "2016-03-11") - .endObject()), - XContentType.JSON)); + ParsedDocument doc = mapper.parse(source(b -> b.field("field", "2016-03-11"))); IndexableField[] fields = doc.rootDoc().getFields("field"); assertEquals(1, fields.length); @@ -144,20 +95,12 @@ public class DateFieldMapperTests extends ESSingleNodeTestCase { } public void testStore() throws Exception { - String mapping = Strings.toString(XContentFactory.jsonBuilder().startObject().startObject("type") - .startObject("properties").startObject("field").field("type", "date").field("store", true).endObject().endObject() - .endObject().endObject()); - DocumentMapper mapper = parser.parse("type", new CompressedXContent(mapping)); + DocumentMapper mapper = createDocumentMapper(fieldMapping(b -> b + .field("type", "date") + .field("store", true))); - assertEquals(mapping, mapper.mappingSource().toString()); - - ParsedDocument doc = mapper.parse(new SourceToParse("test", "type", "1", BytesReference - .bytes(XContentFactory.jsonBuilder() - .startObject() - .field("field", "2016-03-11") - .endObject()), - XContentType.JSON)); + ParsedDocument doc = mapper.parse(source(b -> b.field("field", "2016-03-11"))); IndexableField[] fields = doc.rootDoc().getFields("field"); assertEquals(3, fields.length); @@ -171,46 +114,28 @@ public class DateFieldMapperTests extends ESSingleNodeTestCase { } public void testIgnoreMalformed() throws IOException { - testIgnoreMalfomedForValue("2016-03-99", + testIgnoreMalformedForValue("2016-03-99", "failed to parse date field [2016-03-99] with format [strict_date_optional_time||epoch_millis]"); - testIgnoreMalfomedForValue("-2147483648", + testIgnoreMalformedForValue("-2147483648", "Invalid value for Year (valid values -999999999 - 999999999): -2147483648"); - testIgnoreMalfomedForValue("-522000000", "long overflow"); + testIgnoreMalformedForValue("-522000000", "long overflow"); } - private void testIgnoreMalfomedForValue(String value, String expectedCause) throws IOException { - String mapping = Strings.toString(XContentFactory.jsonBuilder().startObject().startObject("type") - .startObject("properties").startObject("field").field("type", "date").endObject().endObject() - .endObject().endObject()); + private void testIgnoreMalformedForValue(String value, String expectedCause) throws IOException { - DocumentMapper mapper = parser.parse("type", new CompressedXContent(mapping)); + DocumentMapper mapper = createDocumentMapper(fieldMapping(this::minimalMapping)); - assertEquals(mapping, mapper.mappingSource().toString()); - - ThrowingRunnable runnable = () -> mapper.parse(new SourceToParse("test", "type", "1", BytesReference - .bytes(XContentFactory.jsonBuilder() - .startObject() - .field("field", value) - .endObject()), - XContentType.JSON)); - MapperParsingException e = expectThrows(MapperParsingException.class, runnable); + MapperParsingException e = expectThrows(MapperParsingException.class, + () -> mapper.parse(source(b -> b.field("field", value)))); assertThat(e.getMessage(), containsString("failed to parse field [field] of type [date]")); assertThat(e.getMessage(), containsString("Preview of field's value: '" + value + "'")); assertThat(e.getCause().getMessage(), containsString(expectedCause)); - mapping = Strings.toString(XContentFactory.jsonBuilder().startObject().startObject("type") - .startObject("properties").startObject("field").field("type", "date") - .field("ignore_malformed", true).endObject().endObject() - .endObject().endObject()); + DocumentMapper mapper2 = createDocumentMapper(fieldMapping(b -> b + .field("type", "date") + .field("ignore_malformed", true))); - DocumentMapper mapper2 = parser.parse("type", new CompressedXContent(mapping)); - - ParsedDocument doc = mapper2.parse(new SourceToParse("test", "type", "1", BytesReference - .bytes(XContentFactory.jsonBuilder() - .startObject() - .field("field", value) - .endObject()), - XContentType.JSON)); + ParsedDocument doc = mapper2.parse(source(b -> b.field("field", value))); IndexableField[] fields = doc.rootDoc().getFields("field"); assertEquals(0, fields.length); @@ -218,21 +143,12 @@ public class DateFieldMapperTests extends ESSingleNodeTestCase { } public void testChangeFormat() throws IOException { - String mapping = Strings.toString(XContentFactory.jsonBuilder().startObject().startObject("type") - .startObject("properties").startObject("field").field("type", "date") - .field("format", "epoch_second").endObject().endObject() - .endObject().endObject()); - DocumentMapper mapper = parser.parse("type", new CompressedXContent(mapping)); + DocumentMapper mapper = createDocumentMapper(fieldMapping(b -> b + .field("type", "date") + .field("format", "epoch_second"))); - assertEquals(mapping, mapper.mappingSource().toString()); - - ParsedDocument doc = mapper.parse(new SourceToParse("test", "type", "1", BytesReference - .bytes(XContentFactory.jsonBuilder() - .startObject() - .field("field", 1457654400) - .endObject()), - XContentType.JSON)); + ParsedDocument doc = mapper.parse(source(b -> b.field("field", 1457654400))); IndexableField[] fields = doc.rootDoc().getFields("field"); assertEquals(2, fields.length); @@ -242,64 +158,27 @@ public class DateFieldMapperTests extends ESSingleNodeTestCase { public void testChangeLocale() throws IOException { assumeTrue("need java 9 for testing ",JavaVersion.current().compareTo(JavaVersion.parse("9")) >= 0); - String mapping = Strings.toString(XContentFactory.jsonBuilder().startObject().startObject("type") - .startObject("properties").startObject("field").field("type", "date") - .field("format", "E, d MMM yyyy HH:mm:ss Z") - .field("locale", "de") - .endObject().endObject().endObject().endObject()); - DocumentMapper mapper = parser.parse("type", new CompressedXContent(mapping)); + DocumentMapper mapper = createDocumentMapper(fieldMapping(b -> b + .field("type", "date") + .field("format", "E, d MMM yyyy HH:mm:ss Z") + .field("locale", "de"))); - assertEquals(mapping, mapper.mappingSource().toString()); - - mapper.parse(new SourceToParse("test", "type", "1", BytesReference - .bytes(XContentFactory.jsonBuilder() - .startObject() - .field("field", "Mi, 06 Dez 2000 02:55:00 -0800") - .endObject()), - XContentType.JSON)); + mapper.parse(source(b -> b.field("field", "Mi, 06 Dez 2000 02:55:00 -0800"))); } public void testNullValue() throws IOException { - String mapping = Strings.toString(XContentFactory.jsonBuilder().startObject() - .startObject("type") - .startObject("properties") - .startObject("field") - .field("type", "date") - .endObject() - .endObject() - .endObject().endObject()); - DocumentMapper mapper = parser.parse("type", new CompressedXContent(mapping)); - assertEquals(mapping, mapper.mappingSource().toString()); + DocumentMapper mapper = createDocumentMapper(fieldMapping(this::minimalMapping)); - ParsedDocument doc = mapper.parse(new SourceToParse("test", "type", "1", BytesReference - .bytes(XContentFactory.jsonBuilder() - .startObject() - .nullField("field") - .endObject()), - XContentType.JSON)); + ParsedDocument doc = mapper.parse(source(b -> b.nullField("field"))); assertArrayEquals(new IndexableField[0], doc.rootDoc().getFields("field")); - mapping = Strings.toString(XContentFactory.jsonBuilder().startObject() - .startObject("type") - .startObject("properties") - .startObject("field") - .field("type", "date") - .field("null_value", "2016-03-11") - .endObject() - .endObject() - .endObject().endObject()); + mapper = createDocumentMapper(fieldMapping(b -> b + .field("type", "date") + .field("null_value", "2016-03-11"))); - mapper = parser.parse("type", new CompressedXContent(mapping)); - assertEquals(mapping, mapper.mappingSource().toString()); - - doc = mapper.parse(new SourceToParse("test", "type", "1", BytesReference - .bytes(XContentFactory.jsonBuilder() - .startObject() - .nullField("field") - .endObject()), - XContentType.JSON)); + doc = mapper.parse(source(b -> b.nullField("field"))); IndexableField[] fields = doc.rootDoc().getFields("field"); assertEquals(2, fields.length); IndexableField pointField = fields[0]; @@ -313,59 +192,24 @@ public class DateFieldMapperTests extends ESSingleNodeTestCase { assertFalse(dvField.fieldType().stored()); } - public void testNullConfigValuesFail() throws MapperParsingException, IOException { - String mapping = Strings.toString(XContentFactory.jsonBuilder().startObject() - .startObject("type") - .startObject("properties") - .startObject("field") - .field("type", "date") - .field("format", (String) null) - .endObject() - .endObject() - .endObject().endObject()); - - Exception e = expectThrows(MapperParsingException.class, () -> parser.parse("type", new CompressedXContent(mapping))); - assertEquals("[format] on mapper [field] of type [date] must not have a [null] value", e.getMessage()); - } - - public void testEmptyName() throws IOException { - String mapping = Strings.toString(XContentFactory.jsonBuilder().startObject().startObject("type") - .startObject("properties").startObject("").field("type", "date") - .field("format", "epoch_second").endObject().endObject() - .endObject().endObject()); - - IllegalArgumentException e = expectThrows(IllegalArgumentException.class, - () -> parser.parse("type", new CompressedXContent(mapping)) - ); - assertThat(e.getMessage(), containsString("name cannot be empty string")); + public void testNullConfigValuesFail() { + Exception e = expectThrows(MapperParsingException.class, + () -> createDocumentMapper(fieldMapping(b -> b.field("type", "date").nullField("format")))); + assertThat(e.getMessage(), containsString("[format] on mapper [field] of type [date] must not have a [null] value")); } public void testTimeZoneParsing() throws Exception { final String timeZonePattern = "yyyy-MM-dd" + randomFrom("XXX", "[XXX]", "'['XXX']'"); - String mapping = Strings.toString(XContentFactory.jsonBuilder().startObject() - .startObject("type") - .startObject("properties") - .startObject("field") - .field("type", "date") - .field("format", timeZonePattern) - .endObject() - .endObject() - .endObject().endObject()); - - DocumentMapper mapper = parser.parse("type", new CompressedXContent(mapping)); - assertEquals(mapping, mapper.mappingSource().toString()); + DocumentMapper mapper = createDocumentMapper(fieldMapping(b -> b + .field("type", "date") + .field("format", timeZonePattern))); DateFormatter formatter = DateFormatter.forPattern(timeZonePattern); final ZoneId randomTimeZone = randomBoolean() ? ZoneId.of(randomFrom("UTC", "CET")) : randomZone(); final ZonedDateTime randomDate = ZonedDateTime.of(2016, 3, 11, 0, 0, 0, 0, randomTimeZone); - ParsedDocument doc = mapper.parse(new SourceToParse("test", "type", "1", BytesReference - .bytes(XContentFactory.jsonBuilder() - .startObject() - .field("field", formatter.format(randomDate)) - .endObject()), - XContentType.JSON)); + ParsedDocument doc = mapper.parse(source(b -> b.field("field", formatter.format(randomDate)))); IndexableField[] fields = doc.rootDoc().getFields("field"); assertEquals(2, fields.length); @@ -375,86 +219,31 @@ public class DateFieldMapperTests extends ESSingleNodeTestCase { } public void testMergeDate() throws IOException { - String initMapping = Strings.toString(XContentFactory.jsonBuilder().startObject().startObject("movie") - .startObject("properties") - .startObject("release_date").field("type", "date").field("format", "yyyy/MM/dd").endObject() - .endObject().endObject().endObject()); - indexService.mapperService().merge("movie", new CompressedXContent(initMapping), - MapperService.MergeReason.MAPPING_UPDATE); + MapperService mapperService = createMapperService(fieldMapping(b -> b + .field("type", "date") + .field("format", "yyyy/MM/dd"))); - assertThat(indexService.mapperService().fieldType("release_date"), notNullValue()); - assertFalse(indexService.mapperService().fieldType("release_date") + assertThat(mapperService.fieldType("field"), notNullValue()); + assertFalse(mapperService.fieldType("field") .getTextSearchInfo().isStored()); - String updateFormatMapping = Strings.toString(XContentFactory.jsonBuilder().startObject().startObject("movie") - .startObject("properties") - .startObject("release_date").field("type", "date").field("format", "epoch_millis").endObject() - .endObject().endObject().endObject()); - Exception e = expectThrows(IllegalArgumentException.class, - () -> indexService.mapperService().merge("movie", new CompressedXContent(updateFormatMapping), - MapperService.MergeReason.MAPPING_UPDATE)); + () -> merge(mapperService, fieldMapping(b -> b.field("type", "date").field("format", "epoch_millis")))); assertThat(e.getMessage(), containsString("parameter [format] from [yyyy/MM/dd] to [epoch_millis]")); } public void testMergeText() throws Exception { - String mapping = Strings.toString(XContentFactory.jsonBuilder().startObject().startObject("_doc") - .startObject("properties").startObject("date").field("type", "date").endObject() - .endObject().endObject().endObject()); - DocumentMapper mapper = indexService.mapperService().parse("_doc", new CompressedXContent(mapping), false); - - String mappingUpdate = Strings.toString(XContentFactory.jsonBuilder().startObject().startObject("_doc") - .startObject("properties").startObject("date").field("type", "text").endObject() - .endObject().endObject().endObject()); - DocumentMapper update = indexService.mapperService().parse("_doc", new CompressedXContent(mappingUpdate), false); - + MapperService mapperService = createMapperService(fieldMapping(this::minimalMapping)); IllegalArgumentException e = expectThrows(IllegalArgumentException.class, - () -> mapper.merge(update.mapping(), MergeReason.MAPPING_UPDATE)); - assertEquals("mapper [date] cannot be changed from type [date] to [text]", e.getMessage()); + () -> merge(mapperService, fieldMapping(b -> b.field("type", "text")))); + assertEquals("mapper [field] cannot be changed from type [date] to [text]", e.getMessage()); } - public void testIllegalFormatField() throws Exception { - String mapping = Strings.toString(XContentFactory.jsonBuilder() - .startObject() - .startObject("type") - .startObject("properties") - .startObject("field") - .field("type", "date") - .array("format", "test_format") - .endObject() - .endObject() - .endObject() - .endObject()); - - IllegalArgumentException e = expectThrows(IllegalArgumentException.class, - () -> parser.parse("type", new CompressedXContent(mapping))); - assertEquals("Invalid format: [[test_format]]: Unknown pattern letter: t", e.getMessage()); - } - - public void testMeta() throws Exception { - String mapping = Strings.toString(XContentFactory.jsonBuilder().startObject().startObject("_doc") - .startObject("properties").startObject("field").field("type", "date") - .field("meta", Collections.singletonMap("foo", "bar")) - .endObject().endObject().endObject().endObject()); - - DocumentMapper mapper = indexService.mapperService().merge("_doc", - new CompressedXContent(mapping), MergeReason.MAPPING_UPDATE); - assertEquals(mapping, mapper.mappingSource().toString()); - - String mapping2 = Strings.toString(XContentFactory.jsonBuilder().startObject().startObject("_doc") - .startObject("properties").startObject("field").field("type", "date") - .endObject().endObject().endObject().endObject()); - mapper = indexService.mapperService().merge("_doc", - new CompressedXContent(mapping2), MergeReason.MAPPING_UPDATE); - assertEquals(mapping2, mapper.mappingSource().toString()); - - String mapping3 = Strings.toString(XContentFactory.jsonBuilder().startObject().startObject("_doc") - .startObject("properties").startObject("field").field("type", "date") - .field("meta", Collections.singletonMap("baz", "quux")) - .endObject().endObject().endObject().endObject()); - mapper = indexService.mapperService().merge("_doc", - new CompressedXContent(mapping3), MergeReason.MAPPING_UPDATE); - assertEquals(mapping3, mapper.mappingSource().toString()); + public void testIllegalFormatField() { + MapperParsingException e = expectThrows(MapperParsingException.class, () -> createDocumentMapper(fieldMapping(b -> b + .field("type", "date") + .field("format", "test_format")))); + assertThat(e.getMessage(), containsString("Invalid format: [test_format]: Unknown pattern letter: t")); } public void testFetchSourceValue() {