package org.jscookie.test.integration.encoding;

import java.net.URL;
import java.util.concurrent.TimeUnit;

import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.container.test.api.RunAsClient;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.arquillian.test.api.ArquillianResource;
import org.jboss.shrinkwrap.api.Archive;
import org.jboss.shrinkwrap.api.Filters;
import org.jboss.shrinkwrap.api.GenericArchive;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.importer.ExplodedImporter;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.jscookie.test.integration.qunit.QUnitPageObject;
import org.jscookie.test.integration.qunit.QUnitResults;
import org.jscookie.test.integration.test.utils.Debug;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
import org.openqa.selenium.support.PageFactory;

import com.gargoylesoftware.htmlunit.BrowserVersion;

@RunWith( Arquillian.class )
public class CookiesEncodingIT {
	private static Debug debug = Debug.FALSE;

	@Deployment
	public static Archive<?> createDeployment() {
		boolean RECURSIVE_TRUE = true;

		GenericArchive qunitFiles = ShrinkWrap.create( GenericArchive.class )
			.as( ExplodedImporter.class )
			.importDirectory( "bower_components/js-cookie/" )
			.as( GenericArchive.class );

		WebArchive war = ShrinkWrap.create( WebArchive.class )
			.addPackages( RECURSIVE_TRUE, "org.jscookie.test.integration" )
			.merge( qunitFiles, "/", Filters.includeAll() );

		System.out.println( " ----- LOGGING THE FILES ADDED TO JBOSS" );
		System.out.println( war.toString( true ) );
		System.out.println( " ----- END OF LOGGING THE FILES ADDED TO JBOSS" );

		return war;
	}

	@RunAsClient
	@Test
	public void read_qunit_test( @ArquillianResource URL baseURL ) {
		WebDriver driver = createDriver();
		driver.manage().timeouts().implicitlyWait( 20, TimeUnit.SECONDS );

		EncodingPageObject encoding = PageFactory.initElements( driver, EncodingPageObject.class );
		encoding.navigateTo( baseURL );
		QUnitPageObject qunit = encoding.qunit();
		QUnitResults results = qunit.waitForTests( debug );

		int expectedPasses = results.getTotal();
		int actualPasses = results.getPassed();
		Assert.assertEquals( "should pass all tests", expectedPasses, actualPasses );

		dispose( driver );
	}

	private WebDriver createDriver() {
		if ( debug.is( true ) ) {
			return new FirefoxDriver();
		}

		HtmlUnitDriver driver = new HtmlUnitDriver( BrowserVersion.CHROME );
		driver.setJavascriptEnabled( true );

		return driver;
	}

	private void dispose( WebDriver driver ) {
		if ( debug.is( false ) ) {
			driver.quit();
		}
	}
}

@@ -42,16 +42,37 @@
 oding;%0A%0A
+import java.io.File;%0A
 import j
@@ -84,16 +84,16 @@
 et.URL;%0A
-
 import j
@@ -619,32 +619,86 @@
 pec.WebArchive;%0A
+import org.jboss.shrinkwrap.resolver.api.maven.Maven;%0A
 import org.jscoo
@@ -1279,12 +1279,11 @@
 bug.
-FALS
+TRU
 E;%0A%0A
@@ -1620,24 +1620,57 @@
 ive.class )%0A
+%09%09%09.addPackage( %22org.jscookie%22 )%0A
 %09%09%09.addPacka
@@ -1720,24 +1720,308 @@
 egration%22 )%0A
+%09%09%09.addAsLibraries(%0A%09%09%09%09Maven.resolver()%0A%09%09%09%09%09.loadPomFromFile( %22pom.xml%22 )%0A%09%09%09%09%09.resolve(%0A%09%09%09%09%09%09%22joda-time:joda-time%22,%0A%09%09%09%09%09%09%22org.eclipse.jdt:org.eclipse.jdt.annotation%22,%0A%09%09%09%09%09%09%22com.fasterxml.jackson.core:jackson-databind%22%0A%09%09%09%09%09)%0A%09%09%09%09%09.withTransitivity()%0A%09%09%09%09%09.as( File.class )%0A%09%09%09)%0A
 %09%09%09.merge( q

Add remaining classes to arquillian