package com.nflabs.zeppelin.rest;

import static org.junit.Assert.*;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.httpclient.methods.GetMethod;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runners.MethodSorters;

import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;

@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class ZqlRestTest extends RestApiTestAbstract {

  @BeforeClass
  public static void init() throws Exception {
    RestApiTestAbstract.startUp();
  }

  @AfterClass
  public static void destroy() {
    RestApiTestAbstract.shutDown();
  }

  private static Map<String, Object> data = new HashMap<String, Object>();

  @Test
  public void test001_getZqlRoot() throws IOException {
    // when
    GetMethod httpGetRoot = httpGet("/zql");
    // then
    assertThat(httpGetRoot, isAllowed());
    String body = httpGetRoot.getResponseBodyAsString();
    assertThat(body, isValidJSON());
    JsonElement json = new JsonParser().parse(body);
    Gson gson = new Gson();
    data = (Map<String, Object>) gson.fromJson(json, data.getClass());
    assertFalse("JSON object is null", json.isJsonNull());
    httpGetRoot.releaseConnection();
  }

  private String getFisrtId() {
    List<Object> jobs = (List<Object>) data.get("body");
    Map<String, Object> tmp = (Map<String, Object>) jobs.get(0);
    return tmp.get("id").toString();
  }

  @Test
  public void test002_getZqlSessionId() throws IOException {
    // when

    GetMethod httpGetRoot = httpGet("/zql/" + getFisrtId());
    // then
    assertThat(httpGetRoot, isAllowed());
    String body = httpGetRoot.getResponseBodyAsString();
    assertThat(body, isValidJSON());
  }

  @Test
  public void test003_getZqlHistorySessionId() throws IOException {
    // when

    GetMethod httpGetRoot = httpGet("/zql/history/" + getFisrtId());
    // then
    assertThat(httpGetRoot, isAllowed());
    String body = httpGetRoot.getResponseBodyAsString();
    assertThat(body, isValidJSON());
  }

}

@@ -1451,24 +1451,65 @@
 et(%22body%22);%0A
+    if (jobs.isEmpty())%0A      return %22%22;%0A
     Map%3CStri
@@ -1561,16 +1561,56 @@
 get(0);%0A
+    if (tmp.isEmpty())%0A      return %22%22;%0A
     retu
@@ -1716,32 +1716,237 @@
 n %7B%0A    // when%0A
+    String id = getFisrtId();%0A    // TODO (anthony): Implement post method et create test to get at least one element%0A    if (id.isEmpty()) %7B%0A      /** no job */%0A      assertTrue(true);%0A      return;%0A    %7D
 %0A    GetMethod h
@@ -1972,36 +1972,26 @@
 t(%22/zql/%22 + 
-getFisrtId()
+id
 );%0A    // th
@@ -2222,16 +2222,221 @@
 // when%0A
+    String id = getFisrtId();%0A    // TODO (anthony): Implement post method et create test to get at least one element%0A    if (id.isEmpty()) %7B%0A      /** no job */%0A      assertTrue(true);%0A      return;%0A    %7D
 %0A    Get
@@ -2482,28 +2482,18 @@
 ory/%22 + 
-getFisrtId()
+id
 );%0A    /

update test, add todo