/**
 * Maestro4BPEL - SOA Tools Platform.
 * Copyright (c) 2008 EBM Websourcing, http://www.ebmwebsourcing.com/
 *
 * This library 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 library 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 library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * -------------------------------------------------------------------------
 * $id.java
 * -------------------------------------------------------------------------
 */
package com.ebmwebsourcing.easybpel.usecase.crisis;


import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

import javax.xml.namespace.QName;

import org.jdom.JDOMException;

import com.ebmwebsourcing.easybpel.model.bpel.api.BPELException;
import com.ebmwebsourcing.easybpel.model.bpel.api.BPELFactory;
import com.ebmwebsourcing.easybpel.model.bpel.api.message.BPELExternalMessage;
import com.ebmwebsourcing.easybpel.model.bpel.impl.BPELFactoryImpl;
import com.ebmwebsourcing.easybpel.model.bpel.impl.message.BPELExternalMessageImpl;
import com.ebmwebsourcing.easybpel.model.bpel.impl.message.DefaultBPELMessageConverter;
import com.ebmwebsourcing.easyviper.core.api.Core;
import com.ebmwebsourcing.easyviper.core.api.CoreException;
import com.ebmwebsourcing.easyviper.core.api.soa.message.ExternalMessage;
import com.ebmwebsourcing.easyviper.core.impl.model.registry.ProcessContextDefinitionImpl;
import com.ebmwebsourcing.easyviper.environment.test.BpelParentTest;
import com.ebmwebsourcing.easyviper.environment.test.env.ExecutionEnvironmentTestFactory;
import com.ebmwebsourcing.easyviper.environment.test.env.TestSenderImpl;
import com.ebmwebsourcing.easyviper.environment.test.env.api.ClientEndpoint;
import com.ebmwebsourcing.easyviper.environment.test.env.api.ExecutionEnvironmentTest;
import com.ebmwebsourcing.easyviper.environment.test.env.api.ProviderEndpoint;
import com.ebmwebsourcing.easyviper.tools.MemoryReceiverImpl;

/**
 * @author Nicolas Salatge - eBM WebSourcing
 */
public class TestCrisisProcess extends BpelParentTest {


    /**
     * @param name
     */
    public TestCrisisProcess(final String name) {
        super(name);
    }

    /*
     * (non-Javadoc)
     *
     * @see junit.framework.TestCase#setUp()
     */
    @Override
    protected void setUp() throws Exception {
        super.setUp();
        System.setProperty("javax.xml.transform.TransformerFactory",
        "com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl");
    }

    /*
     * (non-Javadoc)
     *
     * @see junit.framework.TestCase#tearDown()
     */
    @Override
    protected void tearDown() throws Exception {
        super.tearDown();
    }

    public void testCrisis() throws CoreException, URISyntaxException, BPELException, InterruptedException, JDOMException, IOException  {
    	final boolean explorer = false;

    	// create execution environment
    	ExecutionEnvironmentTest env = ExecutionEnvironmentTestFactory.newInstance().newEnvironment("TestEnvironnement", explorer);
    	
    	//// create core
    	final Core core = BPELFactoryImpl.getInstance().newBPELEngine(DefaultBPELMessageConverter.get(), 1, MemoryReceiverImpl.class, 1, TestSenderImpl.class);
    	env.setCore(core);
    	
    	//// create client
    	ClientEndpoint client = env.createClientEndpoint("client");
    	
		//// create providers
    	ProviderEndpoint firemenProvider = env.createProviderEndpoint("FiremenEndpoint", FiremenService.class);
    	ProviderEndpoint policemenProvider = env.createProviderEndpoint("PolicemenEndpoint", PolicemenService.class);
    	ProviderEndpoint redcrossProvider = env.createProviderEndpoint("RedcrossEndpoint", RedCrossService.class);
    	ProviderEndpoint emsProvider = env.createProviderEndpoint("EmsEndpoint", EmsService.class);
    	
		
    	// store the definition
    	core.getModel().getRegistry().storeProcessDefinition(new URI("./src/test/resources/CrisisOrchestrator.bpel"),
    			new ProcessContextDefinitionImpl());

		// send client message
    	BPELExternalMessage message = new BPELExternalMessageImpl();
		message.setService(new QName("http://petals.ow2.org/", "CrisisOrchestratorBPEL"));
		message.setEndpoint("CrisisOrchestratorEndpoint");
		message.setQName(new QName("http://petals.ow2.org/", "CrisisOrchestratorRequestMessage"));
		message.setMessage("" +
				"<pet:CrisisOrchestratorRequest xmlns:pet=\"http://petals.ow2.org/\">" +
				"       <pet:input>start isycri process</pet:input>" +
				"</pet:CrisisOrchestratorRequest>"
				);
		ExternalMessage response = client.sendSync(message);
    	    			
		
		// ASSERTION
		BPELExternalMessage expectedResponse = new BPELExternalMessageImpl();
		expectedResponse.setMessage("" +
		"<tns:processResponse xmlns:tns=\"http://petals.ow2.org/\">" +
		"  <tns:CrisisOrchestratorResponse>" +
		"    <tns:result xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:type=\"string\">[figth explosion] OK  [how many people to rescue] 3  [set medical post] OK  [rescue people] ?  [bring people to medical post] ?  [care people] ?  [set security perimeter] OK  [set equipment] OK  [maintain perimeter] ?  [end of crisis] ?</tns:result>" +
		"  </tns:CrisisOrchestratorResponse>" +
		"</tns:processResponse>");
		

		
		if(explorer) {
			Thread.sleep(10000000);
		}

		assertEquals(expectedResponse.toString(), response.toString());

		
    }

}

