package ca.ualberta.physics.cicstart.macro.resource;

import static com.jayway.restassured.RestAssured.expect;

import org.junit.Test;

import ca.ualberta.physics.cssdp.configuration.ResourceUrls;
import ca.ualberta.physics.cssdp.domain.auth.User;
import ca.ualberta.physics.cssdp.domain.macro.Instance;
import ca.ualberta.physics.cssdp.domain.macro.InstanceSpec;

import com.jayway.restassured.http.ContentType;
import com.jayway.restassured.response.Response;

public class MacroResourceTests extends MacroTestsScaffolding {

	@Test
	public void testStartVM() {

		User dataManager = setupDataManager();
		String sessionToken = login(dataManager.getEmail(), "password");

		InstanceSpec instanceSpec = new InstanceSpec();
		instanceSpec.setCloud("DAIR");
		instanceSpec.setFlavor("m1.tiny");
		instanceSpec.setImage("Ubuntu 12.04 cloudimg amd64");
		instanceSpec.setRequestId("TESTJOB");

//		String macroUrl = Common.properties().getString("api.url") + "/macro";
		Response res = expect().statusCode(200).when().given()
				.content(instanceSpec).and().contentType(ContentType.JSON)
				.and().headers("CICSTART.session", sessionToken)
				.post(ResourceUrls.MACRO + "/vm");

		Instance instance = res.as(Instance.class);

		// cleanup, stop the instance
		expect().statusCode(200).when().given().content(instance).and()
				.contentType(ContentType.JSON).and()
				.headers("CICSTART.session", sessionToken)
				.delete(ResourceUrls.MACRO + "/vm");

		// TODO release floating ips
		
	}

}

@@ -104,16 +104,97 @@
 expect;%0A
+import static com.jayway.restassured.RestAssured.given;%0A%0Aimport org.junit.Assert;
 %0Aimport 
@@ -909,22 +909,9 @@
 2.04
- cloudimg amd6
+.
 4%22);
@@ -956,12 +956,13 @@
 );%0A%0A
-//
 %09%09
+// 
 Stri
@@ -1017,16 +1017,21 @@
 .url%22) +
+%0A%09%09//
  %22/macro
@@ -1054,52 +1054,15 @@
 s = 
-expect().statusCode(200).when().
 given()
-%0A%09%09%09%09
 .con
@@ -1085,16 +1085,21 @@
 c).and()
+%0A%09%09%09%09
 .content
@@ -1124,19 +1124,19 @@
 SON)
-%0A%09%09%09%09
 .and()
+%0A%09%09%09%09
 .hea
@@ -1210,24 +1210,203 @@
  + %22/vm%22);%0A%0A
+%09%09if (res.getStatusCode() != 200) %7B%0A%09%09%09System.out.println(res.asString());%0A%09%09%7D%0A%0A%09%09Assert.assertEquals(%22Expecting 200, not %22 + res.getStatusCode(), 200,%0A%09%09%09%09res.getStatusCode());%0A%0A
 %09%09Instance i
@@ -1699,17 +1699,15 @@
 ing ips%0A
-%09%09
 %0A%09%7D%0A%0A%7D%0A

make failing test easier to debug; image name changed on dair