// Path: src/test/java/org/elasticsearch/node/MockNode.java
// public class MockNode extends Node {
// 
//     public MockNode(Environment tmpEnv, Collection<Class<? extends Plugin>> classpathPlugins) {
//         super(tmpEnv, Version.CURRENT, classpathPlugins);
//     }
// 
// }
// 
// Path: src/main/java/org/xbib/elasticsearch/plugin/knapsack/KnapsackPlugin.java
// public class KnapsackPlugin extends Plugin {
// 
//     private final static String ENABLED = "plugins.knapsack.enabled";
// 
//     private final Settings settings;
// 
//     @Inject
//     public KnapsackPlugin(Settings settings) {
//         this.settings = settings;
//     }
// 
//     @Override
//     public String name() {
//         return "knapsack";
//     }
// 
//     @Override
//     public String description() {
//         return "Knapsack import/export plugin for Elasticsearch";
//     }
// 
//     public void onModule(ActionModule module) {
//         if (settings.getAsBoolean(ENABLED, true)) {
//             module.registerAction(KnapsackExportAction.INSTANCE, TransportKnapsackExportAction.class);
//             module.registerAction(KnapsackPushAction.INSTANCE, TransportKnapsackPushAction.class);
//             module.registerAction(KnapsackImportAction.INSTANCE, TransportKnapsackImportAction.class);
//             module.registerAction(KnapsackPullAction.INSTANCE, TransportKnapsackPullAction.class);
//             module.registerAction(KnapsackStateAction.INSTANCE, TransportKnapsackStateAction.class);
//             module.registerAction(KnapsackAbortAction.INSTANCE, TransportKnapsackAbortAction.class);
//         }
//     }
// 
//     public void onModule(RestModule module) {
//         if (settings.getAsBoolean(ENABLED, true)) {
//             module.addRestAction(RestKnapsackExportAction.class);
//             module.addRestAction(RestKnapsackImportAction.class);
//             module.addRestAction(RestKnapsackPushAction.class);
//             module.addRestAction(RestKnapsackPullAction.class);
//             module.addRestAction(RestKnapsackStateAction.class);
//             module.addRestAction(RestKnapsackAbortAction.class);
//         }
//     }
// 
//     /*public void onModule(ClusterModule module) {
//         if ("node".equals(settings.get("client.type")) && settings.getAsBoolean(ENABLED, true)) {
//             module.registerClusterDynamicSetting(KnapsackService.EXPORT_STATE_SETTING_NAME, Validator.EMPTY);
//             module.registerClusterDynamicSetting(KnapsackService.IMPORT_STATE_SETTING_NAME, Validator.EMPTY);
//         }
//     }*/
// 
//     @Override
//     public Collection<Module> nodeModules() {
//         Collection<Module> modules = new ArrayList<>();
//         if ("node".equals(settings.get("client.type")) && settings.getAsBoolean(ENABLED, true)) {
//             modules.add(new KnapsackModule());
//         }
//         return modules;
//     }
// 
//     @Override
//     public Collection<Class<? extends LifecycleComponent>> nodeServices() {
//         Collection<Class<? extends LifecycleComponent>> services = new ArrayList<>();
//         if ("node".equals(settings.get("client.type")) && settings.getAsBoolean(ENABLED, true)) {
//             services.add(KnapsackService.class);
//         }
//         return services;
//     }
// 
// }


