Fix AbstractQueryTestCase.testUnknownObjectException()

When need to check the whole hierarchy of objects to know if the newly inserted "newField" object is part of an arbitrary holding object or not.

Reproduced with `gradle :modules:percolator:test -Dtests.seed=736B0B67DA7A3632 -Dtests.class=org.elasticsearch.percolator.PercolateQueryBuilderTests -Dtests.method="testUnknownObjectException" -Dtests.security.manager=true -Dtests.locale=es-ES -Dtests.timezone=ART`
This commit is contained in:
Tanguy Leroux 2016-08-25 16:24:22 +02:00
parent 3f350f33f1
commit fbcfddbb77
2 changed files with 43 additions and 10 deletions

View File

@ -114,9 +114,12 @@ import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.lang.reflect.Proxy; import java.lang.reflect.Proxy;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.Deque;
import java.util.HashSet; import java.util.HashSet;
import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
import java.util.Map; import java.util.Map;
@ -400,7 +403,9 @@ public abstract class AbstractQueryTestCase<QB extends AbstractQueryBuilder<QB>>
int mutation = 0; int mutation = 0;
while (true) { while (true) {
boolean expectedException = true; // Track the objects hierarchy
Deque<String> hierarchy = new LinkedList<>();
BytesStreamOutput out = new BytesStreamOutput(); BytesStreamOutput out = new BytesStreamOutput();
try ( try (
XContentGenerator generator = XContentType.JSON.xContent().createGenerator(out); XContentGenerator generator = XContentType.JSON.xContent().createGenerator(out);
@ -414,10 +419,8 @@ public abstract class AbstractQueryTestCase<QB extends AbstractQueryBuilder<QB>>
if (token == XContentParser.Token.START_OBJECT) { if (token == XContentParser.Token.START_OBJECT) {
objectIndex++; objectIndex++;
if (hasArbitraryContent) { if (objectIndex <= mutation) {
// The query has one or more fields that hold arbitrary content. If the current hierarchy.push(parser.currentName());
// field is one of those, no exception is expected when parsing the mutated query.
expectedException = arbitraryMarkers.contains(parser.currentName()) == false;
} }
if (objectIndex == mutation) { if (objectIndex == mutation) {
@ -439,15 +442,28 @@ public abstract class AbstractQueryTestCase<QB extends AbstractQueryBuilder<QB>>
// Check that the parser consumed all the tokens // Check that the parser consumed all the tokens
assertThat(token, nullValue()); assertThat(token, nullValue());
if (objectIndex == mutation) { if (objectIndex < mutation) {
// We reached the expected insertion point, so next time we'll try one level deeper
mutation++;
} else {
// We did not reached the insertion point, there's no more mutation to try // We did not reached the insertion point, there's no more mutation to try
break; break;
} else {
// We reached the expected insertion point, so next time we'll try one step further
mutation++;
}
}
boolean expectException = true;
if (hasArbitraryContent) {
// The query has one or more fields that hold arbitrary content. If the current
// field is one (or a child) of those, no exception is expected when parsing the mutated query.
for (String marker : arbitraryMarkers) {
if (hierarchy.contains(marker)) {
expectException = false;
break;
} }
} }
results.add(new Tuple<>(out.bytes().utf8ToString(), expectedException)); }
results.add(new Tuple<>(out.bytes().utf8ToString(), expectException));
} }
} }
return results; return results;

View File

@ -57,6 +57,23 @@ public class AbstractQueryTestCaseTests extends ESTestCase {
hasEntry("{\"bool\":{\"must\":[{\"newField\":{\"match\":{\"field\":\"value\"}}}]}}", true), hasEntry("{\"bool\":{\"must\":[{\"newField\":{\"match\":{\"field\":\"value\"}}}]}}", true),
hasEntry("{\"bool\":{\"must\":[{\"match\":{\"newField\":{\"field\":\"value\"}}}]}}", true) hasEntry("{\"bool\":{\"must\":[{\"match\":{\"newField\":{\"field\":\"value\"}}}]}}", true)
)); ));
alterations = alterateQueries(singleton("{\"function_score\":" +
"{\"query\": {\"term\":{\"foo\": \"bar\"}}, \"script_score\": {\"script\":\"a + 1\", \"params\": {\"a\":0}}}}"), null);
assertAlterations(alterations, allOf(
hasEntry("{\"newField\":{\"function_score\":{\"query\":{\"term\":{\"foo\":\"bar\"}},\"script_score\":{\"script\":\"a + " +
"1\",\"params\":{\"a\":0}}}}}", true),
hasEntry("{\"function_score\":{\"newField\":{\"query\":{\"term\":{\"foo\":\"bar\"}},\"script_score\":{\"script\":\"a + " +
"1\",\"params\":{\"a\":0}}}}}", true),
hasEntry("{\"function_score\":{\"query\":{\"newField\":{\"term\":{\"foo\":\"bar\"}}},\"script_score\":{\"script\":\"a + " +
"1\",\"params\":{\"a\":0}}}}", true),
hasEntry("{\"function_score\":{\"query\":{\"term\":{\"newField\":{\"foo\":\"bar\"}}},\"script_score\":{\"script\":\"a + " +
"1\",\"params\":{\"a\":0}}}}", true),
hasEntry("{\"function_score\":{\"query\":{\"term\":{\"foo\":\"bar\"}},\"script_score\":{\"newField\":{\"script\":\"a + " +
"1\",\"params\":{\"a\":0}}}}}", true),
hasEntry("{\"function_score\":{\"query\":{\"term\":{\"foo\":\"bar\"}},\"script_score\":{\"script\":\"a + 1\"," +
"\"params\":{\"newField\":{\"a\":0}}}}}", true)
));
} }
public void testAlterateQueriesWithArbitraryContent() throws IOException { public void testAlterateQueriesWithArbitraryContent() throws IOException {