/**
 * Copyright (C) 2012 Gist Labs, LLC. (http://gistlabs.com)
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */
package com.gistlabs.mechanize.integration.test;

import static com.gistlabs.mechanize.document.html.query.HtmlQueryBuilder.*;
import static org.junit.Assert.*;

import org.junit.Before;
import org.junit.Test;

import com.gistlabs.mechanize.MechanizeAgent;
import com.gistlabs.mechanize.document.Document;
import com.gistlabs.mechanize.document.html.HtmlDocument;
import com.gistlabs.mechanize.document.html.form.Form;
import com.gistlabs.mechanize.sequence.AbstractSequence;

/**
 * @author Martin Kersten<Martin.Kersten.mk@gmail.com>
 */
@SuppressWarnings("unused")
public class AmazonAddItemToCartAndUseASecondAgentToRemoveTheItemIT extends MozillaUserAgentTestClass {
	static final String firefoxUserAgent = "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.1) Gecko/20100122 firefox/3.6.1";
	MechanizeAgent agent;

	@Before
	public void setUp() throws Exception {
		agent = new MechanizeAgent().setUserAgent(null);
	}


	/**
	 * Adds the processor 'AMD FX 4100' to the shopping cart and using a second agent
	 * to remove it. This test also demonstrates how to copy session cookies (and other cookies)
	 * from one agent to another.
	 */
//	@Test // no longer working
	public void testAddingAndRemovingAItemToAndFromShoppingCartUsingTwoAgents() {
		AddItemToShoppingCartSequence addItemToShoppingCartSequence = new AddItemToShoppingCartSequence("B005UBNL0A");
		RemoveItemFromShoppingCartSequence removeItemFromShoppingCartSequence = new RemoveItemFromShoppingCartSequence();

		MechanizeAgent agentA = new MechanizeAgent();
		MechanizeAgent agentB = new MechanizeAgent();

		addItemToShoppingCartSequence.run(agentA);

		assertTrue("Ensure session cookie is used", agentA.cookies().getCount() > 0);
		agentB.cookies().addAllCloned(agentA.cookies().getAll());
		assertTrue("Ensure session cookies has been transfered", agentB.cookies().getCount() > 0);

		// FRAGILE TEST, no longer works
		//removeItemFromShoppingCartSequence.run(agentB);
		//assertTrue(removeItemFromShoppingCartSequence.wasShoppingCartEmpty());
	}

	private static class AddItemToShoppingCartSequence extends AbstractSequence {

		private final String productCode;

		public AddItemToShoppingCartSequence(final String productCodeToBuy) {
			this.productCode = productCodeToBuy;
		}

		@Override
		protected void run() {
			agent.get("http://www.amazon.com");
			agent.idle(200);

			Document amdProcessorPage = agent.get("http://www.amazon.com/gp/product/" + productCode + "/");
			agent.idle(250);
			Form form = amdProcessorPage.forms().get(byName("handleBuy"));
			agent.idle(200);

			form.submit();
			agent.idle(200);
		}
	}

	private static class RemoveItemFromShoppingCartSequence extends AbstractSequence {
		private boolean wasShoppingCartEmpty;

		@Override
		protected void run() {
			Document page = agent.get("http://www.amazon.com");
			agent.idle(200);
			Document cart = page.links().get(byId("nav-cart")).click();
			Form cartForm = cart.forms().get(byName("cartViewForm"));
			cartForm.get("quantity.C35RMYTCMZTEKE").setValue("0");
			agent.idle(200);
			HtmlDocument response = (HtmlDocument)cartForm.submit();
			wasShoppingCartEmpty = response.htmlElements().getRoot().getHtml().contains("Your Shopping Cart is empty.");
		}

		public boolean wasShoppingCartEmpty() {
			return wasShoppingCartEmpty;
		}
	}
}
@@ -1409,18 +1409,16 @@
 r.%0A%09 */%0A
-//
 %09@Test /
@@ -1437,16 +1437,16 @@
 working%0A
+
 %09public 
@@ -1516,16 +1516,18 @@
 nts() %7B%0A
+//
 %09%09AddIte
@@ -1631,16 +1631,18 @@
 NL0A%22);%0A
+//
 %09%09Remove
@@ -1749,17 +1749,21 @@
 ence();%0A
-%0A
+//%0A//
 %09%09Mechan
@@ -1802,16 +1802,18 @@
 gent();%0A
+//
 %09%09Mechan
@@ -1852,17 +1852,21 @@
 gent();%0A
-%0A
+//%0A//
 %09%09addIte
@@ -1902,17 +1902,21 @@
 gentA);%0A
-%0A
+//%0A//
 %09%09assert
@@ -1987,16 +1987,18 @@
 ) %3E 0);%0A
+//
 %09%09agentB
@@ -2049,16 +2049,18 @@
 All());%0A
+//
 %09%09assert
@@ -2144,17 +2144,21 @@
 ) %3E 0);%0A
-%0A
+//%0A//
 %09%09// FRA
@@ -2184,16 +2184,18 @@
 r works%0A
+//
 %09%09//remo
@@ -2238,16 +2238,18 @@
 gentB);%0A
+//
 %09%09//asse

clean amazon test