]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-833: Add time machine to sonar-ws-client and sonar-gwt-api
authorGodin <mandrikov@gmail.com>
Sun, 12 Dec 2010 04:35:09 +0000 (04:35 +0000)
committerGodin <mandrikov@gmail.com>
Sun, 12 Dec 2010 04:35:09 +0000 (04:35 +0000)
14 files changed:
sonar-gwt-api/src/main/java/org/sonar/gwt/JsonUtils.java
sonar-gwt-api/src/main/java/org/sonar/wsclient/gwt/unmarshallers/ResourceUnmarshaller.java
sonar-gwt-api/src/main/java/org/sonar/wsclient/gwt/unmarshallers/TimeMachineUnmarshaller.java [new file with mode: 0644]
sonar-gwt-api/src/main/java/org/sonar/wsclient/gwt/unmarshallers/Unmarshallers.java
sonar-ws-client/src/main/java/org/sonar/wsclient/services/TimeMachineData.java [new file with mode: 0644]
sonar-ws-client/src/main/java/org/sonar/wsclient/services/TimeMachineQuery.java [new file with mode: 0644]
sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/JsonUtils.java
sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/MetricUnmarshaller.java
sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/PropertyUnmarshaller.java
sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/ResourceUnmarshaller.java
sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/TimeMachineUnmarshaller.java [new file with mode: 0644]
sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/Unmarshallers.java
sonar-ws-client/src/test/java/org/sonar/wsclient/unmarshallers/TimeMachineUnmarshallerTest.java [new file with mode: 0644]
sonar-ws-client/src/test/resources/timemachine/timemachine.json [new file with mode: 0644]

index 9682d29749c55a4f5aa9858e8f7b799634f380ed..57de5419ed5d58ed205bf1e5ff8c3f5ae002aeaa 100644 (file)
@@ -50,13 +50,14 @@ public final class JsonUtils {
       url += "format=json&";
     }
     if (!url.contains("callback=")) {
-      //IMPORTANT : the url should ended with ?callback= or &callback= for JSONP calls
+      // IMPORTANT : the url should ended with ?callback= or &callback= for JSONP calls
       url += "callback=";
     }
     makeJSONRequest(requestId++, URL.encode(url), handler);
   }
 
-  public static native void makeJSONRequest(int requestId, String url, JSONHandler handler) /*-{
+  public static native void makeJSONRequest(int requestId, String url, JSONHandler handler)
+  /*-{
     var callback = "callback" + requestId;
 
     // create SCRIPT tag, and set SRC attribute equal to JSON feed URL + callback function name
@@ -115,12 +116,7 @@ public final class JsonUtils {
   public static Date getDate(JSONObject json, String field) {
     String date = getString(json, field);
     if (date != null) {
-      DateTimeFormat frmt = DateTimeFormat.getFormat("yyyy-MM-dd'T'HH:mm:ssZ");
-      if (date.endsWith("Z") && date.length() > 2) {
-        // see SONAR-1182
-        date = date.substring(0, date.length() - 2) + "+0000";
-      }
-      return frmt.parse(date);
+      return parseDateTime(date);
     }
     return null;
   }
@@ -177,5 +173,16 @@ public final class JsonUtils {
     throw new JavaScriptException("Not implemented");
   }
 
-}
+  /**
+   * @since 2.5
+   */
+  public static Date parseDateTime(String dateTime) {
+    DateTimeFormat frmt = DateTimeFormat.getFormat("yyyy-MM-dd'T'HH:mm:ssZ");
+    if (dateTime.endsWith("Z") && dateTime.length() > 2) {
+      // see SONAR-1182
+      dateTime = dateTime.substring(0, dateTime.length() - 2) + "+0000";
+    }
+    return frmt.parse(dateTime);
+  }
 
