package com.fujitsu.iflowqa.testframe;

import java.awt.Point;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

import sun.misc.BASE64Encoder;

import com.fujitsu.iflow.common.AsapUtils;
import com.fujitsu.iflow.model.workflow.DataItemRef;
import com.fujitsu.iflow.model.workflow.Filter;
import com.fujitsu.iflow.model.workflow.JavaAction;
import com.fujitsu.iflow.model.workflow.JavaActionSet;
import com.fujitsu.iflow.model.workflow.Node;
import com.fujitsu.iflow.model.workflow.Plan;
import com.fujitsu.iflow.model.workflow.TimerAction;
import com.fujitsu.iflow.model.workflow.TimerDef;
import com.fujitsu.iflow.model.workflow.WFObjectFactory;
import com.fujitsu.iflow.model.workflow.WFObjectList;
import com.fujitsu.iflow.model.workflow.WFSession;

/**
* This class implements TestSet and the purpose of this class to test
* Wf-XML 2.0. To run this class iFlowClients.properties file should be in classpath.
* The results of the test are recorded using
* TestRecorder class.
*
* NOTE: This class should be REWRITTEN to extend BPMTestBase class, because this
* duplicated a lot of the methods in that class!!!!!
*/
public class Wfxml2Test implements TestSet    //extend BPMTestBase here!
{

    private String testName = "Wf-XML 2.0 Test";
    private WFSession wfSession = null;
    private TestRecorder tr;
    private String serverName = null;
    private String userName = null;
    private String passWord = null;
    private Plan testPlan = null;
    private String planName = "Wfxml2Test";
    private String newDefnKey = null;

    String soapUri = AsapUtils.soapUri;
    String soapPrefix = AsapUtils.soapPrefix;
    String soapNSattr = AsapUtils.soapNSattr;
    String asapUri = AsapUtils.asapUri;
    String asapPrefix = AsapUtils.asapPrefix;
    String asapNSattr = AsapUtils.asapNSattr;
    String schemaDefUri = AsapUtils.schemaDefUri;
    String schemaDefPrefix = AsapUtils.schemaDefPrefix;
    String schemaDefNSattr = AsapUtils.schemaDefNSattr;
    String iflowNSPrefix = AsapUtils.iflowNSPrefix;
    String iflowNSattr = AsapUtils.iflowNSattr;
    String wfxmlUri = AsapUtils.wfxmlUri;
    String wfxmlPrefix = AsapUtils.wfxmlPrefix;
    String wfxmlNSattr = AsapUtils.wfxmlNSattr;

    public
    void
    runTests(TestRecorder trec)
        throws Exception
    {
        try {
            tr = trec;
            logIn();

            cleanup();

            testPlan = createTemplate();

            Document templateDoc = testPlan.convertToXPDLDom();
            String serviceRegistryUri = tr.getProperty("ServRegUri", "http://localhost:9004/ibpm/jsp/ServReg.jsp");
            tr.log("Using ServRegUri = "+serviceRegistryUri);

            sendWfxmlNewDefinition(templateDoc, serviceRegistryUri);

            sendWfxmlListDefinitions(serviceRegistryUri);

            Document doc = sendWfxmlGetDefinition(newDefnKey);

            sendWfxmlSetDefinition(doc, newDefnKey);

            logOut();

        } catch (Exception e) {
            logException(testName + " --  Exception caught in main test level", e);
        }
   }

    public
    Wfxml2Test()
    {}

    public
    void
    cleanup()
        throws Exception
    {
        try {
            WFObjectList wfObjectList = WFObjectFactory.getWFObjectList(wfSession);
            wfObjectList.addFilter(WFObjectList.LISTFIELD_PLAN_NAME,"=","'" + planName + "'");
            wfObjectList.openBatchedList(Filter.AllPlans);
            Object[] wiList = wfObjectList.getNextBatch(100);
            if (wiList != null) {
                if (wiList.length > 0) {
                    for(int i = 0; i < wiList.length; i++) {
                        Plan pl = (Plan) wiList[i];
                        wfSession.deletePlan(pl.getId());
                    }
                }
            }
            tr.markPassed(testName + " --  Clean up passed... templates from previous test run(s) deleted.");
        } catch (Exception e) {
            logException(testName + " --  Clean up failed... could not delete templates from previous test run(s).", e);
        }
    }

