Skip to content

Commit

Permalink
SONAR-10087 Move some logic from QualityGates to ListAction
Browse files Browse the repository at this point in the history
  • Loading branch information
julienlancelot authored and ehartmann committed Dec 4, 2017
1 parent 69b830f commit c157750
Show file tree
Hide file tree
Showing 7 changed files with 181 additions and 96 deletions.
Expand Up @@ -21,9 +21,7 @@


import com.google.common.base.Strings; import com.google.common.base.Strings;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection;
import java.util.List; import java.util.List;
import javax.annotation.CheckForNull;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import org.sonar.api.web.UserRole; import org.sonar.api.web.UserRole;
Expand Down Expand Up @@ -101,12 +99,6 @@ public QualityGateDto copy(long sourceId, String destinationName) {
} }
} }


public Collection<QualityGateDto> list() {
try (DbSession dbSession = dbClient.openSession(false)) {
return dao.selectAll(dbSession);
}
}

public void delete(long idToDelete) { public void delete(long idToDelete) {
checkIsQualityGateAdministrator(); checkIsQualityGateAdministrator();
QualityGateDto qGate = getNonNullQgate(idToDelete); QualityGateDto qGate = getNonNullQgate(idToDelete);
Expand Down Expand Up @@ -137,17 +129,6 @@ public void setDefault(@Nullable Long idToUseAsDefault) {
} }
} }


