/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.adullact.iparapheur.it;

import com.thoughtworks.selenium.DefaultSelenium;
import com.thoughtworks.selenium.Selenium;
import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import junit.framework.TestCase;
import org.apache.log4j.Logger;

/**
 *
 * @author vbarousse
 */
public abstract class ParapheurIntegrationTestCase extends TestCase {

    private static final Logger logger = Logger.getLogger(ParapheurIntegrationTestCase.class);

    public static final String CONTEXT_PATH = "/parapheur";

    protected Selenium selenium;

    @Override
    public void setUp() throws Exception {
        selenium = new DefaultSelenium("localhost", 4444, "*chrome", "http://localhost:9090/");
        selenium.start();

        init();
    }

    @Override
    public void tearDown() throws Exception {
        selenium.stop();
    }

    /**
     * Creates a temporary file, and returns a reference to it.
     *
     * The created file is an empty PDF file.
     * @return Reference to the created temporary file.
     */
    protected File getTempFile() {
        return getTempFile("/samples/document.pdf");
    }

    /**
     * Copies the specified resource in a temporary file, and returns a reference
     * to that file.
     *
     * @param resource The resource to copy
     * @return Created temporary file
     */
    protected File getTempFile(String resource) {
        try {
            File file = File.createTempFile("parapheur", ".tmp");
            FileOutputStream out = new FileOutputStream(file);
            InputStream in = this.getClass().getResourceAsStream(resource);
            BufferedInputStream bin = new BufferedInputStream(in);
            int readed;
            byte[] buffer = new byte[64 * 1024];
            while ((readed = bin.read(buffer)) >= 0) {
                out.write(buffer, 0, readed);
            }
            out.close();
            bin.close();
            in.close();
            return file;
        } catch (IOException ex) {
            logger.error(ex);
        }
        return null;
    }

    protected byte[] getResourceContent(String resource) {
        try {
            InputStream in = this.getClass().getResourceAsStream(resource);
            byte[] buffer = new byte[4096];
            int readed;
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            while ((readed = in.read(buffer)) != -1) {
                out.write(buffer, 0, readed);
            }
            return out.toByteArray();
        } catch (IOException ex) {
            logger.error(ex);
        }
        return new byte[0];
    }

