@Test
  public void testRenameAttribute() throws Throwable {
    logger.debug("Start testRenameAttribute");
    try {
      logger.debug("Create river {}", getRiver());
      String script = "ctx.document.score2 = ctx.document.score; delete ctx.document.score;";
      super.createRiver(
          "/test/elasticsearch/plugin/river/mongodb/script/test-mongodb-river-with-script.json",
          getRiver(),
          String.valueOf(getMongoPort1()),
          String.valueOf(getMongoPort2()),
          String.valueOf(getMongoPort3()),
          getDatabase(),
          getCollection(),
          script,
          getIndex(),
          getDatabase());

      String mongoDocument =
          copyToStringFromClasspath(
              "/test/elasticsearch/plugin/river/mongodb/script/test-simple-mongodb-document.json");
      DBObject dbObject = (DBObject) JSON.parse(mongoDocument);
      WriteResult result = mongoCollection.insert(dbObject);
      Thread.sleep(wait);
      String id = dbObject.get("_id").toString();
      logger.info("WriteResult: {}", result.toString());
      refreshIndex();

      ActionFuture<IndicesExistsResponse> response =
          getNode().client().admin().indices().exists(new IndicesExistsRequest(getIndex()));
      assertThat(response.actionGet().isExists(), equalTo(true));

      SearchResponse sr =
          getNode()
              .client()
              .prepareSearch(getIndex())
              .setQuery(fieldQuery("_id", id))
              .execute()
              .actionGet();
      logger.debug("SearchResponse {}", sr.toString());
      long totalHits = sr.getHits().getTotalHits();
      logger.debug("TotalHits: {}", totalHits);
      assertThat(totalHits, equalTo(1l));

      assertThat(sr.getHits().getHits()[0].sourceAsMap().containsKey("score2"), equalTo(true));
      mongoCollection.remove(dbObject);
    } catch (Throwable t) {
      logger.error("testRenameAttribute failed.", t);
      t.printStackTrace();
      throw t;
    } finally {
      super.deleteRiver();
      super.deleteIndex();
    }
  }
  @Test
  public void testIgnoreScript() throws Throwable {
    logger.debug("Start testIgnoreScript");
    try {
      logger.debug("Create river {}", getRiver());
      String script = "ctx.ignore = true;";
      super.createRiver(
          "/test/elasticsearch/plugin/river/mongodb/script/test-mongodb-river-with-script.json",
          getRiver(),
          String.valueOf(getMongoPort1()),
          String.valueOf(getMongoPort2()),
          String.valueOf(getMongoPort3()),
          getDatabase(),
          getCollection(),
          script,
          getIndex(),
          getDatabase());

      String mongoDocument =
          copyToStringFromClasspath(
              "/test/elasticsearch/plugin/river/mongodb/script/test-simple-mongodb-document.json");
      DBObject dbObject = (DBObject) JSON.parse(mongoDocument);
      WriteResult result = mongoCollection.insert(dbObject);
      Thread.sleep(wait);
      logger.info("WriteResult: {}", result.toString());
      refreshIndex();

      ActionFuture<IndicesExistsResponse> response =
          getNode().client().admin().indices().exists(new IndicesExistsRequest(getIndex()));
      assertThat(response.actionGet().isExists(), equalTo(true));
      CountResponse countResponse = getNode().client().count(countRequest(getIndex())).actionGet();
      logger.info("Document count: {}", countResponse.getCount());
      assertThat(countResponse.getCount(), equalTo(0l));

      mongoCollection.remove(dbObject);

    } catch (Throwable t) {
      logger.error("testIgnoreScript failed.", t);
      t.printStackTrace();
      throw t;
    } finally {
      super.deleteRiver();
      super.deleteIndex();
    }
  }
 @BeforeClass
 public void createDatabase() {
   logger.debug("createDatabase {}", getDatabase());
   try {
     mongoDB = getMongo().getDB(getDatabase());
     mongoDB.setWriteConcern(WriteConcern.REPLICAS_SAFE);
     super.createRiver(
         "/test/elasticsearch/plugin/river/mongodb/gridfs/test-gridfs-mongodb-river.json");
     ActionFuture<IndicesExistsResponse> response =
         getNode().client().admin().indices().exists(new IndicesExistsRequest(getIndex()));
     assertThat(response.actionGet().isExists(), equalTo(true));
     logger.info("Start createCollection");
     mongoCollection = mongoDB.createCollection(getCollection(), null);
     Assert.assertNotNull(mongoCollection);
   } catch (Throwable t) {
     logger.error("createDatabase failed.", t);
   }
 }
 @Override
 public T actionGet(TimeValue timeout) throws ElasticsearchException {
   return in.actionGet(timeout);
 }
 @Override
 public T actionGet(long timeout, TimeUnit unit) throws ElasticsearchException {
   return in.actionGet(timeout, unit);
 }
 @Override
 public T actionGet(long timeoutMillis) throws ElasticsearchException {
   return in.actionGet(timeoutMillis);
 }
  public void testPlugin() throws Exception {
    client()
        .admin()
        .indices()
        .prepareCreate("test")
        .addMapping(
            "type1",
            jsonBuilder()
                .startObject()
                .startObject("type1")
                .startObject("properties")
                .startObject("test")
                .field("type", "text")
                .endObject()
                .startObject("num1")
                .field("type", "date")
                .endObject()
                .endObject()
                .endObject()
                .endObject())
        .execute()
        .actionGet();
    client()
        .admin()
        .cluster()
        .prepareHealth()
        .setWaitForEvents(Priority.LANGUID)
        .setWaitForYellowStatus()
        .execute()
        .actionGet();

    client()
        .index(
            indexRequest("test")
                .type("type1")
                .id("1")
                .source(
                    jsonBuilder()
                        .startObject()
                        .field("test", "value")
                        .field("num1", "2013-05-26")
                        .endObject()))
        .actionGet();
    client()
        .index(
            indexRequest("test")
                .type("type1")
                .id("2")
                .source(
                    jsonBuilder()
                        .startObject()
                        .field("test", "value")
                        .field("num1", "2013-05-27")
                        .endObject()))
        .actionGet();

    client().admin().indices().prepareRefresh().execute().actionGet();
    DecayFunctionBuilder<?> gfb = new CustomDistanceScoreBuilder("num1", "2013-05-28", "+1d");

    ActionFuture<SearchResponse> response =
        client()
            .search(
                searchRequest()
                    .searchType(SearchType.QUERY_THEN_FETCH)
                    .source(
                        searchSource()
                            .explain(false)
                            .query(functionScoreQuery(termQuery("test", "value"), gfb))));

    SearchResponse sr = response.actionGet();
    ElasticsearchAssertions.assertNoFailures(sr);
    SearchHits sh = sr.getHits();

    assertThat(sh.hits().length, equalTo(2));
    assertThat(sh.getAt(0).getId(), equalTo("1"));
    assertThat(sh.getAt(1).getId(), equalTo("2"));
  }
  @Test
  public void initialImport() throws Throwable {
    logger.debug("Start InitialImport");
    try {
      createDatabase();

      DBObject dbObject1 = new BasicDBObject(ImmutableMap.of("name", "Richard"));
      WriteResult result1 = mongoCollection.insert(dbObject1);
      logger.info("WriteResult: {}", result1.toString());
      Thread.sleep(wait);

      // Make sure we're starting out with the river not setup
      GetResponse statusResponse =
          getNode()
              .client()
              .prepareGet("_river", river, MongoDBRiver.STATUS_ID)
              .execute()
              .actionGet();
      Assert.assertFalse(
          statusResponse.isExists(),
          "Expected no river but found one "
              + XContentMapValues.extractValue(
                  MongoDBRiver.TYPE + "." + MongoDBRiver.STATUS_FIELD,
                  statusResponse.getSourceAsMap()));

      // Setup the river
      createRiver();
      Thread.sleep(wait);

      // Check that it did an initial import successfully
      ActionFuture<IndicesExistsResponse> response =
          getNode().client().admin().indices().exists(new IndicesExistsRequest(getIndex()));
      assertThat(response.actionGet().isExists(), equalTo(true));
      Assert.assertEquals(
          Status.RUNNING, MongoDBRiverHelper.getRiverStatus(getNode().client(), river));
      assertThat(
          getNode().client().count(countRequest(getIndex())).actionGet().getCount(), equalTo(1l));

      // Check that it syncs the oplog
      DBObject dbObject2 = new BasicDBObject(ImmutableMap.of("name", "Ben"));
      WriteResult result2 = mongoCollection.insert(dbObject2);
      logger.info("WriteResult: {}", result2.toString());
      Thread.sleep(wait);

      refreshIndex();
      Assert.assertEquals(
          Status.RUNNING, MongoDBRiverHelper.getRiverStatus(getNode().client(), river));
      assertThat(
          getNode().client().count(countRequest(getIndex())).actionGet().getCount(), equalTo(2l));

      mongoCollection.remove(dbObject1, WriteConcern.REPLICAS_SAFE);

      Thread.sleep(wait);
      refreshIndex();
      assertThat(
          getNode().client().count(countRequest(getIndex())).actionGet().getCount(), equalTo(1L));

    } catch (Throwable t) {
      logger.error("InitialImport failed.", t);
      t.printStackTrace();
      throw t;
    } finally {
      cleanUp();
    }
  }