// Path: connector-elasticsearch/src/main/java/com/stratio/connector/elasticsearch/core/connection/ElasticSearchConnectionHandler.java
// public class ElasticSearchConnectionHandler extends ConnectionHandler {
// 
//     /**
//      * Constructor.
//      *
//      * @param configuration the configuration.
//      */
//     public ElasticSearchConnectionHandler(IConfiguration configuration) {
//         super(configuration);
//     }
// 
//     /**
//      * This method creates a elasticsearch connection.
//      *
//      * @param iCredentials           the credentials to connect with the database.
//      * @param connectorClusterConfig the cluster configuration.
//      * @throws ConnectionException if the connection is not established.
//      * @return a elasticsearch connection.
//      */
//     @Override
//     @TimerJ
//     protected Connection createNativeConnection(ICredentials iCredentials,
//             ConnectorClusterConfig connectorClusterConfig) throws ConnectionException {
//         Connection connection;
//         if (isNodeClient(connectorClusterConfig)) {
//             connection = new NodeConnection(iCredentials, connectorClusterConfig);
//         }
//         else {
//             try {connection = new TransportConnection(iCredentials, connectorClusterConfig);}
//             catch (ExecutionException exception){
//                 throw new ConnectionException("The connection could not be established", exception);
//             }
//         }
//         if (!connection.isConnected()) {
//              throw new ConnectionException("The connection could not be established");}
//         return connection;
//     }
// 
//     /**
//      * Return true if the config says that the connection is nodeClient. false in other case.
//      *
//      * @param config the configuration.
//      * @return true if is configure to be a node connection. False in other case.
//      */
//     @TimerJ
//     private boolean isNodeClient(ConnectorClusterConfig config) {
//         return Boolean.parseBoolean(config.getConnectorOptions().get(ConfigurationOptions.NODE_TYPE.getManifestOption()));
//     }
// 
// }
// 
// Path: connector-elasticsearch/src/main/java/com/stratio/connector/elasticsearch/core/connection/NodeConnection.java
// public class NodeConnection extends Connection<Client> {
// 
//     /**
//      * The Log.
//      */
//     private final Logger logger = LoggerFactory.getLogger(this.getClass());
// 
//     /**
//      * The Elasticsearch client.
//      */
//     private Client elasticClient = null;
// 
//     /**
//      * The elasticsearch node connection.
//      */
//     private Node node = null;
// 
// 
//     /**
//      * Store the connection name.
//      */
//     private String connectionName;
// 
//     /**
//      * Constructor.
//      *
//      * @param credentials the credentials.
//      * @param config      The cluster configuration.
//      */
//     public NodeConnection(ICredentials credentials, ConnectorClusterConfig config) {
//         NodeBuilder nodeBuilder = nodeBuilder();
// 
//         node = nodeBuilder.settings(ElasticsearchClientConfiguration.getSettings(config)).node();
//         elasticClient = node.client();
// 
//         connectionName = config.getName().getName();
//         logger.info("Elasticsearch Node connection established ");
// 
//     }
// 
//     /**
//      * Close the connection.
//      */
//     @TimerJ
//     public void close() {
//         if (node != null) {
//             node.close();
//             node = null;
//             elasticClient = null;
//             logger.info("ElasticSearch  connection [" + connectionName + "] close");
//         }
// 
//     }
// 
//     /**
//      * Retun the connection status.
//      *
//      * @return true if the connection is open. False in other case.
//      */
//     @Override
//     @TimerJ
//     public boolean isConnected() {
//         return (node!=null && !node.isClosed());
//     }
// 
//     /**
//      * Return the native connection.
//      *
//      * @return the native connection.
//      */
//     @Override
//     @TimerJ
//     public Client getNativeConnection() {
//         return elasticClient;
//     }
// 
// }


import java.util.HashMap;
import java.util.Map;
import static org.mockito.Mockito.*;
import com.stratio.connector.elasticsearch.core.connection.ElasticSearchConnectionHandler;
import com.stratio.connector.elasticsearch.core.connection.NodeConnection;
import com.stratio.crossdata.common.connector.ConnectorClusterConfig;
import com.stratio.crossdata.common.data.ClusterName;
import com.stratio.crossdata.common.security.ICredentials;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.internal.util.reflection.Whitebox;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
/*
 * Licensed to STRATIO (C) under one or more contributor license agreements.
 * See the NOTICE file distributed with this work for additional information
 * regarding copyright ownership.  The STRATIO (C) licenses this file
 * to you 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 com.stratio.connector.elasticsearch.core;




/**
 * ElasticsearchConnector Tester.
 *
 * @author <Authors name>
 * @version 1.0
 */
@RunWith(PowerMockRunner.class)

@PrepareForTest(value = { NodeConnection.class, ElasticsearchConnector.class })
public class ElasticsearchConnectorTest {

    private static final String CLUSTER_NAME = "CLUSTER_NAME";
    private ElasticsearchConnector elasticsearchConnector = null;

    @Before
    public void before() throws Exception {

        elasticsearchConnector = new ElasticsearchConnector();
    }

    @After
    public void after() throws Exception {
    }

    /**
     * Method: init(IConfiguration configuration)
     */

