[jboss-svn-commits] JBL Code SVN: r11301 - in labs/jbossesb/trunk/product/services/jbpm/src: test/java/org/jboss/soa/esb/services/jbpm/actions and 1 other directory.

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Tue Apr 24 10:50:13 EDT 2007


Author: estebanschifman
Date: 2007-04-24 10:50:12 -0400 (Tue, 24 Apr 2007)
New Revision: 11301

Added:
   labs/jbossesb/trunk/product/services/jbpm/src/test/java/org/jboss/soa/esb/services/jbpm/actions/EsbActionHandlerUnitTest.java
Modified:
   labs/jbossesb/trunk/product/services/jbpm/src/main/java/org/jboss/soa/esb/services/jbpm/actions/EsbActionHandler.java
   labs/jbossesb/trunk/product/services/jbpm/src/test/java/org/jboss/soa/esb/services/jbpm/actions/CommandInterpreterUnitTest.java
   labs/jbossesb/trunk/product/services/jbpm/src/test/java/org/jboss/soa/esb/services/jbpm/actions/SingleCommandProcessorUnitTest.java
Log:
Unit tests for EsbActionHandler

Modified: labs/jbossesb/trunk/product/services/jbpm/src/main/java/org/jboss/soa/esb/services/jbpm/actions/EsbActionHandler.java
===================================================================
--- labs/jbossesb/trunk/product/services/jbpm/src/main/java/org/jboss/soa/esb/services/jbpm/actions/EsbActionHandler.java	2007-04-24 14:25:54 UTC (rev 11300)
+++ labs/jbossesb/trunk/product/services/jbpm/src/main/java/org/jboss/soa/esb/services/jbpm/actions/EsbActionHandler.java	2007-04-24 14:50:12 UTC (rev 11301)
@@ -115,33 +115,36 @@
 	
 	protected Map<String, String>_outVarMapping;
 	protected Map<String, String>_retVarMapping;
+	protected transient	Token	_token;
 	
 	public void execute(ExecutionContext executionContext) throws Exception 
 	{
-		Message request = getMessageTemplate(executionContext);
-		varsToMessage(executionContext, request);
+		_token			= executionContext.getToken();
+		
+		Message request = getMessageTemplate();
+		varsToRequest(request);
 		if (null==millisToWaitForResponse || millisToWaitForResponse < 1)
 		{
-			Invoker.invoke(request, esbCategoryName, esbServiceName);
+			Invoker.invoke(request,esbCategoryName, esbServiceName);
 			return;
 		}
 		
-		Message response = Invoker.invokeAndAwaitResponse(request, millisToWaitForResponse);
+		Message response = Invoker.invokeAndAwaitResponse
+			(request,esbCategoryName, esbServiceName, millisToWaitForResponse);
 		if (null!=response)
-			varsFromMessage(executionContext, response);
+			varsFromResponse(response);
 	} //________________________________
 
