/*
 * Copyright 2013 - Christian Lipphardt and camunda services GmbH
 *
 * Licensed 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.camunda.bpm.elasticsearch;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.camunda.bpm.engine.test.ProcessEngineRule;
import org.elasticsearch.action.admin.cluster.state.ClusterStateResponse;
import org.elasticsearch.client.AdminClient;
import org.elasticsearch.client.Client;
import org.elasticsearch.cluster.metadata.IndexMetaData;
import org.elasticsearch.cluster.metadata.MappingMetaData;
import org.elasticsearch.common.collect.ImmutableOpenMap;
import org.elasticsearch.common.hppc.cursors.ObjectCursor;
import org.elasticsearch.node.Node;
import org.elasticsearch.test.ElasticsearchIntegrationTest;
import org.junit.Rule;

import java.io.IOException;
import java.util.Map;
import java.util.logging.Logger;

import static org.elasticsearch.client.Requests.clusterStateRequest;
import static org.elasticsearch.client.Requests.refreshRequest;

public abstract class AbstractElasticSearchTest extends ElasticsearchIntegrationTest {

  protected Logger logger = Logger.getLogger(AbstractElasticSearchTest.class.getName());

  protected ObjectMapper mapper = new ObjectMapper();

  protected Node node;

  protected Client client;

  protected AdminClient adminClient;

  @Rule
  public ProcessEngineRule processEngineRule = new ProcessEngineRule();

  protected void ensureIndexRefreshed(String... indices) {
    adminClient.indices().refresh(refreshRequest(indices)).actionGet();
  }

  protected void showMappings(String... indices) throws IOException {
    ClusterStateResponse clusterStateResponse = adminClient.cluster()
        .state(clusterStateRequest()
        .blocks(true)
        .nodes(true)
        .indices(indices))
        .actionGet();

    for (IndexMetaData indexMetaData : clusterStateResponse.getState().getMetaData()) {
      printMapping(indexMetaData.getMappings());
    }
  }

  private void printMapping(ImmutableOpenMap<String, MappingMetaData> mappedMetaData) {
    for (ObjectCursor<MappingMetaData> metaDataEntry : mappedMetaData.values()) {
      try {
        Map<String, Object> sourceAsMap = metaDataEntry.value.getSourceAsMap();
        logger.info(mapper.writeValueAsString(sourceAsMap));
      } catch (IOException e) {
        // nop
      }
    }
  }
}

@@ -848,93 +848,8 @@
 se;%0A
-import org.elasticsearch.client.AdminClient;%0Aimport org.elasticsearch.client.Client;%0A
 impo
@@ -1081,44 +1081,8 @@
 or;%0A
-import org.elasticsearch.node.Node;%0A
 impo
@@ -1317,72 +1317,8 @@
 est;
-%0Aimport static org.elasticsearch.client.Requests.refreshRequest;
 %0A%0Apu
@@ -1554,311 +1554,85 @@
 %0A%0A  
-protected Node node;%0A%0A  protected Client client;%0A%0A  protected AdminClient adminClient;%0A%0A  @Rule%0A  public ProcessEngineRule processEngineRule = new ProcessEngineRule();%0A%0A  protected void ensureIndexRefreshed(String... indices) %7B%0A    adminClient.indices().refresh(refreshRequest(indices)).actionGet();%0A  %7D
+@Rule%0A  public ProcessEngineRule processEngineRule = new ProcessEngineRule();
 %0A%0A  
@@ -1756,14 +1756,10 @@
 dmin
-Client
+()
 .clu

upgrade elasticsearch to 1.0.1