From 58a8a74c05def9176479b2cf951c49f398cb72f8 Mon Sep 17 00:00:00 2001 From: Simon Brandhof Date: Wed, 27 Jul 2011 10:59:00 +0200 Subject: [PATCH] SONAR-2648 New web service /api/manual_measures --- .../api/manual_measures_controller.rb | 12 +- .../wsclient/services/ManualMeasure.java | 133 ++++++++++++++++++ .../wsclient/services/ManualMeasureQuery.java | 64 +++++++++ .../ManualMeasureUnmarshaller.java | 45 ++++++ .../wsclient/unmarshallers/Unmarshallers.java | 1 + .../services/ManualMeasureQueryTest.java | 41 ++++++ .../ManualMeasureUnmarshallerTest.java | 59 ++++++++ .../manual_measures/all_measures.json | 22 +++ .../manual_measures/single_measure.json | 12 ++ 9 files changed, 383 insertions(+), 6 deletions(-) create mode 100644 sonar-ws-client/src/main/java/org/sonar/wsclient/services/ManualMeasure.java create mode 100644 sonar-ws-client/src/main/java/org/sonar/wsclient/services/ManualMeasureQuery.java create mode 100644 sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/ManualMeasureUnmarshaller.java create mode 100644 sonar-ws-client/src/test/java/org/sonar/wsclient/services/ManualMeasureQueryTest.java create mode 100644 sonar-ws-client/src/test/java/org/sonar/wsclient/unmarshallers/ManualMeasureUnmarshallerTest.java create mode 100644 sonar-ws-client/src/test/resources/manual_measures/all_measures.json create mode 100644 sonar-ws-client/src/test/resources/manual_measures/single_measure.json diff --git a/sonar-server/src/main/webapp/WEB-INF/app/controllers/api/manual_measures_controller.rb b/sonar-server/src/main/webapp/WEB-INF/app/controllers/api/manual_measures_controller.rb index 7be0373987b..704058273fe 100644 --- a/sonar-server/src/main/webapp/WEB-INF/app/controllers/api/manual_measures_controller.rb +++ b/sonar-server/src/main/webapp/WEB-INF/app/controllers/api/manual_measures_controller.rb @@ -40,7 +40,7 @@ class Api::ManualMeasuresController < Api::ApiController end respond_to do |format| - format.json { render :json => jsonp(manual_measures_to_json(result)) } + format.json { render :json => jsonp(manual_measures_to_json(resource, result)) } format.xml { render :xml => xml_not_supported } end end @@ -69,7 +69,7 @@ class Api::ManualMeasuresController < Api::ApiController measure.save! respond_to do |format| - format.json { render :json => jsonp(manual_measure_to_json(measure)) } + format.json { render :json => jsonp(manual_measure_to_json(resource, measure)) } format.xml { render :xml => xml_not_supported } end end @@ -91,16 +91,16 @@ class Api::ManualMeasuresController < Api::ApiController private - def manual_measures_to_json(manual_measures) + def manual_measures_to_json(resource, manual_measures) json = [] manual_measures.each do |m| - json< manual_measure.id, :metric => manual_measure.metric.key} + def manual_measure_to_json(resource, manual_measure) + hash={:id => manual_measure.id, :metric => manual_measure.metric.key, :resource => resource.key} hash[:val]=manual_measure.value if manual_measure.value hash[:text]=manual_measure.text_value if manual_measure.text_value hash[:desc]=manual_measure.description if manual_measure.description diff --git a/sonar-ws-client/src/main/java/org/sonar/wsclient/services/ManualMeasure.java b/sonar-ws-client/src/main/java/org/sonar/wsclient/services/ManualMeasure.java new file mode 100644 index 00000000000..a755dfbf438 --- /dev/null +++ b/sonar-ws-client/src/main/java/org/sonar/wsclient/services/ManualMeasure.java @@ -0,0 +1,133 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2008-2011 SonarSource + * 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; + +/** + * @since 2.10 + */ +public class ManualMeasure extends Model { + + private long id; + private String metricKey; + private String resourceKey; + private Double value; + private String textValue; + private Date createdAt; + private Date updatedAt; + private String userLogin; + private String userName; + + public ManualMeasure() { + } + + public long getId() { + return id; + } + + public ManualMeasure setId(long id) { + this.id = id; + return this; + } + + public String getMetricKey() { + return metricKey; + } + + public ManualMeasure setMetricKey(String metricKey) { + this.metricKey = metricKey; + return this; + } + + public Double getValue() { + return value; + } + + public ManualMeasure setValue(Double value) { + this.value = value; + return this; + } + + public String getTextValue() { + return textValue; + } + + public ManualMeasure setTextValue(String textValue) { + this.textValue = textValue; + return this; + } + + public Date getCreatedAt() { + return createdAt; + } + + public ManualMeasure setCreatedAt(Date createdAt) { + this.createdAt = createdAt; + return this; + } + + public Date getUpdatedAt() { + return updatedAt; + } + + public ManualMeasure setUpdatedAt(Date updatedAt) { + this.updatedAt = updatedAt; + return this; + } + + public String getUserLogin() { + return userLogin; + } + + public ManualMeasure setUserLogin(String userLogin) { + this.userLogin = userLogin; + return this; + } + + public String getUserName() { + return userName; + } + + public ManualMeasure setUserName(String userName) { + this.userName = userName; + return this; + } + + public String getResourceKey() { + return resourceKey; + } + + public ManualMeasure setResourceKey(String resourceKey) { + this.resourceKey = resourceKey; + return this; + } + + @Override + public String toString() { + return new StringBuilder().append("Measure{") + .append("id='").append(id).append('\'') + .append("resourceKey='").append(resourceKey).append('\'') + .append("metricKey='").append(metricKey).append('\'') + .append(", value=").append(value) + .append(", textValue='").append(textValue).append('\'') + .append('}').toString(); + } +} diff --git a/sonar-ws-client/src/main/java/org/sonar/wsclient/services/ManualMeasureQuery.java b/sonar-ws-client/src/main/java/org/sonar/wsclient/services/ManualMeasureQuery.java new file mode 100644 index 00000000000..e2db6c2a192 --- /dev/null +++ b/sonar-ws-client/src/main/java/org/sonar/wsclient/services/ManualMeasureQuery.java @@ -0,0 +1,64 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2008-2011 SonarSource + * 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.10 + */ +public final class ManualMeasureQuery extends Query { + public static final String BASE_URL = "/api/manual_measures?"; + + private String resourceKey; + private String metricKey; + + private ManualMeasureQuery(String resourceKey) { + this.resourceKey = resourceKey; + } + + public String getResourceKey() { + return resourceKey; + } + + public String getMetricKey() { + return metricKey; + } + + public ManualMeasureQuery setMetricKey(String s) { + this.metricKey = s; + return this; + } + + @Override + public String getUrl() { + StringBuilder sb = new StringBuilder(BASE_URL); + appendUrlParameter(sb, "resource", resourceKey); + appendUrlParameter(sb, "metric", metricKey); + return sb.toString(); + } + + @Override + public Class getModelClass() { + return ManualMeasure.class; + } + + public static ManualMeasureQuery create(String resourceKey) { + return new ManualMeasureQuery(resourceKey); + } +} diff --git a/sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/ManualMeasureUnmarshaller.java b/sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/ManualMeasureUnmarshaller.java new file mode 100644 index 00000000000..f3b1a2d7bf8 --- /dev/null +++ b/sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/ManualMeasureUnmarshaller.java @@ -0,0 +1,45 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2008-2011 SonarSource + * 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.sonar.wsclient.services.ManualMeasure; +import org.sonar.wsclient.services.WSUtils; + +/** + * @since 2.10 + */ +public class ManualMeasureUnmarshaller extends AbstractUnmarshaller { + + @Override + protected ManualMeasure parse(Object json) { + WSUtils utils = WSUtils.getINSTANCE(); + return new ManualMeasure() + .setId(utils.getLong(json, "id")) + .setMetricKey(utils.getString(json, "metric")) + .setResourceKey(utils.getString(json, "resource")) + .setCreatedAt(utils.getDateTime(json, "created_at")) + .setUpdatedAt(utils.getDateTime(json, "updated_at")) + .setUserLogin(utils.getString(json, "login")) + .setUserName(utils.getString(json, "username")) + .setValue(utils.getDouble(json, "val")) + .setTextValue(utils.getString(json, "text")) + ; + } +} 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 71be7418dcf..5974603e3d4 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 @@ -48,6 +48,7 @@ public final class Unmarshallers { unmarshallers.put(TimeMachine.class, new TimeMachineUnmarshaller()); unmarshallers.put(Profile.class, new ProfileUnmarshaller()); unmarshallers.put(Review.class, new ReviewUnmarshaller()); + unmarshallers.put(ManualMeasure.class, new ManualMeasureUnmarshaller()); } public static Unmarshaller forModel(Class modelClass) { diff --git a/sonar-ws-client/src/test/java/org/sonar/wsclient/services/ManualMeasureQueryTest.java b/sonar-ws-client/src/test/java/org/sonar/wsclient/services/ManualMeasureQueryTest.java new file mode 100644 index 00000000000..dc3c38c071c --- /dev/null +++ b/sonar-ws-client/src/test/java/org/sonar/wsclient/services/ManualMeasureQueryTest.java @@ -0,0 +1,41 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2008-2011 SonarSource + * 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.Test; + +import static org.hamcrest.core.Is.is; +import static org.junit.Assert.assertThat; + +public class ManualMeasureQueryTest extends QueryTestCase { + + @Test + public void shouldGetAllResourceMeasures() { + ManualMeasureQuery query = ManualMeasureQuery.create("foo"); + assertThat(query.getUrl(), is("/api/manual_measures?resource=foo&")); + assertThat(query.getModelClass().getName(), is(ManualMeasure.class.getName())); + } + + @Test + public void shouldFilterMetric() { + ManualMeasureQuery query = ManualMeasureQuery.create("foo").setMetricKey("burned_budget"); + assertThat(query.getUrl(), is("/api/manual_measures?resource=foo&metric=burned_budget&")); + } +} diff --git a/sonar-ws-client/src/test/java/org/sonar/wsclient/unmarshallers/ManualMeasureUnmarshallerTest.java b/sonar-ws-client/src/test/java/org/sonar/wsclient/unmarshallers/ManualMeasureUnmarshallerTest.java new file mode 100644 index 00000000000..cc6667bc4f9 --- /dev/null +++ b/sonar-ws-client/src/test/java/org/sonar/wsclient/unmarshallers/ManualMeasureUnmarshallerTest.java @@ -0,0 +1,59 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2008-2011 SonarSource + * 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.ManualMeasure; +import org.sonar.wsclient.services.Metric; + +import java.util.Collection; +import java.util.List; + +import static org.hamcrest.CoreMatchers.nullValue; +import static org.hamcrest.core.Is.is; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertThat; +import static org.junit.Assert.assertTrue; + +public class ManualMeasureUnmarshallerTest extends UnmarshallerTestCase { + + @Test + public void testSingleMeasure() { + ManualMeasure measure = new ManualMeasureUnmarshaller().toModel("[]"); + assertThat(measure, nullValue()); + + measure = new ManualMeasureUnmarshaller().toModel(loadFile("/manual_measures/single_measure.json")); + assertThat(measure.getId(), is(1L)); + assertThat(measure.getMetricKey(), is("burned_budget")); + assertThat(measure.getResourceKey(), is("org.apache.struts:struts-parent")); + assertThat(measure.getValue(), is(302.5)); + assertThat(measure.getUserLogin(), is("admin")); + assertThat(measure.getUserName(), is("Administrator")); + assertThat(measure.getCreatedAt().getDate(), is(27)); + assertThat(measure.getUpdatedAt().getDate(), is(3)); + } + + + @Test + public void testAllMeasures() { + List measures = new ManualMeasureUnmarshaller().toModels(loadFile("/manual_measures/all_measures.json")); + assertThat(measures.size(), is(2)); + } +} diff --git a/sonar-ws-client/src/test/resources/manual_measures/all_measures.json b/sonar-ws-client/src/test/resources/manual_measures/all_measures.json new file mode 100644 index 00000000000..05bce196c2d --- /dev/null +++ b/sonar-ws-client/src/test/resources/manual_measures/all_measures.json @@ -0,0 +1,22 @@ +[ + { + "id":1, + "metric":"burned_budget", + "resource":"org.apache.struts:struts-parent", + "val":302.0, + "created_at":"2011-07-27T10:22:34+0200", + "updated_at":"2011-07-27T10:22:34+0200", + "login":"admin", + "username":"Administrator" + }, + { + "id":2, + "metric":"team_size", + "resource":"org.apache.struts:struts-parent", + "val":555.0, + "created_at":"2011-07-27T10:53:33+0200", + "updated_at":"2011-07-27T10:53:33+0200", + "login":"admin", + "username":"Administrator" + } +] \ No newline at end of file diff --git a/sonar-ws-client/src/test/resources/manual_measures/single_measure.json b/sonar-ws-client/src/test/resources/manual_measures/single_measure.json new file mode 100644 index 00000000000..587d63bced8 --- /dev/null +++ b/sonar-ws-client/src/test/resources/manual_measures/single_measure.json @@ -0,0 +1,12 @@ +[ + { + "id":1, + "metric":"burned_budget", + "resource":"org.apache.struts:struts-parent", + "val":302.5, + "created_at":"2011-06-27T10:22:34+0200", + "updated_at":"2011-07-03T12:02:12+0200", + "login":"admin", + "username":"Administrator" + } +] \ No newline at end of file -- 2.39.5