    private void init() {
        selenium.open("/parapheur/faces/jsp/parapheur/parapheurs.jsp");
        selenium.type("loginForm:user-name", "admin");
        selenium.type("loginForm:user-password", "admin");
        selenium.click("loginForm:submit");
        selenium.waitForPageToLoad("30000");
        selenium.open("/parapheur/wcs/parapheur/typesDossier/init");
        selenium.waitForPageToLoad("30000");
        selenium.open("/parapheur/faces/jsp/parapheur/parapheurs.jsp");
        selenium.waitForPageToLoad("30000");
        selenium.click("link=Console d'administration");
        selenium.waitForPageToLoad("30000");
        selenium.click("_idJsp6");
        selenium.waitForPageToLoad("30000");
        selenium.click("users:_idJsp11");
        selenium.waitForPageToLoad("30000");
        if (!selenium.isTextPresent("SampleUser")) {
            selenium.click("_idJsp6");
            selenium.waitForPageToLoad("30000");
            selenium.type("person-props:firstName", "SampleUser");
            selenium.type("person-props:lastName", "SampleUser");
            selenium.type("person-props:email", "SampleUser@example.com");
            selenium.click("person-props:next-button");
            selenium.waitForPageToLoad("30000");
            selenium.type("user-props:userName", "SampleUser");
            selenium.type("user-props:password", "secret");
            selenium.type("user-props:confirm", "secret");
            selenium.fireEvent("user-props:confirm", "keyup");
            selenium.click("user-props:next-button");
            selenium.waitForPageToLoad("30000");
            selenium.click("person-props:_idJsp16");
            selenium.waitForPageToLoad("30000");
        }
        selenium.open("/parapheur/faces/jsp/parapheur/parapheurs.jsp");
        selenium.waitForPageToLoad("30000");
        selenium.click("link=Console d'administration");
        selenium.waitForPageToLoad("30000");
        selenium.click("_idJsp6");
        selenium.waitForPageToLoad("30000");
        selenium.click("users:_idJsp11");
        selenium.waitForPageToLoad("30000");
        if (!selenium.isTextPresent("SampleSecretaire")) {
            selenium.click("_idJsp6");
            selenium.waitForPageToLoad("30000");
            selenium.type("person-props:firstName", "SampleSecretaire");
            selenium.type("person-props:lastName", "SampleSecretaire");
            selenium.type("person-props:email", "SampleSecretaire@example.com");
            selenium.click("person-props:next-button");
            selenium.waitForPageToLoad("30000");
            selenium.type("user-props:userName", "SampleSecretaire");
            selenium.type("user-props:password", "secret");
            selenium.type("user-props:confirm", "secret");
            selenium.fireEvent("user-props:confirm", "keyup");
            selenium.click("user-props:next-button");
            selenium.waitForPageToLoad("30000");
            selenium.click("person-props:_idJsp16");
            selenium.waitForPageToLoad("30000");
        }
        selenium.open("/parapheur/faces/jsp/parapheur/parapheurs.jsp");
        selenium.waitForPageToLoad("30000");
        selenium.click("link=Console d'administration");
        selenium.waitForPageToLoad("30000");
        selenium.click("_idJsp8");
        selenium.waitForPageToLoad("30000");
        if (!selenium.isTextPresent("ParapheurAdmin")) {
            selenium.click("link=Créer un nouveau parapheur");
            selenium.waitForPageToLoad("30000");
            selenium.type("dialog:dialog-body:nom", "ParapheurAdmin");
            selenium.type("dialog:dialog-body:titre", "ParapheurAdmin");
            selenium.type("dialog:dialog-body:description", "ParapheurAdmin");
            selenium.fireEvent("dialog:dialog-body:description", "keyup");
            selenium.select("dialog:dialog-body:comboPersonnes", "label=Administrator");
            selenium.click("dialog:finish-button");
            selenium.waitForPageToLoad("30000");
        }
        if (!selenium.isTextPresent("ParapheurUser")) {
            selenium.click("link=Créer un nouveau parapheur");
            selenium.waitForPageToLoad("30000");
            selenium.type("dialog:dialog-body:nom", "ParapheurUser");
            selenium.type("dialog:dialog-body:titre", "ParapheurUser");
            selenium.type("dialog:dialog-body:description", "ParapheurUser");
            selenium.fireEvent("dialog:dialog-body:description", "keyup");
            selenium.select("dialog:dialog-body:comboPersonnes", "label=SampleUser SampleUser");
            selenium.addSelection("dialog:dialog-body:pickerSecretariat", "label=SampleSecretaire SampleSecretaire");
            selenium.click("dialog:finish-button");
            selenium.waitForPageToLoad("30000");
        }
        selenium.open("/parapheur/wcs/parapheur/circuits-3/list");
        selenium.waitForPageToLoad("30000");
        selenium.click("//img[@alt='Nouveau circuit']");
        selenium.waitForPageToLoad("30000");
        selenium.type("circuit_nom", "admin-user-admin");
        selenium.click("circuit_public");
        selenium.click("//img[@alt='Nouvelle étape']");
        selenium.click("//img[@alt='Nouvelle étape']");
        selenium.select("etape1_objet", "label=ParapheurAdmin");
        selenium.select("etape2_objet", "label=ParapheurUser");
        selenium.select("etape2_action", "label=Signature");
        selenium.select("etape3_objet", "label=ParapheurAdmin");
        selenium.select("etape3_action", "label=Archivage");
        selenium.click("//input[@value='Valider']");
        selenium.waitForPageToLoad("30000");
        selenium.open("/parapheur/wcs/parapheur/typesDossier/list");
        selenium.click("//img[@alt=\"Supprimer le Type Technique 'Par Defaut'\"]");
        assertTrue(selenium.getConfirmation().matches("^Etes-vous sûr de vouloir supprimer 'Par Defaut'[\\s\\S]\nCela supprimera aussi les sous-types associés\\.$"));
        selenium.waitForPageToLoad("30000");
        selenium.type("add-typetechnique:newID", "defaut");
        selenium.type("newDesc", "defaut");
        selenium.select("add-typetechnique:newTdTNom", "label=pas de TdT");
        selenium.click("//img[@alt='Ajouter un Type Technique']");
        selenium.waitForPageToLoad("30000");
        selenium.click("//img[@alt='Editer les Sous-Types de defaut']");
        selenium.waitForPageToLoad("30000");
        selenium.type("add-typetechnique:newssID", "defaut");
        selenium.type("newDesc", "defaut");
        selenium.click("//img[@alt='Ajouter un Sous-Type']");
        selenium.waitForPageToLoad("30000");
        selenium.open("/parapheur/faces/jsp/parapheur/parapheurs.jsp");
        selenium.waitForPageToLoad("30000");
        selenium.click("logout");
        selenium.waitForPageToLoad("30000");
    }

}
