
package org.eclipse.birt.report.tests.viewer.regression;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.InputStream;

import org.apache.cactus.HttpSessionCookie;
import org.apache.cactus.WebRequest;
import org.eclipse.birt.report.servlet.ViewerServlet;
import org.eclipse.birt.report.tests.viewer.BaseViewerTestCase;

import com.meterware.httpunit.WebResponse;

/**
 * <b>Bug description</b>
 * <p>
 * BIRT viewer should support both relative/absolute path if
 * WORkING_FOLDER_ACCESS_ONLY is false
 * </p>
 * <b>Steps to reproduce:</b>
 * <p>
 * If the WORKING_FOLDER_ACCESS_ONLY is false, BIRT viewer should allow the
 * __report and __document to be in either relative or absolute path.
 * 
 * So the following URL should work:
 * http://127.0.0.1:54385/viewer/frameset?__report=report_1.rptdesign&__document=test.rptdocument&__format=html&__svg=false&__designer=true&__masterpage=true
 * 
 * Assume report_1.rptdesign exists in BIRT working folder. We should see
 * test.rptdocument to be generated under BIRT working folder.
 * </p>
 * <b>Test description:</b>
 * <p>
 * This test maily ensure that in default status, the
 * DOCUMENT_FOLDER_ACCESS_ONLY is false, so report and document can be accessed
 * through absolute path and relative path.
 * </p>
 */
public class Regression_137643 extends BaseViewerTestCase
{

	private String REPORT = "report/regression_137643.rptdesign"; //$NON-NLS-1$
	private String document = CATALINAHOME + "/webapps/" + RUNTIME
			+ "/report/regression_137643.rptdocument";
	private static HttpSessionCookie sessionCookie;
	private static boolean flag=false;
	
	/**
	 * @param webRequest
	 * @throws Exception
	 */
	public void begin_Regression_137643( WebRequest webRequest )
			throws Exception
	{
		webRequest.setURL( null, null, SERVLET_PATH_FRAMESET, null, null );
		webRequest.addParameter( PARAM_REPORT, REPORT );
		webRequest.addParameter( PARAM_REPORT_DOCUMENT, document );
		webRequest.addHeader( "request-type", "SOAP" );//$NON-NLS-1$
		webRequest.setContentType( "text/xml; charset=utf-8" ); //$NON-NLS-1$
		webRequest.addParameter( PARAM_LOCALE, "en_US" );
		webRequest.addHeader( "SOAPAction", "''" ); //$NON-NLS-1$//$NON-NLS-2$
		webRequest.setUserData( getSoapStream( ) );
		sessionCookie=webRequest.getSessionCookie( );
		webRequest.addCookie( sessionCookie );
	}

	/**
	 * Test accessing report design through relative path. Test create report
	 * document to predefined absolute path.
	 */
	public void test_Regression_137643( )
	{
		session.setAttribute( "test", "ok" );
		ViewerServlet servlet = new ViewerServlet( );
		try
		{
			servlet.init( config );
			servlet.doPost( request, response );
		}
		catch ( Exception e )
		{
			e.printStackTrace( );
			fail( e.toString( ) );
		}
	}

	/**
	 * @param webResponse
	 */
	public void end_Regression_137643( WebResponse webResponse )
			throws Exception
	{
		this.flag=true;
		assertEquals( 200, webResponse.getResponseCode( ) );
		File doc=new File(CATALINAHOME + "/webapps/" + RUNTIME
			+ "/report/regression_137643.rptdocument");
		assertNotNull(doc);
		assertTrue(doc.exists( ));
		assertTrue(doc.length( )>0);
	}

	/**
	 * @param webRequest
	 * @throws Exception
	 */
	public void begin_Regression_137643_1( WebRequest webRequest )
			throws Exception
	{
		webRequest.setURL( null, null, SERVLET_PATH_FRAMESET, null, null );
		REPORT = CATALINAHOME + "/webapps/" + RUNTIME
				+ "/report/regression_137643.rptdesign";
		webRequest.addParameter( PARAM_REPORT, REPORT );
		webRequest.addHeader( "request-type", "SOAP" );//$NON-NLS-1$
		webRequest.setContentType( "text/xml; charset=utf-8" ); //$NON-NLS-1$
		webRequest.addParameter( PARAM_LOCALE, "en_US" );
		webRequest.addHeader( "SOAPAction", "''" ); //$NON-NLS-1$//$NON-NLS-2$
		webRequest.setUserData( getSoapStream( ) );
	}

