Package wfxml

Source Code of wfxml.Instance

/*
* This file is part of the WfMOpen project.
* Copyright (C) 2001-2005 Danet GmbH (www.danet.de), GS-AN.
* All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*
* $Id: Instance.java 2432 2007-07-15 22:01:39Z mlipp $
*
* $Log$
* Revision 1.12  2007/03/29 11:45:41  schnelle
* Code cleanup.
*
* Revision 1.11  2007/03/27 21:59:42  mlipp
* Fixed lots of checkstyle warnings.
*
* Revision 1.10  2007/03/01 12:29:30  schnelle
* Enhanced Instance.SetProperties to process ContextData.
*
* Revision 1.9  2007/02/02 08:18:29  schnelle
* Prepared tests with an Umlaut.
*
* Revision 1.8  2007/02/01 13:45:37  schnelle
* Using namespace for factory schemas that do not contain '&'.
*
* Revision 1.7  2007/02/01 12:39:13  schnelle
* Use of receiver toool to keep the first activity running.
*
* Revision 1.6  2007/02/01 08:49:23  drmlipp
* Fixed.
*
* Revision 1.5  2007/01/31 15:13:20  schnelle
* Unified method to create the base url.
*
* Revision 1.4  2007/01/31 13:39:45  schnelle
* Implemented setUp of test cases.
*
* Revision 1.3  2007/01/31 10:57:36  schnelle
* Adapted to change in receiver key.
*
* Revision 1.2  2007/01/30 11:56:16  drmlipp
* Merged Wf-XML branch.
*
* Revision 1.1.2.4  2007/01/10 14:17:21  schnelle
* Implemented unsubscribe.
*
* Revision 1.1.2.3  2006/12/20 14:38:33  schnelle
* Implemented Factory GetDefinition.
*
* Revision 1.1.2.2  2006/12/14 08:51:05  schnelle
* Implemented CompleteActivity.
*
* Revision 1.1.2.1  2006/12/12 09:35:19  schnelle
* Implemented ChangeState for Instance.
*
* Revision 1.1.2.2  2006/12/11 12:22:31  schnelle
* Support of process creation with a later start.
*
* Revision 1.1.2.1  2006/12/01 15:31:28  schnelle
* Separation of test cases dependend on the role.
*
* Revision 1.4.6.7  2006/12/01 14:18:00  schnelle
* Added support for schema type for data in create process
*
* Revision 1.4.6.6  2006/12/01 12:50:11  schnelle
* Basic import of context data for process creation.
*
* Revision 1.4.6.5  2006/11/30 12:45:20  schnelle
* Basic implementation of Factory CreateInstance.
*
* Revision 1.4.6.4  2006/11/30 10:38:21  schnelle
* Implementation of Factory ListInstance.
*
* Revision 1.4.6.3  2006/11/29 14:13:03  schnelle
* Take respect to namespaces of asap requests and responses.
*
* Revision 1.4.6.2  2006/11/29 11:05:40  schnelle
* Full implementation of the request and response headers.
*
* Revision 1.4.6.1  2006/11/28 15:32:35  schnelle
* Proper selection of the response generator.
*
* Revision 1.4  2006/09/29 12:32:11  drmlipp
* Consistently using WfMOpen as projct name now.
*
* Revision 1.3  2005/01/24 21:11:39  mlipp
* Extended.
*
* Revision 1.2  2005/01/24 20:26:01  mlipp
* Reverted saaj back to 1.1 to fit Axis version.
*
* Revision 1.1  2005/01/23 21:44:23  mlipp
* Initial version.
*
*/
package wfxml;

import java.io.IOException;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.StringTokenizer;

import javax.servlet.GenericServlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.MimeHeaders;
import javax.xml.soap.Name;
import javax.xml.soap.SOAPBodyElement;
import javax.xml.soap.SOAPElement;
import javax.xml.soap.SOAPException;
import javax.xml.soap.SOAPMessage;

import org.apache.xmlrpc.webserver.ServletWebServer;

import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import de.danet.an.util.XMLUtil;

/**
* Test WfXML Instance resource.
* @author Dirk Schnelle
* @author Michael Lipp
*/
public class Instance extends TestCase {
    /** The factory resource. */
    private String instance;
   
