Skip to content

Commit dd34612

Browse files
Add unit test for Groovy script #87
1 parent a4dea64 commit dd34612

File tree

6 files changed

+249
-13
lines changed

6 files changed

+249
-13
lines changed

Diff for: src/test/java/test/elasticsearch/plugin/river/mongodb/RiverMongoDBTestAsbtract.java

+11-7
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828

2929
import java.util.ArrayList;
3030
import java.util.List;
31+
import java.util.Map;
3132

3233
import org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse;
3334
import org.elasticsearch.action.admin.indices.mapping.delete.DeleteMappingRequest;
@@ -260,9 +261,6 @@ private void setupElasticsearchServer() throws Exception {
260261
try {
261262
Tuple<Settings, Environment> initialSettings = InternalSettingsPerparer
262263
.prepareSettings(settings, true);
263-
PluginManager pluginManager = new PluginManager(
264-
initialSettings.v2(), null);
265-
266264
if (!initialSettings.v2().configFile().exists()) {
267265
FileSystemUtils.mkdirs(initialSettings.v2().configFile());
268266
}
@@ -273,10 +271,16 @@ private void setupElasticsearchServer() throws Exception {
273271

274272
if (!initialSettings.v2().pluginsFile().exists()) {
275273
FileSystemUtils.mkdirs(initialSettings.v2().pluginsFile());
276-
pluginManager.downloadAndExtract(
277-
settings.get("plugins.mapper-attachments"), false);
278-
pluginManager.downloadAndExtract(
279-
settings.get("plugins.lang-javascript"), false);
274+
if (settings.getByPrefix("plugins") != null) {
275+
PluginManager pluginManager = new PluginManager(
276+
initialSettings.v2(), null);
277+
278+
Map<String, String> plugins = settings.getByPrefix("plugins").getAsMap();
279+
for(String key : plugins.keySet()) {
280+
pluginManager.downloadAndExtract(
281+
plugins.get(key), false);
282+
}
283+
}
280284
} else {
281285
logger.info("Plugin {} has been already installed.",
282286
settings.get("plugins.mapper-attachments"));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,230 @@
1+
/*
2+
* Licensed to Elastic Search and Shay Banon under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. Elastic Search licenses this
6+
* file to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package test.elasticsearch.plugin.river.mongodb.script;
20+
21+
import static org.elasticsearch.client.Requests.countRequest;
22+
import static org.elasticsearch.common.io.Streams.copyToStringFromClasspath;
23+
import static org.elasticsearch.index.query.QueryBuilders.fieldQuery;
24+
import static org.hamcrest.MatcherAssert.assertThat;
25+
import static org.hamcrest.Matchers.equalTo;
26+
27+
import org.elasticsearch.action.ActionFuture;
28+
import org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsRequest;
29+
import org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsResponse;
30+
import org.elasticsearch.action.count.CountResponse;
31+
import org.elasticsearch.action.search.SearchResponse;
32+
import org.testng.Assert;
33+
import org.testng.annotations.AfterClass;
34+
import org.testng.annotations.BeforeClass;
35+
import org.testng.annotations.Test;
36+
37+
import test.elasticsearch.plugin.river.mongodb.RiverMongoDBTestAsbtract;
38+
39+
import com.mongodb.DB;
40+
import com.mongodb.DBCollection;
41+
import com.mongodb.DBObject;
42+
import com.mongodb.WriteConcern;
43+
import com.mongodb.WriteResult;
44+
import com.mongodb.util.JSON;
45+
46+
@Test
47+
public class RiverMongoGroovyScriptTest extends RiverMongoDBTestAsbtract {
48+
49+
private static final String GROOVY_SCRIPT_TYPE = "groovy";
50+
private DB mongoDB;
51+
private DBCollection mongoCollection;
52+
53+
protected RiverMongoGroovyScriptTest() {
54+
super("testgroovyriver-" + System.currentTimeMillis(), "testgroovydatabase-"
55+
+ System.currentTimeMillis(), "groovydocuments-"
56+
+ System.currentTimeMillis(), "testgroovyindex-"
57+
+ System.currentTimeMillis());
58+
}
59+
60+
@BeforeClass
61+
public void createDatabase() {
62+
logger.debug("createDatabase {}", getDatabase());
63+
try {
64+
mongoDB = getMongo().getDB(getDatabase());
65+
mongoDB.setWriteConcern(WriteConcern.REPLICAS_SAFE);
66+
67+
logger.info("Start createCollection");
68+
mongoCollection = mongoDB.createCollection(getCollection(), null);
69+
Assert.assertNotNull(mongoCollection);
70+
} catch (Throwable t) {
71+
logger.error("createDatabase failed.", t);
72+
}
73+
}
74+
75+
@AfterClass
76+
public void cleanUp() {
77+
logger.info("Drop database " + mongoDB.getName());
78+
mongoDB.dropDatabase();
79+
}
80+
81+
@Test
82+
public void testIgnoreScript() throws Throwable {
83+
logger.debug("Start testIgnoreScript");
84+
try {
85+
logger.debug("Create river {}", getRiver());
86+
String script = "ctx.ignore = true";
87+
super.createRiver(
88+
TEST_MONGODB_RIVER_WITH_SCRIPT_JSON,
89+
getRiver(), String.valueOf(getMongoPort1()),
90+
String.valueOf(getMongoPort2()),
91+
String.valueOf(getMongoPort3()), getDatabase(),
92+
getCollection(), GROOVY_SCRIPT_TYPE, script, getIndex(), getDatabase());
93+
94+
String mongoDocument = copyToStringFromClasspath(TEST_SIMPLE_MONGODB_DOCUMENT_JSON);
95+
DBObject dbObject = (DBObject) JSON.parse(mongoDocument);
96+
WriteResult result = mongoCollection.insert(dbObject);
97+
Thread.sleep(wait);
98+
logger.info("WriteResult: {}", result.toString());
99+
refreshIndex();
100+
101+
ActionFuture<IndicesExistsResponse> response = getNode().client()
102+
.admin().indices()
103+
.exists(new IndicesExistsRequest(getIndex()));
104+
assertThat(response.actionGet().isExists(), equalTo(true));
105+
CountResponse countResponse = getNode().client()
106+
.count(countRequest(getIndex())).actionGet();
107+
logger.info("Document count: {}", countResponse.getCount());
108+
assertThat(countResponse.getCount(), equalTo(0l));
109+
110+
mongoCollection.remove(dbObject);
111+
112+
} catch (Throwable t) {
113+
logger.error("testIgnoreScript failed.", t);
114+
t.printStackTrace();
115+
throw t;
116+
} finally {
117+
super.deleteRiver();
118+
super.deleteIndex();
119+
}
120+
}
121+
122+
@Test
123+
public void testUpdateAttribute() throws Throwable {
124+
logger.debug("Start testUpdateAttribute");
125+
try {
126+
logger.debug("Create river {}", getRiver());
127+
String script = "def now = new Date(); println 'Now: ${now}'; ctx.document.modified = now.clearTime();";
128+
super.createRiver(
129+
TEST_MONGODB_RIVER_WITH_SCRIPT_JSON,
130+
getRiver(), String.valueOf(getMongoPort1()),
131+
String.valueOf(getMongoPort2()),
132+
String.valueOf(getMongoPort3()), getDatabase(),
133+
getCollection(), GROOVY_SCRIPT_TYPE,script, getIndex(), getDatabase());
134+
135+
String mongoDocument = copyToStringFromClasspath(TEST_SIMPLE_MONGODB_DOCUMENT_JSON);
136+
DBObject dbObject = (DBObject) JSON.parse(mongoDocument);
137+
WriteResult result = mongoCollection.insert(dbObject);
138+
Thread.sleep(wait);
139+
String id = dbObject.get("_id").toString();
140+
logger.info("WriteResult: {}", result.toString());
141+
refreshIndex();
142+
143+
ActionFuture<IndicesExistsResponse> response = getNode().client()
144+
.admin().indices()
145+
.exists(new IndicesExistsRequest(getIndex()));
146+
assertThat(response.actionGet().isExists(), equalTo(true));
147+
148+
SearchResponse sr = getNode().client().prepareSearch(getIndex())
149+
.setQuery(fieldQuery("_id", id)).execute().actionGet();
150+
logger.debug("SearchResponse {}", sr.toString());
151+
long totalHits = sr.getHits().getTotalHits();
152+
logger.debug("TotalHits: {}", totalHits);
153+
assertThat(totalHits, equalTo(1l));
154+
155+
assertThat(
156+
sr.getHits().getHits()[0].sourceAsMap()
157+
.containsKey("modified"), equalTo(true));
158+
String modified = sr.getHits().getHits()[0]
159+
.sourceAsMap().get("modified").toString();
160+
161+
logger.debug("modified: {}", modified);
162+
163+
mongoCollection.remove(dbObject, WriteConcern.REPLICAS_SAFE);
164+
165+
} catch (Throwable t) {
166+
logger.error("testUpdateAttribute failed.", t);
167+
t.printStackTrace();
168+
throw t;
169+
} finally {
170+
super.deleteRiver();
171+
super.deleteIndex();
172+
}
173+
}
174+
175+
@Test
176+
public void testDeleteDocument() throws Throwable {
177+
logger.debug("Start testDeleteDocument");
178+
try {
179+
logger.debug("Create river {}", getRiver());
180+
String script = "if (ctx.document.to_be_deleted) { ctx.operation = 'd' }";
181+
super.createRiver(
182+
TEST_MONGODB_RIVER_WITH_SCRIPT_JSON,
183+
getRiver(), String.valueOf(getMongoPort1()),
184+
String.valueOf(getMongoPort2()),
185+
String.valueOf(getMongoPort3()), getDatabase(),
186+
getCollection(), GROOVY_SCRIPT_TYPE,script, getIndex(), getDatabase());
187+
188+
String mongoDocument = copyToStringFromClasspath(TEST_SIMPLE_MONGODB_DOCUMENT_JSON);
189+
DBObject dbObject = (DBObject) JSON.parse(mongoDocument);
190+
WriteResult result = mongoCollection.insert(dbObject);
191+
Thread.sleep(wait);
192+
String id = dbObject.get("_id").toString();
193+
logger.info("WriteResult: {}", result.toString());
194+
refreshIndex();
195+
196+
ActionFuture<IndicesExistsResponse> response = getNode().client()
197+
.admin().indices()
198+
.exists(new IndicesExistsRequest(getIndex()));
199+
assertThat(response.actionGet().isExists(), equalTo(true));
200+
201+
SearchResponse sr = getNode().client().prepareSearch(getIndex())
202+
.setQuery(fieldQuery("_id", id)).execute().actionGet();
203+
logger.debug("SearchResponse {}", sr.toString());
204+
long totalHits = sr.getHits().getTotalHits();
205+
logger.debug("TotalHits: {}", totalHits);
206+
assertThat(totalHits, equalTo(1l));
207+
208+
dbObject.put("to_be_deleted", Boolean.TRUE);
209+
mongoCollection.save(dbObject);
210+
211+
Thread.sleep(wait);
212+
refreshIndex();
213+
214+
CountResponse countResponse = getNode().client()
215+
.count(countRequest(getIndex())).actionGet();
216+
logger.info("Document count: {}", countResponse.getCount());
217+
assertThat(countResponse.getCount(), equalTo(0l));
218+
219+
mongoCollection.remove(dbObject);
220+
} catch (Throwable t) {
221+
logger.error("testDeleteDocument failed.", t);
222+
t.printStackTrace();
223+
throw t;
224+
} finally {
225+
super.deleteRiver();
226+
super.deleteIndex();
227+
}
228+
}
229+
230+
}

Diff for: src/test/java/test/elasticsearch/plugin/river/mongodb/script/RiverMongoParentChildScriptTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ private void createIndicesAndMappings() {
133133
(Object) String.valueOf(getMongoPort2()),
134134
(Object) String.valueOf(getMongoPort3()),
135135
(Object) DATABASE_NAME, (Object) BOOKS_COLLECTION,
136-
(Object) script, (Object) INDEX_NAME, (Object) BOOK_TYPE);
136+
(Object) "js",script, (Object) INDEX_NAME, (Object) BOOK_TYPE);
137137
} catch (Throwable t) {
138138
logger.error("createIndicesAndMappings failed.", t);
139139
Assert.fail("createIndicesAndMappings failed.", t);

Diff for: src/test/java/test/elasticsearch/plugin/river/mongodb/script/RiverMongoScriptTest.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ public void testIgnoreScript() throws Throwable {
9393
getRiver(), String.valueOf(getMongoPort1()),
9494
String.valueOf(getMongoPort2()),
9595
String.valueOf(getMongoPort3()), getDatabase(),
96-
getCollection(), script, getIndex(), getDatabase());
96+
getCollection(), "js", script, getIndex(), getDatabase());
9797

9898
String mongoDocument = copyToStringFromClasspath(TEST_SIMPLE_MONGODB_DOCUMENT_JSON);
9999
DBObject dbObject = (DBObject) JSON.parse(mongoDocument);
@@ -134,7 +134,7 @@ public void testUpdateAttribute() throws Throwable {
134134
getRiver(), String.valueOf(getMongoPort1()),
135135
String.valueOf(getMongoPort2()),
136136
String.valueOf(getMongoPort3()), getDatabase(),
137-
getCollection(), script, getIndex(), getDatabase());
137+
getCollection(), "js",script, getIndex(), getDatabase());
138138

139139
String mongoDocument = copyToStringFromClasspath(TEST_SIMPLE_MONGODB_DOCUMENT_JSON);
140140
DBObject dbObject = (DBObject) JSON.parse(mongoDocument);
@@ -188,7 +188,7 @@ public void testRemoveAttribute() throws Throwable {
188188
getRiver(), String.valueOf(getMongoPort1()),
189189
String.valueOf(getMongoPort2()),
190190
String.valueOf(getMongoPort3()), getDatabase(),
191-
getCollection(), script, getIndex(), getDatabase());
191+
getCollection(), "js",script, getIndex(), getDatabase());
192192

193193
String mongoDocument = copyToStringFromClasspath(TEST_SIMPLE_MONGODB_DOCUMENT_JSON);
194194
DBObject dbObject = (DBObject) JSON.parse(mongoDocument);
@@ -235,7 +235,7 @@ public void testRenameAttribute() throws Throwable {
235235
getRiver(), String.valueOf(getMongoPort1()),
236236
String.valueOf(getMongoPort2()),
237237
String.valueOf(getMongoPort3()), getDatabase(),
238-
getCollection(), script, getIndex(), getDatabase());
238+
getCollection(), "js",script, getIndex(), getDatabase());
239239

240240
String mongoDocument = copyToStringFromClasspath(TEST_SIMPLE_MONGODB_DOCUMENT_JSON);
241241
DBObject dbObject = (DBObject) JSON.parse(mongoDocument);
@@ -282,7 +282,7 @@ public void testDeleteDocument() throws Throwable {
282282
getRiver(), String.valueOf(getMongoPort1()),
283283
String.valueOf(getMongoPort2()),
284284
String.valueOf(getMongoPort3()), getDatabase(),
285-
getCollection(), script, getIndex(), getDatabase());
285+
getCollection(), "js",script, getIndex(), getDatabase());
286286

287287
String mongoDocument = copyToStringFromClasspath(TEST_SIMPLE_MONGODB_DOCUMENT_JSON);
288288
DBObject dbObject = (DBObject) JSON.parse(mongoDocument);

Diff for: src/test/java/test/elasticsearch/plugin/river/mongodb/script/test-mongodb-river-with-script.json

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
"db": "%s",
2020
"collection": "%s",
2121
"gridfs": false,
22+
"scriptType": "%s",
2223
"script": "%s"
2324
},
2425
"index": {

Diff for: src/test/resources/settings.yml

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ cluster:
2424
plugins:
2525
mapper-attachments: elasticsearch/elasticsearch-mapper-attachments/1.7.0
2626
lang-javascript: elasticsearch/elasticsearch-lang-javascript/1.4.0
27+
lang-groovy: elasticsearch/elasticsearch-lang-groovy/1.4.0
2728

2829
mongodb:
2930
version: 2.4.4

0 commit comments

Comments
 (0)