*/
package org.sonar.server.qualitygate;
-import com.google.common.base.Strings;
-import java.util.ArrayList;
-import java.util.List;
import javax.annotation.Nullable;
import org.sonar.api.web.UserRole;
-import org.sonar.core.util.UuidFactory;
import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
import org.sonar.db.component.ComponentDto;
import org.sonar.db.permission.OrganizationPermission;
import org.sonar.db.property.PropertiesDao;
import org.sonar.db.property.PropertyDto;
-import org.sonar.db.qualitygate.QualityGateConditionDao;
-import org.sonar.db.qualitygate.QualityGateConditionDto;
import org.sonar.db.qualitygate.QualityGateDao;
import org.sonar.db.qualitygate.QualityGateDto;
import org.sonar.server.exceptions.NotFoundException;
import org.sonar.server.organization.DefaultOrganizationProvider;
import org.sonar.server.user.UserSession;
-import static java.lang.String.format;
import static org.sonar.server.user.AbstractUserSession.insufficientPrivilegesException;
-import static org.sonar.server.util.Validation.CANT_BE_EMPTY_MESSAGE;
-import static org.sonar.server.util.Validation.IS_ALREADY_USED_MESSAGE;
-import static org.sonar.server.ws.WsUtils.checkRequest;
/**
* Methods from this class should be moved to {@link QualityGateUpdater} and to classes QualityGateFinder / QualityGateConditionsUpdater / etc.
private final DbClient dbClient;
private final QualityGateDao dao;
- private final QualityGateConditionDao conditionDao;
private final PropertiesDao propertiesDao;
private final UserSession userSession;
private final DefaultOrganizationProvider organizationProvider;
- private final UuidFactory uuidFactory;
- public QualityGates(DbClient dbClient, UserSession userSession, DefaultOrganizationProvider organizationProvider, UuidFactory uuidFactory) {
+ public QualityGates(DbClient dbClient, UserSession userSession, DefaultOrganizationProvider organizationProvider) {
this.dbClient = dbClient;
this.dao = dbClient.qualityGateDao();
- this.conditionDao = dbClient.gateConditionDao();
this.propertiesDao = dbClient.propertiesDao();
this.userSession = userSession;
this.organizationProvider = organizationProvider;
- this.uuidFactory = uuidFactory;
- }
-
- public QualityGateDto copy(long sourceId, String destinationName) {
- checkIsQualityGateAdministrator();
- getNonNullQgate(sourceId);
- try (DbSession dbSession = dbClient.openSession(false)) {
- validateQualityGate(dbSession, null, destinationName);
- QualityGateDto destinationGate = new QualityGateDto().setName(destinationName).setBuiltIn(false).setUuid(uuidFactory.create());
- dao.insert(dbSession, destinationGate);
- for (QualityGateConditionDto sourceCondition : conditionDao.selectForQualityGate(dbSession, sourceId)) {
- conditionDao.insert(new QualityGateConditionDto().setQualityGateId(destinationGate.getId())
- .setMetricId(sourceCondition.getMetricId()).setOperator(sourceCondition.getOperator())
- .setWarningThreshold(sourceCondition.getWarningThreshold()).setErrorThreshold(sourceCondition.getErrorThreshold()).setPeriod(sourceCondition.getPeriod()),
- dbSession);
- }
- dbSession.commit();
- return destinationGate;
- }
}
/**
dbSession.commit();
}
- private QualityGateDto getNonNullQgate(long id) {
- try (DbSession dbSession = dbClient.openSession(false)) {
- return getNonNullQgate(dbSession, id);
- }
- }
-
private QualityGateDto getNonNullQgate(DbSession dbSession, long id) {
QualityGateDto qGate = dao.selectById(dbSession, id);
if (qGate == null) {
return qGate;
}
- private void validateQualityGate(DbSession dbSession, @Nullable Long updatingQgateId, @Nullable String name) {
- List<String> errors = new ArrayList<>();
- if (Strings.isNullOrEmpty(name)) {
- errors.add(format(CANT_BE_EMPTY_MESSAGE, "Name"));
- } else {
- checkQgateNotAlreadyExists(dbSession, updatingQgateId, name, errors);
- }
- checkRequest(errors.isEmpty(), errors);
- }
-
- private void checkQgateNotAlreadyExists(DbSession dbSession, @Nullable Long updatingQgateId, String name, List<String> errors) {
- QualityGateDto existingQgate = dao.selectByName(dbSession, name);
- boolean isModifyingCurrentQgate = updatingQgateId != null && existingQgate != null && existingQgate.getId().equals(updatingQgateId);
- if (!isModifyingCurrentQgate && existingQgate != null) {
- errors.add(format(IS_ALREADY_USED_MESSAGE, "Name"));
- }
- }
-
private void checkIsQualityGateAdministrator() {
userSession.checkPermission(OrganizationPermission.ADMINISTER_QUALITY_GATES, organizationProvider.get().getUuid());
}
*/
package org.sonar.server.qualitygate;
-import com.google.common.collect.ImmutableList;
-import java.util.Collection;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.mockito.ArgumentCaptor;
-import org.mockito.Mockito;
-import org.sonar.core.util.UuidFactoryFast;
import org.sonar.core.util.Uuids;
import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
import org.sonar.db.property.PropertiesDao;
import org.sonar.db.property.PropertyDto;
import org.sonar.db.qualitygate.QualityGateConditionDao;
-import org.sonar.db.qualitygate.QualityGateConditionDto;
import org.sonar.db.qualitygate.QualityGateDao;
import org.sonar.db.qualitygate.QualityGateDto;
import org.sonar.server.organization.TestDefaultOrganizationProvider;
import static org.mockito.Matchers.anyLong;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.sonar.db.component.ComponentTesting.newPrivateProjectDto;
when(componentDao.selectOrFailById(eq(dbSession), anyLong())).thenReturn(
newPrivateProjectDto(OrganizationTesting.newOrganizationDto(), PROJECT_UUID).setId(1L).setDbKey(PROJECT_KEY));
- underTest = new QualityGates(dbClient, userSession, organizationProvider, UuidFactoryFast.getInstance());
+ underTest = new QualityGates(dbClient, userSession, organizationProvider);
userSession.logIn().addPermission(OrganizationPermission.ADMINISTER_QUALITY_GATES, organizationProvider.get().getUuid());
}
assertThat(propertyCaptor.getValue().getKey()).isEqualTo("sonar.qualitygate");
assertThat(propertyCaptor.getValue().getValue()).isEqualTo("42");
}
-
- @Test
- public void should_copy_qgate() {
- String name = "Atlantis";
- long sourceId = QUALITY_GATE_ID;
- final long destId = 43L;
- long metric1Id = 1L;
- long metric2Id = 2L;
- QualityGateConditionDto cond1 = new QualityGateConditionDto().setMetricId(metric1Id);
- QualityGateConditionDto cond2 = new QualityGateConditionDto().setMetricId(metric2Id);
- Collection<QualityGateConditionDto> conditions = ImmutableList.of(cond1, cond2);
-
- when(dao.selectById(dbSession, sourceId)).thenReturn(new QualityGateDto().setId(sourceId).setName("SG-1"));
- Mockito.doAnswer(invocation -> {
- ((QualityGateDto) invocation.getArguments()[1]).setId(destId);
- return null;
- }).when(dao).insert(eq(dbSession), any(QualityGateDto.class));
- when(conditionDao.selectForQualityGate(eq(dbSession), anyLong())).thenReturn(conditions);
- QualityGateDto atlantis = underTest.copy(sourceId, name);
- assertThat(atlantis.getName()).isEqualTo(name);
- verify(dao).selectByName(dbSession, name);
- verify(dao).insert(dbSession, atlantis);
- verify(conditionDao).selectForQualityGate(eq(dbSession), anyLong());
- verify(conditionDao, times(conditions.size())).insert(any(QualityGateConditionDto.class), eq(dbSession));
- }
-
}
private DbSession dbSession = db.getSession();
private TestDefaultOrganizationProvider defaultOrganizationProvider = TestDefaultOrganizationProvider.from(db);
private QualityGateUpdater qualityGateUpdater = new QualityGateUpdater(dbClient, UuidFactoryFast.getInstance());
- private QualityGatesWsSupport wsSupport = new QualityGatesWsSupport(dbClient, userSession, defaultOrganizationProvider);
private CopyAction underTest = new CopyAction(dbClient, userSession, defaultOrganizationProvider, qualityGateUpdater);
private WsActionTester ws = new WsActionTester(underTest);
import org.sonar.api.server.ws.WebService;
import org.sonar.api.utils.System2;
import org.sonar.api.web.UserRole;
-import org.sonar.core.util.UuidFactoryFast;
import org.sonar.core.util.Uuids;
import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
private DbClient dbClient = db.getDbClient();
private DbSession dbSession = db.getSession();
private TestDefaultOrganizationProvider organizationProvider = TestDefaultOrganizationProvider.from(db);
- private QualityGates qualityGates = new QualityGates(dbClient, userSession, organizationProvider, UuidFactoryFast.getInstance());
+ private QualityGates qualityGates = new QualityGates(dbClient, userSession, organizationProvider);
private WsActionTester ws;
private ComponentDto project;
private QualityGateDto gate;
import org.sonar.db.DbClient;
import org.sonar.db.qualitygate.ProjectQgateAssociation;
import org.sonar.db.qualitygate.ProjectQgateAssociationQuery;
-import org.sonar.db.qualitygate.QualityGateDto;
import org.sonar.server.component.ComponentFinder;
import org.sonar.server.qualitygate.QgateProjectFinder;
import org.sonar.server.qualitygate.QgateProjectFinder.Association;
assertThat(unsetDefault.isInternal()).isFalse();
}
- @Test
- public void copy_nominal() throws Exception {
- String name = "Copied QG";
- when(qGates.copy(24L, name)).thenReturn(new QualityGateDto().setId(42L).setName(name));
- tester.newPostRequest("api/qualitygates", "copy").setParam("id", "24").setParam("name", name).execute()
- .assertJson("{\"id\":42,\"name\":\"Copied QG\"}");
- }
-
@Test
public void search_with_query() throws Exception {
long gateId = 12345L;