    private String timestamp;
   
    /**
     * Constructor of this TestCase.
     * @param name name of this test case.
     */
    public Instance(String name) {
  super (name);
    }

    /**
     * Creates this test suite.
     * @return created test suite.
     */
    public static Test suite() {
        TestSuite suite = new TestSuite();
        suite.addTest(new Instance("listActivities"));
        suite.addTest(new Instance("getProperties"));
        suite.addTest(new Instance("setProperties"));
        suite.addTest(new Instance("subscribe"));
        suite.addTest(new Instance("unsubscribe"));
        suite.addTest(new Instance("changeState"));
        suite.addTest(new Instance("receiveEvent"));
       
        return suite;
    }

   
    /* (non-Javadoc)
     * @see junit.framework.TestCase#setUp()
     */
    protected void setUp() throws Exception {
        timestamp = XMLUtil.toXsdLocalDateTime(new java.util.Date());

        String sender = Factory.class.getName() + "/setUp";
        String id = "43";

        ServiceRegistry.importDefinition(sender, id);
        String factory = ServiceRegistry.getTestProcessFactory();
        instance = Factory.startTestProcess(factory,
                "wfxmltest instance " + timestamp);
       
        super.setUp();
    }

    /**
     * Test Factory ListInstances.
     * @exception Exception
     *            Test failed.
     */
    public void changeState() throws Exception {
        String sender = Instance.class.getName() + "/changeState";
        String id = "instance42";
       
        SOAPMessage message = Basic.createMessage();
        Basic.fillRequestHeadet(message, instance, sender, id);
        SOAPElement action
            = Basic.createActionElement(message, "ChangeStateRq");
       
        SOAPElement name = action.addChildElement("State", "as");
        String requestedState = "open.notrunning.suspended";
        name.addTextNode(requestedState);
       
        SOAPMessage response = Basic.call(message);

        Basic.checkNoFault(response);
       
        assertEquals(sender, Basic.getHeaderValue(response, "ReceiverKey"));
        assertEquals(instance, Basic.getHeaderValue(response, "SenderKey"));
        assertEquals(id, Basic.getHeaderValue(response, "RequestID"));
       
        SOAPBodyElement responseNode = Basic.getFirstBodyElement(response);
        Name answerName = responseNode.getElementName();
        assertEquals(Basic.ASAP_NS, answerName.getURI());
        assertEquals("ChangeStateRs", answerName.getLocalName());
        Iterator processIterator = responseNode.getChildElements();
        assertTrue(processIterator.hasNext());
        SOAPElement child = (SOAPElement) processIterator.next();
        Name stateName = child.getElementName();
        assertEquals("as", stateName.getPrefix());
        assertEquals("State", stateName.getLocalName());
           
        String newState = XMLUtil.getFirstLevelTextContent(child);
        assertEquals(requestedState, newState);
    }

    /**
     * Test Factory ListInstances.
     * @exception Exception
     *            Test failed.
     */
    public void listActivities() throws Exception {
        String sender = Instance.class.getName() + "/listActivities";
        String id = "instance43";
       
        SOAPMessage message = Basic.createMessage();
        Basic.fillRequestHeadet(message, instance, sender, id);
        Basic.createActionElement(message, "ListActivitiesRq");
       
        SOAPMessage response = Basic.call(message);

        Basic.checkNoFault(response);
       
        assertEquals(sender, Basic.getHeaderValue(response, "ReceiverKey"));
        assertEquals(instance, Basic.getHeaderValue(response, "SenderKey"));
        assertEquals(id, Basic.getHeaderValue(response, "RequestID"));
       
        SOAPBodyElement activitylist = Basic.getFirstBodyElement(response);
        Name answerName = activitylist.getElementName();
        assertEquals(Basic.WFXML_NS, answerName.getURI());
        assertEquals("ListActivitiesRs", answerName.getLocalName());
        for (Iterator activityIterator = activitylist.getChildElements();
            activityIterator.hasNext();) {
           
            SOAPElement child = (SOAPElement) activityIterator.next();
            Name childName = child.getElementName();
            assertEquals("wfxml", childName.getPrefix());
            assertEquals("ActivityInfo", childName.getLocalName());
           
            String activityKey
                = Basic.getChildTextContent(child,  "ActivityKey");
            assertFalse(activityKey.equals(""));
        }
    }

