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;
JsonWriter writer = response.newJsonWriter().beginObject();
addPermissions(writer);
addPeriods(writer);
+ addMetrics(writer);
addMessages(writer);
writer.endObject().close();
}
}
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();
+ }
+
+
}
*/
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;
}
}).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)
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();
*/
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;
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;
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 {
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");
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");
}
}
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;
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;
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();
.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'}]}");
- }
}