]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-833: New Web Service to get history of measures (time machine)
authorGodin <mandrikov@gmail.com>
Mon, 13 Dec 2010 09:45:39 +0000 (09:45 +0000)
committerGodin <mandrikov@gmail.com>
Mon, 13 Dec 2010 09:45:39 +0000 (09:45 +0000)
sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/TimeMachineUnmarshaller.java
sonar-ws-client/src/test/java/org/sonar/wsclient/unmarshallers/TimeMachineUnmarshallerTest.java
sonar-ws-client/src/test/resources/timemachine/many.json [new file with mode: 0644]
sonar-ws-client/src/test/resources/timemachine/timemachine.json

index b9d5eb60be74e181df4ea8f7e3c1f9f51dc56148..8887da9abd2d5ac438df812348110664c2f5f7d5 100644 (file)
@@ -5,10 +5,7 @@ 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;
+import java.util.*;
 
 public class TimeMachineUnmarshaller implements Unmarshaller<TimeMachineData> {
 
@@ -17,7 +14,13 @@ public class TimeMachineUnmarshaller implements Unmarshaller<TimeMachineData> {
     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);
+      List<String> values = new ArrayList<String>();
+      for (int i = 0; i < array.size(); i++) {
+        Object elem = array.get(i);
+        String value = elem == null ? null : elem.toString();
+        values.add(value);
+      }
+      data.put(JsonUtils.parseDateTime((String) key), values);
     }
     return new TimeMachineData().setData(data);
   }
index e722c1120add424a5869d41819e4f838bc17bbd1..a05319884a2a877597b03d2940f1911e932eb8f6 100644 (file)
@@ -5,17 +5,39 @@ import org.junit.Test;
 import org.sonar.wsclient.services.TimeMachineData;
 
 import java.io.IOException;
+import java.text.SimpleDateFormat;
+import java.util.Date;
+import java.util.List;
+import java.util.Map;
 
 import static org.hamcrest.Matchers.is;
+import static org.hamcrest.Matchers.nullValue;
 import static org.junit.Assert.assertThat;
 
 public class TimeMachineUnmarshallerTest {
 
   @Test
-  public void toModel() throws IOException {
+  public void toModel() throws Exception {
     TimeMachineData data = new TimeMachineUnmarshaller().toModel(loadFile("/timemachine/timemachine.json"));
 
-    assertThat(data.getData().size(), is(2));
+    Map<Date, List<String>> map = data.getData();
+    assertThat(map.size(), is(1));
+    Date date = map.keySet().iterator().next();
+    final Date expectedDate = new SimpleDateFormat("yyyy-MM-dd'T'kk:mm:ssZZZZ").parse("2010-12-04T15:59:23+0000");
+    assertThat(date, is(expectedDate));
+    List<String> values = map.values().iterator().next();
+    assertThat(values.size(), is(3));
+    assertThat(values.get(0), is("20.0"));
+    assertThat(values.get(1), nullValue());
+    assertThat(values.get(2), is("12.8"));
+  }
+
+  @Test
+  public void many() throws Exception {
+    TimeMachineData data = new TimeMachineUnmarshaller().toModel(loadFile("/timemachine/many.json"));
+
+    Map<Date, List<String>> map = data.getData();
+    assertThat(map.size(), is(3));
   }
 
   private static String loadFile(String path) throws IOException {
diff --git a/sonar-ws-client/src/test/resources/timemachine/many.json b/sonar-ws-client/src/test/resources/timemachine/many.json
new file mode 100644 (file)
index 0000000..a65a8da
--- /dev/null
@@ -0,0 +1,5 @@
+{
+       "2010-10-10T00:00:00+0000": [25.0, null, 14.6],
+       "2010-11-15T00:00:00+0000": [23.0, null, 10.3],
+       "2010-12-04T00:00:00+0000": [20.0, null, 12.8]
+}
index e4777958df80b8d5f2a22a79370e98fd3cc23c32..03130cc0286d665fc87ff5d48240c7cf9ece3cc2 100644 (file)
@@ -1,4 +1,3 @@
 {
-       "2010-10-04T00:00:00+0000": ["18.0", null, "13.1"],
-       "2010-12-04T00:00:00+0000": ["20.0", null, "12.8"]
+       "2010-12-04T15:59:23+0000": [20.0, null, 12.8]
 }