/**
 * Copyright (c) 2011-2012 EBM WebSourcing, 2012-2013 Linagora
 * 
 * This program/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 program/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 program/library; If not, see <http://www.gnu.org/licenses/>
 * for the GNU Lesser General Public License version 2.1.
 */
package org.ow2.petals.bc.filetransfer;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.jbi.messaging.ExchangeStatus;
import javax.xml.namespace.QName;

import org.junit.BeforeClass;
import org.ow2.easywsdl.wsdl.api.abstractItf.AbsItfOperation;
import org.ow2.petals.commons.log.FlowAttributes;
import org.ow2.petals.component.api.Component;
import org.ow2.petals.component.api.ComponentConfiguration;
import org.ow2.petals.component.api.Message;
import org.ow2.petals.component.api.ServiceConfiguration;
import org.ow2.petals.component.api.ServiceConfiguration.ServiceType;

import com.ebmwebsourcing.easycommons.io.FileSystemHelper;
import com.ebmwebsourcing.jbi.adaptor.impl.AbstractJBIComponentTest;
import com.ebmwebsourcing.jbi.adaptor.impl.ComponentType;
import com.ebmwebsourcing.jbi.adaptor.impl.WrappedComponent;
import com.ebmwebsourcing.jbi.adaptor.impl.WrappedRequestToProviderMessage;
import com.ebmwebsourcing.jbi.adaptor.impl.WrappedStatusFromConsumerMessage;

/**
 * @author Marc Jambert - EBM WebSourcing
 */
public class FileTransferComponentTest extends AbstractJBIComponentTest {

	private static final QName TEST_OPERATION = new QName(
			"http://testProvideService", "get");
	
    private static File folder;

    @BeforeClass
    public static void beforeClass() throws IOException {
        folder = FileSystemHelper.createTempDir();
    }

    @Override
    protected Component createComponentUnderTest(ComponentConfiguration componentConfiguration) {
        return new WrappedComponent(ComponentType.BC_FILETRANSFER, componentConfiguration);
    }

    @Override
    protected Message createMessageToProcessAsProvider(
            ServiceConfiguration testProvideServiceConfiguration) throws Exception {
        String payload = "<?xml version='1.0' encoding='UTF-8'?>\n"
                + "<tns:get xmlns:tns='http://testProvideService'>\n"
                + "  <tns:filename>testFilename.txt</tns:filename>\n" + "</tns:get>";

		return new WrappedRequestToProviderMessage(testProvideServiceConfiguration,
				TEST_OPERATION,
				AbsItfOperation.MEPPatternConstants.IN_OUT.value(),
				createPayloadInputStream(payload), new FlowAttributes(
						"testFlowInstanceId", "testFlowStepId"));
    }

    @Override
    protected Message createTestStatusMessage(
            ServiceConfiguration testServiceConfiguration, ExchangeStatus status) throws Exception {
        String payload = "<?xml version='1.0' encoding='UTF-8'?>\n"
                + "<tns:get xmlns:tns='http://testProvideService'>\n"
                + "  <tns:filename>testFilename.txt</tns:filename>\n" + "</tns:get>";

		return new WrappedStatusFromConsumerMessage(testServiceConfiguration,
				TEST_OPERATION,
				AbsItfOperation.MEPPatternConstants.IN_OUT.value(),
				createPayloadInputStream(payload), new FlowAttributes(
						"testFlowInstanceId", "testFlowStepId"), status);
    }

    @Override
    protected ServiceConfiguration createTestProvideServiceConfiguration()
            throws Exception {
        ServiceConfiguration testProvideServiceConguration = createTestProvideServiceBaseConfiguration();
        testProvideServiceConguration.setParameter("{"
                + FileTransferConstants.FILETRANSFER_SERVICE_NS + "}"
                + FileTransferConstants.PARAM_FOLDER, FileSystemHelper.createTempDir()
                .getAbsolutePath());
        return testProvideServiceConguration;
    }

    @Override
    protected ServiceConfiguration createTestConsumeServiceConfiguration() throws Exception {
        ServiceConfiguration testProvideServiceConguration = new ServiceConfiguration(
                "testServiceConsume", new QName("http://testProvideService",
                        "testProvideInterfaceName"), new QName("http://testProvideService",
                        "testServiceName"), "testEndpointName", ServiceType.CONSUME);
        testProvideServiceConguration.setParameter("{"
                + FileTransferConstants.FILETRANSFER_SERVICE_NS + "}"
                + FileTransferConstants.PARAM_FOLDER, folder.getAbsolutePath());

        return testProvideServiceConguration;
    }

    @Override
    protected Map<String, String> generateExternalEvent() throws Exception {
        File tempFile = createXmlFile();
        File filePathToPoll = new File(folder, tempFile.getName());
        tempFile.renameTo(filePathToPoll);
        Map<String, String> expectedParams = new HashMap<String, String>();
        expectedParams.put(FileTransferConsumeFlowStepBeginLogData.FILEPATH_KEY, filePathToPoll.getAbsolutePath());
        return expectedParams;
    }

    private File createXmlFile() throws Exception {
        File tempFile = File.createTempFile("filetransferTest", ".xml");
        FileOutputStream outputStream = new FileOutputStream(tempFile);
        String xmlText = "<text>vive le foot</text>";
        outputStream.write(xmlText.getBytes());
        return tempFile;
    }
}