    /**
     * Method: close()
     */
    @Test
    public void testConnect() throws Exception {

        ICredentials iCredentials = mock(ICredentials.class);
        ClusterName clusterName = new ClusterName(CLUSTER_NAME);
        Map<String, String> connectorOptions = new HashMap<>();
        Map<String, String> clusterOptions = new HashMap<>();

        ConnectorClusterConfig config = new ConnectorClusterConfig(clusterName, connectorOptions, clusterOptions);

// Path: connector-elasticsearch/src/main/java/com/stratio/connector/elasticsearch/core/connection/ElasticSearchConnectionHandler.java
// public class ElasticSearchConnectionHandler extends ConnectionHandler {
// 
//     /**
//      * Constructor.
//      *
//      * @param configuration the configuration.
//      */
//     public ElasticSearchConnectionHandler(IConfiguration configuration) {
//         super(configuration);
//     }
// 
//     /**
//      * This method creates a elasticsearch connection.
//      *
//      * @param iCredentials           the credentials to connect with the database.
//      * @param connectorClusterConfig the cluster configuration.
//      * @throws ConnectionException if the connection is not established.
//      * @return a elasticsearch connection.
//      */
//     @Override
//     @TimerJ
//     protected Connection createNativeConnection(ICredentials iCredentials,
//             ConnectorClusterConfig connectorClusterConfig) throws ConnectionException {
//         Connection connection;
//         if (isNodeClient(connectorClusterConfig)) {
//             connection = new NodeConnection(iCredentials, connectorClusterConfig);
//         }
//         else {
//             try {connection = new TransportConnection(iCredentials, connectorClusterConfig);}
//             catch (ExecutionException exception){
//                 throw new ConnectionException("The connection could not be established", exception);
//             }
//         }
//         if (!connection.isConnected()) {
//              throw new ConnectionException("The connection could not be established");}
//         return connection;
//     }
// 
//     /**
//      * Return true if the config says that the connection is nodeClient. false in other case.
//      *
//      * @param config the configuration.
//      * @return true if is configure to be a node connection. False in other case.
//      */
//     @TimerJ
//     private boolean isNodeClient(ConnectorClusterConfig config) {
//         return Boolean.parseBoolean(config.getConnectorOptions().get(ConfigurationOptions.NODE_TYPE.getManifestOption()));
//     }
// 
// }
// 
// Path: connector-elasticsearch/src/main/java/com/stratio/connector/elasticsearch/core/connection/NodeConnection.java
// public class NodeConnection extends Connection<Client> {
// 
//     /**
//      * The Log.
//      */
//     private final Logger logger = LoggerFactory.getLogger(this.getClass());
// 
//     /**
//      * The Elasticsearch client.
//      */
//     private Client elasticClient = null;
// 
//     /**
//      * The elasticsearch node connection.
//      */
//     private Node node = null;
// 
// 
//     /**
//      * Store the connection name.
//      */
//     private String connectionName;
// 
//     /**
//      * Constructor.
//      *
//      * @param credentials the credentials.
//      * @param config      The cluster configuration.
//      */
//     public NodeConnection(ICredentials credentials, ConnectorClusterConfig config) {
//         NodeBuilder nodeBuilder = nodeBuilder();
// 
//         node = nodeBuilder.settings(ElasticsearchClientConfiguration.getSettings(config)).node();
//         elasticClient = node.client();
// 
//         connectionName = config.getName().getName();
//         logger.info("Elasticsearch Node connection established ");
// 
//     }
// 
//     /**
//      * Close the connection.
//      */
//     @TimerJ
//     public void close() {
//         if (node != null) {
//             node.close();
//             node = null;
//             elasticClient = null;
//             logger.info("ElasticSearch  connection [" + connectionName + "] close");
//         }
// 
//     }
// 
//     /**
//      * Retun the connection status.
//      *
//      * @return true if the connection is open. False in other case.
//      */
//     @Override
//     @TimerJ
//     public boolean isConnected() {
//         return (node!=null && !node.isClosed());
//     }
// 
//     /**
//      * Return the native connection.
//      *
//      * @return the native connection.
//      */
//     @Override
//     @TimerJ
//     public Client getNativeConnection() {
//         return elasticClient;
//     }
// 
// }


// Path: connector-elasticsearch/src/test/java/com/stratio/connector/elasticsearch/core/ElasticsearchConnectorTest.java
import java.util.HashMap;
import java.util.Map;
import static org.mockito.Mockito.*;
import com.stratio.connector.elasticsearch.core.connection.ElasticSearchConnectionHandler;
import com.stratio.connector.elasticsearch.core.connection.NodeConnection;
import com.stratio.crossdata.common.connector.ConnectorClusterConfig;
import com.stratio.crossdata.common.data.ClusterName;
import com.stratio.crossdata.common.security.ICredentials;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.internal.util.reflection.Whitebox;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

/*
 * Licensed to STRATIO (C) under one or more contributor license agreements.
 * See the NOTICE file distributed with this work for additional information
 * regarding copyright ownership.  The STRATIO (C) licenses this file
 * to you 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 com.stratio.connector.elasticsearch.core;




/**
 * ElasticsearchConnector Tester.
 *
 * @author <Authors name>
 * @version 1.0
 */
@RunWith(PowerMockRunner.class)

@PrepareForTest(value = { NodeConnection.class, ElasticsearchConnector.class })
public class ElasticsearchConnectorTest {

    private static final String CLUSTER_NAME = "CLUSTER_NAME";
    private ElasticsearchConnector elasticsearchConnector = null;

    @Before
    public void before() throws Exception {

        elasticsearchConnector = new ElasticsearchConnector();
    }

    @After
    public void after() throws Exception {
    }

    /**
     * Method: init(IConfiguration configuration)
     */

    /**
     * Method: close()
     */
    @Test
    public void testConnect() throws Exception {

        ICredentials iCredentials = mock(ICredentials.class);
        ClusterName clusterName = new ClusterName(CLUSTER_NAME);
        Map<String, String> connectorOptions = new HashMap<>();
        Map<String, String> clusterOptions = new HashMap<>();

        ConnectorClusterConfig config = new ConnectorClusterConfig(clusterName, connectorOptions, clusterOptions);

        ElasticSearchConnectionHandler connectionHandle = mock(ElasticSearchConnectionHandler.class);