    public
    void
    sendWfxmlNewDefinition(
        Document templateDoc,
        String remoteRegistryUri)
        throws Exception
    {
        try {
            URL url = new URL(remoteRegistryUri);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();

            conn.setRequestProperty("Accept", "text/xml");
            // Authorization header of the form:
            // Authorization: Basic <base64 encoding of username:password>
            BASE64Encoder encoder = new BASE64Encoder();
            String userPass = wfSession.getServerOption("SwapLinkageUserName") + ":" +
                              wfSession.getServerOption("SwapLinkagePassword");
            String base64UserPass = encoder.encode(userPass.getBytes("UTF-8"));
            conn.setRequestProperty("Authorization", "Basic " + base64UserPass);
            conn.setRequestProperty("SOAPAction", "");
            conn.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
            conn.setRequestMethod("POST");
            conn.setDoOutput(true);

            // Create & write request xml
            DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
            docFactory.setNamespaceAware(true);
            DocumentBuilder builder = docFactory.newDocumentBuilder();
            Document doc = builder.newDocument();

            Element envelopeTag = doc.createElementNS(soapUri, soapPrefix + "Envelope");
            envelopeTag.setAttribute(asapNSattr, asapUri);
            envelopeTag.setAttribute(soapNSattr, soapUri);
            envelopeTag.setAttribute(wfxmlNSattr, wfxmlUri);
            doc.appendChild(envelopeTag);

            Element headerTag = doc.createElement(soapPrefix + "Header");
            envelopeTag.appendChild(headerTag);

            Element requestTag = doc.createElementNS(asapUri, asapPrefix + "Request");
            headerTag.appendChild(requestTag);

            Element senderKeyTag = doc.createElement(asapPrefix + "SenderKey");
            requestTag.appendChild(senderKeyTag);

            Element receiverKeyTag = doc.createElement(asapPrefix + "ReceiverKey");
            receiverKeyTag.appendChild(doc.createTextNode(remoteRegistryUri));
            requestTag.appendChild(receiverKeyTag);

            Element responseRequiredTag = doc.createElement(asapPrefix + "ResponseRequired");
            responseRequiredTag.appendChild(doc.createTextNode("Yes"));
            requestTag.appendChild(responseRequiredTag);

            Element bodyTag = doc.createElement(soapPrefix + "Body");
            envelopeTag.appendChild(bodyTag);

            Element newDefinitionRqTag = doc.createElement(wfxmlPrefix + "NewDefinitionRq");
            bodyTag.appendChild(newDefinitionRqTag);

            Element processLanguageTag = doc.createElement(wfxmlPrefix + "ProcessLanguage");
            processLanguageTag.appendChild(doc.createTextNode("XPDL"));
            newDefinitionRqTag.appendChild(processLanguageTag);

            Element definitionTag = doc.createElement(wfxmlPrefix + "Definition");
            newDefinitionRqTag.appendChild(definitionTag);

            // Attach the template xpdl under the definition node.
            Element templateElement = templateDoc.getDocumentElement();
            org.w3c.dom.Node importedNode = doc.importNode(templateElement, true);
            definitionTag.appendChild(importedNode);

            DOMSource docSource = new DOMSource(doc);

            OutputStream out = conn.getOutputStream();
            StreamResult outResult = new StreamResult(out);
            TransformerFactory tf = TransformerFactory.newInstance();
            Transformer transformer = tf.newTransformer();
            transformer.transform(docSource, outResult);

            // Read the response
            DocumentBuilderFactory docFactory2 = DocumentBuilderFactory.newInstance();
            docFactory2.setNamespaceAware(true);
            DocumentBuilder builder2 = docFactory2.newDocumentBuilder();
            InputStream is = conn.getInputStream();
            Document response = builder2.parse(is);

            Element responseRoot = response.getDocumentElement();
            Element bodyElement = AsapUtils.getNamedChildElement(responseRoot, "Body");
            if (bodyElement == null) {
                String exception = AsapUtils.getExceptionFromResponse(responseRoot);
                throw new Exception(exception);
            }
            Element methodElement = AsapUtils.getNamedChildElement(bodyElement, "NewDefinitionRs");
            // The response looks good. We are done. Test passed.

            tr.markPassed(testName + " --  NewDefinition test passed... a new definition has been created in the remote service registry.");
        } catch (Exception e) {
            logException(testName + " --  NewDefinition test failed for Service Registry URI : " + remoteRegistryUri, e);
        }
    }

