*/
package org.sonar.wsclient.qualitygate;
+import java.util.Collection;
+
/**
* @since 4.3
*/
QualityGate rename(long qGateId, String qGateName);
+ Collection<QualityGateCondition> conditions(long qGateId);
+
void destroy(long qGateId);
void setDefault(long qGateId);
--- /dev/null
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2013 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube 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.
+ *
+ * SonarQube 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 this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+package org.sonar.wsclient.qualitygate;
+
+/**
+ * @since 4.3
+ */
+public interface QualityGateCondition {
+
+ Long id();
+
+ String metricKey();
+
+ String operator();
+
+ String warningThreshold();
+
+ String errorThreshold();
+
+ Integer period();
+}
import org.sonar.wsclient.internal.HttpRequestFactory;
import org.sonar.wsclient.qualitygate.QualityGate;
import org.sonar.wsclient.qualitygate.QualityGateClient;
+import org.sonar.wsclient.qualitygate.QualityGateCondition;
import org.sonar.wsclient.qualitygate.QualityGates;
-import java.util.Collections;
-import java.util.HashMap;
-import java.util.Map;
+import java.util.*;
public class DefaultQualityGateClient implements QualityGateClient {
private static final String ROOT_URL = "/api/qualitygates";
private static final String LIST_URL = ROOT_URL + "/list";
+ private static final String SHOW_URL = ROOT_URL + "/show";
private static final String CREATE_URL = ROOT_URL + "/create";
private static final String RENAME_URL = ROOT_URL + "/rename";
private static final String DESTROY_URL = ROOT_URL + "/destroy";
return jsonToQualityGate(json);
}
+ @Override
+ public Collection<QualityGateCondition> conditions(long qGateId) {
+ String json = requestFactory.get(SHOW_URL, Collections.singletonMap("id", (Object) qGateId));
+ return jsonToConditions(json);
+ }
+
@Override
public void destroy(long qGateId) {
requestFactory.post(DESTROY_URL, Collections.singletonMap("id", (Object) qGateId));
return new DefaultQualityGates((Map) jsonRoot);
}
+ @SuppressWarnings({"rawtypes", "unchecked"})
+ private Collection<QualityGateCondition> jsonToConditions(String json) {
+ Map jsonRoot = (Map) JSONValue.parse(json);
+ Collection<Map> conditionArray = (Collection<Map>) jsonRoot.get("conditions");
+ Collection<QualityGateCondition> conditions = new ArrayList<QualityGateCondition>();
+ for (Map conditionJson: conditionArray) {
+ conditions.add(new DefaultQualityGateCondition(conditionJson));
+ }
+ return conditions;
+ }
}
--- /dev/null
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2013 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube 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.
+ *
+ * SonarQube 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 this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+package org.sonar.wsclient.qualitygate.internal;
+
+import org.sonar.wsclient.qualitygate.QualityGateCondition;
+import org.sonar.wsclient.unmarshallers.JsonUtils;
+
+import java.util.Map;
+
+public class DefaultQualityGateCondition implements QualityGateCondition {
+
+ private Map<String, String> json;
+
+ public DefaultQualityGateCondition(Map<String, String> json) {
+ this.json = json;
+ }
+
+ @Override
+ public Long id() {
+ return JsonUtils.getLong(json, "id");
+ }
+
+ @Override
+ public String metricKey() {
+ return JsonUtils.getString(json, "metric");
+ }
+
+ @Override
+ public String operator() {
+ return JsonUtils.getString(json, "op");
+ }
+
+ @Override
+ public String warningThreshold() {
+ return JsonUtils.getString(json, "warning");
+ }
+
+ @Override
+ public String errorThreshold() {
+ return JsonUtils.getString(json, "error");
+ }
+
+ @Override
+ public Integer period() {
+ return JsonUtils.getInteger(json, "period");
+ }
+
+}
*/
package org.sonar.wsclient.qualitygate.internal;
-import java.net.HttpURLConnection;
-
-import org.sonar.wsclient.qualitygate.QualityGates;
import org.junit.Rule;
import org.junit.Test;
import org.sonar.wsclient.MockHttpServerInterceptor;
import org.sonar.wsclient.internal.HttpRequestFactory;
import org.sonar.wsclient.qualitygate.QualityGate;
import org.sonar.wsclient.qualitygate.QualityGateClient;
+import org.sonar.wsclient.qualitygate.QualityGateCondition;
+import org.sonar.wsclient.qualitygate.QualityGates;
+
+import java.net.HttpURLConnection;
+import java.util.Collection;
+import java.util.Iterator;
+
import static org.fest.assertions.Assertions.assertThat;
import static org.fest.assertions.MapAssert.entry;
entry("name", "Ninth")
);
assertThat(result).isNotNull();
+ assertThat(result.id()).isEqualTo(666L);
+ assertThat(result.name()).isEqualTo("Ninth");
}
@Test
assertThat(result).isNotNull();
}
+ @Test
+ public void should_show_qualitygate_conditions() {
+ HttpRequestFactory requestFactory = new HttpRequestFactory(httpServer.url());
+
+ httpServer.stubResponseBody("{\"id\":5,\"name\":\"Sonar way\",\"conditions\":["
+ + "{\"id\":6,\"metric\":\"blocker_violations\",\"op\":\"GT\",\"warning\":\"\",\"error\":\"0\"},"
+ + "{\"id\":7,\"metric\":\"critical_violations\",\"op\":\"GT\",\"warning\":\"\",\"error\":\"0\"},"
+ + "{\"id\":10,\"metric\":\"test_errors\",\"op\":\"GT\",\"warning\":\"\",\"error\":\"0\"},"
+ + "{\"id\":11,\"metric\":\"test_failures\",\"op\":\"GT\",\"warning\":\"\",\"error\":\"0\"},"
+ + "{\"id\":12,\"metric\":\"new_coverage\",\"op\":\"LT\",\"warning\":\"\",\"error\":\"80%\",\"period\":3},"
+ + "{\"id\":13,\"metric\":\"open_issues\",\"op\":\"GT\",\"warning\":\"0\",\"error\":\"\"},"
+ + "{\"id\":14,\"metric\":\"reopened_issues\",\"op\":\"GT\",\"warning\":\"0\",\"error\":\"\"},"
+ + "{\"id\":15,\"metric\":\"skipped_tests\",\"op\":\"GT\",\"warning\":\"0\",\"error\":\"\"}"
+ + "]}");
+
+ QualityGateClient client = new DefaultQualityGateClient(requestFactory);
+
+ Collection<QualityGateCondition> conditions = client.conditions(5L);
+
+ assertThat(httpServer.requestedPath()).isEqualTo("/api/qualitygates/show?id=5");
+
+ assertThat(conditions).hasSize(8);
+ Iterator<QualityGateCondition> condIterator = conditions.iterator();
+ QualityGateCondition first = condIterator.next();
+ assertThat(first.id()).isEqualTo(6L);
+ QualityGateCondition second = condIterator.next();
+ assertThat(second.period()).isNull();
+ QualityGateCondition third = condIterator.next();
+ assertThat(third.metricKey()).isEqualTo("test_errors");
+ QualityGateCondition fourth = condIterator.next();
+ assertThat(fourth.operator()).isEqualTo("GT");
+ QualityGateCondition fifth = condIterator.next();
+ assertThat(fifth.errorThreshold()).isEqualTo("80%");
+ assertThat(fifth.period()).isEqualTo(3);
+ QualityGateCondition sixth = condIterator.next();
+ assertThat(sixth.warningThreshold()).isEqualTo("0");
+ }
+
@Test
public void should_destroy_qualitygate() {
HttpRequestFactory requestFactory = new HttpRequestFactory(httpServer.url());