--- /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;
+
+import javax.annotation.Nullable;
+
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * @since 4.3
+ */
+public class NewCondition {
+
+ private final Map<String, Object> params;
+
+ private NewCondition() {
+ params = new HashMap<String, Object>();
+ }
+
+ public static NewCondition create(long qGateId) {
+ NewCondition newCondition = new NewCondition();
+ newCondition.params.put("gateId", qGateId);
+ return newCondition;
+ }
+
+ public Map<String, Object> urlParams() {
+ return params;
+ }
+
+ public NewCondition metricKey(String metricKey) {
+ params.put("metric", metricKey);
+ return this;
+ }
+
+ public NewCondition operator(String operator) {
+ params.put("op", operator);
+ return this;
+ }
+
+ public NewCondition warningThreshold(@Nullable String warning) {
+ params.put("warning", warning);
+ return this;
+ }
+
+ public NewCondition errorThreshold(@Nullable String error) {
+ params.put("error", error);
+ return this;
+ }
+
+ public NewCondition period(@Nullable Integer period) {
+ params.put("period", period);
+ return this;
+ }
+}
+
QualityGateDetails show(String qGateName);
+ QualityGateCondition createCondition(NewCondition condition);
+
+ QualityGateCondition updateCondition(UpdateCondition condition);
+
+ void deleteCondition(long conditionId);
+
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;
+
+import javax.annotation.Nullable;
+
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * @since 4.3
+ */
+public class UpdateCondition {
+
+ private final Map<String, Object> params;
+
+ private UpdateCondition() {
+ params = new HashMap<String, Object>();
+ }
+
+ public static UpdateCondition create(long id) {
+ UpdateCondition newCondition = new UpdateCondition();
+ newCondition.params.put("id", id);
+ return newCondition;
+ }
+
+ public Map<String, Object> urlParams() {
+ return params;
+ }
+
+ public UpdateCondition metricKey(String metricKey) {
+ params.put("metric", metricKey);
+ return this;
+ }
+
+ public UpdateCondition operator(String operator) {
+ params.put("op", operator);
+ return this;
+ }
+
+ public UpdateCondition warningThreshold(@Nullable String warning) {
+ params.put("warning", warning);
+ return this;
+ }
+
+ public UpdateCondition errorThreshold(@Nullable String error) {
+ params.put("error", error);
+ return this;
+ }
+
+ public UpdateCondition period(@Nullable Integer period) {
+ params.put("period", period);
+ return this;
+ }
+}
+
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 CREATE_CONDITION_URL = ROOT_URL + "/create_condition";
+ private static final String UPDATE_CONDITION_URL = ROOT_URL + "/update_condition";
+ private static final String DELETE_CONDITION_URL = ROOT_URL + "/delete_condition";
private static final String RENAME_URL = ROOT_URL + "/rename";
private static final String DESTROY_URL = ROOT_URL + "/destroy";
private static final String SET_DEFAULT_URL = ROOT_URL + "/set_as_default";
return jsonToDetails(json);
}
+ @Override
+ public QualityGateCondition createCondition(NewCondition condition) {
+ String json = requestFactory.post(CREATE_CONDITION_URL, condition.urlParams());
+ return jsonToCondition(json);
+ }
+
+ @Override
+ public QualityGateCondition updateCondition(UpdateCondition condition) {
+ String json = requestFactory.post(UPDATE_CONDITION_URL, condition.urlParams());
+ return jsonToCondition(json);
+ }
+
+ @Override
+ public void deleteCondition(long conditionId) {
+ requestFactory.post(DELETE_CONDITION_URL, Collections.singletonMap("id", (Object) conditionId));
+ }
+
@Override
public void destroy(long qGateId) {
requestFactory.post(DESTROY_URL, Collections.singletonMap("id", (Object) qGateId));
return conditions;
}
-
+ @SuppressWarnings({"rawtypes", "unchecked"})
+ private DefaultQualityGateCondition jsonToCondition(String json) {
+ return new DefaultQualityGateCondition((Map) JSONValue.parse(json));
+ }
}
assertThat(httpServer.requestedPath()).isEqualTo("/api/qualitygates/unset_default");
assertThat(httpServer.requestParams()).isEmpty();
}
+
+ @Test
+ public void should_create_condition_on_qualitygate() {
+ HttpRequestFactory requestFactory = new HttpRequestFactory(httpServer.url());
+
+ httpServer.stubResponseBody("{\"id\":42,\"metric\":\"new_coverage\",\"op\":\"LT\",\"warning\":\"90\",\"error\":\"80\",\"period\":3}");
+
+ QualityGateClient client = new DefaultQualityGateClient(requestFactory);
+ QualityGateCondition result = client.createCondition(NewCondition.create(12345L)
+ .metricKey("new_coverage").operator("LT").warningThreshold("90").errorThreshold("80").period(3));
+
+ assertThat(httpServer.requestedPath()).isEqualTo("/api/qualitygates/create_condition");
+ assertThat(httpServer.requestParams()).includes(
+ entry("gateId", "12345"),
+ entry("metric", "new_coverage"),
+ entry("op", "LT"),
+ entry("warning", "90"),
+ entry("error", "80"),
+ entry("period", "3")
+ );
+ assertThat(result).isNotNull();
+ assertThat(result.id()).isEqualTo(42L);
+ }
+
+ @Test
+ public void should_update_condition() {
+ HttpRequestFactory requestFactory = new HttpRequestFactory(httpServer.url());
+
+ httpServer.stubResponseBody("{\"id\":12345,\"metric\":\"ncloc\",\"op\":\"GT\",\"warning\":\"1000\",\"error\":\"2000\",\"period\":1}");
+
+ QualityGateClient client = new DefaultQualityGateClient(requestFactory);
+ QualityGateCondition result = client.updateCondition(UpdateCondition.create(12345L)
+ .metricKey("ncloc").operator("GT").warningThreshold("1000").errorThreshold("2000").period(1));
+
+ assertThat(httpServer.requestedPath()).isEqualTo("/api/qualitygates/update_condition");
+ assertThat(httpServer.requestParams()).includes(
+ entry("id", "12345"),
+ entry("metric", "ncloc"),
+ entry("op", "GT"),
+ entry("warning", "1000"),
+ entry("error", "2000"),
+ entry("period", "1")
+ );
+ assertThat(result).isNotNull();
+ assertThat(result.id()).isEqualTo(12345L);
+ }
+
+ @Test
+ public void should_delete_condition() {
+ HttpRequestFactory requestFactory = new HttpRequestFactory(httpServer.url());
+
+ httpServer.stubStatusCode(HttpURLConnection.HTTP_NO_CONTENT);
+
+ QualityGateClient client = new DefaultQualityGateClient(requestFactory);
+ client.deleteCondition(666L);;
+
+ assertThat(httpServer.requestedPath()).isEqualTo("/api/qualitygates/delete_condition");
+ assertThat(httpServer.requestParams()).includes(
+ entry("id", "666")
+ );
+ }
}