import org.elasticsearch.ElasticsearchTimeoutException;
import org.elasticsearch.action.admin.cluster.health.ClusterHealthAction;
import org.elasticsearch.action.admin.cluster.health.ClusterHealthRequest;
import org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse;
import org.elasticsearch.action.admin.cluster.node.info.NodesInfoRequest;
import org.elasticsearch.action.admin.cluster.node.info.NodesInfoResponse;
import org.elasticsearch.client.support.AbstractClient;
import org.elasticsearch.common.logging.ESLogger;
import org.elasticsearch.common.logging.ESLoggerFactory;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.env.Environment;
import org.elasticsearch.node.MockNode;
import org.elasticsearch.node.Node;
import org.elasticsearch.plugins.Plugin;
import org.junit.After;
import org.junit.Before;
import org.xbib.elasticsearch.plugin.knapsack.KnapsackPlugin;
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Random;
import java.util.Set;
import java.util.concurrent.atomic.AtomicInteger;
import static org.elasticsearch.common.settings.Settings.settingsBuilder;
        logger.info("closing all nodes");
        for (Node node : nodes.values()) {
            if (node != null) {
                node.close();
            }
        }
        nodes.clear();
        logger.info("all nodes closed");
    }

    protected void findNodeAddress() {
        NodesInfoRequest nodesInfoRequest = new NodesInfoRequest().transport(true);
        NodesInfoResponse response = client("1").admin().cluster().nodesInfo(nodesInfoRequest).actionGet();
        Object obj = response.iterator().next().getTransport().getAddress()
                .publishAddress();
        if (obj instanceof InetSocketTransportAddress) {
            InetSocketTransportAddress address = (InetSocketTransportAddress) obj;
            host = address.address().getHostName();
            port = address.address().getPort();
        }
    }

    private Node buildNode(String id) throws IOException {
        Settings nodeSettings = settingsBuilder()
                .put(getNodeSettings())
                .put("name", id)
                .build();
        logger.info("settings={}", nodeSettings.getAsMap());
        // ES 2.1 renders NodeBuilder as useless
        Set<Class<? extends Plugin>> plugins = new HashSet<>();
// Path: src/test/java/org/elasticsearch/node/MockNode.java
// public class MockNode extends Node {
// 
//     public MockNode(Environment tmpEnv, Collection<Class<? extends Plugin>> classpathPlugins) {
//         super(tmpEnv, Version.CURRENT, classpathPlugins);
//     }
// 
// }
// 
// Path: src/main/java/org/xbib/elasticsearch/plugin/knapsack/KnapsackPlugin.java
// public class KnapsackPlugin extends Plugin {
// 
//     private final static String ENABLED = "plugins.knapsack.enabled";
// 
//     private final Settings settings;
// 
//     @Inject
//     public KnapsackPlugin(Settings settings) {
//         this.settings = settings;
//     }
// 
//     @Override
//     public String name() {
//         return "knapsack";
//     }
// 
//     @Override
//     public String description() {
//         return "Knapsack import/export plugin for Elasticsearch";
//     }
// 
//     public void onModule(ActionModule module) {
//         if (settings.getAsBoolean(ENABLED, true)) {
//             module.registerAction(KnapsackExportAction.INSTANCE, TransportKnapsackExportAction.class);
//             module.registerAction(KnapsackPushAction.INSTANCE, TransportKnapsackPushAction.class);
//             module.registerAction(KnapsackImportAction.INSTANCE, TransportKnapsackImportAction.class);
//             module.registerAction(KnapsackPullAction.INSTANCE, TransportKnapsackPullAction.class);
//             module.registerAction(KnapsackStateAction.INSTANCE, TransportKnapsackStateAction.class);
//             module.registerAction(KnapsackAbortAction.INSTANCE, TransportKnapsackAbortAction.class);
//         }
//     }
// 
//     public void onModule(RestModule module) {
//         if (settings.getAsBoolean(ENABLED, true)) {
//             module.addRestAction(RestKnapsackExportAction.class);
//             module.addRestAction(RestKnapsackImportAction.class);
//             module.addRestAction(RestKnapsackPushAction.class);
//             module.addRestAction(RestKnapsackPullAction.class);
//             module.addRestAction(RestKnapsackStateAction.class);
//             module.addRestAction(RestKnapsackAbortAction.class);
//         }
//     }
// 
//     /*public void onModule(ClusterModule module) {
//         if ("node".equals(settings.get("client.type")) && settings.getAsBoolean(ENABLED, true)) {
//             module.registerClusterDynamicSetting(KnapsackService.EXPORT_STATE_SETTING_NAME, Validator.EMPTY);
//             module.registerClusterDynamicSetting(KnapsackService.IMPORT_STATE_SETTING_NAME, Validator.EMPTY);
//         }
//     }*/
// 
//     @Override
//     public Collection<Module> nodeModules() {
//         Collection<Module> modules = new ArrayList<>();
//         if ("node".equals(settings.get("client.type")) && settings.getAsBoolean(ENABLED, true)) {
//             modules.add(new KnapsackModule());
//         }
//         return modules;
//     }
// 
//     @Override
//     public Collection<Class<? extends LifecycleComponent>> nodeServices() {
//         Collection<Class<? extends LifecycleComponent>> services = new ArrayList<>();
//         if ("node".equals(settings.get("client.type")) && settings.getAsBoolean(ENABLED, true)) {
//             services.add(KnapsackService.class);
//         }
//         return services;
//     }
// 
// }


// Path: src/test/java/org/xbib/elasticsearch/util/NodeTestUtils.java
import org.elasticsearch.ElasticsearchTimeoutException;
import org.elasticsearch.action.admin.cluster.health.ClusterHealthAction;
import org.elasticsearch.action.admin.cluster.health.ClusterHealthRequest;
import org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse;
import org.elasticsearch.action.admin.cluster.node.info.NodesInfoRequest;
import org.elasticsearch.action.admin.cluster.node.info.NodesInfoResponse;
import org.elasticsearch.client.support.AbstractClient;
import org.elasticsearch.common.logging.ESLogger;
import org.elasticsearch.common.logging.ESLoggerFactory;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.env.Environment;
import org.elasticsearch.node.MockNode;
import org.elasticsearch.node.Node;
import org.elasticsearch.plugins.Plugin;
import org.junit.After;
import org.junit.Before;
import org.xbib.elasticsearch.plugin.knapsack.KnapsackPlugin;
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Random;
import java.util.Set;
import java.util.concurrent.atomic.AtomicInteger;
import static org.elasticsearch.common.settings.Settings.settingsBuilder;

        logger.info("closing all nodes");
        for (Node node : nodes.values()) {
            if (node != null) {
                node.close();
            }
        }
        nodes.clear();
        logger.info("all nodes closed");
    }

    protected void findNodeAddress() {
        NodesInfoRequest nodesInfoRequest = new NodesInfoRequest().transport(true);
        NodesInfoResponse response = client("1").admin().cluster().nodesInfo(nodesInfoRequest).actionGet();
        Object obj = response.iterator().next().getTransport().getAddress()
                .publishAddress();
        if (obj instanceof InetSocketTransportAddress) {
            InetSocketTransportAddress address = (InetSocketTransportAddress) obj;
            host = address.address().getHostName();
            port = address.address().getPort();
        }
    }

    private Node buildNode(String id) throws IOException {
        Settings nodeSettings = settingsBuilder()
                .put(getNodeSettings())
                .put("name", id)
                .build();
        logger.info("settings={}", nodeSettings.getAsMap());
        // ES 2.1 renders NodeBuilder as useless
        Set<Class<? extends Plugin>> plugins = new HashSet<>();
        plugins.add(KnapsackPlugin.class);