package com.sun.dft.glassfish.cdi; import com.meterware.httpunit.GetMethodWebRequest; import com.meterware.httpunit.SubmitButton; import com.meterware.httpunit.WebConversation; import com.meterware.httpunit.WebForm; import com.meterware.httpunit.WebRequest; import com.meterware.httpunit.WebResponse; import com.sun.dft.glassfish.config.BaseTestCase; import com.sun.dft.glassfish.helpers.HttpMessageLogger; import com.sun.dft.glassfish.utils.TestUtils; import org.testng.annotations.AfterTest; import org.testng.annotations.BeforeTest; import org.testng.annotations.Test; /** * * @author sony */ public class ShoppingCartTest extends BaseTestCase { @BeforeTest public void setup() { asadminHelper.createJDBCConnectionPoolAndResource(TestUtils.getDefaultClusterName(), "jdbc/st-pool", "jdbc/st-ds"); asadminHelper.deploy("cdi/ShoppingCart.war", TestUtils.getDefaultClusterName()); } @Test public void testShoppingCart() { try { WebConversation wc = new WebConversation(); int httpPort = TestUtils.getInstanceHttpPort(1); String host = TestUtils.getInstanceHost(1); // Index page WebRequest req = new GetMethodWebRequest("http://" + host + ":" + httpPort + "/ShoppingCart"); WebResponse resp = wc.getResponse(req); HttpMessageLogger.logHttpRequestResponse(wc, req, resp); // StartShopping WebForm initialForm = resp.getFormWithName("fmrIndex"); req = initialForm.getRequest(); SubmitButton startShopping = initialForm.getSubmitButtonWithID("btnStartShopping"); req = initialForm.getRequest(startShopping); resp = wc.getResponse(req); HttpMessageLogger.logHttpRequestResponse(wc, req, resp); // Add Banana to cart WebForm shoppingForm1 = resp.getFormWithName("searchForm"); shoppingForm1.setParameter("name", "Banana"); SubmitButton addToCartButton = shoppingForm1.getSubmitButtonWithID("btnAddCart"); req = shoppingForm1.getRequest(addToCartButton); resp = wc.getResponse(req); HttpMessageLogger.logHttpRequestResponse(wc, req, resp); // Add Mango to cart WebForm shoppingForm2 = resp.getFormWithName("searchForm"); shoppingForm2.setParameter("name", "Mango"); addToCartButton = shoppingForm2.getSubmitButtonWithID("btnAddCart"); req = shoppingForm2.getRequest(addToCartButton); resp = wc.getResponse(req); HttpMessageLogger.logHttpRequestResponse(wc, req, resp); //Introduce failure here and sleep from some time. asadminHelper.stopInstance("instance101"); TestUtils.sleepSeconds(10); //Now call the application as if nothing happenned.Since this is multi instance setup with HA and LB Configured request goes to another instance of running glassfish. // Checkout shopping cart WebForm shoppingForm3 = resp.getFormWithName("searchForm"); SubmitButton checkoutButton = shoppingForm3.getSubmitButtonWithID("btnCheckout"); checkPoint("After instance restart checkout button name is :"+checkoutButton.getID()); req = shoppingForm3.getRequest(checkoutButton); resp = wc.getResponse(req); HttpMessageLogger.logHttpRequestResponse(wc, req, resp); // Credit card page WebForm checkoutForm = resp.getFormWithName("fmCheckout"); checkoutForm.setParameter("fmCheckout:radioPayment", "Visa"); checkoutForm.setParameter("fmCheckout:txtAcctNumber", "1111-2222-3333-4444"); SubmitButton payButton = checkoutForm.getSubmitButtonWithID("fmCheckout:btnPay"); req = checkoutForm.getRequest(payButton); resp = wc.getResponse(req); HttpMessageLogger.logHttpRequestResponse(wc, req, resp); } catch (Exception ex) { ex.printStackTrace(); throw new RuntimeException(ex); } } @AfterTest public void unsetup() { //asadminHelper.undeploy("ShoppingCart", TestUtils.getDefaultClusterName()); //asadminHelper.deleteJDBCConnectionPoolAndResource(TestUtils.getDefaultClusterName(), "jdbc/st-pool", "jdbc/st-ds"); } }