"No quality gate has been found for id %s in organization %s", qualityGateId, organization.getName());
}
+ // TODO As there is always a default quality gate, this method should not return an optional
public Optional<QualityGateDto> getDefault(DbSession dbSession) {
Optional<Long> defaultQualityGateId = getDefaultId(dbSession);
*/
package org.sonar.server.qualitygate.ws;
+import java.util.Optional;
import org.sonar.api.server.ws.Request;
import org.sonar.api.server.ws.Response;
import org.sonar.api.server.ws.WebService;
import org.sonar.db.qualitygate.QualityGateDto;
import org.sonar.server.qualitygate.QualityGateFinder;
+import static com.google.common.base.Preconditions.checkArgument;
+
public class DestroyAction implements QualityGatesWsAction {
private final DbClient dbClient;
try (DbSession dbSession = dbClient.openSession(false)) {
QualityGateDto qualityGate = finder.getById(dbSession, qualityGateId);
+ Optional<QualityGateDto> defaultQualityGate = finder.getDefault(dbSession);
+ checkArgument(!defaultQualityGate.isPresent() || !defaultQualityGate.get().getId().equals(qualityGate.getId()), "The default quality gate cannot be removed");
wsSupport.checkCanEdit(qualityGate);
dbClient.qualityGateDao().delete(qualityGate, dbSession);
}
@Test
- public void fail_when_invalid_id() {
+ public void delete_quality_gate_if_non_default_when_a_default_exist() {
userSession.addPermission(ADMINISTER_QUALITY_GATES, db.getDefaultOrganization());
+ QualityGateDto qualityGate = db.qualityGates().insertQualityGate(qg -> qg.setName("To Delete"));
+ Long toDeleteQualityGateId = qualityGate.getId();
+ assertThat(db.getDbClient().qualityGateDao().selectById(dbSession, toDeleteQualityGateId)).isNotNull();
- String invalidId = "invalid-id";
-
- expectedException.expect(IllegalArgumentException.class);
- expectedException.expectMessage(format("The 'id' parameter cannot be parsed as a long value: %s", invalidId));
+ QualityGateDto defaultQualityGate = db.qualityGates().insertQualityGate(qg -> qg.setName("Default"));
+ db.qualityGates().setDefaultQualityGate(defaultQualityGate);
ws.newRequest()
- .setParam(PARAM_ID, valueOf(invalidId))
+ .setParam(PARAM_ID, valueOf(toDeleteQualityGateId))
.execute();
+
+ assertThat(db.getDbClient().qualityGateDao().selectById(dbSession, toDeleteQualityGateId)).isNull();
}
@Test
- public void should_fail_when_missing_id() {
+ public void does_not_delete_built_in_quality_gate() {
userSession.addPermission(ADMINISTER_QUALITY_GATES, db.getDefaultOrganization());
+ QualityGateDto qualityGate = db.qualityGates().insertQualityGate(qg -> qg.setBuiltIn(true));
+ Long qualityGateId = qualityGate.getId();
expectedException.expect(IllegalArgumentException.class);
+ expectedException.expectMessage(format("Operation forbidden for built-in Quality Gate '%s'", qualityGate.getName()));
ws.newRequest()
- .setParam(PARAM_ID, valueOf(EMPTY))
+ .setParam(PARAM_ID, valueOf(qualityGateId))
.execute();
+
+ assertThat(db.getDbClient().qualityGateDao().selectById(dbSession, qualityGateId)).isNotNull();
}
@Test
- public void delete_quality_gate_even_if_default() {
+ public void fail_when_invalid_id() {
userSession.addPermission(ADMINISTER_QUALITY_GATES, db.getDefaultOrganization());
- QualityGateDto qualityGate = db.qualityGates().insertQualityGate(qg -> qg.setName("To Delete"));
- db.qualityGates().setDefaultQualityGate(qualityGate);
- Long qualityGateId = qualityGate.getId();
+
+ String invalidId = "invalid-id";
+
+ expectedException.expect(IllegalArgumentException.class);
+ expectedException.expectMessage(format("The 'id' parameter cannot be parsed as a long value: %s", invalidId));
ws.newRequest()
- .setParam(PARAM_ID, valueOf(qualityGateId))
+ .setParam(PARAM_ID, valueOf(invalidId))
.execute();
-
- assertThat(db.getDbClient().qualityGateDao().selectById(dbSession, qualityGateId)).isNull();
}
@Test
- public void delete_quality_gate_if_non_default_when_a_default_exist() {
+ public void fail_when_missing_id() {
userSession.addPermission(ADMINISTER_QUALITY_GATES, db.getDefaultOrganization());
- QualityGateDto qualityGate = db.qualityGates().insertQualityGate(qg -> qg.setName("To Delete"));
- Long toDeleteQualityGateId = qualityGate.getId();
- assertThat(db.getDbClient().qualityGateDao().selectById(dbSession, toDeleteQualityGateId)).isNotNull();
- QualityGateDto defaultQualityGate = db.qualityGates().insertQualityGate(qg -> qg.setName("Default"));
- db.qualityGates().setDefaultQualityGate(defaultQualityGate);
+ expectedException.expect(IllegalArgumentException.class);
ws.newRequest()
- .setParam(PARAM_ID, valueOf(toDeleteQualityGateId))
+ .setParam(PARAM_ID, valueOf(EMPTY))
.execute();
-
- assertThat(db.getDbClient().qualityGateDao().selectById(dbSession, toDeleteQualityGateId)).isNull();
}
@Test
- public void does_not_delete_built_in_quality_gate() {
+ public void fail_to_delete_default_quality_gate() {
userSession.addPermission(ADMINISTER_QUALITY_GATES, db.getDefaultOrganization());
- QualityGateDto qualityGate = db.qualityGates().insertQualityGate(qg -> qg.setBuiltIn(true));
+ QualityGateDto qualityGate = db.qualityGates().insertQualityGate(qg -> qg.setName("To Delete"));
+ db.qualityGates().setDefaultQualityGate(qualityGate);
Long qualityGateId = qualityGate.getId();
expectedException.expect(IllegalArgumentException.class);
- expectedException.expectMessage(format("Operation forbidden for built-in Quality Gate '%s'", qualityGate.getName()));
+ expectedException.expectMessage("The default quality gate cannot be removed");
ws.newRequest()
.setParam(PARAM_ID, valueOf(qualityGateId))
.execute();
-
- assertThat(db.getDbClient().qualityGateDao().selectById(dbSession, qualityGateId)).isNotNull();
}
@Test
import org.sonarqube.qa.util.Tester;
import org.sonarqube.qa.util.pageobjects.Navigation;
import org.sonarqube.qa.util.pageobjects.ProjectQualityGatePage;
+import org.sonarqube.ws.Qualitygates;
import org.sonarqube.ws.client.PostRequest;
import org.sonarqube.ws.client.qualitygates.SelectRequest;
@Test
public void should_display_default() {
- QualityGate customQualityGate = createCustomQualityGate("should_display_default");
- qualityGateClient().setDefault(customQualityGate.id());
-
- try {
- ProjectQualityGatePage page = openPage();
- SelenideElement selectedQualityGate = page.getSelectedQualityGate();
- selectedQualityGate.should(Condition.text("Default"));
- selectedQualityGate.should(Condition.text(customQualityGate.name()));
- } finally {
- qualityGateClient().destroy(customQualityGate.id());
- }
+ Qualitygates.CreateResponse customQualityGate = tester.qGates().generate();
+ qualityGateClient().setDefault(customQualityGate.getId());
+
+ ProjectQualityGatePage page = openPage();
+ SelenideElement selectedQualityGate = page.getSelectedQualityGate();
+ selectedQualityGate.should(Condition.text("Default"));
+ selectedQualityGate.should(Condition.text(customQualityGate.getName()));
}
@Test
public void should_display_custom() {
- QualityGate customQualityGate = createCustomQualityGate("should_display_custom");
+ Qualitygates.CreateResponse customQualityGate = tester.qGates().generate();
associateWithQualityGate(customQualityGate);
ProjectQualityGatePage page = openPage();
SelenideElement selectedQualityGate = page.getSelectedQualityGate();
selectedQualityGate.shouldNot(Condition.text("Default"));
- selectedQualityGate.should(Condition.text(customQualityGate.name()));
+ selectedQualityGate.should(Condition.text(customQualityGate.getName()));
}
@Test
public void should_set_custom() {
- QualityGate customQualityGate = createCustomQualityGate("should_set_custom");
+ Qualitygates.CreateResponse customQualityGate = tester.qGates().generate();
ProjectQualityGatePage page = openPage();
- page.setQualityGate(customQualityGate.name());
+ page.setQualityGate(customQualityGate.getName());
SelenideElement selectedQualityGate = page.getSelectedQualityGate();
- selectedQualityGate.should(Condition.text(customQualityGate.name()));
+ selectedQualityGate.should(Condition.text(customQualityGate.getName()));
}
@Test
public void should_set_default() {
- QualityGate customQualityGate = createCustomQualityGate("should_set_default");
- qualityGateClient().setDefault(customQualityGate.id());
-
- try {
- ProjectQualityGatePage page = openPage();
- page.setQualityGate(customQualityGate.name());
-
- SelenideElement selectedQualityGate = page.getSelectedQualityGate();
- selectedQualityGate.should(Condition.text("Default"));
- selectedQualityGate.should(Condition.text(customQualityGate.name()));
- } finally {
- qualityGateClient().destroy(customQualityGate.id());
- }
+ Qualitygates.CreateResponse customQualityGate = tester.qGates().generate();
+ qualityGateClient().setDefault(customQualityGate.getId());
+
+ ProjectQualityGatePage page = openPage();
+ page.setQualityGate(customQualityGate.getName());
+
+ SelenideElement selectedQualityGate = page.getSelectedQualityGate();
+ selectedQualityGate.should(Condition.text("Default"));
+ selectedQualityGate.should(Condition.text(customQualityGate.getName()));
}
private ProjectQualityGatePage openPage() {
return navigation.openProjectQualityGate("sample");
}
- private QualityGate createCustomQualityGate(String name) {
- return qualityGateClient().create(name);
- }
-
- private void associateWithQualityGate(QualityGate qualityGate) {
- tester.wsClient().qualitygates().select(new SelectRequest().setProjectKey("sample").setGateId(String.valueOf(qualityGate.id())));
+ private void associateWithQualityGate(Qualitygates.CreateResponse qualityGate) {
+ tester.wsClient().qualitygates().select(new SelectRequest().setProjectKey("sample").setGateId(String.valueOf(qualityGate.getId())));
}
private QualityGateClient qualityGateClient() {