/**
 * Copyright (c) 2011-2012 EBM WebSourcing, 2012-2024 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.assertNotEquals;

import java.io.File;
import java.io.IOException;
import java.util.logging.Logger;

import javax.jbi.messaging.MessagingException;

import org.apache.commons.io.FileUtils;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.ow2.petals.bc.filetransfer.FileTransferConstants.TransfertMode;
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 {

    private final Logger LOG = Logger.getLogger(FileTransferUtilsTest.class.getName());

    @Rule
    final public TemporaryFolder tempFolder = new TemporaryFolder();

    /**
     * Test of the method {@link FileTransferUtils#createMessageOnBaseMsg(File, String, String, TransfertMode, Logger)}, using
     * the mode 'attachment'.
     */
    @Test
    public void createMessageOnBaseMsgWithAttachementMode() throws IOException, MessagingException {

        final 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>";

        // Create the file whose content will be used to create a message
        final File processedFile = this.tempFolder.newFile("test-file.properties");

        final Document doc = FileTransferUtils.createMessageOnBaseMsg(processedFile, processedFile.getName(), baseMsg,
                TransfertMode.ATTACHMENT, LOG);
        final NodeList tesLst = doc.getElementsByTagNameNS("http://petals.ow2.org/talend/TestAttachPetals/", "*");
        assertNotEquals("Test if is namespace aware : Retrieve all element with the default namespace", 0,
                tesLst.getLength());
        final NodeList xopLst = doc.getElementsByTagNameNS("http://www.w3.org/2004/08/xop/include", "*");
        assertNotEquals("Test if is namespace aware : Retrieved the added xop:Include element", 0, xopLst.getLength());
    }

    /**
     * Test of the method {@link FileTransferUtils#createMessageOnBaseMsg(File, String, String, TransfertMode, Logger)}, using
     * the mode 'content'.
     */
    @Test
    public void createMessageOnBaseMsgWithContentMode() throws IOException, MessagingException {

        final 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>";

        // Create the file whose content will be used to create a message
        final File processedFile = this.tempFolder.newFile("test-file.properties");
        final String xmlContent = "<testns:elt1 xmlns:testns=\"http://petals.ow2.org/testNS/\">tutu</testns:elt1>";
        FileUtils.write(processedFile, xmlContent);

        final Document doc = FileTransferUtils.createMessageOnBaseMsg(processedFile, processedFile.getName(), baseMsg,
                TransfertMode.CONTENT, LOG);
        final NodeList tesLst = doc.getElementsByTagNameNS("http://petals.ow2.org/talend/TestAttachPetals/", "*");
        assertNotEquals("Test if is namespace aware : Retrieve all element with the default namespace", 0,
                tesLst.getLength());
        final NodeList includeDocLst = doc.getElementsByTagNameNS("http://petals.ow2.org/testNS/", "elt1");
        assertNotEquals("Test if is namespace aware : Retrieved the added element", 0, includeDocLst.getLength());
    }

    /**
     * Test of the method {@link FileTransferUtils#createMessageOnDefaultMsg(File, String, TransfertMode, Logger)}, using the
     * mode 'attachment'.
     */
    @Test
    public void createMessageOnDefaultMsgWithAttachementMode() throws IOException, MessagingException {

        // Create the file whose content will be used to create a message
        final File processedFile = this.tempFolder.newFile("test-file.properties");

        final Document doc = FileTransferUtils.createMessageOnDefaultMsg(processedFile, processedFile.getName(),
                TransfertMode.ATTACHMENT, LOG);
        final NodeList tesLst = doc.getElementsByTagNameNS(FileTransferConstants.FILETRANSFER_SERVICE_NS,
                "attachment");
        assertNotEquals("Test if is namespace aware : Retrieve all element with the default namespace", 0,
                tesLst.getLength());
        final NodeList xopLst = doc.getElementsByTagNameNS("http://www.w3.org/2004/08/xop/include", "*");
        assertNotEquals("Test if is namespace aware : Retrieved the added xop:Include element", 0, xopLst.getLength());
    }

    /**
     * Test of the method {@link FileTransferUtils#createMessageOnDefaultMsg(File, String, TransfertMode, Logger)}, using the
     * mode 'content'.
     */
    @Test
    public void createMessageOnDefaultMsgWithContentMode() throws IOException, MessagingException {

        // Create the file whose content will be used to create a message
        final File processedFile = this.tempFolder.newFile("test-file.properties");
        final String xmlContent = "<testns:elt1 xmlns:testns=\"http://petals.ow2.org/testNS/\">tutu</testns:elt1>";
        FileUtils.write(processedFile, xmlContent);

        final Document doc = FileTransferUtils.createMessageOnDefaultMsg(processedFile, processedFile.getName(),
                TransfertMode.CONTENT, LOG);
        final NodeList tesLst = doc.getElementsByTagNameNS("http://petals.ow2.org/testNS/", "elt1");
        assertNotEquals("Test if is namespace aware : Retrieve all element with the default namespace", 0,
                tesLst.getLength());
    }

}