-
-	protected void varsToMessage(ExecutionContext context, Message request) throws Exception 
+	protected void varsToRequest(Message request) 
+		throws Exception 
 	{
-		ContextInstance ctxInstance = context.getContextInstance();
-		Token	token = context.getToken();
+		ContextInstance ctxInstance = _token.getProcessInstance().getContextInstance();
 		Body body = request.getBody();
 		boolean global = (null==processVariables) ? false : processVariables;
 		if (null==jbpmToEsb_variables)
 		{
 			Map map = (global)? ctxInstance.getVariables() 
-					: ctxInstance.getVariables(token);
+					: ctxInstance.getVariables(_token);
 			if (null!=map)
 				for (Iterator iter=map.entrySet().iterator(); iter.hasNext();)
 				{
@@ -165,23 +168,22 @@
 		for (String curr: jbpmNames)
 		{
 			Object obj	= (global) ? ctxInstance.getVariable(curr)
-					: ctxInstance.getVariable(curr, token);
+					: ctxInstance.getVariable(curr, _token);
 			if (null!=obj)
 				body.add(esbNames[index], obj);
 			index++;
 		}
 	} //________________________________
 
-	protected void varsFromMessage(ExecutionContext context, Message response) throws Exception 
+	protected void varsFromResponse(Message response) throws Exception 
 	{
-		ContextInstance ctxInstance = context.getContextInstance();
-		Token token = context.getToken();
+		ContextInstance ctxInstance = _token.getProcessInstance().getContextInstance();
 		Body body = response.getBody();
 		boolean global = (null==processVariables) ? false : processVariables;
 		if (null==return_esbNames)
 		{
 			Map map = (global)? ctxInstance.getVariables() 
-					: ctxInstance.getVariables(token);
+					: ctxInstance.getVariables(_token);
 			if (null!=map)
 				for (Iterator iter=map.keySet().iterator(); iter.hasNext();)
 				{
@@ -192,7 +194,7 @@
 						if (global)
 							ctxInstance.setVariable(key, obj);
 						else
-							ctxInstance.setVariable(key, obj, token);
+							ctxInstance.setVariable(key, obj, _token);
 					}
 				}
 			return;
@@ -214,7 +216,7 @@
 			Object obj	= body.get(curr);
 			String jbpmVar = jbpmNames[index];
 			Object oldVal = (global) ? ctxInstance.getVariable(jbpmVar)
-							: ctxInstance.getVariable(jbpmVar,context.getToken());
+							: ctxInstance.getVariable(jbpmVar,_token);
 			if (null==oldVal)
 			{
 				if (null!=obj)
@@ -222,7 +224,7 @@
 					if (global)
 						ctxInstance.createVariable(jbpmVar, obj);
 					else
-						ctxInstance.createVariable(jbpmVar, obj, context.getToken());
+						ctxInstance.createVariable(jbpmVar, obj, _token);
 				}
 			}
 			else
@@ -232,21 +234,21 @@
 					if (global)
 						ctxInstance.deleteVariable(jbpmVar);
 					else
-						ctxInstance.deleteVariable(jbpmVar, context.getToken());
+						ctxInstance.deleteVariable(jbpmVar, _token);
 				}
 				else
 				{
 					if (global)
 						ctxInstance.setVariable(jbpmVar,obj);
 					else
-						ctxInstance.setVariable(jbpmVar, context.getToken());
+						ctxInstance.setVariable(jbpmVar,obj,_token);
 				}
 			}
 			index++;
 		}
 	} //________________________________
 
-	protected Message getMessageTemplate(ExecutionContext context) throws Exception 
+	protected Message getMessageTemplate() throws Exception 
 	{
 		if (null==esbCategoryName)
 			throw new ConfigurationException("Service category (esbCategoryName element) must not be null");
@@ -257,26 +259,17 @@
 		Call call = template.getHeader().getCall();
 		call.setMessageID(new URI(UUID.randomUUID().toString()));
 		
-		ProcessDefinition process	= context.getProcessDefinition();
-		if (null!=process)
-		{
-			Helper.setObjectValue(template	,Constants.PROCESS_DEFINITION_NAME, process.getName());
-			Helper.setIntValue	 (template	,Constants.PROCESS_DEFINITION_VERSION, process.getVersion());
-			Helper.setLongValue  (template	,Constants.PROCESS_DEFINITION_ID, process.getId());
-		}
+		Helper.setLongValue	 (template,Constants.TOKEN_ID,_token.getId());
+		Helper.setStringValue(template, Constants.CURRENT_NODE_NAME, _token.getNode().getName());
+
+		ProcessInstance instance = _token.getProcessInstance();
+		Helper.setLongValue(template,Constants.PROCESS_INSTANCE_ID,instance.getId());
 		
-		ProcessInstance instance = context.getProcessInstance();
-		if (null!=instance)
-		{
-			Helper.setLongValue(template,Constants.PROCESS_INSTANCE_ID,context.getProcessInstance().getId());
-		}
+		ProcessDefinition process	= instance.getProcessDefinition();
+		Helper.setObjectValue(template	,Constants.PROCESS_DEFINITION_NAME, process.getName());
+		Helper.setIntValue	 (template	,Constants.PROCESS_DEFINITION_VERSION, process.getVersion());
+		Helper.setLongValue  (template	,Constants.PROCESS_DEFINITION_ID, process.getId());
 		
-		Token token	= context.getToken();
-		if (null!=token)
-		{
-			Helper.setLongValue	 (template,Constants.TOKEN_ID,token.getId());
-			Helper.setStringValue(template, Constants.CURRENT_NODE_NAME, token.getNode().getName());
-		}
 		
 		return template;
 	} //________________________________

