Skip to content

Commit

Permalink
more test fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
jakelandis committed Sep 23, 2024
1 parent d6a6cee commit 34aae04
Show file tree
Hide file tree
Showing 9 changed files with 4 additions and 455 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import org.elasticsearch.common.compress.CompressedXContent;
import org.elasticsearch.common.io.stream.BytesStreamOutput;
import org.elasticsearch.common.lucene.uid.Versions;
import org.elasticsearch.core.RestApiVersion;
import org.elasticsearch.index.get.GetResult;
import org.elasticsearch.index.mapper.MapperService;
import org.elasticsearch.index.query.QueryBuilder;
Expand All @@ -32,9 +31,7 @@
import org.elasticsearch.test.AbstractQueryTestCase;
import org.elasticsearch.xcontent.XContentBuilder;
import org.elasticsearch.xcontent.XContentFactory;
import org.elasticsearch.xcontent.XContentParser;
import org.elasticsearch.xcontent.XContentType;
import org.elasticsearch.xcontent.json.JsonXContent;
import org.hamcrest.Matchers;

import java.io.IOException;
Expand Down Expand Up @@ -379,31 +376,4 @@ public void testDisallowExpensiveQueries() {
ElasticsearchException e = expectThrows(ElasticsearchException.class, () -> queryBuilder.toQuery(searchExecutionContext));
assertEquals("[percolate] queries cannot be executed when 'search.allow_expensive_queries' is set to false.", e.getMessage());
}

public void testFromJsonWithDocumentType() throws IOException {
SearchExecutionContext searchExecutionContext = createSearchExecutionContext();
String queryAsString = Strings.format("""
{"percolate" : { "document": {}, "document_type":"%s", "field":"%s"}}
""", docType, queryField);
XContentParser parser = createParserWithCompatibilityFor(JsonXContent.jsonXContent, queryAsString, RestApiVersion.V_7);
QueryBuilder queryBuilder = parseQuery(parser);
queryBuilder.toQuery(searchExecutionContext);
assertCriticalWarnings(PercolateQueryBuilder.DOCUMENT_TYPE_DEPRECATION_MESSAGE);
}

public void testFromJsonWithType() throws IOException {
indexedDocumentIndex = randomAlphaOfLength(4);
indexedDocumentId = randomAlphaOfLength(4);
indexedDocumentVersion = Versions.MATCH_ANY;
documentSource = Collections.singletonList(randomSource(new HashSet<>()));
SearchExecutionContext searchExecutionContext = createSearchExecutionContext();

String queryAsString = Strings.format("""
{"percolate" : { "index": "%s", "type": "_doc", "id": "%s", "field":"%s"}}
""", indexedDocumentIndex, indexedDocumentId, queryField);
XContentParser parser = createParserWithCompatibilityFor(JsonXContent.jsonXContent, queryAsString, RestApiVersion.V_7);
QueryBuilder queryBuilder = parseQuery(parser);
rewriteAndFetch(queryBuilder, searchExecutionContext).toQuery(searchExecutionContext);
assertCriticalWarnings(PercolateQueryBuilder.TYPE_DEPRECATION_MESSAGE);
}
}
4 changes: 4 additions & 0 deletions rest-api-spec/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,7 @@ tasks.register('enforceYamlTestConvention').configure {
tasks.named("precommit").configure {
dependsOn 'enforceYamlTestConvention'
}

tasks.named("yamlRestCompatTestTransform").configure({ task ->
task.skipTestsByFilePattern("indices.create/synthetic_source*.yml", "@UpdateForV9 -> tests do not pass after bumping API version to 9 [ES-9597]")
})
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import org.elasticsearch.common.unit.ByteSizeUnit;
import org.elasticsearch.common.unit.ByteSizeValue;
import org.elasticsearch.common.xcontent.XContentHelper;
import org.elasticsearch.core.RestApiVersion;
import org.elasticsearch.core.TimeValue;
import org.elasticsearch.index.mapper.MapperService;
import org.elasticsearch.indices.IndicesModule;
Expand All @@ -30,9 +29,7 @@
import org.elasticsearch.xcontent.XContentBuilder;
import org.elasticsearch.xcontent.XContentFactory;
import org.elasticsearch.xcontent.XContentParseException;
import org.elasticsearch.xcontent.XContentParser;
import org.elasticsearch.xcontent.XContentType;
import org.elasticsearch.xcontent.json.JsonXContent;
import org.junit.Before;

import java.io.IOException;
Expand Down Expand Up @@ -276,72 +273,4 @@ public void testValidation() {
);
}
}