+}
index f6ad914bffc190317a05edb5437038b3dfc35d12..3451bf16ed547c0e08e5b8ec9022913655d949bc 100644 (file)
@@ -92,7 +92,7 @@ public class ResourceUnmarshaller extends AbstractUnmarshaller<Resource> {
         .setRuleKey(JsonUtils.getString(json, "rule_key"))
         .setRuleName(JsonUtils.getString(json, "rule_name"))
         .setRuleCategory(JsonUtils.getString(json, "rule_category"))
-        .setRulePriority(JsonUtils.getString(json, "rule_priority"))
+        .setRuleSeverity(JsonUtils.getString(json, "rule_priority"))
         .setCharacteristicKey(JsonUtils.getString(json, "ctic_key"))
         .setCharacteristicName(JsonUtils.getString(json, "ctic_name"));
     return measure;
diff --git a/sonar-gwt-api/src/main/java/org/sonar/wsclient/gwt/unmarshallers/TimeMachineUnmarshaller.java b/sonar-gwt-api/src/main/java/org/sonar/wsclient/gwt/unmarshallers/TimeMachineUnmarshaller.java
new file mode 100644 (file)
index 0000000..53f56ba
--- /dev/null
@@ -0,0 +1,32 @@
+package org.sonar.wsclient.gwt.unmarshallers;
+
+import com.google.gwt.core.client.JavaScriptObject;
+import com.google.gwt.json.client.JSONArray;
+import com.google.gwt.json.client.JSONObject;
+import org.sonar.gwt.JsonUtils;
+import org.sonar.wsclient.services.TimeMachineData;
+
+import java.util.*;
+
+public class TimeMachineUnmarshaller implements Unmarshaller<TimeMachineData> {
+
+  public TimeMachineData toModel(JavaScriptObject json) {
+    JSONObject map = new JSONObject(json);
+    Map<Date, List<String>> data = new HashMap<Date, List<String>>();
+    for (String dateTimeStr : map.keySet()) {
+      JSONArray array = map.get(dateTimeStr).isArray();
+      List<String> values = new ArrayList<String>();
+      for (int i = 0; i < JsonUtils.getArraySize(array); i++) {
+        String value = array.get(i).isString().stringValue();
+        values.add(value);
+      }
+      data.put(JsonUtils.parseDateTime(dateTimeStr), values);
+    }
+    return new TimeMachineData().setData(data);
+  }
+
+  public List<TimeMachineData> toModels(JavaScriptObject json) {
+    return Arrays.asList(toModel(json));
+  }
+
+}
index deb77356232519ee68ac79cbb32003b70af7933f..93a5dbf8110e203b31b6abd43f2efc936788f5b8 100644 (file)
@@ -41,6 +41,7 @@ public final class Unmarshallers {
     unmarshallers.put(Violation.class, new ViolationUnmarshaller());
     unmarshallers.put(Server.class, new ServerUnmarshaller());
     unmarshallers.put(DependencyTree.class, new DependencyTreeUnmarshaller());
+    unmarshallers.put(TimeMachineData.class, new TimeMachineUnmarshaller());
   }
 
   public static Unmarshaller forModel(Class modelClass) {
diff --git a/sonar-ws-client/src/main/java/org/sonar/wsclient/services/TimeMachineData.java b/sonar-ws-client/src/main/java/org/sonar/wsclient/services/TimeMachineData.java
new file mode 100644 (file)
index 0000000..e017253
--- /dev/null
@@ -0,0 +1,21 @@
+package org.sonar.wsclient.services;
+
+import java.util.Date;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+public class TimeMachineData extends Model {
+
+  private Map<Date, List<String>> data = new HashMap<Date, List<String>>();
+
+  public Map<Date, List<String>> getData() {
+    return data;
+  }
+
+  public TimeMachineData setData(Map<Date, List<String>> data) {
+    this.data = data;
+    return this;
+  }
+
+}
diff --git a/sonar-ws-client/src/main/java/org/sonar/wsclient/services/TimeMachineQuery.java b/sonar-ws-client/src/main/java/org/sonar/wsclient/services/TimeMachineQuery.java
new file mode 100644 (file)
index 0000000..31a00fe
--- /dev/null
@@ -0,0 +1,65 @@
+package org.sonar.wsclient.services;
+
+import java.util.Date;
+
+public class TimeMachineQuery extends Query<TimeMachineData> {
+
+  public static final String BASE_URL = "/api/timemachine";
+
+  private String resourceKeyOrId;
+  private String[] metrics;
+  private Date from;
+  private Date to;
+
+  public TimeMachineQuery(String resourceKeyOrId) {
+    this.resourceKeyOrId = resourceKeyOrId;
+  }
+
+  public String[] getMetrics() {
+    return metrics;
+  }
+
+  public TimeMachineQuery setMetrics(String... metrics) {
+    this.metrics = metrics;
+    return this;
+  }
+
+  public Date getFrom() {
+    return from;
+  }
+
+  public TimeMachineQuery setFrom(Date from) {
+    this.from = from;
+    return this;
+  }
+
+  public Date getTo() {
+    return to;
+  }
+
+  public TimeMachineQuery setTo(Date to) {
+    this.to = to;
+    return this;
+  }
+
+  @Override
+  public String getUrl() {
+    StringBuilder url = new StringBuilder(BASE_URL);
+    url.append('?');
+    appendUrlParameter(url, "resource", resourceKeyOrId);
+    appendUrlParameter(url, "metrics", metrics);
+    appendUrlParameter(url, "first_date", from);
+    appendUrlParameter(url, "last_date", to);
+    return url.toString();
+  }
+
+  @Override
+  public Class<TimeMachineData> getModelClass() {
+    return TimeMachineData.class;
+  }
+
+  public static TimeMachineQuery create(String resourceKeyOrId) {
+    return new TimeMachineQuery(resourceKeyOrId);
+  }
+
+}
index 1df2dded9016e2082e760ad0c7587a81de77ad6a..bdf87e27ac029461163c6dc9a0e3ed87d053e779 100644 (file)
@@ -19,6 +19,8 @@
  */
 package org.sonar.wsclient.unmarshallers;
 
+import org.json.simple.JSONArray;
+
 import java.text.ParseException;
 import java.text.SimpleDateFormat;
 import java.util.Date;
@@ -72,6 +74,13 @@ public final class JsonUtils {
     return null;
   }
 
+  /**
+   * @since 2.5
+   */
+  public static JSONArray getArray(Map obj, String field) {
+    return (JSONArray) obj.get(field);
+  }
+
   public static Date getDateTime(Map obj, String field) {
     return parseDate(obj, field, "yyyy-MM-dd'T'HH:mm:ssZ");
   }
@@ -93,4 +102,16 @@ public final class JsonUtils {
     }
     return null;
   }
+
+  /**
+   * @since 2.5
+   */
+  public static Date parseDateTime(String dateTime) {
+    try {
+      SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
+      return dateFormat.parse(dateTime);
+    } catch (ParseException e) {
+      throw new RuntimeException(e);
+    }
+  }
 }