    /**
     * Test Instance getProperties.
     * @exception Exception
     *            Test failed.
     */
    public void getProperties() throws Exception {
        String sender = Instance.class.getName() + "/getProperties";
        String id = "instance44";
       
        SOAPMessage message = Basic.createMessage();
        Basic.fillRequestHeadet(message, instance, sender, id);
        Basic.createActionElement(message, "GetPropertiesRq");
       
        SOAPMessage response = Basic.call(message);
       
        Basic.checkNoFault(response);
       
        assertEquals(sender, Basic.getHeaderValue(response, "ReceiverKey"));
        assertEquals(instance, Basic.getHeaderValue(response, "SenderKey"));
        assertEquals(id, Basic.getHeaderValue(response, "RequestID"));
       
        SOAPBodyElement processlist = Basic.getFirstBodyElement(response);
        Name answerName = processlist.getElementName();
        assertEquals(Basic.ASAP_NS, answerName.getURI());
        assertEquals("GetPropertiesRs", answerName.getLocalName());
    }

    /**
     * Test Instance setProperties.
     * @exception Exception
     *            Test failed.
     */
    public void setProperties() throws Exception {
        String sender = Instance.class.getName() + "/getProperties";
        String id = "instance45";
       
        SOAPMessage message = Basic.createMessage();
        Basic.fillRequestHeadet(message, instance, sender, id);
        SOAPElement action
            = Basic.createActionElement(message, "SetPropertiesRq");
       
        SOAPElement cd = action.addChildElement("Data", "as");
        cd.addNamespaceDeclaration("pd",
                "http://localhost:8080/wfxml/dummy-pd.xsd");       

        SOAPElement stringdata = cd.addChildElement("newpkg_wp1_string", "pd");
        stringdata.addTextNode("text content " + timestamp);
        SOAPElement intdata = cd.addChildElement("newpkg_wp1_integer", "pd");
        intdata.addTextNode("45");
        SOAPElement floatdata = cd.addChildElement("newpkg_wp1_float", "pd");
        floatdata.addTextNode("45.45");
        SOAPElement booldata = cd.addChildElement("newpkg_wp1_boolean", "pd");
        booldata.addTextNode("true");
        SOAPElement datedata = cd.addChildElement("newpkg_wp1_date", "pd");
        datedata.addTextNode(timestamp);
        SOAPElement schemadata = cd.addChildElement("newpkg_wp1_schema", "pd");
        SOAPElement order = schemadata.addChildElement("order");
        order.setAttribute("id", "08/17");
        SOAPElement position1 = order.addChildElement("position");
        position1.setAttribute("pos", "3");
        SOAPElement position2 = order.addChildElement("position");
        position2.setAttribute("pos", "4");
       
        SOAPMessage response = Basic.call(message);
       
        Basic.checkNoFault(response);
       
        assertEquals(sender, Basic.getHeaderValue(response, "ReceiverKey"));
        assertEquals(instance, Basic.getHeaderValue(response, "SenderKey"));
        assertEquals(id, Basic.getHeaderValue(response, "RequestID"));
       
        SOAPBodyElement processlist = Basic.getFirstBodyElement(response);
        Name answerName = processlist.getElementName();
        assertEquals(Basic.ASAP_NS, answerName.getURI());
        assertEquals("SetPropertiesRs", answerName.getLocalName());
    }
   
    /**
     * Test Instance subscribe.
     * @exception Exception
     *            Test failed.
     */
    public void subscribe() throws Exception {
        String sender = Instance.class.getName() + "/subscribe";
        String id = "instance46";
       
        SOAPMessage message = Basic.createMessage();
        Basic.fillRequestHeadet(message, instance, sender, id);
        SOAPElement action
            = Basic.createActionElement(message, "SubscribeRq");

        SOAPElement observer = action.addChildElement("ObserverKey", "as");
        observer.addTextNode(sender);
       
        SOAPMessage response = Basic.call(message);
       
        Basic.checkNoFault(response);
       
        assertEquals(sender, Basic.getHeaderValue(response, "ReceiverKey"));
        assertEquals(instance, Basic.getHeaderValue(response, "SenderKey"));
        assertEquals(id, Basic.getHeaderValue(response, "RequestID"));
       
        SOAPBodyElement processlist = Basic.getFirstBodyElement(response);
        Name answerName = processlist.getElementName();
        assertEquals(Basic.ASAP_NS, answerName.getURI());
        assertEquals("SubscribeRs", answerName.getLocalName());
    }

