Skip to content

Commit

Permalink
HLRestClient: Follow-up for put index template api (#30592)
Browse files Browse the repository at this point in the history
This commit addresses some comments given after the original PR was in.

Follow-up #30400
  • Loading branch information
dnhatn committed May 15, 2018
1 parent 7f1a15b commit 511856d
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import org.elasticsearch.common.logging.DeprecationLogger;
import org.elasticsearch.common.logging.Loggers;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.DeprecationHandler;
import org.elasticsearch.common.xcontent.LoggingDeprecationHandler;
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
import org.elasticsearch.common.xcontent.ToXContent;
Expand All @@ -45,6 +46,7 @@
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.support.XContentMapValues;

import java.io.IOException;
Expand Down Expand Up @@ -543,9 +545,6 @@ public void writeTo(StreamOutput out) throws IOException {

@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
if (customs.isEmpty() == false) {
throw new IllegalArgumentException("Custom data type is no longer supported in index template [" + customs + "]");
}
builder.field("index_patterns", indexPatterns);
builder.field("order", order);
if (version != null) {
Expand All @@ -558,8 +557,10 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws

builder.startObject("mappings");
for (Map.Entry<String, String> entry : mappings.entrySet()) {
Map<String, Object> mapping = XContentHelper.convertToMap(new BytesArray(entry.getValue()), false).v2();
builder.field(entry.getKey(), mapping);
builder.field(entry.getKey());
XContentParser parser = JsonXContent.jsonXContent.createParser(NamedXContentRegistry.EMPTY,
DeprecationHandler.THROW_UNSUPPORTED_OPERATION, entry.getValue());
builder.copyCurrentStructure(parser);
}
builder.endObject();

Expand All @@ -568,6 +569,11 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
alias.toXContent(builder, params);
}
builder.endObject();

for (Map.Entry<String, IndexMetaData.Custom> entry : customs.entrySet()) {
builder.field(entry.getKey(), entry.getValue(), params);
}

return builder;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,18 @@
import org.elasticsearch.action.admin.indices.alias.Alias;
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.settings.Settings;
import org.elasticsearch.common.xcontent.ToXContent;
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.yaml.YamlXContent;
import org.elasticsearch.test.ESTestCase;
import org.elasticsearch.test.AbstractXContentTestCase;

import java.io.IOException;
import java.io.UncheckedIOException;
import java.util.Arrays;
import java.util.Base64;
import java.util.Collections;
Expand All @@ -45,7 +45,7 @@
import static org.hamcrest.Matchers.nullValue;
import static org.hamcrest.core.Is.is;

public class PutIndexTemplateRequestTests extends ESTestCase {
public class PutIndexTemplateRequestTests extends AbstractXContentTestCase<PutIndexTemplateRequest> {

// bwc for #21009
public void testPutIndexTemplateRequest510() throws IOException {
Expand Down Expand Up @@ -137,13 +137,14 @@ public void testValidateErrorMessage() throws Exception {
assertThat(noError, is(nullValue()));
}

private PutIndexTemplateRequest randomPutIndexTemplateRequest() throws IOException {
@Override
protected PutIndexTemplateRequest createTestInstance() {
PutIndexTemplateRequest request = new PutIndexTemplateRequest();
request.name("test");
if (randomBoolean()){
if (randomBoolean()) {
request.version(randomInt());
}
if (randomBoolean()){
if (randomBoolean()) {
request.order(randomInt());
}
request.patterns(Arrays.asList(generateRandomStringArray(20, 100, false, false)));
Expand All @@ -159,30 +160,39 @@ private PutIndexTemplateRequest randomPutIndexTemplateRequest() throws IOExcepti
request.alias(alias);
}
if (randomBoolean()) {
request.mapping("doc", XContentFactory.jsonBuilder().startObject()
.startObject("doc").startObject("properties")
.startObject("field-" + randomInt()).field("type", randomFrom("keyword", "text")).endObject()
.endObject().endObject().endObject());
try {
request.mapping("doc", XContentFactory.jsonBuilder().startObject()
.startObject("doc").startObject("properties")
.startObject("field-" + randomInt()).field("type", randomFrom("keyword", "text")).endObject()
.endObject().endObject().endObject());
} catch (IOException ex) {
throw new UncheckedIOException(ex);
}
}
if (randomBoolean()){
if (randomBoolean()) {
request.settings(Settings.builder().put("setting1", randomLong()).put("setting2", randomTimeValue()).build());
}
return request;
}

public void testFromToXContentPutTemplateRequest() throws Exception {
for (int i = 0; i < 10; i++) {
PutIndexTemplateRequest expected = randomPutIndexTemplateRequest();
XContentType xContentType = randomFrom(XContentType.values());
BytesReference shuffled = toShuffledXContent(expected, xContentType, ToXContent.EMPTY_PARAMS, randomBoolean());
PutIndexTemplateRequest parsed = new PutIndexTemplateRequest().source(shuffled, xContentType);
assertNotSame(expected, parsed);
assertThat(parsed.version(), equalTo(expected.version()));
assertThat(parsed.order(), equalTo(expected.order()));
assertThat(parsed.patterns(), equalTo(expected.patterns()));
assertThat(parsed.aliases(), equalTo(expected.aliases()));
assertThat(parsed.mappings(), equalTo(expected.mappings()));
assertThat(parsed.settings(), equalTo(expected.settings()));
}
@Override
protected PutIndexTemplateRequest doParseInstance(XContentParser parser) throws IOException {
return new PutIndexTemplateRequest().source(parser.map());
}

@Override
protected void assertEqualInstances(PutIndexTemplateRequest expected, PutIndexTemplateRequest actual) {
assertNotSame(expected, actual);
assertThat(actual.version(), equalTo(expected.version()));
assertThat(actual.order(), equalTo(expected.order()));
assertThat(actual.patterns(), equalTo(expected.patterns()));
assertThat(actual.aliases(), equalTo(expected.aliases()));
assertThat(actual.mappings(), equalTo(expected.mappings()));
assertThat(actual.settings(), equalTo(expected.settings()));
}

@Override
protected boolean supportsUnknownFields() {
return false;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Licensed to Elasticsearch under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.elasticsearch.action.admin.indices.template.put;

import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.test.AbstractStreamableXContentTestCase;

public class PutIndexTemplateResponseTests extends AbstractStreamableXContentTestCase<PutIndexTemplateResponse> {
@Override
protected PutIndexTemplateResponse doParseInstance(XContentParser parser) {
return PutIndexTemplateResponse.fromXContent(parser);
}

@Override
protected PutIndexTemplateResponse createTestInstance() {
return new PutIndexTemplateResponse(randomBoolean());
}

@Override
protected PutIndexTemplateResponse createBlankInstance() {
return new PutIndexTemplateResponse();
}

@Override
protected PutIndexTemplateResponse mutateInstance(PutIndexTemplateResponse response) {
return new PutIndexTemplateResponse(response.isAcknowledged() == false);
}
}

0 comments on commit 511856d

Please sign in to comment.