return newQualityGate;
}
- public QualityGateDto get(Long parseId) {
- return getNonNullQgate(parseId);
+ public QualityGateDto get(Long qGateId) {
+ return getNonNullQgate(qGateId);
+ }
+
+ public QualityGateDto get(String qGateName) {
+ return getNonNullQgate(qGateName);
}
public QualityGateDto rename(long idToRename, String name) {
return qGate;
}
+ private QualityGateDto getNonNullQgate(String name) {
+ QualityGateDto qGate = dao.selectByName(name);
+ if (qGate == null) {
+ throw new NotFoundException("There is no quality gate with name=" + name);
+ }
+ return qGate;
+ }
+
private Metric getNonNullMetric(String metricKey) {
Metric metric = metricFinder.findByKey(metricKey);
if (metric == null) {
public void handle(Request request, Response response) {
show(request, response);
}
- }).newParam(PARAM_ID).setDescription("The ID of the quality gate.");
+ }).newParam(PARAM_ID, "The ID of the quality gate; either this or name must be provided.")
+ .newParam(PARAM_NAME, "The name of the quality gate; either this or id must be provided.");
controller.newAction("destroy")
.setDescription("Destroy a quality gate, given its id.")
}
protected void show(Request request, Response response) {
- final Long qGateId = parseId(request, PARAM_ID);
- QualityGateDto qGate = qualityGates.get(qGateId);
+ 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.");
+ }
+
+ QualityGateDto qGate = qGateId == null ? qualityGates.get(qGateName) : qualityGates.get(qGateId);
+ if (qGateId == null) {
+ qGateId = qGate.getId();
+ }
+
JsonWriter writer = response.newJsonWriter().beginObject()
.prop(PARAM_ID, qGate.getId())
.prop(PARAM_NAME, qGate.getName());
}
@Test
- public void should_get_qgate() throws Exception {
+ public void should_get_qgate_by_id() throws Exception {
long id = 42L;
final String name = "Golden";
QualityGateDto existing = new QualityGateDto().setId(id).setName(name);
verify(dao).selectById(id);
}
+ @Test
+ public void should_get_qgate_by_name() throws Exception {
+ long id = 42L;
+ final String name = "Golden";
+ QualityGateDto existing = new QualityGateDto().setId(id).setName(name);
+ when(dao.selectByName(name)).thenReturn(existing);
+ assertThat(qGates.get(name)).isEqualTo(existing);
+ verify(dao).selectByName(name);
+ }
+
+ @Test(expected = NotFoundException.class)
+ public void should_fail_to_find_qgate_by_name() throws Exception {
+ qGates.get("Does not exist");
+ }
+
@Test
public void should_rename_qgate() throws Exception {
long id = 42L;
}
@Test
- public void show_nominal() throws Exception {
+ public void show_by_id_nominal() throws Exception {
long gateId = 12345L;
when(qGates.get(gateId)).thenReturn(new QualityGateDto().setId(gateId).setName("Golden"));
when(qGates.listConditions(gateId)).thenReturn(ImmutableList.of(
+ "]}");
}
+ @Test
+ public void show_by_name_nominal() throws Exception {
+ long qGateId = 12345L;
+ String gateName = "Golden";
+ when(qGates.get(gateName)).thenReturn(new QualityGateDto().setId(qGateId).setName(gateName));
+ when(qGates.listConditions(qGateId)).thenReturn(ImmutableList.of(
+ new QualityGateConditionDto().setId(1L).setMetricKey("ncloc").setOperator("GT").setErrorThreshold("10000"),
+ new QualityGateConditionDto().setId(2L).setMetricKey("new_coverage").setOperator("LT").setWarningThreshold("90").setPeriod(3)
+ ));
+ tester.newRequest("show").setParam("name", gateName).execute().assertJson(
+ "{'id':12345,'name':'Golden','conditions':["
+ + "{'id':1,'metric':'ncloc','op':'GT','error':'10000'},"
+ + "{'id':2,'metric':'new_coverage','op':'LT','warning':'90','period':3}"
+ + "]}");
+ }
+
+ @Test(expected = BadRequestException.class)
+ public void show_without_parameters() throws Exception {
+ tester.newRequest("show").execute();
+ }
+
+ @Test(expected = BadRequestException.class)
+ public void show_with_both_parameters() throws Exception {
+ tester.newRequest("show").setParam("id", "12345").setParam("name", "Polop").execute();
+ }
+
@Test
public void create_condition_nominal() throws Exception {
long qGateId = 42L;
*/
package org.sonar.wsclient.qualitygate;
-import java.util.Collection;
/**
* @since 4.3
QualityGate rename(long qGateId, String qGateName);
- Collection<QualityGateCondition> conditions(long qGateId);
+ QualityGateDetails show(long qGateId);
+
+ QualityGateDetails show(String qGateName);
void destroy(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 java.util.Collection;
+
+/**
+ * @since 4.3
+ */
+public interface QualityGateDetails extends QualityGate {
+
+ Collection<QualityGateCondition> conditions();
+}
import org.json.simple.JSONValue;
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 org.sonar.wsclient.qualitygate.*;
import java.util.*;
}
@Override
- public Collection<QualityGateCondition> conditions(long qGateId) {
+ public QualityGateDetails show(long qGateId) {
String json = requestFactory.get(SHOW_URL, Collections.singletonMap("id", (Object) qGateId));
- return jsonToConditions(json);
+ return jsonToDetails(json);
+ }
+
+ @Override
+ public QualityGateDetails show(String qGateName) {
+ String json = requestFactory.get(SHOW_URL, Collections.singletonMap("name", (Object) qGateName));
+ return jsonToDetails(json);
}
@Override
return new DefaultQualityGates((Map) jsonRoot);
}
+ @SuppressWarnings({"rawtypes", "unchecked"})
+ private QualityGateDetails jsonToDetails(String json) {
+ Map jsonRoot = (Map) JSONValue.parse(json);
+ return new DefaultQualityGateDetails((Map) jsonRoot, jsonToConditions(json));
+ }
+
@SuppressWarnings({"rawtypes", "unchecked"})
private Collection<QualityGateCondition> jsonToConditions(String json) {
Map jsonRoot = (Map) JSONValue.parse(json);
}
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.qualitygate.QualityGateDetails;
+
+import java.util.Collection;
+import java.util.Map;
+
+public class DefaultQualityGateDetails extends DefaultQualityGate implements QualityGateDetails {
+
+ private final Collection<QualityGateCondition> conditions;
+
+ DefaultQualityGateDetails(Map<String, String> json, Collection<QualityGateCondition> conditions) {
+ super(json);
+ this.conditions = conditions;
+ }
+
+ @Override
+ public Collection<QualityGateCondition> conditions() {
+ return conditions;
+ }
+
+}
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 org.sonar.wsclient.qualitygate.*;
import java.net.HttpURLConnection;
import java.util.Collection;
}
@Test
- public void should_show_qualitygate_conditions() {
+ public void should_show_qualitygate_by_id() {
HttpRequestFactory requestFactory = new HttpRequestFactory(httpServer.url());
httpServer.stubResponseBody("{\"id\":5,\"name\":\"Sonar way\",\"conditions\":["
QualityGateClient client = new DefaultQualityGateClient(requestFactory);
- Collection<QualityGateCondition> conditions = client.conditions(5L);
-
+ QualityGateDetails qGate = client.show(5L);
assertThat(httpServer.requestedPath()).isEqualTo("/api/qualitygates/show?id=5");
+ assertThat(qGate.id()).isEqualTo(5L);
+ assertThat(qGate.name()).isEqualTo("Sonar way");
+
+ Collection<QualityGateCondition> conditions = qGate.conditions();
+ 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_show_qualitygate_by_name() {
+ 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);
+
+ QualityGateDetails qGate = client.show("Sonar way");
+ assertThat(httpServer.requestedPath()).isEqualTo("/api/qualitygates/show?name=Sonar%20way");
+ assertThat(qGate.id()).isEqualTo(5L);
+ assertThat(qGate.name()).isEqualTo("Sonar way");
+
+ Collection<QualityGateCondition> conditions = qGate.conditions();
assertThat(conditions).hasSize(8);
Iterator<QualityGateCondition> condIterator = conditions.iterator();
QualityGateCondition first = condIterator.next();
assertThat(sixth.warningThreshold()).isEqualTo("0");
}
+
@Test
public void should_destroy_qualitygate() {
HttpRequestFactory requestFactory = new HttpRequestFactory(httpServer.url());