import com.google.common.base.Strings;
import java.util.ArrayList;
-import java.util.Collection;
import java.util.List;
-import javax.annotation.CheckForNull;
import javax.annotation.Nullable;
import org.apache.commons.lang.StringUtils;
import org.sonar.api.web.UserRole;
}
}
- public Collection<QualityGateDto> list() {
- try (DbSession dbSession = dbClient.openSession(false)) {
- return dao.selectAll(dbSession);
- }
- }
-
public void delete(long idToDelete) {
checkIsQualityGateAdministrator();
QualityGateDto qGate = getNonNullQgate(idToDelete);
}
}
- @CheckForNull
- public QualityGateDto getDefault() {
- Long defaultId = getDefaultId();
- if (defaultId == null) {
- return null;
- }
- try (DbSession dbSession = dbClient.openSession(false)) {
- return dao.selectById(dbSession, defaultId);
- }
- }
-
public void dissociateProject(DbSession dbSession, ComponentDto project) {
checkProjectAdmin(project);
propertiesDao.deleteProjectProperty(SONAR_QUALITYGATE_PROPERTY, project.getId(), dbSession);
package org.sonar.server.qualitygate.ws;
import com.google.common.io.Resources;
+import javax.annotation.CheckForNull;
+import org.apache.commons.lang.StringUtils;
import org.sonar.api.server.ws.Change;
import org.sonar.api.server.ws.Request;
import org.sonar.api.server.ws.Response;
import org.sonar.api.server.ws.WebService;
import org.sonar.api.utils.text.JsonWriter;
+import org.sonar.db.DbClient;
+import org.sonar.db.DbSession;
+import org.sonar.db.property.PropertyDto;
import org.sonar.db.qualitygate.QualityGateDto;
-import org.sonar.server.qualitygate.QualityGates;
+import static org.sonar.server.qualitygate.QualityGates.SONAR_QUALITYGATE_PROPERTY;
import static org.sonarqube.ws.client.qualitygate.QualityGatesWsParameters.PARAM_ID;
import static org.sonarqube.ws.client.qualitygate.QualityGatesWsParameters.PARAM_NAME;
public class ListAction implements QualityGatesWsAction {
- private final QualityGates qualityGates;
+ private final DbClient dbClient;
- public ListAction(QualityGates qualityGates) {
- this.qualityGates = qualityGates;
+ public ListAction(DbClient dbClient) {
+ this.dbClient = dbClient;
}
@Override
controller.createAction("list")
.setDescription("Get a list of quality gates")
.setSince("4.3")
- .setResponseExample(Resources.getResource(this.getClass(), "example-list.json"))
+ .setResponseExample(Resources.getResource(this.getClass(), "list-example.json"))
.setChangelog(
new Change("7.0", "'isDefault' field is added on quality gate level"),
new Change("7.0", "'default' field on root level is deprecated"))
@Override
public void handle(Request request, Response response) {
- try (JsonWriter writer = response.newJsonWriter()) {
- QualityGateDto defaultQgate = qualityGates.getDefault();
+ try (DbSession dbSession = dbClient.openSession(false);
+ JsonWriter writer = response.newJsonWriter()) {
+ QualityGateDto defaultQgate = getDefault(dbSession);
Long defaultQgateId = defaultQgate == null ? null : defaultQgate.getId();
writer.beginObject().name("qualitygates").beginArray();
- for (QualityGateDto qualityGate : qualityGates.list()) {
+ for (QualityGateDto qualityGate : dbClient.qualityGateDao().selectAll(dbSession)) {
writer.beginObject()
.prop(PARAM_ID, qualityGate.getId())
.prop(PARAM_NAME, qualityGate.getName())
}
writer.endObject().close();
}
+
+ }
+
+ @CheckForNull
+ private QualityGateDto getDefault(DbSession dbSession) {
+ Long defaultId = getDefaultId(dbSession);
+ if (defaultId == null) {
+ return null;
+ }
+ return dbClient.qualityGateDao().selectById(dbSession, defaultId);
+ }
+
+ @CheckForNull
+ private Long getDefaultId(DbSession dbSession) {
+ PropertyDto defaultQgate = dbClient.propertiesDao().selectGlobalProperty(dbSession, SONAR_QUALITYGATE_PROPERTY);
+ if (defaultQgate == null || StringUtils.isBlank(defaultQgate.getValue())) {
+ return null;
+ }
+ return Long.valueOf(defaultQgate.getValue());
}
}
+++ /dev/null
-{
- "qualitygates": [
- {
- "id": 2,
- "name": "QG1"
- },
- {
- "id": 4,
- "name": "QG2"
- }
- ]
-}
--- /dev/null
+{
+ "qualitygates": [
+ {
+ "id": 2,
+ "name": "Sonar way",
+ "isDefault": true
+ },
+ {
+ "id": 4,
+ "name": "Sonar way - Without Coverage",
+ "isDefault": false
+ }
+ ],
+ "default": 2
+}
package org.sonar.server.qualitygate;
import com.google.common.collect.ImmutableList;
-import com.google.common.collect.Lists;
import java.util.Collection;
-import java.util.List;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
userSession.logIn().addPermission(OrganizationPermission.ADMINISTER_QUALITY_GATES, organizationProvider.get().getUuid());
}
- @Test
- public void should_list_qgates() {
- List<QualityGateDto> allQgates = Lists.newArrayList(new QualityGateDto().setName("Gate One"), new QualityGateDto().setName("Gate Two"));
- when(dao.selectAll(dbSession)).thenReturn(allQgates);
- assertThat(underTest.list()).isEqualTo(allQgates);
- }
-
@Test
public void should_rename_qgate() {
long id = QUALITY_GATE_ID;
verify(dao).delete(toDelete, dbSession);
}
- @Test
- public void should_return_default_qgate_if_set() {
- String defaultName = "Sonar way";
- long defaultId = QUALITY_GATE_ID;
- when(propertiesDao.selectGlobalProperty("sonar.qualitygate")).thenReturn(new PropertyDto().setValue(Long.toString(defaultId)));
- QualityGateDto defaultQgate = new QualityGateDto().setId(defaultId).setName(defaultName);
- when(dao.selectById(dbSession, defaultId)).thenReturn(defaultQgate);
- assertThat(underTest.getDefault()).isEqualTo(defaultQgate);
- }
-
- @Test
- public void should_return_null_default_qgate_if_unset() {
- when(propertiesDao.selectGlobalProperty("sonar.qualitygate")).thenReturn(new PropertyDto().setValue(""));
- assertThat(underTest.getDefault()).isNull();
- }
-
@Test
public void should_copy_qgate() {
String name = "Atlantis";
--- /dev/null
+/*
+ * SonarQube
+ * Copyright (C) 2009-2017 SonarSource SA
+ * mailto:info AT sonarsource DOT com
+ *
+ * This program 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.
+ *
+ * This program 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.server.qualitygate.ws;
+
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.sonar.api.server.ws.Change;
+import org.sonar.api.server.ws.WebService;
+import org.sonar.db.DbTester;
+import org.sonar.db.qualitygate.QualityGateDto;
+import org.sonar.server.tester.UserSessionRule;
+import org.sonar.server.ws.WsActionTester;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.AssertionsForClassTypes.tuple;
+import static org.sonar.test.JsonAssert.assertJson;
+
+public class ListActionTest {
+
+ @Rule
+ public ExpectedException expectedException = ExpectedException.none();
+ @Rule
+ public UserSessionRule userSession = UserSessionRule.standalone();
+ @Rule
+ public DbTester db = DbTester.create();
+
+ private WsActionTester ws = new WsActionTester(new ListAction(db.getDbClient()));
+
+ @Test
+ public void verify_definition() {
+ WebService.Action action = ws.getDef();
+ assertThat(action.since()).isEqualTo("4.3");
+ assertThat(action.key()).isEqualTo("list");
+ assertThat(action.isPost()).isFalse();
+ assertThat(action.isInternal()).isFalse();
+ assertThat(action.changelog()).extracting(Change::getVersion, Change::getDescription)
+ .containsExactlyInAnyOrder(
+ tuple("7.0", "'isDefault' field is added on quality gate level"),
+ tuple("7.0", "'default' field on root level is deprecated"));
+ assertThat(action.params()).isEmpty();
+ }
+
+ @Test
+ public void json_example() {
+ QualityGateDto defaultQualityGate = db.qualityGates().insertQualityGate("Sonar way");
+ db.qualityGates().insertQualityGate("Sonar way - Without Coverage");
+ db.qualityGates().setDefaultQualityGate(defaultQualityGate);
+
+ String response = ws.newRequest()
+ .execute()
+ .getInput();
+
+ assertJson(response).ignoreFields("id", "default")
+ .isSimilarTo(getClass().getResource("list-example.json"));
+ }
+
+ @Test
+ public void list_quality_gates() {
+ QualityGateDto defaultQualityGate = db.qualityGates().insertQualityGate("Sonar way");
+ db.qualityGates().insertQualityGate("Sonar way - Without Coverage");
+ db.qualityGates().setDefaultQualityGate(defaultQualityGate);
+
+ String response = ws.newRequest()
+ .execute()
+ .getInput();
+
+ assertJson(response).ignoreFields("id", "default")
+ .isSimilarTo("{\n" +
+ " \"qualitygates\": [\n" +
+ " {\n" +
+ " \"name\": \"Sonar way\",\n" +
+ " \"isDefault\": true\n" +
+ " },\n" +
+ " {\n" +
+ " \"id\": 4,\n" +
+ " \"name\": \"Sonar way - Without Coverage\",\n" +
+ " \"isDefault\": false\n" +
+ " }\n" +
+ " ]\n" +
+ "}\n");
+ }
+
+ @Test
+ public void no_default_quality_gate() {
+ db.qualityGates().insertQualityGate("Sonar way");
+
+ String response = ws.newRequest()
+ .execute()
+ .getInput();
+
+ assertJson(response).ignoreFields("id", "default")
+ .isSimilarTo("{\n" +
+ " \"qualitygates\": [\n" +
+ " {\n" +
+ " \"name\": \"Sonar way\",\n" +
+ " \"isDefault\": false\n" +
+ " }\n" +
+ " ]\n" +
+ "}\n");
+ }
+
+ @Test
+ public void empty() {
+ String response = ws.newRequest()
+ .execute()
+ .getInput();
+
+ assertJson(response).ignoreFields("id", "default")
+ .isSimilarTo("{\n" +
+ " \"qualitygates\": []\n" +
+ "}\n");
+ }
+}
package org.sonar.server.qualitygate.ws;
import com.google.common.collect.ImmutableList;
-import com.google.common.collect.Lists;
import java.util.List;
import org.junit.Before;
import org.junit.Test;
SelectAction selectAction = new SelectAction(mock(DbClient.class), mock(UserSessionRule.class), mock(ComponentFinder.class));
tester = new WsTester(new QualityGatesWs(
- new ListAction(qGates),
new SearchAction(projectFinder),
new CreateAction(null, null, null, null),
new CopyAction(qGates),
assertThat(controller).isNotNull();
assertThat(controller.path()).isEqualTo("api/qualitygates");
assertThat(controller.description()).isNotEmpty();
- assertThat(controller.actions()).hasSize(14);
-
- Action list = controller.action("list");
- assertThat(list).isNotNull();
- assertThat(list.handler()).isNotNull();
- assertThat(list.since()).isEqualTo("4.3");
- assertThat(list.isPost()).isFalse();
- assertThat(list.isInternal()).isFalse();
+ assertThat(controller.actions()).hasSize(13);
Action create = controller.action("create");
assertThat(create).isNotNull();
tester.newPostRequest("api/qualitygates", "destroy").setParam("id", "polop").execute();
}
- @Test
- public void list_nominal() throws Exception {
- when(qGates.list()).thenReturn(Lists.newArrayList(
- new QualityGateDto().setId(42L).setName("Golden"),
- new QualityGateDto().setId(43L).setName("Star"),
- new QualityGateDto().setId(666L).setName("Ninth")));
- tester.newGetRequest("api/qualitygates", "list").execute().assertJson(
- "{\"qualitygates\":[{\"id\":42,\"name\":\"Golden\"},{\"id\":43,\"name\":\"Star\"},{\"id\":666,\"name\":\"Ninth\"}]}");
- }
-
- @Test
- public void list_with_default() throws Exception {
- QualityGateDto defaultQgate = new QualityGateDto().setId(42L).setName("Golden");
- when(qGates.list()).thenReturn(Lists.newArrayList(
- defaultQgate,
- new QualityGateDto().setId(43L).setName("Star"),
- new QualityGateDto().setId(666L).setName("Ninth")));
- when(qGates.getDefault()).thenReturn(defaultQgate);
- tester.newGetRequest("api/qualitygates", "list").execute().assertJson(
- "{\"qualitygates\":[{\"id\":42,\"name\":\"Golden\",\"isDefault\":true},{\"id\":43,\"name\":\"Star\",\"isDefault\":false},{\"id\":666,\"name\":\"Ninth\",\"isDefault\":false}],\"default\":42}");
- }
-
@Test
public void search_with_query() throws Exception {
long gateId = 12345L;