aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJulien Lancelot <julien.lancelot@sonarsource.com>2017-12-06 18:24:52 +0100
committerJulien Lancelot <julien.lancelot@sonarsource.com>2017-12-14 17:03:35 +0100
commit3bb41831cfa64547e4e4b9060d8d4586d2cd8bc2 (patch)
tree7bda460ae43c7fb3d458126ed403a7ccb37881ce
parent3cd91cdaf3f1167677761b476d2d0edb261cdc46 (diff)
downloadsonarqube-3bb41831cfa64547e4e4b9060d8d4586d2cd8bc2.tar.gz
sonarqube-3bb41831cfa64547e4e4b9060d8d4586d2cd8bc2.zip
SONAR-10134 Sanitize SelectActionTest by removing setup method
-rw-r--r--server/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/SelectActionTest.java202
1 files changed, 104 insertions, 98 deletions
diff --git a/server/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/SelectActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/SelectActionTest.java
index 87b27812369..b08a915d318 100644
--- a/server/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/SelectActionTest.java
+++ b/server/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/SelectActionTest.java
@@ -19,20 +19,16 @@
*/
package org.sonar.server.qualitygate.ws;
-import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.sonar.api.utils.System2;
import org.sonar.api.web.UserRole;
-import org.sonar.core.util.Uuids;
import org.sonar.db.DbClient;
-import org.sonar.db.DbSession;
import org.sonar.db.DbTester;
import org.sonar.db.component.ComponentDto;
import org.sonar.db.organization.OrganizationDto;
import org.sonar.db.qualitygate.QualityGateDto;
-import org.sonar.server.component.ComponentFinder;
import org.sonar.server.component.TestComponentFinder;
import org.sonar.server.exceptions.ForbiddenException;
import org.sonar.server.exceptions.NotFoundException;
@@ -41,6 +37,7 @@ import org.sonar.server.ws.WsActionTester;
import static java.lang.String.format;
import static org.assertj.core.api.Assertions.assertThat;
+import static org.sonar.api.web.UserRole.ADMIN;
import static org.sonar.db.permission.OrganizationPermission.ADMINISTER_QUALITY_GATES;
import static org.sonar.server.qualitygate.QualityGates.SONAR_QUALITYGATE_PROPERTY;
@@ -54,184 +51,193 @@ public class SelectActionTest {
public DbTester db = DbTester.create(System2.INSTANCE);
private DbClient dbClient = db.getDbClient();
- private DbSession dbSession = db.getSession();
- private WsActionTester ws;
- private ComponentDto project;
- private QualityGateDto gate;
- private SelectAction underTest;
-
- @Before
- public void setUp() {
- ComponentFinder componentFinder = TestComponentFinder.from(db);
- underTest = new SelectAction(dbClient, userSession, componentFinder);
- ws = new WsActionTester(underTest);
- project = db.components().insertPrivateProject();
- gate = insertQualityGate();
- }
+ private SelectAction underTest = new SelectAction(dbClient, userSession, TestComponentFinder.from(db));
+ private WsActionTester ws = new WsActionTester(underTest);
@Test
- public void select_by_id() throws Exception {
- logInAsRoot();
- String gateId = String.valueOf(gate.getId());
+ public void select_by_id() {
+ userSession.addPermission(ADMINISTER_QUALITY_GATES, db.getDefaultOrganization());
+ QualityGateDto qualityGate = db.qualityGates().insertQualityGate();
+ ComponentDto project = db.components().insertPrivateProject();
- callById(gateId, project.getId());
+ ws.newRequest()
+ .setParam("gateId", qualityGate.getId().toString())
+ .setParam("projectId", project.getId().toString())
+ .execute();
- assertSelected(gateId, project.getId());
+ assertSelected(qualityGate, project);
}
@Test
- public void select_by_uuid() throws Exception {
- logInAsRoot();
- String gateId = String.valueOf(gate.getId());
+ public void select_by_uuid() {
+ userSession.addPermission(ADMINISTER_QUALITY_GATES, db.getDefaultOrganization());
+ QualityGateDto qualityGate = db.qualityGates().insertQualityGate();
+ ComponentDto project = db.components().insertPrivateProject();
- callByUuid(gateId, project.uuid());
+ ws.newRequest()
+ .setParam("gateId", qualityGate.getId().toString())
+ .setParam("projectId", project.uuid())
+ .execute();
- assertSelected(gateId, project.getId());
+ assertSelected(qualityGate, project);
}
@Test
- public void select_by_key() throws Exception {
- logInAsRoot();
- String gateId = String.valueOf(gate.getId());
+ public void select_by_key() {
+ userSession.addPermission(ADMINISTER_QUALITY_GATES, db.getDefaultOrganization());
+ QualityGateDto qualityGate = db.qualityGates().insertQualityGate();
+ ComponentDto project = db.components().insertPrivateProject();
- callByKey(gateId, project.getDbKey());
+ ws.newRequest()
+ .setParam("gateId", qualityGate.getId().toString())
+ .setParam("projectKey", project.getDbKey())
+ .execute();
- assertSelected(gateId, project.getId());
+ assertSelected(qualityGate, project);
}
@Test
- public void project_admin() throws Exception {
- userSession.logIn().addProjectPermission(UserRole.ADMIN, project);
- String gateId = String.valueOf(gate.getId());
+ public void project_admin() {
+ QualityGateDto qualityGate = db.qualityGates().insertQualityGate();
+ ComponentDto project = db.components().insertPrivateProject();
+ userSession.logIn().addProjectPermission(ADMIN, project);
- callByKey(gateId, project.getDbKey());
+ ws.newRequest()
+ .setParam("gateId", qualityGate.getId().toString())
+ .setParam("projectKey", project.getDbKey())
+ .execute();
- assertSelected(gateId, project.getId());
+ assertSelected(qualityGate, project);
}
@Test
- public void gate_administrator_can_associate_a_gate_to_a_project() throws Exception {
+ public void gate_administrator_can_associate_a_gate_to_a_project() {
+ QualityGateDto qualityGate = db.qualityGates().insertQualityGate();
+ ComponentDto project = db.components().insertPrivateProject();
userSession.logIn().addPermission(ADMINISTER_QUALITY_GATES, project.getOrganizationUuid());
- String gateId = String.valueOf(gate.getId());
- callByKey(gateId, project.getDbKey());
+ ws.newRequest()
+ .setParam("gateId", qualityGate.getId().toString())
+ .setParam("projectKey", project.getDbKey())
+ .execute();
- assertSelected(gateId, project.getId());
+ assertSelected(qualityGate, project);
}
@Test
- public void fail_when_no_quality_gate() throws Exception {
+ public void fail_when_no_quality_gate() {
+ userSession.addPermission(ADMINISTER_QUALITY_GATES, db.getDefaultOrganization());
+ ComponentDto project = db.components().insertPrivateProject();
+
expectedException.expect(NotFoundException.class);
- callByKey("1", project.getDbKey());
+
+ ws.newRequest()
+ .setParam("gateId", String.valueOf("1"))
+ .setParam("projectKey", project.getDbKey())
+ .execute();
}
@Test
- public void fail_when_no_project_id() throws Exception {
- String gateId = String.valueOf(gate.getId());
+ public void fail_when_no_project_id() {
+ userSession.addPermission(ADMINISTER_QUALITY_GATES, db.getDefaultOrganization());
+ QualityGateDto qualityGate = db.qualityGates().insertQualityGate();
expectedException.expect(NotFoundException.class);
- callById(gateId, 1L);
+ ws.newRequest()
+ .setParam("gateId", qualityGate.getId().toString())
+ .setParam("projectId", String.valueOf((Long) 1L))
+ .execute();
}
@Test
- public void fail_when_no_project_key() throws Exception {
- String gateId = String.valueOf(gate.getId());
+ public void fail_when_no_project_key() {
+ userSession.addPermission(ADMINISTER_QUALITY_GATES, db.getDefaultOrganization());
+ QualityGateDto qualityGate = db.qualityGates().insertQualityGate();
expectedException.expect(NotFoundException.class);
- callByKey(gateId, "unknown");
+ ws.newRequest()
+ .setParam("gateId", qualityGate.getId().toString())
+ .setParam("projectKey", "unknown")
+ .execute();
}
@Test
- public void fail_when_anonymous() throws Exception {
- String gateId = String.valueOf(gate.getId());
+ public void fail_when_anonymous() {
+ userSession.addPermission(ADMINISTER_QUALITY_GATES, db.getDefaultOrganization());
+ QualityGateDto qualityGate = db.qualityGates().insertQualityGate();
+ ComponentDto project = db.components().insertPrivateProject();
userSession.anonymous();
expectedException.expect(ForbiddenException.class);
- callByKey(gateId, project.getDbKey());
+ ws.newRequest()
+ .setParam("gateId", qualityGate.getId().toString())
+ .setParam("projectKey", project.getDbKey())
+ .execute();
}
@Test
- public void fail_when_not_project_admin() throws Exception {
- String gateId = String.valueOf(gate.getId());
-
+ public void fail_when_not_project_admin() {
+ QualityGateDto qualityGate = db.qualityGates().insertQualityGate();
+ ComponentDto project = db.components().insertPrivateProject();
userSession.logIn().addProjectPermission(UserRole.ISSUE_ADMIN, project);
expectedException.expect(ForbiddenException.class);
- callByKey(gateId, project.getDbKey());
+ ws.newRequest()
+ .setParam("gateId", qualityGate.getId().toString())
+ .setParam("projectKey", project.getDbKey())
+ .execute();
}
@Test
- public void fail_when_not_quality_gates_admin() throws Exception {
- String gateId = String.valueOf(gate.getId());
-
+ public void fail_when_not_quality_gates_admin() {
+ QualityGateDto qualityGate = db.qualityGates().insertQualityGate();
+ ComponentDto project = db.components().insertPrivateProject();
userSession.logIn();
expectedException.expect(ForbiddenException.class);
- callByKey(gateId, project.getDbKey());
+ ws.newRequest()
+ .setParam("gateId", qualityGate.getId().toString())
+ .setParam("projectKey", project.getDbKey())
+ .execute();
}
@Test
public void fail_when_using_branch_db_key() throws Exception {
OrganizationDto organization = db.organizations().insert();
ComponentDto project = db.components().insertMainBranch(organization);
- userSession.logIn().addProjectPermission(UserRole.ADMIN, project);
+ userSession.logIn().addProjectPermission(ADMIN, project);
ComponentDto branch = db.components().insertProjectBranch(project);
- String gateId = String.valueOf(gate.getId());
+ QualityGateDto qualityGate = db.qualityGates().insertQualityGate();
expectedException.expect(NotFoundException.class);
expectedException.expectMessage(format("Component key '%s' not found", branch.getDbKey()));
- callByKey(gateId, branch.getDbKey());
+ ws.newRequest()
+ .setParam("gateId", qualityGate.getId().toString())
+ .setParam("projectKey", branch.getDbKey())
+ .execute();
}
@Test
public void fail_when_using_branch_id() {
OrganizationDto organization = db.organizations().insert();
ComponentDto project = db.components().insertMainBranch(organization);
- userSession.logIn().addProjectPermission(UserRole.ADMIN, project);
+ userSession.logIn().addProjectPermission(ADMIN, project);
ComponentDto branch = db.components().insertProjectBranch(project);
- String gateId = String.valueOf(gate.getId());
+ QualityGateDto qualityGate = db.qualityGates().insertQualityGate();
expectedException.expect(NotFoundException.class);
expectedException.expectMessage(format("Component id '%s' not found", branch.uuid()));
- callByUuid(gateId, branch.uuid());
- }
-
- private QualityGateDto insertQualityGate() {
- QualityGateDto gate = new QualityGateDto().setName("Custom").setUuid(Uuids.createFast());
- dbClient.qualityGateDao().insert(dbSession, gate);
- dbSession.commit();
- return gate;
- }
-
- private void callByKey(String gateId, String projectKey) {
- ws.newRequest()
- .setParam("gateId", String.valueOf(gateId))
- .setParam("projectKey", projectKey)
- .execute();
- }
-
- private void callById(String gateId, Long projectId) {
ws.newRequest()
- .setParam("gateId", String.valueOf(gateId))
- .setParam("projectId", String.valueOf(projectId))
+ .setParam("gateId", qualityGate.getId().toString())
+ .setParam("projectId", branch.uuid())
.execute();
}
- private void callByUuid(String gateId, String projectUuid) {
- ws.newRequest()
- .setParam("gateId", String.valueOf(gateId))
- .setParam("projectId", projectUuid)
- .execute();
+ private void assertSelected(QualityGateDto qualityGate, ComponentDto project) {
+ assertThat(dbClient.propertiesDao().selectProjectProperty(project.getId(), SONAR_QUALITYGATE_PROPERTY).getValue()).isEqualTo(qualityGate.getId().toString());
}
- private void assertSelected(String gateId, Long projectId) {
- assertThat(dbClient.propertiesDao().selectProjectProperty(projectId, SONAR_QUALITYGATE_PROPERTY).getValue()).isEqualTo(gateId);
- }
-
- private void logInAsRoot() {
- userSession.logIn().setRoot();
- }
}