    public
    void
    sendWfxmlListDefinitions(
        String servRegUri)
        throws Exception
    {
        try {
            URL url = new URL(servRegUri);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();

            conn.setRequestProperty("Accept", "text/xml");
            // Authorization header of the form:
            // Authorization: Basic <base64 encoding of username:password>
            BASE64Encoder encoder = new BASE64Encoder();
            String userPass = wfSession.getServerOption("SwapLinkageUserName") + ":" +
                              wfSession.getServerOption("SwapLinkagePassword");
            String base64UserPass = encoder.encode(userPass.getBytes("UTF-8"));
            conn.setRequestProperty("Authorization", "Basic " + base64UserPass);
            conn.setRequestProperty("SOAPAction", "");
            conn.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
            conn.setRequestMethod("POST");
            conn.setDoOutput(true);

            // Create & write request xml
            DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
            docFactory.setNamespaceAware(true);
            DocumentBuilder builder = docFactory.newDocumentBuilder();
            Document doc = builder.newDocument();

            Element envelopeTag = doc.createElementNS(soapUri, soapPrefix + "Envelope");
            envelopeTag.setAttribute(asapNSattr, asapUri);
            envelopeTag.setAttribute(soapNSattr, soapUri);
            envelopeTag.setAttribute(wfxmlNSattr, wfxmlUri);
            doc.appendChild(envelopeTag);

            Element headerTag = doc.createElement(soapPrefix + "Header");
            envelopeTag.appendChild(headerTag);

            Element requestTag = doc.createElementNS(asapUri, asapPrefix + "Request");
            headerTag.appendChild(requestTag);

            Element senderKeyTag = doc.createElement(asapPrefix + "SenderKey");
            requestTag.appendChild(senderKeyTag);

            Element receiverKeyTag = doc.createElement(asapPrefix + "ReceiverKey");
            receiverKeyTag.appendChild(doc.createTextNode(servRegUri));
            requestTag.appendChild(receiverKeyTag);

            Element responseRequiredTag = doc.createElement(asapPrefix + "ResponseRequired");
            responseRequiredTag.appendChild(doc.createTextNode("Yes"));
            requestTag.appendChild(responseRequiredTag);

            Element bodyTag = doc.createElement(soapPrefix + "Body");
            envelopeTag.appendChild(bodyTag);

            Element newDefinitionRqTag = doc.createElement(wfxmlPrefix + "ListDefinitionsRq");
            bodyTag.appendChild(newDefinitionRqTag);

            DOMSource docSource = new DOMSource(doc);

            OutputStream outputStrm = conn.getOutputStream();
            StreamResult outResult = new StreamResult(outputStrm);
            TransformerFactory tf = TransformerFactory.newInstance();
            Transformer transformer = tf.newTransformer();
            transformer.transform(docSource, outResult);

            // Read the response
            DocumentBuilderFactory docFactory2 = DocumentBuilderFactory.newInstance();
            docFactory2.setNamespaceAware(true);
            DocumentBuilder builder2 = docFactory2.newDocumentBuilder();
            InputStream is = conn.getInputStream();
            Document responseDoc = builder2.parse(is);

            Element responseRoot = responseDoc.getDocumentElement();
            Element bodyElement = AsapUtils.getNamedChildElement(responseRoot, "Body");
            Element methodElement = AsapUtils.getNamedChildElement(bodyElement, "ListDefinitionsRs");

            boolean newPlanFound=false;

            Element[] definitionInfos = AsapUtils.getAllChildElements(methodElement);
            for (int j = 0; j < definitionInfos.length; j++) {
                Element definitionInfoElement = definitionInfos[j];
                String s = definitionInfoElement.getLocalName();

                if (definitionInfoElement.getLocalName().equals("DefinitionInfo")) {
                    String nameTag    = AsapUtils.getTagValue(AsapUtils.getNamedChildElement(definitionInfoElement, "Name"));

                    if (nameTag.equals(planName)) {
                        newDefnKey = AsapUtils.getTagValue(AsapUtils.getNamedChildElement(definitionInfoElement, "DefinitionKey"));
                        newPlanFound = true;
                        break;
                    }
                }
            }
            if (newPlanFound) {
                tr.markPassed(testName + " --  ListDefinitions test passed... newly created definition found in retrieved list");
            }
            else {
                tr.markFailed(testName + " --  ListDefinitions", "Newly created definition not found in the list of definitions");
            }
        } catch (Exception e) {
            logException(testName + " --  ListDefinitions test failed for Service Registry URI : " + servRegUri, e);
        }
    }

