package application.rest;

import static org.junit.Assert.*;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.InputStream;
import java.net.URL;
import java.util.Enumeration;
import java.util.Properties;

import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.shrinkwrap.resolver.api.maven.Maven;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.spec.WebArchive;

@RunWith(Arquillian.class)
public class TransactionRestEndpointIT {
	
	@Deployment
	public static WebArchive createDeploymentY() {
		File[] mavenFiles = Maven.resolver().loadPomFromFile("pom.xml").importRuntimeDependencies().resolve()
				.withTransitivity().asFile();

		return ShrinkWrap.create(WebArchive.class).addPackage("application.rest").addPackage("application")
				.addAsLibraries(mavenFiles);
	}

	@Test
	public void testRunningOnServer() throws Exception {
		Properties p = System.getProperties();
		Enumeration<Object> keys = p.keys();
		while (keys.hasMoreElements()) {
			String key = (String) keys.nextElement();
			String value = (String) p.get(key);
			if (key.equals("wlp.process.type") && value.equals("server")) {
				return;
			}
		}
		assertTrue(false);
	}

	@Test
	public void testDataExistsAtEndpoint() throws Exception {
		URL endpoint = new URL("http://localhost:9080/myLibertyApp/api/transactions");
		String body = readAllAndClose(endpoint.openStream());
		int bodyLength = body.length();
		assertTrue(bodyLength > 0);
	}

	@Test
	public void testNewDataAddedToEndpoint() throws Exception {
		String firstBody = readAllAndClose(new URL("http://localhost:9080/myLibertyApp/api/transactions").openStream());
		int firstBodyLength = firstBody.length();
		Thread.sleep(12000);
		String secondBody = readAllAndClose(
				new URL("http://localhost:9080/myLibertyApp/api/transactions").openStream());
		int secondBodyLength = secondBody.length();
		assertTrue(secondBodyLength > firstBodyLength);
	}
	
	String readAllAndClose(InputStream is) throws Exception {
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		try {
			int read;
			while ((read = is.read()) != -1) {
				out.write(read);
			}
		} finally {
			try {
				is.close();
			} catch (Exception e) {
			}
		}
		return out.toString();
	}

}
@@ -600,18 +600,17 @@
 intIT %7B%0A
-%09
 %0A
+
 %09@Deploy
@@ -1379,16 +1379,39 @@
 ption %7B%0A
+%09%09Thread.sleep(10000);%0A
 %09%09URL en
@@ -1675,16 +1675,39 @@
 ption %7B%0A
+%09%09Thread.sleep(10000);%0A
 %09%09String
@@ -2104,10 +2104,9 @@
 %0A%09%7D%0A
-%09
 %0A
+
 %09Str
@@ -2408,9 +2408,10 @@
 ();%0A%09%7D%0A%0A
-
 %7D
+%0A

Add sleep to arquillian-tests for when verifyApps isn't set