    /**
     * Test Instance unsubscribe.
     * @exception Exception
     *            Test failed.
     */
    public void unsubscribe() throws Exception {
        String sender = Instance.class.getName() + "/unsubscribe";
        String id = "instance46";
       
        SOAPMessage message = Basic.createMessage();
        Basic.fillRequestHeadet(message, instance, sender, id);
        SOAPElement action
            = Basic.createActionElement(message, "UnsubscribeRq");

        SOAPElement observer = action.addChildElement("ObserverKey", "as");
        observer.addTextNode(Instance.class.getName() + "/subscribe");
       
        SOAPMessage response = Basic.call(message);
       
        Basic.checkNoFault(response);
       
        assertEquals(sender, Basic.getHeaderValue(response, "ReceiverKey"));
        assertEquals(instance, Basic.getHeaderValue(response, "SenderKey"));
        assertEquals(id, Basic.getHeaderValue(response, "RequestID"));
       
        SOAPBodyElement processlist = Basic.getFirstBodyElement(response);
        Name answerName = processlist.getElementName();
        assertEquals(Basic.ASAP_NS, answerName.getURI());
        assertEquals("UnsubscribeRs", answerName.getLocalName());
    }
   
    /**
     * Receive an event
     */
    public void receiveEvent () throws Exception {
        EventReceiver evtRec = new EventReceiver();
        ServletWebServer server = new ServletWebServer (evtRec, 18080);
        server.start ();
        try {
            String instanceKey = createProcess();
            subscribeToProcess(instanceKey, "http://localhost:18080");
            startProcess(instanceKey);
            for (int i = 15; i > 0; i--) {
                if (evtRec.isCalled()) {
                    break;
                }
                Thread.sleep(1000);
            }
            assertTrue (evtRec.isCalled());
        } finally {
            server.shutdown();
        }
    }
   
    public class EventReceiver extends HttpServlet {
        private boolean called = false;

        /* (non-Javadoc)
         * Comment copied from interface or superclass.
         */
        public void init(ServletConfig arg0) throws ServletException {
        }

        /* (non-Javadoc)
         * Comment copied from interface or superclass.
         */
        protected void doPost
            (HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
            MessageFactory messageFactory = null;
            try {
                messageFactory = MessageFactory.newInstance();
            } catch (SOAPException e) {
                response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
                        "Unable to access SOAP subsystem: " + e.getMessage());
                return;
            }

            SOAPMessage respMsg = null;
            MimeHeaders mimeHeaders = new MimeHeaders();
            for (Enumeration en = request.getHeaderNames(); en
                    .hasMoreElements();) {
                String headerName = (String) en.nextElement();
                String headerVal = request.getHeader(headerName);
                StringTokenizer tk = new StringTokenizer(headerVal, ",");
                while (tk.hasMoreTokens()) {
                    mimeHeaders.addHeader(headerName, tk.nextToken().trim());
                }
            }
           
            // build request message from headers and stream
            try {
                SOAPMessage reqMsg = messageFactory.createMessage(mimeHeaders,
                        request.getInputStream());
                respMsg = messageFactory.createMessage();
                OutputStream os = response.getOutputStream();
                respMsg.writeTo(os);
                os.flush();
            } catch (SOAPException e) {
                throw new ServletException(e);
            }

            called = true;
        }

