]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-5113 Move the contents of the /api/qualitygates/metrics WS to the app init WS
authorJean-Baptiste Lievremont <jean-baptiste.lievremont@sonarsource.com>
Wed, 5 Mar 2014 17:11:52 +0000 (18:11 +0100)
committerJean-Baptiste Lievremont <jean-baptiste.lievremont@sonarsource.com>
Wed, 5 Mar 2014 17:11:52 +0000 (18:11 +0100)
sonar-server/src/main/java/org/sonar/server/qualitygate/ws/QgateAppHandler.java
sonar-server/src/main/java/org/sonar/server/qualitygate/ws/QualityGatesWs.java
sonar-server/src/test/java/org/sonar/server/qualitygate/ws/QgateAppHandlerTest.java
sonar-server/src/test/java/org/sonar/server/qualitygate/ws/QualityGatesWsTest.java

index 8e74a1118c19cac466e3cbe71203ce3d34949e9a..7da851cab3958ca99b5d84b46695e8bb63802c5b 100644 (file)
@@ -20,6 +20,7 @@
 package org.sonar.server.qualitygate.ws;
 
 import org.sonar.api.i18n.I18n;
+import org.sonar.api.measures.Metric;
 import org.sonar.api.server.ws.Request;
 import org.sonar.api.server.ws.RequestHandler;
 import org.sonar.api.server.ws.Response;
@@ -96,6 +97,7 @@ public class QgateAppHandler implements RequestHandler {
     JsonWriter writer = response.newJsonWriter().beginObject();
     addPermissions(writer);
     addPeriods(writer);
+    addMetrics(writer);
     addMessages(writer);
     writer.endObject().close();
   }
@@ -119,4 +121,20 @@ public class QgateAppHandler implements RequestHandler {
     }
     writer.endObject();
   }
+
+  private void addMetrics(JsonWriter writer) {
+    writer.name("metrics").beginArray();
+    for (Metric metric: qualityGates.gateMetrics()) {
+      writer.beginObject()
+        .prop("id", metric.getId())
+        .prop("key", metric.getKey())
+        .prop("name", metric.getName())
+        .prop("type", metric.getType().toString())
+        .prop("domain", metric.getDomain())
+      .endObject();
+    }
+    writer.endArray();
+  }
+
+
 }
index 74ea268c1d7f91e65251e1ba01543230dff3706f..71ca3ede91f74a06cd29e82fd0e06c8fadbc0fe3 100644 (file)
@@ -19,7 +19,6 @@
  */
 package org.sonar.server.qualitygate.ws;
 
-import org.sonar.api.measures.Metric;
 import org.sonar.api.server.ws.Request;
 import org.sonar.api.server.ws.RequestHandler;
 import org.sonar.api.server.ws.Response;
@@ -200,15 +199,6 @@ public class QualityGatesWs implements WebService {
       }
     }).newParam(PARAM_ID).setDescription("The ID of the quality gate.");
 
-    controller.newAction("metrics")
-    .setDescription("List metrics available for definition of conditions.")
-    .setHandler(new RequestHandler() {
-      @Override
-      public void handle(Request request, Response response) {
-        listMetrics(request, response);
-      }
-    });
-
     controller.newAction("destroy")
     .setDescription("Destroy a quality gate, given its id.")
     .setPost(true)
@@ -262,20 +252,6 @@ public class QualityGatesWs implements WebService {
     writeQualityGate(newQualityGate, writer).close();
   }
 
