public class QualityGateProvider extends ProviderAdapter {
+
private static final Logger LOG = LoggerFactory.getLogger(QualityGateProvider.class);
private static final String PROPERTY_QUALITY_GATE = "sonar.qualitygate";
private static final String SHOW_URL = "/api/qualitygates/show";
+ private static final String ATTRIBUTE_CONDITIONS = "conditions";
+
private QualityGate instance;
public QualityGate provide(Settings settings, ServerClient client, MetricFinder metricFinder) {
QualityGate configuredGate = new QualityGate(root.get("name").getAsString());
- if (root.has("conditions")) {
- for (JsonElement condition: root.get("conditions").getAsJsonArray()) {
+ if (root.has(ATTRIBUTE_CONDITIONS)) {
+ for (JsonElement condition: root.get(ATTRIBUTE_CONDITIONS).getAsJsonArray()) {
JsonObject conditionObject = condition.getAsJsonObject();
configuredGate.add(new ResolvedCondition(conditionObject, metricFinder.findByKey(conditionObject.get("metric").getAsString())));
}
return json.get("op").getAsString();
}
- public @CheckForNull String warningThreshold() {
+ @CheckForNull
+ public String warningThreshold() {
return json.has(ATTRIBUTE_WARNING) ? json.get(ATTRIBUTE_WARNING).getAsString() : null;
}
- public @CheckForNull String errorThreshold() {
+ @CheckForNull
+ public String errorThreshold() {
return json.has(ATTRIBUTE_ERROR) ? json.get(ATTRIBUTE_ERROR).getAsString() : null;
}
- public @CheckForNull Integer period() {
+ @CheckForNull
+ public Integer period() {
return json.has(ATTRIBUTE_PERIOD) ? json.get(ATTRIBUTE_PERIOD).getAsInt() : null;
}
}
defineConditionActions(controller);
+ defineProjectAssociationActions(controller);
+
controller.createAction("app")
.setInternal(true)
.setDescription("Get initialization items for the admin UI. For internal use.")
search.createParam(PARAM_QUERY).setDescription("Optionally, part of the name of the projects to search for.");
search.createParam(PARAM_PAGE);
search.createParam(PARAM_PAGE_SIZE);
+ }
+ private void defineProjectAssociationActions(NewController controller) {
NewAction select = controller.createAction("select")
.setPost(true)
.setHandler(new RequestHandler() {
protected void show(Request request, Response response) {
Long qGateId = request.paramAsLong(PARAM_ID);
String qGateName = request.param(PARAM_NAME);
- if (qGateId == null && qGateName == null) {
- throw new BadRequestException("Either one of 'id' or 'name' is required.");
- } else if (qGateId != null && qGateName != null) {
- throw new BadRequestException("Only one of 'id' or 'name' must be provided.");
- }
+ checkOneOfIdOrNamePresent(qGateId, qGateName);
QualityGateDto qGate = qGateId == null ? qualityGates.get(qGateName) : qualityGates.get(qGateId);
- if (qGateId == null) {
- qGateId = qGate.getId();
- }
+ qGateId = qGate.getId();
JsonWriter writer = response.newJsonWriter().beginObject()
.prop(PARAM_ID, qGate.getId())
writer.endObject().close();
}
+ private void checkOneOfIdOrNamePresent(Long qGateId, String qGateName) {
+ if (qGateId == null && qGateName == null) {
+ throw new BadRequestException("Either one of 'id' or 'name' is required.");
+ } else if (qGateId != null && qGateName != null) {
+ throw new BadRequestException("Only one of 'id' or 'name' must be provided.");
+ }
+ }
+
protected void createCondition(Request request, Response response) {
writeQualityGateCondition(
qualityGates.createCondition(
*/
package org.sonar.wsclient.qualitygate.internal;
+import org.json.simple.JSONArray;
+
import org.sonar.wsclient.unmarshallers.JsonUtils;
import org.sonar.wsclient.qualitygate.QualityGate;
@SuppressWarnings("unchecked")
public DefaultQualityGates(Map<String, Object> json) {
qualityGates = new LinkedHashMap<Long, QualityGate>();
- for (Object entry: JsonUtils.getArray(json, "qualitygates")) {
- QualityGate qGate = new DefaultQualityGate((Map<String, String>) entry);
- qualityGates.put(qGate.id(), qGate);
+ JSONArray gatesJson = JsonUtils.getArray(json, "qualitygates");
+ if (gatesJson != null) {
+ for (Object entry: gatesJson) {
+ QualityGate qGate = new DefaultQualityGate((Map<String, String>) entry);
+ qualityGates.put(qGate.id(), qGate);
+ }
}
defaultId = JsonUtils.getLong(json, "default");
}
HttpRequestFactory requestFactory = new HttpRequestFactory(httpServer.url());
httpServer.stubResponseBody(
- "{\"qualitygates\":[{\"id\":666,\"name\":\"Ninth\"},{\"id\":42,\"name\":\"Golden\"},{\"id\":43,\"name\":\"Star\"}]}");
+ "{\"qualitygates\":[{\"id\":666,\"name\":\"Ninth\"},{\"id\":42,\"name\":\"Golden\"},{\"id\":43,\"name\":\"Star\"}],\"default\":42}");
QualityGateClient client = new DefaultQualityGateClient(requestFactory);
QualityGates qGates = client.list();
assertThat(httpServer.requestedPath()).isEqualTo("/api/qualitygates/list");
assertThat(httpServer.requestParams()).isEmpty();
assertThat(qGates.qualityGates()).hasSize(3);
+ assertThat(qGates.defaultGate().id()).isEqualTo(42L);
+ }
+
+ @Test
+ public void should_list_qualitygates_empty() {
+ HttpRequestFactory requestFactory = new HttpRequestFactory(httpServer.url());
+
+ httpServer.stubResponseBody(
+ "{}");
+
+ QualityGateClient client = new DefaultQualityGateClient(requestFactory);
+ QualityGates qGates = client.list();
+
+ assertThat(httpServer.requestedPath()).isEqualTo("/api/qualitygates/list");
+ assertThat(httpServer.requestParams()).isEmpty();
+ assertThat(qGates.qualityGates()).isEmpty();
assertThat(qGates.defaultGate()).isNull();
}