	/**
	 * Test accessing report design through absolute path.
	 */
	public void test_Regression_137643_1( )
	{
		ViewerServlet servlet = new ViewerServlet( );
		try
		{
			servlet.init( config );
			servlet.doPost( request, response );
		}
		catch ( Exception e )
		{
			e.printStackTrace( );
			fail( e.toString( ) );
		}
	}

	/**
	 * @param webResponse
	 */
	public void end_Regression_137643_1( WebResponse webResponse )
			throws Exception
	{
		assertEquals( 200, webResponse.getResponseCode( ) );
	}

	/**
	 * @param webRequest
	 * @throws Exception
	 */
	public void begin_Regression_137643_2( WebRequest webRequest )
			throws Exception
	{
		assertTrue(flag);
		assertNotNull(sessionCookie);
		webRequest.addCookie( sessionCookie );
		webRequest.setURL( null, null, SERVLET_PATH_FRAMESET, null, null );
		document = "report/regression_137643.rptdocument";
		webRequest.addParameter( PARAM_REPORT_DOCUMENT, document );
		webRequest.addHeader( "request-type", "SOAP" );//$NON-NLS-1$
		webRequest.setContentType( "text/xml; charset=utf-8" ); //$NON-NLS-1$
		webRequest.addParameter( PARAM_LOCALE, "en_US" );
		webRequest.addHeader( "SOAPAction", "''" ); //$NON-NLS-1$//$NON-NLS-2$
		webRequest.setUserData( getSoapStream( ) );
	}

	/**
	 * Test accessing report document through relative path.
	 */
	public void test_Regression_137643_2( )
	{
		assertEquals("ok",session.getAttribute( "test" ));
		ViewerServlet servlet = new ViewerServlet( );
		try
		{
			servlet.init( config );
			servlet.doPost( request, response );
		}
		catch ( Exception e )
		{
			e.printStackTrace( );
			fail( e.toString( ) );
		}
	}

	/**
	 * @param webResponse
	 */
	public void end_Regression_137643_2( WebResponse webResponse )
			throws Exception
	{
		assertEquals( 200, webResponse.getResponseCode( ) );
	}

	/**
	 * @param webRequest
	 * @throws Exception
	 */
	public void begin_Regression_137643_3( WebRequest webRequest )
			throws Exception
	{
		assertTrue(flag);
		assertNotNull(sessionCookie);
		webRequest.addCookie( sessionCookie );
		webRequest.setURL( null, null, SERVLET_PATH_FRAMESET, null, null );
		document = CATALINAHOME + "/webapps/" + RUNTIME
				+ "/report/regression_137643.rptdocument";
		webRequest.addParameter( PARAM_REPORT_DOCUMENT, document );
		webRequest.addHeader( "request-type", "SOAP" );//$NON-NLS-1$
		webRequest.setContentType( "text/xml; charset=utf-8" ); //$NON-NLS-1$
		webRequest.addParameter( PARAM_LOCALE, "en_US" );
		webRequest.addHeader( "SOAPAction", "''" ); //$NON-NLS-1$//$NON-NLS-2$
		webRequest.setUserData( getSoapStream( ) );
	}

	/**
	 * Test accessing report document through absolute path.
	 */
	public void test_Regression_137643_3( )
	{
		assertEquals("ok",session.getAttribute( "test" ));
		ViewerServlet servlet = new ViewerServlet( );
		try
		{
			servlet.init( config );
			servlet.doPost( request, response );
		}
		catch ( Exception e )
		{
			e.printStackTrace( );
			fail( e.toString( ) );
		}
	}

	/**
	 * @param webResponse
	 */
	public void end_Regression_137643_3( WebResponse webResponse )
			throws Exception
	{
		assertTrue(flag);
		assertEquals( 200, webResponse.getResponseCode( ) );
	}

	
	/* utility methods */

	private InputStream getSoapStream( )
	{
		String soapMessage = "<Envelope xmlns=\"http://schemas.xmlsoap.org/soap/envelope/\"><Body><GetUpdatedObjects xmlns=\"http://schemas.eclipse.org/birt\"><Operation><Target><Id>Document</Id><Type>Document</Type></Target><Operator>GetPage</Operator><Oprand><Name>page</Name><Value>1</Value></Oprand><Oprand><Name>svg</Name><Value>false</Value></Oprand></Operation></GetUpdatedObjects></Body></Envelope>"; //$NON-NLS-1$
		return new ByteArrayInputStream( soapMessage.getBytes( ) );
	}

}