
package org.eclipse.birt.tests.data.engine.regression;

import org.eclipse.birt.core.data.DataType;
import org.eclipse.birt.data.engine.api.IBaseExpression;
import org.eclipse.birt.data.engine.api.querydefn.BaseDataSetDesign;
import org.eclipse.birt.data.engine.api.querydefn.ComputedColumn;
import org.eclipse.birt.data.engine.api.querydefn.ConditionalExpression;
import org.eclipse.birt.data.engine.api.querydefn.FilterDefinition;
import org.eclipse.birt.data.engine.api.querydefn.OdaDataSetDesign;
import org.eclipse.birt.data.engine.api.querydefn.QueryDefinition;
import org.eclipse.birt.data.engine.api.querydefn.ScriptExpression;

import testutil.APITestCase;
import testutil.ConfigText;

/**
 * <b>Regression test case description:</b>
 * <p>
 * Result set is empty when add computed column and filter with operator: top
 * n/bottom n/top percent n/bottom percent n
 * </p>
 * <p>
 * Description:
 * </p>
 * <p>
 * Steps to reproduce:
 * </p>
 * <ol>
 * <li>Add a computed column for a data set</li>
 * <li>Add a filter in edit data set with operator TOP N/BOTTOM N/TOP percent
 * N/bottom percent n</li>
 * <li>Preview</li>
 * </ol>
 * <p>
 * Actual result:
 * </p>
 * <p>
 * Result set is null, yet actually there are many rows of data exist in dataset
 * </p>
 * <p>
 * <b>Test description:</b>
 * </p>
 * <p>
 * Construct a computed column based on the dataSet and add a filter with
 * conditional expression Bottom N on it, to compare the result table with
 * corresponding golden file.
 * </p>
 */

public class Regression_117686 extends APITestCase
{

	String callsTableName;

	protected void setUp( ) throws Exception
	{
		super.setUp( );

		prepareDataSet( new DataSourceInfo( ConfigText
				.getString( "Api.TestData0.TableName" ), ConfigText
				.getString( "Api.TestData0.TableSQL" ), ConfigText
				.getString( "Api.TestData0.TestDataFileName" ) ) );

		callsTableName = ConfigText.getString( "Api.TestData0.TableName" );
	}

	/*
	 * @see org.eclipse.birt.data.engine.api.APITestCase#getDataSourceInfo()
	 */
	protected DataSourceInfo getDataSourceInfo( )
	{
		return new DataSourceInfo( ConfigText
				.getString( "Api.TestData.TableName" ), ConfigText
				.getString( "Api.TestData.TableSQL" ), ConfigText
				.getString( "Api.TestData.TestDataFileName" ) );
	}

	public void testFilter_TopNOnCompCol( ) throws Exception
	{
		String testSQL = "select " + callsTableName + ".AMOUNT1 " + "from "
				+ callsTableName;

		( (OdaDataSetDesign) this.dataSet ).setQueryText( testSQL );

		String[] ccName = new String[]{"ccc"};
		String[] ccExpr = new String[]{"dataSetRow.AMOUNT1"};

		ComputedColumn computedColumn = new ComputedColumn( ccName[0],
				ccExpr[0], DataType.ANY_TYPE );
		( (BaseDataSetDesign) this.dataSet ).addComputedColumn( computedColumn );

		String[] bindingNameFilter = new String[1];
		bindingNameFilter[0] = "FILTER_AMOUNT";
		IBaseExpression[] bindingExprFilter = new IBaseExpression[1];
		bindingExprFilter[0] = new ScriptExpression( "dataSetRow.ccc" );
		FilterDefinition[] filters = new FilterDefinition[]{new FilterDefinition(
				new ConditionalExpression( "row.FILTER_AMOUNT",
						ConditionalExpression.OP_BOTTOM_N, "3" ) )};

		String[] bindingNameRow = new String[2];
		bindingNameRow[0] = "ROW_AMOUNT1";

		bindingNameRow[1] = "ROW_ccc";
		ScriptExpression[] bindingExprRow = new ScriptExpression[]{
				new ScriptExpression( "dataSetRow.AMOUNT1" ),

				new ScriptExpression( "dataSetRow.ccc" )};

		QueryDefinition qd = this.createQuery( null, null, null, null, null,
				null, bindingNameFilter, bindingExprFilter, filters,
				bindingNameRow, bindingExprRow );

		String[] columnStr = new String[]{"AMOUNT1", "ccc"};
		String outputStr = getOutputStrForGroupTest( 30, qd, 0, bindingNameRow,
				columnStr );
		// use DtE output framework
		testPrint( outputStr );
		// golden file compare
		this.checkOutputFile( );
		checkOutputFile( );

	}
}