/**
 * Core4BPEL - 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.veolia;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;

import javax.xml.namespace.QName;

import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;

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.exception.UserDefinedException;
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.environment.test.util.MockServiceBuilder;
import com.ebmwebsourcing.easyviper.environment.test.util.XMLComparator;
import com.ebmwebsourcing.easyviper.tools.MemoryReceiverImpl;



/**
 * @author Nicolas Salatge - eBM WebSourcing
 */
public class TestVeoliaProcess extends BpelParentTest {


	/**
	 * @param name
	 */
	public TestVeoliaProcess(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();
	}

	private ExecutionEnvironmentTest createAllEnvironment(final boolean explorer)
	throws CoreException, BPELException, URISyntaxException {
		// 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 providers
				ProviderEndpoint accountProvider = env.createProviderEndpoint("Default", AccountService.class);
				ProviderEndpoint contactProvider = env.createProviderEndpoint("Default2", ContactService.class);


		// store the definition
		core.getModel().getRegistry().storeProcessDefinition(new URI("./src/test/resources/veoliaProcess.bpel"), new ProcessContextDefinitionImpl());
		return env;
	}

	public void testVeolia() throws CoreException, URISyntaxException, BPELException, InterruptedException, JDOMException, IOException  {
		final boolean explorer = false;
		ExecutionEnvironmentTest env = createAllEnvironment(explorer);

		//// create client
		ClientEndpoint client = env.createClientEndpoint("client");

		ExternalMessage response = null;
		
		// send client message
		BPELExternalMessage message = new BPELExternalMessageImpl();
		message.setService(new QName("urn:crmondemand/ws/account/10/2004", "ProcessService"));
		message.setEndpoint("ProcessServicePort");
		message.setQName(new QName("urn:crmondemand/ws/account/10/2004", "query_Input"));
		message.setMessage("" +
				"<ns:query_Input xmlns:ns=\"urn:crmondemand/ws/account/10/2004\">" +
				"        <ns:externalSystemId>46546</ns:externalSystemId>" +
				"     </ns:query_Input>" 
		);
		try {
		response = client.sendSync(message);
		} catch(Exception e) {
			e.printStackTrace();
		}




		if(explorer) {
			Thread.sleep(10000000);
		}
		
		// ASSERTION
		BPELExternalMessage expectedResponse = new BPELExternalMessageImpl();
		Element content = MockServiceBuilder.buildContent(new File(
		"./src/test/resources/messages/ProcessResponse.xml"));
		expectedResponse.setContent(content);
		

		assertEquals(expectedResponse.toString(), response.toString());


	}



}

