/*******************************************************************************
 * Copyright (c) 2004, 2005 Actuate Corporation. All rights reserved. This
 * program and the accompanying materials are made available under the terms of
 * the Eclipse Public License v1.0 which accompanies this distribution, and is
 * available at http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors: Actuate Corporation - initial API and implementation
 ******************************************************************************/

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

import org.eclipse.birt.data.engine.api.IBaseExpression;
import org.eclipse.birt.data.engine.api.IGroupDefinition;
import org.eclipse.birt.data.engine.api.ISortDefinition;
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 org.eclipse.birt.data.engine.api.querydefn.SortDefinition;
import org.eclipse.birt.data.engine.core.DataException;
import org.eclipse.birt.data.engine.i18n.ResourceConstants;

import testutil.APITestCase;
import testutil.ConfigText;

/**
 * DtE features test.
 */
public class DsSortTest extends APITestCase
{

	/*
	 * @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" ) );
	}

	/**
	 * Test feature without any group, sort, filter
	 */
	public void test_SimpleExpression( ) throws Exception
	{
		String[] bindingNameRow = new String[4];
		bindingNameRow[0] = "ROW_1";
		bindingNameRow[1] = "ROW_2";
		bindingNameRow[2] = "ROW_3";
		bindingNameRow[3] = "ROW_4";
		IBaseExpression[] bindingExprRow = new IBaseExpression[4];
		bindingExprRow[0] = new ScriptExpression( "dataSetRow.COUNTRY", 0 );
		bindingExprRow[1] = new ScriptExpression( "dataSetRow.CITY", 0 );
		bindingExprRow[2] = new ScriptExpression( "dataSetRow.AMOUNT", 0 );
		bindingExprRow[3] = new ScriptExpression( "dataSetRow.SALE_DATE", 6 );

		String[] columnStr = new String[]{"Country", "City", "Amount", "Date",};

		QueryDefinition qd = this.createQuery( null, null, null, null, null,
				null, null, null, null, bindingNameRow, bindingExprRow );
		String outputStr = getOutputStrForGroupTest( 20, qd, 0, bindingNameRow,
				columnStr );
		testPrint( outputStr );
		checkOutputFile( );
	}

	/**
	 * Test feature of Sort with two level group
	 */
	public void test_SortWithGroup( ) throws Exception
	{
		String[] bindingNameGroup = new String[2];
		bindingNameGroup[0] = "GROUP_GROUP1";
		bindingNameGroup[1] = "GROUP_GROUP2";
		IBaseExpression[] bindingExprGroup = new IBaseExpression[2];
		bindingExprGroup[0] = new ScriptExpression( "dataSetRow.COUNTRY" );
		bindingExprGroup[1] = new ScriptExpression( "dataSetRow.SALE_DATE" );

		GroupDefinition[] groupDefn = new GroupDefinition[]{
				new GroupDefinition( "group1" ), new GroupDefinition( "group2" )};
		groupDefn[0].setKeyExpression( "row.GROUP_GROUP1" );
		groupDefn[1].setKeyExpression( "row.GROUP_GROUP2" );
		groupDefn[1].setIntervalRange( 2 );
		groupDefn[1].setInterval( IGroupDefinition.YEAR_INTERVAL );

		SortDefinition[] sortDefn = new SortDefinition[]{new SortDefinition( )};
		sortDefn[0].setExpression( "dataSetRow.AMOUNT" );
		sortDefn[0].setSortDirection( ISortDefinition.SORT_DESC );

		String[] bindingNameRow = new String[4];
		bindingNameRow[0] = "ROW_1";
		bindingNameRow[1] = "ROW_2";
		bindingNameRow[2] = "ROW_3";
		bindingNameRow[3] = "ROW_4";
		IBaseExpression[] bindingExprRow = new IBaseExpression[4];
		bindingExprRow[0] = new ScriptExpression( "dataSetRow.COUNTRY", 0 );
		bindingExprRow[1] = new ScriptExpression( "dataSetRow.SALE_DATE", 6 );
		bindingExprRow[2] = new ScriptExpression( "dataSetRow.AMOUNT", 0 );
		bindingExprRow[3] = new ScriptExpression( "dataSetRow.SALE_DATE", 6 );

		String[] columnStr = new String[]{"GP1", "GP2", "Amount", "Date",};

		QueryDefinition qd = this.createQuery( bindingNameGroup,
				bindingExprGroup, groupDefn, null, null, sortDefn, null, null,
				null, bindingNameRow, bindingExprRow );
		String outputStr = getOutputStrForGroupTest( 37, qd, groupDefn.length,
				bindingNameRow, columnStr );
		testPrint( outputStr );
		checkOutputFile( );
	}

