package core.framework.test.search;

import core.framework.impl.search.ElasticSearchImpl;
import core.framework.util.StopWatch;
import org.elasticsearch.client.Client;
import org.elasticsearch.common.network.NetworkModule;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.env.Environment;
import org.elasticsearch.node.NodeValidationException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.nio.file.Path;

/**
 * @author neo
 */
public class MockElasticSearch extends ElasticSearchImpl {
    private final Logger logger = LoggerFactory.getLogger(MockElasticSearch.class);
    private final Path dataPath;

    public MockElasticSearch(Path dataPath) {
        this.dataPath = dataPath;
    }

    @Override
    protected Client createClient() {
        StopWatch watch = new StopWatch();
        try {
            Settings.Builder settings = Settings.builder();
            settings.put(NetworkModule.HTTP_ENABLED.getKey(), false)
                    .put(Environment.PATH_HOME_SETTING.getKey(), dataPath);
            MockNode node = new MockNode(settings.build());
            node.start();
            return node.client();
        } catch (NodeValidationException e) {
            throw new Error(e);
        } finally {
            logger.info("create local elasticsearch node, dataPath={}, elapsedTime={}", dataPath, watch.elapsedTime());
        }
    }
}

@@ -252,25 +252,133 @@
 mon.
-settings.Settings
+network.NetworkService;%0Aimport org.elasticsearch.common.settings.Settings;%0Aimport org.elasticsearch.discovery.DiscoveryModule
 ;%0Aim
@@ -1043,125 +1043,307 @@
 put(
-NetworkModule.HTTP_ENABLED.getKey(), false)%0A                    .put(Environment.PATH_HOME_SETTING.getKey(), dataPath
+Environment.PATH_HOME_SETTING.getKey(), dataPath)%0A                    .put(NetworkModule.HTTP_ENABLED.getKey(), false)%0A                    .put(NetworkService.GLOBAL_NETWORK_BINDHOST_SETTING.getKey(), %22_local_%22)%0A                    .put(DiscoveryModule.DISCOVERY_TYPE_SETTING.getKey(), %22single-node%22
 );%0A 

disable zen discovery for ES integration test