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

import org.eclipse.birt.data.engine.api.IBaseExpression;
import org.eclipse.birt.data.engine.api.IConditionalExpression;
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.GroupDefinition;
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>Bug description</b>
 * <p>
 * The TopN do not work correctly on the two level grounp
 * <p>
 * <p>
 * Steps to reproduce:
 * </p>
 * <ol>
 * <li>Import the attached report desgin file</li>
 * <li>On the second grounp-row["productCode"].substr(0,2), add two filters:
 * one is that "Total.ave(row["ordrLineNumber"])" is larger than or equal to
 * 2,and the other is that "Total.ave(row["ordrLineNumber"])" is Top 2.</li>
 * <li>preview.</li>
 * </ol>
 * 
 * <p>
 * Actual result:
 * </p>
 * The records which "Total.ave(row["ordrLineNumber"])" is 1 are displayed
 * 
 * <p>
 * Expect result:
 * </p>
 * The records which "Total.ave(row["ordrLineNumber"])" is large than or equal
 * to 2 and "Total.ave(row["ordrLineNumber"])" is the top 2 in the grounp are
 * displayed.
 * <p>
 * <b>Test description</b>
 * </p>
 * <p>
 * Define two groups, and two filter definitions and add them to group[1],
 * compare the results with golden file.
 * </p>
 */

public class Regression_128354 extends APITestCase
{

	protected DataSourceInfo getDataSourceInfo( )
	{
		return new DataSourceInfo( ConfigText
				.getString( "Api.TestDataforGroup.TableName" ), ConfigText
				.getString( "Api.TestDataforGroup.TableSQL" ), ConfigText
				.getString( "Api.TestDataforGroup.TestDataFileName" ) );
	}

	public void test_TwoFiltersOnGroup( ) throws Exception
	{
		String[] bindingNameFilter = new String[1];
		bindingNameFilter[0] = "FILTER_AMOUNT";
		IBaseExpression[] bindingExprFilter = new IBaseExpression[1];
		bindingExprFilter[0] = new ScriptExpression( "dataSetRow.AMOUNT" );

		FilterDefinition filterDefn = new FilterDefinition(
				new ConditionalExpression( "Total.ave(row.ROW_AMOUNT)",
						IConditionalExpression.OP_TOP_N, "3" ) );

		String[] bindingNameGroup = new String[2];
		bindingNameGroup[0] = "GROUP_GROUP0";
		bindingNameGroup[1] = "GROUP_GROUP1";

		IBaseExpression[] bindingExprGroup = new IBaseExpression[2];
		bindingExprGroup[0] = new ScriptExpression( "dataSetRow.COUNTRY" );
		bindingExprGroup[1] = new ScriptExpression( "dataSetRow.CITY" );

		GroupDefinition[] groupDefn = new GroupDefinition[]{
				new GroupDefinition( "group0" ), new GroupDefinition( "group1" )};
		groupDefn[0].setKeyExpression( "row.GROUP_GROUP0" );
		groupDefn[1].setKeyExpression( "row.GROUP_GROUP1" );
		groupDefn[1].addFilter( filterDefn );

		// add the second filter to group1

		filterDefn = new FilterDefinition( new ConditionalExpression(
				"Total.ave(row.ROW_AMOUNT)", IConditionalExpression.OP_GE,
				"200" ) );
		groupDefn[1].setKeyExpression( "row.GROUP_GROUP1" );

		groupDefn[1].addFilter( filterDefn );

		String[] bindingNameRow = new String[4];
		bindingNameRow[0] = "ROW_COUNTRY";
		bindingNameRow[1] = "ROW_CITY";
		bindingNameRow[2] = "ROW_SALE_DATE";
		bindingNameRow[3] = "ROW_AMOUNT";
		IBaseExpression[] bindingExprRow = new IBaseExpression[4];
		bindingExprRow[0] = new ScriptExpression( "dataSetRow.COUNTRY" );
		bindingExprRow[1] = new ScriptExpression( "dataSetRow.CITY" );
		bindingExprRow[2] = new ScriptExpression(
				"Total.ave(row.ROW_AMOUNT,null,2)" );
		bindingExprRow[3] = new ScriptExpression( "dataSetRow.AMOUNT" );

		QueryDefinition qd = this.createQuery( bindingNameGroup,
				bindingExprGroup, groupDefn, null, null, null, null, null,
				null, bindingNameRow, bindingExprRow );
		String[] columnStr = new String[]{"COUNTRY", "CITY", "AVE_AMOUNT",
				"AMOUNT"};

		String outputStr = getOutputStrForGroupTest( 30, qd, groupDefn.length,
				bindingNameRow, columnStr );
		// use DtE output framework
		testPrint( outputStr );
		// golden file compare

		// assert
		checkOutputFile( );

	}

}