/*
 * JOnAS: Java(TM) Open Application Server
 * Copyright (C) 1999 Bull S.A.
 * Contact: jonas-team@objectweb.org
 *
 * 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 1any 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$
 * --------------------------------------------------------------------------
 */

package org.objectweb.jonas.examples.clients.petstore;

import junit.framework.TestSuite;

import org.objectweb.jonas.examples.util.JExampleTestCase;

import com.meterware.httpunit.TableCell;
import com.meterware.httpunit.WebForm;
import com.meterware.httpunit.WebLink;
import com.meterware.httpunit.WebResponse;
import com.meterware.httpunit.WebTable;

/**
 * Define a class to test the petstore example
 * @author Jerome Camilleri
 */
public class C_petstore extends JExampleTestCase {

    /**
     * URL of the petstore page
     */
    private static final String URL_BASEPETSTORE = "/petstore/populating.jsp?forcefully=true";
    private static final String URL_MAINPAGE = "/petstore/main.screen";
    private static final String URL_CART = "/petstore/cart.do";
    private static final String URL_SIGNON = "/petstore/signon_welcome.screen";

    /**
     * Main method
     * @param args the arguments
     */
    public static void main(String[] args) {

        String testtorun = null;
        // Get args
        for (int argn = 0; argn < args.length; argn++) {
            String sArg = args[argn];
            if (sArg.equals("-n")) {
                testtorun = args[++argn];
            }
        }

        if (testtorun == null) {
            junit.textui.TestRunner.run(suite());
        } else {
            junit.textui.TestRunner.run(new C_petstore(testtorun));
        }
    }

    /**
     * Get a new TestSuite for this class
     * @return a new TestSuite for this class
     */
    public static TestSuite suite() {
        return new TestSuite(C_petstore.class);
    }

    /**
     * Setup need for these tests
     * opc.ear, petstoreadmin.ear, petstore.ear, supplierPetstore is required
     * @throws Exception if it fails
     */
    protected void setUp() throws Exception {
        super.setUp();
        useEar("opc");
        useEar("petstoreadmin");
        useEar("petstore");
        useEar("supplier");
    }

    /**
     * Constructor with a specified name
     * @param s name
     */
    public C_petstore(String s) {
        super(s, URL_BASEPETSTORE);
    }

    /**
     * Test that the first page of Petstore contains links to 5 pets store into database
     * @throws Exception if an error occurs
     */
    public void testHomePage() throws Exception {
        WebResponse wr = wc.getResponse(getAbsoluteUrl(URL_MAINPAGE));
        WebTable[] webTables = wr.getTables();

        WebTable wTable = webTables[0];
        assertNotNull("There must be a table in the html page", wTable);

        int rows = wTable.getRowCount();
        int cols = wTable.getColumnCount();

        TableCell tableCell = wTable.getTableCell(1, 0);
        assertNotNull("There must be a cell in this row", tableCell);
        WebLink[] links = tableCell.getLinks();
        assertNotNull("There must have links in this cell", links);
        if (links.length == 5) {
            String lk1 = links[0].asText();
            String lk2 = links[1].asText();
            String lk3 = links[2].asText();
            String lk4 = links[3].asText();
            String lk5 = links[4].asText();
            if (!lk1.equalsIgnoreCase("Birds")) {
                fail("The first link must be egal to Birds and the value is " + lk1 + ".");
            } else if (!lk2.equalsIgnoreCase("Cats")) {
                fail("The first link must be egal to Cats and the value is " + lk2 + ".");
            } else if (!lk3.equalsIgnoreCase("Dogs")) {
                fail("The first link must be egal to Dogs and the value is " + lk3 + ".");
            } else if (!lk4.equalsIgnoreCase("Fish")) {
                fail("The first link must be egal to Fish and the value is " + lk4 + ".");
            } else if (!lk5.equalsIgnoreCase("Reptiles")) {
                fail("The first link must be egal to Reptiles and the value is " + lk5 + ".");
            }
        } else {
            fail("There must have 5 links into this cell but there are " + links.length + " links !");
        }
    }

    /**
     * Test that the first navigation for buy a reptile
     * @throws Exception if an error occurs
     */
    public void testFirstNavigation() throws Exception {
        WebResponse wr = wc.getResponse(getAbsoluteUrl(URL_MAINPAGE));
        WebTable[] webTables = wr.getTables();

        WebTable wTable = webTables[0];
        assertNotNull("There must be a table in the html page", wTable);

        int rows = wTable.getRowCount();
        int cols = wTable.getColumnCount();

        TableCell tableCell = wTable.getTableCell(1, 0);
        assertNotNull("There must be a cell in this row", tableCell);
        WebLink[] links = tableCell.getLinks();
        assertNotNull("There must have links in this cell", links);
        if (links.length == 5) {
            wr = links[4].click();
            webTables = wr.getTables();

            wTable = webTables[0];
            assertNotNull("There must be a table in the html page", wTable);

            rows = wTable.getRowCount();
            cols = wTable.getColumnCount();

            tableCell = wTable.getTableCell(1, 1);
            assertNotNull("There must be a cell in this row", tableCell);
            links = tableCell.getLinks();
            assertNotNull("There must have links in this cell", links);
            String lk1 = links[0].asText();
            String lk2 = links[1].asText();
            if (!lk1.equalsIgnoreCase("Iguana")) {
                fail("The first link must be egal to Iguana and the value is " + lk1 + ".");
            } else if (!lk2.equalsIgnoreCase("Rattlesnake")) {
                fail("The first link must be egal to Rattlesnake and the value is " + lk2 + ".");
            }

        } else {
            fail("There must have 5 links into this cell but there are " + links.length + " links !");
        }
    }

