/*
* 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.commons.ftest.functionalTestQuery;
import com.stratio.connector.commons.ftest.GenericConnectorTest;
import com.stratio.connector.commons.metadata.TableMetadataBuilder;
import com.stratio.connector.commons.test.util.LogicalWorkFlowCreator;
import com.stratio.crossdata.common.data.Cell;
import com.stratio.crossdata.common.data.ClusterName;
import com.stratio.crossdata.common.data.Row;
import com.stratio.crossdata.common.exceptions.ConnectorException;
import com.stratio.crossdata.common.logicalplan.LogicalWorkflow;
import com.stratio.crossdata.common.metadata.ColumnType;
import com.stratio.crossdata.common.metadata.DataType;
import com.stratio.crossdata.common.metadata.TableMetadata;
import com.stratio.crossdata.common.result.QueryResult;
import org.junit.Test;
import java.util.*;
import static org.junit.Assert.*;
/**
*
*/
public abstract class GenericQueryProjectFT extends GenericConnectorTest {
public static final String COLUMN_1 = "bin1";
public static final String COLUMN_2 = "bin2";
public static final String COLUMN_3 = "bin3";
private static final Integer DEFAULT_GET_TO_SEARCH = 1000;
protected Integer getRowsToSearch() {
return DEFAULT_GET_TO_SEARCH;
}
@Test
public void selectFilterProject() throws ConnectorException {
ClusterName clusterNodeName = getClusterName();
TableMetadataBuilder tableMetadataBuilder = new TableMetadataBuilder(CATALOG, TABLE, clusterNodeName.getName());
tableMetadataBuilder.addColumn(COLUMN_1, new ColumnType(DataType.VARCHAR)).addColumn(COLUMN_2,
new ColumnType(DataType.VARCHAR))
.addColumn(COLUMN_3, new ColumnType(DataType.VARCHAR));
TableMetadata targetTable = tableMetadataBuilder.build(getConnectorHelper().isPKMandatory());
if (getConnectorHelper().isTableMandatory()) {
connector.getMetadataEngine().createTable(getClusterName(), targetTable);
}
for (int i = 0; i < getRowsToSearch(); i++) {
insertRow(i, clusterNodeName, targetTable);
}
refresh(CATALOG);
LogicalWorkflow logicalPlan = createLogicalWorkFlow();
QueryResult queryResult = connector.getQueryEngine().execute("", logicalPlan);
Set<Object> probeSet = new HashSet<>();
Iterator<Row> rowIterator = queryResult.getResultSet().iterator();
while (rowIterator.hasNext()) {
Row row = rowIterator.next();
for (String cell : row.getCells().keySet()) {
probeSet.add(cell + row.getCell(cell).getValue());
}
}
assertEquals("The record number is not correct", getRowsToSearch(),
(Integer) queryResult.getResultSet().size());
for (int i = 0; i < getRowsToSearch(); i++) {
assertTrue("Return correct record", probeSet.contains("bin1ValueBin1_r" + i));
assertTrue("Return correct record", probeSet.contains("bin2ValueBin2_r" + i));
assertFalse("This record should not be returned", probeSet.contains("bin3ValueBin3_r+i"));
}
}
private LogicalWorkflow createLogicalWorkFlow() {
return new LogicalWorkFlowCreator(CATALOG, TABLE, getClusterName()).addColumnName(COLUMN_1, COLUMN_2)
.build();
}
private void insertRow(int ikey, ClusterName clusterNodeName, TableMetadata targetTable)
throws UnsupportedOperationException, ConnectorException {
Row row = new Row();
Map<String, Cell> cells = new HashMap<>();
cells.put(COLUMN_1, new Cell("ValueBin1_r" + ikey));
cells.put(COLUMN_2, new Cell("ValueBin2_r" + ikey));
cells.put(COLUMN_3, new Cell("ValueBin3_r" + ikey));
row.setCells(cells);
connector.getStorageEngine().insert(clusterNodeName, targetTable, row, false);
}
}