package com.stratio.connector.deep;

import static com.stratio.connector.deep.LogicalWorkflowBuilder.createColumn;
import static com.stratio.connector.deep.LogicalWorkflowBuilder.createProject;
import static com.stratio.connector.deep.LogicalWorkflowBuilder.createSelect;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.apache.log4j.Logger;
import org.junit.BeforeClass;
import org.junit.Test;

import com.stratio.connector.deep.engine.query.DeepQueryEngine;
import com.stratio.crossdata.common.data.Row;
import com.stratio.crossdata.common.exceptions.ConnectionException;
import com.stratio.crossdata.common.exceptions.ExecutionException;
import com.stratio.crossdata.common.exceptions.InitializationException;
import com.stratio.crossdata.common.exceptions.UnsupportedException;
import com.stratio.crossdata.common.logicalplan.LogicalStep;
import com.stratio.crossdata.common.logicalplan.LogicalWorkflow;
import com.stratio.crossdata.common.logicalplan.Project;
import com.stratio.crossdata.common.metadata.ColumnMetadata;
import com.stratio.crossdata.common.result.QueryResult;

/**
 * Functional tests using MongoDB
 */
public class DeepConnectorAerospikeFT {

    private static final Logger logger = Logger.getLogger(DeepConnectorAerospikeFT.class);

    private static final String KEYSPACE = "book";

    private static final String MYTABLE1_CONSTANT = "input";

    private static final String AEROSPIKE_CLUSTERNAME_CONSTANT = "aerospike";

    private static DeepQueryEngine deepQueryEngine;

    @BeforeClass
    public static void setUp() throws InitializationException, ConnectionException, UnsupportedException {
        ConnectionsHandler connectionBuilder = new ConnectionsHandler();
        connectionBuilder.connect(AerospikeConnectionConfigurationBuilder.prepareConfiguration());
        deepQueryEngine = connectionBuilder.getQueryEngine();
    }

    @Test
    public void testSingleProjectAndSelectTest() throws UnsupportedException, ExecutionException {

        // Input data
        List<LogicalStep> stepList = new ArrayList<>();
        Project project = createProject(AEROSPIKE_CLUSTERNAME_CONSTANT, KEYSPACE, MYTABLE1_CONSTANT,
                Arrays.asList("cantos", "id", "metadata"));
        project.setNextStep(createSelect(Arrays.asList(createColumn(KEYSPACE, MYTABLE1_CONSTANT,
                "cantos")), Arrays.asList("cantos")));

        // One single initial step
        stepList.add(project);

        LogicalWorkflow logicalWorkflow = new LogicalWorkflow(stepList);

        // Execution
        QueryResult result = deepQueryEngine.executeWorkFlow(logicalWorkflow);

        // Assertions
        List<ColumnMetadata> columnsMetadata = result.getResultSet().getColumnMetadata();
        List<Row> rowsList = result.getResultSet().getRows();
    }
}

@@ -264,16 +264,66 @@
 eSelect;
+%0Aimport static org.jgroups.util.Util.assertEquals;
 %0A%0Aimport
@@ -475,24 +475,169 @@
 unit.Test;%0A%0A
+import com.aerospike.client.AerospikeClient;%0Aimport com.aerospike.client.Bin;%0Aimport com.aerospike.client.Host;%0Aimport com.aerospike.client.Key;%0A
 import com.s
@@ -1534,12 +1534,12 @@
  = %22
-book
+test
 %22;%0A%0A
@@ -2086,24 +2086,767 @@
 ryEngine();%0A
+        Host%5B%5D hosts = %7B   new Host(AerospikeConnectionConfigurationBuilder.HOST,Integer.valueOf%0A                (AerospikeConnectionConfigurationBuilder%0A                .PORT))%7D;%0A        AerospikeClient client =  new AerospikeClient(null, hosts);%0A        client.put(null,new Key(KEYSPACE,MYTABLE1_CONSTANT,1),new Bin(%22id%22,1),new Bin(%22cantos%22,%22cantos1%22),new Bin%0A                (%22metadata%22,%22metadata1%22));%0A        client.put(null,new Key(KEYSPACE,MYTABLE1_CONSTANT,2),new Bin(%22id%22,2),new Bin(%22cantos%22,%22cantos2%22),new Bin%0A                (%22metadata%22,%22metadata3%22));%0A        client.put(null,new Key(KEYSPACE,MYTABLE1_CONSTANT,3),new Bin(%22id%22,3),new Bin(%22cantos%22,%22cantos3%22),new Bin%0A                (%22metadata%22,%22metadata3%22));%0A        client.close();%0A
     %7D%0A%0A    @
@@ -3606,16 +3606,18 @@
 ertions%0A
+%0A%0A
         
@@ -3625,144 +3625,130 @@
 ist%3C
-ColumnMetadata%3E columnsMetadata = result.getResultSet().getColumnMetadata();%0A        List%3CRow%3E rowsList = result.getResultSet().getRows(
+Row%3E rowsList = result.getResultSet().getRows();%0A        assertEquals(%22We revive the correct row number%22,3,rowsList.size()
 );%0A 
@@ -3752,10 +3752,12 @@
 ;%0A    %7D%0A
+%0A%0A
 %7D%0A

add assert in aeropike FT