Source

osworkflow / docs / 2. Testing your workflow.html

Full commit
<html>
    <head>
        <title>2. Testing your workflow</title>
	    <link rel="stylesheet" href="styles/site.css" type="text/css" />
    </head>

    <body>
	    <table class="pagecontent" border="0" cellpadding="0" cellspacing="0" width="100%" bgcolor="#ffffff">
		    <tr>
			    <td valign="top" class="pagebody">

				    <p class="paragraph">Now that we have a complete workflow definition, the next step is to verify thaqt it behaves as expected.</p>The easiest way to do this in a rapid development environment is to write a test case. The test case will invoke the workflow, and by examining the results and the potential errors thrown, we can ensure that our definition is correct.<p class="paragraph">It is assumed that you are already familiar with JUnit and writing testcases. If not, then go to the JUnit website and go through the documentation there. Writing tests will soon become a very useful addition to your toolbox.</p>Before we can load in our workflow descriptor and call actions on it, we need to configure OSWorkflow to inform it of what data store to use, as well as the descriptor location and suchlike.<p class="paragraph"><h3 class="heading3">Configuring osworkflow.xml</h3></p>The first file that needs to be created is <em class="emphasis">osworkflow.xml</em>. Below is a simple example:

<div class="code"><pre>&lt;osworkflow&gt;&#10;  &lt;persistence class=&quot;com.opensymphony.workflow.spi.memory.MemoryWorkflowStore&quot;/&gt;&#10;  &lt;factory class=&quot;com.opensymphony.workflow.loader.XMLWorkflowFactory&quot;&gt;&#10;    &lt;property name=&quot;resource&quot; value=&quot;workflows.xml&quot; /&gt;&#10;  &lt;/factory&gt; &#10;&lt;/osworkflow&gt;</pre></div><p class="paragraph">This example specifies that we should use the memory workflow store. This saves us from the trouble of having to set up a database or some other store that might require a lot of configuration and initialising. Of course, the memory store is only really useful for testing purposes.</p><h3 class="heading3">Workflow factories</h3><p class="paragraph">The configuration file above also specifies that we should use the XML workflow factory. The workflow factory is responsible for managing workflow descriptors. This includes reading them at a bare minimum, and possibly modifying and writing them. The XML workflow factory has a special property called &#039;resource&#039; which specifies the file where the workflow name to descriptor XML file can be found. The resource is loaded from the classpath, so for the case above, you will need to create a file called workflows.xml that is in your classpath.</p>The contents of workflows.xml should be:

<div class="code"><pre>&lt;workflows&gt;&#10;  &lt;workflow name=&quot;mytest&quot; type=&quot;resource&quot; location=&quot;myworkflow.xml&quot;/&gt;&#10;&lt;/workflows&gt;</pre></div><p class="paragraph">So, you need to have the <em class="emphasis">myworkflow.xml</em> file we created earlier alongside workflows.xml, since it will likewise be loaded in as a resources.</p>At this point we&#039;re done with configuration, so first initialize then call our workflow.<p class="paragraph"><h3 class="heading3">Initialising OSWorkflow</h3></p>OSWorkflow has a fairly simple invocation model. There is a main entry point through which almost all interaction takes place. This main entry point is the <em class="emphasis">Workflow</em> interface, and implementation-wise, is usually a subclass of <em class="emphasis">AbstractWorkflow</em>. Example implementations are EJBWorkflow and SOAPWorkflow. For the sake of simplicity, we will use the simplest form, BasicWorkflow.<p class="paragraph">First, we create our workflow. This object should usually be stored in a global location and should be reused. Although not recommended, one way of doing so is to make it a static. Creating a new one every time can be potentially expensive. BasicWorkflow&#039;s constructor takes in one parameter, the username of the current caller. This might seem odd given the earlier recommendation to reuse it, and the fact that any serious usage will involve multiple users. However, most other workflow implementations have their own mechanism for figuring out the current caller, and so are not created &#039;for&#039; a particular user up front.</p>BasicWorkflow provides the ability to pin a workflow to a user for the sake of simplicity and to avoid the hassle of hooking up OSWorkflow to a user lookup mechanism.<p class="paragraph">So, we create our workflow with a user caller &#039;testuser&#039;:</p><div class="code"><pre>Workflow workflow = <span class="java&#45;keyword">new</span> BasicWorkflow(&quot;testuser&quot;);</pre></div><p class="paragraph">The next step is to provide the workflow with a configuration to use. In most cases, it is sufficient to simply pass in a DefaultConfiguration instance, like so:</p><div class="code"><pre>DefaultConfiguration config = <span class="java&#45;keyword">new</span> DefaultConfiguration();&#10;workflow.setConfiguration(config);</pre></div><p class="paragraph">We now have an initialised and configured workflow session, and can move on to invoking a particular workflow and calling actions on it.
 
<h3 class="heading3">Starting and progressing a workflow</h3>
The first thing to do to start a workflow is to call the initialize method. This method takes in 3 parameters. These are the workflow name (how that&#039;ll be handled depends on our workflow factory), the action ID (which initial action we want to call), and inputs to the action. For now, we&#039;ll simply pass in null for the inputs as we aren&#039;t using any (more on them later though).</p><div class="code"><pre><span class="java&#45;object">long</span> workflowId = workflow.initialize(&quot;mytest&quot;, 1, <span class="java&#45;keyword">null</span>);</pre></div><p class="paragraph">We now have a workflow instance started. The ID returned is what we will use to specify this workflow for all future interactions. This ID is a parameter to most of the methods in the Workflow interface.</p><h4 class="heading4">Verifying the workflow</h4><p class="paragraph">Now that we&#039;ve initialised our workflow instance, let&#039;s confirm that it behaves as expected. From our workflow definition, we expect that the current step is 1, and that we should be able to only perform action 1 (start first draft).</p><div class="code"><pre>Collection currentSteps = workflow.getCurrentSteps(workflowId);&#10;//verify we only have one current step&#10;assertEquals(&quot;Unexpected number of current steps&quot;, 1, currentSteps.size());&#10;//verify it&#039;s step 1&#10;Step currentStep = (Step)currentSteps.iterator().next();&#10;assertEquals(&quot;Unexpected current step&quot;, 1, currentStep.getStepId());&#10;&#10;<span class="java&#45;object">int</span>&#91;&#93; availableActions = workflow.getAvailableActions(workflowId);&#10;//verify we only have one available action&#10;assertEquals(&quot;Unexpected number of available actions&quot;, 1, availableActions.length);&#10;//verify it&#039;s action 1&#10;assertEquals(&quot;Unexpected available action&quot;, 1, availableActions&#91;0&#93;);</pre></div><p class="paragraph"><h4 class="heading4">Calling actions</h4></p>Now that we&#039;ve initialised our workflow and verified that it behaves as expected, let&#039;s start the first draft!

<div class="code"><pre>workflow.doAction(workflowId, 1, <span class="java&#45;keyword">null</span>);</pre></div><p class="paragraph">We simply call the first action. The conditions we&#039;ve specified on it will be evaluated, and the workflow transitions to be &#039;Underway&#039;, while remaining in the same step.</p>Similarly, we can then call the second action now that we&#039;ve called the first, since the required conditions are met.<p class="paragraph">After calling the second action, we have no more available actions, and as expected, the getAvailableActions will return an empty array.</p>Congratulations, you have now written and called your first workflow! The next topic will cover more advanced descriptor elements.<p class="paragraph">Go to <a href="3. Further descriptor concepts.html">3. Further descriptor concepts</a></p>

				    
			    </td>
		    </tr>
	    </table>
    </body>
</html>