fix mocking in tests

Original commit: elastic/x-pack-elasticsearch@b43de1ea2b
This commit is contained in:
Martijn van Groningen 2017-04-25 09:52:12 +02:00
parent 85f573156f
commit 6c0613f36f
2 changed files with 5 additions and 10 deletions

View File

@ -68,13 +68,12 @@ import org.elasticsearch.index.shard.IndexShard;
import org.elasticsearch.index.shard.ShardId; import org.elasticsearch.index.shard.ShardId;
import org.elasticsearch.indices.TermsLookup; import org.elasticsearch.indices.TermsLookup;
import org.elasticsearch.license.XPackLicenseState; import org.elasticsearch.license.XPackLicenseState;
import org.elasticsearch.script.CompiledScript;
import org.elasticsearch.script.ExecutableScript;
import org.elasticsearch.script.Script; import org.elasticsearch.script.Script;
import org.elasticsearch.script.ScriptContext; import org.elasticsearch.script.ScriptContext;
import org.elasticsearch.script.ScriptService; import org.elasticsearch.script.ScriptService;
import org.elasticsearch.script.ScriptType; import org.elasticsearch.script.ScriptType;
import org.elasticsearch.search.aggregations.LeafBucketCollector; import org.elasticsearch.search.aggregations.LeafBucketCollector;
import org.elasticsearch.template.CompiledTemplate;
import org.elasticsearch.test.ESTestCase; import org.elasticsearch.test.ESTestCase;
import org.elasticsearch.test.IndexSettingsModule; import org.elasticsearch.test.IndexSettingsModule;
import org.elasticsearch.xpack.security.authz.accesscontrol.DocumentSubsetReader.DocumentSubsetDirectoryReader; import org.elasticsearch.xpack.security.authz.accesscontrol.DocumentSubsetReader.DocumentSubsetDirectoryReader;
@ -458,10 +457,8 @@ public class SecurityIndexSearcherWrapperUnitTests extends ESTestCase {
} }
}; };
CompiledScript compiledScript = mock(CompiledScript.class); CompiledTemplate compiledScript = mock(CompiledTemplate.class);
when(scriptService.compile(any(Script.class), eq(ScriptContext.Standard.SEARCH))).thenReturn(compiledScript); when(scriptService.compileTemplate(any(Script.class), eq(ScriptContext.Standard.SEARCH))).thenReturn(compiledScript);
ExecutableScript executableScript = mock(ExecutableScript.class);
when(scriptService.executable(eq(compiledScript), any())).thenReturn(executableScript);
XContentBuilder builder = jsonBuilder(); XContentBuilder builder = jsonBuilder();
String query = new TermQueryBuilder("field", "{{_user.username}}").toXContent(builder, ToXContent.EMPTY_PARAMS).string(); String query = new TermQueryBuilder("field", "{{_user.username}}").toXContent(builder, ToXContent.EMPTY_PARAMS).string();
@ -472,7 +469,7 @@ public class SecurityIndexSearcherWrapperUnitTests extends ESTestCase {
securityIndexSearcherWrapper.evaluateTemplate(querySource); securityIndexSearcherWrapper.evaluateTemplate(querySource);
ArgumentCaptor<Script> argument = ArgumentCaptor.forClass(Script.class); ArgumentCaptor<Script> argument = ArgumentCaptor.forClass(Script.class);
verify(scriptService).compile(argument.capture(), eq(ScriptContext.Standard.SEARCH)); verify(scriptService).compileTemplate(argument.capture(), eq(ScriptContext.Standard.SEARCH));
Script usedScript = argument.getValue(); Script usedScript = argument.getValue();
assertThat(usedScript.getIdOrCode(), equalTo(script.getIdOrCode())); assertThat(usedScript.getIdOrCode(), equalTo(script.getIdOrCode()));
assertThat(usedScript.getType(), equalTo(script.getType())); assertThat(usedScript.getType(), equalTo(script.getType()));

View File

@ -5,7 +5,6 @@
*/ */
package org.elasticsearch.xpack.watcher.transform.script; package org.elasticsearch.xpack.watcher.transform.script;
import org.elasticsearch.common.bytes.BytesArray;
import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.common.xcontent.XContentParser;
@ -15,7 +14,6 @@ import org.elasticsearch.script.Script;
import org.elasticsearch.script.ScriptException; import org.elasticsearch.script.ScriptException;
import org.elasticsearch.script.ScriptService; import org.elasticsearch.script.ScriptService;
import org.elasticsearch.script.ScriptType; import org.elasticsearch.script.ScriptType;
import org.elasticsearch.template.CompiledTemplate;
import org.elasticsearch.test.ESTestCase; import org.elasticsearch.test.ESTestCase;
import org.elasticsearch.threadpool.TestThreadPool; import org.elasticsearch.threadpool.TestThreadPool;
import org.elasticsearch.threadpool.ThreadPool; import org.elasticsearch.threadpool.ThreadPool;
@ -171,7 +169,7 @@ public class ScriptTransformTests extends ESTestCase {
String errorMessage = "expected error message"; String errorMessage = "expected error message";
ScriptException scriptException = new ScriptException(errorMessage, new RuntimeException("foo"), ScriptException scriptException = new ScriptException(errorMessage, new RuntimeException("foo"),
Collections.emptyList(), "whatever", "whatever"); Collections.emptyList(), "whatever", "whatever");
when(scriptService.compileTemplate(anyObject(), eq(Watcher.SCRIPT_CONTEXT))).thenThrow(scriptException); when(scriptService.compile(anyObject(), eq(Watcher.SCRIPT_CONTEXT))).thenThrow(scriptException);
ScriptTransformFactory transformFactory = new ScriptTransformFactory(Settings.builder().build(), scriptService); ScriptTransformFactory transformFactory = new ScriptTransformFactory(Settings.builder().build(), scriptService);