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> {
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);
}
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 {
--- /dev/null
+{
+ "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]
+}