    public
    Document
    sendWfxmlGetDefinition(
        String remoteTemplateUri)
        throws Exception
    {
        Document templateDoc = null;
        try {
            URL url = new URL(remoteTemplateUri);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();

            conn.setRequestProperty("Accept", "text/xml");
            // Authorization header of the form:
            // Authorization: Basic <base64 encoding of username:password>
            BASE64Encoder encoder = new BASE64Encoder();
            String userPass = wfSession.getServerOption("SwapLinkageUserName") + ":" +
                              wfSession.getServerOption("SwapLinkagePassword");
            String base64UserPass = encoder.encode(userPass.getBytes("UTF-8"));
            conn.setRequestProperty("Authorization", "Basic " + base64UserPass);
            conn.setRequestProperty("SOAPAction", "");
            conn.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
            conn.setRequestMethod("POST");
            conn.setDoOutput(true);

            // Create & write request xml
            DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
            docFactory.setNamespaceAware(true);
            DocumentBuilder builder = docFactory.newDocumentBuilder();
            Document doc = builder.newDocument();

            Element envelopeTag = doc.createElementNS(soapUri, soapPrefix + "Envelope");
            envelopeTag.setAttribute(asapNSattr, asapUri);
            envelopeTag.setAttribute(soapNSattr, soapUri);
            envelopeTag.setAttribute(wfxmlNSattr, wfxmlUri);
            doc.appendChild(envelopeTag);

            Element headerTag = doc.createElement(soapPrefix + "Header");
            envelopeTag.appendChild(headerTag);

            Element requestTag = doc.createElementNS(asapUri, asapPrefix + "Request");
            headerTag.appendChild(requestTag);

            Element senderKeyTag = doc.createElement(asapPrefix + "SenderKey");
            requestTag.appendChild(senderKeyTag);

            Element receiverKeyTag = doc.createElement(asapPrefix + "ReceiverKey");
            receiverKeyTag.appendChild(doc.createTextNode(remoteTemplateUri));
            requestTag.appendChild(receiverKeyTag);

            Element responseRequiredTag = doc.createElement(asapPrefix + "ResponseRequired");
            responseRequiredTag.appendChild(doc.createTextNode("Yes"));
            requestTag.appendChild(responseRequiredTag);

            Element bodyTag = doc.createElement(soapPrefix + "Body");
            envelopeTag.appendChild(bodyTag);

            Element getDefinitionRqTag = doc.createElement(wfxmlPrefix + "GetDefinitionRq");
            bodyTag.appendChild(getDefinitionRqTag);

            Element processLanguageTag = doc.createElement(wfxmlPrefix + "ProcessLanguage");
            processLanguageTag.appendChild(doc.createTextNode("XPDL"));
            getDefinitionRqTag.appendChild(processLanguageTag);

            DOMSource docSource = new DOMSource(doc);

            OutputStream out = conn.getOutputStream();
            StreamResult outResult = new StreamResult(out);
            TransformerFactory tf = TransformerFactory.newInstance();
            Transformer transformer = tf.newTransformer();
            transformer.transform(docSource, outResult);

            // Read the response
            DocumentBuilderFactory docFactory2 = DocumentBuilderFactory.newInstance();
            docFactory2.setNamespaceAware(true);
            DocumentBuilder builder2 = docFactory2.newDocumentBuilder();
            InputStream is = conn.getInputStream();
            Document response = builder2.parse(is);

            Element responseRoot = response.getDocumentElement();
            Element bodyElement = AsapUtils.getNamedChildElement(responseRoot, "Body");
            Element methodElement = AsapUtils.getNamedChildElement(bodyElement, "GetDefinitionRs");
            Element templateElement = AsapUtils.getFirstChildElement(methodElement);

            templateDoc = builder.newDocument();
            org.w3c.dom.Node importedNode = templateDoc.importNode(templateElement, true);
            templateDoc.appendChild(importedNode);

            // The XPDL has been retrieved. Test passed.
            tr.markPassed(testName + " --  GetDefinition test passed... XPDL representation of the newly created definition retrieved");
        } catch (Exception e) {
            logException(testName + " --  GetDefinition test failed for Remote definition URI : " + remoteTemplateUri, e);
        }
        return templateDoc;
    }