-  protected void listMetrics(Request request, Response response) {
-    JsonWriter writer = response.newJsonWriter().beginObject().name("metrics").beginArray();
-    for (Metric metric: qualityGates.gateMetrics()) {
-      writer.beginObject()
-        .prop("id", metric.getId())
-        .prop("key", metric.getKey())
-        .prop("name", metric.getName())
-        .prop("type", metric.getType().toString())
-        .prop("domain", metric.getDomain())
-      .endObject();
-    }
-    writer.endArray().endObject().close();
-  }
-
   protected void select(Request request, Response response) {
     qualityGates.associateProject(parseId(request, PARAM_GATE_ID), parseId(request, PARAM_PROJECT_ID));
     response.noContent();
index 5b916d28c579a6638021880092bed34389de2cb5..3a7fdba6415a60e996bf4a8e770c0cbc61ee5139 100644 (file)
@@ -19,6 +19,7 @@
  */
 package org.sonar.server.qualitygate.ws;
 
+import com.google.common.collect.ImmutableList;
 import org.json.simple.JSONValue;
 import org.junit.Before;
 import org.junit.Test;
@@ -28,6 +29,8 @@ import org.mockito.invocation.InvocationOnMock;
 import org.mockito.runners.MockitoJUnitRunner;
 import org.mockito.stubbing.Answer;
 import org.sonar.api.i18n.I18n;
+import org.sonar.api.measures.Metric;
+import org.sonar.api.measures.Metric.ValueType;
 import org.sonar.api.server.ws.WsTester;
 import org.sonar.core.timemachine.Periods;
 import org.sonar.server.qualitygate.QualityGates;
@@ -38,6 +41,8 @@ import java.util.Map.Entry;
 import static org.fest.assertions.Assertions.assertThat;
 import static org.mockito.Matchers.any;
 import static org.mockito.Mockito.doAnswer;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
 
 @RunWith(MockitoJUnitRunner.class)
 public class QgateAppHandlerTest {
@@ -67,7 +72,17 @@ public class QgateAppHandlerTest {
         return (String) invocation.getArguments()[1];
       }
     }).when(i18n).message(any(Locale.class), any(String.class), any(String.class));
+
+    Metric metric = mock(Metric.class);
+    when(metric.getId()).thenReturn(42);
+    when(metric.getKey()).thenReturn("metric");
+    when(metric.getName()).thenReturn("Metric");
+    when(metric.getType()).thenReturn(ValueType.BOOL);
+    when(metric.getDomain()).thenReturn("General");
+    when(qGates.gateMetrics()).thenReturn(ImmutableList.of(metric));
+
     String json = tester.newRequest("app").execute().outputAsString();
+
     Map responseJson = (Map) JSONValue.parse(json);
     assertThat((Boolean) responseJson.get("edit")).isFalse();
     Collection<Map> periods = (Collection<Map>) responseJson.get("periods");
@@ -77,5 +92,8 @@ public class QgateAppHandlerTest {
     for (Entry message: (Set<Entry>) messages.entrySet()) {
       assertThat(message.getKey()).isEqualTo(message.getValue());
     }
+    Collection<Map> metrics = (Collection<Map>) responseJson.get("metrics");
+    assertThat(metrics).hasSize(1);
+    assertThat(metrics.iterator().next().get("key")).isEqualTo("metric");
   }
 }
index e426d2b95cfef8a252226be9d880d08258d30377..2011e67bef690e908e419fcbb4f9606970c59b8a 100644 (file)
@@ -26,8 +26,6 @@ import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.mockito.Mock;
 import org.mockito.runners.MockitoJUnitRunner;
-import org.sonar.api.measures.Metric;
-import org.sonar.api.measures.Metric.ValueType;
 import org.sonar.api.server.ws.WebService.Action;
 import org.sonar.api.server.ws.WebService.Controller;
 import org.sonar.api.server.ws.WsTester;
@@ -40,7 +38,6 @@ import org.sonar.server.qualitygate.QgateProjectFinder;
 import org.sonar.server.qualitygate.QgateProjectFinder.Association;
 import org.sonar.server.qualitygate.QualityGates;
 
-import java.util.Collection;
 import java.util.List;
 
 import static org.fest.assertions.Assertions.assertThat;
@@ -74,7 +71,7 @@ public class QualityGatesWsTest {
     assertThat(controller).isNotNull();
     assertThat(controller.path()).isEqualTo("api/qualitygates");
     assertThat(controller.description()).isNotEmpty();
-    assertThat(controller.actions()).hasSize(16);
+    assertThat(controller.actions()).hasSize(15);
 
     Action list = controller.action("list");
     assertThat(list).isNotNull();
@@ -387,19 +384,4 @@ public class QualityGatesWsTest {
       .assertNoContent();
     verify(qGates).dissociateProject(gateId, projectId);
   }
-
-  @Test
-  public void metrics_nominal() throws Exception {
-    Metric metric = mock(Metric.class);
-    when(metric.getId()).thenReturn(42);
-    when(metric.getKey()).thenReturn("metric");
-    when(metric.getName()).thenReturn("Metric");
-    when(metric.getType()).thenReturn(ValueType.BOOL);
-    when(metric.getDomain()).thenReturn("General");
-    Collection<Metric> metrics = ImmutableList.of(metric);
-    when(qGates.gateMetrics()).thenReturn(metrics);
-    tester.newRequest("metrics")
-      .execute()
-      .assertJson("{'metrics':[{'id':42,'key':'metric','name':'Metric','type':'BOOL','domain':'General'}]}");
-  }
 }