|
| 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.simple; |
| 20 | + |
| 21 | +import static org.elasticsearch.client.Requests.countRequest; |
| 22 | +import static org.elasticsearch.common.io.Streams.copyToStringFromClasspath; |
| 23 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 24 | +import static org.hamcrest.Matchers.equalTo; |
| 25 | + |
| 26 | +import org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsRequest; |
| 27 | +import org.elasticsearch.action.count.CountResponse; |
| 28 | +import org.testng.Assert; |
| 29 | +import org.testng.annotations.AfterClass; |
| 30 | +import org.testng.annotations.BeforeClass; |
| 31 | +import org.testng.annotations.Test; |
| 32 | + |
| 33 | +import test.elasticsearch.plugin.river.mongodb.RiverMongoDBTestAsbtract; |
| 34 | + |
| 35 | +import com.mongodb.DB; |
| 36 | +import com.mongodb.DBCollection; |
| 37 | +import com.mongodb.DBObject; |
| 38 | +import com.mongodb.WriteConcern; |
| 39 | +import com.mongodb.util.JSON; |
| 40 | + |
| 41 | +@Test |
| 42 | +public class RiverMongoInitialTimestampTest extends RiverMongoDBTestAsbtract { |
| 43 | + |
| 44 | + private static final String TEST_SIMPLE_MONGODB_RIVER_INITIAL_TIMESTAMP_JSON = "/test/elasticsearch/plugin/river/mongodb/simple/test-simple-mongodb-river-initial-timestamp.json"; |
| 45 | + private static final String GROOVY_SCRIPT_TYPE = "groovy"; |
| 46 | + private static final String JAVASCRIPT_SCRIPT_TYPE = "js"; |
| 47 | + private DB mongoDB; |
| 48 | + private DBCollection mongoCollection; |
| 49 | + |
| 50 | + protected RiverMongoInitialTimestampTest() { |
| 51 | + super("initial-timestamp-river-" + System.currentTimeMillis(), |
| 52 | + "initial-timestamp-river-" + System.currentTimeMillis(), |
| 53 | + "initial-timestamp-collection-" + System.currentTimeMillis(), |
| 54 | + "initial-timestamp-index-" + System.currentTimeMillis()); |
| 55 | + } |
| 56 | + |
| 57 | + protected RiverMongoInitialTimestampTest(String river, String database, |
| 58 | + String collection, String index) { |
| 59 | + super(river, database, collection, index); |
| 60 | + } |
| 61 | + |
| 62 | + @BeforeClass |
| 63 | + public void createDatabase() { |
| 64 | + logger.debug("createDatabase {}", getDatabase()); |
| 65 | + try { |
| 66 | + mongoDB = getMongo().getDB(getDatabase()); |
| 67 | + mongoDB.setWriteConcern(WriteConcern.REPLICAS_SAFE); |
| 68 | + logger.info("Start createCollection"); |
| 69 | + mongoCollection = mongoDB.createCollection(getCollection(), null); |
| 70 | + Assert.assertNotNull(mongoCollection); |
| 71 | + } catch (Throwable t) { |
| 72 | + logger.error("createDatabase failed.", t); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + @AfterClass |
| 77 | + public void cleanUp() { |
| 78 | + // super.deleteRiver(); |
| 79 | + logger.info("Drop database " + mongoDB.getName()); |
| 80 | + mongoDB.dropDatabase(); |
| 81 | + } |
| 82 | + |
| 83 | + //String script = "def now = new Date(); println 'Now: ${now}'; ctx.document.modified = now.clearTime();"; |
| 84 | + @Test |
| 85 | + public void testInitialTimestampInGroovy() throws Throwable { |
| 86 | + logger.debug("Start testInitialTimestampInGroovy"); |
| 87 | + try { |
| 88 | + String script = "import groovy.time.TimeCategory; use(TimeCategory){def date = new Date() + 5.second; date.time;}"; |
| 89 | + super.createRiver(TEST_SIMPLE_MONGODB_RIVER_INITIAL_TIMESTAMP_JSON, |
| 90 | + getRiver(), (Object) String.valueOf(getMongoPort1()), |
| 91 | + (Object) String.valueOf(getMongoPort2()), |
| 92 | + (Object) String.valueOf(getMongoPort3()), |
| 93 | + (Object) GROOVY_SCRIPT_TYPE, (Object) script, |
| 94 | + (Object) getDatabase(), (Object) getCollection(), |
| 95 | + (Object) getIndex(), (Object) getDatabase()); |
| 96 | + |
| 97 | + String mongoDocument = copyToStringFromClasspath(TEST_SIMPLE_MONGODB_DOCUMENT_JSON); |
| 98 | + DBObject dbObject = (DBObject) JSON.parse(mongoDocument); |
| 99 | + mongoCollection.insert(dbObject); |
| 100 | + Thread.sleep(wait); |
| 101 | + |
| 102 | + assertThat( |
| 103 | + getNode().client().admin().indices() |
| 104 | + .exists(new IndicesExistsRequest(getIndex())) |
| 105 | + .actionGet().isExists(), equalTo(true)); |
| 106 | + |
| 107 | + refreshIndex(); |
| 108 | + |
| 109 | + CountResponse countResponse = getNode().client() |
| 110 | + .count(countRequest(getIndex())).actionGet(); |
| 111 | + assertThat(countResponse.getCount(), equalTo(0L)); |
| 112 | + |
| 113 | + mongoCollection.remove(dbObject); |
| 114 | + |
| 115 | + // Wait 5 seconds and store a new document |
| 116 | + Thread.sleep(5000); |
| 117 | + |
| 118 | + dbObject = (DBObject) JSON.parse(mongoDocument); |
| 119 | + mongoCollection.insert(dbObject); |
| 120 | + Thread.sleep(wait); |
| 121 | + |
| 122 | + assertThat( |
| 123 | + getNode().client().admin().indices() |
| 124 | + .exists(new IndicesExistsRequest(getIndex())) |
| 125 | + .actionGet().isExists(), equalTo(true)); |
| 126 | + assertThat( |
| 127 | + getNode().client().admin().indices() |
| 128 | + .prepareTypesExists(getIndex()) |
| 129 | + .setTypes(getDatabase()).execute().actionGet() |
| 130 | + .isExists(), equalTo(true)); |
| 131 | + |
| 132 | + refreshIndex(); |
| 133 | + |
| 134 | + countResponse = getNode().client().count(countRequest(getIndex())) |
| 135 | + .actionGet(); |
| 136 | + assertThat(countResponse.getCount(), equalTo(1L)); |
| 137 | + |
| 138 | + mongoCollection.remove(dbObject); |
| 139 | + } catch (Throwable t) { |
| 140 | + logger.error("testInitialTimestampInGroovy failed.", t); |
| 141 | + t.printStackTrace(); |
| 142 | + throw t; |
| 143 | + } finally { |
| 144 | + super.deleteRiver(); |
| 145 | + super.deleteIndex(); |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + // Convert JavaScript types to Java types: http://stackoverflow.com/questions/6730062/passing-common-types-between-java-and-rhino-javascript |
| 150 | + @Test |
| 151 | + public void testInitialTimestampInJavascript() throws Throwable { |
| 152 | + logger.debug("Start testInitialTimestampInJavascript"); |
| 153 | + try { |
| 154 | + String script = "var date = new Date(); date.setSeconds(date.getSeconds() + 5); new java.lang.Long(date.getTime());"; |
| 155 | + super.createRiver(TEST_SIMPLE_MONGODB_RIVER_INITIAL_TIMESTAMP_JSON, |
| 156 | + getRiver(), (Object) String.valueOf(getMongoPort1()), |
| 157 | + (Object) String.valueOf(getMongoPort2()), |
| 158 | + (Object) String.valueOf(getMongoPort3()), |
| 159 | + (Object) JAVASCRIPT_SCRIPT_TYPE, (Object) script, |
| 160 | + (Object) getDatabase(), (Object) getCollection(), |
| 161 | + (Object) getIndex(), (Object) getDatabase()); |
| 162 | + |
| 163 | + String mongoDocument = copyToStringFromClasspath(TEST_SIMPLE_MONGODB_DOCUMENT_JSON); |
| 164 | + DBObject dbObject = (DBObject) JSON.parse(mongoDocument); |
| 165 | + mongoCollection.insert(dbObject); |
| 166 | + Thread.sleep(wait); |
| 167 | + |
| 168 | + assertThat( |
| 169 | + getNode().client().admin().indices() |
| 170 | + .exists(new IndicesExistsRequest(getIndex())) |
| 171 | + .actionGet().isExists(), equalTo(true)); |
| 172 | + |
| 173 | + refreshIndex(); |
| 174 | + |
| 175 | + CountResponse countResponse = getNode().client() |
| 176 | + .count(countRequest(getIndex())).actionGet(); |
| 177 | + assertThat(countResponse.getCount(), equalTo(0L)); |
| 178 | + |
| 179 | + mongoCollection.remove(dbObject); |
| 180 | + |
| 181 | + // Wait 5 seconds and store a new document |
| 182 | + Thread.sleep(5000); |
| 183 | + |
| 184 | + dbObject = (DBObject) JSON.parse(mongoDocument); |
| 185 | + mongoCollection.insert(dbObject); |
| 186 | + Thread.sleep(wait); |
| 187 | + |
| 188 | + assertThat( |
| 189 | + getNode().client().admin().indices() |
| 190 | + .exists(new IndicesExistsRequest(getIndex())) |
| 191 | + .actionGet().isExists(), equalTo(true)); |
| 192 | + assertThat( |
| 193 | + getNode().client().admin().indices() |
| 194 | + .prepareTypesExists(getIndex()) |
| 195 | + .setTypes(getDatabase()).execute().actionGet() |
| 196 | + .isExists(), equalTo(true)); |
| 197 | + |
| 198 | + refreshIndex(); |
| 199 | + |
| 200 | + countResponse = getNode().client().count(countRequest(getIndex())) |
| 201 | + .actionGet(); |
| 202 | + assertThat(countResponse.getCount(), equalTo(1L)); |
| 203 | + |
| 204 | + mongoCollection.remove(dbObject); |
| 205 | + } catch (Throwable t) { |
| 206 | + logger.error("testInitialTimestampInJavascript failed.", t); |
| 207 | + t.printStackTrace(); |
| 208 | + throw t; |
| 209 | + } finally { |
| 210 | + super.deleteRiver(); |
| 211 | + super.deleteIndex(); |
| 212 | + } |
| 213 | + } |
| 214 | +} |
0 commit comments