/**
 * 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.mail;


import static org.junit.Assert.*;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

import javax.xml.namespace.QName;

import org.jdom.JDOMException;
import org.junit.Test;

import com.ebmwebsourcing.easybpel.model.bpel.api.BPELException;
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.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 TestMailProcess {


    @Test
	public void testMail() throws CoreException, URISyntaxException, BPELException, InterruptedException, JDOMException, IOException  {
		final boolean explorer = false;

		// create execution environment
		ExecutionEnvironmentTest env = ExecutionEnvironmentTestFactory.newInstance().newEnvironment("TestEnvironnement", explorer);

		//// create maestro
		final Core core = BPELFactoryImpl.getInstance().newBPELEngine(DefaultBPELMessageConverter.get(), 1, MemoryReceiverImpl.class, 1, TestSenderImpl.class);
		env.setCore(core);

		// store the definition
		core.getModel().getRegistry().storeProcessDefinition(new URI("./src/test/resources/process.bpel"),
				new ProcessContextDefinitionImpl());
		
		//// create providers
		ProviderEndpoint AnnuProvider = env.createProviderEndpoint("ServicesCSIAEndpoint", AnnuService.class);
		ProviderEndpoint SigabaseProvider = env.createProviderEndpoint("ServicesSigabaseServiceEndpoint", SigabaseService.class);
		ProviderEndpoint SendMailProvider = env.createProviderEndpoint("SendMailtestEndpoint", SendMailService.class);

		//// create client 1
		ClientEndpoint client1 = env.createClientEndpoint("client1");
		
		// send client message
		BPELExternalMessage message1 = new BPELExternalMessageImpl();
		message1.setService(new QName("acme.fr", "NotificationAffectationService"));
		message1.setEndpoint("NotificationAffectationServiceEndpoint");
		message1.setQName(new QName("acme.fr", "NotificationAffectationItfRequestMessage"));
		message1.setMessage("" +
				"<acme:process xmlns:acme=\"acme.fr\">" + 
				"         <payload>" + 
				"            <acme:nid>312</acme:nid>" + 
				"         </payload>" + 
				"</acme:process>" 
		);
		ExternalMessage response1 = client1.sendSync(message1);


		// ASSERTION
		BPELExternalMessage expectedResponse1 = new BPELExternalMessageImpl();
		expectedResponse1.setMessage("" +
				"<tns:processResponse xmlns:tns=\"acme.fr\">" +
				"  <payload xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:type=\"tns:NotificationAffectationItfResponse\">" +
				"    <tns:result xsi:type=\"string\">Process KO: Error to insert Affectation =&gt; the size of array equals 0</tns:result>" +
				"  </payload>" +
				"</tns:processResponse>");


		assertEquals(expectedResponse1.toString(), response1.toString());

		// unstore the definition
		core.getModel().getRegistry().unStoreProcessDefinition(new URI("./src/test/resources/process.bpel"), true);

		
//		//// create client 2
//		ClientEndpoint client2 = env.createClientEndpoint("client2");
//		
//		// send client message
//		BPELExternalMessage message2 = new BPELExternalMessageImpl();
//		message2.setService(new QName("acme.fr", "NotificationAffectationService"));
//		message2.setEndpoint("NotificationAffectationServiceEndpoint");
//		message2.setQName(new QName("acme.fr", "NotificationAffectationItfRequestMessage"));
//		message2.setMessage("" +
//				"<acme:process xmlns:acme=\"acme.fr\">" + 
//				"         <payload>" + 
//				"            <acme:nid>313</acme:nid>" + 
//				"         </payload>" + 
//				"</acme:process>" 
//		);
//		ExternalMessage response2 = client2.sendSync(message2);
//
//
//		// ASSERTION
//		BPELExternalMessage expectedResponse2 = new BPELExternalMessageImpl();
//		expectedResponse2.setMessage("" +
//				"<tns:NotificationAffectationItfResponseMessage xmlns:tns=\"acme.fr\">" +
//				"  <payload type=\"tns:NotificationAffectationItfResponse\">" +
//				"    <tns:result type=\"string\">Process OK: a mail has been sent to nicolas.salatge@ebmwebsourcing.com and to nicolas.salatge@ebmwebsourcing.com</tns:result>" +
//				"  </payload>" +
//				"</tns:NotificationAffectationItfResponseMessage>");
//
//
//		assertEquals(expectedResponse2.toString(), response2.toString());

		
		if(explorer) {
			Thread.sleep(10000000);
		}
	}

//	public void testSendMailPerf() throws MaestroException, URISyntaxException, BPELException, InterruptedException, JDOMException, IOException  {
//	final boolean explorer = true;
//
//
//	// create execution environment
//	ExecutionEnvironmentTest env = ExecutionEnvironmentTestFactory.newInstance().newEnvironment("TestEnvironnement", explorer);
//
//	//// create maestro
//	final Maestro maestro = BPELFactory.newInstance().newBPELEngine(DefaultBPELMessageConverter.get(), 1, MemoryReceiverImpl.class, 1, BPELTestSenderImpl.class);
//	env.setMaestro(maestro);
//
//	// store the definition
//	maestro.getModel().getRegistry().storeProcessDefinition(new URI(Constants.TEST_PATH_USECASE + "mail/process.bpel"));
//	
//	//// create providers
//	ProviderEndpoint AnnuProvider = env.createProviderEndpoint("ServicesCSIAEndpoint", AnnuService.class);
//	ProviderEndpoint SigabaseProvider = env.createProviderEndpoint("ServicesSigabaseServiceEndpoint", SigabaseService.class);
//	ProviderEndpoint SendMailProvider = env.createProviderEndpoint("SendMailtestEndpoint", SendMailService.class);
//
//
//	
//
//	// create client message
//	BPELExternalMessage message1 = new BPELExternalMessageImpl();
//	message1.setService(new QName("acme.fr", "NotificationAffectationService"));
//	message1.setEndpoint("NotificationAffectationServiceEndpoint");
//	message1.setQName(new QName("acme.fr", "NotificationAffectationItfRequestMessage"));
//	message1.setMessage("" +
//			"<acme:process xmlns:acme=\"acme.fr\">" + 
//			"         <payload>" + 
//			"            <acme:nid>312</acme:nid>" + 
//			"         </payload>" + 
//			"</acme:process>" 
//	);
//
//	// create client
//	int nbClient = 5;
//	List<ThreadClient> clients = new ArrayList<ThreadClient>();
//	for(int i = 0; i < nbClient; i++) {
//		ClientEndpoint client = env.createClientEndpoint("client" + i);
//		clients.add(new ThreadClient(client, message1));
//	}
//
//	// start all clients
//	int i = 0;
//	while(i <= 2) {
//		clients.get(i).start();
//		i++;
//	}
//	
//	Thread.sleep(3000);
//	
//	clients.get(3).start();
//	clients.get(4).start();
//	
//	Thread.sleep(5000);
//	// wait the end of all client
////	boolean allfinished = false;
////	while(!allfinished) {
////		allfinished = true;
////		for(ThreadClient client: clients) {
////			if(!client.isFinished()) {
////				allfinished = false;
////			}
////		}
////		System.out.println("wait that all finish");
////	}
//
//	
//	// assert for all clients
//	BPELExternalMessage expectedResponse1 = new BPELExternalMessageImpl();
//	expectedResponse1.setMessage("" +
//			"<tns:NotificationAffectationItfResponseMessage xmlns:tns=\"acme.fr\">" +
//			"  <payload xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:type=\"tns:NotificationAffectationItfResponse\">" +
//			"    <tns:result xsi:type=\"string\">Process KO: BPELError to insert Affectation =&gt; the size of array equals 0</tns:result>" +
//			"  </payload>" +
//			"</tns:NotificationAffectationItfResponseMessage>");
//
//	for(ThreadClient client: clients) {
//		assertEquals(expectedResponse1.toString(), client.getResponse().toString());
//	}
//
//	// unstore the definition
//	maestro.getModel().getRegistry().unStoreProcessDefinition(new URI(Constants.TEST_PATH_USECASE + "mail/process.bpel"));
//
//
//
//	if(explorer) {
//		Thread.sleep(10000000);
//	}
//
//
//}
	
}

