|
@@ -21,17 +21,26 @@ package org.elasticsearch.action.admin.indices.mapping.put;
|
|
|
|
|
|
import org.elasticsearch.Version;
|
|
|
import org.elasticsearch.action.ActionRequestValidationException;
|
|
|
+import org.elasticsearch.action.admin.indices.create.CreateIndexRequestTests;
|
|
|
+import org.elasticsearch.common.Strings;
|
|
|
import org.elasticsearch.common.bytes.BytesArray;
|
|
|
+import org.elasticsearch.common.bytes.BytesReference;
|
|
|
import org.elasticsearch.common.io.stream.BytesStreamOutput;
|
|
|
import org.elasticsearch.common.io.stream.StreamInput;
|
|
|
+import org.elasticsearch.common.xcontent.XContentBuilder;
|
|
|
+import org.elasticsearch.common.xcontent.XContentFactory;
|
|
|
import org.elasticsearch.common.xcontent.XContentHelper;
|
|
|
+import org.elasticsearch.common.xcontent.XContentParser;
|
|
|
import org.elasticsearch.common.xcontent.XContentType;
|
|
|
+import org.elasticsearch.common.xcontent.json.JsonXContent;
|
|
|
import org.elasticsearch.common.xcontent.yaml.YamlXContent;
|
|
|
import org.elasticsearch.index.Index;
|
|
|
import org.elasticsearch.test.ESTestCase;
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
|
+import static org.elasticsearch.common.xcontent.ToXContent.EMPTY_PARAMS;
|
|
|
+
|
|
|
public class PutMappingRequestTests extends ESTestCase {
|
|
|
|
|
|
public void testValidation() {
|
|
@@ -94,4 +103,79 @@ public class PutMappingRequestTests extends ESTestCase {
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ public void testToXContent() throws IOException {
|
|
|
+ PutMappingRequest request = new PutMappingRequest("foo");
|
|
|
+ request.type("my_type");
|
|
|
+
|
|
|
+ XContentBuilder mapping = JsonXContent.contentBuilder().startObject();
|
|
|
+ mapping.startObject("properties");
|
|
|
+ mapping.startObject("email");
|
|
|
+ mapping.field("type", "text");
|
|
|
+ mapping.endObject();
|
|
|
+ mapping.endObject();
|
|
|
+ mapping.endObject();
|
|
|
+ request.source(mapping);
|
|
|
+
|
|
|
+ String actualRequestBody = Strings.toString(request);
|
|
|
+ String expectedRequestBody = "{\"properties\":{\"email\":{\"type\":\"text\"}}}";
|
|
|
+ assertEquals(expectedRequestBody, actualRequestBody);
|
|
|
+ }
|
|
|
+
|
|
|
+ public void testToXContentWithEmptySource() throws IOException {
|
|
|
+ PutMappingRequest request = new PutMappingRequest("foo");
|
|
|
+ request.type("my_type");
|
|
|
+
|
|
|
+ String actualRequestBody = Strings.toString(request);
|
|
|
+ String expectedRequestBody = "{}";
|
|
|
+ assertEquals(expectedRequestBody, actualRequestBody);
|
|
|
+ }
|
|
|
+
|
|
|
+ public void testToAndFromXContent() throws IOException {
|
|
|
+
|
|
|
+ final PutMappingRequest putMappingRequest = createTestItem();
|
|
|
+
|
|
|
+ boolean humanReadable = randomBoolean();
|
|
|
+ final XContentType xContentType = randomFrom(XContentType.values());
|
|
|
+ BytesReference originalBytes = toShuffledXContent(putMappingRequest, xContentType, EMPTY_PARAMS, humanReadable);
|
|
|
+
|
|
|
+ PutMappingRequest parsedPutMappingRequest = new PutMappingRequest();
|
|
|
+ parsedPutMappingRequest.source(originalBytes, xContentType);
|
|
|
+
|
|
|
+ assertMappingsEqual(putMappingRequest.source(), parsedPutMappingRequest.source());
|
|
|
+ }
|
|
|
+
|
|
|
+ private void assertMappingsEqual(String expected, String actual) throws IOException {
|
|
|
+
|
|
|
+ XContentParser expectedJson = createParser(XContentType.JSON.xContent(), expected);
|
|
|
+ XContentParser actualJson = createParser(XContentType.JSON.xContent(), actual);
|
|
|
+ assertEquals(expectedJson.mapOrdered(), actualJson.mapOrdered());
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Returns a random {@link PutMappingRequest}.
|
|
|
+ */
|
|
|
+ private static PutMappingRequest createTestItem() throws IOException {
|
|
|
+ String index = randomAlphaOfLength(5);
|
|
|
+
|
|
|
+ PutMappingRequest request = new PutMappingRequest(index);
|
|
|
+
|
|
|
+ String type = randomAlphaOfLength(5);
|
|
|
+ request.type(type);
|
|
|
+ request.source(randomMapping());
|
|
|
+
|
|
|
+ return request;
|
|
|
+ }
|
|
|
+
|
|
|
+ private static XContentBuilder randomMapping() throws IOException {
|
|
|
+ XContentBuilder builder = XContentFactory.jsonBuilder();
|
|
|
+ builder.startObject();
|
|
|
+
|
|
|
+ if (randomBoolean()) {
|
|
|
+ CreateIndexRequestTests.randomMappingFields(builder, true);
|
|
|
+ }
|
|
|
+
|
|
|
+ builder.endObject();
|
|
|
+ return builder;
|
|
|
+ }
|
|
|
}
|