index d90101b833d6931937e7062e384c393f21974ae5..e63bcd8c08b17717fcbf5746fda859ee74864954 100644 (file)
@@ -27,10 +27,10 @@ public class MetricUnmarshaller extends AbstractUnmarshaller<Metric> {
   @Override
   protected Metric parse(JSONObject json) {
     return new Metric()
-        .setKey((String) json.get("key"))
-        .setName((String) json.get("name"))
-        .setDomain((String) json.get("domain"))
-        .setDescription((String) json.get("description"))
+        .setKey(JsonUtils.getString(json, "key"))
+        .setName(JsonUtils.getString(json, "name"))
+        .setDomain(JsonUtils.getString(json, "domain"))
+        .setDescription(JsonUtils.getString(json, "description"))
         .setDirection(JsonUtils.getInteger(json, "direction"))
         .setType(JsonUtils.getString(json, "val_type"))
         .setUserManaged(JsonUtils.getBoolean(json, "user_managed"))
index cf71b47c32b6ddb6cd68dd74cdc6ddb3530bf341..9df96d1433d3591ca28df518094daa9b8058c44c 100644 (file)
@@ -27,7 +27,7 @@ public class PropertyUnmarshaller extends AbstractUnmarshaller<Property> {
   @Override
   protected Property parse(JSONObject json) {
     return new Property()
-        .setKey((String) json.get("key"))
-        .setValue((String) json.get("value"));
+        .setKey(JsonUtils.getString(json, "key"))
+        .setValue(JsonUtils.getString(json, "value"));
   }
 }
\ No newline at end of file
index 24c5f0d687c819658304f0227528bf9f2cd3773f..81b86cd8b12bbd998cc30516139c31adb34f5480 100644 (file)
@@ -51,7 +51,7 @@ public class ResourceUnmarshaller extends AbstractUnmarshaller<Resource> {
   }
 
   private void parseMeasures(JSONObject json, Resource resource) {
-    JSONArray measuresJson = (JSONArray) json.get("msr");
+    JSONArray measuresJson = JsonUtils.getArray(json, "msr");
     if (measuresJson != null) {
       resource.setMeasures(parseMeasures(measuresJson));
     }
diff --git a/sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/TimeMachineUnmarshaller.java b/sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/TimeMachineUnmarshaller.java
new file mode 100644 (file)
index 0000000..b9d5eb6
--- /dev/null
@@ -0,0 +1,29 @@
+package org.sonar.wsclient.unmarshallers;
+
+import org.json.simple.JSONArray;
+import org.json.simple.JSONObject;
+import org.json.simple.JSONValue;
+import org.sonar.wsclient.services.TimeMachineData;
+
+import java.util.Date;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+public class TimeMachineUnmarshaller implements Unmarshaller<TimeMachineData> {
+
+  public TimeMachineData toModel(String json) {
+    JSONObject map = (JSONObject) JSONValue.parse(json);
+    Map<Date, List<String>> data = new HashMap<Date, List<String>>();
+    for (Object key : map.keySet()) {
+      JSONArray array = (JSONArray) map.get(key);
+      data.put(JsonUtils.parseDateTime((String) key), array);
+    }
+    return new TimeMachineData().setData(data);
+  }
+
+  public List<TimeMachineData> toModels(String json) {
+    throw new UnsupportedOperationException();
+  }
+
+}
index eecb274d7c6ef2efa4fe055eaa95ecb3db082c63..9f16130116b5a5245be347e2d82c3801d358a3b8 100644 (file)
@@ -44,6 +44,7 @@ public final class Unmarshallers {
     unmarshallers.put(Favourite.class, new FavouriteUnmarshaller());
     unmarshallers.put(Plugin.class, new PluginUnmarshaller());
     unmarshallers.put(Rule.class, new RuleUnmarshaller());
+    unmarshallers.put(TimeMachineData.class, new TimeMachineUnmarshaller());
   }
 
   public static <MODEL extends Model> Unmarshaller<MODEL> forModel(Class<MODEL> modelClass) {
diff --git a/sonar-ws-client/src/test/java/org/sonar/wsclient/unmarshallers/TimeMachineUnmarshallerTest.java b/sonar-ws-client/src/test/java/org/sonar/wsclient/unmarshallers/TimeMachineUnmarshallerTest.java
new file mode 100644 (file)
index 0000000..e722c11
--- /dev/null
@@ -0,0 +1,25 @@
+package org.sonar.wsclient.unmarshallers;
+
+import org.apache.commons.io.IOUtils;
+import org.junit.Test;
+import org.sonar.wsclient.services.TimeMachineData;
+
+import java.io.IOException;
+
+import static org.hamcrest.Matchers.is;
+import static org.junit.Assert.assertThat;
+
+public class TimeMachineUnmarshallerTest {
+
+  @Test
+  public void toModel() throws IOException {
+    TimeMachineData data = new TimeMachineUnmarshaller().toModel(loadFile("/timemachine/timemachine.json"));
+
+    assertThat(data.getData().size(), is(2));
+  }
+
+  private static String loadFile(String path) throws IOException {
+    return IOUtils.toString(TimeMachineUnmarshallerTest.class.getResourceAsStream(path));
+  }
+
+}
diff --git a/sonar-ws-client/src/test/resources/timemachine/timemachine.json b/sonar-ws-client/src/test/resources/timemachine/timemachine.json
new file mode 100644 (file)
index 0000000..e477795
--- /dev/null
@@ -0,0 +1,4 @@
+{
+       "2010-10-04T00:00:00+0000": ["18.0", null, "13.1"],
+       "2010-12-04T00:00:00+0000": ["20.0", null, "12.8"]
+}