Skip to content

Commit

Permalink
Convert some more tests to MapperServiceTestCase (#63997)
Browse files Browse the repository at this point in the history
Relates to #62774
  • Loading branch information
romseygeek committed Oct 21, 2020
1 parent 2c0145a commit 629e04b
Show file tree
Hide file tree
Showing 8 changed files with 305 additions and 496 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,29 +20,19 @@
package org.elasticsearch.index.mapper;

import org.apache.lucene.index.IndexableField;
import org.elasticsearch.common.bytes.BytesArray;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.IndexService;
import org.elasticsearch.index.mapper.ParseContext.Document;
import org.elasticsearch.test.ESSingleNodeTestCase;

import static org.elasticsearch.test.StreamsUtils.copyToBytesFromClasspath;
import static org.elasticsearch.test.StreamsUtils.copyToStringFromClasspath;
import static org.hamcrest.Matchers.equalTo;

public class GenericStoreDynamicTemplateTests extends ESSingleNodeTestCase {
public class GenericStoreDynamicTemplateTests extends MapperServiceTestCase {
public void testSimple() throws Exception {
String mapping = copyToStringFromClasspath("/org/elasticsearch/index/mapper/dynamictemplate/genericstore/test-mapping.json");
IndexService index = createIndex("test");
client().admin().indices().preparePutMapping("test").setType("person").setSource(mapping, XContentType.JSON).get();
MapperService mapperService = createMapperService("person", mapping);

MapperService mapperService = index.mapperService();

byte[] json = copyToBytesFromClasspath("/org/elasticsearch/index/mapper/dynamictemplate/genericstore/test-data.json");
ParsedDocument parsedDoc = mapperService.documentMapper().parse(
new SourceToParse("test", "person", "1", new BytesArray(json), XContentType.JSON));
client().admin().indices().preparePutMapping("test").setType("person")
.setSource(parsedDoc.dynamicMappingsUpdate().toString(), XContentType.JSON).get();
String json = copyToStringFromClasspath("/org/elasticsearch/index/mapper/dynamictemplate/genericstore/test-data.json");
ParsedDocument parsedDoc = mapperService.documentMapper().parse(source(json));
merge(mapperService, dynamicMapping(parsedDoc.dynamicMappingsUpdate()));
Document doc = parsedDoc.rootDoc();

IndexableField f = doc.getField("name");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,88 +21,54 @@

import org.apache.lucene.index.IndexOptions;
import org.apache.lucene.index.IndexableField;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.bytes.BytesArray;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.compress.CompressedXContent;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.IndexService;
import org.elasticsearch.index.mapper.MapperService.MergeReason;
import org.elasticsearch.indices.IndicesService;
import org.elasticsearch.plugins.Plugin;
import org.elasticsearch.test.ESSingleNodeTestCase;
import org.elasticsearch.test.InternalSettingsPlugin;

import java.io.IOException;
import java.util.Collection;
import java.util.Collections;

import static org.elasticsearch.index.mapper.IdFieldMapper.ID_FIELD_DATA_DEPRECATION_MESSAGE;
import static org.hamcrest.Matchers.containsString;

public class IdFieldMapperTests extends ESSingleNodeTestCase {

@Override
protected Collection<Class<? extends Plugin>> getPlugins() {
return Collections.singleton(InternalSettingsPlugin.class);
}
public class IdFieldMapperTests extends MapperServiceTestCase {

public void testIncludeInObjectNotAllowed() throws Exception {
String mapping = Strings.toString(XContentFactory.jsonBuilder().startObject().startObject("type").endObject().endObject());
DocumentMapper docMapper = createIndex("test").mapperService().parse("type", new CompressedXContent(mapping), false);
DocumentMapper docMapper = createDocumentMapper(mapping(b -> {}));

try {
docMapper.parse(new SourceToParse("test", "type", "1", BytesReference.bytes(XContentFactory.jsonBuilder()
.startObject().field("_id", "1").endObject()), XContentType.JSON));
fail("Expected failure to parse metadata field");
} catch (MapperParsingException e) {
assertTrue(e.getCause().getMessage(),
e.getCause().getMessage().contains("Field [_id] is a metadata field and cannot be added inside a document"));
}
Exception e = expectThrows(MapperParsingException.class,
() -> docMapper.parse(source(b -> b.field("_id", 1))));

assertThat(e.getCause().getMessage(),
containsString("Field [_id] is a metadata field and cannot be added inside a document"));
}

public void testDefaults() throws IOException {
Settings indexSettings = Settings.EMPTY;
MapperService mapperService = createIndex("test", indexSettings).mapperService();
DocumentMapper mapper = mapperService.merge("type", new CompressedXContent("{\"type\":{}}"), MergeReason.MAPPING_UPDATE);
ParsedDocument document = mapper.parse(new SourceToParse("index", "type", "id",
new BytesArray("{}"), XContentType.JSON));
DocumentMapper mapper = createDocumentMapper(mapping(b -> {}));
ParsedDocument document = mapper.parse(source(b -> {}));
IndexableField[] fields = document.rootDoc().getFields(IdFieldMapper.NAME);
assertEquals(1, fields.length);
assertEquals(IndexOptions.DOCS, fields[0].fieldType().indexOptions());
assertTrue(fields[0].fieldType().stored());
assertEquals(Uid.encodeId("id"), fields[0].binaryValue());
assertEquals(Uid.encodeId("1"), fields[0].binaryValue());
}

public void testEnableFieldData() throws IOException {
IndexService service = createIndex("test", Settings.EMPTY);
MapperService mapperService = service.mapperService();
mapperService.merge("type", new CompressedXContent("{\"type\":{}}"), MergeReason.MAPPING_UPDATE);
IdFieldMapper.IdFieldType ft = (IdFieldMapper.IdFieldType) service.mapperService().fieldType("_id");

boolean[] enabled = new boolean[1];

MapperService mapperService = createMapperService(() -> enabled[0], mapping(b -> {}));
IdFieldMapper.IdFieldType ft = (IdFieldMapper.IdFieldType) mapperService.fieldType("_id");

IllegalArgumentException exc = expectThrows(IllegalArgumentException.class,
() -> ft.fielddataBuilder("test", () -> {
throw new UnsupportedOperationException();
}).build(null, null));
assertThat(exc.getMessage(), containsString(IndicesService.INDICES_ID_FIELD_DATA_ENABLED_SETTING.getKey()));
assertFalse(ft.isAggregatable());

enabled[0] = true;
ft.fielddataBuilder("test", () -> {
throw new UnsupportedOperationException();
}).build(null, null);
assertWarnings(ID_FIELD_DATA_DEPRECATION_MESSAGE);
assertTrue(ft.isAggregatable());

client().admin().cluster().prepareUpdateSettings()
.setTransientSettings(Settings.builder().put(IndicesService.INDICES_ID_FIELD_DATA_ENABLED_SETTING.getKey(), false))
.get();
try {
IllegalArgumentException exc = expectThrows(IllegalArgumentException.class,
() -> ft.fielddataBuilder("test", () -> {
throw new UnsupportedOperationException();
}).build(null, null));
assertThat(exc.getMessage(), containsString(IndicesService.INDICES_ID_FIELD_DATA_ENABLED_SETTING.getKey()));
assertFalse(ft.isAggregatable());
} finally {
// unset cluster setting
client().admin().cluster().prepareUpdateSettings()
.setTransientSettings(Settings.builder().putNull(IndicesService.INDICES_ID_FIELD_DATA_ENABLED_SETTING.getKey()))
.get();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,52 +19,23 @@

package org.elasticsearch.index.mapper;

import org.elasticsearch.common.Strings;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.compress.CompressedXContent;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.plugins.Plugin;
import org.elasticsearch.test.ESSingleNodeTestCase;
import org.elasticsearch.test.InternalSettingsPlugin;

import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.nullValue;

import java.io.IOException;
import java.util.Collection;

public class IndexFieldMapperTests extends ESSingleNodeTestCase {

@Override
protected Collection<Class<? extends Plugin>> getPlugins() {
return pluginList(InternalSettingsPlugin.class);
}
public class IndexFieldMapperTests extends MapperServiceTestCase {

public void testDefaultDisabledIndexMapper() throws Exception {
String mapping = Strings.toString(XContentFactory.jsonBuilder().startObject().startObject("type")
.endObject().endObject());
DocumentMapper docMapper = createIndex("test").mapperService().parse("type", new CompressedXContent(mapping), false);

ParsedDocument doc = docMapper.parse(new SourceToParse("test", "type", "1",
BytesReference.bytes(XContentFactory.jsonBuilder()
.startObject()
.field("field", "value")
.endObject()),
XContentType.JSON));

DocumentMapper docMapper = createDocumentMapper(mapping(b -> {}));
ParsedDocument doc = docMapper.parse(source(b -> b.field("field", "value")));
assertThat(doc.rootDoc().get("_index"), nullValue());
assertThat(doc.rootDoc().get("field"), equalTo("value"));
}

public void testIndexNotConfigurable() throws IOException {
String mapping = Strings.toString(XContentFactory.jsonBuilder().startObject().startObject("type")
.startObject("_index").endObject()
.endObject().endObject());
MapperService mapperService = createIndex("test").mapperService();
public void testIndexNotConfigurable() {
MapperParsingException e = expectThrows(MapperParsingException.class,
() -> mapperService.parse("type", new CompressedXContent(mapping), false));
assertEquals("_index is not configurable", e.getMessage());
() -> createMapperService(topMapping(b -> b.startObject("_index").endObject())));
assertThat(e.getMessage(), containsString("_index is not configurable"));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,49 +20,26 @@

import org.apache.lucene.index.DocValuesType;
import org.apache.lucene.index.IndexableField;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.compress.CompressedXContent;
import org.elasticsearch.common.network.InetAddresses;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.test.ESSingleNodeTestCase;
import org.junit.Before;

import java.util.HashMap;
import java.util.Map;

import static org.hamcrest.Matchers.containsString;

public class IpRangeFieldMapperTests extends ESSingleNodeTestCase {
public class IpRangeFieldMapperTests extends MapperServiceTestCase {

private MapperService mapperService;
public void testStoreCidr() throws Exception {

@Before
public void setup() {
mapperService = createIndex("test").mapperService();
}
DocumentMapper mapper = createDocumentMapper(fieldMapping(b -> b.field("type", "ip_range").field("store", true)));

public void testStoreCidr() throws Exception {
XContentBuilder mapping = XContentFactory.jsonBuilder().startObject().startObject("type")
.startObject("properties").startObject("field").field("type", "ip_range")
.field("store", true);
mapping = mapping.endObject().endObject().endObject().endObject();
DocumentMapper mapper = mapperService.parse("type", new CompressedXContent(Strings.toString(mapping)), false);
assertEquals(Strings.toString(mapping), mapper.mappingSource().toString());
final Map<String, String> cases = new HashMap<>();
cases.put("192.168.0.0/15", "192.169.255.255");
cases.put("192.168.0.0/16", "192.168.255.255");
cases.put("192.168.0.0/17", "192.168.127.255");
for (final Map.Entry<String, String> entry : cases.entrySet()) {
ParsedDocument doc =
mapper.parse(new SourceToParse("test", "type", "1", BytesReference.bytes(XContentFactory.jsonBuilder()
.startObject()
.field("field", entry.getKey())
.endObject()),
XContentType.JSON
));
mapper.parse(source(b -> b.field("field", entry.getKey())));
IndexableField[] fields = doc.rootDoc().getFields("field");
assertEquals(3, fields.length);
IndexableField dvField = fields[0];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,26 +25,23 @@
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.mapper.ParseContext.Document;
import org.elasticsearch.test.ESSingleNodeTestCase;

import static org.elasticsearch.test.StreamsUtils.copyToStringFromClasspath;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.notNullValue;
import static org.hamcrest.Matchers.nullValue;

public class JavaMultiFieldMergeTests extends ESSingleNodeTestCase {
public class JavaMultiFieldMergeTests extends MapperServiceTestCase {
public void testMergeMultiField() throws Exception {
String mapping = copyToStringFromClasspath("/org/elasticsearch/index/mapper/multifield/merge/test-mapping1.json");
MapperService mapperService = createIndex("test").mapperService();

mapperService.merge("person", new CompressedXContent(mapping), MapperService.MergeReason.MAPPING_UPDATE);
MapperService mapperService = createMapperService("person", mapping);

assertTrue(mapperService.fieldType("name").isSearchable());
assertThat(mapperService.fieldType("name.indexed"), nullValue());

BytesReference json = BytesReference.bytes(XContentFactory.jsonBuilder().startObject().field("name", "some name").endObject());
Document doc = mapperService.documentMapper().parse(
new SourceToParse("test", "person", "1", json, XContentType.JSON)).rootDoc();
new SourceToParse("test", "_doc", "1", json, XContentType.JSON)).rootDoc();
IndexableField f = doc.getField("name");
assertThat(f, notNullValue());
f = doc.getField("name.indexed");
Expand All @@ -60,7 +57,7 @@ public void testMergeMultiField() throws Exception {
assertThat(mapperService.fieldType("name.not_indexed2"), nullValue());
assertThat(mapperService.fieldType("name.not_indexed3"), nullValue());

doc = mapperService.documentMapper().parse(new SourceToParse("test", "person", "1", json, XContentType.JSON)).rootDoc();
doc = mapperService.documentMapper().parse(new SourceToParse("test", "_doc", "1", json, XContentType.JSON)).rootDoc();
f = doc.getField("name");
assertThat(f, notNullValue());
f = doc.getField("name.indexed");
Expand Down Expand Up @@ -89,22 +86,17 @@ public void testMergeMultiField() throws Exception {

public void testUpgradeFromMultiFieldTypeToMultiFields() throws Exception {
String mapping = copyToStringFromClasspath("/org/elasticsearch/index/mapper/multifield/merge/test-mapping1.json");
MapperService mapperService = createIndex("test").mapperService();

mapperService.merge("person", new CompressedXContent(mapping), MapperService.MergeReason.MAPPING_UPDATE);
MapperService mapperService = createMapperService("person", mapping);

assertTrue(mapperService.fieldType("name").isSearchable());
assertThat(mapperService.fieldType("name.indexed"), nullValue());

BytesReference json = BytesReference.bytes(XContentFactory.jsonBuilder().startObject().field("name", "some name").endObject());
Document doc = mapperService.documentMapper().parse(
new SourceToParse("test", "person", "1", json, XContentType.JSON)).rootDoc();
Document doc = mapperService.documentMapper().parse(source(b -> b.field("name", "some name"))).rootDoc();
IndexableField f = doc.getField("name");
assertThat(f, notNullValue());
f = doc.getField("name.indexed");
assertThat(f, nullValue());


mapping = copyToStringFromClasspath("/org/elasticsearch/index/mapper/multifield/merge/upgrade1.json");
mapperService.merge("person", new CompressedXContent(mapping), MapperService.MergeReason.MAPPING_UPDATE);

Expand All @@ -115,8 +107,7 @@ public void testUpgradeFromMultiFieldTypeToMultiFields() throws Exception {
assertThat(mapperService.fieldType("name.not_indexed2"), nullValue());
assertThat(mapperService.fieldType("name.not_indexed3"), nullValue());

doc = mapperService.documentMapper().parse(
new SourceToParse("test", "person", "1", json, XContentType.JSON)).rootDoc();
doc = mapperService.documentMapper().parse(source(b -> b.field("name", "some name"))).rootDoc();
f = doc.getField("name");
assertThat(f, notNullValue());
f = doc.getField("name.indexed");
Expand Down

0 comments on commit 629e04b

Please sign in to comment.