From 5e835f007164865b6b8a1a49eeb8d606562dac0e Mon Sep 17 00:00:00 2001 From: Alexander Reelsen Date: Mon, 18 Nov 2013 11:00:00 +0100 Subject: [PATCH] Added test for Update API via native scripts Motivation was to have a test ready as well as something other people could have a look at. --- .../update/UpdateByNativeScriptTests.java | 103 ++++++++++++++++++ 1 file changed, 103 insertions(+) create mode 100644 src/test/java/org/elasticsearch/update/UpdateByNativeScriptTests.java diff --git a/src/test/java/org/elasticsearch/update/UpdateByNativeScriptTests.java b/src/test/java/org/elasticsearch/update/UpdateByNativeScriptTests.java new file mode 100644 index 00000000000..13079fad1b2 --- /dev/null +++ b/src/test/java/org/elasticsearch/update/UpdateByNativeScriptTests.java @@ -0,0 +1,103 @@ +/* + * Licensed to ElasticSearch and Shay Banon 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.update; + +import com.google.common.collect.Maps; +import org.elasticsearch.common.Nullable; +import org.elasticsearch.common.settings.ImmutableSettings; +import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.script.AbstractExecutableScript; +import org.elasticsearch.script.ExecutableScript; +import org.elasticsearch.script.NativeScriptFactory; +import org.elasticsearch.test.ElasticsearchIntegrationTest; +import org.elasticsearch.test.ElasticsearchIntegrationTest.ClusterScope; +import org.elasticsearch.test.ElasticsearchIntegrationTest.Scope; +import org.junit.Test; + +import java.util.Map; + +import static org.hamcrest.Matchers.hasKey; +import static org.hamcrest.Matchers.is; + +/** + * + */ +@ClusterScope(scope=Scope.SUITE, numNodes=1) +public class UpdateByNativeScriptTests extends ElasticsearchIntegrationTest { + + @Override + protected Settings nodeSettings(int nodeOrdinal) { + return ImmutableSettings.settingsBuilder() + .put("script.native.custom.type", CustomNativeScriptFactory.class.getName()) + .put(super.nodeSettings(nodeOrdinal)) + .build(); + } + + @Test + public void testThatUpdateUsingNativeScriptWorks() throws Exception { + prepareCreate("test").setSettings(ImmutableSettings.settingsBuilder().put("index.number_of_shards", 1).put("index.number_of_replicas", 0).build()).get(); + ensureGreen(); + + index("test", "type", "1", "text", "value"); + + Map params = Maps.newHashMap(); + params.put("foo", "SETVALUE"); + client().prepareUpdate("test", "type", "1").setScript("custom").setScriptLang("native").setScriptParams(params).get(); + + Map data = client().prepareGet("test", "type", "1").get().getSource(); + assertThat(data, hasKey("foo")); + assertThat(data.get("foo").toString(), is("SETVALUE")); + } + + static class CustomNativeScriptFactory implements NativeScriptFactory { + @Override + public ExecutableScript newScript(@Nullable Map params) { + return new CustomScript(params); + } + } + + static class CustomScript extends AbstractExecutableScript { + private Map params; + private Map vars = Maps.newHashMapWithExpectedSize(2); + + public CustomScript(Map params) { + this.params = params; + } + + @Override + public Object run() { + if (vars.containsKey("ctx") && vars.get("ctx") instanceof Map) { + Map ctx = (Map) vars.get("ctx"); + if (ctx.containsKey("_source") && ctx.get("_source") instanceof Map) { + Map source = (Map) ctx.get("_source"); + source.putAll(params); + } + } + // return value does not matter, the UpdateHelper class + return null; + } + + @Override + public void setNextVar(String name, Object value) { + vars.put(name, value); + } + + } + +}