diff options
Diffstat (limited to 'sonar-ws-client')
10 files changed, 173 insertions, 7 deletions
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 index 00000000000..e017253f783 --- /dev/null +++ b/sonar-ws-client/src/main/java/org/sonar/wsclient/services/TimeMachineData.java @@ -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 index 00000000000..31a00fe9518 --- /dev/null +++ b/sonar-ws-client/src/main/java/org/sonar/wsclient/services/TimeMachineQuery.java @@ -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); + } + +} diff --git a/sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/JsonUtils.java b/sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/JsonUtils.java index 1df2dded901..bdf87e27ac0 100644 --- a/sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/JsonUtils.java +++ b/sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/JsonUtils.java @@ -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); + } + } } diff --git a/sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/MetricUnmarshaller.java b/sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/MetricUnmarshaller.java index d90101b833d..e63bcd8c08b 100644 --- a/sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/MetricUnmarshaller.java +++ b/sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/MetricUnmarshaller.java @@ -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")) diff --git a/sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/PropertyUnmarshaller.java b/sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/PropertyUnmarshaller.java index cf71b47c32b..9df96d1433d 100644 --- a/sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/PropertyUnmarshaller.java +++ b/sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/PropertyUnmarshaller.java @@ -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 diff --git a/sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/ResourceUnmarshaller.java b/sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/ResourceUnmarshaller.java index 24c5f0d687c..81b86cd8b12 100644 --- a/sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/ResourceUnmarshaller.java +++ b/sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/ResourceUnmarshaller.java @@ -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 index 00000000000..b9d5eb60be7 --- /dev/null +++ b/sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/TimeMachineUnmarshaller.java @@ -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(); + } + +} diff --git a/sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/Unmarshallers.java b/sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/Unmarshallers.java index eecb274d7c6..9f16130116b 100644 --- a/sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/Unmarshallers.java +++ b/sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/Unmarshallers.java @@ -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 index 00000000000..e722c1120ad --- /dev/null +++ b/sonar-ws-client/src/test/java/org/sonar/wsclient/unmarshallers/TimeMachineUnmarshallerTest.java @@ -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 index 00000000000..e4777958df8 --- /dev/null +++ b/sonar-ws-client/src/test/resources/timemachine/timemachine.json @@ -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"] +} |