package com.fujitsu.iflowqa.testframe;

import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.HttpURLConnection;
import java.net.URL;

public class ReportTest extends BPMTestBase
{
    private String LoginFailed = "Login to i-Flow server failed";
    private String LoginSucessfull = "Welcome to i-Flow Reports";
    private String REPORTMSG = "Report Test";

    public ReportTest()
    {
    }

    public
    void
    runTests(TestRecorder tr)
        throws Exception
    {
        try
        {
            init(tr);
            String reportURL = tr.getRequiredProperty("reporturl");
            String username = tr.getRequiredProperty("username");
            String password = tr.getRequiredProperty("password");
            String reportLoginUrl = reportURL + "/LoginAction.do?userName=" + username + "&password=" + password;
            reportLogin(reportLoginUrl);
        }
        catch(Exception e)
        {
            logAndRethrow("Exception caught in main test level", e);
        }
    }

    public
    void
    reportLogin(String reportUrl) throws Exception
    {
        try
        {
            URL logURL = new URL(reportUrl);
            HttpURLConnection httpURL = (HttpURLConnection)logURL.openConnection();
            if (httpURL == null) {
                throw new Exception("Strange, got a null back from openConnection.  Something is wrong.");
            }
            InputStream bis = httpURL.getInputStream();
            if (httpURL == null) {
                throw new Exception("Strange, got a null back from getInputStream.  Something is wrong.");
            }

            StringBuffer thePage = new StringBuffer();
            Reader r = new InputStreamReader(bis);
            char[] ba = new char[1000];  //this is the read buffer

            int lenRead = r.read(ba);
            while (lenRead>0) {
                thePage.append(ba,0,lenRead);
                lenRead = r.read(ba);
            }
            httpURL.disconnect();
            String output = thePage.toString();

            if(output == null || output.length() == 0)
            {
                tr.markFailed(REPORTMSG,"Was not able to retrieve any page at all.  Is ("+reportUrl+") really the web address of the reports?");
                return;
            }
            if(output.indexOf(LoginSucessfull) != -1) {
                tr.markPassed(REPORTMSG+" - Found '"+LoginSucessfull+"' in the page");
            }
            else if(output.indexOf(LoginFailed) != -1) {
                tr.markFailed(REPORTMSG," - Found '"+LoginFailed+"' in the page");
            }
            else {
                tr.markFailed(REPORTMSG," - Did not find '"+LoginSucessfull+"' in the page");
            }
        }
        catch(Exception e)
        {
            logAndRethrow(REPORTMSG, e);
        }
    }

    public
    void
    logAndRethrow(
        String message,
        Exception e)
            throws Exception
    {
        throw new ExceptionWrapper(message, e);
    }

    /**
    * TestDriver is the class that knows how to run these tests.
    * So just call it with the name of this class in there.
    */
    public
    static
    void
    main(String[] args)
    {
        String[] newArgs = new String[1];
        newArgs[0] = "com.fujitsu.iflowqa.testframe.ReportTest";
        TestDriver.main(newArgs);
    }


}
