diff options
Diffstat (limited to 'sonar-ws-client/src')
14 files changed, 384 insertions, 101 deletions
diff --git a/sonar-ws-client/src/main/java/org/sonar/wsclient/services/TimeMachine.java b/sonar-ws-client/src/main/java/org/sonar/wsclient/services/TimeMachine.java new file mode 100644 index 00000000000..fc859009ea0 --- /dev/null +++ b/sonar-ws-client/src/main/java/org/sonar/wsclient/services/TimeMachine.java @@ -0,0 +1,58 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2009 SonarSource SA + * mailto:contact AT sonarsource DOT com + * + * Sonar is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * Sonar is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.wsclient.services; + +/** + * Past values of a given resource + * + * @since 2.5 + */ +public class TimeMachine extends Model { + + private TimeMachineColumn[] columns; + private TimeMachineCell[] cells; + + public TimeMachine(TimeMachineColumn[] columns, TimeMachineCell[] cells) { + this.columns = columns; + this.cells = cells; + } + + public TimeMachineColumn[] getColumns() { + return columns; + } + + public TimeMachineCell[] getCells() { + return cells; + } + + public TimeMachineColumn getColumn(String metricKey) { + for (TimeMachineColumn column : columns) { + if (metricKey.equals(column.getMetricKey()) && column.getCharacteristicKey()==null) { + return column; + } + } + return null; + } + + public int getColumnIndex(String metricKey) { + TimeMachineColumn col = getColumn(metricKey); + return col!=null ? col.getIndex() : -1; + } +} 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/TimeMachineCell.java index 23d3bfc48c9..3f3beb188cf 100644 --- a/sonar-ws-client/src/main/java/org/sonar/wsclient/services/TimeMachineData.java +++ b/sonar-ws-client/src/main/java/org/sonar/wsclient/services/TimeMachineCell.java @@ -19,46 +19,26 @@ */ package org.sonar.wsclient.services; -import java.util.ArrayList; import java.util.Date; -import java.util.List; /** * @since 2.5 */ -public class TimeMachineData extends Model { +public class TimeMachineCell { + private Date date; + public Object[] values; - /** - * We use strings here in order to support measures with string value. - */ - private List<String> values = new ArrayList<String>(); + public TimeMachineCell(Date date, Object[] values) { + this.date = date; + this.values = values; + } public Date getDate() { return date; } - public TimeMachineData setDate(Date date) { - this.date = date; - return this; - } - - public List<String> getValues() { + public Object[] getValues() { return values; } - - public TimeMachineData setValues(List<String> values) { - this.values = values; - return this; - } - - public Double getValueAsDouble(int index) { - String valueStr = values.get(index); - try { - return valueStr == null ? null : Double.valueOf(valueStr); - } catch (NumberFormatException e) { - return null; - } - } - } diff --git a/sonar-ws-client/src/main/java/org/sonar/wsclient/services/TimeMachineColumn.java b/sonar-ws-client/src/main/java/org/sonar/wsclient/services/TimeMachineColumn.java new file mode 100644 index 00000000000..1d9bd009e36 --- /dev/null +++ b/sonar-ws-client/src/main/java/org/sonar/wsclient/services/TimeMachineColumn.java @@ -0,0 +1,54 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2009 SonarSource SA + * mailto:contact AT sonarsource DOT com + * + * Sonar is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * Sonar is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.wsclient.services; + +/** + * @since 2.5 + */ +public class TimeMachineColumn { + + private int index; + private String metricKey; + private String modelName; + private String characteristicKey; + + public TimeMachineColumn(int index, String metricKey, String modelName, String characteristicKey) { + this.index = index; + this.metricKey = metricKey; + this.modelName = modelName; + this.characteristicKey = characteristicKey; + } + + public String getMetricKey() { + return metricKey; + } + + public String getModelName() { + return modelName; + } + + public String getCharacteristicKey() { + return characteristicKey; + } + + public int getIndex() { + return index; + } +} 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 index 285f977715a..04fa7c4ea8f 100644 --- 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 @@ -24,7 +24,7 @@ import java.util.Date; /** * @since 2.5 */ -public class TimeMachineQuery extends Query<TimeMachineData> { +public class TimeMachineQuery extends Query<TimeMachine> { public static final String BASE_URL = "/api/timemachine"; @@ -87,18 +87,16 @@ public class TimeMachineQuery extends Query<TimeMachineData> { } @Override - public Class<TimeMachineData> getModelClass() { - return TimeMachineData.class; + public Class<TimeMachine> getModelClass() { + return TimeMachine.class; } public static TimeMachineQuery createForMetrics(String resourceKeyOrId, String... metricKeys) { - return new TimeMachineQuery(resourceKeyOrId) - .setMetrics(metricKeys); + return new TimeMachineQuery(resourceKeyOrId).setMetrics(metricKeys); } - public static TimeMachineQuery createForResource(Resource resource, String... metricKeys) { - return new TimeMachineQuery(resource.getId().toString()) - .setMetrics(metricKeys); + public static TimeMachineQuery createForMetrics(Resource resource, String... metricKeys) { + return new TimeMachineQuery(resource.getId().toString()).setMetrics(metricKeys); } } diff --git a/sonar-ws-client/src/main/java/org/sonar/wsclient/services/WSUtils.java b/sonar-ws-client/src/main/java/org/sonar/wsclient/services/WSUtils.java index 61e5940cb0f..ab4cd8d1028 100644 --- a/sonar-ws-client/src/main/java/org/sonar/wsclient/services/WSUtils.java +++ b/sonar-ws-client/src/main/java/org/sonar/wsclient/services/WSUtils.java @@ -1,3 +1,22 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2009 SonarSource SA + * mailto:contact AT sonarsource DOT com + * + * Sonar is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * Sonar is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ package org.sonar.wsclient.services; import java.util.Date; 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 index 1289e63a5ae..72203450dfc 100644 --- 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 @@ -1,26 +1,63 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2009 SonarSource SA + * mailto:contact AT sonarsource DOT com + * + * Sonar is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * Sonar is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ package org.sonar.wsclient.unmarshallers; import org.json.simple.JSONArray; import org.json.simple.JSONObject; -import org.sonar.wsclient.services.TimeMachineData; +import org.sonar.wsclient.services.TimeMachine; +import org.sonar.wsclient.services.TimeMachineCell; +import org.sonar.wsclient.services.TimeMachineColumn; -import java.util.ArrayList; -import java.util.List; +public class TimeMachineUnmarshaller extends AbstractUnmarshaller<TimeMachine> { -public class TimeMachineUnmarshaller extends AbstractUnmarshaller<TimeMachineData> { + protected TimeMachine parse(JSONObject json) { + JSONArray cols = JsonUtils.getArray(json, "cols"); + JSONArray cells = JsonUtils.getArray(json, "cells"); + return new TimeMachine(toColumns(cols), toCells(cells)); + } + + private TimeMachineColumn[] toColumns(JSONArray cols) { + int size = cols.size(); + TimeMachineColumn[] result = new TimeMachineColumn[size]; + for (int index = 0; index < size; index++) { + JSONObject colJson = (JSONObject)cols.get(index); + result[index]=new TimeMachineColumn(index, JsonUtils.getString(colJson, "metric"), null, null); + } + return result; + } + + private TimeMachineCell[] toCells(JSONArray cells) { + int size = cells.size(); + TimeMachineCell[] result = new TimeMachineCell[size]; + for (int i = 0; i < size; i++) { + JSONObject cellJson = (JSONObject)cells.get(i); + JSONArray valuesJson = JsonUtils.getArray(cellJson, "v"); - protected TimeMachineData parse(JSONObject json) { - String dateTimeStr = (String) json.keySet().iterator().next(); - JSONArray array = (JSONArray) json.get(dateTimeStr); - List<String> measures = new ArrayList<String>(); - for (int i = 0; i < array.size(); i++) { - Object elem = array.get(i); - String value = elem == null ? null : elem.toString(); - measures.add(value); + Object[] resultValues = new Object[valuesJson.size()]; + for (int indexValue = 0; indexValue < valuesJson.size(); indexValue++) { + Object value = valuesJson.get(indexValue); + resultValues[indexValue]=value; + } + result[i]=new TimeMachineCell(JsonUtils.getDateTime(cellJson, "d"), resultValues); } - return new TimeMachineData() - .setDate(JsonUtils.parseDateTime(dateTimeStr)) - .setValues(measures); + return result; } } 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 9f16130116b..ece84d4e2b3 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,7 +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()); + unmarshallers.put(TimeMachine.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/services/TimeMachineDataTest.java b/sonar-ws-client/src/test/java/org/sonar/wsclient/services/TimeMachineDataTest.java deleted file mode 100644 index 6672a3acf5e..00000000000 --- a/sonar-ws-client/src/test/java/org/sonar/wsclient/services/TimeMachineDataTest.java +++ /dev/null @@ -1,22 +0,0 @@ -package org.sonar.wsclient.services; - -import org.junit.Test; - -import java.util.Arrays; - -import static org.hamcrest.Matchers.is; -import static org.hamcrest.Matchers.nullValue; -import static org.junit.Assert.assertThat; - -public class TimeMachineDataTest { - - @Test - public void valueAsDouble() { - TimeMachineData data = new TimeMachineData().setValues(Arrays.asList(null, "20.3", "hello")); - - assertThat(data.getValueAsDouble(0), nullValue()); - assertThat(data.getValueAsDouble(1), is(20.3)); - assertThat(data.getValueAsDouble(2), nullValue()); - } - -} diff --git a/sonar-ws-client/src/test/java/org/sonar/wsclient/services/TimeMachineQueryTest.java b/sonar-ws-client/src/test/java/org/sonar/wsclient/services/TimeMachineQueryTest.java new file mode 100644 index 00000000000..86bc13d4542 --- /dev/null +++ b/sonar-ws-client/src/test/java/org/sonar/wsclient/services/TimeMachineQueryTest.java @@ -0,0 +1,52 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2009 SonarSource SA + * mailto:contact AT sonarsource DOT com + * + * Sonar is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * Sonar is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.wsclient.services; + +import org.junit.Before; +import org.junit.Test; +import org.sonar.wsclient.JdkUtils; + +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Date; + +import static org.hamcrest.Matchers.is; +import static org.junit.Assert.assertThat; + +public class TimeMachineQueryTest { + + @Before + public void before() { + WSUtils.setInstance(new JdkUtils()); + } + @Test + public void shouldGetUrl() { + TimeMachineQuery query = TimeMachineQuery.createForMetrics("12345", "ncloc", "coverage"); + assertThat(query.getUrl(), is("/api/timemachine?resource=12345&metrics=ncloc,coverage&")); + } + + @Test + public void shouldSetPeriod() throws ParseException { + Date from = new SimpleDateFormat("yyyy-MM-dd").parse("2010-02-18"); + Date to = new SimpleDateFormat("yyyy-MM-dd HH:mm").parse("2010-03-25 14:59"); + TimeMachineQuery query = TimeMachineQuery.createForMetrics("12345", "ncloc").setFrom(from).setTo(to); + assertThat(query.getUrl(), is("/api/timemachine?resource=12345&metrics=ncloc&fromDateTime=2010-02-18T00%3A00%3A00%2B0100&toDateTime=2010-03-25T14%3A59%3A00%2B0100&")); + } +} 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 index 908658f336b..774dd1bfd81 100644 --- 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 @@ -1,37 +1,74 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2009 SonarSource SA + * mailto:contact AT sonarsource DOT com + * + * Sonar is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * Sonar is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ package org.sonar.wsclient.unmarshallers; import org.junit.Test; -import org.sonar.wsclient.services.TimeMachineData; - -import java.text.SimpleDateFormat; -import java.util.Date; -import java.util.List; +import org.sonar.wsclient.services.TimeMachine; +import static org.hamcrest.CoreMatchers.nullValue; 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 Exception { - TimeMachineData data = new TimeMachineUnmarshaller().toModel(WSTestUtils.loadFile("/timemachine/timemachine.json")); - - Date date = data.getDate(); - 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 = data.getValues(); - assertThat(values.size(), is(3)); - assertThat(values.get(0), is("20.0")); - assertThat(values.get(1), nullValue()); - assertThat(values.get(2), is("12.8")); + public void testToModel() throws Exception { + TimeMachine timeMachine = new TimeMachineUnmarshaller().toModel(WSTestUtils.loadFile("/timemachine/timemachine.json")); + + // columns + assertThat(timeMachine.getColumns().length, is(2)); + assertThat(timeMachine.getColumns()[0].getIndex(), is(0)); + assertThat(timeMachine.getColumns()[0].getMetricKey(), is("ncloc")); + assertThat(timeMachine.getColumns()[1].getIndex(), is(1)); + assertThat(timeMachine.getColumns()[1].getMetricKey(), is("coverage")); + + // values sorted by date + assertThat(timeMachine.getCells().length, is(3)); // 3 days + assertThat(timeMachine.getCells()[0].getDate().getDate(), is(19)); + assertThat(timeMachine.getCells()[1].getDate().getDate(), is(21)); + assertThat(timeMachine.getCells()[2].getDate().getDate(), is(25)); + + assertThat(timeMachine.getCells()[0].getValues().length, is(2)); + assertThat((Double) timeMachine.getCells()[0].getValues()[0], is(21.0)); + assertThat((Double) timeMachine.getCells()[0].getValues()[1], is(80.0)); } @Test - public void many() throws Exception { - List<TimeMachineData> data = new TimeMachineUnmarshaller().toModels(WSTestUtils.loadFile("/timemachine/many.json")); + public void shouldAcceptNullValues() throws Exception { + TimeMachine timeMachine = new TimeMachineUnmarshaller().toModel(WSTestUtils.loadFile("/timemachine/null-values.json")); + + assertThat(timeMachine.getCells()[0].getValues().length, is(2)); + assertThat(timeMachine.getCells()[0].getValues()[0], nullValue()); + assertThat((Double) timeMachine.getCells()[0].getValues()[1], is(80.0)); - assertThat(data.size(), is(3)); + assertThat((Double) timeMachine.getCells()[1].getValues()[0], is(29.0)); + assertThat(timeMachine.getCells()[1].getValues()[1], nullValue()); } + @Test + public void shouldCastValues() throws Exception { + TimeMachine timeMachine = new TimeMachineUnmarshaller().toModel(WSTestUtils.loadFile("/timemachine/typed-values.json")); + + assertThat(timeMachine.getCells()[0].getValues().length, is(2)); + assertThat((String)timeMachine.getCells()[0].getValues()[0], is("Sonar way")); + assertThat((Double) timeMachine.getCells()[0].getValues()[1], is(80.0)); + + } } diff --git a/sonar-ws-client/src/test/resources/timemachine/many.json b/sonar-ws-client/src/test/resources/timemachine/many.json deleted file mode 100644 index 6ee5fa2312a..00000000000 --- a/sonar-ws-client/src/test/resources/timemachine/many.json +++ /dev/null @@ -1,5 +0,0 @@ -[ - {"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]} -] diff --git a/sonar-ws-client/src/test/resources/timemachine/null-values.json b/sonar-ws-client/src/test/resources/timemachine/null-values.json new file mode 100644 index 00000000000..394d008208b --- /dev/null +++ b/sonar-ws-client/src/test/resources/timemachine/null-values.json @@ -0,0 +1,26 @@ +[ + { + "cols":[ + { + "metric":"ncloc" + }, + { + "metric":"coverage" + } + ], + "cells":[ + { + "d":"2010-10-19T00:01:00+0200", + "v":[null,80.0] + }, + { + "d":"2010-10-21T00:01:00+0100", + "v":[29.0,null] + }, + { + "d":"2010-10-25T00:04:00+0100", + "v":[22.6,null] + } + ] + } +]
\ No newline at end of file diff --git a/sonar-ws-client/src/test/resources/timemachine/timemachine.json b/sonar-ws-client/src/test/resources/timemachine/timemachine.json index 17f064aeb0b..376770398e9 100644 --- a/sonar-ws-client/src/test/resources/timemachine/timemachine.json +++ b/sonar-ws-client/src/test/resources/timemachine/timemachine.json @@ -1,3 +1,26 @@ [ - {"2010-12-04T15:59:23+0000": [20.0, null, 12.8]} -] + { + "cols":[ + { + "metric":"ncloc" + }, + { + "metric":"coverage" + } + ], + "cells":[ + { + "d":"2010-10-19T00:01:00+0200", + "v":[21.0,80.0] + }, + { + "d":"2010-10-21T00:01:00+0100", + "v":[29.0,70.0] + }, + { + "d":"2010-10-25T00:04:00+0100", + "v":[22.6,75.0] + } + ] + } +]
\ No newline at end of file diff --git a/sonar-ws-client/src/test/resources/timemachine/typed-values.json b/sonar-ws-client/src/test/resources/timemachine/typed-values.json new file mode 100644 index 00000000000..b33d948f271 --- /dev/null +++ b/sonar-ws-client/src/test/resources/timemachine/typed-values.json @@ -0,0 +1,26 @@ +[ + { + "cols":[ + { + "metric":"profile" + }, + { + "metric":"coverage" + } + ], + "cells":[ + { + "d":"2010-10-19T00:01:00+0200", + "v":["Sonar way",80.0] + }, + { + "d":"2010-10-21T00:01:00+0100", + "v":["Sonar way",70.0] + }, + { + "d":"2010-10-25T00:04:00+0100", + "v":["Sun checks",75.0] + } + ] + } +]
\ No newline at end of file |