    public
    void
    sendWfxmlSetDefinition(
        Document templateDoc,
        String remoteFactoryUri)
        throws Exception
    {
        try {
            URL url = new URL(remoteFactoryUri);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();

            conn.setRequestProperty("Accept", "text/xml");
            // Authorization header of the form:
            // Authorization: Basic <base64 encoding of username:password>
            BASE64Encoder encoder = new BASE64Encoder();
            String userPass = wfSession.getServerOption("SwapLinkageUserName") + ":" +
                              wfSession.getServerOption("SwapLinkagePassword");
            String base64UserPass = encoder.encode(userPass.getBytes("UTF-8"));
            conn.setRequestProperty("Authorization", "Basic " + base64UserPass);
            conn.setRequestProperty("SOAPAction", "");
            conn.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
            conn.setRequestMethod("POST");
            conn.setDoOutput(true);

            // Create & write request xml
            DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
            docFactory.setNamespaceAware(true);
            DocumentBuilder builder = docFactory.newDocumentBuilder();
            Document doc = builder.newDocument();

            Element envelopeTag = doc.createElementNS(soapUri, soapPrefix + "Envelope");
            envelopeTag.setAttribute(asapNSattr, asapUri);
            envelopeTag.setAttribute(soapNSattr, soapUri);
            envelopeTag.setAttribute(wfxmlNSattr, wfxmlUri);
            doc.appendChild(envelopeTag);

            Element headerTag = doc.createElement(soapPrefix + "Header");
            envelopeTag.appendChild(headerTag);

            Element requestTag = doc.createElementNS(asapUri, asapPrefix + "Request");
            headerTag.appendChild(requestTag);

            Element senderKeyTag = doc.createElement(asapPrefix + "SenderKey");
            requestTag.appendChild(senderKeyTag);

            Element receiverKeyTag = doc.createElement(asapPrefix + "ReceiverKey");
            receiverKeyTag.appendChild(doc.createTextNode(remoteFactoryUri));
            requestTag.appendChild(receiverKeyTag);

            Element responseRequiredTag = doc.createElement(asapPrefix + "ResponseRequired");
            responseRequiredTag.appendChild(doc.createTextNode("Yes"));
            requestTag.appendChild(responseRequiredTag);

            Element bodyTag = doc.createElement(soapPrefix + "Body");
            envelopeTag.appendChild(bodyTag);

            Element newDefinitionRqTag = doc.createElement(wfxmlPrefix + "SetDefinitionRq");
            bodyTag.appendChild(newDefinitionRqTag);

            Element processLanguageTag = doc.createElement(wfxmlPrefix + "ProcessLanguage");
            processLanguageTag.appendChild(doc.createTextNode("XPDL"));
            newDefinitionRqTag.appendChild(processLanguageTag);

            Element definitionTag = doc.createElement(wfxmlPrefix + "Definition");
            newDefinitionRqTag.appendChild(definitionTag);

            // Attach the template xpdl under the definition node.
            Element templateElement = templateDoc.getDocumentElement();
            org.w3c.dom.Node importedNode = doc.importNode(templateElement, true);
            definitionTag.appendChild(importedNode);

            DOMSource docSource = new DOMSource(doc);

            OutputStream out = conn.getOutputStream();
            StreamResult outResult = new StreamResult(out);
            TransformerFactory tf = TransformerFactory.newInstance();
            Transformer transformer = tf.newTransformer();
            transformer.transform(docSource, outResult);

            // Read the response
            DocumentBuilderFactory docFactory2 = DocumentBuilderFactory.newInstance();
            docFactory2.setNamespaceAware(true);
            DocumentBuilder builder2 = docFactory2.newDocumentBuilder();
            InputStream is = conn.getInputStream();
            Document response = builder2.parse(is);

            Element responseRoot = response.getDocumentElement();
            Element bodyElement = AsapUtils.getNamedChildElement(responseRoot, "Body");
            Element methodElement = AsapUtils.getNamedChildElement(bodyElement, "SetDefinitionRs");
            // The response looks good. We are done. Test passed.

            tr.markPassed(testName + " --  SetDefinition test passed... the definition has been updated");
        } catch (Exception e) {
            logException(testName + " --  SetDefinition test failed for Remote definition URI : " + remoteFactoryUri, e);
        }
    }

