/**
 * 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.collecteRegimeCNAV;

import java.io.File;
import java.net.URI;

import org.junit.Test;

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.DefaultBPELMessageConverter;
import com.ebmwebsourcing.easyviper.core.api.Core;
import com.ebmwebsourcing.easyviper.core.impl.model.registry.ProcessContextDefinitionImpl;
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 alouis - eBM WebSourcing
 */
public class TestCollecteRegimeCNAVProcess {

    @Test
	public void testCollecteRegimeCNAV() throws Exception {
		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
		env.createProviderEndpoint(
				"ServiceActiviteCollectePartenaire1EJBEndpoint",
				CollecteService1.class);
		env.createProviderEndpoint(
				"ServiceActiviteCollectePartenaire2EJBEndpoint",
				CollecteService2.class);
		env.createProviderEndpoint(
				"ServiceActiviteCollectePartenaire3EJBEndpoint",
				CollecteService3.class);
		env.createProviderEndpoint("ServiceMAJSuiviEJBEndpoint",
				MajSuiviService.class);
		ProviderEndpoint docProvider = env.createProviderEndpoint(
				"ServiceDocNS4EJBEndpoint", DocNSVService.class);
		ProviderEndpoint notifProvider = env.createProviderEndpoint(
				"ServiceNotifRetardEJBEndpoint", NotifRetardService.class);
		// store the definition
		core.getModel().getRegistry().storeProcessDefinition(
				new URI("./src/test/resources/ProcessCollecte.bpel"), new ProcessContextDefinitionImpl(5));

		// send client message
		File xmlFile = new File("./src/test/resources/ProcessRequest.xml");
		BPELExternalMessage message = MockServiceBuilder.buildEmptyMessage(
				"http://process.collecte.architecture.cnav.fr/",
				"ProcessCollecte", "ProcessCollecteEndpoint", xmlFile,
				"ProcessCollecteRequestMessage");
		client.send(message);

		while (docProvider.getService().getOperations().get(0)
				.getSuccessfulInvocation() == 0
				&& notifProvider.getService().getOperations().get(0)
						.getSuccessfulInvocation() == 0) {
			// do nothing
			Thread.sleep(200);
			// System.out.println("Wait the end process");

			if (client.getException() != null) {
				throw client.getException();
			}
		}
		if (explorer) {
			Thread.sleep(10000000);
		}
	}

}
