From cd9388ce660a697bd499f4fb0d8377821656f429 Mon Sep 17 00:00:00 2001 From: javanna Date: Thu, 4 Aug 2016 19:46:45 +0200 Subject: [PATCH] [TEST] parse query alternate versions in strict mode AbstractQueryTestCase parses the main version of the query in strict mode, meaning that it will fail if any deprecated syntax is used. It should do the same for alternate versions (e.g. short versions). This is the way it is because the two alternate versions for ids query are both deprecated. Moved testing for those to a specific test method that isolates the deprecations and actually tests that the two are deprecated. --- .../index/query/IdsQueryBuilderTests.java | 76 +++++++++++-------- .../test/AbstractQueryTestCase.java | 2 +- 2 files changed, 44 insertions(+), 34 deletions(-) diff --git a/core/src/test/java/org/elasticsearch/index/query/IdsQueryBuilderTests.java b/core/src/test/java/org/elasticsearch/index/query/IdsQueryBuilderTests.java index 42f4aaf56aa..4ad90edc8cb 100644 --- a/core/src/test/java/org/elasticsearch/index/query/IdsQueryBuilderTests.java +++ b/core/src/test/java/org/elasticsearch/index/query/IdsQueryBuilderTests.java @@ -23,13 +23,12 @@ package org.elasticsearch.index.query; import org.apache.lucene.queries.TermsQuery; import org.apache.lucene.search.Query; import org.elasticsearch.cluster.metadata.MetaData; +import org.elasticsearch.common.ParseFieldMatcher; import org.elasticsearch.common.ParsingException; import org.elasticsearch.common.lucene.search.MatchNoDocsQuery; import org.elasticsearch.test.AbstractQueryTestCase; import java.io.IOException; -import java.util.HashMap; -import java.util.Map; import static org.hamcrest.CoreMatchers.instanceOf; import static org.hamcrest.Matchers.containsString; @@ -94,37 +93,6 @@ public class IdsQueryBuilderTests extends AbstractQueryTestCase } } - @Override - protected Map getAlternateVersions() { - Map alternateVersions = new HashMap<>(); - - IdsQueryBuilder tempQuery = createTestQueryBuilder(); - if (tempQuery.types() != null && tempQuery.types().length > 0) { - String type = tempQuery.types()[0]; - IdsQueryBuilder testQuery = new IdsQueryBuilder(type); - - //single value type can also be called _type - String contentString1 = "{\n" + - " \"ids\" : {\n" + - " \"_type\" : \"" + type + "\",\n" + - " \"values\" : []\n" + - " }\n" + - "}"; - alternateVersions.put(contentString1, testQuery); - - //array of types can also be called type rather than types - String contentString2 = "{\n" + - " \"ids\" : {\n" + - " \"type\" : [\"" + type + "\"],\n" + - " \"values\" : []\n" + - " }\n" + - "}"; - alternateVersions.put(contentString2, testQuery); - } - - return alternateVersions; - } - public void testIllegalArguments() { try { new IdsQueryBuilder((String[])null); @@ -166,4 +134,46 @@ public class IdsQueryBuilderTests extends AbstractQueryTestCase assertEquals(json, 3, parsed.ids().size()); assertEquals(json, "my_type", parsed.types()[0]); } + + public void testFromJsonDeprecatedSyntax() throws IOException { + IdsQueryBuilder tempQuery = createTestQueryBuilder(); + assumeTrue("test requires at least one type", tempQuery.types() != null && tempQuery.types().length > 0); + + String type = tempQuery.types()[0]; + IdsQueryBuilder testQuery = new IdsQueryBuilder(type); + + //single value type can also be called _type + String contentString = "{\n" + + " \"ids\" : {\n" + + " \"_type\" : \"" + type + "\",\n" + + " \"values\" : []\n" + + " }\n" + + "}"; + + IdsQueryBuilder parsed = (IdsQueryBuilder) parseQuery(contentString, ParseFieldMatcher.EMPTY); + assertEquals(testQuery, parsed); + + try { + parseQuery(contentString); + fail("parse should have failed"); + } catch(IllegalArgumentException e) { + assertEquals("Deprecated field [_type] used, expected [type] instead", e.getMessage()); + } + + //array of types can also be called type rather than types + contentString = "{\n" + + " \"ids\" : {\n" + + " \"types\" : [\"" + type + "\"],\n" + + " \"values\" : []\n" + + " }\n" + + "}"; + parsed = (IdsQueryBuilder) parseQuery(contentString, ParseFieldMatcher.EMPTY); + assertEquals(testQuery, parsed); + try { + parseQuery(contentString); + fail("parse should have failed"); + } catch(IllegalArgumentException e) { + assertEquals("Deprecated field [types] used, expected [type] instead", e.getMessage()); + } + } } diff --git a/test/framework/src/main/java/org/elasticsearch/test/AbstractQueryTestCase.java b/test/framework/src/main/java/org/elasticsearch/test/AbstractQueryTestCase.java index 92acd702e3a..f59afcb40d8 100644 --- a/test/framework/src/main/java/org/elasticsearch/test/AbstractQueryTestCase.java +++ b/test/framework/src/main/java/org/elasticsearch/test/AbstractQueryTestCase.java @@ -245,7 +245,7 @@ public abstract class AbstractQueryTestCase> assertParsedQuery(shuffled.bytes(), testQuery); for (Map.Entry alternateVersion : getAlternateVersions().entrySet()) { String queryAsString = alternateVersion.getKey(); - assertParsedQuery(new BytesArray(queryAsString), alternateVersion.getValue(), ParseFieldMatcher.EMPTY); + assertParsedQuery(new BytesArray(queryAsString), alternateVersion.getValue()); } } }