Package org.apache.agila.services

Source Code of org.apache.agila.services.InstanceServiceTestCase

/*
* Copyright 2004 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License")
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.agila.services;

import java.util.HashMap;
import java.util.Map;

import org.apache.agila.engine.EngineMessage;
import org.apache.agila.engine.Instance;
import org.apache.agila.engine.InstanceID;
import org.apache.agila.engine.MessageProcessor;
import org.apache.agila.engine.Token;
import org.apache.agila.engine.TokenID;
import org.apache.agila.impl.BusinessProcessImpl;
import org.apache.agila.impl.QueueServiceImpl;
import org.apache.agila.impl.memory.TimerServiceImpl;
import org.apache.agila.impl.memory.InstanceServiceImpl;
import org.apache.agila.impl.memory.TokenServiceImpl;
import org.apache.agila.impl.memory.BusinessProcessServiceImpl;
import org.apache.agila.impl.memory.TaskServiceImpl;
import org.apache.agila.model.BusinessProcessID;
import org.apache.agila.model.Connection;
import org.apache.agila.model.Node;
import org.apache.agila.model.NodeContext;
import org.apache.agila.model.NodeID;
import org.apache.agila.model.node.BaseNodeImpl;
import org.apache.agila.services.task.AbstractTaskService;

import junit.framework.TestCase;

/**
* I haven't gotten around to "elementarizing" the other services... maybe if I have more
* time I will do that.
*
*/
public class InstanceServiceTestCase extends TestCase {

    AbstractInstanceService instanceService;

    AbstractBusinessProcessService bpService;
    TokenServiceImpl tokenService;
    QueueServiceImpl qService;
    MessageProcessor msgProcessor;
    TimerServiceImpl timerService;
    AbstractTaskService taskService;


    EngineMessage engineMessage;

    static int count = 1;

    /**
     * Obtain an implementation of an instance service based on test.properties, then
     * inject business process, queue, and token services into it.
     *
     * @see TestCase#setUp()
     */
    protected void setUp() throws Exception {
        super.setUp();
        // create the basic services
        instanceService = new InstanceServiceImpl();
        tokenService = new TokenServiceImpl();
        bpService = new BusinessProcessServiceImpl();
        timerService = new TimerServiceImpl();
        taskService = new TaskServiceImpl();

        // create a custom message processor that will allow us access to the EngineMessage
        msgProcessor = new MessageProcessor() {
            public boolean processMessage(EngineMessage msg) {
                InstanceServiceTestCase.this.engineMessage = msg;

                return false;
            }
        };

        msgProcessor.setBusinessProcessService(bpService);
        msgProcessor.setExecutionInstanceService(instanceService);
        msgProcessor.setTokenService(tokenService);

        qService = new QueueServiceImpl(msgProcessor);
        msgProcessor.setQueueService(qService);

        timerService.setQueueService(qService);

        // set services used by instance service
        instanceService.setBusinessProcessService(bpService);
        instanceService.setQueueService(qService);
        instanceService.setTokenService(tokenService);

        // start the queue service
        qService.start();
    }

    /*
     * @see TestCase#tearDown()
     */
    protected void tearDown() throws Exception {
        super.tearDown();
    }

    /**
     * Test the creation of a new process instance.
     *
     */
    public void testNewInstance() {
        // get a business process id
        BusinessProcessImpl bProcess = new BusinessProcessImpl();
        String graphName = "Graph" + count++;
        bProcess.setName(graphName);
        BusinessProcessID bpId = bpService.addGraph(bProcess);

        // then create a map of parameters
        Map params = new HashMap();
        params.put("foo", "foo value");
        params.put("bar", "bar value");
        params.put("baz", "baz value");

        // create a new instance using the service
        InstanceID instanceId = instanceService.newInstance(bpId, params);

        Instance instance = instanceService.getInstanceByID(instanceId);

        assertEquals("Instance id's are not equal", instanceId, instance.getInstanceID());
        assertNotNull("Application parameter map is null", instance.getInstanceVariables());
        assertEquals("There must be three parameters", 3, instance.getInstanceVariables().size());
        // also get the parameter values?
        assertEquals("The graph must be named '"+ graphName +"'", graphName, instance.getGraphName());
        assertEquals("Instance doesn't have a running status", Instance.STATUS_RUNNING, instance.getStatus());
    }

