/**
 * 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 static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.jbi.messaging.MessagingException;

import org.junit.Ignore;
import org.junit.Test;
import org.ow2.petals.bc.filetransfer.util.FileTransferUtils;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;

/**
 * @author Adrien Ruffie - EBM WebSourcing
 */
public class FileTransferUtilsTest {

    /**
     * Test of the method createMessageOnBaseMsg(final File processedFile, final
     * String fileName,final String baseMsg, final String transferMode). Test
     * the attachment mode
     * 
     * @throws IOException
     */
    @Test
    public void testCreateMessageOnBaseMsgWithAttachementMode() {
        File processedFile;
        String fileName;
        Document doc;
        String transferMode = FileTransferConstants.ENUM_TRANSFER_MODE_ATTACHMENT;
        String baseMsg = "<tes:executeJobOnly xmlns:tes=\"http://petals.ow2.org/talend/TestAttachPetals/\">        <tes:contexts>            <tes:outputLocation>C:/Documents and Settings/vzurczak/Bureau/de/out.xml</tes:outputLocation>         </tes:contexts>         <tes:in-attachments>            <tes:inputLocation>               <tes:fileContent>$attachment</tes:fileContent>            </tes:inputLocation>         </tes:in-attachments>      </tes:executeJobOnly>";
        try {
            processedFile = File.createTempFile("test-file", "properties");
            processedFile.deleteOnExit();
            fileName = processedFile.getName();
            doc = FileTransferUtils.createMessageOnBaseMsg(processedFile, fileName, baseMsg,
                    transferMode);
            NodeList tesLst = doc.getElementsByTagNameNS(
                    "http://petals.ow2.org/talend/TestAttachPetals/", "*");
            assertTrue(
                    "Test if is namespace aware : Retrieve all element with the default namespace",
                    tesLst.getLength() != 0);
            NodeList xopLst = doc.getElementsByTagNameNS("http://www.w3.org/2004/08/xop/include",
                    "*");
            assertTrue("Test if is namespace aware : Retrieved the added xop:Include element",
                    xopLst.getLength() != 0);
        } catch (MessagingException e) {
            fail(e.getMessage());
        } catch (IOException e) {
            fail(e.getMessage());
        }
    }

    /**
     * Test of the method createMessageOnBaseMsg(final File processedFile, final
     * String fileName,final String baseMsg, final String transferMode). Test
     * the content mode
     */
    @Test
    public void testCreateMessageOnBaseMsgWithContentMode() {
        FileOutputStream fos = null;
        File processedFile;
        String transferMode = FileTransferConstants.ENUM_TRANSFER_MODE_CONTENT;
        String xmlContent = "<testns:elt1 xmlns:testns=\"http://petals.ow2.org/testNS/\">tutu</testns:elt1>";
        String baseMsg = "<tes:executeJobOnly xmlns:tes=\"http://petals.ow2.org/talend/TestAttachPetals/\">        <tes:contexts>            <tes:outputLocation>C:/Documents and Settings/vzurczak/Bureau/de/out.xml</tes:outputLocation>         </tes:contexts>         <tes:in-attachments>            <tes:inputLocation>               <tes:fileContent>$content</tes:fileContent>            </tes:inputLocation>         </tes:in-attachments>      </tes:executeJobOnly>";
        try {
            processedFile = File.createTempFile("test-file", "properties");
            processedFile.deleteOnExit();
            fos = new FileOutputStream(processedFile);
            String fileName = processedFile.getName();
            fos.write(xmlContent.getBytes());
            Document doc = FileTransferUtils.createMessageOnBaseMsg(processedFile, fileName,
                    baseMsg, transferMode);
            NodeList tesLst = doc.getElementsByTagNameNS(
                    "http://petals.ow2.org/talend/TestAttachPetals/", "*");
            assertTrue(
                    "Test if is namespace aware : Retrieve all element with the default namespace",
                    tesLst.getLength() != 0);
            NodeList includeDocLst = doc.getElementsByTagNameNS("http://petals.ow2.org/testNS/",
                    "*");
            assertTrue("Test if is namespace aware : Retrieved the added element",
                    includeDocLst.getLength() != 0);
        } catch (MessagingException e) {
            fail(e.getMessage());
        } catch (IOException e) {
            fail(e.getMessage());
        } finally {
            try {
                fos.close();
            } catch (IOException e) {
            }
        }
    }

    @Test
    @Ignore
    public void testCreateMessageOnDefaultMsg() {
        // fail("Not yet implemented");
    }

}
