package org.rhq.metrics.rest;

import static com.jayway.restassured.RestAssured.given;
import static org.hamcrest.CoreMatchers.equalTo;

import java.io.File;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;

import com.jayway.restassured.http.ContentType;
import com.jayway.restassured.path.json.JsonPath;
import com.jayway.restassured.response.Response;

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.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.jboss.shrinkwrap.resolver.api.maven.archive.importer.MavenImporter;

@RunWith(Arquillian.class)
public class BaseTest {

    private static final int HTTP_PORT = 58080;

    @Deployment(testable=false)
    public static WebArchive createDeployment() {
        File pomFile = new File("../rest-servlet/pom.xml");
        System.out.println("pomfile " + pomFile.getAbsolutePath());
        System.out.flush();

        WebArchive archive =
        ShrinkWrap.create(MavenImporter.class)
            .offline()
            .loadPomFromFile(pomFile)
            .importBuildOutput()
            .as(WebArchive.class);
        System.out.println("archive is " + archive.toString(false));
        System.out.flush();
        return archive;
    }

    @Test
    public void pingTest() throws Exception {
        Response jsonp = given()
                .port(HTTP_PORT)
                .expect()
                    .statusCode(200)
                .when()
                    .post("/rhq-metrics/ping")
                .then()
                    .contentType(ContentType.JSON)
                .extract()
                    .response();

        JsonPath jsonPath = new JsonPath(jsonp.asString());

        DateFormat df = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.US);
        Date date = df.parse(jsonPath.getString("pong"));
        Date now = new Date();

        long timeDifference = now.getTime() - date.getTime();

        Assert.assertTrue("Difference is " + timeDifference, timeDifference < 2500L);
    }

    @Test
    public void testAddGetValue() throws Exception {

        Map<String,Object> data = new HashMap<>();
        String id = "foo";
        data.put("id", id);
        long now = System.currentTimeMillis();
        data.put("timestamp", now);
        data.put("value",42d);

        given()
            .port(HTTP_PORT)
            .body(data)
            .pathParam("id",id)
            .contentType(ContentType.JSON)
        .expect()
            .statusCode(200)
        .when()
            .post("/rhq-metrics/metrics/{id}");


        given()
            .port(HTTP_PORT)
            .pathParam("id", id)
          .header("Accepts", "application/json")
        .expect()
           .statusCode(200)
            .log().ifError()
            .body("timestamp[0]", equalTo(now))
        .when()
           .get("/rhq-metrics/metrics/{id}");
    }
}

@@ -147,24 +147,45 @@
 va.io.File;%0A
+import java.net.URL;%0A
 import java.
@@ -337,238 +337,8 @@
 p;%0A%0A
-import org.junit.Assert;%0Aimport org.junit.Test;%0Aimport org.junit.runner.RunWith;%0A%0Aimport com.jayway.restassured.http.ContentType;%0Aimport com.jayway.restassured.path.json.JsonPath;%0Aimport com.jayway.restassured.response.Response;%0A%0A
 impo
@@ -424,38 +424,24 @@
 ian.
-container.test.api.RunAsClient
+junit.Arquillian
 ;%0Aim
@@ -458,37 +458,40 @@
 boss.arquillian.
-junit
+test.api
 .Arquillian;%0Aimp
@@ -477,32 +477,40 @@
 t.api.Arquillian
+Resource
 ;%0Aimport org.jbo
@@ -666,16 +666,245 @@
 mporter;
+%0Aimport org.junit.Assert;%0Aimport org.junit.Test;%0Aimport org.junit.runner.RunWith;%0A%0Aimport com.jayway.restassured.http.ContentType;%0Aimport com.jayway.restassured.path.json.JsonPath;%0Aimport com.jayway.restassured.response.Response;
 %0A%0A@RunWi
@@ -953,57 +953,8 @@
  %7B%0A%0A
-    private static final int HTTP_PORT = 58080;%0A%0A
     
@@ -1513,32 +1513,82 @@
 archive;%0A    %7D%0A%0A
+    @ArquillianResource%0A    private URL baseUrl;%0A%0A
     @Test%0A    pu
@@ -1664,41 +1664,8 @@
 n()%0A
-                .port(HTTP_PORT)%0A
     
@@ -1765,32 +1765,49 @@
           .post(
+new URL(baseUrl, 
 %22/rhq-metrics/pi
@@ -1810,16 +1810,17 @@
 s/ping%22)
+)
 %0A       
@@ -2649,37 +2649,8 @@
 n()%0A
-            .port(HTTP_PORT)%0A
     
@@ -2825,16 +2825,33 @@
   .post(
+new URL(baseUrl, 
 %22/rhq-me
@@ -2870,16 +2870,17 @@
 s/%7Bid%7D%22)
+)
 ;%0A%0A%0A    
@@ -2895,37 +2895,8 @@
 n()%0A
-            .port(HTTP_PORT)%0A
     
@@ -3128,16 +3128,33 @@
    .get(
+new URL(baseUrl, 
 %22/rhq-me
@@ -3173,16 +3173,17 @@
 s/%7Bid%7D%22)
+)
 ;%0A    %7D%0A

Use @ArquillianResource URL injection to avoid hardcoding ports