	/**
	 * Test feature of Two level sort first level, sort on date time second
	 * level, sort on integer
	 */
	public void test_SortOnDate( ) throws Exception
	{
		SortDefinition[] sortDefn = new SortDefinition[]{new SortDefinition( ),
				new SortDefinition( )};
		sortDefn[0].setExpression( "dataSetRow.SALE_DATE" );
		sortDefn[0].setSortDirection( ISortDefinition.SORT_DESC );
		sortDefn[1].setExpression( "dataSetRow.AMOUNT" );
		sortDefn[1].setSortDirection( ISortDefinition.SORT_ASC );

		String[] bindingNameRow = new String[4];
		bindingNameRow[0] = "ROW_1";
		bindingNameRow[1] = "ROW_2";
		bindingNameRow[2] = "ROW_3";
		bindingNameRow[3] = "ROW_4";
		IBaseExpression[] bindingExprRow = new IBaseExpression[4];
		bindingExprRow[0] = new ScriptExpression( "dataSetRow.COUNTRY", 0 );
		bindingExprRow[1] = new ScriptExpression( "dataSetRow.CITY", 0 );
		bindingExprRow[2] = new ScriptExpression( "dataSetRow.AMOUNT", 0 );
		bindingExprRow[3] = new ScriptExpression( "dataSetRow.SALE_DATE", 6 );

		String[] columnStr = new String[]{"Country", "City", "Amount", "Date",};

		QueryDefinition qd = this.createQuery( null, null, null, null, null,
				sortDefn, null, null, null, bindingNameRow, bindingExprRow );
		String outputStr = getOutputStrForGroupTest( 20, qd, 0, bindingNameRow,
				columnStr );
		testPrint( outputStr );
		checkOutputFile( );
	}

	/**
	 * Test feature of incorrect sorting expression, throw exception
	 */
	public void test_incorrectExpression( ) throws Exception
	{
		String[] bindingNameGroup = new String[2];
		bindingNameGroup[0] = "GROUP_GROUP1";
		bindingNameGroup[1] = "GROUP_GROUP2";
		IBaseExpression[] bindingExprGroup = new IBaseExpression[2];
		bindingExprGroup[0] = new ScriptExpression( "dataSetRow.COUNTRY" );
		bindingExprGroup[1] = new ScriptExpression( "dataSetRow.CITY" );

		GroupDefinition[] groupDefn = new GroupDefinition[]{
				new GroupDefinition( "group1" ), new GroupDefinition( "group2" )};
		groupDefn[0].setKeyExpression( "row.GROUP_GROUP1" );
		groupDefn[1].setKeyExpression( "row.GROUP_GROUP2" );

		SortDefinition[] sortDefn = new SortDefinition[]{new SortDefinition( )};

		// wrong sort column
		sortDefn[0].setExpression( "dataSetRow.Error" );
		sortDefn[0].setSortDirection( ISortDefinition.SORT_DESC );

		String[] bindingNameRow = new String[4];
		bindingNameRow[0] = "ROW_1";
		bindingNameRow[1] = "ROW_2";
		bindingNameRow[2] = "ROW_3";
		bindingNameRow[3] = "ROW_4";
		IBaseExpression[] bindingExprRow = new IBaseExpression[4];
		bindingExprRow[0] = new ScriptExpression( "dataSetRow.COUNTRY", 0 );
		bindingExprRow[1] = new ScriptExpression( "dataSetRow.CITY", 0 );
		bindingExprRow[2] = new ScriptExpression( "dataSetRow.AMOUNT", 0 );
		bindingExprRow[3] = new ScriptExpression( "dataSetRow.SALE_DATE", 6 );
		QueryDefinition qd = this.createQuery( null, null, null, null, null,
				sortDefn, null, null, null, bindingNameRow, bindingExprRow );

		try
		{
			executeQuery( qd );
			fail( "Should throw DteException!" );
		}
		catch ( DataException e )
		{
			String errorCode = e.getErrorCode( );
			assertEquals( ResourceConstants.WRONG_DATA_TYPE_SCRIPT_RESULT,
					errorCode );
		}
	}
}