|
|
@@ -22,31 +22,86 @@ package org.elasticsearch.action.admin.indices.create;
|
|
|
import org.elasticsearch.ElasticsearchParseException;
|
|
|
import org.elasticsearch.action.admin.indices.alias.Alias;
|
|
|
import org.elasticsearch.common.Strings;
|
|
|
-import org.elasticsearch.common.bytes.BytesReference;
|
|
|
import org.elasticsearch.common.io.stream.BytesStreamOutput;
|
|
|
import org.elasticsearch.common.io.stream.StreamInput;
|
|
|
-import org.elasticsearch.common.settings.Settings;
|
|
|
import org.elasticsearch.common.xcontent.LoggingDeprecationHandler;
|
|
|
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
|
|
|
import org.elasticsearch.common.xcontent.XContentParser;
|
|
|
import org.elasticsearch.common.xcontent.XContentType;
|
|
|
import org.elasticsearch.common.xcontent.json.JsonXContent;
|
|
|
import org.elasticsearch.index.RandomCreateIndexGenerator;
|
|
|
-import org.elasticsearch.test.ESTestCase;
|
|
|
-import org.elasticsearch.test.hamcrest.ElasticsearchAssertions;
|
|
|
+import org.elasticsearch.test.AbstractXContentTestCase;
|
|
|
|
|
|
import java.io.IOException;
|
|
|
import java.util.Map;
|
|
|
import java.util.Set;
|
|
|
|
|
|
-import static org.elasticsearch.cluster.metadata.IndexMetaData.SETTING_NUMBER_OF_SHARDS;
|
|
|
-import static org.elasticsearch.common.xcontent.ToXContent.EMPTY_PARAMS;
|
|
|
+public class CreateIndexRequestTests extends AbstractXContentTestCase<CreateIndexRequest> {
|
|
|
|
|
|
-public class CreateIndexRequestTests extends ESTestCase {
|
|
|
+ @Override
|
|
|
+ protected CreateIndexRequest createTestInstance() {
|
|
|
+ try {
|
|
|
+ return RandomCreateIndexGenerator.randomCreateIndexRequest();
|
|
|
+ } catch (IOException e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ protected CreateIndexRequest doParseInstance(XContentParser parser) throws IOException {
|
|
|
+ CreateIndexRequest request = new CreateIndexRequest();
|
|
|
+ request.source(parser.map(), LoggingDeprecationHandler.INSTANCE);
|
|
|
+ return request;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ protected void assertEqualInstances(CreateIndexRequest expectedInstance, CreateIndexRequest newInstance) {
|
|
|
+ assertEquals(expectedInstance.settings(), newInstance.settings());
|
|
|
+ assertAliasesEqual(expectedInstance.aliases(), newInstance.aliases());
|
|
|
+ assertMappingsEqual(expectedInstance.mappings(), newInstance.mappings());
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ protected boolean supportsUnknownFields() {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void assertMappingsEqual(Map<String, String> expected, Map<String, String> actual) {
|
|
|
+ assertEquals(expected.keySet(), actual.keySet());
|
|
|
+
|
|
|
+ for (Map.Entry<String, String> expectedEntry : expected.entrySet()) {
|
|
|
+ String expectedValue = expectedEntry.getValue();
|
|
|
+ String actualValue = actual.get(expectedEntry.getKey());
|
|
|
+ try (XContentParser expectedJson = JsonXContent.jsonXContent.createParser(NamedXContentRegistry.EMPTY,
|
|
|
+ LoggingDeprecationHandler.INSTANCE, expectedValue);
|
|
|
+ XContentParser actualJson = JsonXContent.jsonXContent.createParser(NamedXContentRegistry.EMPTY,
|
|
|
+ LoggingDeprecationHandler.INSTANCE, actualValue)) {
|
|
|
+ assertEquals(expectedJson.map(), actualJson.map());
|
|
|
+ } catch (IOException e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void assertAliasesEqual(Set<Alias> expected, Set<Alias> actual) {
|
|
|
+ assertEquals(expected, actual);
|
|
|
+
|
|
|
+ for (Alias expectedAlias : expected) {
|
|
|
+ for (Alias actualAlias : actual) {
|
|
|
+ if (expectedAlias.equals(actualAlias)) {
|
|
|
+ // As Alias#equals only looks at name, we check the equality of the other Alias parameters here.
|
|
|
+ assertEquals(expectedAlias.filter(), actualAlias.filter());
|
|
|
+ assertEquals(expectedAlias.indexRouting(), actualAlias.indexRouting());
|
|
|
+ assertEquals(expectedAlias.searchRouting(), actualAlias.searchRouting());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
public void testSerialization() throws IOException {
|
|
|
CreateIndexRequest request = new CreateIndexRequest("foo");
|
|
|
- String mapping = Strings.toString(JsonXContent.contentBuilder().startObject().startObject("type").endObject().endObject());
|
|
|
+ String mapping = Strings.toString(JsonXContent.contentBuilder().startObject()
|
|
|
+ .startObject("type").endObject().endObject());
|
|
|
request.mapping("my_type", mapping, XContentType.JSON);
|
|
|
|
|
|
try (BytesStreamOutput output = new BytesStreamOutput()) {
|
|
|
@@ -63,7 +118,7 @@ public class CreateIndexRequestTests extends ESTestCase {
|
|
|
|
|
|
public void testTopLevelKeys() {
|
|
|
String createIndex =
|
|
|
- "{\n"
|
|
|
+ "{\n"
|
|
|
+ " \"FOO_SHOULD_BE_ILLEGAL_HERE\": {\n"
|
|
|
+ " \"BAR_IS_THE_SAME\": 42\n"
|
|
|
+ " },\n"
|
|
|
@@ -80,81 +135,7 @@ public class CreateIndexRequestTests extends ESTestCase {
|
|
|
|
|
|
CreateIndexRequest request = new CreateIndexRequest();
|
|
|
ElasticsearchParseException e = expectThrows(ElasticsearchParseException.class,
|
|
|
- () -> {request.source(createIndex, XContentType.JSON);});
|
|
|
+ () -> {request.source(createIndex, XContentType.JSON);});
|
|
|
assertEquals("unknown key [FOO_SHOULD_BE_ILLEGAL_HERE] for create index", e.getMessage());
|
|
|
}
|
|
|
-
|
|
|
- public void testToXContent() throws IOException {
|
|
|
- CreateIndexRequest request = new CreateIndexRequest("foo");
|
|
|
-
|
|
|
- String mapping = Strings.toString(JsonXContent.contentBuilder().startObject().startObject("type").endObject().endObject());
|
|
|
- request.mapping("my_type", mapping, XContentType.JSON);
|
|
|
-
|
|
|
- Alias alias = new Alias("test_alias");
|
|
|
- alias.routing("1");
|
|
|
- alias.filter("{\"term\":{\"year\":2016}}");
|
|
|
- alias.writeIndex(true);
|
|
|
- request.alias(alias);
|
|
|
-
|
|
|
- Settings.Builder settings = Settings.builder();
|
|
|
- settings.put(SETTING_NUMBER_OF_SHARDS, 10);
|
|
|
- request.settings(settings);
|
|
|
-
|
|
|
- String actualRequestBody = Strings.toString(request);
|
|
|
-
|
|
|
- String expectedRequestBody = "{\"settings\":{\"index\":{\"number_of_shards\":\"10\"}}," +
|
|
|
- "\"mappings\":{\"my_type\":{\"type\":{}}}," +
|
|
|
- "\"aliases\":{\"test_alias\":{\"filter\":{\"term\":{\"year\":2016}},\"routing\":\"1\",\"is_write_index\":true}}}";
|
|
|
-
|
|
|
- assertEquals(expectedRequestBody, actualRequestBody);
|
|
|
- }
|
|
|
-
|
|
|
- public void testToAndFromXContent() throws IOException {
|
|
|
-
|
|
|
- final CreateIndexRequest createIndexRequest = RandomCreateIndexGenerator.randomCreateIndexRequest();
|
|
|
-
|
|
|
- boolean humanReadable = randomBoolean();
|
|
|
- final XContentType xContentType = randomFrom(XContentType.values());
|
|
|
- BytesReference originalBytes = toShuffledXContent(createIndexRequest, xContentType, EMPTY_PARAMS, humanReadable);
|
|
|
-
|
|
|
- CreateIndexRequest parsedCreateIndexRequest = new CreateIndexRequest();
|
|
|
- parsedCreateIndexRequest.source(originalBytes, xContentType);
|
|
|
-
|
|
|
- assertMappingsEqual(createIndexRequest.mappings(), parsedCreateIndexRequest.mappings());
|
|
|
- assertAliasesEqual(createIndexRequest.aliases(), parsedCreateIndexRequest.aliases());
|
|
|
- assertEquals(createIndexRequest.settings(), parsedCreateIndexRequest.settings());
|
|
|
-
|
|
|
- BytesReference finalBytes = toShuffledXContent(parsedCreateIndexRequest, xContentType, EMPTY_PARAMS, humanReadable);
|
|
|
- ElasticsearchAssertions.assertToXContentEquivalent(originalBytes, finalBytes, xContentType);
|
|
|
- }
|
|
|
-
|
|
|
- public static void assertMappingsEqual(Map<String, String> expected, Map<String, String> actual) throws IOException {
|
|
|
- assertEquals(expected.keySet(), actual.keySet());
|
|
|
-
|
|
|
- for (Map.Entry<String, String> expectedEntry : expected.entrySet()) {
|
|
|
- String expectedValue = expectedEntry.getValue();
|
|
|
- String actualValue = actual.get(expectedEntry.getKey());
|
|
|
- try (XContentParser expectedJson = JsonXContent.jsonXContent.createParser(NamedXContentRegistry.EMPTY,
|
|
|
- LoggingDeprecationHandler.INSTANCE, expectedValue);
|
|
|
- XContentParser actualJson = JsonXContent.jsonXContent.createParser(NamedXContentRegistry.EMPTY,
|
|
|
- LoggingDeprecationHandler.INSTANCE, actualValue)){
|
|
|
- assertEquals(expectedJson.map(), actualJson.map());
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- public static void assertAliasesEqual(Set<Alias> expected, Set<Alias> actual) throws IOException {
|
|
|
- assertEquals(expected, actual);
|
|
|
-
|
|
|
- for (Alias expectedAlias : expected) {
|
|
|
- for (Alias actualAlias : actual) {
|
|
|
- if (expectedAlias.equals(actualAlias)) {
|
|
|
- // As Alias#equals only looks at name, we check the equality of the other Alias parameters here.
|
|
|
- assertEquals(expectedAlias.filter(), actualAlias.filter());
|
|
|
- assertEquals(expectedAlias.indexRouting(), actualAlias.indexRouting());
|
|
|
- assertEquals(expectedAlias.searchRouting(), actualAlias.searchRouting());
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
}
|