Modified: labs/jbossesb/trunk/product/services/jbpm/src/test/java/org/jboss/soa/esb/services/jbpm/actions/CommandInterpreterUnitTest.java
===================================================================
--- labs/jbossesb/trunk/product/services/jbpm/src/test/java/org/jboss/soa/esb/services/jbpm/actions/CommandInterpreterUnitTest.java	2007-04-24 14:25:54 UTC (rev 11300)
+++ labs/jbossesb/trunk/product/services/jbpm/src/test/java/org/jboss/soa/esb/services/jbpm/actions/CommandInterpreterUnitTest.java	2007-04-24 14:50:12 UTC (rev 11301)
@@ -64,7 +64,7 @@
     	_logger.info("@BeforeClass invoked");
     	try 
     	{
-    		JbpmConfiguration.getInstance().createSchema();
+    		CommandInterpreter.getJbpmConfig().createSchema();
     	}
     	catch (Exception e)
     	{

Added: labs/jbossesb/trunk/product/services/jbpm/src/test/java/org/jboss/soa/esb/services/jbpm/actions/EsbActionHandlerUnitTest.java
===================================================================
--- labs/jbossesb/trunk/product/services/jbpm/src/test/java/org/jboss/soa/esb/services/jbpm/actions/EsbActionHandlerUnitTest.java	                        (rev 0)
+++ labs/jbossesb/trunk/product/services/jbpm/src/test/java/org/jboss/soa/esb/services/jbpm/actions/EsbActionHandlerUnitTest.java	2007-04-24 14:50:12 UTC (rev 11301)
@@ -0,0 +1,264 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2006, JBoss Inc., and individual contributors as indicated
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+
+package org.jboss.soa.esb.services.jbpm.actions;
+
+import static org.junit.Assert.assertTrue;
+
+import java.io.File;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.Map;
+
+import junit.framework.JUnit4TestAdapter;
+
+import org.apache.log4j.Logger;
+import org.jboss.soa.esb.helpers.ConfigTree;
+import org.jboss.soa.esb.message.Body;
+import org.jboss.soa.esb.message.Message;
+import org.jboss.soa.esb.services.jbpm.Constants;
+import org.jboss.soa.esb.services.jbpm.util.Helper;
+import org.jbpm.context.exe.ContextInstance;
+import org.jbpm.graph.exe.ExecutionContext;
+import org.jbpm.graph.exe.ProcessInstance;
+import org.jbpm.graph.exe.Token;
+import org.junit.AfterClass;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+
+/**
+ * Test the EsbActionHandler class
+ *
+ * @author <a href="mailto:schifest at heuristica.com.ar">Esteban</a>
+ */
+
+
+public class EsbActionHandlerUnitTest
+{
+
+	private static Class<EsbActionHandlerUnitTest> 		thisClass = EsbActionHandlerUnitTest.class;
+	static Logger 				_logger = Logger.getLogger(thisClass);
+
+    public static junit.framework.Test suite()
+    {
+        return new JUnit4TestAdapter(thisClass);
+    }
+    
+    static File WORKDIR;
+    static
+    {
+    	String os = System.getProperty("os.name","").toLowerCase();
+    	String dflt = (os.indexOf("win")>=0) ? "/temp": "/tmp";
+    	WORKDIR	= new File(System.getProperty("java.io.tmpdir",dflt));
+    }
+
+    @BeforeClass
+    public static void runBeforeAllTests()
+    {
+    	_logger.info("@BeforeClass invoked");
+    	try 
+    	{
+    		CommandInterpreter.getJbpmConfig().createSchema();
+    	}
+    	catch (Exception e)
+    	{
+    		e.printStackTrace();
+    		assertTrue(false);
+    	}
+    }
+
+
+    @AfterClass
+    public static void runAfterAllTests() throws Exception
+    {
+    	_logger.info("@AfterClass invoked");
+    }
+
+    @Test
+    public void mockTest()
+    {
+    	// place holder so test will run even with no other @Test
+    	_logger.info("Mock test invoked");
+    	assertTrue(true);
+    }
+    
+    @SuppressWarnings("unchecked")
+	@Test
+	public void testEsbActionHandler()
+	{
+    	Message msg=null;
+    	MyEsbActionHandler handler = null;
+    	
+    	for(Boolean processVars: new boolean[]{false,true})   
+			try
+			{
+				// use the same config as the SingleCommandProcessorUnitTest
+				String xml = JbpmTestUtil.stringFromFile("testSingleCommands.xml");
+				ConfigTree[] actions = ConfigTree.fromXml(xml).getChildren("action");
+	
+				// create a process def
+				SingleCommandProcessor command = new SingleCommandProcessor(actions[0]);
+				command.process(Helper.commandMessageTemplate());
+	
+				// create a process instance
+				command = new SingleCommandProcessor(actions[1]);
+				msg = command.process(Helper.commandMessageTemplate());
+				ProcessInstance inst = (ProcessInstance)msg.getBody()
+						.get(Constants.JBPM_RETURN_OBJECT);
+	
+				Token			token = inst.getRootToken();
+				ContextInstance	ctx	= inst.getContextInstance();
+				
+				handler	= getHandler(token, "No variables - No mappings - no response",processVars);
+				handler.execute(null);
+				
+				handler	= getHandler(token,"V3 and V2  - No mappings - no response",processVars);
+				handler.jbpmToEsb_variables="jbV3,jbV2";
+				handler.execute(null);	
+				
+				handler	= getHandler(token,"V3 and V2  mapped - no response",processVars);
+				handler.jbpmToEsb_variables="jbV3,jbV2";
+				handler.jbpmToEsb_esbNames="esbV3,esbV2";
+				handler.execute(null);	
+				
+				handler	= getHandler(token, "No variables - No mappings - Response expected",processVars);
+				handler.millisToWaitForResponse=1;
+				handler.execute(null);
+				assertTrue((s_varMap.get("jbV1")+"_P").equals(ctx.getVariable("jbV1",token)));
+				assertTrue((s_varMap.get("jbV2")+"_P").equals(ctx.getVariable("jbV2",token)));
+				assertTrue((s_varMap.get("jbV3")+"_P").equals(ctx.getVariable("jbV3",token)));
+				
+				handler	= getHandler(token,"All variables mapped both ways - Response expected",processVars);
+				handler.jbpmToEsb_variables="jbV3,jbV2,jbV1";
+				handler.jbpmToEsb_esbNames="esbV3,esbV2,esbV1";
+				handler.millisToWaitForResponse=1;
+	
+				handler.return_esbNames="esbV1,esbV2,esbV3";
+				handler.return_variables="jbV1,jbV2,jbV3";
+				
+				handler.execute(null);
+				assertTrue((s_varMap.get("jbV1")+"_P").equals(ctx.getVariable("jbV1",token)));
+				assertTrue((s_varMap.get("jbV2")+"_P").equals(ctx.getVariable("jbV2",token)));
+				assertTrue((s_varMap.get("jbV3")+"_P").equals(ctx.getVariable("jbV3",token)));
+	
+				handler	= getHandler(token,"V1 mapped both ways (trick j1 to e3 and back) - Response expected",processVars);
+				handler.jbpmToEsb_variables="jbV1";
+				handler.jbpmToEsb_esbNames="esbV3";
+				handler.return_esbNames="esbV3";
+				handler.return_variables="jbV1";
+				handler.millisToWaitForResponse=1;
+				handler.execute(null);
+				assertTrue((s_varMap.get("jbV1")+"_P").equals(ctx.getVariable("jbV1",token)));
+				assertTrue(s_varMap.get("jbV2").equals(ctx.getVariable("jbV2",token)));
+				assertTrue(s_varMap.get("jbV3").equals(ctx.getVariable("jbV3",token)));
+	
+			}
+			catch (Exception e)
+			{
+				e.printStackTrace();
+				assertTrue(false);
+			}
+	} //________________________________
+    
+    private static MyEsbActionHandler getHandler(Token token, String title, boolean processVars)
+    {
+    	Iterator iter=null;
+    	
+		ContextInstance	ctxInstance = token.getProcessInstance().getContextInstance();
+	
+		iter = ctxInstance.getVariables(token).keySet().iterator();
+		while (iter.hasNext())
+			ctxInstance.deleteVariable((String)iter.next(),token);
+		
+		iter = ctxInstance.getVariables().keySet().iterator();
+		while (iter.hasNext())
+			ctxInstance.deleteVariable((String)iter.next());
+
+		ctxInstance.addVariables(s_varMap,token);
+		
+		MyEsbActionHandler handler = new MyEsbActionHandler(title,processVars);
+		handler.setToken(token);
+
+		return handler;
+    } //________________________________
+    
+    private static final String[] s_vars = 
+    	{"jbV1","jbV2","jbV3","esbV1","esbV2","esbV3"};
+	private static final Map<String,Object> s_varMap	= new HashMap<String, Object>();
+	static
+	{
+		s_varMap.put("jbV1","1");
+		s_varMap.put("jbV2","22");
+		s_varMap.put("jbV3","333");
+	};
+    private static void dumpMessage(String title, Message msg) throws Exception
+    {
+    	StringBuilder sb = new StringBuilder("\n---").append(title).append("---");
+    	Body body = msg.getBody();
+    	for (String curr:s_vars)
+    		sb.append(" ").append(curr).append("=").append(body.get(curr));
+    	System.out.println(sb);
+    }
+
+	private static class MyEsbActionHandler extends EsbActionHandler
+	{
+		public static final long serialVersionUID = 1L;
+		public void setToken(Token token)	{_token = token; }
+		
+		public MyEsbActionHandler(String title, boolean processLevelVars) 
+		{
+			String level = (processLevelVars)?"PROCESS VARS ":"TOKEN VARS";
+			System.out.println(level+" "+title);
+			esbCategoryName	= "mockCategory";
+			esbServiceName	= "mockServiceName";
+			processVariables = processLevelVars;
+		}
+		
+		
+		// this is a mock execute method to avoid need of ESB running for
+		// the unit test
+		@Override
+		public void execute(ExecutionContext ctx) throws Exception
+		{
+			// Does the same as the real one, but does not go to ESB
+			Message request = getMessageTemplate();
+			varsToRequest(request);
+			dumpMessage("Request ",request);
+			
+			if (null!=millisToWaitForResponse && millisToWaitForResponse > 0)
+			{
+				Body body	= request.getBody();
+				for(String curr:s_vars)
+				{
+					String val = (String)body.get(curr);
+					if (null!=val)
+						body.add(curr, val+"_P");
+				}
+				
+				dumpMessage("Response ",request);
+				varsFromResponse(request);
+			}
+			System.out.println("______________________________________");
+		}
+	};
+}
\ No newline at end of file


Property changes on: labs/jbossesb/trunk/product/services/jbpm/src/test/java/org/jboss/soa/esb/services/jbpm/actions/EsbActionHandlerUnitTest.java
___________________________________________________________________
Name: svn:eol-style
   + native

Modified: labs/jbossesb/trunk/product/services/jbpm/src/test/java/org/jboss/soa/esb/services/jbpm/actions/SingleCommandProcessorUnitTest.java
===================================================================
--- labs/jbossesb/trunk/product/services/jbpm/src/test/java/org/jboss/soa/esb/services/jbpm/actions/SingleCommandProcessorUnitTest.java	2007-04-24 14:25:54 UTC (rev 11300)
+++ labs/jbossesb/trunk/product/services/jbpm/src/test/java/org/jboss/soa/esb/services/jbpm/actions/SingleCommandProcessorUnitTest.java	2007-04-24 14:50:12 UTC (rev 11301)
@@ -65,7 +65,7 @@
     	_logger.info("@BeforeClass invoked");
     	try 
     	{
-    		JbpmConfiguration.getInstance().createSchema();
+    		CommandInterpreter.getJbpmConfig().createSchema();
     	}
     	catch (Exception e)
     	{




More information about the jboss-svn-commits mailing list