]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-2648 New web service /api/manual_measures
authorSimon Brandhof <simon.brandhof@gmail.com>
Wed, 27 Jul 2011 08:59:00 +0000 (10:59 +0200)
committerSimon Brandhof <simon.brandhof@gmail.com>
Wed, 27 Jul 2011 10:12:01 +0000 (12:12 +0200)
sonar-server/src/main/webapp/WEB-INF/app/controllers/api/manual_measures_controller.rb
sonar-ws-client/src/main/java/org/sonar/wsclient/services/ManualMeasure.java [new file with mode: 0644]
sonar-ws-client/src/main/java/org/sonar/wsclient/services/ManualMeasureQuery.java [new file with mode: 0644]
sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/ManualMeasureUnmarshaller.java [new file with mode: 0644]
sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/Unmarshallers.java
sonar-ws-client/src/test/java/org/sonar/wsclient/services/ManualMeasureQueryTest.java [new file with mode: 0644]
sonar-ws-client/src/test/java/org/sonar/wsclient/unmarshallers/ManualMeasureUnmarshallerTest.java [new file with mode: 0644]
sonar-ws-client/src/test/resources/manual_measures/all_measures.json [new file with mode: 0644]
sonar-ws-client/src/test/resources/manual_measures/single_measure.json [new file with mode: 0644]

index 7be0373987ba9c6f3aa54274e10fa86c990d8f3a..704058273fe60590bd44316cc58eac2542d40acd 100644 (file)
@@ -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_to_json(m)
+      json<<manual_measure_to_json(resource, m)
     end
     json
   end
 
-  def manual_measure_to_json(manual_measure)
-    hash={:id => 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 (file)
index 0000000..a755dfb
--- /dev/null
@@ -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 (file)
index 0000000..e2db6c2
--- /dev/null
@@ -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<ManualMeasure> {
+  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<ManualMeasure> 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 (file)
index 0000000..f3b1a2d
--- /dev/null
@@ -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<ManualMeasure> {
+
+  @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"))
+        ;
+  }
+}
index 71be7418dcf6d38b806bf5efcf3dd482d41360c9..5974603e3d4e79c88345e2a6cc1b6b3f9cf15436 100644 (file)
@@ -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 <MODEL extends Model> Unmarshaller<MODEL> forModel(Class<MODEL> 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 (file)
index 0000000..dc3c38c
--- /dev/null
@@ -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 (file)
index 0000000..cc6667b
--- /dev/null
@@ -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<ManualMeasure> 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 (file)
index 0000000..05bce19
--- /dev/null
@@ -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 (file)
index 0000000..587d63b
--- /dev/null
@@ -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