/*
 * 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
      }
    }
  }
}

/*
 * 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.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.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;

public abstract class AbstractElasticSearchTest extends ElasticsearchIntegrationTest {

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

  protected ObjectMapper mapper = new ObjectMapper();

  @Rule
  public ProcessEngineRule processEngineRule = new ProcessEngineRule();

  protected void showMappings(String... indices) throws IOException {
    ClusterStateResponse clusterStateResponse = admin().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
      }
    }
  }
}

upgrade elasticsearch to 1.0.1