@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) { public void dissociateProject(DbSession dbSession, ComponentDto project) {
checkProjectAdmin(project); checkProjectAdmin(project);
propertiesDao.deleteProjectProperty(SONAR_QUALITYGATE_PROPERTY, project.getId(), dbSession); propertiesDao.deleteProjectProperty(SONAR_QUALITYGATE_PROPERTY, project.getId(), dbSession);
Expand Down
Expand Up @@ -20,31 +20,36 @@
package org.sonar.server.qualitygate.ws; package org.sonar.server.qualitygate.ws;


import com.google.common.io.Resources; 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.Change;
import org.sonar.api.server.ws.Request; import org.sonar.api.server.ws.Request;
import org.sonar.api.server.ws.Response; import org.sonar.api.server.ws.Response;
import org.sonar.api.server.ws.WebService; import org.sonar.api.server.ws.WebService;
import org.sonar.api.utils.text.JsonWriter; 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.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_ID;
import static org.sonarqube.ws.client.qualitygate.QualityGatesWsParameters.PARAM_NAME; import static org.sonarqube.ws.client.qualitygate.QualityGatesWsParameters.PARAM_NAME;


public class ListAction implements QualityGatesWsAction { public class ListAction implements QualityGatesWsAction {


private final QualityGates qualityGates; private final DbClient dbClient;


public ListAction(QualityGates qualityGates) { public ListAction(DbClient dbClient) {
this.qualityGates = qualityGates; this.dbClient = dbClient;
} }


@Override @Override
public void define(WebService.NewController controller) { public void define(WebService.NewController controller) {
controller.createAction("list") controller.createAction("list")
.setDescription("Get a list of quality gates") .setDescription("Get a list of quality gates")
.setSince("4.3") .setSince("4.3")
.setResponseExample(Resources.getResource(this.getClass(), "example-list.json")) .setResponseExample(Resources.getResource(this.getClass(), "list-example.json"))
.setChangelog( .setChangelog(
new Change("7.0", "'isDefault' field is added on quality gate level"), new Change("7.0", "'isDefault' field is added on quality gate level"),
new Change("7.0", "'default' field on root level is deprecated")) new Change("7.0", "'default' field on root level is deprecated"))
Expand All @@ -53,11 +58,12 @@ public void define(WebService.NewController controller) {


@Override @Override
public void handle(Request request, Response response) { public void handle(Request request, Response response) {
try (JsonWriter writer = response.newJsonWriter()) { try (DbSession dbSession = dbClient.openSession(false);
QualityGateDto defaultQgate = qualityGates.getDefault(); JsonWriter writer = response.newJsonWriter()) {
QualityGateDto defaultQgate = getDefault(dbSession);
Long defaultQgateId = defaultQgate == null ? null : defaultQgate.getId(); Long defaultQgateId = defaultQgate == null ? null : defaultQgate.getId();
writer.beginObject().name("qualitygates").beginArray(); writer.beginObject().name("qualitygates").beginArray();
for (QualityGateDto qualityGate : qualityGates.list()) { for (QualityGateDto qualityGate : dbClient.qualityGateDao().selectAll(dbSession)) {
writer.beginObject() writer.beginObject()
.prop(PARAM_ID, qualityGate.getId()) .prop(PARAM_ID, qualityGate.getId())
.prop(PARAM_NAME, qualityGate.getName()) .prop(PARAM_NAME, qualityGate.getName())
Expand All @@ -70,6 +76,25 @@ public void handle(Request request, Response response) {
} }
writer.endObject().close(); 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());
} }


} }

This file was deleted.

@@ -0,0 +1,15 @@
{
"qualitygates": [
{
"id": 2,
"name": "Sonar way",
"isDefault": true
},
{
"id": 4,
"name": "Sonar way - Without Coverage",
"isDefault": false
}
],
"default": 2
}
Expand Up @@ -20,9 +20,7 @@
package org.sonar.server.qualitygate; package org.sonar.server.qualitygate;


import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
import java.util.Collection; import java.util.Collection;
import java.util.List;
import org.junit.Before; import org.junit.Before;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
Expand Down Expand Up @@ -93,13 +91,6 @@ public void setUp() {
userSession.logIn().addPermission(OrganizationPermission.ADMINISTER_QUALITY_GATES, organizationProvider.get().getUuid()); 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 @Test
public void should_rename_qgate() { public void should_rename_qgate() {
long id = QUALITY_GATE_ID; long id = QUALITY_GATE_ID;
Expand Down Expand Up @@ -199,22 +190,6 @@ public void should_delete_qgate_even_if_default() {
verify(dao).delete(toDelete, dbSession); 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 @Test
public void should_copy_qgate() { public void should_copy_qgate() {
String name = "Atlantis"; String name = "Atlantis";
Expand Down
@@ -0,0 +1,132 @@
/*
* 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");
}
}
Expand Up @@ -20,7 +20,6 @@
package org.sonar.server.qualitygate.ws; package org.sonar.server.qualitygate.ws;


import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
import java.util.List; import java.util.List;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
Expand Down Expand Up @@ -68,7 +67,6 @@ public void setUp() {
SelectAction selectAction = new SelectAction(mock(DbClient.class), mock(UserSessionRule.class), mock(ComponentFinder.class)); SelectAction selectAction = new SelectAction(mock(DbClient.class), mock(UserSessionRule.class), mock(ComponentFinder.class));


tester = new WsTester(new QualityGatesWs( tester = new WsTester(new QualityGatesWs(
new ListAction(qGates),
new SearchAction(projectFinder), new SearchAction(projectFinder),
new CreateAction(null, null, null, null), new CreateAction(null, null, null, null),
new CopyAction(qGates), new CopyAction(qGates),
Expand All @@ -88,14 +86,7 @@ public void define_ws() {
assertThat(controller).isNotNull(); assertThat(controller).isNotNull();
assertThat(controller.path()).isEqualTo("api/qualitygates"); assertThat(controller.path()).isEqualTo("api/qualitygates");
assertThat(controller.description()).isNotEmpty(); assertThat(controller.description()).isNotEmpty();
assertThat(controller.actions()).hasSize(14); assertThat(controller.actions()).hasSize(13);

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();


Action create = controller.action("create"); Action create = controller.action("create");
assertThat(create).isNotNull(); assertThat(create).isNotNull();
Expand Down Expand Up @@ -226,28 +217,6 @@ public void destroy_with_invalid_id() throws Exception {
tester.newPostRequest("api/qualitygates", "destroy").setParam("id", "polop").execute(); 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 @Test
public void search_with_query() throws Exception { public void search_with_query() throws Exception {
long gateId = 12345L; long gateId = 12345L;
Expand Down

0 comments on commit c157750

Please sign in to comment.