|
| 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 | +} |
0 commit comments