/**
 * 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.seinedemo;


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 Sebastien Andre - eBM WebSourcing
 */
public class TestPull {


    @Test
    public void testPull() 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);

		//// create client
		ClientEndpoint client = env.createClientEndpoint("client");

		// create providers
		ProviderEndpoint downloadThalesDatasetProvider = env.createProviderEndpoint("ThalesServiceInterfacePort", ThalesService.class);
		ProviderEndpoint transformThalesToAP214Provider = env.createProviderEndpoint("SeineServiceInterfacePort", TransformService.class);
		ProviderEndpoint uploadAP214DatasetProvider = env.createProviderEndpoint("AP214ServiceInterfacePort", AP214Service.class);
		ProviderEndpoint ap214AP239Provider = env.createProviderEndpoint("AP214AP239ServiceInterfacePort", AP214AP239Service.class);

		// store the definition
		core.getModel().getRegistry().storeProcessDefinition(new URI("./src/test/resources/SeineService.bpel"),
				new ProcessContextDefinitionImpl());

		// send client message
		BPELExternalMessage message = new BPELExternalMessageImpl();
		message.setService(new QName("http://seinedemo.ebmwebsourcing.com/Seine/", "SeineService"));
		message.setEndpoint("SeineServiceInterfacePort");
		message.setQName(new QName("http://seinedemo.ebmwebsourcing.com/Seine/", "PullRequest"));
		message.setMessage("" +
			      "<sein:PullRequest_AP239Envelope xmlns:sein=\"http://seinedemo.ebmwebsourcing.com/Seine/\" xmlns:ap2=\"http://AP239/\" xmlns:jaws=\"http://Entities/AP239/message/jaws\" xmlns:jaws1=\"http://Entities/AP239/envelope/jaws\" xmlns:jaws2=\"http://ValueObjects/AP239/jaws\">" +
			      "   <ap2:datasetId>45</ap2:datasetId>" +
			      "   <ap2:envelopeId>99</ap2:envelopeId>" +
			      "   <ap2:receivers>dassault</ap2:receivers>" +
			      "   <ap2:sender>thales</ap2:sender>" +
			      "</sein:PullRequest_AP239Envelope>"
		);
		ExternalMessage response = client.sendSync(message);


		// ASSERTION
		BPELExternalMessage expectedResponse = new BPELExternalMessageImpl();
		expectedResponse.setMessage("" +
				"<tns:PullResponse xmlns:tns=\"http://seinedemo.ebmwebsourcing.com/Seine/\">true</tns:PullResponse>"
			);

		System.out.println(response.toString());
		assertEquals(expectedResponse.toString(), response.toString());

		//assertEquals(2, sadaProvider.getService().getOperations().get(0).getInvocationNumber());
	}
}