    /**
     * Test starting a created instance.
     *
     */
    public void testStart() {
        // get a business process id
        BusinessProcessImpl bProcess = new BusinessProcessImpl();
        String graphName = "Graph" + count++;
        bProcess.setName(graphName);
        Node rootNode = new BaseNodeImpl() {

            public Connection[] doEnd(NodeContext ctx) {
                return null;
            }
        };

        rootNode.setNodeId(new NodeID(1));
        rootNode.setDisplayName("root");

        bProcess.setRoot(rootNode);
        BusinessProcessID bpId = bpService.addGraph(bProcess);

        // then create a map of parameters
        Map params = new HashMap();
        params.put("foo", "foo value");
        params.put("bar", "bar value");
        params.put("baz", "baz value");

        // create a new instance using the service
        InstanceID instanceId = instanceService.newInstance(bpId, params);

        // start the newly created instance
        instanceService.start(instanceId);

        // wait until the message has been consumed in the queue
        while(engineMessage == null) {
            try {
                Thread.sleep(1000);
            }
            catch (InterruptedException e) {
                e.printStackTrace();
                fail();
            }
        }

        if(engineMessage == null) {
            fail("something went wrong");
        }

        assertEquals("Type must be 'move token'", EngineMessage.TYPE_MOVE_TOKEN, engineMessage.getMessageType());

        TokenID tokenId = engineMessage.getCurrentTokenID();
        Token token = tokenService.getTokenByID(tokenId);
        assertEquals(1, token.getCurrentNodeID().getID());
        assertTrue(token.isActive());
        assertEquals(Token.PRE, token.getCurrentState());
        assertNotNull(bpService.getGraphByID(instanceService.getInstanceByID(token.getInstanceID()).getBusinessProcessID()));
        assertSame(bProcess.getRoot(),
                bpService.getGraphByID(instanceService.getInstanceByID(token.getInstanceID()).getBusinessProcessID()).getRoot());
    }

    public void testSave() {
        // get a business process id
        BusinessProcessImpl bProcess = new BusinessProcessImpl();
        String graphName = "Graph" + count++;
        bProcess.setName(graphName);
        BusinessProcessID bpId = bpService.addGraph(bProcess);

        // then create a map of parameters
        Map params = new HashMap();
        params.put("foo", "foo value");
        params.put("bar", "bar value");
        params.put("baz", "baz value");

        // create a new instance using the service
        InstanceID instanceId = instanceService.newInstance(bpId, params);

        Instance instance = instanceService.getInstanceByID(instanceId);

        assertEquals("Instance id's are not equal", instanceId, instance.getInstanceID());
        assertNotNull("Application parameter map is null", instance.getInstanceVariables());
        assertEquals("There must be three parameters", 3, instance.getInstanceVariables().size());

        assertNotNull(instance.getInstanceVariables().get("foo"));
        assertEquals("foo value", instance.getInstanceVariables().get("foo").toString());
        assertNotNull(instance.getInstanceVariables().get("bar"));
        assertEquals("bar value", instance.getInstanceVariables().get("bar").toString());
        assertNotNull(instance.getInstanceVariables().get("baz"));
        assertEquals("baz value", instance.getInstanceVariables().get("baz").toString());

        assertEquals("The graph must be named '"+ graphName +"'", graphName, instance.getGraphName());
        assertEquals("Instance doesn't have a running status", Instance.STATUS_RUNNING, instance.getStatus());

        // set a new name
        instance.setGraphName("New_Graph_Name");

        // create a new set of parameters and then set it
        Map newParams = new HashMap();
        newParams.put("aaa", "aaa value");
        newParams.put("bbb", "bbb value");
        newParams.put("ccc", "ccc value");
        newParams.put("ddd", "ddd value");

        instance.setInstanceVariables(newParams);
        instance.setStatus(Instance.STATUS_COMPLETE);

        // save it then retrieve the saved instance
        instanceService.save(instance);
        instance = instanceService.getInstanceByID(instanceId);

        // it should still be the same instance, but with the modified values, as above
        assertEquals("Instance id's are not equal", instanceId, instance.getInstanceID());
        assertNotNull("Application parameter map is null", instance.getInstanceVariables());
        assertEquals("There must be four parameters", 4, instance.getInstanceVariables().size());

        assertNotNull(instance.getInstanceVariables().get("aaa"));
        assertEquals("aaa value", instance.getInstanceVariables().get("aaa").toString());
        assertNotNull(instance.getInstanceVariables().get("bbb"));
        assertEquals("bbb value", instance.getInstanceVariables().get("bbb").toString());
        assertNotNull(instance.getInstanceVariables().get("ccc"));
        assertEquals("ccc value", instance.getInstanceVariables().get("ccc").toString());
        assertNotNull(instance.getInstanceVariables().get("ddd"));
        assertEquals("ddd value", instance.getInstanceVariables().get("ddd").toString());

        assertEquals("The graph must be named 'New_Graph_Name'", "New_Graph_Name", instance.getGraphName());
        assertEquals("Instance doesn't have a completed status", Instance.STATUS_COMPLETE, instance.getStatus());
    }
}
TOP

Related Classes of org.apache.agila.services.InstanceServiceTestCase

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.