        /**
         * @return Returns the called.
         */
        public boolean isCalled() {
            return called;
        }
    }
   
    private String createProcess () throws Exception {
        String factory = ServiceRegistry.getTestProcessFactory();
        String sender = Factory.class.getName() + "/createProcInstance";
        String id = "43";
       
        SOAPMessage message = Basic.createMessage();
        Basic.fillRequestHeadet(message, factory, sender, id);
        SOAPElement action
            = Basic.createActionElement(message, "CreateInstanceRq");
        SOAPElement start = action.addChildElement("StartImmediately", "as");
        start.addTextNode("NO");

        SOAPElement cd = action.addChildElement("ContextData", "as");
        cd.addNamespaceDeclaration("pd",
                "http://localhost:8080/wfxml/dummy-pd.xsd");       

        SOAPElement name = action.addChildElement("Name", "as");
        name.addTextNode("wfxmltest factory " + timestamp);
        SOAPElement stringdata = cd.addChildElement("newpkg_wp1_string", "pd");
        stringdata.addTextNode("text content " + timestamp);
        SOAPElement intdata = cd.addChildElement("newpkg_wp1_integer", "pd");
        intdata.addTextNode("43");
        SOAPElement floatdata = cd.addChildElement("newpkg_wp1_float", "pd");
        floatdata.addTextNode("43.43");
        SOAPElement booldata = cd.addChildElement("newpkg_wp1_boolean", "pd");
        booldata.addTextNode("true");
        SOAPElement datedata = cd.addChildElement("newpkg_wp1_date", "pd");
        datedata.addTextNode(timestamp);
        SOAPElement schemadata = cd.addChildElement("newpkg_wp1_schema", "pd");
        SOAPElement order = schemadata.addChildElement("order");
        order.setAttribute("id", "08/16");
        SOAPElement position1 = order.addChildElement("position");
        position1.setAttribute("pos", "1");
        SOAPElement position2 = order.addChildElement("position");
        position2.setAttribute("pos", "2");
       
        SOAPMessage response = Basic.call(message);

        Basic.checkNoFault(response);
       
        SOAPBodyElement processlist = Basic.getFirstBodyElement(response);
        Name answerName = processlist.getElementName();
        Iterator processIterator = processlist.getChildElements();
        SOAPElement child = (SOAPElement) processIterator.next();
        Name childName = child.getElementName();
       
        return XMLUtil.getFirstLevelTextContent(child);   
    }
   
    public void subscribeToProcess(String instance, String obsEPR)
        throws Exception {
        String sender = Instance.class.getName() + "/subscribe";
        String id = "instance46";
       
        SOAPMessage message = Basic.createMessage();
        Basic.fillRequestHeadet(message, instance, sender, id);
        SOAPElement action
            = Basic.createActionElement(message, "SubscribeRq");

        SOAPElement observer = action.addChildElement("ObserverKey", "as");
        observer.addTextNode(obsEPR);
       
        SOAPMessage response = Basic.call(message);
       
        Basic.checkNoFault(response);
    }
   
    public void startProcess(String instance) throws Exception {
        String sender = Instance.class.getName() + "/changeState";
        String id = "instance42";
       
        SOAPMessage message = Basic.createMessage();
        Basic.fillRequestHeadet(message, instance, sender, id);
        SOAPElement action
            = Basic.createActionElement(message, "ChangeStateRq");
       
        SOAPElement name = action.addChildElement("State", "as");
        String requestedState = "open.running";
        name.addTextNode(requestedState);
       
        SOAPMessage response = Basic.call(message);

        Basic.checkNoFault(response);
    }
   
    /**
     * retrieves the first activity of the given process instance.
     * @param testInstance the instance
     * @return key of the first activity.
     * @throws SOAPException
     *         Error in the SOAP message.
     */
    static String getFirstActivity(String testInstance)
            throws SOAPException {
        String sender = Instance.class.getName() + "/getFirstActivity";
        String id = "instance43";
       
        SOAPMessage message = Basic.createMessage();
        Basic.fillRequestHeadet(message, testInstance, sender, id);
        Basic.createActionElement(message, "ListActivitiesRq");
       
        SOAPMessage response = Basic.call(message);
       
        SOAPBodyElement activitylist = Basic.getFirstBodyElement(response);
        Iterator activityIterator = activitylist.getChildElements();
        SOAPElement child = (SOAPElement) activityIterator.next();

        return Basic.getChildTextContent(child,  "ActivityKey");
    }
}
TOP

Related Classes of wfxml.Instance

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.