public void testParsingWithType() throws Exception {
final XContentBuilder builder = XContentFactory.jsonBuilder()
.startObject()
.startObject("conditions")
.field("max_age", "10d")
.field("max_docs", 100)
.endObject()
.startObject("mappings")
.startObject("type1")
.startObject("properties")
.startObject("field1")
.field("type", "string")
.field("index", "not_analyzed")
.endObject()
.endObject()
.endObject()
.endObject()
.startObject("settings")
.field("number_of_shards", 10)
.endObject()
.startObject("aliases")
.startObject("alias1")
.endObject()
.endObject()
.endObject();

try (
XContentParser parser = createParserWithCompatibilityFor(
JsonXContent.jsonXContent,
BytesReference.bytes(builder).utf8ToString(),
RestApiVersion.V_7
)
) {
final RolloverRequest request = new RolloverRequest(randomAlphaOfLength(10), randomAlphaOfLength(10));
request.fromXContent(true, parser);
Map<String, Condition<?>> conditions = request.getConditions().getConditions();
assertThat(conditions.size(), equalTo(2));
assertThat(request.getCreateIndexRequest().mappings(), equalTo("""
{"_doc":{"properties":{"field1":{"index":"not_analyzed","type":"string"}}}}"""));
}
}

public void testTypedRequestWithoutIncludeTypeName() throws IOException {
final XContentBuilder builder = XContentFactory.jsonBuilder()
.startObject()
.startObject("mappings")
.startObject("_doc")
.startObject("properties")
.startObject("field1")
.field("type", "string")
.field("index", "not_analyzed")
.endObject()
.endObject()
.endObject()
.endObject()
.endObject();
try (
XContentParser parser = createParserWithCompatibilityFor(
JsonXContent.jsonXContent,
BytesReference.bytes(builder).utf8ToString(),
RestApiVersion.V_7
)
) {
final RolloverRequest request = new RolloverRequest(randomAlphaOfLength(10), randomAlphaOfLength(10));
expectThrows(IllegalArgumentException.class, () -> request.fromXContent(false, parser));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -471,52 +471,6 @@ public void testWritingExpandWildcards() throws IOException {
);
}

public void testEmptyFirstLine1() throws Exception {
MultiSearchRequest request = parseMultiSearchRequestFromString("""
{ "query": {"match_all": {}}}
{}
{ "query": {"match_all": {}}}
{ "query": {"match_all": {}}}
{}
{ "query": {"match_all": {}}}
""", RestApiVersion.V_7);
assertThat(request.requests().size(), equalTo(4));
for (SearchRequest searchRequest : request.requests()) {
assertThat(searchRequest.indices().length, equalTo(0));
assertThat(searchRequest.source().query(), instanceOf(MatchAllQueryBuilder.class));
}
assertCriticalWarnings(
"support for empty first line before any action metadata in msearch API is deprecated and will be removed "
+ "in the next major version"
);
}

public void testEmptyFirstLine2() throws Exception {
MultiSearchRequest request = parseMultiSearchRequestFromString("""
{}
{ "query": {"match_all": {}}}
{ "query": {"match_all": {}}}
{}
{ "query": {"match_all": {}}}
{ "query": {"match_all": {}}}
""", RestApiVersion.V_7);
assertThat(request.requests().size(), equalTo(4));
for (SearchRequest searchRequest : request.requests()) {
assertThat(searchRequest.indices().length, equalTo(0));
assertThat(searchRequest.source().query(), instanceOf(MatchAllQueryBuilder.class));
}
assertCriticalWarnings(
"support for empty first line before any action metadata in msearch API is deprecated and will be removed "
+ "in the next major version"
);
}

public void testTaskDescription() {
MultiSearchRequest request = new MultiSearchRequest();
request.add(new SearchRequest().preference("abc"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,15 @@

package org.elasticsearch.rest.action.admin.indices;

import org.elasticsearch.action.admin.indices.create.CreateIndexRequest;
import org.elasticsearch.client.internal.node.NodeClient;
import org.elasticsearch.common.bytes.BytesArray;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.xcontent.XContentHelper;
import org.elasticsearch.core.RestApiVersion;
import org.elasticsearch.rest.RestRequest;
import org.elasticsearch.test.ESTestCase;
import org.elasticsearch.test.rest.FakeRestRequest;
import org.elasticsearch.xcontent.XContentBuilder;
import org.elasticsearch.xcontent.XContentFactory;
import org.elasticsearch.xcontent.XContentType;

import java.io.IOException;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static org.elasticsearch.rest.BaseRestHandler.INCLUDE_TYPE_NAME_PARAMETER;
import static org.hamcrest.Matchers.equalTo;
import static org.mockito.Mockito.mock;

public class RestCreateIndexActionTests extends ESTestCase {

public void testPrepareTypelessRequest() throws IOException {
Expand Down Expand Up @@ -99,59 +85,4 @@ public void testMalformedMappings() throws IOException {
Map<String, Object> source = RestCreateIndexAction.prepareMappings(contentAsMap);
assertEquals(contentAsMap, source);
}

public void testIncludeTypeName() throws IOException {
RestCreateIndexAction action = new RestCreateIndexAction();
List<String> compatibleMediaType = Collections.singletonList(randomCompatibleMediaType(RestApiVersion.V_7));

Map<String, String> params = new HashMap<>();
params.put(INCLUDE_TYPE_NAME_PARAMETER, randomFrom("true", "false"));
RestRequest deprecatedRequest = new FakeRestRequest.Builder(xContentRegistry()).withHeaders(Map.of("Accept", compatibleMediaType))
.withMethod(RestRequest.Method.PUT)
.withPath("/some_index")
.withParams(params)
.build();

action.prepareRequest(deprecatedRequest, mock(NodeClient.class));
assertCriticalWarnings(RestCreateIndexAction.TYPES_DEPRECATION_MESSAGE);

RestRequest validRequest = new FakeRestRequest.Builder(xContentRegistry()).withMethod(RestRequest.Method.PUT)
.withPath("/some_index")
.build();
action.prepareRequest(validRequest, mock(NodeClient.class));
}

public void testTypeInMapping() throws IOException {
RestCreateIndexAction action = new RestCreateIndexAction();

List<String> contentTypeHeader = Collections.singletonList(compatibleMediaType(XContentType.VND_JSON, RestApiVersion.V_7));

String content = """
{
"mappings": {
"some_type": {
"properties": {
"field1": {
"type": "text"
}
}
}
}
}""";

Map<String, String> params = new HashMap<>();
params.put(RestCreateIndexAction.INCLUDE_TYPE_NAME_PARAMETER, "true");
RestRequest request = new FakeRestRequest.Builder(xContentRegistry()).withMethod(RestRequest.Method.PUT)
.withHeaders(Map.of("Content-Type", contentTypeHeader, "Accept", contentTypeHeader))
.withPath("/some_index")
.withParams(params)
.withContent(new BytesArray(content), null)
.build();

CreateIndexRequest createIndexRequest = RestCreateIndexAction.prepareRequestV7(request);
// some_type is replaced with _doc
assertThat(createIndexRequest.mappings(), equalTo("""
{"_doc":{"properties":{"field1":{"type":"text"}}}}"""));
assertCriticalWarnings(RestCreateIndexAction.TYPES_DEPRECATION_MESSAGE);
}
}

This file was deleted.

Loading

0 comments on commit 34aae04

Please sign in to comment.