    public
    Plan
    createTemplate()
        throws Exception
    {
        Plan plan = null;

        try {
            JavaActionSet actionSet =null;
            JavaAction[] actions = null;
            JavaActionSet actionSet11 =null;
            JavaAction[] actions11 = null;
            String udaInitValue = "TimerStarted";

            plan = WFObjectFactory.getPlan();
            plan.setWFSession(wfSession);
            plan.startEdit();

            plan.addDataItemRef("TimerUDA", "STRING", udaInitValue);

            plan.setName( planName );
            plan.setTitle( planName + " title" );
            plan.setDesc( planName + " description" );
            plan.setPlanIdentifier("Global");
            String script = "setProcessAttribute(\"TimerUDA\" ,\"STRING\",\"TimerExpired\" )";

            TimerAction tAction = WFObjectFactory.getTimerAction(TimerAction.SCRIPT);
            DataItemRef data = plan.addDataItemRef("timeScript","STRING", script);

            tAction.setScriptAction(data.getName());
            TimerDef td = WFObjectFactory.getTimerDef(TimerDef.ABSOLUTE);
            String currentTime = Long.toString(System.currentTimeMillis()+3000);
            DataItemRef data1 = plan.addDataItemRef("time","LONG", currentTime);

            td.setTime(data1.getName());
            td.setName("Test");
            td.addTimerAction(tAction);
            Node startNode = plan.addNode( "Start", Node.TYPE_START );
            int startX = 50;
            int startY = 100;
            startNode.setPosition( new Point( startX, startY ) );

            Node firstActNode = plan.addNode( "ACTIVITY-TIMER", Node.TYPE_ACTIVITY );
            firstActNode.setPosition( new Point( startX + 200, startY ) );
            firstActNode.setRole( "Role" );
            firstActNode.setDesc("ACTIVITY-TIMER");
            firstActNode.addTimer(td);
            plan.addArrow( "START", startNode, firstActNode );

            Node exitNode = plan.addNode( "EXIT", Node.TYPE_EXIT );
            exitNode.setPosition( new Point( startX + 200, startY + 200) );
            plan.addArrow( "DONE", firstActNode, exitNode );
            plan = plan.createProcessDef();
        }
        catch (Exception e) {
            logException(testName + " --  Plan creation failed", e);
        }
        return plan;

    }

    public
    void
    logIn()
    throws Exception
    {
        try {
            String transPortType = tr.getRequiredProperty("TWFTransportType");
            wfSession = WFObjectFactory.getWFSession();
            wfSession.initForApplication(null, tr.getProps());
            serverName = tr.getRequiredProperty("servername");
            userName = tr.getRequiredProperty("username");
            passWord = tr.getRequiredProperty("password");
            wfSession.logIn(serverName+"Flow", userName, passWord);
            tr.markPassed(testName + " --  Login passed on Server : " + serverName + " with  User Name : " + userName + " and Password : " + passWord);
        } catch (Exception e) {
            logException(testName + " --  Login failed on Server : " + serverName + " with  User Name : " + userName + " and Password : " + passWord, e);
        }
    }

    public
    void
    logOut()
    throws Exception
    {
        try {
            wfSession.logOut();
        } catch (Exception e) {
            logException(testName + " --  Logout failed on Server : " + serverName, e);
        }
        tr.markPassed(testName + " --  Logout passed on Server : " + serverName);
    }

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

}