    /**
     * Test add cart one Rattleless Rattlesnake and Venomless
     * @throws Exception if an error occurs
     */
    public void testAddCartRattlelessVenomless() throws Exception {
        WebResponse wr = wc.getResponse(getAbsoluteUrl(URL_MAINPAGE));
        WebTable[] webTables = wr.getTables();

        WebTable wTable = webTables[0];
        assertNotNull("There must be a table in the html page", wTable);

        int rows = wTable.getRowCount();
        int cols = wTable.getColumnCount();

        TableCell tableCell = wTable.getTableCell(1, 0);
        assertNotNull("There must be a cell in this row", tableCell);
        WebLink[] links = tableCell.getLinks();
        assertNotNull("There must have links in this cell", links);
        if (links.length == 5) {
            wr = links[4].click(); // click into reptile
            webTables = wr.getTables();

            wTable = webTables[0];
            assertNotNull("There must be a table in the html page", wTable);

            rows = wTable.getRowCount();
            cols = wTable.getColumnCount();

            tableCell = wTable.getTableCell(1, 1);
            assertNotNull("There must be a cell in this row", tableCell);
            links = tableCell.getLinks();
            assertNotNull("There must have links in this cell", links);

            WebLink rattlesnake = links[1];
            wr = rattlesnake.click(); // click into  Rattlesnake

            webTables = wr.getTables();

            wTable = webTables[0];
            assertNotNull("There must be a table in the html page", wTable);

            rows = wTable.getRowCount();
            cols = wTable.getColumnCount();

            tableCell = wTable.getTableCell(1, 1);
            assertNotNull("There must be a cell in this row", tableCell);
            links = tableCell.getLinks();
            assertNotNull("There must have links in this cell", links);
            if (links.length != 4) {
                fail("There must have 4 links into this cell but there are " + links.length + " links !");
            }
            wr = links[3].click(); //  click into Rattleless Rattlesnake Add cart
            webTables = wr.getTables();
            WebForm[] webForms = wr.getForms();
            WebForm webForm = webForms[4];
            assertNotNull("There must be a form in the html page", webForm);
            WebResponse wFormRes = webForm.submit();

            wr = rattlesnake.click(); // click into  Rattlesnake

            webTables = wr.getTables();

            wTable = webTables[0];
            assertNotNull("There must be a table in the html page", wTable);

            rows = wTable.getRowCount();
            cols = wTable.getColumnCount();

            tableCell = wTable.getTableCell(1, 1);
            assertNotNull("There must be a cell in this row", tableCell);
            links = tableCell.getLinks();
            assertNotNull("There must have links in this cell", links);
            if (links.length != 4) {
                fail("There must have 4 links into this cell but there are " + links.length + " links !");
            }
            wr = links[1].click(); //  click into  Venomless Rattlesnake Add cart
            webTables = wr.getTables();
            webForms = wr.getForms();
            webForm = webForms[4];
            assertNotNull("There must be a form in the html page", webForm);
            wFormRes = webForm.submit();

            // Verify card contain 2 snakes
            wr = wc.getResponse(getAbsoluteUrl(URL_CART));
            webTables = wr.getTables();

            wTable = webTables[0];
            assertNotNull("There must be a table in the html page", wTable);

            rows = wTable.getRowCount();
            cols = wTable.getColumnCount();

            tableCell = wTable.getTableCell(1, 1);
            assertNotNull("There must be a cell in this row", tableCell);
            webTables = tableCell.getTables();

            wTable = webTables[0];
            assertNotNull("There must be a table in the html page", wTable);

            rows = wTable.getRowCount();
            cols = wTable.getColumnCount();

            tableCell = wTable.getTableCell(0, 0);

            webTables = tableCell.getTables();

            wTable = webTables[0];
            assertNotNull("There must be a table in the html page", wTable);

            rows = wTable.getRowCount();
            cols = wTable.getColumnCount();
            tableCell = wTable.getTableCell(2, 3);
            String total = tableCell.asText().trim();
            if (!total.equalsIgnoreCase("$37.00")) {
                fail("Total of the card must be $37.00 and it is " + total);
            }
        } else {
            fail("There must have 5 links into this cell but there are " + links.length + " links !");
        }
    }

}
