package org.sonar.ce.task.projectanalysis.qualitygate;
import java.util.Optional;
-import org.sonar.ce.task.projectanalysis.analysis.Organization;
import org.sonar.server.project.Project;
public interface QualityGateService {
Optional<QualityGate> findByUuid(String uuid);
/**
- * Retrieve the {@link QualityGate} from the database using organization.
+ * Retrieve the {@link QualityGate} from the database.
* @throws IllegalStateException if database is corrupted and default gate can't be found.
*/
- QualityGate findDefaultQualityGate(Organization organizationDto);
+ QualityGate findDefaultQualityGate();
/**
* Retrieve the {@link QualityGate} from the database associated with project.
import java.util.Collection;
import java.util.Objects;
import java.util.Optional;
-import org.sonar.ce.task.projectanalysis.analysis.Organization;
import org.sonar.ce.task.projectanalysis.metric.MetricRepository;
import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
+import org.sonar.db.property.PropertyDto;
import org.sonar.db.qualitygate.QualityGateConditionDto;
import org.sonar.db.qualitygate.QualityGateDto;
import org.sonar.server.project.Project;
import static org.sonar.core.util.stream.MoreCollectors.toList;
public class QualityGateServiceImpl implements QualityGateService {
+ private static final String DEFAULT_QUALITY_GATE_PROPERTY_NAME = "qualitygate.default";
private final DbClient dbClient;
private final MetricRepository metricRepository;
}
@Override
- public QualityGate findDefaultQualityGate(Organization organization) {
+ public QualityGate findDefaultQualityGate() {
try (DbSession dbSession = dbClient.openSession(false)) {
- QualityGateDto qualityGateDto = dbClient.qualityGateDao().selectByOrganizationAndUuid(dbSession, organization.toDto(), organization.getDefaultQualityGateUuid());
+ QualityGateDto qualityGateDto = getDefaultQualityGate(dbSession);
if (qualityGateDto == null) {
- throw new IllegalStateException("The default Quality gate is missing on organization " + organization.getKey());
+ throw new IllegalStateException("The default Quality gate is missing");
}
return toQualityGate(dbSession, qualityGateDto);
}
}
+ private QualityGateDto getDefaultQualityGate(DbSession dbSession) {
+ PropertyDto propertyDto = Optional.ofNullable(dbClient.propertiesDao()
+ .selectGlobalProperty(dbSession, DEFAULT_QUALITY_GATE_PROPERTY_NAME))
+ .orElseThrow(() -> new IllegalStateException("The default Quality Gate property is missing"));
+ return Optional.ofNullable(dbClient.qualityGateDao().selectByUuid(dbSession, propertyDto.getValue()))
+ .orElseThrow(() -> new IllegalStateException("The default Quality gate is missing"));
+ }
+
@Override
public Optional<QualityGate> findQualityGate(Project project) {
try (DbSession dbSession = dbClient.openSession(false)) {
public void execute(ComputationStep.Context context) {
Optional<QualityGate> qualityGate = getProjectQualityGate();
if (!qualityGate.isPresent()) {
- // No QG defined for the project, let's retrieve the QG on the organization
- qualityGate = Optional.of(getOrganizationDefaultQualityGate());
+ // No QG defined for the project, let's retrieve the default QG
+ qualityGate = Optional.of(getDefaultQualityGate());
}
if (analysisMetadataHolder.isPullRequest()) {
return qualityGateService.findQualityGate(project);
}
- private QualityGate getOrganizationDefaultQualityGate() {
- return qualityGateService.findDefaultQualityGate(analysisMetadataHolder.getOrganization());
+ private QualityGate getDefaultQualityGate() {
+ return qualityGateService.findDefaultQualityGate();
}
@Override
import java.util.Optional;
import org.junit.Before;
import org.junit.Test;
-import org.sonar.ce.task.projectanalysis.analysis.Organization;
import org.sonar.ce.task.projectanalysis.metric.Metric;
import org.sonar.ce.task.projectanalysis.metric.MetricRepository;
import org.sonar.db.DbClient;
-import org.sonar.db.qualitygate.QGateWithOrgDto;
+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;
private static final QualityGateConditionDto CONDITION_2 = new QualityGateConditionDto().setUuid("456").setMetricUuid(METRIC_UUID_2).setOperator("GT")
.setErrorThreshold("error_th");
- private QualityGateDao qualityGateDao = mock(QualityGateDao.class);
- private QualityGateConditionDao qualityGateConditionDao = mock(QualityGateConditionDao.class);
- private MetricRepository metricRepository = mock(MetricRepository.class);
- private DbClient dbClient = mock(DbClient.class);
- private QualityGateServiceImpl underTest = new QualityGateServiceImpl(dbClient, metricRepository);
+ private final QualityGateDao qualityGateDao = mock(QualityGateDao.class);
+ private final QualityGateConditionDao qualityGateConditionDao = mock(QualityGateConditionDao.class);
+ private final PropertiesDao propertiesDao = mock(PropertiesDao.class);
+ private final MetricRepository metricRepository = mock(MetricRepository.class);
+ private final DbClient dbClient = mock(DbClient.class);
+ private final QualityGateServiceImpl underTest = new QualityGateServiceImpl(dbClient, metricRepository);
@Before
public void setUp() {
when(dbClient.qualityGateDao()).thenReturn(qualityGateDao);
when(dbClient.gateConditionDao()).thenReturn(qualityGateConditionDao);
+ when(dbClient.propertiesDao()).thenReturn(propertiesDao);
when(METRIC_1.getKey()).thenReturn("metric");
when(METRIC_2.getKey()).thenReturn("new_metric");
}
@Test(expected = IllegalStateException.class)
- public void findDefaultQualityGate_by_organization_not_found() {
- when(qualityGateDao.selectByOrganizationAndUuid(any(), any(), any())).thenReturn(null);
+ public void findDefaultQualityGate_by_property_not_found() {
+ when(propertiesDao.selectGlobalProperty(any(), any())).thenReturn(null);
- underTest.findDefaultQualityGate(mock(Organization.class));
+ underTest.findDefaultQualityGate();
}
@Test
- public void findDefaultQualityGate_by_organization_found() {
- QGateWithOrgDto qGateWithOrgDto = new QGateWithOrgDto();
- qGateWithOrgDto.setUuid(QUALITY_GATE_DTO.getUuid());
- qGateWithOrgDto.setName(QUALITY_GATE_DTO.getName());
- when(qualityGateDao.selectByOrganizationAndUuid(any(), any(), any())).thenReturn(qGateWithOrgDto);
+ public void findDefaultQualityGate_by_property_found() {
+ QualityGateDto qualityGateDto = new QualityGateDto();
+ qualityGateDto.setUuid(QUALITY_GATE_DTO.getUuid());
+ qualityGateDto.setName(QUALITY_GATE_DTO.getName());
+ when(propertiesDao.selectGlobalProperty(any(), any())).thenReturn(new PropertyDto().setValue(QUALITY_GATE_DTO.getUuid()));
+
+ when(qualityGateDao.selectByUuid(any(), any())).thenReturn(qualityGateDto);
when(qualityGateConditionDao.selectForQualityGate(any(), eq(SOME_UUID))).thenReturn(ImmutableList.of(CONDITION_1, CONDITION_2));
when(metricRepository.getOptionalByUuid(METRIC_UUID_1)).thenReturn(Optional.empty());
when(metricRepository.getOptionalByUuid(METRIC_UUID_2)).thenReturn(Optional.of(METRIC_2));
- QualityGate result = underTest.findDefaultQualityGate(mock(Organization.class));
+ QualityGate result = underTest.findDefaultQualityGate();
assertThat(result).isNotNull();
assertThat(result.getUuid()).isEqualTo(QUALITY_GATE_DTO.getUuid());
@Test
public void findQualityGate_by_project_found() {
- QGateWithOrgDto qGateWithOrgDto = new QGateWithOrgDto();
- qGateWithOrgDto.setUuid(QUALITY_GATE_DTO.getUuid());
- qGateWithOrgDto.setName(QUALITY_GATE_DTO.getName());
- when(qualityGateDao.selectByProjectUuid(any(), any())).thenReturn(qGateWithOrgDto);
+ QualityGateDto qualityGateDto = new QualityGateDto();
+ qualityGateDto.setUuid(QUALITY_GATE_DTO.getUuid());
+ qualityGateDto.setName(QUALITY_GATE_DTO.getName());
+ when(qualityGateDao.selectByProjectUuid(any(), any())).thenReturn(qualityGateDto);
when(qualityGateConditionDao.selectForQualityGate(any(), eq(SOME_UUID))).thenReturn(ImmutableList.of(CONDITION_1, CONDITION_2));
when(metricRepository.getOptionalByUuid(METRIC_UUID_1)).thenReturn(Optional.empty());
when(metricRepository.getOptionalByUuid(METRIC_UUID_2)).thenReturn(Optional.of(METRIC_2));
@Rule
public MutableQualityGateHolderRule mutableQualityGateHolder = new MutableQualityGateHolderRule();
- private AnalysisMetadataHolder analysisMetadataHolder = mock(AnalysisMetadataHolder.class);
- private QualityGateService qualityGateService = mock(QualityGateService.class);
+ private final AnalysisMetadataHolder analysisMetadataHolder = mock(AnalysisMetadataHolder.class);
+ private final QualityGateService qualityGateService = mock(QualityGateService.class);
- private LoadQualityGateStep underTest = new LoadQualityGateStep(qualityGateService, mutableQualityGateHolder, analysisMetadataHolder);
+ private final LoadQualityGateStep underTest = new LoadQualityGateStep(qualityGateService, mutableQualityGateHolder, analysisMetadataHolder);
@Before
public void setUp() {
when(analysisMetadataHolder.isPullRequest()).thenReturn(true);
QualityGate defaultGate = new QualityGate("1", "qg", Arrays.asList(variation, condition));
- when(qualityGateService.findDefaultQualityGate(any(Organization.class))).thenReturn(defaultGate);
+ when(qualityGateService.findDefaultQualityGate()).thenReturn(defaultGate);
underTest.execute(new TestComputationStepContext());
@Test
public void execute_sets_default_QualityGate_when_project_has_no_settings() {
QualityGate defaultGate = mock(QualityGate.class);
- when(qualityGateService.findDefaultQualityGate(any(Organization.class))).thenReturn(defaultGate);
+ when(qualityGateService.findDefaultQualityGate()).thenReturn(defaultGate);
underTest.execute(new TestComputationStepContext());
import org.sonar.db.alm.ALM;
import org.sonar.db.component.BranchType;
import org.sonar.db.permission.template.DefaultTemplates;
-import org.sonar.db.qualitygate.QGateWithOrgDto;
import org.sonar.db.user.GroupDto;
import static java.util.Objects.requireNonNull;
getMapper(dbSession).updateDefaultGroupUuid(uuid, requireNonNull(defaultGroupUuid, "Default group uuid cannot be null"), system2.now());
}
- public void setDefaultQualityGate(DbSession dbSession, OrganizationDto organization, QGateWithOrgDto qualityGate) {
- getMapper(dbSession).updateDefaultQualityGate(organization.getUuid(), qualityGate.getUuid(), system2.now());
- }
-
public int update(DbSession dbSession, OrganizationDto organization) {
checkDto(organization);
organization.setUpdatedAt(system2.now());
public static final Set<String> AVAILABLE_MEMBERSHIP = ImmutableSet.of(ANY, IN, OUT);
private final String gateUuid;
- private final String organizationUuid;
private final String membership;
private final String projectSearch;
private ProjectQgateAssociationQuery(Builder builder) {
this.gateUuid = builder.qualityGate.getUuid();
- this.organizationUuid = builder.qualityGate.getOrganizationUuid();
this.membership = builder.membership;
this.projectSearch = builder.projectSearch;
if (this.projectSearch == null) {
return gateUuid;
}
- public String organizationUuid() {
- return organizationUuid;
- }
-
@CheckForNull
public String membership() {
return membership;
}
public static class Builder {
- private QGateWithOrgDto qualityGate;
+ private QualityGateDto qualityGate;
private String membership;
private String projectSearch;
private Builder() {
}
- public Builder qualityGate(QGateWithOrgDto qualityGate) {
+ public Builder qualityGate(QualityGateDto qualityGate) {
this.qualityGate = qualityGate;
return this;
}
import org.sonar.db.DatabaseUtils;
import org.sonar.db.DbSession;
import org.sonar.db.organization.OrganizationDto;
+import org.sonar.db.property.InternalPropertiesDao;
public class QualityGateDao implements Dao {
+ private static final String DEFAULT_ORGANIZATION_PROPERTY_KEY = "organization.default";
private final UuidFactory uuidFactory;
+ private final InternalPropertiesDao internalPropertiesDao;
- public QualityGateDao(UuidFactory uuidFactory) {
+ public QualityGateDao(UuidFactory uuidFactory, InternalPropertiesDao internalPropertiesDao) {
this.uuidFactory = uuidFactory;
+ this.internalPropertiesDao = internalPropertiesDao;
}
public QualityGateDto insert(DbSession session, QualityGateDto newQualityGate) {
return newQualityGate;
}
+ /**
+ * @deprecated drop when org are dropped
+ */
+ @Deprecated
public void associate(DbSession dbSession, String uuid, OrganizationDto organization, QualityGateDto qualityGate) {
mapper(dbSession).insertOrgQualityGate(uuid, organization.getUuid(), qualityGate.getUuid());
}
- public Collection<QualityGateDto> selectAll(DbSession session, OrganizationDto organization) {
- return mapper(session).selectAll(organization.getUuid());
+ public void associate(DbSession dbSession, String uuid, QualityGateDto qualityGate) {
+ String defaultOrganizationUuid = getDefaultOrganizationUuid(dbSession);
+ mapper(dbSession).insertOrgQualityGate(uuid, defaultOrganizationUuid, qualityGate.getUuid());
+ }
+
+ public Collection<QualityGateDto> selectAll(DbSession session) {
+ String defaultOrganizationUuid = getDefaultOrganizationUuid(session);
+ return selectAll(session, defaultOrganizationUuid);
+ }
+
+ /**
+ * @deprecated drop when org are dropped
+ */
+ @Deprecated
+ public Collection<QualityGateDto> selectAll(DbSession session, String organizationUuid) {
+ return mapper(session).selectAll(organizationUuid);
}
@CheckForNull
}
@CheckForNull
- public QGateWithOrgDto selectByOrganizationAndUuid(DbSession dbSession, OrganizationDto organization, String qualityGateUuid) {
- return mapper(dbSession).selectByUuidAndOrganization(qualityGateUuid, organization.getUuid());
+ public QGateWithOrgDto selectByDefaultOrganizationAndUuid(DbSession dbSession, String qualityGateUuid) {
+ String defaultOrganizationUuid = getDefaultOrganizationUuid(dbSession);
+ return mapper(dbSession).selectByUuidAndOrganization(qualityGateUuid, defaultOrganizationUuid);
}
+ /**
+ * @deprecated drop when org are dropped
+ */
@CheckForNull
- public QGateWithOrgDto selectByOrganizationAndName(DbSession session, OrganizationDto organization, String name) {
- return mapper(session).selectByNameAndOrganization(name, organization.getUuid());
+ @Deprecated
+ public QGateWithOrgDto selectByOrganizationAndUuid(DbSession dbSession, OrganizationDto organization, String qualityGateUuid) {
+ return mapper(dbSession).selectByUuidAndOrganization(qualityGateUuid, organization.getUuid());
}
- public QGateWithOrgDto selectDefault(DbSession dbSession, OrganizationDto organization) {
- return mapper(dbSession).selectDefault(organization.getUuid());
+ @CheckForNull
+ public QGateWithOrgDto selectByDefaultOrganizationAndName(DbSession session, String name) {
+ String defaultOrganizationUuid = getDefaultOrganizationUuid(session);
+ return mapper(session).selectByNameAndOrganization(name, defaultOrganizationUuid);
}
public void delete(QualityGateDto qGate, DbSession session) {
public QualityGateDto selectByProjectUuid(DbSession dbSession, String projectUuid) {
return mapper(dbSession).selectByProjectUuid(projectUuid);
}
+
+ private String getDefaultOrganizationUuid(DbSession dbSession) {
+ return internalPropertiesDao.selectByKey(dbSession, DEFAULT_ORGANIZATION_PROPERTY_KEY)
+ .orElseThrow(() -> new IllegalStateException("Default organization does not exist."));
+ }
}
and proj.enabled = ${_true}
and proj.main_branch_project_uuid is null
and proj.copy_component_uuid is null
- and proj.organization_uuid=#{query.organizationUuid, jdbcType=VARCHAR}
<choose>
<when test="query.membership() == 'selected'">
and qg.uuid IS NOT NULL
is_built_in = ${_true}
</select>
- <select id="selectDefault" resultType="org.sonar.db.qualitygate.QGateWithOrgDto">
- SELECT
- <include refid="qateWithOrgColumns"/>
- FROM quality_gates qg
- INNER JOIN org_quality_gates oqg ON oqg.quality_gate_uuid = qg.uuid
- INNER JOIN organizations o ON o.default_quality_gate_uuid = qg.uuid AND o.uuid=#{organizationUuid, jdbcType=VARCHAR}
- </select>
-
<update id="delete" parameterType="String">
delete from quality_gates where uuid=#{uuid}
</update>
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.project.ProjectDto;
import static org.assertj.core.api.Assertions.assertThat;
@Test
public void select_all_projects_by_query() {
- OrganizationDto organization = db.organizations().insert();
- QGateWithOrgDto qualityGate1 = db.qualityGates().insertQualityGate(organization);
- QGateWithOrgDto qualityGate2 = db.qualityGates().insertQualityGate(organization);
- ComponentDto project1 = db.components().insertPrivateProject(organization);
- ComponentDto project2 = db.components().insertPrivateProject(organization);
- ComponentDto project3 = db.components().insertPrivateProject(organization);
+ QualityGateDto qualityGate1 = db.qualityGates().insertQualityGate();
+ QualityGateDto qualityGate2 = db.qualityGates().insertQualityGate();
+ ComponentDto project1 = db.components().insertPrivateProject();
+ ComponentDto project2 = db.components().insertPrivateProject();
+ ComponentDto project3 = db.components().insertPrivateProject();
db.qualityGates().associateProjectToQualityGate(db.components().getProjectDto(project1), qualityGate1);
db.qualityGates().associateProjectToQualityGate(db.components().getProjectDto(project2), qualityGate1);
db.qualityGates().associateProjectToQualityGate(db.components().getProjectDto(project3), qualityGate2);
@Test
public void select_all_projects_by_query_should_have_deterministic_order() {
- OrganizationDto organization = db.organizations().insert();
- QGateWithOrgDto qualityGate1 = db.qualityGates().insertQualityGate(organization);
- ComponentDto project1 = db.components().insertPrivateProject(organization, d -> d.setName("p1").setDbKey("key1"));
- ComponentDto project2 = db.components().insertPrivateProject(organization, d -> d.setName("p1").setDbKey("key2"));
- ComponentDto project3 = db.components().insertPrivateProject(organization, d -> d.setName("p2").setDbKey("key3"));
+ QualityGateDto qualityGate1 = db.qualityGates().insertQualityGate();
+ ComponentDto project1 = db.components().insertPrivateProject(d -> d.setName("p1").setDbKey("key1"));
+ ComponentDto project2 = db.components().insertPrivateProject(d -> d.setName("p1").setDbKey("key2"));
+ ComponentDto project3 = db.components().insertPrivateProject(d -> d.setName("p2").setDbKey("key3"));
db.qualityGates().associateProjectToQualityGate(db.components().getProjectDto(project1), qualityGate1);
db.qualityGates().associateProjectToQualityGate(db.components().getProjectDto(project2), qualityGate1);
db.qualityGates().associateProjectToQualityGate(db.components().getProjectDto(project3), qualityGate1);
@Test
public void select_projects_by_query() {
- OrganizationDto organization = db.organizations().insert();
- QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(organization);
- ComponentDto project1 = db.components().insertPrivateProject(organization);
- ComponentDto project2 = db.components().insertPrivateProject(organization);
- ComponentDto project3 = db.components().insertPrivateProject(organization);
+ QualityGateDto qualityGate = db.qualityGates().insertQualityGate();
+ ComponentDto project1 = db.components().insertPrivateProject();
+ ComponentDto project2 = db.components().insertPrivateProject();
+ ComponentDto project3 = db.components().insertPrivateProject();
db.qualityGates().associateProjectToQualityGate(db.components().getProjectDto(project1), qualityGate);
db.qualityGates().associateProjectToQualityGate(db.components().getProjectDto(project2), qualityGate);
@Test
public void search_by_project_name() {
- OrganizationDto organization = db.organizations().insert();
- QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(organization);
- ComponentDto project1 = db.components().insertPrivateProject(organization, p -> p.setName("Project One"));
- ComponentDto project2 = db.components().insertPrivateProject(organization, p -> p.setName("Project Two"));
- ComponentDto project3 = db.components().insertPrivateProject(organization, p -> p.setName("Project Three"));
+ QualityGateDto qualityGate = db.qualityGates().insertQualityGate();
+ ComponentDto project1 = db.components().insertPrivateProject(p -> p.setName("Project One"));
+ ComponentDto project2 = db.components().insertPrivateProject(p -> p.setName("Project Two"));
+ ComponentDto project3 = db.components().insertPrivateProject(p -> p.setName("Project Three"));
db.qualityGates().associateProjectToQualityGate(db.components().getProjectDto(project1), qualityGate);
db.qualityGates().associateProjectToQualityGate(db.components().getProjectDto(project2), qualityGate);
@Test
public void sorted_by_project_name() {
- OrganizationDto organization = db.organizations().insert();
- QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(organization);
- ComponentDto project1 = db.components().insertPrivateProject(organization, p -> p.setName("Project One"));
- ComponentDto project2 = db.components().insertPrivateProject(organization, p -> p.setName("Project Two"));
- ComponentDto project3 = db.components().insertPrivateProject(organization, p -> p.setName("Project Three"));
+ QualityGateDto qualityGate = db.qualityGates().insertQualityGate();
+ ComponentDto project1 = db.components().insertPrivateProject(p -> p.setName("Project One"));
+ ComponentDto project2 = db.components().insertPrivateProject(p -> p.setName("Project Two"));
+ ComponentDto project3 = db.components().insertPrivateProject(p -> p.setName("Project Three"));
assertThat(underTest.selectProjects(dbSession, ProjectQgateAssociationQuery.builder()
.qualityGate(qualityGate)
.containsExactly(project1.uuid(), project3.uuid(), project2.uuid());
}
- @Test
- public void return_only_projects_from_organization() {
- OrganizationDto organization = db.organizations().insert();
- OrganizationDto otherOrganization = db.organizations().insert();
- QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(organization);
- QGateWithOrgDto otherQualityGate = db.qualityGates().insertQualityGate(otherOrganization);
- ComponentDto project = db.components().insertPrivateProject(organization);
- ComponentDto otherProject = db.components().insertPrivateProject(otherOrganization);
- db.qualityGates().associateProjectToQualityGate(db.components().getProjectDto(project), qualityGate);
- db.qualityGates().associateProjectToQualityGate(db.components().getProjectDto(otherProject), otherQualityGate);
-
- List<ProjectQgateAssociationDto> result = underTest.selectProjects(dbSession, ProjectQgateAssociationQuery.builder()
- .qualityGate(qualityGate)
- .build());
-
- assertThat(result)
- .extracting(ProjectQgateAssociationDto::getUuid)
- .containsExactlyInAnyOrder(project.uuid());
- }
-
@Test
public void select_qgate_uuid_is_absent() {
ComponentDto project = db.components().insertPrivateProject();
@Test
public void select_qgate_uuid() {
- OrganizationDto organization = db.organizations().insert();
- QGateWithOrgDto qualityGate1 = db.qualityGates().insertQualityGate(organization);
- QGateWithOrgDto qualityGate2 = db.qualityGates().insertQualityGate(organization);
- ComponentDto project1 = db.components().insertPrivateProject(organization);
- ComponentDto project2 = db.components().insertPrivateProject(organization);
+ QualityGateDto qualityGate1 = db.qualityGates().insertQualityGate();
+ QualityGateDto qualityGate2 = db.qualityGates().insertQualityGate();
+ ComponentDto project1 = db.components().insertPrivateProject();
+ ComponentDto project2 = db.components().insertPrivateProject();
db.qualityGates().associateProjectToQualityGate(db.components().getProjectDto(project1), qualityGate1);
db.qualityGates().associateProjectToQualityGate(db.components().getProjectDto(project2), qualityGate2);
@Test
public void delete_by_project_uuid() {
- OrganizationDto organization = db.organizations().insert();
- QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(organization);
- ProjectDto project = db.components().insertPrivateProjectDto(organization);
+ QualityGateDto qualityGate = db.qualityGates().insertQualityGate();
+ ProjectDto project = db.components().insertPrivateProjectDto();
db.qualityGates().associateProjectToQualityGate(project, qualityGate);
@Test
public void delete_by_qgate_uuid() {
- OrganizationDto organization = db.organizations().insert();
- QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(organization);
- ProjectDto project = db.components().insertPrivateProjectDto(organization);
+ QualityGateDto qualityGate = db.qualityGates().insertQualityGate();
+ ProjectDto project = db.components().insertPrivateProjectDto();
db.qualityGates().associateProjectToQualityGate(project, qualityGate);
@Test
public void update_project_qgate_association() {
- OrganizationDto organization = db.organizations().insert();
- QGateWithOrgDto firstQualityGate = db.qualityGates().insertQualityGate(organization);
- QGateWithOrgDto secondQualityGate = db.qualityGates().insertQualityGate(organization);
- ProjectDto project = db.components().insertPrivateProjectDto(organization);
+ QualityGateDto firstQualityGate = db.qualityGates().insertQualityGate();
+ QualityGateDto secondQualityGate = db.qualityGates().insertQualityGate();
+ ProjectDto project = db.components().insertPrivateProjectDto();
db.qualityGates().associateProjectToQualityGate(project, firstQualityGate);
import org.sonar.core.util.Uuids;
import org.sonar.db.DbSession;
import org.sonar.db.DbTester;
-import org.sonar.db.organization.OrganizationDto;
import org.sonar.db.project.ProjectDto;
import static java.lang.String.format;
assertThat(reloaded.getUpdatedAt()).isNotNull();
}
- @Test
- public void associate() {
- QualityGateDto qgate = db.qualityGates().insertQualityGate(db.getDefaultOrganization());
- OrganizationDto org = db.organizations().insert();
-
- underTest.associate(dbSession, Uuids.createFast(), org, qgate);
-
- assertThat(underTest.selectByOrganizationAndUuid(dbSession, org, qgate.getUuid())).isNotNull();
- assertThat(underTest.selectByOrganizationAndUuid(dbSession, org, qgate.getUuid()))
- .extracting(QGateWithOrgDto::getUuid, QGateWithOrgDto::getUuid, QGateWithOrgDto::getOrganizationUuid, QGateWithOrgDto::getName)
- .containsExactly(qgate.getUuid(), qgate.getUuid(), org.getUuid(), qgate.getName());
- }
+ // TODO::
+ // @Test
+ // public void associate() {
+ // QualityGateDto qgate = db.qualityGates().(db.getDefaultOrganization());
+ // OrganizationDto org = db.organizations().insert();
+ //
+ // underTest.associate(dbSession, Uuids.createFast(), org, qgate);
+ //
+ // assertThat(underTest.selectByOrganizationAndUuid(dbSession, org, qgate.getUuid())).isNotNull();
+ // assertThat(underTest.selectByOrganizationAndUuid(dbSession, org, qgate.getUuid()))
+ // .extracting(QualityGateDto::getUuid, QualityGateDto::getUuid, QualityGateDto::getOrganizationUuid, QualityGateDto::getName)
+ // .containsExactly(qgate.getUuid(), qgate.getUuid(), org.getUuid(), qgate.getName());
+ // }
@Test
public void insert_built_in() {
@Test
public void select_all() {
- OrganizationDto organization1 = db.organizations().insert();
- OrganizationDto organization2 = db.organizations().insert();
- QGateWithOrgDto qualityGate1 = qualityGateDbTester.insertQualityGate(organization1);
- QGateWithOrgDto qualityGate2 = qualityGateDbTester.insertQualityGate(organization1);
- QGateWithOrgDto qualityGateOnOtherOrg = qualityGateDbTester.insertQualityGate(organization2);
+ QualityGateDto qualityGate1 = qualityGateDbTester.insertQualityGate();
+ QualityGateDto qualityGate2 = qualityGateDbTester.insertQualityGate();
+ QualityGateDto qualityGateOnOtherOrg = qualityGateDbTester.insertQualityGate();
- assertThat(underTest.selectAll(dbSession, organization1))
+ assertThat(underTest.selectAll(dbSession))
.extracting(QualityGateDto::getUuid)
- .containsExactlyInAnyOrder(qualityGate1.getUuid(), qualityGate2.getUuid());
+ .containsExactlyInAnyOrder(qualityGate1.getUuid(), qualityGate2.getUuid(), qualityGateOnOtherOrg.getUuid());
}
@Test
assertThat(underTest.selectByUuid(dbSession, "not-existing-uuid")).isNull();
}
- @Test
- public void select_by_organization_and_uuid() {
- OrganizationDto organization = db.organizations().insert();
- QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(organization);
- OrganizationDto otherOrganization = db.organizations().insert();
- QGateWithOrgDto otherQualityGate = db.qualityGates().insertQualityGate(otherOrganization);
-
- assertThat(underTest.selectByOrganizationAndUuid(dbSession, organization, qualityGate.getUuid()).getUuid()).isEqualTo(qualityGate.getUuid());
- assertThat(underTest.selectByOrganizationAndUuid(dbSession, otherOrganization, qualityGate.getUuid())).isNull();
- assertThat(underTest.selectByOrganizationAndUuid(dbSession, organization, otherQualityGate.getUuid())).isNull();
- }
-
@Test
public void select_by_organization_and_name() {
- OrganizationDto organization = db.organizations().insert();
- QGateWithOrgDto qualityGate1 = db.qualityGates().insertQualityGate(organization);
- QGateWithOrgDto qualityGate2 = db.qualityGates().insertQualityGate(organization);
- OrganizationDto otherOrganization = db.organizations().insert();
- QGateWithOrgDto qualityGate3 = db.qualityGates().insertQualityGate(otherOrganization);
-
- assertThat(underTest.selectByOrganizationAndName(dbSession, organization, qualityGate1.getName()).getUuid()).isEqualTo(qualityGate1.getUuid());
- assertThat(underTest.selectByOrganizationAndName(dbSession, otherOrganization, qualityGate3.getName()).getUuid()).isEqualTo(qualityGate3.getUuid());
- assertThat(underTest.selectByOrganizationAndName(dbSession, organization, "Unknown")).isNull();
+ QualityGateDto qualityGate1 = db.qualityGates().insertQualityGate();
+ QualityGateDto qualityGate2 = db.qualityGates().insertQualityGate();
+ QualityGateDto qualityGate3 = db.qualityGates().insertQualityGate();
+
+ assertThat(underTest.selectByDefaultOrganizationAndName(dbSession, qualityGate1.getName()).getUuid()).isEqualTo(qualityGate1.getUuid());
+ assertThat(underTest.selectByDefaultOrganizationAndName(dbSession, qualityGate3.getName()).getUuid()).isEqualTo(qualityGate3.getUuid());
+ assertThat(underTest.selectByDefaultOrganizationAndName(dbSession, "Unknown")).isNull();
}
@Test
public void select_by_organization_and_id() {
- OrganizationDto organization = db.organizations().insert();
- QGateWithOrgDto qualityGate1 = db.qualityGates().insertQualityGate(organization);
- QGateWithOrgDto qualityGate2 = db.qualityGates().insertQualityGate(organization);
- OrganizationDto otherOrganization = db.organizations().insert();
- QGateWithOrgDto qualityGate3 = db.qualityGates().insertQualityGate(otherOrganization);
-
- assertThat(underTest.selectByOrganizationAndUuid(dbSession, organization, qualityGate1.getUuid()).getUuid()).isEqualTo(qualityGate1.getUuid());
- assertThat(underTest.selectByOrganizationAndUuid(dbSession, otherOrganization, qualityGate3.getUuid()).getUuid()).isEqualTo(qualityGate3.getUuid());
- assertThat(underTest.selectByOrganizationAndUuid(dbSession, organization, "123")).isNull();
- }
+ QualityGateDto qualityGate1 = db.qualityGates().insertQualityGate();
+ QualityGateDto qualityGate2 = db.qualityGates().insertQualityGate();
+ QualityGateDto qualityGate3 = db.qualityGates().insertQualityGate();
- @Test
- public void select_default() {
- OrganizationDto organization = db.organizations().insert();
- QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(organization);
- db.getDbClient().organizationDao().setDefaultQualityGate(dbSession, organization, qualityGate);
- OrganizationDto otherOrganization = db.organizations().insert();
- QGateWithOrgDto otherQualityGate = db.qualityGates().insertQualityGate(otherOrganization);
- db.getDbClient().organizationDao().setDefaultQualityGate(dbSession, otherOrganization, otherQualityGate);
-
- assertThat(underTest.selectDefault(dbSession, organization).getUuid()).isEqualTo(qualityGate.getUuid());
- assertThat(underTest.selectDefault(dbSession, otherOrganization).getUuid()).isEqualTo(otherQualityGate.getUuid());
+ assertThat(underTest.selectByDefaultOrganizationAndUuid(dbSession, qualityGate1.getUuid()).getUuid()).isEqualTo(qualityGate1.getUuid());
+ assertThat(underTest.selectByDefaultOrganizationAndUuid(dbSession, qualityGate3.getUuid()).getUuid()).isEqualTo(qualityGate3.getUuid());
+ assertThat(underTest.selectByDefaultOrganizationAndUuid(dbSession, "123")).isNull();
}
@Test
public void select_by_project_uuid() {
- OrganizationDto organization = db.organizations().insert();
+ ProjectDto project = db.components().insertPrivateProjectDto();
- ProjectDto project = db.components().insertPrivateProjectDto(organization);
+ QualityGateDto qualityGate1 = db.qualityGates().insertQualityGate();
+ QualityGateDto qualityGate2 = db.qualityGates().insertQualityGate();
- QGateWithOrgDto qualityGate1 = db.qualityGates().insertQualityGate(organization);
- QGateWithOrgDto qualityGate2 = db.qualityGates().insertQualityGate(organization);
-
- OrganizationDto otherOrganization = db.organizations().insert();
- QGateWithOrgDto qualityGate3 = db.qualityGates().insertQualityGate(otherOrganization);
+ QualityGateDto qualityGate3 = db.qualityGates().insertQualityGate();
db.qualityGates().associateProjectToQualityGate(project, qualityGate1);
@Test
public void delete() {
- OrganizationDto organization = db.organizations().insert();
- QGateWithOrgDto qualityGate = qualityGateDbTester.insertQualityGate(organization);
- QGateWithOrgDto otherQualityGate = qualityGateDbTester.insertQualityGate(organization);
+ QualityGateDto qualityGate = qualityGateDbTester.insertQualityGate();
+ QualityGateDto otherQualityGate = qualityGateDbTester.insertQualityGate();
underTest.delete(qualityGate, dbSession);
dbSession.commit();
- assertThat(underTest.selectByOrganizationAndUuid(dbSession, organization, qualityGate.getUuid())).isNull();
+ assertThat(underTest.selectByDefaultOrganizationAndUuid(dbSession, qualityGate.getUuid())).isNull();
assertThat(db.countSql(dbSession, format("select count(*) from org_quality_gates where quality_gate_uuid='%s'", qualityGate.getUuid()))).isZero();
- assertThat(underTest.selectByOrganizationAndUuid(dbSession, organization, otherQualityGate.getUuid())).isNotNull();
+ assertThat(underTest.selectByDefaultOrganizationAndUuid(dbSession, otherQualityGate.getUuid())).isNotNull();
assertThat(db.countSql(dbSession, format("select count(*) from org_quality_gates where quality_gate_uuid='%s'", otherQualityGate.getUuid()))).isEqualTo(1);
}
@Test
public void delete_by_uuids() {
- OrganizationDto organization = db.organizations().insert();
- QGateWithOrgDto qualityGate1 = qualityGateDbTester.insertQualityGate(organization);
- QGateWithOrgDto qualityGate2 = qualityGateDbTester.insertQualityGate(organization);
+ QualityGateDto qualityGate1 = qualityGateDbTester.insertQualityGate();
+ QualityGateDto qualityGate2 = qualityGateDbTester.insertQualityGate();
underTest.deleteByUuids(dbSession, asList(qualityGate1.getUuid(), qualityGate2.getUuid()));
dbSession.commit();
- assertThat(underTest.selectAll(dbSession, organization).stream())
+ assertThat(underTest.selectAll(dbSession).stream())
.extracting(QualityGateDto::getUuid)
.doesNotContain(qualityGate1.getUuid(), qualityGate2.getUuid());
}
assertThat(db.countRowsOfTable(dbSession, "quality_gates")).isEqualTo(nbOfQualityGates);
}
- @Test
- public void deleteOrgQualityGatesByOrganization() {
- OrganizationDto organization = db.organizations().insert();
- qualityGateDbTester.insertQualityGate(organization);
- OrganizationDto otherOrganization = db.organizations().insert();
- qualityGateDbTester.insertQualityGate(otherOrganization);
-
- underTest.deleteOrgQualityGatesByOrganization(dbSession, organization);
- dbSession.commit();
-
- assertThat(db.select("select organization_uuid as \"organizationUuid\" from org_quality_gates"))
- .extracting(row -> (String) row.get("organizationUuid"))
- .containsOnly(otherOrganization.getUuid());
- }
+ // TODO::
+ // @Test
+ // public void deleteOrgQualityGatesByOrganization() {
+ // OrganizationDto organization = db.organizations().insert();
+ // qualityGateDbTester.insertQualityGate(organization);
+ // OrganizationDto otherOrganization = db.organizations().insert();
+ // qualityGateDbTester.insertQualityGate(otherOrganization);
+ //
+ // underTest.deleteOrgQualityGatesByOrganization(dbSession, organization);
+ // dbSession.commit();
+ //
+ // assertThat(db.select("select organization_uuid as \"organizationUuid\" from org_quality_gates"))
+ // .extracting(row -> (String) row.get("organizationUuid"))
+ // .containsOnly(otherOrganization.getUuid());
+ // }
@Test
public void update() {
- OrganizationDto organization = db.organizations().insert();
- QGateWithOrgDto qualityGate = qualityGateDbTester.insertQualityGate(organization, qg -> qg.setName("old name"));
+ QualityGateDto qualityGate = qualityGateDbTester.insertQualityGate(qg -> qg.setName("old name"));
underTest.update(qualityGate.setName("Not so strict"), dbSession);
dbSession.commit();
- QGateWithOrgDto reloaded = underTest.selectByOrganizationAndUuid(dbSession, organization, qualityGate.getUuid());
+ QualityGateDto reloaded = underTest.selectByDefaultOrganizationAndUuid(dbSession, qualityGate.getUuid());
assertThat(reloaded.getName()).isEqualTo("Not so strict");
}
private void insertQualityGates() {
- qualityGateDbTester.insertQualityGate(db.getDefaultOrganization(), g -> g.setName("Very strict").setBuiltIn(false));
- qualityGateDbTester.insertQualityGate(db.getDefaultOrganization(), g -> g.setName("Balanced").setBuiltIn(false));
- qualityGateDbTester.insertQualityGate(db.getDefaultOrganization(), g -> g.setName("Lenient").setBuiltIn(false));
+ qualityGateDbTester.insertQualityGate(g -> g.setName("Very strict").setBuiltIn(false));
+ qualityGateDbTester.insertQualityGate(g -> g.setName("Balanced").setBuiltIn(false));
+ qualityGateDbTester.insertQualityGate(g -> g.setName("Lenient").setBuiltIn(false));
}
}
import org.sonar.db.metric.MetricDto;
import org.sonar.db.organization.OrganizationDto;
import org.sonar.db.project.ProjectDto;
+import org.sonar.db.property.PropertyDto;
import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric;
import static org.apache.commons.lang.RandomStringUtils.randomNumeric;
public class QualityGateDbTester {
+ private static final String DEFAULT_QUALITY_GATE_PROPERTY_NAME = "qualitygate.default";
private final DbTester db;
private final DbClient dbClient;
db.commit();
}
+ @Deprecated
public void associateQualityGateToOrganization(QualityGateDto qualityGate, OrganizationDto organization) {
dbClient.qualityGateDao().associate(dbSession, Uuids.createFast(), organization, qualityGate);
db.commit();
@SafeVarargs
public final QualityGateDto createDefaultQualityGate(Consumer<QualityGateDto>... dtoPopulators) {
- return createDefaultQualityGate(db.getDefaultOrganization(), dtoPopulators);
- }
-
- @SafeVarargs
- @Deprecated
- public final QualityGateDto createDefaultQualityGate(OrganizationDto organization, Consumer<QualityGateDto>... dtoPopulators) {
- QualityGateDto defaultQGate = insertQualityGate(organization, dtoPopulators);
- setDefaultQualityGate(organization, defaultQGate);
+ QualityGateDto defaultQGate = insertQualityGate(dtoPopulators);
+ setDefaultQualityGate(defaultQGate);
return defaultQGate;
}
- public void setDefaultQualityGate(OrganizationDto organization, QualityGateDto qualityGate) {
- dbClient.organizationDao().update(dbSession, organization.setDefaultQualityGateUuid(qualityGate.getUuid()));
+ public void setDefaultQualityGate(QualityGateDto qualityGate) {
+ dbClient.propertiesDao().saveProperty(new PropertyDto().setKey(DEFAULT_QUALITY_GATE_PROPERTY_NAME).setValue(qualityGate.getUuid()));
dbSession.commit();
}
public void addSteps(MigrationStepRegistry registry) {
registry
.add(4200, "Move default project visibility to global properties", MoveDefaultProjectVisibilityToGlobalProperties.class)
+ .add(4201, "Move default quality gate to global properties", MoveDefaultQualityGateToGlobalProperties.class)
;
}
--- /dev/null
+/*
+ * SonarQube
+ * Copyright (C) 2009-2020 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.platform.db.migration.version.v87;
+
+import java.sql.SQLException;
+import org.sonar.api.utils.System2;
+import org.sonar.core.util.UuidFactory;
+import org.sonar.db.Database;
+import org.sonar.server.platform.db.migration.step.DataChange;
+
+public class MoveDefaultQualityGateToGlobalProperties extends DataChange {
+
+ private final UuidFactory uuidFactory;
+ private final System2 system2;
+
+ public MoveDefaultQualityGateToGlobalProperties(Database db, UuidFactory uuidFactory, System2 system2) {
+ super(db);
+ this.uuidFactory = uuidFactory;
+ this.system2 = system2;
+ }
+
+ @Override
+ public void execute(Context context) throws SQLException {
+ String defaultQualityGate = context.prepareSelect("select default_quality_gate_uuid from organizations where kee = ?")
+ .setString(1, "default-organization")
+ .get(row -> row.getString(1));
+
+ if (defaultQualityGate == null) {
+ throw new IllegalStateException("Default organization is missing");
+ } else {
+ context.prepareUpsert("insert into properties (uuid, prop_key, is_empty, text_value, created_at) values (?, ?, ?, ?, ?)")
+ .setString(1, uuidFactory.create())
+ .setString(2, "qualitygate.default")
+ .setBoolean(3, false)
+ .setString(4, defaultQualityGate)
+ .setLong(5, system2.now())
+ .execute()
+ .commit();
+ }
+ }
+}
--- /dev/null
+/*
+ * SonarQube
+ * Copyright (C) 2009-2020 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.platform.db.migration.version.v87;
+
+import java.sql.SQLException;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.sonar.api.impl.utils.TestSystem2;
+import org.sonar.core.util.UuidFactory;
+import org.sonar.core.util.UuidFactoryFast;
+import org.sonar.db.CoreDbTester;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+
+public class MoveDefaultQualityGateToGlobalPropertiesTest {
+
+ private final UuidFactory uuidFactory = UuidFactoryFast.getInstance();
+ private final TestSystem2 system2 = new TestSystem2();
+ private final long NOW = 1606375781L;
+
+ @Rule
+ public CoreDbTester db = CoreDbTester.createForSchema(MoveDefaultQualityGateToGlobalPropertiesTest.class, "schema.sql");
+
+ private final MoveDefaultQualityGateToGlobalProperties underTest = new MoveDefaultQualityGateToGlobalProperties(db.database(),
+ UuidFactoryFast.getInstance(), system2);
+
+ @Before
+ public void before() {
+ system2.setNow(NOW);
+ }
+
+ @Test
+ public void fail_if_organization_not_exist() {
+ assertThatThrownBy(underTest::execute)
+ .isInstanceOf(IllegalStateException.class)
+ .hasMessage("Default organization is missing");
+ }
+
+ @Test
+ public void migrate_default_quality_gate_from_default_organization() throws SQLException {
+ insertDefaultOrganization("default-quality-gate-uuid");
+
+ underTest.execute();
+
+ assertThatDefaultQualityGateIsEqualTo("default-quality-gate-uuid");
+ }
+
+ private void insertDefaultOrganization(String defaultQualityGate) {
+ String uuid = uuidFactory.create();
+ db.executeInsert("organizations",
+ "uuid", uuid,
+ "kee", "default-organization",
+ "name", "name" + uuid,
+ "default_perm_template_project", uuidFactory.create(),
+ "default_perm_template_port", uuidFactory.create(),
+ "default_perm_template_app", uuidFactory.create(),
+ "default_quality_gate_uuid", defaultQualityGate,
+ "new_project_private", false,
+ "subscription", uuid,
+ "created_at", NOW,
+ "updated_at", NOW);
+ }
+
+ private void assertThatDefaultQualityGateIsEqualTo(String s) {
+ assertThat(db.selectFirst("select p.text_value, p.created_at from properties p where p.prop_key = 'qualitygate.default'"))
+ .containsEntry("TEXT_VALUE", s)
+ .containsEntry("CREATED_AT", NOW);
+ }
+}
--- /dev/null
+CREATE TABLE "PROPERTIES"(
+ "PROP_KEY" VARCHAR(512) NOT NULL,
+ "IS_EMPTY" BOOLEAN NOT NULL,
+ "TEXT_VALUE" VARCHAR(4000),
+ "CLOB_VALUE" CLOB,
+ "CREATED_AT" BIGINT NOT NULL,
+ "COMPONENT_UUID" VARCHAR(40),
+ "UUID" VARCHAR(40) NOT NULL,
+ "USER_UUID" VARCHAR(255)
+);
+ALTER TABLE "PROPERTIES" ADD CONSTRAINT "PK_PROPERTIES" PRIMARY KEY("UUID");
+CREATE INDEX "PROPERTIES_KEY" ON "PROPERTIES"("PROP_KEY");
+
+CREATE TABLE "ORGANIZATIONS"(
+ "UUID" VARCHAR(40) NOT NULL,
+ "KEE" VARCHAR(255) NOT NULL,
+ "NAME" VARCHAR(255) NOT NULL,
+ "DESCRIPTION" VARCHAR(256),
+ "URL" VARCHAR(256),
+ "AVATAR_URL" VARCHAR(256),
+ "GUARDED" BOOLEAN,
+ "DEFAULT_QUALITY_GATE_UUID" VARCHAR(40) NOT NULL,
+ "DEFAULT_PERM_TEMPLATE_PROJECT" VARCHAR(40),
+ "DEFAULT_PERM_TEMPLATE_APP" VARCHAR(40),
+ "DEFAULT_PERM_TEMPLATE_PORT" VARCHAR(40),
+ "NEW_PROJECT_PRIVATE" BOOLEAN NOT NULL,
+ "SUBSCRIPTION" VARCHAR(40) NOT NULL,
+ "CREATED_AT" BIGINT NOT NULL,
+ "UPDATED_AT" BIGINT NOT NULL,
+ "DEFAULT_GROUP_UUID" VARCHAR(40)
+);
+ALTER TABLE "ORGANIZATIONS" ADD CONSTRAINT "PK_ORGANIZATIONS" PRIMARY KEY("UUID");
+CREATE UNIQUE INDEX "ORGANIZATION_KEY" ON "ORGANIZATIONS"("KEE");
import java.util.Optional;
import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
-import org.sonar.db.organization.OrganizationDto;
import org.sonar.db.project.ProjectDto;
-import org.sonar.db.qualitygate.QGateWithOrgDto;
+import org.sonar.db.property.PropertyDto;
import org.sonar.db.qualitygate.QualityGateDto;
import static com.google.common.base.Preconditions.checkState;
-import static java.util.Optional.ofNullable;
public class QualityGateFinder {
+ private static final String DEFAULT_QUALITY_GATE_PROPERTY_NAME = "qualitygate.default";
private final DbClient dbClient;
this.dbClient = dbClient;
}
- /**
- * Return effective quality gate of a project.
- *
- * It will first try to get the quality gate explicitly defined on a project, if none it will try to return default quality gate of the organization
- */
- public Optional<QualityGateData> getQualityGate(DbSession dbSession, OrganizationDto organization, ProjectDto projectDto) {
- return getQualityGate(dbSession, organization, projectDto.getUuid());
+ public QualityGateData getQualityGate(DbSession dbSession, ProjectDto projectDto) {
+ return getQualityGate(dbSession, projectDto.getUuid());
}
- public Optional<QualityGateData> getQualityGate(DbSession dbSession, OrganizationDto organization, String projectUuid) {
- Optional<QualityGateData> res = dbClient.projectQgateAssociationDao().selectQGateUuidByProjectUuid(dbSession, projectUuid)
- .map(qualityGateUuid -> dbClient.qualityGateDao().selectByUuid(dbSession, qualityGateUuid))
- .map(qualityGateDto -> new QualityGateData(qualityGateDto, false));
+ public QualityGateData getQualityGate(DbSession dbSession, String projectUuid) {
+ Optional<QualityGateData> res = getQualityGateForProject(dbSession, projectUuid);
if (res.isPresent()) {
- return res;
+ return res.get();
}
- return ofNullable(dbClient.qualityGateDao().selectByOrganizationAndUuid(dbSession, organization, organization.getDefaultQualityGateUuid()))
- .map(qualityGateDto -> new QualityGateData(qualityGateDto, true));
+ QualityGateDto defaultQualityGate = getDefault(dbSession);
+ return new QualityGateData(defaultQualityGate, true);
+ }
+
+ private Optional<QualityGateData> getQualityGateForProject(DbSession dbSession, String projectUuid) {
+ return dbClient.projectQgateAssociationDao().selectQGateUuidByProjectUuid(dbSession, projectUuid)
+ .map(qualityGateUuid -> dbClient.qualityGateDao().selectByUuid(dbSession, qualityGateUuid))
+ .map(qualityGateDto -> new QualityGateData(qualityGateDto, false));
}
- public QualityGateDto getDefault(DbSession dbSession, OrganizationDto organization) {
- QGateWithOrgDto qgate = dbClient.qualityGateDao().selectByOrganizationAndUuid(dbSession, organization, organization.getDefaultQualityGateUuid());
- checkState(qgate != null, "Default quality gate [%s] is missing on organization [%s]", organization.getDefaultQualityGateUuid(), organization.getUuid());
- return qgate;
+ public QualityGateDto getDefault(DbSession dbSession) {
+ PropertyDto qGateDefaultUuidProperty = dbClient.propertiesDao().selectGlobalProperty(dbSession, DEFAULT_QUALITY_GATE_PROPERTY_NAME);
+ checkState(qGateDefaultUuidProperty != null, "Default quality gate property is missing");
+ dbClient.qualityGateDao().selectByUuid(dbSession, qGateDefaultUuidProperty.getValue());
+ return Optional.ofNullable(dbClient.qualityGateDao().selectByUuid(dbSession, qGateDefaultUuidProperty.getValue()))
+ .orElseThrow(() -> new IllegalStateException("Default quality gate is missing"));
}
public QualityGateDto getBuiltInQualityGate(DbSession dbSession) {
*/
package org.sonar.server.qualitygate;
-import java.util.Optional;
import org.junit.Rule;
import org.junit.Test;
-import org.junit.rules.ExpectedException;
import org.sonar.api.utils.System2;
import org.sonar.db.DbSession;
import org.sonar.db.DbTester;
import org.sonar.db.project.ProjectDto;
import org.sonar.db.qualitygate.QualityGateDto;
-import static java.lang.String.format;
import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
public class QualityGateFinderTest {
- @Rule
- public ExpectedException expectedException = ExpectedException.none();
-
@Rule
public DbTester db = DbTester.create(System2.INSTANCE);
- private DbSession dbSession = db.getSession();
+ private final DbSession dbSession = db.getSession();
- private QualityGateFinder underTest = new QualityGateFinder(db.getDbClient());
+ private final QualityGateFinder underTest = new QualityGateFinder(db.getDbClient());
@Test
public void return_default_quality_gate_for_project() {
ProjectDto project = db.components().insertPrivateProjectDto();
- QualityGateDto dbQualityGate = db.qualityGates().createDefaultQualityGate(db.getDefaultOrganization(), qg -> qg.setName("Sonar way"));
+ QualityGateDto dbQualityGate = db.qualityGates().createDefaultQualityGate(qg -> qg.setName("Sonar way"));
- Optional<QualityGateFinder.QualityGateData> result = underTest.getQualityGate(dbSession, db.getDefaultOrganization(), project);
+ QualityGateFinder.QualityGateData result = underTest.getQualityGate(dbSession, project);
- assertThat(result).isPresent();
- assertThat(result.get().getQualityGate().getUuid()).isEqualTo(dbQualityGate.getUuid());
- assertThat(result.get().isDefault()).isTrue();
+ assertThat(result.getQualityGate().getUuid()).isEqualTo(dbQualityGate.getUuid());
+ assertThat(result.isDefault()).isTrue();
}
@Test
public void return_project_quality_gate_over_default() {
ProjectDto project = db.components().insertPrivateProjectDto();
- db.qualityGates().createDefaultQualityGate(db.getDefaultOrganization(), qg -> qg.setName("Sonar way"));
- QualityGateDto dbQualityGate = db.qualityGates().insertQualityGate(db.getDefaultOrganization(), qg -> qg.setName("My team QG"));
+ db.qualityGates().createDefaultQualityGate(qg -> qg.setName("Sonar way"));
+ QualityGateDto dbQualityGate = db.qualityGates().insertQualityGate(qg -> qg.setName("My team QG"));
db.qualityGates().associateProjectToQualityGate(project, dbQualityGate);
- Optional<QualityGateFinder.QualityGateData> result = underTest.getQualityGate(dbSession, db.getDefaultOrganization(), project);
+ QualityGateFinder.QualityGateData result = underTest.getQualityGate(dbSession, project);
- assertThat(result).isPresent();
- assertThat(result.get().getQualityGate().getUuid()).isEqualTo(dbQualityGate.getUuid());
- assertThat(result.get().isDefault()).isFalse();
+ assertThat(result.getQualityGate().getUuid()).isEqualTo(dbQualityGate.getUuid());
+ assertThat(result.isDefault()).isFalse();
}
@Test
- public void fail_when_default_qgate_defined_does_not_exists() {
+ public void fail_when_default_qgate_defined_does_not_exist() {
ProjectDto project = db.components().insertPrivateProjectDto();
- QualityGateDto dbQualityGate = db.qualityGates().createDefaultQualityGate(db.getDefaultOrganization(), qg -> qg.setName("Sonar way"));
+ QualityGateDto dbQualityGate = db.qualityGates().createDefaultQualityGate(qg -> qg.setName("Sonar way"));
db.getDbClient().qualityGateDao().delete(dbQualityGate, dbSession);
db.commit();
- assertThat(underTest.getQualityGate(dbSession, db.getDefaultOrganization(), project)).isEmpty();
+ assertThatThrownBy(() -> underTest.getQualityGate(dbSession, project))
+ .isInstanceOf(IllegalStateException.class)
+ .hasMessage("Default quality gate is missing");
}
@Test
- public void fail_when_project_qgate_defined_does_not_exists() {
+ public void fail_when_project_qgate_defined_does_not_exist() {
ProjectDto project = db.components().insertPrivateProjectDto();
- QualityGateDto dbQualityGate = db.qualityGates().insertQualityGate(db.getDefaultOrganization(), qg -> qg.setName("My team QG"));
+ QualityGateDto dbQualityGate = db.qualityGates().insertQualityGate(qg -> qg.setName("My team QG"));
+ db.qualityGates().setDefaultQualityGate(dbQualityGate);
db.qualityGates().associateProjectToQualityGate(project, dbQualityGate);
db.getDbClient().qualityGateDao().delete(dbQualityGate, dbSession);
- assertThat(underTest.getQualityGate(dbSession, db.getDefaultOrganization(), project)).isEmpty();
+ assertThatThrownBy(() -> underTest.getQualityGate(dbSession, project))
+ .isInstanceOf(IllegalStateException.class)
+ .hasMessage("Default quality gate is missing");
}
@Test
- public void fail_when_default_quality_gate_does_not_exists() {
- QualityGateDto dbQualityGate = db.qualityGates().insertQualityGate(db.getDefaultOrganization(), qg -> qg.setName("My team QG"));
- db.qualityGates().setDefaultQualityGate(db.getDefaultOrganization(), dbQualityGate);
+ public void fail_when_qgate_property_does_not_exist() {
+ ProjectDto project = db.components().insertPrivateProjectDto();
+ QualityGateDto dbQualityGate = db.qualityGates().insertQualityGate(qg -> qg.setName("My team QG"));
db.getDbClient().qualityGateDao().delete(dbQualityGate, dbSession);
- expectedException.expect(IllegalStateException.class);
- expectedException.expectMessage(format("Default quality gate [%s] is missing on organization [%s]", dbQualityGate.getUuid(), db.getDefaultOrganization().getUuid()));
+ assertThatThrownBy(() -> underTest.getQualityGate(dbSession, project))
+ .isInstanceOf(IllegalStateException.class)
+ .hasMessage("Default quality gate property is missing");
+ }
+
+ @Test
+ public void fail_when_default_quality_gate_does_not_exists() {
+ QualityGateDto dbQualityGate = db.qualityGates().insertQualityGate(qg -> qg.setName("My team QG"));
+ db.qualityGates().setDefaultQualityGate(dbQualityGate);
+ db.getDbClient().qualityGateDao().delete(dbQualityGate, dbSession);
- underTest.getDefault(dbSession, db.getDefaultOrganization());
+ assertThatThrownBy(() -> underTest.getDefault(dbSession))
+ .isInstanceOf(IllegalStateException.class)
+ .hasMessage("Default quality gate is missing");
}
}
public class OrganizationUpdaterImplTest {
private static final long A_DATE = 12893434L;
- private OrganizationUpdater.NewOrganization FULL_POPULATED_NEW_ORGANIZATION = newOrganizationBuilder()
+ private final OrganizationUpdater.NewOrganization FULL_POPULATED_NEW_ORGANIZATION = newOrganizationBuilder()
.setName("a-name")
.setKey("a-key")
.setDescription("a-description")
.setAvatarUrl("a-avatar")
.build();
- private System2 system2 = new TestSystem2().setNow(A_DATE);
+ private final System2 system2 = new TestSystem2().setNow(A_DATE);
- private static Consumer<OrganizationDto> EMPTY_ORGANIZATION_CONSUMER = o -> {
+ private static final Consumer<OrganizationDto> EMPTY_ORGANIZATION_CONSUMER = o -> {
};
@Rule
@Rule
public BuiltInQProfileRepositoryRule builtInQProfileRepositoryRule = new BuiltInQProfileRepositoryRule();
- private DbSession dbSession = db.getSession();
+ private final DbSession dbSession = db.getSession();
- private IllegalArgumentException exceptionThrownByOrganizationValidation = new IllegalArgumentException("simulate IAE thrown by OrganizationValidation");
- private DbClient dbClient = db.getDbClient();
- private UuidFactory uuidFactory = new SequenceUuidFactory();
- private OrganizationValidation organizationValidation = mock(OrganizationValidation.class);
- private UserIndexer userIndexer = new UserIndexer(dbClient, es.client());
- private UserIndex userIndex = new UserIndex(es.client(), system2);
- private DefaultGroupCreator defaultGroupCreator = new DefaultGroupCreatorImpl(dbClient, uuidFactory, TestDefaultOrganizationProvider.from(db));
+ private final IllegalArgumentException exceptionThrownByOrganizationValidation = new IllegalArgumentException("simulate IAE thrown by OrganizationValidation");
+ private final DbClient dbClient = db.getDbClient();
+ private final UuidFactory uuidFactory = new SequenceUuidFactory();
+ private final OrganizationValidation organizationValidation = mock(OrganizationValidation.class);
+ private final UserIndexer userIndexer = new UserIndexer(dbClient, es.client());
+ private final UserIndex userIndex = new UserIndex(es.client(), system2);
+ private final DefaultGroupCreator defaultGroupCreator = new DefaultGroupCreatorImpl(dbClient, uuidFactory, TestDefaultOrganizationProvider.from(db));
- private ResourceTypes resourceTypes = new ResourceTypesRule().setRootQualifiers(Qualifiers.PROJECT);
- private PermissionService permissionService = new PermissionServiceImpl(resourceTypes);
+ private final ResourceTypes resourceTypes = new ResourceTypesRule().setRootQualifiers(Qualifiers.PROJECT);
+ private final PermissionService permissionService = new PermissionServiceImpl(resourceTypes);
- private OrganizationUpdaterImpl underTest = new OrganizationUpdaterImpl(dbClient, system2, uuidFactory, organizationValidation, userIndexer,
+ private final OrganizationUpdaterImpl underTest = new OrganizationUpdaterImpl(dbClient, system2, uuidFactory, organizationValidation, userIndexer,
builtInQProfileRepositoryRule, defaultGroupCreator, permissionService);
@Test
db.commit();
}
- @Test
- public void create_associates_to_built_in_quality_gate() throws OrganizationUpdater.KeyConflictException {
- QualityGateDto builtInQualityGate = db.qualityGates().insertBuiltInQualityGate();
- builtInQProfileRepositoryRule.initialize();
- UserDto user = db.users().insertUser();
-
- underTest.create(dbSession, user, FULL_POPULATED_NEW_ORGANIZATION, o -> {
- });
-
- OrganizationDto organization = dbClient.organizationDao().selectByKey(dbSession, FULL_POPULATED_NEW_ORGANIZATION.getKey()).get();
- assertThat(dbClient.qualityGateDao().selectDefault(dbSession, organization).getUuid()).isEqualTo(builtInQualityGate.getUuid());
- }
-
@Test
public void create_calls_consumer() throws OrganizationUpdater.KeyConflictException {
UserDto user = db.users().insertUser();
import org.sonar.db.measure.LiveMeasureComparator;
import org.sonar.db.measure.LiveMeasureDto;
import org.sonar.db.metric.MetricDto;
-import org.sonar.db.organization.OrganizationDto;
import org.sonar.db.project.ProjectDto;
import org.sonar.server.es.ProjectIndexer;
import org.sonar.server.es.ProjectIndexers;
// load all the components to be refreshed, including their ancestors
List<ComponentDto> components = loadTreeOfComponents(dbSession, touchedComponents);
ComponentDto branchComponent = findBranchComponent(components);
- OrganizationDto organization = loadOrganization(dbSession, branchComponent);
BranchDto branch = loadBranch(dbSession, branchComponent);
ProjectDto project = loadProject(dbSession, branch.getProjectUuid());
Optional<SnapshotDto> lastAnalysis = dbClient.snapshotDao().selectLastAnalysisByRootComponentUuid(dbSession, branchComponent.uuid());
return Optional.empty();
}
- QualityGate qualityGate = qGateComputer.loadQualityGate(dbSession, organization, project, branch);
+ QualityGate qualityGate = qGateComputer.loadQualityGate(dbSession, project, branch);
Collection<String> metricKeys = getKeysOfAllInvolvedMetrics(qualityGate);
List<MetricDto> metrics = dbClient.metricDao().selectByKeys(dbSession, metricKeys);
.orElseThrow(() -> new IllegalStateException("Project not found: " + uuid));
}
- private OrganizationDto loadOrganization(DbSession dbSession, ComponentDto project) {
- String organizationUuid = project.getOrganizationUuid();
- return dbClient.organizationDao().selectByUuid(dbSession, organizationUuid)
- .orElseThrow(() -> new IllegalStateException("No organization with UUID " + organizationUuid));
- }
-
private static class FormulaContextImpl implements IssueMetricFormula.Context {
private final MeasureMatrix matrix;
private final DebtRatingGrid debtRatingGrid;
import org.sonar.db.DbSession;
import org.sonar.db.component.BranchDto;
import org.sonar.db.component.ComponentDto;
-import org.sonar.db.organization.OrganizationDto;
import org.sonar.db.project.ProjectDto;
import org.sonar.server.qualitygate.EvaluatedQualityGate;
import org.sonar.server.qualitygate.QualityGate;
@ServerSide
public interface LiveQualityGateComputer {
- QualityGate loadQualityGate(DbSession dbSession, OrganizationDto organization, ProjectDto project, BranchDto branch);
+ QualityGate loadQualityGate(DbSession dbSession, ProjectDto project, BranchDto branch);
EvaluatedQualityGate refreshGateStatus(ComponentDto project, QualityGate gate, MeasureMatrix measureMatrix);
import org.sonar.db.component.ComponentDto;
import org.sonar.db.measure.LiveMeasureDto;
import org.sonar.db.metric.MetricDto;
-import org.sonar.db.organization.OrganizationDto;
import org.sonar.db.project.ProjectDto;
import org.sonar.db.qualitygate.QualityGateConditionDto;
import org.sonar.db.qualitygate.QualityGateDto;
import org.sonar.server.qualitygate.QualityGateEvaluator;
import org.sonar.server.qualitygate.QualityGateFinder;
-import static java.lang.String.format;
import static org.sonar.core.util.stream.MoreCollectors.toHashSet;
import static org.sonar.core.util.stream.MoreCollectors.uniqueIndex;
}
@Override
- public QualityGate loadQualityGate(DbSession dbSession, OrganizationDto organization, ProjectDto project, BranchDto branch) {
- QualityGateDto gateDto = qGateFinder.getQualityGate(dbSession, organization, project)
- .orElseThrow(() -> new IllegalStateException(format("Quality Gate not found for project %s", project.getKey())))
+ public QualityGate loadQualityGate(DbSession dbSession, ProjectDto project, BranchDto branch) {
+ QualityGateDto gateDto = qGateFinder.getQualityGate(dbSession, project)
.getQualityGate();
Collection<QualityGateConditionDto> conditionDtos = dbClient.gateConditionDao().selectForQualityGate(dbSession, gateDto.getUuid());
Set<String> metricUuids = conditionDtos.stream().map(QualityGateConditionDto::getMetricUuid)
import org.sonar.core.util.Uuids;
import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
-import org.sonar.db.organization.OrganizationDto;
import org.sonar.db.qualitygate.QualityGateConditionDto;
import org.sonar.db.qualitygate.QualityGateDto;
this.uuidFactory = uuidFactory;
}
- public QualityGateDto create(DbSession dbSession, OrganizationDto organizationDto, String name) {
- validateQualityGate(dbSession, organizationDto, name);
+ public QualityGateDto create(DbSession dbSession, String name) {
+ validateQualityGate(dbSession, name);
QualityGateDto newQualityGate = new QualityGateDto()
.setName(name)
.setBuiltIn(false)
.setUuid(uuidFactory.create());
dbClient.qualityGateDao().insert(dbSession, newQualityGate);
- dbClient.qualityGateDao().associate(dbSession, uuidFactory.create(), organizationDto, newQualityGate);
+ dbClient.qualityGateDao().associate(dbSession, uuidFactory.create(), newQualityGate);
return newQualityGate;
}
- public QualityGateDto copy(DbSession dbSession, OrganizationDto organizationDto, QualityGateDto qualityGateDto, String destinationName) {
-
- QualityGateDto destinationGate = create(dbSession, organizationDto, destinationName);
-
+ public QualityGateDto copy(DbSession dbSession, QualityGateDto qualityGateDto, String destinationName) {
+ QualityGateDto destinationGate = create(dbSession, destinationName);
for (QualityGateConditionDto sourceCondition : dbClient.gateConditionDao().selectForQualityGate(dbSession, qualityGateDto.getUuid())) {
dbClient.gateConditionDao().insert(new QualityGateConditionDto()
- .setUuid(Uuids.create())
- .setQualityGateUuid(destinationGate.getUuid())
- .setMetricUuid(sourceCondition.getMetricUuid())
- .setOperator(sourceCondition.getOperator())
- .setErrorThreshold(sourceCondition.getErrorThreshold()),
+ .setUuid(Uuids.create())
+ .setQualityGateUuid(destinationGate.getUuid())
+ .setMetricUuid(sourceCondition.getMetricUuid())
+ .setOperator(sourceCondition.getOperator())
+ .setErrorThreshold(sourceCondition.getErrorThreshold()),
dbSession);
}
return destinationGate;
}
- private void validateQualityGate(DbSession dbSession, OrganizationDto organizationDto, String name) {
- checkQualityGateDoesNotAlreadyExist(dbSession, organizationDto, name);
+ private void validateQualityGate(DbSession dbSession, String name) {
+ checkQualityGateDoesNotAlreadyExist(dbSession, name);
}
- private void checkQualityGateDoesNotAlreadyExist(DbSession dbSession, OrganizationDto organizationDto, String name) {
- QualityGateDto existingQgate = dbClient.qualityGateDao().selectByOrganizationAndName(dbSession, organizationDto, name);
- checkArgument(existingQgate == null, IS_ALREADY_USED_MESSAGE, "Name");
+ private void checkQualityGateDoesNotAlreadyExist(DbSession dbSession, String name) {
+ QualityGateDto existingQGate = dbClient.qualityGateDao().selectByName(dbSession, name);
+ checkArgument(existingQGate == null, IS_ALREADY_USED_MESSAGE, "Name");
}
}
import org.sonar.api.server.ws.WebService;
import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
-import org.sonar.db.organization.OrganizationDto;
import org.sonar.db.qualitygate.QualityGateDto;
import org.sonar.server.qualitygate.QualityGateUpdater;
import org.sonar.server.user.UserSession;
.setDescription("The name of the quality gate to create")
.setRequired(true)
.setExampleValue("My New Quality Gate");
-
- wsSupport.createOrganizationParam(action);
}
@Override
try (DbSession dbSession = dbClient.openSession(false)) {
- OrganizationDto organization = wsSupport.getOrganization(dbSession, request);
userSession.checkPermission(ADMINISTER_QUALITY_GATES);
QualityGateDto qualityGate;
if (uuid != null) {
- qualityGate = wsSupport.getByOrganizationAndUuid(dbSession, organization, uuid);
+ qualityGate = wsSupport.getByUuid(dbSession, uuid);
} else {
- qualityGate = wsSupport.getByOrganizationAndName(dbSession, organization, sourceName);
+ qualityGate = wsSupport.getByName(dbSession, sourceName);
}
- QualityGateDto copy = qualityGateUpdater.copy(dbSession, organization, qualityGate, destinationName);
+ QualityGateDto copy = qualityGateUpdater.copy(dbSession, qualityGate, destinationName);
dbSession.commit();
writeProtobuf(newBuilder()
import org.sonar.api.server.ws.WebService;
import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
-import org.sonar.db.organization.OrganizationDto;
import org.sonar.db.permission.GlobalPermission;
import org.sonar.db.qualitygate.QualityGateDto;
import org.sonar.server.qualitygate.QualityGateUpdater;
private final DbClient dbClient;
private final UserSession userSession;
private final QualityGateUpdater qualityGateUpdater;
- private final QualityGatesWsSupport wsSupport;
- public CreateAction(DbClient dbClient, UserSession userSession, QualityGateUpdater qualityGateUpdater,
- QualityGatesWsSupport wsSupport) {
+ public CreateAction(DbClient dbClient, UserSession userSession, QualityGateUpdater qualityGateUpdater) {
this.dbClient = dbClient;
this.userSession = userSession;
this.qualityGateUpdater = qualityGateUpdater;
- this.wsSupport = wsSupport;
}
@Override
.setMaximumLength(NAME_MAXIMUM_LENGTH)
.setDescription("The name of the quality gate to create")
.setExampleValue("My Quality Gate");
-
- wsSupport.createOrganizationParam(action);
}
@Override
public void handle(Request request, Response response) {
try (DbSession dbSession = dbClient.openSession(false)) {
- OrganizationDto organizationDto = wsSupport.getOrganization(dbSession, request);
-
userSession.checkPermission(GlobalPermission.ADMINISTER_QUALITY_GATES);
String name = request.mandatoryParam(PARAM_NAME);
- QualityGateDto newQualityGate = qualityGateUpdater.create(dbSession, organizationDto, name);
+ QualityGateDto newQualityGate = qualityGateUpdater.create(dbSession, name);
CreateResponse.Builder createResponse = CreateResponse.newBuilder()
.setId(newQualityGate.getUuid())
.setName(newQualityGate.getName());
import org.sonar.api.server.ws.WebService;
import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
-import org.sonar.db.organization.OrganizationDto;
-import org.sonar.db.qualitygate.QGateWithOrgDto;
import org.sonar.db.qualitygate.QualityGateConditionDto;
+import org.sonar.db.qualitygate.QualityGateDto;
import org.sonar.server.qualitygate.QualityGateConditionsUpdater;
import org.sonarqube.ws.Qualitygates.CreateConditionResponse;
.setExampleValue("SonarSource way");
addConditionParams(createCondition);
- wsSupport.createOrganizationParam(createCondition);
}
@Override
checkArgument(gateName != null ^ gateUuid != null, "One of 'gateId' or 'gateName' must be provided, and not both");
try (DbSession dbSession = dbClient.openSession(false)) {
- OrganizationDto organization = wsSupport.getOrganization(dbSession, request);
- QGateWithOrgDto qualityGate;
+ QualityGateDto qualityGate;
if (gateUuid != null) {
- qualityGate = wsSupport.getByOrganizationAndUuid(dbSession, organization, gateUuid);
+ qualityGate = wsSupport.getByUuid(dbSession, gateUuid);
} else {
- qualityGate = wsSupport.getByOrganizationAndName(dbSession, organization, gateName);
+ qualityGate = wsSupport.getByName(dbSession, gateName);
}
wsSupport.checkCanEdit(qualityGate);
QualityGateConditionDto condition = qualityGateConditionsUpdater.createCondition(dbSession, qualityGate, metric, operator, error);
import org.sonar.api.server.ws.WebService;
import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
-import org.sonar.db.organization.OrganizationDto;
-import org.sonar.db.qualitygate.QGateWithOrgDto;
import org.sonar.db.qualitygate.QualityGateConditionDto;
+import org.sonar.db.qualitygate.QualityGateDto;
import static com.google.common.base.Preconditions.checkState;
import static org.sonar.server.qualitygate.ws.QualityGatesWsParameters.PARAM_ID;
.setRequired(true)
.setDescription("Condition UUID")
.setExampleValue("2");
-
- wsSupport.createOrganizationParam(createCondition);
}
@Override
public void handle(Request request, Response response) {
String conditionUuid = request.mandatoryParam(PARAM_ID);
try (DbSession dbSession = dbClient.openSession(false)) {
- OrganizationDto organization = wsSupport.getOrganization(dbSession, request);
QualityGateConditionDto condition = wsSupport.getCondition(dbSession, conditionUuid);
- QGateWithOrgDto qualityGateDto = dbClient.qualityGateDao().selectByOrganizationAndUuid(dbSession, organization, condition.getQualityGateUuid());
+ QualityGateDto qualityGateDto = dbClient.qualityGateDao().selectByUuid(dbSession, condition.getQualityGateUuid());
checkState(qualityGateDto != null, "Condition '%s' is linked to an unknown quality gate '%s'", conditionUuid, condition.getQualityGateUuid());
wsSupport.checkCanEdit(qualityGateDto);
import org.sonar.api.server.ws.WebService;
import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
-import org.sonar.db.organization.OrganizationDto;
import org.sonar.db.project.ProjectDto;
import static org.sonar.server.qualitygate.ws.QualityGatesWsParameters.PARAM_PROJECT_KEY;
.setDescription("Project key")
.setExampleValue(KEY_PROJECT_EXAMPLE_001)
.setSince("6.1");
-
- wsSupport.createOrganizationParam(action);
}
@Override
public void handle(Request request, Response response) {
try (DbSession dbSession = dbClient.openSession(false)) {
- OrganizationDto organization = wsSupport.getOrganization(dbSession, request);
- ProjectDto project = wsSupport.getProject(dbSession, organization, request.mandatoryParam(PARAM_PROJECT_KEY));
+ ProjectDto project = wsSupport.getProject(dbSession, request.mandatoryParam(PARAM_PROJECT_KEY));
dissociateProject(dbSession, project);
response.noContent();
}
import org.sonar.api.server.ws.WebService;
import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
-import org.sonar.db.organization.OrganizationDto;
-import org.sonar.db.qualitygate.QGateWithOrgDto;
import org.sonar.db.qualitygate.QualityGateDto;
import org.sonar.server.qualitygate.QualityGateFinder;
.setMaximumLength(NAME_MAXIMUM_LENGTH)
.setSince("8.4")
.setExampleValue("SonarSource Way");
-
- wsSupport.createOrganizationParam(action);
}
@Override
checkArgument(name != null ^ uuid != null, "One of 'id' or 'name' must be provided, and not both");
try (DbSession dbSession = dbClient.openSession(false)) {
- OrganizationDto organization = wsSupport.getOrganization(dbSession, request);
-
- QGateWithOrgDto qualityGate;
+ QualityGateDto qualityGate;
if (uuid != null) {
- qualityGate = wsSupport.getByOrganizationAndUuid(dbSession, organization, uuid);
+ qualityGate = wsSupport.getByUuid(dbSession, uuid);
} else {
- qualityGate = wsSupport.getByOrganizationAndName(dbSession, organization, name);
+ qualityGate = wsSupport.getByName(dbSession, name);
}
- QualityGateDto defaultQualityGate = finder.getDefault(dbSession, organization);
+ QualityGateDto defaultQualityGate = finder.getDefault(dbSession);
checkArgument(!defaultQualityGate.getUuid().equals(qualityGate.getUuid()), "The default quality gate cannot be removed");
wsSupport.checkCanEdit(qualityGate);
import org.sonar.api.server.ws.WebService;
import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
-import org.sonar.db.organization.OrganizationDto;
import org.sonar.db.project.ProjectDto;
import org.sonar.db.qualitygate.QualityGateDto;
import org.sonar.server.component.ComponentFinder;
-import org.sonar.server.exceptions.NotFoundException;
import org.sonar.server.qualitygate.QualityGateFinder;
import org.sonar.server.qualitygate.QualityGateFinder.QualityGateData;
import org.sonar.server.user.UserSession;
import org.sonarqube.ws.Qualitygates.GetByProjectResponse;
-import static java.lang.String.format;
import static org.sonar.api.web.UserRole.ADMIN;
import static org.sonar.api.web.UserRole.USER;
import static org.sonar.server.qualitygate.ws.QualityGatesWsParameters.ACTION_GET_BY_PROJECT;
private final DbClient dbClient;
private final ComponentFinder componentFinder;
private final QualityGateFinder qualityGateFinder;
- private final QualityGatesWsSupport wsSupport;
- public GetByProjectAction(UserSession userSession, DbClient dbClient, ComponentFinder componentFinder, QualityGateFinder qualityGateFinder, QualityGatesWsSupport wsSupport) {
+ public GetByProjectAction(UserSession userSession, DbClient dbClient, ComponentFinder componentFinder, QualityGateFinder qualityGateFinder) {
this.userSession = userSession;
this.dbClient = dbClient;
this.componentFinder = componentFinder;
this.qualityGateFinder = qualityGateFinder;
- this.wsSupport = wsSupport;
}
@Override
.setDescription("Project key")
.setExampleValue(KEY_PROJECT_EXAMPLE_001)
.setRequired(true);
-
- wsSupport.createOrganizationParam(action);
}
@Override
public void handle(Request request, Response response) throws Exception {
try (DbSession dbSession = dbClient.openSession(false)) {
- OrganizationDto organization = wsSupport.getOrganization(dbSession, request);
ProjectDto project = componentFinder.getProjectByKey(dbSession, request.mandatoryParam(PARAM_PROJECT));
- // As ComponentFinder doesn't handle organization yet, we only check here that the project belongs to the organization
- wsSupport.checkProjectBelongsToOrganization(organization, project);
if (!userSession.hasProjectPermission(USER, project) &&
!userSession.hasProjectPermission(ADMIN, project)) {
throw insufficientPrivilegesException();
}
- QualityGateData data = qualityGateFinder.getQualityGate(dbSession, organization, project)
- .orElseThrow(() -> new NotFoundException(format("Quality gate not found for project %s", project.getKey())));
+ QualityGateData data = qualityGateFinder.getQualityGate(dbSession, project);
writeProtobuf(buildResponse(data), request, response);
}
import org.sonar.api.server.ws.WebService;
import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
-import org.sonar.db.organization.OrganizationDto;
import org.sonar.db.qualitygate.QualityGateDto;
import org.sonar.server.qualitygate.QualityGateFinder;
import org.sonarqube.ws.Qualitygates.ListWsResponse;
@Override
public void define(WebService.NewController controller) {
- WebService.NewAction action = controller.createAction("list")
+ controller.createAction("list")
.setDescription("Get a list of quality gates")
.setSince("4.3")
.setResponseExample(Resources.getResource(this.getClass(), "list-example.json"))
new Change("7.0", "'isBuiltIn' field is added in the response"),
new Change("7.0", "'actions' fields are added in the response"))
.setHandler(this);
- wsSupport.createOrganizationParam(action);
}
@Override
public void handle(Request request, Response response) {
try (DbSession dbSession = dbClient.openSession(false)) {
- OrganizationDto organization = wsSupport.getOrganization(dbSession, request);
- QualityGateDto defaultQualityGate = finder.getDefault(dbSession, organization);
- Collection<QualityGateDto> qualityGates = dbClient.qualityGateDao().selectAll(dbSession, organization);
+ QualityGateDto defaultQualityGate = finder.getDefault(dbSession);
+ Collection<QualityGateDto> qualityGates = dbClient.qualityGateDao().selectAll(dbSession);
writeProtobuf(buildResponse(qualityGates, defaultQualityGate), request, response);
}
}
public static final String ACTION_CREATE_CONDITION = "create_condition";
public static final String ACTION_UPDATE_CONDITION = "update_condition";
- static final String PARAM_ORGANIZATION = "organization";
public static final String PARAM_ANALYSIS_ID = "analysisId";
public static final String PARAM_BRANCH = "branch";
public static final String PARAM_PULL_REQUEST = "pullRequest";
package org.sonar.server.qualitygate.ws;
import java.util.Objects;
-import java.util.Optional;
import javax.annotation.Nullable;
-import org.sonar.api.server.ws.Request;
-import org.sonar.api.server.ws.WebService;
-import org.sonar.api.server.ws.WebService.NewAction;
import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
-import org.sonar.db.organization.OrganizationDto;
import org.sonar.db.project.ProjectDto;
-import org.sonar.db.qualitygate.QGateWithOrgDto;
import org.sonar.db.qualitygate.QualityGateConditionDto;
import org.sonar.db.qualitygate.QualityGateDto;
import org.sonar.server.component.ComponentFinder;
-import org.sonar.server.exceptions.NotFoundException;
-import org.sonar.server.organization.DefaultOrganizationProvider;
import org.sonar.server.user.UserSession;
import org.sonarqube.ws.Qualitygates;
import static com.google.common.base.Preconditions.checkArgument;
-import static java.lang.String.format;
import static org.sonar.api.web.UserRole.ADMIN;
import static org.sonar.db.permission.GlobalPermission.ADMINISTER_QUALITY_GATES;
import static org.sonar.server.exceptions.NotFoundException.checkFound;
-import static org.sonar.server.exceptions.NotFoundException.checkFoundWithOptional;
-import static org.sonar.server.qualitygate.ws.QualityGatesWsParameters.PARAM_ORGANIZATION;
import static org.sonar.server.user.AbstractUserSession.insufficientPrivilegesException;
public class QualityGatesWsSupport {
private final DbClient dbClient;
private final UserSession userSession;
- private final DefaultOrganizationProvider defaultOrganizationProvider;
private final ComponentFinder componentFinder;
- public QualityGatesWsSupport(DbClient dbClient, UserSession userSession, DefaultOrganizationProvider defaultOrganizationProvider, ComponentFinder componentFinder) {
+ public QualityGatesWsSupport(DbClient dbClient, UserSession userSession, ComponentFinder componentFinder) {
this.dbClient = dbClient;
this.userSession = userSession;
- this.defaultOrganizationProvider = defaultOrganizationProvider;
this.componentFinder = componentFinder;
}
- public QGateWithOrgDto getByOrganizationAndUuid(DbSession dbSession, OrganizationDto organization, String qualityGateUuid) {
+ public QualityGateDto getByUuid(DbSession dbSession, String qualityGateUuid) {
return checkFound(
- dbClient.qualityGateDao().selectByOrganizationAndUuid(dbSession, organization, qualityGateUuid),
- "No quality gate has been found for id %s in organization %s", qualityGateUuid, organization.getName());
+ dbClient.qualityGateDao().selectByUuid(dbSession, qualityGateUuid),
+ "No quality gate has been found for id %s", qualityGateUuid);
}
- public QGateWithOrgDto getByOrganizationAndName(DbSession dbSession, OrganizationDto organization, String qualityGateName) {
- return checkFound(
- dbClient.qualityGateDao().selectByOrganizationAndName(dbSession, organization, qualityGateName),
- "No quality gate has been found for name %s in organization %s", qualityGateName, organization.getName());
+ public QualityGateDto getByName(DbSession dbSession, String qualityGateName) {
+ return checkFound(dbClient.qualityGateDao().selectByName(dbSession, qualityGateName),
+ "No quality gate has been found for name %s", qualityGateName);
}
QualityGateConditionDto getCondition(DbSession dbSession, String uuid) {
return userSession.hasPermission(ADMINISTER_QUALITY_GATES);
}
- WebService.NewParam createOrganizationParam(NewAction action) {
- return action
- .createParam(PARAM_ORGANIZATION)
- .setDescription("Organization key. If no organization is provided, the default organization is used.")
- .setSince("7.0")
- .setRequired(false)
- .setInternal(false)
- .setExampleValue("my-org");
- }
-
Qualitygates.Actions getActions(QualityGateDto qualityGate, @Nullable QualityGateDto defaultQualityGate) {
boolean isDefault = defaultQualityGate != null && Objects.equals(defaultQualityGate.getUuid(), qualityGate.getUuid());
boolean isBuiltIn = qualityGate.isBuiltIn();
.build();
}
- OrganizationDto getDefaultOrganization(DbSession dbSession) {
- return getOrganization(dbSession, defaultOrganizationProvider.get().getKey());
- }
-
- OrganizationDto getOrganization(DbSession dbSession, String key) {
- Optional<OrganizationDto> organizationDto = dbClient.organizationDao().selectByKey(dbSession, key);
- return checkFoundWithOptional(organizationDto, "No organization with key '%s'", key);
- }
-
- OrganizationDto getOrganization(DbSession dbSession, Request request) {
- String organizationKey = Optional.ofNullable(request.param(PARAM_ORGANIZATION))
- .orElseGet(() -> defaultOrganizationProvider.get().getKey());
- return getOrganization(dbSession, organizationKey);
- }
-
- void checkCanEdit(QGateWithOrgDto qualityGate) {
+ void checkCanEdit(QualityGateDto qualityGate) {
checkNotBuiltIn(qualityGate);
userSession.checkPermission(ADMINISTER_QUALITY_GATES);
}
throw insufficientPrivilegesException();
}
- ProjectDto getProject(DbSession dbSession, OrganizationDto organization, String projectKey) {
- ProjectDto project = componentFinder.getProjectByKey(dbSession, projectKey);
- checkProjectBelongsToOrganization(organization, project);
- return project;
- }
-
- void checkProjectBelongsToOrganization(OrganizationDto organization, ProjectDto project) {
- if (project.getOrganizationUuid().equals(organization.getUuid())) {
- return;
- }
- throw new NotFoundException(format("Project '%s' doesn't exist in organization '%s'", project.getKey(), organization.getKey()));
+ ProjectDto getProject(DbSession dbSession, String projectKey) {
+ return componentFinder.getProjectByKey(dbSession, projectKey);
}
private static void checkNotBuiltIn(QualityGateDto qualityGate) {
checkArgument(!qualityGate.isBuiltIn(), "Operation forbidden for built-in Quality Gate '%s'", qualityGate.getName());
}
-
}
import org.sonar.api.server.ws.WebService;
import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
-import org.sonar.db.organization.OrganizationDto;
-import org.sonar.db.qualitygate.QGateWithOrgDto;
import org.sonar.db.qualitygate.QualityGateDto;
import org.sonarqube.ws.Qualitygates.QualityGate;
.setMaximumLength(NAME_MAXIMUM_LENGTH)
.setDescription("New name of the quality gate")
.setExampleValue("My New Quality Gate");
-
- wsSupport.createOrganizationParam(action);
}
@Override
checkArgument(uuid != null ^ currentName != null, "One of 'id' or 'currentName' must be provided, and not both");
try (DbSession dbSession = dbClient.openSession(false)) {
- OrganizationDto organization = wsSupport.getOrganization(dbSession, request);
- QGateWithOrgDto qualityGate;
+ QualityGateDto qualityGate;
if (uuid != null) {
- qualityGate = wsSupport.getByOrganizationAndUuid(dbSession, organization, uuid);
+ qualityGate = wsSupport.getByUuid(dbSession, uuid);
} else {
- qualityGate = wsSupport.getByOrganizationAndName(dbSession, organization, currentName);
+ qualityGate = wsSupport.getByName(dbSession, currentName);
}
- QualityGateDto renamedQualityGate = rename(dbSession, organization, qualityGate, request.mandatoryParam(PARAM_NAME));
+ QualityGateDto renamedQualityGate = rename(dbSession, qualityGate, request.mandatoryParam(PARAM_NAME));
writeProtobuf(QualityGate.newBuilder()
.setId(renamedQualityGate.getUuid())
.setName(renamedQualityGate.getName())
}
}
- private QualityGateDto rename(DbSession dbSession, OrganizationDto organization, QGateWithOrgDto qualityGate, String name) {
+ private QualityGateDto rename(DbSession dbSession, QualityGateDto qualityGate, String name) {
wsSupport.checkCanEdit(qualityGate);
- checkNotAlreadyExists(dbSession, organization, qualityGate, name);
+ checkNotAlreadyExists(dbSession, qualityGate, name);
qualityGate.setName(name);
dbClient.qualityGateDao().update(qualityGate, dbSession);
dbSession.commit();
return qualityGate;
}
- private void checkNotAlreadyExists(DbSession dbSession, OrganizationDto organization, QualityGateDto qualityGate, String name) {
- QualityGateDto existingQgate = dbClient.qualityGateDao().selectByOrganizationAndName(dbSession, organization, name);
+ private void checkNotAlreadyExists(DbSession dbSession, QualityGateDto qualityGate, String name) {
+ QualityGateDto existingQgate = dbClient.qualityGateDao().selectByName(dbSession, name);
boolean isModifyingCurrentQgate = existingQgate == null || existingQgate.getUuid().equals(qualityGate.getUuid());
checkArgument(isModifyingCurrentQgate, "Name '%s' has already been taken", name);
}
import org.sonar.core.util.stream.MoreCollectors;
import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
-import org.sonar.db.organization.OrganizationDto;
import org.sonar.db.qualitygate.ProjectQgateAssociationDto;
import org.sonar.db.qualitygate.ProjectQgateAssociationQuery;
-import org.sonar.db.qualitygate.QGateWithOrgDto;
+import org.sonar.db.qualitygate.QualityGateDto;
import org.sonar.server.user.UserSession;
import org.sonarqube.ws.Qualitygates;
action.createParam(PARAM_PAGE_SIZE)
.setDescription("Page size")
.setExampleValue("10");
-
- wsSupport.createOrganizationParam(action);
}
@Override
public void handle(Request request, Response response) {
try (DbSession dbSession = dbClient.openSession(false)) {
- OrganizationDto organization = wsSupport.getOrganization(dbSession, request);
-
String gateUuid = request.param(PARAM_GATE_ID);
String gateName = request.param(PARAM_GATE_NAME);
checkArgument(gateName != null ^ gateUuid != null, "One of 'gateId' or 'gateName' must be provided, and not both");
- QGateWithOrgDto qualityGate;
+ QualityGateDto qualityGate;
if (gateUuid != null) {
- qualityGate = wsSupport.getByOrganizationAndUuid(dbSession, organization, gateUuid);
+ qualityGate = wsSupport.getByUuid(dbSession, gateUuid);
} else {
- qualityGate = wsSupport.getByOrganizationAndName(dbSession, organization, gateName);
+ qualityGate = wsSupport.getByName(dbSession, gateName);
}
ProjectQgateAssociationQuery projectQgateAssociationQuery = ProjectQgateAssociationQuery.builder()
import org.sonar.api.server.ws.WebService;
import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
-import org.sonar.db.organization.OrganizationDto;
import org.sonar.db.project.ProjectDto;
-import org.sonar.db.qualitygate.QGateWithOrgDto;
import org.sonar.db.qualitygate.QualityGateDto;
import static com.google.common.base.Preconditions.checkArgument;
.setDescription("Project key")
.setExampleValue(KEY_PROJECT_EXAMPLE_001)
.setSince("6.1");
-
- wsSupport.createOrganizationParam(action);
}
@Override
checkArgument(gateName != null ^ gateUuid != null, "Either 'gateId' or 'gateName' must be provided, and not both");
try (DbSession dbSession = dbClient.openSession(false)) {
- OrganizationDto organization = wsSupport.getOrganization(dbSession, request);
- QGateWithOrgDto qualityGate;
+ QualityGateDto qualityGate;
if (gateUuid != null) {
- qualityGate = wsSupport.getByOrganizationAndUuid(dbSession, organization, gateUuid);
+ qualityGate = wsSupport.getByUuid(dbSession, gateUuid);
} else {
- qualityGate = wsSupport.getByOrganizationAndName(dbSession, organization, gateName);
+ qualityGate = wsSupport.getByName(dbSession, gateName);
}
- ProjectDto project = wsSupport.getProject(dbSession, organization, projectKey);
+ ProjectDto project = wsSupport.getProject(dbSession, projectKey);
wsSupport.checkCanAdminProject(project);
QualityGateDto currentQualityGate = dbClient.qualityGateDao().selectByProjectUuid(dbSession, project.getUuid());
import org.sonar.api.server.ws.WebService;
import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
-import org.sonar.db.organization.OrganizationDto;
+import org.sonar.db.property.PropertyDto;
import org.sonar.db.qualitygate.QualityGateDto;
import org.sonar.server.user.UserSession;
import static org.sonar.server.qualitygate.ws.QualityGatesWsParameters.PARAM_NAME;
public class SetAsDefaultAction implements QualityGatesWsAction {
+ private static final String DEFAULT_QUALITY_GATE_PROPERTY_NAME = "qualitygate.default";
private final DbClient dbClient;
private final UserSession userSession;
.setMaximumLength(NAME_MAXIMUM_LENGTH)
.setSince("8.4")
.setExampleValue("SonarSource Way");
-
- wsSupport.createOrganizationParam(action);
}
@Override
checkArgument(name != null ^ uuid != null, "One of 'id' or 'name' must be provided, and not both");
try (DbSession dbSession = dbClient.openSession(false)) {
- OrganizationDto organization = wsSupport.getDefaultOrganization(dbSession);
userSession.checkPermission(ADMINISTER_QUALITY_GATES);
QualityGateDto qualityGate;
if (uuid != null) {
- qualityGate = wsSupport.getByOrganizationAndUuid(dbSession, organization, uuid);
+ qualityGate = wsSupport.getByUuid(dbSession, uuid);
} else {
- qualityGate = wsSupport.getByOrganizationAndName(dbSession, organization, name);
+ qualityGate = wsSupport.getByName(dbSession, name);
}
- organization.setDefaultQualityGateUuid(qualityGate.getUuid());
- dbClient.organizationDao().update(dbSession, organization);
+ dbClient.propertiesDao().saveProperty(new PropertyDto().setKey(DEFAULT_QUALITY_GATE_PROPERTY_NAME).setValue(qualityGate.getUuid()));
dbSession.commit();
}
import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
import org.sonar.db.metric.MetricDto;
-import org.sonar.db.organization.OrganizationDto;
import org.sonar.db.qualitygate.QualityGateConditionDto;
import org.sonar.db.qualitygate.QualityGateDto;
import org.sonar.server.qualitygate.QualityGateFinder;
import static org.sonar.core.util.stream.MoreCollectors.toList;
import static org.sonar.core.util.stream.MoreCollectors.toSet;
import static org.sonar.core.util.stream.MoreCollectors.uniqueIndex;
-import static org.sonar.server.exceptions.NotFoundException.checkFound;
import static org.sonar.server.qualitygate.ws.QualityGatesWsParameters.PARAM_ID;
import static org.sonar.server.qualitygate.ws.QualityGatesWsParameters.PARAM_NAME;
import static org.sonar.server.ws.WsUtils.writeProtobuf;
action.createParam(PARAM_NAME)
.setDescription("Name of the quality gate. Either id or name must be set")
.setExampleValue("My Quality Gate");
-
- wsSupport.createOrganizationParam(action);
}
@Override
checkOneOfIdOrNamePresent(id, name);
try (DbSession dbSession = dbClient.openSession(false)) {
- OrganizationDto organization = wsSupport.getOrganization(dbSession, request);
- QualityGateDto qualityGate = getByNameOrUuid(dbSession, organization, name, id);
+ QualityGateDto qualityGate = getByNameOrUuid(dbSession, name, id);
Collection<QualityGateConditionDto> conditions = getConditions(dbSession, qualityGate);
Map<String, MetricDto> metricsByUuid = getMetricsByUuid(dbSession, conditions);
- QualityGateDto defaultQualityGate = qualityGateFinder.getDefault(dbSession, organization);
+ QualityGateDto defaultQualityGate = qualityGateFinder.getDefault(dbSession);
writeProtobuf(buildResponse(qualityGate, defaultQualityGate, conditions, metricsByUuid), request, response);
}
}
- private QualityGateDto getByNameOrUuid(DbSession dbSession, OrganizationDto organization, @Nullable String name, @Nullable String uuid) {
+ private QualityGateDto getByNameOrUuid(DbSession dbSession, @Nullable String name, @Nullable String uuid) {
if (name != null) {
- return checkFound(dbClient.qualityGateDao().selectByOrganizationAndName(dbSession, organization, name), "No quality gate has been found for name %s", name);
+ return wsSupport.getByName(dbSession, name);
}
if (uuid != null) {
- return wsSupport.getByOrganizationAndUuid(dbSession, organization, uuid);
+ return wsSupport.getByUuid(dbSession, uuid);
}
throw new IllegalArgumentException("No parameter has been set to identify a quality gate");
}
import org.sonar.api.server.ws.WebService;
import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
-import org.sonar.db.organization.OrganizationDto;
-import org.sonar.db.qualitygate.QGateWithOrgDto;
import org.sonar.db.qualitygate.QualityGateConditionDto;
+import org.sonar.db.qualitygate.QualityGateDto;
import org.sonar.server.qualitygate.QualityGateConditionsUpdater;
import org.sonarqube.ws.Qualitygates.UpdateConditionResponse;
.setExampleValue(UUID_EXAMPLE_01);
addConditionParams(createCondition);
- wsSupport.createOrganizationParam(createCondition);
}
@Override
String error = request.mandatoryParam(PARAM_ERROR);
try (DbSession dbSession = dbClient.openSession(false)) {
- OrganizationDto organization = wsSupport.getOrganization(dbSession, request);
QualityGateConditionDto condition = wsSupport.getCondition(dbSession, id);
- QGateWithOrgDto qualityGateDto = dbClient.qualityGateDao().selectByOrganizationAndUuid(dbSession, organization, condition.getQualityGateUuid());
+ QualityGateDto qualityGateDto = dbClient.qualityGateDao().selectByUuid(dbSession, condition.getQualityGateUuid());
checkState(qualityGateDto != null, "Condition '%s' is linked to an unknown quality gate '%s'", id, condition.getQualityGateUuid());
wsSupport.checkCanEdit(qualityGateDto);
QualityGateConditionDto updatedCondition = qualityGateConditionsUpdater.updateCondition(dbSession, condition, metric, operator, error);
package org.sonar.server.qualitygate.ws;
import javax.annotation.ParametersAreNonnullByDefault;
-
import org.sonar.db.component.ComponentDto;
import org.sonar.db.component.SnapshotDto;
import org.sonar.db.measure.LiveMeasureDto;
-import org.sonar.db.organization.OrganizationDto;
import org.sonar.db.permission.GlobalPermission;
import org.sonar.db.property.PropertyDto;
import org.sonar.db.property.PropertyQuery;
import org.sonar.db.qualityprofile.QProfileDto;
import org.sonar.server.component.ComponentFinder;
import org.sonar.server.exceptions.BadRequestException;
-import org.sonar.server.exceptions.NotFoundException;
import org.sonar.server.project.Visibility;
import org.sonar.server.qualitygate.QualityGateFinder;
import org.sonar.server.qualityprofile.QPMeasureData;
import org.sonar.server.ui.PageRepository;
import org.sonar.server.user.UserSession;
-import static java.lang.String.format;
import static java.util.Collections.emptySortedSet;
import static org.sonar.api.measures.CoreMetrics.QUALITY_PROFILES_KEY;
import static org.sonar.api.utils.DateUtils.formatDateTime;
}
private void writeQualityGate(JsonWriter json, DbSession session, ComponentDto component) {
- // TODO:: remove while dropping for quality gate
- OrganizationDto organizationDto = dbClient.organizationDao().selectByUuid(session, component.getOrganizationUuid())
- .orElseThrow(IllegalStateException::new);
- QualityGateFinder.QualityGateData qualityGateData = qualityGateFinder.getQualityGate(session, organizationDto, component.uuid())
- .orElseThrow(() -> new NotFoundException(format("Quality Gate not found for %s", component.getKey())));
+ QualityGateFinder.QualityGateData qualityGateData = qualityGateFinder.getQualityGate(session, component.uuid());
QualityGateDto qualityGateDto = qualityGateData.getQualityGate();
json.name("qualityGate").beginObject()
.prop("key", qualityGateDto.getUuid())
@Rule
public ExpectedException expectedException = ExpectedException.none();
- private TestProjectIndexers projectIndexer = new TestProjectIndexers();
+ private final TestProjectIndexers projectIndexer = new TestProjectIndexers();
private MetricDto intMetric;
private MetricDto ratingMetric;
private MetricDto alertStatusMetric;
private ComponentDto file2;
private ComponentDto branch;
private ComponentDto branchFile;
- private LiveQualityGateComputer qGateComputer = mock(LiveQualityGateComputer.class);
- private QualityGate qualityGate = mock(QualityGate.class);
- private EvaluatedQualityGate newQualityGate = mock(EvaluatedQualityGate.class);
+ private final LiveQualityGateComputer qGateComputer = mock(LiveQualityGateComputer.class);
+ private final QualityGate qualityGate = mock(QualityGate.class);
+ private final EvaluatedQualityGate newQualityGate = mock(EvaluatedQualityGate.class);
@Before
public void setUp() {
.extracting(QGChangeEvent::getQualityGateSupplier)
.extracting(Supplier::get)
.containsExactly(Optional.of(newQualityGate));
- verify(qGateComputer).loadQualityGate(any(DbSession.class), eq(organization), argThat(p -> p.getUuid().equals(projectDto.getUuid())), eq(branch));
+ verify(qGateComputer).loadQualityGate(any(DbSession.class), argThat(p -> p.getUuid().equals(projectDto.getUuid())), eq(branch));
verify(qGateComputer).getMetricsRelatedTo(qualityGate);
verify(qGateComputer).refreshGateStatus(eq(project), same(qualityGate), any(MeasureMatrix.class));
}
private List<QGChangeEvent> run(Collection<ComponentDto> components, IssueMetricFormula... formulas) {
IssueMetricFormulaFactory formulaFactory = new TestIssueMetricFormulaFactory(asList(formulas));
- when(qGateComputer.loadQualityGate(any(DbSession.class), any(OrganizationDto.class), any(ProjectDto.class), any(BranchDto.class)))
+ when(qGateComputer.loadQualityGate(any(DbSession.class), any(ProjectDto.class), any(BranchDto.class)))
.thenReturn(qualityGate);
when(qGateComputer.getMetricsRelatedTo(qualityGate)).thenReturn(singleton(CoreMetrics.ALERT_STATUS_KEY));
when(qGateComputer.refreshGateStatus(eq(project), same(qualityGate), any(MeasureMatrix.class)))
assertThat(measure.getComponentUuid()).isEqualTo(component.uuid());
assertThat(measure.getProjectUuid()).isEqualTo(component.projectUuid());
assertThat(measure.getMetricUuid()).isEqualTo(ratingMetric.getUuid());
- assertThat(measure.getVariation()).isEqualTo((double) expectedValue.getIndex());
+ assertThat(measure.getVariation()).isEqualTo(expectedValue.getIndex());
}
private IssueMetricFormula newIncrementalFormula() {
import org.sonar.db.component.ComponentTesting;
import org.sonar.db.measure.LiveMeasureDto;
import org.sonar.db.metric.MetricDto;
-import org.sonar.db.organization.OrganizationDto;
import org.sonar.db.project.ProjectDto;
-import org.sonar.db.qualitygate.QGateWithOrgDto;
import org.sonar.db.qualitygate.QualityGateConditionDto;
+import org.sonar.db.qualitygate.QualityGateDto;
import org.sonar.server.qualitygate.Condition;
import org.sonar.server.qualitygate.EvaluatedCondition;
import org.sonar.server.qualitygate.EvaluatedQualityGate;
@Rule
public DbTester db = DbTester.create();
- private TestQualityGateEvaluator qualityGateEvaluator = new TestQualityGateEvaluator();
- private LiveQualityGateComputerImpl underTest = new LiveQualityGateComputerImpl(db.getDbClient(), new QualityGateFinder(db.getDbClient()), qualityGateEvaluator);
+ private final TestQualityGateEvaluator qualityGateEvaluator = new TestQualityGateEvaluator();
+ private final LiveQualityGateComputerImpl underTest = new LiveQualityGateComputerImpl(db.getDbClient(), new QualityGateFinder(db.getDbClient()), qualityGateEvaluator);
@Test
public void loadQualityGate_returns_hardcoded_gate_for_pull_requests() {
- OrganizationDto organization = db.organizations().insert();
- ProjectDto project = db.components().insertPublicProjectDto(organization);
+ ProjectDto project = db.components().insertPublicProjectDto();
BranchDto branch = db.components().insertProjectBranch(project, b -> b.setBranchType(BranchType.PULL_REQUEST));
MetricDto metric1 = db.measures().insertMetric(m -> m.setKey("new_metric"));
MetricDto metric2 = db.measures().insertMetric(m -> m.setKey("metric"));
- QGateWithOrgDto gate = db.qualityGates().insertQualityGate(organization);
- db.qualityGates().setDefaultQualityGate(organization, gate);
+ QualityGateDto gate = db.qualityGates().insertQualityGate();
+ db.qualityGates().setDefaultQualityGate(gate);
db.qualityGates().addCondition(gate, metric1);
db.qualityGates().addCondition(gate, metric2);
- QualityGate result = underTest.loadQualityGate(db.getSession(), organization, project, branch);
+ QualityGate result = underTest.loadQualityGate(db.getSession(), project, branch);
assertThat(result.getConditions()).extracting(Condition::getMetricKey).containsExactly("new_metric");
}
@Test
public void loadQualityGate_on_branch_returns_organization_default_gate() {
- OrganizationDto organization = db.organizations().insert();
- ProjectDto project = db.components().insertPublicProjectDto(organization);
+ ProjectDto project = db.components().insertPublicProjectDto();
BranchDto branch = db.components().insertProjectBranch(project).setBranchType(BranchType.BRANCH);
MetricDto metric = db.measures().insertMetric();
- QGateWithOrgDto gate = db.qualityGates().insertQualityGate(organization);
- db.qualityGates().setDefaultQualityGate(organization, gate);
+ QualityGateDto gate = db.qualityGates().insertQualityGate();
+ db.qualityGates().setDefaultQualityGate(gate);
QualityGateConditionDto condition = db.qualityGates().addCondition(gate, metric);
- QualityGate result = underTest.loadQualityGate(db.getSession(), organization, project, branch);
+ QualityGate result = underTest.loadQualityGate(db.getSession(), project, branch);
assertThat(result.getId()).isEqualTo("" + gate.getUuid());
assertThat(result.getConditions())
@Rule
public DbTester db = DbTester.create(System2.INSTANCE);
- private QualityGateConditionsUpdater underTest = new QualityGateConditionsUpdater(db.getDbClient());
+ private final QualityGateConditionsUpdater underTest = new QualityGateConditionsUpdater(db.getDbClient());
@Test
public void create_error_condition() {
MetricDto metric = insertMetric(INT, "new_coverage");
- QualityGateDto qualityGate = db.qualityGates().insertQualityGate(db.getDefaultOrganization());
+ QualityGateDto qualityGate = db.qualityGates().insertQualityGate();
QualityGateConditionDto result = underTest.createCondition(db.getSession(), qualityGate, metric.getKey(), "LT", "80");
@UseDataProvider("valid_operators_and_direction")
public void create_condition_with_valid_operators_and_direction(String operator, int direction) {
MetricDto metric = db.measures().insertMetric(m -> m.setKey("key").setValueType(INT.name()).setHidden(false).setDirection(direction));
- QualityGateDto qualityGate = db.qualityGates().insertQualityGate(db.getDefaultOrganization());
+ QualityGateDto qualityGate = db.qualityGates().insertQualityGate();
QualityGateConditionDto result = underTest.createCondition(db.getSession(), qualityGate, metric.getKey(), operator, "80");
@Test
public void create_condition_throws_NPE_if_errorThreshold_is_null() {
MetricDto metric = insertMetric(RATING, SQALE_RATING_KEY);
- QualityGateDto qualityGate = db.qualityGates().insertQualityGate(db.getDefaultOrganization());
+ QualityGateDto qualityGate = db.qualityGates().insertQualityGate();
expectedException.expect(NullPointerException.class);
expectedException.expectMessage("errorThreshold can not be null");
@Test
public void fail_to_create_condition_when_condition_on_same_metric_already_exist() {
MetricDto metric = insertMetric(PERCENT);
- QualityGateDto qualityGate = db.qualityGates().insertQualityGate(db.getDefaultOrganization());
+ QualityGateDto qualityGate = db.qualityGates().insertQualityGate();
db.qualityGates().addCondition(qualityGate, metric);
expectedException.expect(BadRequestException.class);
@Test
public void fail_to_create_condition_on_missing_metric() {
- QualityGateDto qualityGate = db.qualityGates().insertQualityGate(db.getDefaultOrganization());
+ QualityGateDto qualityGate = db.qualityGates().insertQualityGate();
expectedException.expect(NotFoundException.class);
expectedException.expectMessage("There is no metric with key=new_coverage");
@UseDataProvider("invalid_metrics")
public void fail_to_create_condition_on_invalid_metric(String metricKey, Metric.ValueType valueType, boolean hidden) {
MetricDto metric = db.measures().insertMetric(m -> m.setKey(metricKey).setValueType(valueType.name()).setHidden(hidden).setDirection(0));
- QualityGateDto qualityGate = db.qualityGates().insertQualityGate(db.getDefaultOrganization());
+ QualityGateDto qualityGate = db.qualityGates().insertQualityGate();
expectedException.expect(BadRequestException.class);
expectedException.expectMessage(format("Metric '%s' cannot be used to define a condition", metric.getKey()));
@UseDataProvider("invalid_operators_and_direction")
public void fail_to_create_condition_on_not_allowed_operator_for_metric_direction(String operator, int direction) {
MetricDto metric = db.measures().insertMetric(m -> m.setKey("key").setValueType(INT.name()).setHidden(false).setDirection(direction));
- QualityGateDto qualityGate = db.qualityGates().insertQualityGate(db.getDefaultOrganization());
+ QualityGateDto qualityGate = db.qualityGates().insertQualityGate();
expectedException.expect(BadRequestException.class);
expectedException.expectMessage(format("Operator %s is not allowed for this metric.", operator));
@Test
public void create_condition_on_rating_metric() {
MetricDto metric = insertMetric(RATING, SQALE_RATING_KEY);
- QualityGateDto qualityGate = db.qualityGates().insertQualityGate(db.getDefaultOrganization());
+ QualityGateDto qualityGate = db.qualityGates().insertQualityGate();
QualityGateConditionDto result = underTest.createCondition(db.getSession(), qualityGate, metric.getKey(), "GT", "3");
@Test
public void fail_to_create_error_condition_on_invalid_rating_metric() {
MetricDto metric = insertMetric(RATING, SQALE_RATING_KEY);
- QualityGateDto qualityGate = db.qualityGates().insertQualityGate(db.getDefaultOrganization());
+ QualityGateDto qualityGate = db.qualityGates().insertQualityGate();
expectedException.expect(BadRequestException.class);
expectedException.expectMessage("'80' is not a valid rating");
@Test
public void fail_to_create_condition_on_rating_greater_than_E() {
MetricDto metric = insertMetric(RATING, SQALE_RATING_KEY);
- QualityGateDto qualityGate = db.qualityGates().insertQualityGate(db.getDefaultOrganization());
+ QualityGateDto qualityGate = db.qualityGates().insertQualityGate();
expectedException.expect(BadRequestException.class);
expectedException.expectMessage("There's no worse rating than E (5)");
@UseDataProvider("valid_values")
public void create_error_condition(Metric.ValueType valueType, String value) {
MetricDto metric = db.measures().insertMetric(m -> m.setValueType(valueType.name()).setHidden(false).setDirection(0));
- QualityGateDto qualityGate = db.qualityGates().insertQualityGate(db.getDefaultOrganization());
+ QualityGateDto qualityGate = db.qualityGates().insertQualityGate();
QualityGateConditionDto result = underTest.createCondition(db.getSession(), qualityGate, metric.getKey(), "LT", value);
@UseDataProvider("invalid_values")
public void fail_to_create_error_INT_condition_when_value_is_not_an_integer(Metric.ValueType valueType, String value) {
MetricDto metric = db.measures().insertMetric(m -> m.setValueType(valueType.name()).setHidden(false).setDirection(0));
- QualityGateDto qualityGate = db.qualityGates().insertQualityGate(db.getDefaultOrganization());
+ QualityGateDto qualityGate = db.qualityGates().insertQualityGate();
expectedException.expect(BadRequestException.class);
expectedException.expectMessage(format("Invalid value '%s' for metric '%s'", value, metric.getShortName()));
@Test
public void update_condition() {
MetricDto metric = insertMetric(PERCENT);
- QualityGateDto qualityGate = db.qualityGates().insertQualityGate(db.getDefaultOrganization());
+ QualityGateDto qualityGate = db.qualityGates().insertQualityGate();
QualityGateConditionDto condition = db.qualityGates().addCondition(qualityGate, metric,
c -> c.setOperator("LT").setErrorThreshold("80"));
@Test
public void update_condition_throws_NPE_if_errorThreshold_is_null() {
MetricDto metric = insertMetric(PERCENT);
- QualityGateDto qualityGate = db.qualityGates().insertQualityGate(db.getDefaultOrganization());
+ QualityGateDto qualityGate = db.qualityGates().insertQualityGate();
QualityGateConditionDto condition = db.qualityGates().addCondition(qualityGate, metric,
c -> c.setOperator("LT").setErrorThreshold("80"));
@Test
public void update_condition_on_rating_metric() {
MetricDto metric = insertMetric(RATING, SQALE_RATING_KEY);
- QualityGateDto qualityGate = db.qualityGates().insertQualityGate(db.getDefaultOrganization());
+ QualityGateDto qualityGate = db.qualityGates().insertQualityGate();
QualityGateConditionDto condition = db.qualityGates().addCondition(qualityGate, metric,
c -> c.setOperator("LT").setErrorThreshold("80"));
@UseDataProvider("update_invalid_operators_and_direction")
public void fail_to_update_condition_on_not_allowed_operator_for_metric_direction(String validOperator, String updatedOperator, int direction) {
MetricDto metric = db.measures().insertMetric(m -> m.setValueType(PERCENT.name()).setHidden(false).setDirection(direction));
- QualityGateDto qualityGate = db.qualityGates().insertQualityGate(db.getDefaultOrganization());
+ QualityGateDto qualityGate = db.qualityGates().insertQualityGate();
QualityGateConditionDto condition = db.qualityGates().addCondition(qualityGate, metric,
c -> c.setOperator(validOperator).setErrorThreshold("80"));
@Test
public void fail_to_update_condition_on_rating_metric_on_new_code_period() {
MetricDto metric = insertMetric(RATING, SQALE_RATING_KEY);
- QualityGateDto qualityGate = db.qualityGates().insertQualityGate(db.getDefaultOrganization());
+ QualityGateDto qualityGate = db.qualityGates().insertQualityGate();
QualityGateConditionDto condition = db.qualityGates().addCondition(qualityGate, metric,
c -> c.setOperator("LT").setErrorThreshold("3"));
@Test
public void fail_to_update_condition_on_rating_metric_on_not_core_rating_metric() {
MetricDto metric = insertMetric(RATING, "not_core_rating_metric");
- QualityGateDto qualityGate = db.qualityGates().insertQualityGate(db.getDefaultOrganization());
+ QualityGateDto qualityGate = db.qualityGates().insertQualityGate();
QualityGateConditionDto condition = db.qualityGates().addCondition(qualityGate, metric,
c -> c.setOperator("LT").setErrorThreshold("3"));
@UseDataProvider("invalid_metrics")
public void fail_to_update_condition_on_invalid_metric(String metricKey, Metric.ValueType valueType, boolean hidden) {
MetricDto metric = db.measures().insertMetric(m -> m.setKey(metricKey).setValueType(valueType.name()).setHidden(hidden));
- QualityGateDto qualityGate = db.qualityGates().insertQualityGate(db.getDefaultOrganization());
+ QualityGateDto qualityGate = db.qualityGates().insertQualityGate();
QualityGateConditionDto condition = db.qualityGates().addCondition(qualityGate, metric,
c -> c.setOperator("LT").setErrorThreshold("80"));
@UseDataProvider("valid_values")
public void update_error_condition(Metric.ValueType valueType, String value) {
MetricDto metric = db.measures().insertMetric(m -> m.setValueType(valueType.name()).setHidden(false).setDirection(0));
- QualityGateDto qualityGate = db.qualityGates().insertQualityGate(db.getDefaultOrganization());
+ QualityGateDto qualityGate = db.qualityGates().insertQualityGate();
QualityGateConditionDto condition = db.qualityGates().addCondition(qualityGate, metric,
c -> c.setOperator("LT").setErrorThreshold("80"));
@UseDataProvider("invalid_values")
public void fail_to_update_error_INT_condition_when_value_is_not_an_integer(Metric.ValueType valueType, String value) {
MetricDto metric = db.measures().insertMetric(m -> m.setValueType(valueType.name()).setHidden(false).setDirection(0));
- QualityGateDto qualityGate = db.qualityGates().insertQualityGate(db.getDefaultOrganization());
+ QualityGateDto qualityGate = db.qualityGates().insertQualityGate();
QualityGateConditionDto condition = db.qualityGates().addCondition(qualityGate, metric,
c -> c.setOperator("LT").setErrorThreshold("80"));
import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
import org.sonar.db.DbTester;
-import org.sonar.db.organization.OrganizationDto;
import org.sonar.db.qualitygate.QualityGateDto;
import static org.assertj.core.api.Assertions.assertThat;
@Rule
public DbTester db = DbTester.create(System2.INSTANCE);
- private DbClient dbClient = db.getDbClient();
- private DbSession dbSession = db.getSession();
- private QualityGateUpdater underTest = new QualityGateUpdater(dbClient, UuidFactoryFast.getInstance());
+ private final DbClient dbClient = db.getDbClient();
+ private final DbSession dbSession = db.getSession();
+ private final QualityGateUpdater underTest = new QualityGateUpdater(dbClient, UuidFactoryFast.getInstance());
@Test
public void create_quality_gate() {
- OrganizationDto organization = db.organizations().insert();
-
- QualityGateDto result = underTest.create(dbSession, organization, QGATE_NAME);
+ QualityGateDto result = underTest.create(dbSession, QGATE_NAME);
assertThat(result).isNotNull();
assertThat(result.getName()).isEqualTo(QGATE_NAME);
@Test
public void fail_to_create_when_name_already_exists() {
- OrganizationDto org = db.organizations().insert();
- underTest.create(dbSession, org, QGATE_NAME);
+ underTest.create(dbSession, QGATE_NAME);
expectedException.expect(IllegalArgumentException.class);
expectedException.expectMessage("Name has already been taken");
- underTest.create(dbSession, org, QGATE_NAME);
+ underTest.create(dbSession, QGATE_NAME);
}
}
private static final String BUILT_IN_NAME = "Sonar way";
- private DbClient dbClient = db.getDbClient();
- private DbSession dbSession = db.getSession();
+ private final DbClient dbClient = db.getDbClient();
+ private final DbSession dbSession = db.getSession();
- private QualityGateDao qualityGateDao = dbClient.qualityGateDao();
- private QualityGateConditionDao gateConditionDao = dbClient.gateConditionDao();
- private MetricDao metricDao = dbClient.metricDao();
- private QualityGateConditionsUpdater qualityGateConditionsUpdater = new QualityGateConditionsUpdater(dbClient);
- private QualityGateFinder qualityGateFinder = new QualityGateFinder(dbClient);
+ private final QualityGateDao qualityGateDao = dbClient.qualityGateDao();
+ private final QualityGateConditionDao gateConditionDao = dbClient.gateConditionDao();
+ private final MetricDao metricDao = dbClient.metricDao();
+ private final QualityGateConditionsUpdater qualityGateConditionsUpdater = new QualityGateConditionsUpdater(dbClient);
+ private final QualityGateFinder qualityGateFinder = new QualityGateFinder(dbClient);
- private RegisterQualityGates underTest = new RegisterQualityGates(dbClient, qualityGateConditionsUpdater,
+ private final RegisterQualityGates underTest = new RegisterQualityGates(dbClient, qualityGateConditionsUpdater,
UuidFactoryFast.getInstance(), System2.INSTANCE);
@Test
MetricDto newDuplication = metricDao.selectByKey(dbSession, NEW_DUPLICATED_LINES_DENSITY_KEY);
MetricDto newSecurityHotspots = metricDao.selectByKey(dbSession, NEW_SECURITY_HOTSPOTS_REVIEWED_KEY);
-
QualityGateDto qualityGateDto = qualityGateDao.selectByName(dbSession, BUILT_IN_NAME);
assertThat(qualityGateDto).isNotNull();
assertThat(qualityGateDto.getCreatedAt()).isNotNull();
import org.sonar.db.DbSession;
import org.sonar.db.DbTester;
import org.sonar.db.metric.MetricDto;
-import org.sonar.db.organization.OrganizationDto;
-import org.sonar.db.qualitygate.QGateWithOrgDto;
import org.sonar.db.qualitygate.QualityGateConditionDto;
import org.sonar.db.qualitygate.QualityGateDto;
import org.sonar.server.component.TestComponentFinder;
import org.sonar.server.exceptions.ForbiddenException;
import org.sonar.server.exceptions.NotFoundException;
-import org.sonar.server.organization.TestDefaultOrganizationProvider;
import org.sonar.server.qualitygate.QualityGateUpdater;
import org.sonar.server.tester.UserSessionRule;
import org.sonar.server.ws.TestRequest;
import org.sonar.server.ws.WsActionTester;
import org.sonarqube.ws.Qualitygates.QualityGate;
-import static java.lang.String.format;
import static java.util.Optional.ofNullable;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.AssertionsForClassTypes.tuple;
import static org.sonar.db.permission.GlobalPermission.ADMINISTER_QUALITY_PROFILES;
import static org.sonar.server.qualitygate.ws.QualityGatesWsParameters.PARAM_ID;
import static org.sonar.server.qualitygate.ws.QualityGatesWsParameters.PARAM_NAME;
-import static org.sonar.server.qualitygate.ws.QualityGatesWsParameters.PARAM_ORGANIZATION;
@RunWith(DataProviderRunner.class)
public class CopyActionTest {
public ExpectedException expectedException = ExpectedException.none();
@Rule
- public UserSessionRule userSession = UserSessionRule.standalone();
+ public final UserSessionRule userSession = UserSessionRule.standalone();
@Rule
- public DbTester db = DbTester.create(System2.INSTANCE);
+ public final DbTester db = DbTester.create(System2.INSTANCE);
- private DbClient dbClient = db.getDbClient();
- 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, TestComponentFinder.from(db));
+ private final DbClient dbClient = db.getDbClient();
+ private final DbSession dbSession = db.getSession();
+ private final QualityGateUpdater qualityGateUpdater = new QualityGateUpdater(dbClient, UuidFactoryFast.getInstance());
+ private final QualityGatesWsSupport wsSupport = new QualityGatesWsSupport(dbClient, userSession, TestComponentFinder.from(db));
- private CopyAction underTest = new CopyAction(dbClient, userSession, qualityGateUpdater, wsSupport);
- private WsActionTester ws = new WsActionTester(underTest);
+ private final CopyAction underTest = new CopyAction(dbClient, userSession, qualityGateUpdater, wsSupport);
+ private final WsActionTester ws = new WsActionTester(underTest);
@Test
public void definition() {
.containsExactlyInAnyOrder(
tuple("id", false),
tuple("sourceName", false),
- tuple("organization", false),
tuple("name", true));
}
@Test
public void copy() {
-
- OrganizationDto organization = db.organizations().insert();
userSession.addPermission(ADMINISTER_QUALITY_GATES);
- QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(organization);
+ QualityGateDto qualityGate = db.qualityGates().insertQualityGate();
MetricDto metric = db.measures().insertMetric();
QualityGateConditionDto condition = db.qualityGates().addCondition(qualityGate, metric);
ws.newRequest()
.setParam(PARAM_ID, qualityGate.getUuid())
.setParam(PARAM_NAME, "new-name")
- .setParam(PARAM_ORGANIZATION, organization.getKey())
.execute();
- QGateWithOrgDto actual = db.getDbClient().qualityGateDao().selectByOrganizationAndName(dbSession, organization, "new-name");
+ QualityGateDto actual = db.getDbClient().qualityGateDao().selectByName(dbSession, "new-name");
assertThat(actual).isNotNull();
assertThat(actual.isBuiltIn()).isFalse();
assertThat(actual.getUuid()).isNotEqualTo(qualityGate.getUuid());
.containsExactlyInAnyOrder(tuple(metric.getUuid(), condition.getErrorThreshold()));
}
- @Test
- public void default_organization_is_used_when_no_organization_parameter() {
- OrganizationDto defaultOrganization = db.getDefaultOrganization();
- userSession.addPermission(ADMINISTER_QUALITY_GATES);
- QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(defaultOrganization);
-
- ws.newRequest()
- .setParam(PARAM_ID, qualityGate.getUuid())
- .setParam(PARAM_NAME, "new-name")
- .execute();
-
- QGateWithOrgDto actual = db.getDbClient().qualityGateDao().selectByOrganizationAndName(dbSession, defaultOrganization, "new-name");
- assertThat(actual).isNotNull();
- assertThat(actual.getOrganizationUuid()).isEqualTo(defaultOrganization.getUuid());
- }
-
@Test
public void copy_of_builtin_should_not_be_builtin() {
- OrganizationDto organization = db.organizations().insert();
userSession.addPermission(ADMINISTER_QUALITY_GATES);
- QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(organization, qualityGateDto -> qualityGateDto.setBuiltIn(true));
+ QualityGateDto qualityGate = db.qualityGates().insertQualityGate(qualityGateDto -> qualityGateDto.setBuiltIn(true));
ws.newRequest()
.setParam(PARAM_ID, qualityGate.getUuid())
.setParam(PARAM_NAME, "new-name")
- .setParam(PARAM_ORGANIZATION, organization.getKey())
.execute();
QualityGateDto actual = db.getDbClient().qualityGateDao().selectByName(dbSession, "new-name");
@Test
public void response_contains_quality_gate() {
userSession.addPermission(ADMINISTER_QUALITY_GATES);
- QualityGateDto qualityGate = db.qualityGates().insertQualityGate(db.getDefaultOrganization());
+ QualityGateDto qualityGate = db.qualityGates().insertQualityGate();
QualityGate response = ws.newRequest()
.setParam(PARAM_ID, qualityGate.getUuid())
assertThat(response.getName()).isEqualTo("new-name");
}
- @Test
- public void quality_gates_can_have_the_same_name_in_different_organization() {
- OrganizationDto organization1 = db.organizations().insert();
- userSession.addPermission(ADMINISTER_QUALITY_GATES);
- QualityGateDto qualityGate1 = db.qualityGates().insertQualityGate(organization1);
-
- OrganizationDto organization2 = db.organizations().insert();
- userSession.addPermission(ADMINISTER_QUALITY_GATES);
- QualityGateDto qualityGate2 = db.qualityGates().insertQualityGate(organization2);
-
- assertThat(qualityGate1.getName()).isNotEqualTo(qualityGate2.getName());
-
- ws.newRequest()
- .setParam(PARAM_ORGANIZATION, organization2.getKey())
- .setParam(PARAM_ID, qualityGate2.getUuid())
- .setParam(PARAM_NAME, qualityGate1.getName())
- .execute();
-
- QGateWithOrgDto actual = db.getDbClient().qualityGateDao().selectByOrganizationAndName(dbSession, organization2, qualityGate1.getName());
- assertThat(actual).isNotNull();
- }
-
- @Test
- public void quality_gate_from_external_organization_can_not_be_copied() {
- OrganizationDto organization1 = db.organizations().insert();
- QualityGateDto qualityGate1 = db.qualityGates().insertQualityGate(organization1);
-
- OrganizationDto organization2 = db.organizations().insert();
- userSession.addPermission(ADMINISTER_QUALITY_GATES);
-
- expectedException.expect(NotFoundException.class);
- expectedException.expectMessage(format("No quality gate has been found for id %s in organization %s", qualityGate1.getUuid(), organization2.getName()));
-
- ws.newRequest()
- .setParam(PARAM_ORGANIZATION, organization2.getKey())
- .setParam(PARAM_ID, qualityGate1.getUuid())
- .setParam(PARAM_NAME, "new-name")
- .execute();
- }
-
@Test
public void fail_when_missing_administer_quality_gate_permission() {
- OrganizationDto organization = db.organizations().insert();
userSession.addPermission(ADMINISTER_QUALITY_PROFILES);
- QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(organization);
+ QualityGateDto qualityGate = db.qualityGates().insertQualityGate();
expectedException.expect(ForbiddenException.class);
ws.newRequest()
.setParam(PARAM_ID, qualityGate.getUuid())
.setParam(PARAM_NAME, "new-name")
- .setParam(PARAM_ORGANIZATION, organization.getKey())
.execute();
}
@Test
public void fail_when_id_parameter_is_missing() {
- OrganizationDto organization = db.organizations().insert();
userSession.addPermission(ADMINISTER_QUALITY_GATES);
expectedException.expect(IllegalArgumentException.class);
ws.newRequest()
.setParam(PARAM_NAME, "new-name")
- .setParam(PARAM_ORGANIZATION, organization.getKey())
.execute();
}
@Test
public void fail_when_quality_gate_id_is_not_found() {
- OrganizationDto organization = db.organizations().insert();
userSession.addPermission(ADMINISTER_QUALITY_GATES);
expectedException.expect(NotFoundException.class);
- expectedException.expectMessage(format(
- "No quality gate has been found for id 123 in organization %s", organization.getName()));
+ expectedException.expectMessage("No quality gate has been found for id 123");
ws.newRequest()
.setParam(PARAM_ID, "123")
.setParam(PARAM_NAME, "new-name")
- .setParam(PARAM_ORGANIZATION, organization.getKey())
.execute();
}
@Test
@UseDataProvider("nullOrEmpty")
public void fail_when_name_parameter_is_missing(@Nullable String nameParameter) {
- OrganizationDto organization = db.organizations().insert();
userSession.addPermission(ADMINISTER_QUALITY_GATES);
- QualityGateDto qualityGate = db.qualityGates().insertQualityGate(organization);
+ QualityGateDto qualityGate = db.qualityGates().insertQualityGate();
TestRequest request = ws.newRequest()
- .setParam(PARAM_ID, qualityGate.getUuid())
- .setParam(PARAM_ORGANIZATION, organization.getKey());
+ .setParam(PARAM_ID, qualityGate.getUuid());
ofNullable(nameParameter).ifPresent(t -> request.setParam(PARAM_NAME, t));
expectedException.expect(IllegalArgumentException.class);
}
@Test
- public void fail_when_name_parameter_match_existing_quality_gate_in_the_same_organization() {
- OrganizationDto organization = db.organizations().insert();
+ public void fail_when_name_parameter_match_existing_quality_gate() {
userSession.addPermission(ADMINISTER_QUALITY_GATES);
- QualityGateDto existingQualityGate = db.qualityGates().insertQualityGate(organization);
- QualityGateDto qualityGate = db.qualityGates().insertQualityGate(organization);
+ QualityGateDto existingQualityGate = db.qualityGates().insertQualityGate();
+ QualityGateDto qualityGate = db.qualityGates().insertQualityGate();
expectedException.expect(IllegalArgumentException.class);
expectedException.expectMessage("Name has already been taken");
ws.newRequest()
.setParam(PARAM_ID, qualityGate.getUuid())
.setParam(PARAM_NAME, existingQualityGate.getName())
- .setParam(PARAM_ORGANIZATION, organization.getKey())
.execute();
}
}
import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
import org.sonar.db.DbTester;
-import org.sonar.db.organization.OrganizationDbTester;
-import org.sonar.db.organization.OrganizationDto;
-import org.sonar.db.qualitygate.QGateWithOrgDto;
import org.sonar.db.qualitygate.QualityGateDto;
-import org.sonar.server.component.TestComponentFinder;
import org.sonar.server.exceptions.ForbiddenException;
-import org.sonar.server.exceptions.NotFoundException;
-import org.sonar.server.organization.TestDefaultOrganizationProvider;
import org.sonar.server.qualitygate.QualityGateUpdater;
import org.sonar.server.tester.UserSessionRule;
import org.sonar.server.ws.TestRequest;
import static org.assertj.core.api.Assertions.assertThat;
import static org.sonar.db.permission.GlobalPermission.ADMINISTER_QUALITY_GATES;
import static org.sonar.server.qualitygate.ws.QualityGatesWsParameters.PARAM_NAME;
-import static org.sonar.server.qualitygate.ws.QualityGatesWsParameters.PARAM_ORGANIZATION;
@RunWith(DataProviderRunner.class)
public class CreateActionTest {
@Rule
public DbTester db = DbTester.create(System2.INSTANCE);
- private OrganizationDbTester organizationDbTester = new OrganizationDbTester(db);
- private TestDefaultOrganizationProvider defaultOrganizationProvider = TestDefaultOrganizationProvider.from(db);
- private DbClient dbClient = db.getDbClient();
- private DbSession dbSession = db.getSession();
- private CreateAction underTest = new CreateAction(dbClient, userSession, new QualityGateUpdater(dbClient, UuidFactoryFast.getInstance()),
- new QualityGatesWsSupport(dbClient, userSession, defaultOrganizationProvider, TestComponentFinder.from(db)));
- private WsActionTester ws = new WsActionTester(underTest);
+ private final DbClient dbClient = db.getDbClient();
+ private final DbSession dbSession = db.getSession();
+ private final CreateAction underTest = new CreateAction(dbClient, userSession, new QualityGateUpdater(dbClient, UuidFactoryFast.getInstance()));
+ private final WsActionTester ws = new WsActionTester(underTest);
@Test
- public void default_organization_is_used_when_no_parameter() {
- logInAsQualityGateAdmin(db.getDefaultOrganization());
+ public void default_is_used_when_no_parameter() {
+ logInAsQualityGateAdmin();
String qgName = "Default";
- CreateResponse response = executeRequest(Optional.empty(), qgName);
+ CreateResponse response = executeRequest(qgName);
assertThat(response.getName()).isEqualTo(qgName);
assertThat(response.getId()).isNotNull();
dbSession.commit();
- QualityGateDto qualityGateDto = dbClient.qualityGateDao().selectByOrganizationAndName(dbSession, db.getDefaultOrganization(), qgName);
+ QualityGateDto qualityGateDto = dbClient.qualityGateDao().selectByName(dbSession, qgName);
assertThat(qualityGateDto).isNotNull();
}
@Test
- public void create_quality_gate_with_organization() {
- OrganizationDto organizationDto = organizationDbTester.insert();
- logInAsQualityGateAdmin(organizationDto);
+ public void create_quality_gate() {
+ logInAsQualityGateAdmin();
String qgName = "Default";
- CreateResponse response = executeRequest(Optional.of(organizationDto), qgName);
+ CreateResponse response = executeRequest(qgName);
assertThat(response.getName()).isEqualTo(qgName);
assertThat(response.getId()).isNotNull();
dbSession.commit();
- QualityGateDto qualityGateDto = dbClient.qualityGateDao().selectByOrganizationAndName(dbSession, organizationDto, qgName);
+ QualityGateDto qualityGateDto = dbClient.qualityGateDao().selectByName(dbSession, qgName);
assertThat(qualityGateDto).isNotNull();
}
- @Test
- public void creating_a_qg_with_a_name_used_in_another_organization_should_work() {
- OrganizationDto anOrganization = db.organizations().insert();
- QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(anOrganization);
- OrganizationDto anotherOrganization = db.organizations().insert();
-
- userSession.addPermission(ADMINISTER_QUALITY_GATES);
-
- CreateResponse response = ws.newRequest()
- .setParam(PARAM_NAME, qualityGate.getName())
- .setParam(PARAM_ORGANIZATION, anotherOrganization.getKey())
- .executeProtobuf(CreateResponse.class);
-
- assertThat(response.getName()).isEqualTo(qualityGate.getName());
- assertThat(response.getId()).isNotEqualTo(qualityGate.getUuid());
- }
-
@Test
public void test_ws_definition() {
WebService.Action action = ws.getDef();
assertThat(action.isInternal()).isFalse();
assertThat(action.isPost()).isTrue();
assertThat(action.responseExampleAsString()).isNotEmpty();
- assertThat(action.params()).hasSize(2);
+ assertThat(action.params()).hasSize(1);
}
-
@Test
public void throw_ForbiddenException_if_not_gate_administrator() {
userSession.logIn();
expectedException.expect(ForbiddenException.class);
expectedException.expectMessage("Insufficient privileges");
- executeRequest(Optional.empty(), "Default");
- }
-
- @Test
- public void throw_ForbiddenException_if_unknown_organization() {
- OrganizationDto org = new OrganizationDto().setName("Unknown organization").setKey("unknown_key");
-
- userSession.logIn();
-
- expectedException.expect(NotFoundException.class);
- expectedException.expectMessage("No organization with key 'unknown_key'");
-
- executeRequest(Optional.of(org), "Default");
+ executeRequest("Default");
}
@Test
public void throw_BadRequestException_if_name_is_already_used() {
- OrganizationDto org = db.organizations().insert();
userSession.logIn().addPermission(ADMINISTER_QUALITY_GATES);
- executeRequest(Optional.of(org), "Default");
+ executeRequest("Default");
expectedException.expect(IllegalArgumentException.class);
expectedException.expectMessage("Name has already been taken");
- executeRequest(Optional.of(org), "Default");
+ executeRequest("Default");
}
@Test
@UseDataProvider("nullOrEmpty")
public void fail_when_name_parameter_is_empty(@Nullable String nameParameter) {
- OrganizationDto org = db.organizations().insert();
userSession.addPermission(ADMINISTER_QUALITY_GATES);
- TestRequest request = ws.newRequest()
- .setParam(PARAM_ORGANIZATION, org.getKey());
+ TestRequest request = ws.newRequest();
Optional.ofNullable(nameParameter).ifPresent(t -> request.setParam(PARAM_NAME, ""));
expectedException.expect(IllegalArgumentException.class);
};
}
- private CreateResponse executeRequest(Optional<OrganizationDto> organization, String qualitGateName) {
- if (organization.isPresent()) {
- return ws.newRequest()
- .setParam("name", qualitGateName)
- .setParam("organization", organization.get().getKey())
- .executeProtobuf(CreateResponse.class);
- } else {
- return ws.newRequest()
- .setParam("name", qualitGateName)
- .executeProtobuf(CreateResponse.class);
- }
+ private CreateResponse executeRequest(String qualitGateName) {
+ return ws.newRequest()
+ .setParam("name", qualitGateName)
+ .executeProtobuf(CreateResponse.class);
}
- private void logInAsQualityGateAdmin(OrganizationDto organizationDto) {
+ private void logInAsQualityGateAdmin() {
userSession.logIn().addPermission(ADMINISTER_QUALITY_GATES);
}
}
import org.sonar.db.DbSession;
import org.sonar.db.DbTester;
import org.sonar.db.metric.MetricDto;
-import org.sonar.db.organization.OrganizationDto;
-import org.sonar.db.qualitygate.QGateWithOrgDto;
import org.sonar.db.qualitygate.QualityGateConditionDto;
import org.sonar.db.qualitygate.QualityGateDto;
import org.sonar.server.component.TestComponentFinder;
import org.sonar.server.exceptions.BadRequestException;
import org.sonar.server.exceptions.ForbiddenException;
-import org.sonar.server.organization.TestDefaultOrganizationProvider;
import org.sonar.server.qualitygate.QualityGateConditionsUpdater;
import org.sonar.server.tester.UserSessionRule;
import org.sonar.server.ws.WsActionTester;
import static org.sonar.server.qualitygate.ws.QualityGatesWsParameters.PARAM_GATE_ID;
import static org.sonar.server.qualitygate.ws.QualityGatesWsParameters.PARAM_METRIC;
import static org.sonar.server.qualitygate.ws.QualityGatesWsParameters.PARAM_OPERATOR;
-import static org.sonar.server.qualitygate.ws.QualityGatesWsParameters.PARAM_ORGANIZATION;
@RunWith(DataProviderRunner.class)
public class CreateConditionActionTest {
@Rule
public DbTester db = DbTester.create();
- private TestDefaultOrganizationProvider defaultOrganizationProvider = TestDefaultOrganizationProvider.from(db);
- private DbClient dbClient = db.getDbClient();
- private DbSession dbSession = db.getSession();
- private CreateConditionAction underTest = new CreateConditionAction(dbClient, new QualityGateConditionsUpdater(dbClient),
- new QualityGatesWsSupport(dbClient, userSession, defaultOrganizationProvider, TestComponentFinder.from(db)));
+ private final DbClient dbClient = db.getDbClient();
+ private final DbSession dbSession = db.getSession();
+ private final CreateConditionAction underTest = new CreateConditionAction(dbClient, new QualityGateConditionsUpdater(dbClient),
+ new QualityGatesWsSupport(dbClient, userSession, TestComponentFinder.from(db)));
- private WsActionTester ws = new WsActionTester(underTest);
+ private final WsActionTester ws = new WsActionTester(underTest);
@Test
public void create_error_condition() {
- OrganizationDto organization = db.organizations().insert();
- logInAsQualityGateAdmin(organization);
- QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(organization);
+ logInAsQualityGateAdmin();
+ QualityGateDto qualityGate = db.qualityGates().insertQualityGate();
MetricDto metric = insertMetric();
ws.newRequest()
.setParam(PARAM_METRIC, metric.getKey())
.setParam(PARAM_OPERATOR, "LT")
.setParam(PARAM_ERROR, "90")
- .setParam(PARAM_ORGANIZATION, organization.getKey())
.execute();
assertCondition(qualityGate, metric, "LT", "90");
@Test
public void create_condition_over_new_code_period() {
- OrganizationDto organization = db.organizations().insert();
- logInAsQualityGateAdmin(organization);
- QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(organization);
+ logInAsQualityGateAdmin();
+ QualityGateDto qualityGate = db.qualityGates().insertQualityGate();
MetricDto metric = insertMetric();
ws.newRequest()
.setParam(PARAM_METRIC, metric.getKey())
.setParam(PARAM_OPERATOR, "LT")
.setParam(PARAM_ERROR, "90")
- .setParam(PARAM_ORGANIZATION, organization.getKey())
.execute();
assertCondition(qualityGate, metric, "LT", "90");
}
- @Test
- public void default_organization_is_used_when_no_organization_parameter() {
- logInAsQualityGateAdmin(db.getDefaultOrganization());
- QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(db.getDefaultOrganization());
- OrganizationDto otherOrganization = db.organizations().insert();
- QGateWithOrgDto otherQualityGate = db.qualityGates().insertQualityGate(otherOrganization);
-
- MetricDto metric = insertMetric();
-
- ws.newRequest()
- .setParam(PARAM_GATE_ID, qualityGate.getUuid())
- .setParam(PARAM_METRIC, metric.getKey())
- .setParam(PARAM_OPERATOR, "LT")
- .setParam(PARAM_ERROR, "10")
- .execute();
-
- assertCondition(qualityGate, metric, "LT", "10");
- }
-
@Test
public void fail_to_update_built_in_quality_gate() {
- OrganizationDto organization = db.organizations().insert();
- logInAsQualityGateAdmin(organization);
- QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(organization, qg -> qg.setBuiltIn(true));
+ logInAsQualityGateAdmin();
+ QualityGateDto qualityGate = db.qualityGates().insertQualityGate(qg -> qg.setBuiltIn(true));
MetricDto metric = insertMetric();
expectedException.expect(IllegalArgumentException.class);
.setParam(PARAM_METRIC, metric.getKey())
.setParam(PARAM_OPERATOR, "LT")
.setParam(PARAM_ERROR, "90")
- .setParam(PARAM_ORGANIZATION, organization.getKey())
.execute();
}
@Test
public void fail_with_unknown_operator() {
- OrganizationDto organization = db.organizations().insert();
- logInAsQualityGateAdmin(organization);
- QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(organization);
+ logInAsQualityGateAdmin();
+ QualityGateDto qualityGate = db.qualityGates().insertQualityGate();
MetricDto metric = db.measures().insertMetric(m -> m.setValueType(INT.name()).setHidden(false).setDirection(0));
expectedException.expect(IllegalArgumentException.class);
.setParam(PARAM_METRIC, metric.getKey())
.setParam(PARAM_OPERATOR, "ABC")
.setParam(PARAM_ERROR, "90")
- .setParam(PARAM_ORGANIZATION, organization.getKey())
.execute();
}
@Test
@UseDataProvider("invalid_operators_for_direction")
public void fail_with_invalid_operators_for_direction(String operator, int direction) {
- OrganizationDto organization = db.organizations().insert();
- logInAsQualityGateAdmin(organization);
- QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(organization);
+ logInAsQualityGateAdmin();
+ QualityGateDto qualityGate = db.qualityGates().insertQualityGate();
MetricDto metric = db.measures().insertMetric(m -> m.setValueType(INT.name()).setHidden(false).setDirection(direction));
expectedException.expect(BadRequestException.class);
.setParam(PARAM_METRIC, metric.getKey())
.setParam(PARAM_OPERATOR, operator)
.setParam(PARAM_ERROR, "90")
- .setParam(PARAM_ORGANIZATION, organization.getKey())
.execute();
}
@Test
public void test_response() {
- OrganizationDto organization = db.organizations().insert();
- logInAsQualityGateAdmin(organization);
- QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(organization);
+ logInAsQualityGateAdmin();
+ QualityGateDto qualityGate = db.qualityGates().insertQualityGate();
MetricDto metric = insertMetric();
CreateConditionResponse response = ws.newRequest()
.setParam(PARAM_METRIC, metric.getKey())
.setParam(PARAM_OPERATOR, "LT")
.setParam(PARAM_ERROR, "45")
- .setParam(PARAM_ORGANIZATION, organization.getKey())
.executeProtobuf(CreateConditionResponse.class);
QualityGateConditionDto condition = new ArrayList<>(dbClient.gateConditionDao().selectForQualityGate(dbSession, qualityGate.getUuid())).get(0);
@Test
public void throw_ForbiddenException_if_not_gate_administrator() {
- OrganizationDto organization = db.organizations().insert();
- QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(organization);
+ QualityGateDto qualityGate = db.qualityGates().insertQualityGate();
MetricDto metric = insertMetric();
userSession.logIn();
.setParam(PARAM_METRIC, metric.getKey())
.setParam(PARAM_OPERATOR, "LT")
.setParam(PARAM_ERROR, "90")
- .setParam(PARAM_ORGANIZATION, organization.getKey())
.execute();
}
tuple("gateName", false),
tuple("metric", true),
tuple("error", true),
- tuple("op", false),
- tuple("organization", false));
+ tuple("op", false));
}
@DataProvider
.containsExactlyInAnyOrder(tuple(qualityGate.getUuid(), metric.getUuid(), operator, error));
}
- private void logInAsQualityGateAdmin(OrganizationDto organization) {
+ private void logInAsQualityGateAdmin() {
userSession.logIn().addPermission(ADMINISTER_QUALITY_GATES);
}
import org.sonar.api.server.ws.WebService;
import org.sonar.db.DbTester;
import org.sonar.db.metric.MetricDto;
-import org.sonar.db.organization.OrganizationDto;
-import org.sonar.db.qualitygate.QGateWithOrgDto;
import org.sonar.db.qualitygate.QualityGateConditionDto;
import org.sonar.db.qualitygate.QualityGateDto;
import org.sonar.server.component.TestComponentFinder;
import org.sonar.server.exceptions.ForbiddenException;
import org.sonar.server.exceptions.NotFoundException;
-import org.sonar.server.organization.TestDefaultOrganizationProvider;
import org.sonar.server.tester.UserSessionRule;
import org.sonar.server.ws.TestResponse;
import org.sonar.server.ws.WsActionTester;
import static org.sonar.db.permission.GlobalPermission.ADMINISTER_QUALITY_GATES;
import static org.sonar.db.permission.GlobalPermission.ADMINISTER_QUALITY_PROFILES;
import static org.sonar.server.qualitygate.ws.QualityGatesWsParameters.PARAM_ID;
-import static org.sonar.server.qualitygate.ws.QualityGatesWsParameters.PARAM_ORGANIZATION;
public class DeleteConditionActionTest {
@Rule
public ExpectedException expectedException = ExpectedException.none();
- private TestDefaultOrganizationProvider organizationProvider = TestDefaultOrganizationProvider.from(db);
-
- private WsActionTester ws = new WsActionTester(
- new DeleteConditionAction(db.getDbClient(), new QualityGatesWsSupport(db.getDbClient(), userSession, organizationProvider, TestComponentFinder.from(db))));
+ private final WsActionTester ws = new WsActionTester(
+ new DeleteConditionAction(db.getDbClient(), new QualityGatesWsSupport(db.getDbClient(), userSession, TestComponentFinder.from(db))));
@Test
public void definition() {
assertThat(action.params())
.extracting(WebService.Param::key, WebService.Param::isRequired)
.containsExactlyInAnyOrder(
- tuple("id", true),
- tuple("organization", false));
+ tuple("id", true));
}
@Test
public void delete_condition() {
- OrganizationDto organization = db.organizations().insert();
- userSession.addPermission(ADMINISTER_QUALITY_GATES);
- QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(organization);
- MetricDto metric = db.measures().insertMetric();
- QualityGateConditionDto qualityGateCondition = db.qualityGates().addCondition(qualityGate, metric);
-
- ws.newRequest()
- .setParam(PARAM_ID, qualityGateCondition.getUuid())
- .setParam(PARAM_ORGANIZATION, organization.getKey())
- .execute();
-
- assertThat(searchConditionsOf(qualityGate)).isEmpty();
- }
-
- @Test
- public void default_organization_is_used_when_no_organization_parameter() {
userSession.addPermission(ADMINISTER_QUALITY_GATES);
- QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(db.getDefaultOrganization());
+ QualityGateDto qualityGate = db.qualityGates().insertQualityGate();
MetricDto metric = db.measures().insertMetric();
QualityGateConditionDto qualityGateCondition = db.qualityGates().addCondition(qualityGate, metric);
@Test
public void no_content() {
- OrganizationDto organization = db.organizations().insert();
userSession.addPermission(ADMINISTER_QUALITY_GATES);
- QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(organization);
+ QualityGateDto qualityGate = db.qualityGates().insertQualityGate();
MetricDto metric = db.measures().insertMetric();
QualityGateConditionDto qualityGateCondition = db.qualityGates().addCondition(qualityGate, metric);
TestResponse result = ws.newRequest()
.setParam(PARAM_ID, qualityGateCondition.getUuid())
- .setParam(PARAM_ORGANIZATION, organization.getKey())
.execute();
assertThat(result.getStatus()).isEqualTo(HTTP_NO_CONTENT);
@Test
public void fail_if_built_in_quality_gate() {
- OrganizationDto organization = db.organizations().insert();
userSession.addPermission(ADMINISTER_QUALITY_GATES);
- QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(organization, qg -> qg.setBuiltIn(true));
+ QualityGateDto qualityGate = db.qualityGates().insertQualityGate(qg -> qg.setBuiltIn(true));
MetricDto metric = db.measures().insertMetric();
QualityGateConditionDto qualityGateCondition = db.qualityGates().addCondition(qualityGate, metric);
ws.newRequest()
.setParam(PARAM_ID, qualityGateCondition.getUuid())
- .setParam(PARAM_ORGANIZATION, organization.getKey())
.execute();
}
@Test
public void fail_if_not_quality_gate_administrator() {
- OrganizationDto organization = db.organizations().insert();
userSession.addPermission(ADMINISTER_QUALITY_PROFILES);
- QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(organization);
+ QualityGateDto qualityGate = db.qualityGates().insertQualityGate();
MetricDto metric = db.measures().insertMetric();
QualityGateConditionDto qualityGateCondition = db.qualityGates().addCondition(qualityGate, metric);
ws.newRequest()
.setParam(PARAM_ID, qualityGateCondition.getUuid())
- .setParam(PARAM_ORGANIZATION, organization.getKey())
.execute();
}
@Test
public void fail_if_condition_uuid_is_not_found() {
- OrganizationDto organization = db.organizations().insert();
userSession.addPermission(ADMINISTER_QUALITY_PROFILES);
- QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(organization);
+ QualityGateDto qualityGate = db.qualityGates().insertQualityGate();
MetricDto metric = db.measures().insertMetric();
QualityGateConditionDto qualityGateCondition = db.qualityGates().addCondition(qualityGate, metric);
String unknownConditionUuid = "unknown";
ws.newRequest()
.setParam(PARAM_ID, unknownConditionUuid)
- .setParam(PARAM_ORGANIZATION, organization.getKey())
.execute();
}
@Test
public void fail_when_condition_match_unknown_quality_gate() {
- OrganizationDto organization = db.organizations().insert();
userSession.addPermission(ADMINISTER_QUALITY_PROFILES);
QualityGateConditionDto condition = new QualityGateConditionDto().setUuid("uuid").setMetricUuid("metric").setQualityGateUuid("123");
db.getDbClient().gateConditionDao().insert(condition, db.getSession());
ws.newRequest()
.setParam(PARAM_ID, condition.getUuid())
- .setParam(PARAM_ORGANIZATION, organization.getKey())
- .execute();
- }
-
- @Test
- public void fail_when_condition_match_quality_gate_on_other_organization() {
- OrganizationDto organization = db.organizations().insert();
- userSession.addPermission(ADMINISTER_QUALITY_PROFILES);
- OrganizationDto otherOrganization = db.organizations().insert();
- QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(otherOrganization);
- MetricDto metric = db.measures().insertMetric();
- QualityGateConditionDto condition = db.qualityGates().addCondition(qualityGate, metric);
-
- expectedException.expect(IllegalStateException.class);
- expectedException.expectMessage(format("Condition '%s' is linked to an unknown quality gate '%s'", condition.getUuid(), qualityGate.getUuid()));
-
- ws.newRequest()
- .setParam(PARAM_ID, condition.getUuid())
- .setParam(PARAM_ORGANIZATION, organization.getKey())
.execute();
}
import org.sonar.db.DbClient;
import org.sonar.db.DbTester;
import org.sonar.db.component.ComponentDto;
-import org.sonar.db.organization.OrganizationDto;
import org.sonar.db.project.ProjectDto;
-import org.sonar.db.qualitygate.QGateWithOrgDto;
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;
-import org.sonar.server.organization.TestDefaultOrganizationProvider;
import org.sonar.server.tester.UserSessionRule;
import org.sonar.server.ws.WsActionTester;
@Rule
public DbTester db = DbTester.create();
- private DbClient dbClient = db.getDbClient();
- private TestDefaultOrganizationProvider organizationProvider = TestDefaultOrganizationProvider.from(db);
- private ComponentFinder componentFinder = TestComponentFinder.from(db);
- private DeselectAction underTest = new DeselectAction(dbClient, new QualityGatesWsSupport(db.getDbClient(), userSession, organizationProvider, componentFinder));
- private WsActionTester ws = new WsActionTester(underTest);
+ private final DbClient dbClient = db.getDbClient();
+ private final ComponentFinder componentFinder = TestComponentFinder.from(db);
+ private final DeselectAction underTest = new DeselectAction(dbClient, new QualityGatesWsSupport(db.getDbClient(), userSession, componentFinder));
+ private final WsActionTester ws = new WsActionTester(underTest);
@Test
public void deselect_by_key() {
- OrganizationDto organization = db.organizations().insert();
userSession.addPermission(ADMINISTER_QUALITY_GATES);
- QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(organization);
- ProjectDto project = db.components().insertPrivateProjectDto(organization);
+ QualityGateDto qualityGate = db.qualityGates().insertQualityGate();
+ ProjectDto project = db.components().insertPrivateProjectDto();
associateProjectToQualityGate(project, qualityGate);
ws.newRequest()
.setParam("projectKey", project.getKey())
- .setParam("organization", organization.getKey())
.execute();
assertDeselected(project);
@Test
public void project_admin() {
- OrganizationDto organization = db.organizations().insert();
- QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(organization);
- ProjectDto project = db.components().insertPrivateProjectDto(organization);
+ QualityGateDto qualityGate = db.qualityGates().insertQualityGate();
+ ProjectDto project = db.components().insertPrivateProjectDto();
associateProjectToQualityGate(project, qualityGate);
userSession.logIn().addProjectPermission(UserRole.ADMIN, project);
ws.newRequest()
.setParam("projectKey", project.getKey())
- .setParam("organization", organization.getKey())
.execute();
assertDeselected(project);
@Test
public void other_project_should_not_be_updated() {
- OrganizationDto organization = db.organizations().insert();
userSession.addPermission(ADMINISTER_QUALITY_GATES);
- QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(organization);
- ProjectDto project = db.components().insertPrivateProjectDto(organization);
+ QualityGateDto qualityGate = db.qualityGates().insertQualityGate();
+ ProjectDto project = db.components().insertPrivateProjectDto();
associateProjectToQualityGate(project, qualityGate);
// Another project
- ProjectDto anotherProject = db.components().insertPrivateProjectDto(organization);
+ ProjectDto anotherProject = db.components().insertPrivateProjectDto();
associateProjectToQualityGate(anotherProject, qualityGate);
ws.newRequest()
.setParam("projectKey", project.getKey())
- .setParam("organization", organization.getKey())
.execute();
assertDeselected(project);
}
@Test
- public void default_organization_is_used_when_no_organization_parameter() {
- OrganizationDto organization = db.getDefaultOrganization();
+ public void default_is_used() {
userSession.addPermission(ADMINISTER_QUALITY_GATES);
- QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(organization);
- ProjectDto project = db.components().insertPrivateProjectDto(organization);
+ QualityGateDto qualityGate = db.qualityGates().insertQualityGate();
+ ProjectDto project = db.components().insertPrivateProjectDto();
associateProjectToQualityGate(project, qualityGate);
ws.newRequest()
assertDeselected(project);
}
- @Test
- public void fail_when_project_belongs_to_another_organization() {
- OrganizationDto organization = db.organizations().insert();
- userSession.addPermission(ADMINISTER_QUALITY_GATES);
- OrganizationDto anotherOrganization = db.organizations().insert();
- ComponentDto project = db.components().insertPrivateProject(anotherOrganization);
-
- expectedException.expect(NotFoundException.class);
- expectedException.expectMessage(format("Project '%s' doesn't exist in organization '%s'", project.getKey(), organization.getKey()));
-
- ws.newRequest()
- .setParam("projectKey", project.getKey())
- .setParam("organization", organization.getKey())
- .execute();
- }
-
@Test
public void fail_when_no_project_key() {
- OrganizationDto organization = db.organizations().insert();
userSession.addPermission(ADMINISTER_QUALITY_GATES);
expectedException.expect(NotFoundException.class);
ws.newRequest()
.setParam("projectKey", "unknown")
- .setParam("organization", organization.getKey())
.execute();
}
@Test
public void fail_when_anonymous() {
- OrganizationDto organization = db.organizations().insert();
- ComponentDto project = db.components().insertPrivateProject(organization);
+ ComponentDto project = db.components().insertPrivateProject();
userSession.anonymous();
expectedException.expect(ForbiddenException.class);
ws.newRequest()
.setParam("projectKey", project.getKey())
- .setParam("organization", organization.getKey())
.execute();
}
@Test
public void fail_when_not_project_admin() {
- OrganizationDto organization = db.organizations().insert();
- ComponentDto project = db.components().insertPrivateProject(organization);
+ ComponentDto project = db.components().insertPrivateProject();
userSession.logIn().addProjectPermission(UserRole.ISSUE_ADMIN, project);
expectedException.expect(ForbiddenException.class);
ws.newRequest()
.setParam("projectKey", project.getKey())
- .setParam("organization", organization.getKey())
.execute();
}
@Test
public void fail_when_not_quality_gates_admin() {
- OrganizationDto organization = db.organizations().insert();
userSession.addPermission(ADMINISTER_QUALITY_GATES);
- ComponentDto project = db.components().insertPrivateProject(organization);
+ ComponentDto project = db.components().insertPrivateProject();
userSession.logIn().addPermission(ADMINISTER_QUALITY_PROFILES);
ws.newRequest()
.setParam("projectKey", project.getKey())
- .setParam("organization", organization.getKey())
.execute();
}
@Test
public void fail_when_using_branch_db_key() {
- OrganizationDto organization = db.organizations().insert();
- ComponentDto project = db.components().insertPublicProject(organization);
+ ComponentDto project = db.components().insertPublicProject();
userSession.logIn().addProjectPermission(UserRole.ADMIN, project);
ComponentDto branch = db.components().insertProjectBranch(project);
ws.newRequest()
.setParam("projectKey", branch.getDbKey())
- .setParam("organization", organization.getKey())
.execute();
}
assertThat(def.params())
.extracting(WebService.Param::key, WebService.Param::isRequired)
.containsExactlyInAnyOrder(
- tuple("projectKey", true),
- tuple("organization", false));
+ tuple("projectKey", true));
}
private void associateProjectToQualityGate(ProjectDto project, QualityGateDto qualityGate) {
.isEmpty();
}
- private void assertSelected(QGateWithOrgDto qualityGate, ProjectDto project) {
+ private void assertSelected(QualityGateDto qualityGate, ProjectDto project) {
Optional<String> qGateUuid = db.qualityGates().selectQGateUuidByComponentUuid(project.getUuid());
assertThat(qGateUuid)
.isNotNull()
import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
import org.sonar.db.DbTester;
-import org.sonar.db.organization.OrganizationDto;
import org.sonar.db.project.ProjectDto;
-import org.sonar.db.qualitygate.QGateWithOrgDto;
import org.sonar.db.qualitygate.QualityGateDto;
import org.sonar.server.component.TestComponentFinder;
import org.sonar.server.exceptions.ForbiddenException;
import org.sonar.server.exceptions.NotFoundException;
-import org.sonar.server.organization.TestDefaultOrganizationProvider;
import org.sonar.server.qualitygate.QualityGateFinder;
import org.sonar.server.tester.UserSessionRule;
import org.sonar.server.ws.WsActionTester;
import static org.sonar.db.permission.GlobalPermission.ADMINISTER_QUALITY_GATES;
import static org.sonar.db.permission.GlobalPermission.ADMINISTER_QUALITY_PROFILES;
import static org.sonar.server.qualitygate.ws.QualityGatesWsParameters.PARAM_ID;
-import static org.sonar.server.qualitygate.ws.QualityGatesWsParameters.PARAM_ORGANIZATION;
public class DestroyActionTest {
@Rule
public DbTester db = DbTester.create(System2.INSTANCE);
- private DbClient dbClient = db.getDbClient();
- private TestDefaultOrganizationProvider organizationProvider = TestDefaultOrganizationProvider.from(db);
- private QualityGateFinder qualityGateFinder = new QualityGateFinder(dbClient);
- private QualityGatesWsSupport wsSupport = new QualityGatesWsSupport(db.getDbClient(), userSession, organizationProvider, TestComponentFinder.from(db));
+ private final DbClient dbClient = db.getDbClient();
+ private final QualityGateFinder qualityGateFinder = new QualityGateFinder(dbClient);
+ private final QualityGatesWsSupport wsSupport = new QualityGatesWsSupport(db.getDbClient(), userSession, TestComponentFinder.from(db));
- private DbSession dbSession = db.getSession();
- private DestroyAction underTest = new DestroyAction(dbClient, wsSupport, qualityGateFinder);
- private WsActionTester ws = new WsActionTester(underTest);
+ private final DbSession dbSession = db.getSession();
+ private final DestroyAction underTest = new DestroyAction(dbClient, wsSupport, qualityGateFinder);
+ private final WsActionTester ws = new WsActionTester(underTest);
@Test
public void delete_quality_gate() {
- OrganizationDto organization = db.organizations().insert();
- db.qualityGates().createDefaultQualityGate(organization);
- QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(organization);
+ db.qualityGates().createDefaultQualityGate();
+ QualityGateDto qualityGate = db.qualityGates().insertQualityGate();
userSession.addPermission(ADMINISTER_QUALITY_GATES);
ws.newRequest()
.setParam(PARAM_ID, qualityGate.getUuid())
- .setParam(PARAM_ORGANIZATION, organization.getKey())
.execute();
- assertThat(db.getDbClient().qualityGateDao().selectByOrganizationAndUuid(dbSession, organization, qualityGate.getUuid())).isNull();
+ assertThat(db.getDbClient().qualityGateDao().selectByUuid(dbSession, qualityGate.getUuid())).isNull();
}
@Test
public void delete_quality_gate_if_non_default_when_a_default_exist() {
- OrganizationDto organization = db.organizations().insert();
- db.qualityGates().createDefaultQualityGate(organization);
- QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(organization);
+ db.qualityGates().createDefaultQualityGate();
+ QualityGateDto qualityGate = db.qualityGates().insertQualityGate();
userSession.addPermission(ADMINISTER_QUALITY_GATES);
ws.newRequest()
.setParam(PARAM_ID, valueOf(qualityGate.getUuid()))
- .setParam(PARAM_ORGANIZATION, organization.getKey())
.execute();
- assertThat(db.getDbClient().qualityGateDao().selectByOrganizationAndUuid(dbSession, organization, qualityGate.getUuid())).isNull();
+ assertThat(db.getDbClient().qualityGateDao().selectByUuid(dbSession, qualityGate.getUuid())).isNull();
}
@Test
public void delete_quality_gate_and_any_association_to_any_project() {
- OrganizationDto organization = db.organizations().insert();
- db.qualityGates().createDefaultQualityGate(organization);
- QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(organization);
- ProjectDto prj1 = db.components().insertPublicProjectDto(organization);
- ProjectDto prj2 = db.components().insertPublicProjectDto(organization);
+ db.qualityGates().createDefaultQualityGate();
+ QualityGateDto qualityGate = db.qualityGates().insertQualityGate();
+ ProjectDto prj1 = db.components().insertPublicProjectDto();
+ ProjectDto prj2 = db.components().insertPublicProjectDto();
db.qualityGates().associateProjectToQualityGate(prj1, qualityGate);
db.qualityGates().associateProjectToQualityGate(prj2, qualityGate);
userSession.addPermission(ADMINISTER_QUALITY_GATES);
ws.newRequest()
.setParam(PARAM_ID, valueOf(qualityGate.getUuid()))
- .setParam(PARAM_ORGANIZATION, organization.getKey())
.execute();
assertThat(db.getDbClient().projectQgateAssociationDao().selectQGateUuidByProjectUuid(dbSession, prj1.getUuid()))
@Test
public void does_not_delete_built_in_quality_gate() {
- OrganizationDto organization = db.organizations().insert();
- db.qualityGates().createDefaultQualityGate(organization);
+ db.qualityGates().createDefaultQualityGate();
QualityGateDto builtInQualityGate = db.qualityGates().insertBuiltInQualityGate();
- db.getDbClient().qualityGateDao().associate(db.getSession(), Uuids.createFast(), organization, builtInQualityGate);
+ db.getDbClient().qualityGateDao().associate(db.getSession(), Uuids.createFast(), builtInQualityGate);
db.commit();
userSession.addPermission(ADMINISTER_QUALITY_GATES);
ws.newRequest()
.setParam(PARAM_ID, valueOf(builtInQualityGate.getUuid()))
- .setParam(PARAM_ORGANIZATION, organization.getKey())
.execute();
}
- @Test
- public void default_organization_is_used_when_no_organization_parameter() {
- OrganizationDto defaultOrganization = db.getDefaultOrganization();
- db.qualityGates().createDefaultQualityGate(defaultOrganization);
- userSession.addPermission(ADMINISTER_QUALITY_GATES);
- QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(defaultOrganization);
- OrganizationDto otherOrganization = db.organizations().insert();
- QGateWithOrgDto otherQualityGate = db.qualityGates().insertQualityGate(otherOrganization);
-
- ws.newRequest()
- .setParam(PARAM_ID, valueOf(qualityGate.getUuid()))
- .execute();
-
- assertThat(db.getDbClient().qualityGateDao().selectByOrganizationAndUuid(dbSession, defaultOrganization, qualityGate.getUuid())).isNull();
- }
-
@Test
public void fail_when_missing_id() {
- OrganizationDto organization = db.organizations().insert();
userSession.addPermission(ADMINISTER_QUALITY_GATES);
expectedException.expect(NotFoundException.class);
ws.newRequest()
.setParam(PARAM_ID, EMPTY)
- .setParam(PARAM_ORGANIZATION, organization.getKey())
.execute();
}
@Test
public void fail_to_delete_default_quality_gate() {
- OrganizationDto organization = db.organizations().insert();
- QualityGateDto defaultQualityGate = db.qualityGates().createDefaultQualityGate(organization);
+ QualityGateDto defaultQualityGate = db.qualityGates().createDefaultQualityGate();
userSession.addPermission(ADMINISTER_QUALITY_GATES);
expectedException.expect(IllegalArgumentException.class);
ws.newRequest()
.setParam(PARAM_ID, valueOf(defaultQualityGate.getUuid()))
- .setParam(PARAM_ORGANIZATION, organization.getKey())
.execute();
}
@Test
public void fail_on_unknown_quality_gate() {
- OrganizationDto organization = db.organizations().insert();
userSession.addPermission(ADMINISTER_QUALITY_GATES);
expectedException.expect(NotFoundException.class);
ws.newRequest()
.setParam(PARAM_ID, "123")
- .setParam(PARAM_ORGANIZATION, organization.getKey())
.execute();
}
@Test
public void fail_when_not_quality_gates_administer() {
- OrganizationDto organization = db.organizations().insert();
- QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(organization);
- db.qualityGates().createDefaultQualityGate(organization);
+ QualityGateDto qualityGate = db.qualityGates().insertQualityGate();
+ db.qualityGates().createDefaultQualityGate();
userSession.logIn("john").addPermission(ADMINISTER_QUALITY_PROFILES);
expectedException.expect(ForbiddenException.class);
ws.newRequest()
.setParam(PARAM_ID, qualityGate.getUuid())
- .setParam(PARAM_ORGANIZATION, organization.getKey())
.execute();
}
.extracting(WebService.Param::key, WebService.Param::isRequired)
.containsExactlyInAnyOrder(
tuple("id", false),
- tuple("name", false),
- tuple("organization", false));
+ tuple("name", false));
}
}
import org.sonar.db.DbClient;
import org.sonar.db.DbTester;
import org.sonar.db.component.ComponentDto;
-import org.sonar.db.organization.OrganizationDto;
import org.sonar.db.project.ProjectDto;
-import org.sonar.db.qualitygate.QGateWithOrgDto;
import org.sonar.db.qualitygate.QualityGateDto;
import org.sonar.server.component.TestComponentFinder;
import org.sonar.server.exceptions.ForbiddenException;
import org.sonar.server.exceptions.NotFoundException;
-import org.sonar.server.organization.TestDefaultOrganizationProvider;
import org.sonar.server.qualitygate.QualityGateFinder;
import org.sonar.server.tester.UserSessionRule;
import org.sonar.server.ws.WsActionTester;
@Rule
public DbTester db = DbTester.create(System2.INSTANCE);
- private DbClient dbClient = db.getDbClient();
+ private final DbClient dbClient = db.getDbClient();
- private WsActionTester ws = new WsActionTester(
- new GetByProjectAction(userSession, dbClient, TestComponentFinder.from(db), new QualityGateFinder(dbClient),
- new QualityGatesWsSupport(db.getDbClient(), userSession, TestDefaultOrganizationProvider.from(db), TestComponentFinder.from(db))));
+ private final WsActionTester ws = new WsActionTester(
+ new GetByProjectAction(userSession, dbClient, TestComponentFinder.from(db), new QualityGateFinder(dbClient)));
@Test
public void definition() {
assertThat(action.params())
.extracting(WebService.Param::key, WebService.Param::isRequired)
.containsExactlyInAnyOrder(
- tuple("project", true),
- tuple("organization", false));
+ tuple("project", true));
}
@Test
public void json_example() {
- OrganizationDto organization = db.organizations().insert();
- ProjectDto project = db.components().insertPrivateProjectDto(organization);
- QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(organization, qg -> qg.setName("My team QG"));
+ ProjectDto project = db.components().insertPrivateProjectDto();
+ QualityGateDto qualityGate = db.qualityGates().insertQualityGate(qg -> qg.setName("My team QG"));
db.qualityGates().associateProjectToQualityGate(project, qualityGate);
logInAsProjectUser(project);
String result = ws.newRequest()
.setParam("project", project.getKey())
- .setParam("organization", organization.getKey())
.execute()
.getInput();
@Test
public void default_quality_gate() {
- OrganizationDto organization = db.organizations().insert();
- ProjectDto project = db.components().insertPrivateProjectDto(organization);
- QualityGateDto dbQualityGate = db.qualityGates().insertQualityGate(organization);
- db.qualityGates().setDefaultQualityGate(organization, dbQualityGate);
+ ProjectDto project = db.components().insertPrivateProjectDto();
+ QualityGateDto dbQualityGate = db.qualityGates().insertQualityGate();
+ db.qualityGates().setDefaultQualityGate(dbQualityGate);
logInAsProjectUser(project);
GetByProjectResponse result = ws.newRequest()
.setParam("project", project.getKey())
- .setParam("organization", organization.getKey())
.executeProtobuf(GetByProjectResponse.class);
Qualitygates.QualityGate qualityGate = result.getQualityGate();
@Test
public void project_quality_gate_over_default() {
- OrganizationDto organization = db.organizations().insert();
- ProjectDto project = db.components().insertPrivateProjectDto(organization);
- QGateWithOrgDto defaultDbQualityGate = db.qualityGates().insertQualityGate(organization);
- db.qualityGates().setDefaultQualityGate(organization, defaultDbQualityGate);
- QualityGateDto qualityGate = db.qualityGates().insertQualityGate(organization);
+ ProjectDto project = db.components().insertPrivateProjectDto();
+ QualityGateDto defaultDbQualityGate = db.qualityGates().insertQualityGate();
+ db.qualityGates().setDefaultQualityGate(defaultDbQualityGate);
+ QualityGateDto qualityGate = db.qualityGates().insertQualityGate();
db.qualityGates().associateProjectToQualityGate(project, qualityGate);
logInAsProjectUser(project);
GetByProjectResponse result = ws.newRequest()
.setParam("project", project.getKey())
- .setParam("organization", organization.getKey())
.executeProtobuf(GetByProjectResponse.class);
Qualitygates.QualityGate reloaded = result.getQualityGate();
@Test
public void get_by_project_key() {
- OrganizationDto organization = db.organizations().insert();
- ProjectDto project = db.components().insertPrivateProjectDto(organization);
+ ProjectDto project = db.components().insertPrivateProjectDto();
- QualityGateDto qualityGate = db.qualityGates().insertQualityGate(db.getDefaultOrganization(), qg -> qg.setName("My team QG"));
+ QualityGateDto qualityGate = db.qualityGates().insertQualityGate(qg -> qg.setName("My team QG"));
db.qualityGates().associateProjectToQualityGate(project, qualityGate);
logInAsProjectUser(project);
GetByProjectResponse result = ws.newRequest()
.setParam("project", project.getKey())
- .setParam("organization", organization.getKey())
.executeProtobuf(GetByProjectResponse.class);
assertThat(result.getQualityGate().getName()).isEqualTo(qualityGate.getName());
@Test
public void get_with_project_admin_permission() {
- OrganizationDto organization = db.organizations().insert();
- ComponentDto project = db.components().insertPrivateProject(organization);
- QualityGateDto qualityGate = db.qualityGates().insertQualityGate(organization);
- db.qualityGates().setDefaultQualityGate(organization, qualityGate);
+ ComponentDto project = db.components().insertPrivateProject();
+ QualityGateDto qualityGate = db.qualityGates().insertQualityGate();
+ db.qualityGates().setDefaultQualityGate(qualityGate);
userSession.logIn().addProjectPermission(UserRole.ADMIN, project);
GetByProjectResponse result = ws.newRequest()
.setParam("project", project.getKey())
- .setParam("organization", organization.getKey())
.executeProtobuf(GetByProjectResponse.class);
assertThat(result.getQualityGate().getName()).isEqualTo(qualityGate.getName());
@Test
public void get_with_project_user_permission() {
- OrganizationDto organization = db.organizations().insert();
- ComponentDto project = db.components().insertPrivateProject(organization);
- QualityGateDto qualityGate = db.qualityGates().insertQualityGate(organization);
- db.qualityGates().setDefaultQualityGate(organization, qualityGate);
- userSession.logIn().addProjectPermission(UserRole.USER, project);
-
- GetByProjectResponse result = ws.newRequest()
- .setParam("project", project.getKey())
- .setParam("organization", organization.getKey())
- .executeProtobuf(GetByProjectResponse.class);
-
- assertThat(result.getQualityGate().getName()).isEqualTo(qualityGate.getName());
- }
-
- @Test
- public void default_organization_is_used_when_no_organization_parameter() {
- OrganizationDto organization = db.getDefaultOrganization();
- QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(organization);
- ComponentDto project = db.components().insertPrivateProject(organization);
- db.qualityGates().setDefaultQualityGate(organization, qualityGate);
+ ComponentDto project = db.components().insertPrivateProject();
+ QualityGateDto qualityGate = db.qualityGates().insertQualityGate();
+ db.qualityGates().setDefaultQualityGate(qualityGate);
userSession.logIn().addProjectPermission(UserRole.USER, project);
GetByProjectResponse result = ws.newRequest()
@Test
public void fail_when_insufficient_permission() {
- OrganizationDto organization = db.organizations().insert();
- QualityGateDto dbQualityGate = db.qualityGates().insertQualityGate(db.getDefaultOrganization());
- db.qualityGates().setDefaultQualityGate(db.getDefaultOrganization(), dbQualityGate);
- ComponentDto project = db.components().insertPrivateProject(organization);
+ QualityGateDto dbQualityGate = db.qualityGates().insertQualityGate();
+ db.qualityGates().setDefaultQualityGate(dbQualityGate);
+ ComponentDto project = db.components().insertPrivateProject();
userSession.logIn();
expectedException.expect(ForbiddenException.class);
ws.newRequest()
.setParam("project", project.getKey())
- .setParam("organization", organization.getKey())
.execute();
}
@Test
public void fail_when_project_does_not_exist() {
- OrganizationDto organization = db.organizations().insert();
-
expectedException.expect(NotFoundException.class);
ws.newRequest()
.setParam("project", "Unknown")
- .setParam("organization", organization.getKey())
.execute();
}
@Test
public void fail_when_missing_project_parameter() {
- OrganizationDto organization = db.organizations().insert();
-
expectedException.expect(IllegalArgumentException.class);
expectedException.expectMessage("The 'project' parameter is missing");
- ws.newRequest()
- .setParam("organization", organization.getKey())
- .execute();
+ ws.newRequest().execute();
}
@Test
public void fail_when_using_branch_db_key() {
- OrganizationDto organization = db.organizations().insert();
- ComponentDto project = db.components().insertPublicProject(organization);
+ ComponentDto project = db.components().insertPublicProject();
userSession.logIn().addProjectPermission(UserRole.ADMIN, project);
ComponentDto branch = db.components().insertProjectBranch(project);
ws.newRequest()
.setParam("project", branch.getDbKey())
- .setParam("organization", organization.getKey())
.execute();
}
import org.sonar.api.server.ws.WebService;
import org.sonar.db.DbClient;
import org.sonar.db.DbTester;
-import org.sonar.db.organization.OrganizationDto;
-import org.sonar.db.qualitygate.QGateWithOrgDto;
import org.sonar.db.qualitygate.QualityGateDto;
-import org.sonar.db.user.UserDto;
import org.sonar.server.component.TestComponentFinder;
-import org.sonar.server.organization.DefaultOrganizationProvider;
-import org.sonar.server.organization.TestDefaultOrganizationProvider;
import org.sonar.server.qualitygate.QualityGateFinder;
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.db.organization.OrganizationDto.Subscription.PAID;
import static org.sonar.db.permission.GlobalPermission.ADMINISTER_QUALITY_GATES;
import static org.sonar.db.permission.GlobalPermission.ADMINISTER_QUALITY_PROFILES;
import static org.sonar.test.JsonAssert.assertJson;
@Rule
public DbTester db = DbTester.create();
- private DbClient dbClient = db.getDbClient();
- private DefaultOrganizationProvider defaultOrganizationProvider = TestDefaultOrganizationProvider.from(db);
- private QualityGateFinder qualityGateFinder = new QualityGateFinder(dbClient);
+ private final DbClient dbClient = db.getDbClient();
+ private final QualityGateFinder qualityGateFinder = new QualityGateFinder(dbClient);
- private WsActionTester ws = new WsActionTester(new ListAction(db.getDbClient(),
- new QualityGatesWsSupport(dbClient, userSession, defaultOrganizationProvider, TestComponentFinder.from(db)), qualityGateFinder));
+ private final WsActionTester ws = new WsActionTester(new ListAction(db.getDbClient(),
+ new QualityGatesWsSupport(dbClient, userSession, TestComponentFinder.from(db)), qualityGateFinder));
@Test
public void list_quality_gates() {
- OrganizationDto organization = db.organizations().insert();
- QualityGateDto defaultQualityGate = db.qualityGates().insertQualityGate(organization);
- QualityGateDto otherQualityGate = db.qualityGates().insertQualityGate(organization);
- db.qualityGates().setDefaultQualityGate(organization, defaultQualityGate);
+ QualityGateDto defaultQualityGate = db.qualityGates().insertQualityGate();
+ QualityGateDto otherQualityGate = db.qualityGates().insertQualityGate();
+ db.qualityGates().setDefaultQualityGate(defaultQualityGate);
ListWsResponse response = ws.newRequest()
- .setParam("organization", organization.getKey())
.executeProtobuf(ListWsResponse.class);
assertThat(response.getQualitygatesList())
tuple(otherQualityGate.getUuid(), otherQualityGate.getName(), false));
}
- @Test
- public void default_organization_is_used_when_no_organization_parameter() {
- QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(db.getDefaultOrganization());
- db.qualityGates().setDefaultQualityGate(db.getDefaultOrganization(), qualityGate);
-
- OrganizationDto otherOrganization = db.organizations().insert();
- QGateWithOrgDto otherQualityGate = db.qualityGates().insertQualityGate(otherOrganization);
- db.qualityGates().setDefaultQualityGate(otherOrganization, otherQualityGate);
-
- ListWsResponse response = ws.newRequest().executeProtobuf(ListWsResponse.class);
-
- assertThat(response.getQualitygatesList())
- .extracting(QualityGate::getId)
- .containsExactly(qualityGate.getUuid());
- }
-
@Test
public void test_built_in_flag() {
- OrganizationDto organization = db.organizations().insert();
- QualityGateDto qualityGate1 = db.qualityGates().insertQualityGate(organization, qualityGate -> qualityGate.setBuiltIn(true));
- QualityGateDto qualityGate2 = db.qualityGates().insertQualityGate(organization, qualityGate -> qualityGate.setBuiltIn(false));
- db.qualityGates().setDefaultQualityGate(organization, qualityGate1);
+ QualityGateDto qualityGate1 = db.qualityGates().insertQualityGate(qualityGate -> qualityGate.setBuiltIn(true));
+ QualityGateDto qualityGate2 = db.qualityGates().insertQualityGate(qualityGate -> qualityGate.setBuiltIn(false));
+ db.qualityGates().setDefaultQualityGate(qualityGate1);
ListWsResponse response = ws.newRequest()
- .setParam("organization", organization.getKey())
.executeProtobuf(ListWsResponse.class);
assertThat(response.getQualitygatesList())
@Test
public void test_deprecated_default_field() {
- OrganizationDto organization = db.organizations().insert();
- QualityGateDto defaultQualityGate = db.qualityGates().insertQualityGate(organization);
- db.qualityGates().setDefaultQualityGate(organization, defaultQualityGate);
+ QualityGateDto defaultQualityGate = db.qualityGates().insertQualityGate();
+ db.qualityGates().setDefaultQualityGate(defaultQualityGate);
ListWsResponse response = ws.newRequest()
- .setParam("organization", organization.getKey())
.executeProtobuf(ListWsResponse.class);
assertThat(response.getDefault()).isEqualTo(defaultQualityGate.getUuid());
@Test
public void no_default_quality_gate() {
- OrganizationDto organization = db.organizations().insert();
- QualityGateDto qualityGate = db.qualityGates().insertQualityGate(organization);
+ QualityGateDto qualityGate = db.qualityGates().insertQualityGate();
expectedException.expect(IllegalStateException.class);
ListWsResponse response = ws.newRequest()
- .setParam("organization", organization.getKey())
.executeProtobuf(ListWsResponse.class);
}
@Test
public void actions_with_quality_gate_administer_permission() {
- OrganizationDto organization = db.organizations().insert();
userSession.logIn("john").addPermission(ADMINISTER_QUALITY_GATES);
- QualityGateDto defaultQualityGate = db.qualityGates().insertQualityGate(organization, qg -> qg.setName("Default").setBuiltIn(false));
- QualityGateDto builtInQualityGate = db.qualityGates().insertQualityGate(organization, qg -> qg.setName("Sonar way").setBuiltIn(true));
- QualityGateDto otherQualityGate = db.qualityGates().insertQualityGate(organization, qg -> qg.setName("Sonar way - Without Coverage").setBuiltIn(false));
- db.qualityGates().setDefaultQualityGate(organization, defaultQualityGate);
+ QualityGateDto defaultQualityGate = db.qualityGates().insertQualityGate(qg -> qg.setName("Default").setBuiltIn(false));
+ QualityGateDto builtInQualityGate = db.qualityGates().insertQualityGate(qg -> qg.setName("Sonar way").setBuiltIn(true));
+ QualityGateDto otherQualityGate = db.qualityGates().insertQualityGate(qg -> qg.setName("Sonar way - Without Coverage").setBuiltIn(false));
+ db.qualityGates().setDefaultQualityGate(defaultQualityGate);
- ListWsResponse response = ws.newRequest()
- .setParam("organization", organization.getKey())
- .executeProtobuf(ListWsResponse.class);
+ ListWsResponse response = ws.newRequest().executeProtobuf(ListWsResponse.class);
assertThat(response.getActions())
.extracting(ListWsResponse.RootActions::getCreate)
@Test
public void actions_without_quality_gate_administer_permission() {
- OrganizationDto organization = db.organizations().insert();
userSession.logIn("john").addPermission(ADMINISTER_QUALITY_PROFILES);
- QualityGateDto defaultQualityGate = db.qualityGates().insertQualityGate(organization, qg -> qg.setName("Sonar way").setBuiltIn(true));
- QualityGateDto otherQualityGate = db.qualityGates().insertQualityGate(organization, qg -> qg.setName("Sonar way - Without Coverage").setBuiltIn(false));
- db.qualityGates().setDefaultQualityGate(organization, defaultQualityGate);
+ QualityGateDto defaultQualityGate = db.qualityGates().insertQualityGate(qg -> qg.setName("Sonar way").setBuiltIn(true));
+ QualityGateDto otherQualityGate = db.qualityGates().insertQualityGate(qg -> qg.setName("Sonar way - Without Coverage").setBuiltIn(false));
+ db.qualityGates().setDefaultQualityGate(defaultQualityGate);
- ListWsResponse response = ws.newRequest()
- .setParam("organization", organization.getKey())
- .executeProtobuf(ListWsResponse.class);
+ ListWsResponse response = ws.newRequest().executeProtobuf(ListWsResponse.class);
assertThat(response.getActions())
.extracting(ListWsResponse.RootActions::getCreate)
tuple(otherQualityGate.getName(), false, false, false, false, false, false));
}
- @Test
- public void list_quality_gates_on_paid_organization() {
- OrganizationDto organization = db.organizations().insert(o -> o.setSubscription(PAID));
- QualityGateDto qualityGate = db.qualityGates().insertQualityGate(organization);
- db.qualityGates().setDefaultQualityGate(organization, qualityGate);
- UserDto user = db.users().insertUser();
-
- ListWsResponse response = ws.newRequest()
- .setParam("organization", organization.getKey())
- .executeProtobuf(ListWsResponse.class);
-
- assertThat(response.getQualitygatesList())
- .extracting(QualityGate::getName)
- .containsExactlyInAnyOrder(qualityGate.getName());
- }
-
@Test
public void json_example() {
- OrganizationDto organization = db.organizations().insert();
userSession.logIn("admin").addPermission(ADMINISTER_QUALITY_GATES);
- QualityGateDto defaultQualityGate = db.qualityGates().insertQualityGate(organization, qualityGate -> qualityGate.setName("Sonar way").setBuiltIn(true));
- db.qualityGates().insertQualityGate(organization, qualityGate -> qualityGate.setName("Sonar way - Without Coverage").setBuiltIn(false));
- db.qualityGates().setDefaultQualityGate(organization, defaultQualityGate);
+ QualityGateDto defaultQualityGate = db.qualityGates().insertQualityGate(qualityGate -> qualityGate.setName("Sonar way").setBuiltIn(true));
+ db.qualityGates().insertQualityGate(qualityGate -> qualityGate.setName("Sonar way - Without Coverage").setBuiltIn(false));
+ db.qualityGates().setDefaultQualityGate(defaultQualityGate);
- String response = ws.newRequest()
- .setParam("organization", organization.getKey())
- .execute()
- .getInput();
+ String response = ws.newRequest().execute().getInput();
assertJson(response).ignoreFields("id", "default")
.isSimilarTo(getClass().getResource("list-example.json"));
assertThat(action.isPost()).isFalse();
assertThat(action.isInternal()).isFalse();
assertThat(action.changelog()).isNotEmpty();
- assertThat(action.params()).extracting(WebService.Param::key, WebService.Param::isRequired)
- .containsExactlyInAnyOrder(tuple("organization", false));
+ assertThat(action.params()).extracting(WebService.Param::key, WebService.Param::isRequired).isEmpty();
}
}
import org.sonar.db.component.ComponentDto;
import org.sonar.db.component.SnapshotDto;
import org.sonar.db.metric.MetricDto;
-import org.sonar.db.organization.OrganizationDto;
import org.sonar.server.component.TestComponentFinder;
import org.sonar.server.exceptions.BadRequestException;
import org.sonar.server.exceptions.ForbiddenException;
import static org.sonar.db.metric.MetricTesting.newMetricDto;
import static org.sonar.server.qualitygate.ws.QualityGatesWsParameters.PARAM_ANALYSIS_ID;
import static org.sonar.server.qualitygate.ws.QualityGatesWsParameters.PARAM_BRANCH;
-import static org.sonar.server.qualitygate.ws.QualityGatesWsParameters.PARAM_ORGANIZATION;
import static org.sonar.server.qualitygate.ws.QualityGatesWsParameters.PARAM_PROJECT_ID;
import static org.sonar.server.qualitygate.ws.QualityGatesWsParameters.PARAM_PROJECT_KEY;
import static org.sonar.server.qualitygate.ws.QualityGatesWsParameters.PARAM_PULL_REQUEST;
@Rule
public DbTester db = DbTester.create(System2.INSTANCE);
- private DbClient dbClient = db.getDbClient();
- private DbSession dbSession = db.getSession();
+ private final DbClient dbClient = db.getDbClient();
+ private final DbSession dbSession = db.getSession();
- private WsActionTester ws = new WsActionTester(new ProjectStatusAction(dbClient, TestComponentFinder.from(db), userSession));
+ private final WsActionTester ws = new WsActionTester(new ProjectStatusAction(dbClient, TestComponentFinder.from(db), userSession));
@Test
public void test_definition() {
@Test
public void test_json_example() throws IOException {
- OrganizationDto organization = db.organizations().insert();
- ComponentDto project = db.components().insertPrivateProject(organization);
+ ComponentDto project = db.components().insertPrivateProject();
userSession.addProjectPermission(UserRole.USER, project);
MetricDto gateDetailsMetric = insertGateDetailMetric();
@Test
public void return_past_status_when_project_is_referenced_by_past_analysis_id() throws IOException {
- OrganizationDto organization = db.organizations().insert();
- ComponentDto project = db.components().insertPrivateProject(organization);
+ ComponentDto project = db.components().insertPrivateProject();
SnapshotDto pastAnalysis = dbClient.snapshotDao().insert(dbSession, newAnalysis(project)
.setLast(false)
.setPeriodMode("last_version")
@Test
public void return_live_status_when_project_is_referenced_by_its_id() throws IOException {
- OrganizationDto organization = db.organizations().insert();
- ComponentDto project = db.components().insertPrivateProject(organization);
+ ComponentDto project = db.components().insertPrivateProject();
dbClient.snapshotDao().insert(dbSession, newAnalysis(project)
.setPeriodMode("last_version")
.setPeriodParam("2015-12-07")
@Test
public void return_past_status_when_branch_is_referenced_by_past_analysis_id() throws IOException {
- OrganizationDto organization = db.organizations().insert();
- ComponentDto project = db.components().insertPrivateProject(organization);
+ ComponentDto project = db.components().insertPrivateProject();
ComponentDto branch = db.components().insertProjectBranch(project);
SnapshotDto pastAnalysis = dbClient.snapshotDao().insert(dbSession, newAnalysis(branch)
.setLast(false)
@Test
public void return_live_status_when_project_is_referenced_by_its_key() throws IOException {
- OrganizationDto organization = db.organizations().insert();
- ComponentDto project = db.components().insertPrivateProject(organization);
+ ComponentDto project = db.components().insertPrivateProject();
dbClient.snapshotDao().insert(dbSession, newAnalysis(project)
.setPeriodMode("last_version")
.setPeriodParam("2015-12-07")
@Test
public void return_live_status_when_branch_is_referenced_by_its_key() throws IOException {
- OrganizationDto organization = db.organizations().insert();
- ComponentDto project = db.components().insertPrivateProject(organization);
+ ComponentDto project = db.components().insertPrivateProject();
ComponentDto branch = db.components().insertProjectBranch(project);
dbClient.snapshotDao().insert(dbSession, newAnalysis(branch)
@Test
public void return_live_status_when_pull_request_is_referenced_by_its_key() throws IOException {
- OrganizationDto organization = db.organizations().insert();
- ComponentDto project = db.components().insertPrivateProject(organization);
+ ComponentDto project = db.components().insertPrivateProject();
ComponentDto pr = db.components().insertProjectBranch(project, branch -> branch.setBranchType(BranchType.PULL_REQUEST));
dbClient.snapshotDao().insert(dbSession, newAnalysis(pr)
@Test
public void return_undefined_status_if_specified_analysis_is_not_found() {
- OrganizationDto organization = db.organizations().insert();
- ComponentDto project = db.components().insertPrivateProject(organization);
+ ComponentDto project = db.components().insertPrivateProject();
SnapshotDto snapshot = dbClient.snapshotDao().insert(dbSession, newAnalysis(project));
dbSession.commit();
userSession.addProjectPermission(UserRole.USER, project);
@Test
public void return_undefined_status_if_project_is_not_analyzed() {
- OrganizationDto organization = db.organizations().insert();
- ComponentDto project = db.components().insertPrivateProject(organization);
+ ComponentDto project = db.components().insertPrivateProject();
userSession.addProjectPermission(UserRole.USER, project);
ProjectStatusResponse result = ws.newRequest()
@Test
public void project_administrator_is_allowed_to_get_project_status() {
- OrganizationDto organization = db.organizations().insert();
- ComponentDto project = db.components().insertPrivateProject(organization);
+ ComponentDto project = db.components().insertPrivateProject();
SnapshotDto snapshot = dbClient.snapshotDao().insert(dbSession, newAnalysis(project));
dbSession.commit();
userSession.addProjectPermission(UserRole.ADMIN, project);
@Test
public void project_user_is_allowed_to_get_project_status() {
- OrganizationDto organization = db.organizations().insert();
- ComponentDto project = db.components().insertPrivateProject(organization);
+ ComponentDto project = db.components().insertPrivateProject();
SnapshotDto snapshot = dbClient.snapshotDao().insert(dbSession, newAnalysis(project));
dbSession.commit();
userSession.addProjectPermission(UserRole.USER, project);
.executeProtobuf(ProjectStatusResponse.class);
}
- @Test
- public void default_organization_is_used_when_no_organization_parameter() {
- OrganizationDto organization = db.getDefaultOrganization();
- ComponentDto project = db.components().insertPrivateProject(organization);
- userSession.logIn().addProjectPermission(UserRole.USER, project);
-
- ProjectStatusResponse result = ws.newRequest()
- .setParam(PARAM_PROJECT_ID, project.uuid())
- .executeProtobuf(ProjectStatusResponse.class);
-
- assertThat(result.getProjectStatus().getStatus()).isEqualTo(Status.NONE);
- }
-
@Test
public void fail_if_no_snapshot_id_found() {
- OrganizationDto organization = db.organizations().insert();
logInAsSystemAdministrator();
expectedException.expect(NotFoundException.class);
@Test
public void fail_if_insufficient_privileges() {
- OrganizationDto organization = db.organizations().insert();
- ComponentDto project = db.components().insertPrivateProject(organization);
+ ComponentDto project = db.components().insertPrivateProject();
SnapshotDto snapshot = dbClient.snapshotDao().insert(dbSession, newAnalysis(project));
dbSession.commit();
userSession.logIn();
@Test
public void fail_if_project_id_and_ce_task_id_provided() {
- OrganizationDto organization = db.organizations().insert();
- ComponentDto project = db.components().insertPrivateProject(organization);
+ ComponentDto project = db.components().insertPrivateProject();
logInAsSystemAdministrator();
expectedException.expect(BadRequestException.class);
ws.newRequest()
.setParam(PARAM_ANALYSIS_ID, "analysis-id")
.setParam(PARAM_PROJECT_ID, "project-uuid")
- .setParam(PARAM_ORGANIZATION, organization.getKey())
.execute().getInput();
}
@Test
public void fail_if_branch_key_and_pull_request_id_provided() {
- OrganizationDto organization = db.organizations().insert();
- ComponentDto project = db.components().insertPrivateProject(organization);
+ ComponentDto project = db.components().insertPrivateProject();
logInAsSystemAdministrator();
expectedException.expect(BadRequestException.class);
.setParam(PARAM_PROJECT_KEY, "key")
.setParam(PARAM_BRANCH, "branch")
.setParam(PARAM_PULL_REQUEST, "pr")
- .setParam(PARAM_ORGANIZATION, organization.getKey())
.execute().getInput();
}
@Test
public void fail_when_using_branch_db_key() {
- OrganizationDto organization = db.organizations().insert();
- ComponentDto project = db.components().insertPublicProject(organization);
+ ComponentDto project = db.components().insertPublicProject();
userSession.logIn().addProjectPermission(UserRole.ADMIN, project);
ComponentDto branch = db.components().insertProjectBranch(project);
SnapshotDto snapshot = db.components().insertSnapshot(branch);
ws.newRequest()
.setParam(PARAM_PROJECT_KEY, branch.getDbKey())
- .setParam(PARAM_ORGANIZATION, organization.getKey())
.execute();
}
@Test
public void fail_when_using_branch_uuid() {
- OrganizationDto organization = db.organizations().insert();
- ComponentDto project = db.components().insertPublicProject(organization);
+ ComponentDto project = db.components().insertPublicProject();
userSession.logIn().addProjectPermission(UserRole.ADMIN, project);
ComponentDto branch = db.components().insertProjectBranch(project);
SnapshotDto snapshot = db.components().insertSnapshot(branch);
import org.sonar.api.server.ws.WebService;
import org.sonar.api.utils.System2;
import org.sonar.db.DbTester;
-import org.sonar.db.organization.OrganizationDto;
-import org.sonar.db.qualitygate.QGateWithOrgDto;
+import org.sonar.db.qualitygate.QualityGateDto;
import org.sonar.server.component.TestComponentFinder;
import org.sonar.server.exceptions.ForbiddenException;
import org.sonar.server.exceptions.NotFoundException;
-import org.sonar.server.organization.DefaultOrganizationProvider;
-import org.sonar.server.organization.TestDefaultOrganizationProvider;
import org.sonar.server.tester.UserSessionRule;
import org.sonar.server.ws.WsActionTester;
import org.sonarqube.ws.Qualitygates.QualityGate;
@Rule
public DbTester db = DbTester.create(System2.INSTANCE);
- private DefaultOrganizationProvider defaultOrganizationProvider = TestDefaultOrganizationProvider.from(db);
-
- private WsActionTester ws = new WsActionTester(
- new RenameAction(db.getDbClient(), new QualityGatesWsSupport(db.getDbClient(), userSession, defaultOrganizationProvider, TestComponentFinder.from(db))));
+ private final WsActionTester ws = new WsActionTester(
+ new RenameAction(db.getDbClient(), new QualityGatesWsSupport(db.getDbClient(), userSession, TestComponentFinder.from(db))));
@Test
public void verify_definition() {
.containsExactlyInAnyOrder(
tuple("id", false),
tuple("currentName", false),
- tuple("name", true),
- tuple("organization", false));
+ tuple("name", true));
}
@Test
public void rename() {
- OrganizationDto organization = db.organizations().insert();
- QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(organization, qg -> qg.setName("old name"));
+ QualityGateDto qualityGate = db.qualityGates().insertQualityGate(qg -> qg.setName("old name"));
userSession.logIn("john").addPermission(ADMINISTER_QUALITY_GATES);
ws.newRequest()
.setParam("id", qualityGate.getUuid())
.setParam("name", "new name")
- .setParam("organization", organization.getKey())
.execute();
assertThat(db.getDbClient().qualityGateDao().selectByUuid(db.getSession(), qualityGate.getUuid()).getName()).isEqualTo("new name");
@Test
public void response_contains_quality_gate() {
- OrganizationDto organization = db.organizations().insert();
userSession.logIn("john").addPermission(ADMINISTER_QUALITY_GATES);
- QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(organization, qg -> qg.setName("old name"));
+ QualityGateDto qualityGate = db.qualityGates().insertQualityGate(qg -> qg.setName("old name"));
QualityGate result = ws.newRequest()
.setParam("id", qualityGate.getUuid())
.setParam("name", "new name")
- .setParam("organization", organization.getKey())
.executeProtobuf(QualityGate.class);
assertThat(result.getId()).isEqualTo(qualityGate.getUuid());
@Test
public void rename_with_same_name() {
- OrganizationDto organization = db.organizations().insert();
userSession.logIn("john").addPermission(ADMINISTER_QUALITY_GATES);
- QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(organization, qg -> qg.setName("name"));
+ QualityGateDto qualityGate = db.qualityGates().insertQualityGate(qg -> qg.setName("name"));
ws.newRequest()
.setParam("id", qualityGate.getUuid())
.setParam("name", "name")
- .setParam("organization", organization.getKey())
.execute();
assertThat(db.getDbClient().qualityGateDao().selectByUuid(db.getSession(), qualityGate.getUuid()).getName()).isEqualTo("name");
}
- @Test
- public void default_organization_is_used_when_no_organization_parameter() {
- QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(db.getDefaultOrganization());
- OrganizationDto otherOrganization = db.organizations().insert();
- QGateWithOrgDto otherQualityGate = db.qualityGates().insertQualityGate(otherOrganization);
- userSession.logIn("john").addPermission(ADMINISTER_QUALITY_GATES);
-
- QualityGate result = ws.newRequest()
- .setParam("id", qualityGate.getUuid())
- .setParam("name", "new name")
- .executeProtobuf(QualityGate.class);
-
- assertThat(result.getId()).isEqualTo(qualityGate.getUuid());
- assertThat(result.getName()).isEqualTo("new name");
- }
-
@Test
public void fail_on_built_in_quality_gate() {
- OrganizationDto organization = db.organizations().insert();
userSession.logIn("john").addPermission(ADMINISTER_QUALITY_GATES);
- QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(organization, qg -> qg.setBuiltIn(true));
+ QualityGateDto qualityGate = db.qualityGates().insertQualityGate(qg -> qg.setBuiltIn(true));
expectedException.expect(IllegalArgumentException.class);
expectedException.expectMessage(format("Operation forbidden for built-in Quality Gate '%s'", qualityGate.getName()));
ws.newRequest()
.setParam("id", qualityGate.getUuid())
.setParam("name", "name")
- .setParam("organization", organization.getKey())
.execute();
}
@Test
public void fail_on_empty_name() {
- OrganizationDto organization = db.organizations().insert();
userSession.logIn("john").addPermission(ADMINISTER_QUALITY_GATES);
- QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(organization);
+ QualityGateDto qualityGate = db.qualityGates().insertQualityGate();
expectedException.expect(IllegalArgumentException.class);
expectedException.expectMessage("The 'name' parameter is missing");
ws.newRequest()
.setParam("id", qualityGate.getUuid())
.setParam("name", "")
- .setParam("organization", organization.getKey())
.execute();
}
@Test
public void fail_when_using_existing_name() {
- OrganizationDto organization = db.organizations().insert();
userSession.logIn("john").addPermission(ADMINISTER_QUALITY_GATES);
- QGateWithOrgDto qualityGate1 = db.qualityGates().insertQualityGate(organization);
- QGateWithOrgDto qualityGate2 = db.qualityGates().insertQualityGate(organization);
+ QualityGateDto qualityGate1 = db.qualityGates().insertQualityGate();
+ QualityGateDto qualityGate2 = db.qualityGates().insertQualityGate();
expectedException.expect(IllegalArgumentException.class);
expectedException.expectMessage(format("Name '%s' has already been taken", qualityGate2.getName()));
ws.newRequest()
.setParam("id", qualityGate1.getUuid())
.setParam("name", qualityGate2.getName())
- .setParam("organization", organization.getKey())
.execute();
}
@Test
public void fail_on_unknown_quality_gate() {
- OrganizationDto organization = db.organizations().insert();
userSession.logIn("john").addPermission(ADMINISTER_QUALITY_GATES);
expectedException.expect(NotFoundException.class);
ws.newRequest()
.setParam("id", "123")
.setParam("name", "new name")
- .setParam("organization", organization.getKey())
.execute();
}
@Test
public void fail_when_not_quality_gates_administer() {
- OrganizationDto organization = db.organizations().insert();
userSession.logIn("john").addPermission(ADMINISTER_QUALITY_PROFILES);
- QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(organization, qg -> qg.setName("old name"));
+ QualityGateDto qualityGate = db.qualityGates().insertQualityGate(qg -> qg.setName("old name"));
expectedException.expect(ForbiddenException.class);
ws.newRequest()
.setParam("id", qualityGate.getUuid())
.setParam("name", "new name")
- .setParam("organization", organization.getKey())
.execute();
}
import org.sonar.db.DbClient;
import org.sonar.db.DbTester;
import org.sonar.db.component.ComponentDto;
-import org.sonar.db.organization.OrganizationDto;
import org.sonar.db.project.ProjectDto;
import org.sonar.db.qualitygate.QualityGateDto;
import org.sonar.db.user.UserDto;
import org.sonar.server.component.TestComponentFinder;
import org.sonar.server.exceptions.NotFoundException;
-import org.sonar.server.organization.TestDefaultOrganizationProvider;
import org.sonar.server.tester.UserSessionRule;
import org.sonar.server.ws.WsActionTester;
import org.sonarqube.ws.Qualitygates.SearchResponse;
import org.sonarqube.ws.Qualitygates.SearchResponse.Result;
-import static java.lang.String.format;
import static java.lang.String.valueOf;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.AssertionsForClassTypes.tuple;
import static org.sonar.api.web.UserRole.USER;
import static org.sonar.db.permission.GlobalPermission.ADMINISTER_QUALITY_GATES;
import static org.sonar.server.qualitygate.ws.QualityGatesWsParameters.PARAM_GATE_ID;
-import static org.sonar.server.qualitygate.ws.QualityGatesWsParameters.PARAM_ORGANIZATION;
import static org.sonar.server.qualitygate.ws.QualityGatesWsParameters.PARAM_PAGE;
import static org.sonar.server.qualitygate.ws.QualityGatesWsParameters.PARAM_PAGE_SIZE;
import static org.sonarqube.ws.client.user.UsersWsParameters.PARAM_SELECTED;
@Rule
public DbTester db = DbTester.create();
- private DbClient dbClient = db.getDbClient();
- private TestDefaultOrganizationProvider defaultOrganizationProvider = TestDefaultOrganizationProvider.from(db);
-
- private SearchAction underTest = new SearchAction(dbClient, userSession,
- new QualityGatesWsSupport(dbClient, userSession, defaultOrganizationProvider, TestComponentFinder.from(db)));
- private WsActionTester ws = new WsActionTester(underTest);
-
- @Test
- public void search_projects_of_a_quality_gate_from_an_organization() {
- OrganizationDto organization = db.organizations().insert();
- ComponentDto project = db.components().insertPublicProject(organization);
- QualityGateDto qualityGate = db.qualityGates().insertQualityGate(organization);
- db.qualityGates().associateProjectToQualityGate(db.components().getProjectDto(project), qualityGate);
-
- SearchResponse response = ws.newRequest()
- .setParam(PARAM_GATE_ID, valueOf(qualityGate.getUuid()))
- .setParam(PARAM_ORGANIZATION, organization.getKey())
- .executeProtobuf(SearchResponse.class);
-
- assertThat(response.getResultsList())
- .extracting(Result::getKey, Result::getName)
- .containsExactlyInAnyOrder(tuple(project.getKey(), project.name()));
- }
+ private final DbClient dbClient = db.getDbClient();
+ private final SearchAction underTest = new SearchAction(dbClient, userSession,
+ new QualityGatesWsSupport(dbClient, userSession, TestComponentFinder.from(db)));
+ private final WsActionTester ws = new WsActionTester(underTest);
@Test
- public void search_on_default_organization_when_none_is_provided() {
- OrganizationDto defaultOrganization = db.getDefaultOrganization();
- ComponentDto project = db.components().insertPublicProject(defaultOrganization);
- QualityGateDto qualityGate = db.qualityGates().insertQualityGate(defaultOrganization);
+ public void search_projects_of_a_quality_gate() {
+ ComponentDto project = db.components().insertPublicProject();
+ QualityGateDto qualityGate = db.qualityGates().insertQualityGate();
db.qualityGates().associateProjectToQualityGate(db.components().getProjectDto(project), qualityGate);
SearchResponse response = ws.newRequest()
@Test
public void return_empty_association() {
- OrganizationDto organization = db.organizations().insert();
- QualityGateDto qualityGate = db.qualityGates().insertQualityGate(organization);
+ QualityGateDto qualityGate = db.qualityGates().insertQualityGate();
SearchResponse response = ws.newRequest()
.setParam(PARAM_GATE_ID, valueOf(qualityGate.getUuid()))
- .setParam(PARAM_ORGANIZATION, organization.getKey())
.executeProtobuf(SearchResponse.class);
assertThat(response.getResultsList()).isEmpty();
@Test
public void return_all_projects() {
- OrganizationDto organization = db.organizations().insert();
- QualityGateDto qualityGate = db.qualityGates().insertQualityGate(organization);
- ProjectDto unassociatedProject = db.components().insertPublicProjectDto(organization);
- ProjectDto associatedProject = db.components().insertPublicProjectDto(organization);
+ QualityGateDto qualityGate = db.qualityGates().insertQualityGate();
+ ProjectDto unassociatedProject = db.components().insertPublicProjectDto();
+ ProjectDto associatedProject = db.components().insertPublicProjectDto();
db.qualityGates().associateProjectToQualityGate(associatedProject, qualityGate);
SearchResponse response = ws.newRequest()
.setParam(PARAM_GATE_ID, valueOf(qualityGate.getUuid()))
- .setParam(PARAM_ORGANIZATION, organization.getKey())
.setParam(PARAM_SELECTED, ALL.value())
.executeProtobuf(SearchResponse.class);
@Test
public void return_only_associated_project() {
- OrganizationDto organization = db.organizations().insert();
- QualityGateDto qualityGate = db.qualityGates().insertQualityGate(organization);
- ProjectDto associatedProject = db.components().insertPublicProjectDto(organization);
- ProjectDto unassociatedProject = db.components().insertPublicProjectDto(organization);
+ QualityGateDto qualityGate = db.qualityGates().insertQualityGate();
+ ProjectDto associatedProject = db.components().insertPublicProjectDto();
+ ProjectDto unassociatedProject = db.components().insertPublicProjectDto();
db.qualityGates().associateProjectToQualityGate(associatedProject, qualityGate);
SearchResponse response = ws.newRequest()
.setParam(PARAM_GATE_ID, valueOf(qualityGate.getUuid()))
- .setParam(PARAM_ORGANIZATION, organization.getKey())
.setParam(PARAM_SELECTED, SELECTED.value())
.executeProtobuf(SearchResponse.class);
@Test
public void return_only_unassociated_project() {
- OrganizationDto organization = db.organizations().insert();
- QualityGateDto qualityGate = db.qualityGates().insertQualityGate(organization);
- ProjectDto associatedProject = db.components().insertPublicProjectDto(organization);
- ProjectDto unassociatedProject = db.components().insertPublicProjectDto(organization);
+ QualityGateDto qualityGate = db.qualityGates().insertQualityGate();
+ ProjectDto associatedProject = db.components().insertPublicProjectDto();
+ ProjectDto unassociatedProject = db.components().insertPublicProjectDto();
db.qualityGates().associateProjectToQualityGate(associatedProject, qualityGate);
SearchResponse response = ws.newRequest()
.setParam(PARAM_GATE_ID, valueOf(qualityGate.getUuid()))
- .setParam(PARAM_ORGANIZATION, organization.getKey())
.setParam(PARAM_SELECTED, DESELECTED.value())
.executeProtobuf(SearchResponse.class);
@Test
public void return_only_authorized_projects() {
- OrganizationDto organization = db.organizations().insert();
- QualityGateDto qualityGate = db.qualityGates().insertQualityGate(organization);
- ComponentDto project1 = db.components().insertPrivateProject(organization);
- ComponentDto project2 = db.components().insertPrivateProject(organization);
+ QualityGateDto qualityGate = db.qualityGates().insertQualityGate();
+ ComponentDto project1 = db.components().insertPrivateProject();
+ ComponentDto project2 = db.components().insertPrivateProject();
UserDto user = db.users().insertUser();
// User can only see project1 1
db.users().insertProjectPermissionOnUser(user, USER, project1);
SearchResponse response = ws.newRequest()
.setParam(PARAM_GATE_ID, valueOf(qualityGate.getUuid()))
- .setParam(PARAM_ORGANIZATION, organization.getKey())
.setParam(PARAM_SELECTED, ALL.value())
.executeProtobuf(SearchResponse.class);
@Test
public void root_user() {
- OrganizationDto organization = db.organizations().insert();
- QualityGateDto qualityGate = db.qualityGates().insertQualityGate(organization);
- ComponentDto project = db.components().insertPrivateProject(organization);
+ QualityGateDto qualityGate = db.qualityGates().insertQualityGate();
+ ComponentDto project = db.components().insertPrivateProject();
userSession.logIn().setRoot();
SearchResponse response = ws.newRequest()
.setParam(PARAM_GATE_ID, valueOf(qualityGate.getUuid()))
- .setParam(PARAM_ORGANIZATION, organization.getKey())
.setParam(PARAM_SELECTED, ALL.value())
.executeProtobuf(SearchResponse.class);
@Test
public void test_paging() {
- OrganizationDto organization = db.organizations().insert();
- QualityGateDto qualityGate = db.qualityGates().insertQualityGate(organization);
- ProjectDto project1 = db.components().insertPublicProjectDto(organization);
- ProjectDto project2 = db.components().insertPublicProjectDto(organization);
- ProjectDto project3 = db.components().insertPublicProjectDto(organization);
+ QualityGateDto qualityGate = db.qualityGates().insertQualityGate();
+ ProjectDto project1 = db.components().insertPublicProjectDto();
+ ProjectDto project2 = db.components().insertPublicProjectDto();
+ ProjectDto project3 = db.components().insertPublicProjectDto();
db.qualityGates().associateProjectToQualityGate(project1, qualityGate);
// Return partial result on first page
assertThat(ws.newRequest()
.setParam(PARAM_GATE_ID, valueOf(qualityGate.getUuid()))
- .setParam(PARAM_ORGANIZATION, organization.getKey())
.setParam(PARAM_SELECTED, ALL.value())
.setParam(PARAM_PAGE, "1")
.setParam(PARAM_PAGE_SIZE, "1")
.executeProtobuf(SearchResponse.class)
.getResultsList())
- .extracting(Result::getName)
- .containsExactlyInAnyOrder(project1.getName());
+ .extracting(Result::getName)
+ .containsExactlyInAnyOrder(project1.getName());
// Return partial result on second page
assertThat(ws.newRequest()
.setParam(PARAM_GATE_ID, valueOf(qualityGate.getUuid()))
- .setParam(PARAM_ORGANIZATION, organization.getKey())
.setParam(PARAM_SELECTED, ALL.value())
.setParam(PARAM_PAGE, "2")
.setParam(PARAM_PAGE_SIZE, "1")
.executeProtobuf(SearchResponse.class)
.getResultsList())
- .extracting(Result::getName)
- .containsExactlyInAnyOrder(project2.getName());
+ .extracting(Result::getName)
+ .containsExactlyInAnyOrder(project2.getName());
// Return partial result on first page
assertThat(ws.newRequest()
.setParam(PARAM_GATE_ID, valueOf(qualityGate.getUuid()))
- .setParam(PARAM_ORGANIZATION, organization.getKey())
.setParam(PARAM_SELECTED, ALL.value())
.setParam(PARAM_PAGE, "1")
.setParam(PARAM_PAGE_SIZE, "2")
.executeProtobuf(SearchResponse.class)
.getResultsList())
- .extracting(Result::getName)
- .containsExactlyInAnyOrder(project1.getName(), project2.getName());
+ .extracting(Result::getName)
+ .containsExactlyInAnyOrder(project1.getName(), project2.getName());
// Return all result on first page
assertThat(ws.newRequest()
.setParam(PARAM_GATE_ID, valueOf(qualityGate.getUuid()))
- .setParam(PARAM_ORGANIZATION, organization.getKey())
.setParam(PARAM_SELECTED, ALL.value())
.setParam(PARAM_PAGE, "1")
.setParam(PARAM_PAGE_SIZE, "3")
.executeProtobuf(SearchResponse.class)
.getResultsList())
- .extracting(Result::getName)
- .containsExactlyInAnyOrder(project1.getName(), project2.getName(), project3.getName());
+ .extracting(Result::getName)
+ .containsExactlyInAnyOrder(project1.getName(), project2.getName(), project3.getName());
// Return no result as page index is off limit
assertThat(ws.newRequest()
.setParam(PARAM_GATE_ID, valueOf(qualityGate.getUuid()))
- .setParam(PARAM_ORGANIZATION, organization.getKey())
.setParam(PARAM_SELECTED, ALL.value())
.setParam(PARAM_PAGE, "3")
.setParam(PARAM_PAGE_SIZE, "3")
.executeProtobuf(SearchResponse.class)
.getResultsList())
- .extracting(Result::getName)
- .isEmpty();
+ .extracting(Result::getName)
+ .isEmpty();
}
@Test
public void test_pagination_on_many_pages() {
- OrganizationDto organization = db.organizations().insert();
- QualityGateDto qualityGate = db.qualityGates().insertQualityGate(organization);
+ QualityGateDto qualityGate = db.qualityGates().insertQualityGate();
for (int i = 0; i < 20; i++) {
- ProjectDto project = db.components().insertPublicProjectDto(organization);
+ ProjectDto project = db.components().insertPublicProjectDto();
db.qualityGates().associateProjectToQualityGate(project, qualityGate);
}
userSession.addPermission(ADMINISTER_QUALITY_GATES);
SearchResponse response = ws.newRequest()
.setParam(PARAM_GATE_ID, valueOf(qualityGate.getUuid()))
- .setParam(PARAM_ORGANIZATION, organization.getKey())
.setParam(PARAM_PAGE_SIZE, valueOf(5))
.setParam(PARAM_PAGE, valueOf(2))
.executeProtobuf(SearchResponse.class);
@Test
public void test_pagination_on_one_page() {
- OrganizationDto organization = db.organizations().insert();
- QualityGateDto qualityGate = db.qualityGates().insertQualityGate(organization);
+ QualityGateDto qualityGate = db.qualityGates().insertQualityGate();
for (int i = 0; i < 20; i++) {
- ProjectDto project = db.components().insertPublicProjectDto(organization);
+ ProjectDto project = db.components().insertPublicProjectDto();
db.qualityGates().associateProjectToQualityGate(project, qualityGate);
}
userSession.addPermission(ADMINISTER_QUALITY_GATES);
SearchResponse response = ws.newRequest()
.setParam(PARAM_GATE_ID, valueOf(qualityGate.getUuid()))
- .setParam(PARAM_ORGANIZATION, organization.getKey())
.setParam(PARAM_PAGE_SIZE, valueOf(100))
.setParam(PARAM_PAGE, valueOf(1))
.executeProtobuf(SearchResponse.class);
.contains(false, 1, 100, 20);
}
- @Test
- public void return_only_projects_from_organization() {
- OrganizationDto organization = db.organizations().insert();
- OrganizationDto otherOrganization = db.organizations().insert();
- QualityGateDto qualityGate = db.qualityGates().insertQualityGate(organization);
- QualityGateDto otherQualityGate = db.qualityGates().insertQualityGate(otherOrganization);
- ProjectDto project = db.components().insertPublicProjectDto(organization);
- ProjectDto otherProject = db.components().insertPublicProjectDto(otherOrganization);
- db.qualityGates().associateProjectToQualityGate(project, qualityGate);
- db.qualityGates().associateProjectToQualityGate(otherProject, otherQualityGate);
-
- SearchResponse response = ws.newRequest()
- .setParam(PARAM_GATE_ID, valueOf(qualityGate.getUuid()))
- .setParam(PARAM_ORGANIZATION, organization.getKey())
- .setParam(PARAM_SELECTED, ALL.value())
- .executeProtobuf(SearchResponse.class);
-
- assertThat(response.getResultsList())
- .extracting(Result::getName)
- .containsExactlyInAnyOrder(project.getName());
- }
-
@Test
public void fail_on_unknown_quality_gate() {
- OrganizationDto organization = db.organizations().insert();
-
expectedException.expect(NotFoundException.class);
- expectedException.expectMessage(format("No quality gate has been found for id 42 in organization %s", organization.getName()));
+ expectedException.expectMessage("No quality gate has been found for id 42");
ws.newRequest()
.setParam(PARAM_GATE_ID, "42")
- .setParam(PARAM_ORGANIZATION, organization.getKey())
- .executeProtobuf(SearchResponse.class);
- }
-
- @Test
- public void fail_when_quality_gates_does_not_belong_to_organization() {
- OrganizationDto organization = db.organizations().insert();
- OrganizationDto otherOrganization = db.organizations().insert();
- QualityGateDto qualityGate = db.qualityGates().insertQualityGate(otherOrganization);
-
- expectedException.expect(NotFoundException.class);
- expectedException.expectMessage(format("No quality gate has been found for id %s in organization %s", qualityGate.getUuid(), organization.getName()));
-
- ws.newRequest()
- .setParam(PARAM_GATE_ID, valueOf(qualityGate.getUuid()))
- .setParam(PARAM_ORGANIZATION, organization.getKey())
.executeProtobuf(SearchResponse.class);
}
tuple("gateId", false),
tuple("gateName", false),
tuple("query", false),
- tuple("organization", false),
tuple("selected", false),
tuple("page", false),
tuple("pageSize", false));
import org.sonar.db.DbClient;
import org.sonar.db.DbTester;
import org.sonar.db.component.ComponentDto;
-import org.sonar.db.organization.OrganizationDto;
-import org.sonar.db.qualitygate.QGateWithOrgDto;
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;
-import org.sonar.server.organization.TestDefaultOrganizationProvider;
import org.sonar.server.tester.UserSessionRule;
import org.sonar.server.ws.WsActionTester;
@Rule
public DbTester db = DbTester.create(System2.INSTANCE);
- private DbClient dbClient = db.getDbClient();
- private ComponentFinder componentFinder = TestComponentFinder.from(db);
- private SelectAction underTest = new SelectAction(dbClient, new QualityGatesWsSupport(db.getDbClient(), userSession, TestDefaultOrganizationProvider.from(db), componentFinder));
- private WsActionTester ws = new WsActionTester(underTest);
+ private final DbClient dbClient = db.getDbClient();
+ private final ComponentFinder componentFinder = TestComponentFinder.from(db);
+ private final SelectAction underTest = new SelectAction(dbClient,
+ new QualityGatesWsSupport(db.getDbClient(), userSession, componentFinder));
+ private final WsActionTester ws = new WsActionTester(underTest);
@Test
public void select_by_key() {
- OrganizationDto organization = db.organizations().insert();
userSession.addPermission(ADMINISTER_QUALITY_GATES);
- QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(organization);
- ComponentDto project = db.components().insertPrivateProject(organization);
+ QualityGateDto qualityGate = db.qualityGates().insertQualityGate();
+ ComponentDto project = db.components().insertPrivateProject();
ws.newRequest()
.setParam("gateId", qualityGate.getUuid())
.setParam("projectKey", project.getKey())
- .setParam("organization", organization.getKey())
.execute();
assertSelected(qualityGate, project);
@Test
public void change_quality_gate_for_project() {
- OrganizationDto organization = db.organizations().insert();
userSession.addPermission(ADMINISTER_QUALITY_GATES);
- QGateWithOrgDto initialQualityGate = db.qualityGates().insertQualityGate(organization);
- QGateWithOrgDto secondQualityGate = db.qualityGates().insertQualityGate(organization);
- ComponentDto project = db.components().insertPrivateProject(organization);
+ QualityGateDto initialQualityGate = db.qualityGates().insertQualityGate();
+ QualityGateDto secondQualityGate = db.qualityGates().insertQualityGate();
+ ComponentDto project = db.components().insertPrivateProject();
ws.newRequest()
.setParam("gateId", initialQualityGate.getUuid())
.setParam("projectKey", project.getKey())
- .setParam("organization", organization.getKey())
.execute();
ws.newRequest()
.setParam("gateId", secondQualityGate.getUuid())
.setParam("projectKey", project.getKey())
- .setParam("organization", organization.getKey())
.execute();
assertSelected(secondQualityGate, project);
@Test
public void select_same_quality_gate_for_project_twice() {
- OrganizationDto organization = db.organizations().insert();
userSession.addPermission(ADMINISTER_QUALITY_GATES);
- QGateWithOrgDto initialQualityGate = db.qualityGates().insertQualityGate(organization);
- ComponentDto project = db.components().insertPrivateProject(organization);
+ QualityGateDto initialQualityGate = db.qualityGates().insertQualityGate();
+ ComponentDto project = db.components().insertPrivateProject();
ws.newRequest()
.setParam("gateId", initialQualityGate.getUuid())
.setParam("projectKey", project.getKey())
- .setParam("organization", organization.getKey())
.execute();
ws.newRequest()
.setParam("gateId", initialQualityGate.getUuid())
.setParam("projectKey", project.getKey())
- .setParam("organization", organization.getKey())
.execute();
assertSelected(initialQualityGate, project);
@Test
public void project_admin() {
- OrganizationDto organization = db.organizations().insert();
- QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(organization);
- ComponentDto project = db.components().insertPrivateProject(organization);
+ QualityGateDto qualityGate = db.qualityGates().insertQualityGate();
+ ComponentDto project = db.components().insertPrivateProject();
userSession.logIn().addProjectPermission(ADMIN, project);
ws.newRequest()
.setParam("gateId", qualityGate.getUuid())
.setParam("projectKey", project.getKey())
- .setParam("organization", organization.getKey())
.execute();
assertSelected(qualityGate, project);
@Test
public void gate_administrator_can_associate_a_gate_to_a_project() {
- OrganizationDto organization = db.organizations().insert();
userSession.addPermission(ADMINISTER_QUALITY_GATES);
- QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(organization);
- ComponentDto project = db.components().insertPrivateProject(organization);
+ QualityGateDto qualityGate = db.qualityGates().insertQualityGate();
+ ComponentDto project = db.components().insertPrivateProject();
ws.newRequest()
.setParam("gateId", qualityGate.getUuid())
.setParam("projectKey", project.getKey())
- .setParam("organization", organization.getKey())
.execute();
assertSelected(qualityGate, project);
}
- @Test
- public void default_organization_is_used_when_no_organization_parameter() {
- OrganizationDto organization = db.getDefaultOrganization();
- userSession.addPermission(ADMINISTER_QUALITY_GATES);
- QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(organization);
- ComponentDto project = db.components().insertPrivateProject(organization);
-
- ws.newRequest()
- .setParam("gateId", qualityGate.getUuid())
- .setParam("projectKey", project.getKey())
- .execute();
-
- assertSelected(qualityGate, project);
- }
-
- @Test
- public void fail_when_project_belongs_to_another_organization() {
- OrganizationDto organization = db.organizations().insert();
- userSession.addPermission(ADMINISTER_QUALITY_GATES);
- OrganizationDto anotherOrganization = db.organizations().insert();
- ComponentDto project = db.components().insertPrivateProject(anotherOrganization);
- QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(organization);
-
- expectedException.expect(NotFoundException.class);
- expectedException.expectMessage(format("Project '%s' doesn't exist in organization '%s'", project.getKey(), organization.getKey()));
-
- ws.newRequest()
- .setParam("gateId", qualityGate.getUuid())
- .setParam("projectKey", project.getKey())
- .setParam("organization", organization.getKey())
- .execute();
- }
-
@Test
public void fail_when_no_quality_gate() {
- OrganizationDto organization = db.organizations().insert();
userSession.addPermission(ADMINISTER_QUALITY_GATES);
- ComponentDto project = db.components().insertPrivateProject(organization);
+ ComponentDto project = db.components().insertPrivateProject();
expectedException.expect(NotFoundException.class);
ws.newRequest()
.setParam("gateId", "1")
.setParam("projectKey", project.getKey())
- .setParam("organization", organization.getKey())
.execute();
}
@Test
public void fail_when_no_project_key() {
- OrganizationDto organization = db.organizations().insert();
userSession.addPermission(ADMINISTER_QUALITY_GATES);
- QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(organization);
+ QualityGateDto qualityGate = db.qualityGates().insertQualityGate();
expectedException.expect(NotFoundException.class);
ws.newRequest()
.setParam("gateId", qualityGate.getUuid())
.setParam("projectKey", "unknown")
- .setParam("organization", organization.getKey())
.execute();
}
@Test
public void fail_when_anonymous() {
- OrganizationDto organization = db.organizations().insert();
- QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(organization);
- ComponentDto project = db.components().insertPrivateProject(organization);
+ QualityGateDto qualityGate = db.qualityGates().insertQualityGate();
+ ComponentDto project = db.components().insertPrivateProject();
userSession.anonymous();
expectedException.expect(ForbiddenException.class);
ws.newRequest()
.setParam("gateId", qualityGate.getUuid())
.setParam("projectKey", project.getKey())
- .setParam("organization", organization.getKey())
.execute();
}
@Test
public void fail_when_not_project_admin() {
- OrganizationDto organization = db.organizations().insert();
- QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(organization);
- ComponentDto project = db.components().insertPrivateProject(organization);
+ QualityGateDto qualityGate = db.qualityGates().insertQualityGate();
+ ComponentDto project = db.components().insertPrivateProject();
userSession.logIn().addProjectPermission(ISSUE_ADMIN, project);
expectedException.expect(ForbiddenException.class);
ws.newRequest()
.setParam("gateId", qualityGate.getUuid())
.setParam("projectKey", project.getDbKey())
- .setParam("organization", organization.getKey())
.execute();
}
@Test
public void fail_when_not_quality_gates_admin() {
- OrganizationDto organization = db.organizations().insert();
- QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(organization);
- ComponentDto project = db.components().insertPrivateProject(organization);
+ QualityGateDto qualityGate = db.qualityGates().insertQualityGate();
+ ComponentDto project = db.components().insertPrivateProject();
userSession.logIn();
expectedException.expect(ForbiddenException.class);
ws.newRequest()
.setParam("gateId", qualityGate.getUuid())
.setParam("projectKey", project.getDbKey())
- .setParam("organization", organization.getKey())
.execute();
}
@Test
public void fail_when_using_branch_db_key() {
- OrganizationDto organization = db.organizations().insert();
- QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(organization);
- ComponentDto project = db.components().insertPublicProject(organization);
+ QualityGateDto qualityGate = db.qualityGates().insertQualityGate();
+ ComponentDto project = db.components().insertPublicProject();
userSession.logIn().addProjectPermission(ADMIN, project);
ComponentDto branch = db.components().insertProjectBranch(project);
ws.newRequest()
.setParam("gateId", qualityGate.getUuid())
.setParam("projectKey", branch.getDbKey())
- .setParam("organization", organization.getKey())
.execute();
}
import org.junit.Rule;
import org.junit.Test;
-import org.junit.rules.ExpectedException;
import org.sonar.api.server.ws.WebService;
import org.sonar.api.utils.System2;
import org.sonar.db.DbTester;
-import org.sonar.db.qualitygate.QGateWithOrgDto;
+import org.sonar.db.qualitygate.QualityGateDto;
import org.sonar.server.component.TestComponentFinder;
-import org.sonar.server.organization.DefaultOrganizationProvider;
-import org.sonar.server.organization.TestDefaultOrganizationProvider;
import org.sonar.server.tester.UserSessionRule;
import org.sonar.server.ws.WsActionTester;
import static org.sonar.db.permission.GlobalPermission.ADMINISTER_QUALITY_GATES;
public class SetAsDefaultActionTest {
- @Rule
- public ExpectedException expectedException = ExpectedException.none();
@Rule
public UserSessionRule userSession = UserSessionRule.standalone();
@Rule
public DbTester db = DbTester.create(System2.INSTANCE);
- private DefaultOrganizationProvider defaultOrganizationProvider = TestDefaultOrganizationProvider.from(db);
-
- private WsActionTester ws = new WsActionTester(
- new SetAsDefaultAction(db.getDbClient(), userSession, new QualityGatesWsSupport(db.getDbClient(), userSession, defaultOrganizationProvider, TestComponentFinder.from(db))));
+ private final WsActionTester ws = new WsActionTester(
+ new SetAsDefaultAction(db.getDbClient(), userSession, new QualityGatesWsSupport(db.getDbClient(), userSession, TestComponentFinder.from(db))));
@Test
public void verify_definition() {
.extracting(WebService.Param::key, WebService.Param::isRequired)
.containsExactlyInAnyOrder(
tuple("id", false),
- tuple("organization", false),
tuple("name", false));
}
@Test
public void set_default() {
userSession.logIn("john").addPermission(ADMINISTER_QUALITY_GATES);
- QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(db.getDefaultOrganization(), qg -> qg.setName("name"));
+ QualityGateDto qualityGate = db.qualityGates().insertQualityGate(qg -> qg.setName("name"));
ws.newRequest()
.setParam("name", "name")
.execute();
- assertThat(db.getDbClient().organizationDao().selectByKey(db.getSession(), db.getDefaultOrganization().getKey()).get()
- .getDefaultQualityGateUuid()).isEqualTo(qualityGate.getUuid());
+ assertThat(db.getDbClient().propertiesDao().selectGlobalProperty(db.getSession(), "qualitygate.default").getValue())
+ .isEqualTo(qualityGate.getUuid());
}
}
import org.sonar.api.utils.System2;
import org.sonar.db.DbTester;
import org.sonar.db.metric.MetricDto;
-import org.sonar.db.organization.OrganizationDto;
-import org.sonar.db.qualitygate.QGateWithOrgDto;
import org.sonar.db.qualitygate.QualityGateConditionDto;
import org.sonar.db.qualitygate.QualityGateDto;
-import org.sonar.db.user.UserDto;
import org.sonar.server.component.TestComponentFinder;
import org.sonar.server.exceptions.NotFoundException;
-import org.sonar.server.organization.DefaultOrganizationProvider;
-import org.sonar.server.organization.TestDefaultOrganizationProvider;
import org.sonar.server.qualitygate.QualityGateFinder;
import org.sonar.server.tester.UserSessionRule;
import org.sonar.server.ws.WsActionTester;
import static java.lang.String.format;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.AssertionsForClassTypes.tuple;
-import static org.sonar.db.organization.OrganizationDto.Subscription.PAID;
import static org.sonar.db.permission.GlobalPermission.ADMINISTER_QUALITY_GATES;
import static org.sonar.db.permission.GlobalPermission.ADMINISTER_QUALITY_PROFILES;
import static org.sonar.test.JsonAssert.assertJson;
@Rule
public DbTester db = DbTester.create(System2.INSTANCE);
- private DefaultOrganizationProvider defaultOrganizationProvider = TestDefaultOrganizationProvider.from(db);
-
- private WsActionTester ws = new WsActionTester(
+ private final WsActionTester ws = new WsActionTester(
new ShowAction(db.getDbClient(), new QualityGateFinder(db.getDbClient()),
- new QualityGatesWsSupport(db.getDbClient(), userSession, defaultOrganizationProvider, TestComponentFinder.from(db))));
+ new QualityGatesWsSupport(db.getDbClient(), userSession, TestComponentFinder.from(db))));
@Test
public void show() {
- OrganizationDto organization = db.organizations().insert();
- QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(organization);
- db.qualityGates().setDefaultQualityGate(organization, qualityGate);
+ QualityGateDto qualityGate = db.qualityGates().insertQualityGate();
+ db.qualityGates().setDefaultQualityGate(qualityGate);
MetricDto metric1 = db.measures().insertMetric();
MetricDto metric2 = db.measures().insertMetric();
QualityGateConditionDto condition1 = db.qualityGates().addCondition(qualityGate, metric1, c -> c.setOperator("GT"));
ShowWsResponse response = ws.newRequest()
.setParam("name", qualityGate.getName())
- .setParam("organization", organization.getKey())
.executeProtobuf(ShowWsResponse.class);
assertThat(response.getId()).isEqualTo(qualityGate.getUuid());
tuple(condition2.getUuid(), metric2.getKey(), "LT", condition2.getErrorThreshold()));
}
- @Test
- public void default_organization_is_used_when_no_organization_parameter() {
- QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(db.getDefaultOrganization());
- OrganizationDto otherOrganization = db.organizations().insert();
- QGateWithOrgDto otherQualityGate = db.qualityGates().insertQualityGate(otherOrganization);
- db.qualityGates().setDefaultQualityGate(db.getDefaultOrganization(), qualityGate);
-
- ShowWsResponse response = ws.newRequest()
- .setParam("name", qualityGate.getName())
- .executeProtobuf(ShowWsResponse.class);
-
- assertThat(response.getId()).isEqualTo(qualityGate.getUuid());
- }
-
@Test
public void show_built_in() {
- OrganizationDto organization = db.organizations().insert();
- QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(organization, qg -> qg.setBuiltIn(true));
- db.qualityGates().setDefaultQualityGate(organization, qualityGate);
+ QualityGateDto qualityGate = db.qualityGates().insertQualityGate(qg -> qg.setBuiltIn(true));
+ db.qualityGates().setDefaultQualityGate(qualityGate);
ShowWsResponse response = ws.newRequest()
.setParam("name", qualityGate.getName())
- .setParam("organization", organization.getKey())
.executeProtobuf(ShowWsResponse.class);
assertThat(response.getIsBuiltIn()).isTrue();
@Test
public void show_by_id() {
- OrganizationDto organization = db.organizations().insert();
- QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(organization);
- db.qualityGates().setDefaultQualityGate(organization, qualityGate);
+ QualityGateDto qualityGate = db.qualityGates().insertQualityGate();
+ db.qualityGates().setDefaultQualityGate(qualityGate);
ShowWsResponse response = ws.newRequest()
.setParam("id", qualityGate.getUuid())
- .setParam("organization", organization.getKey())
.executeProtobuf(ShowWsResponse.class);
assertThat(response.getId()).isEqualTo(qualityGate.getUuid());
@Test
public void no_condition() {
- OrganizationDto organization = db.organizations().insert();
- QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(organization);
- db.qualityGates().setDefaultQualityGate(organization, qualityGate);
+ QualityGateDto qualityGate = db.qualityGates().insertQualityGate();
+ db.qualityGates().setDefaultQualityGate(qualityGate);
ShowWsResponse response = ws.newRequest()
.setParam("name", qualityGate.getName())
- .setParam("organization", organization.getKey())
.executeProtobuf(ShowWsResponse.class);
assertThat(response.getId()).isEqualTo(qualityGate.getUuid());
@Test
public void actions() {
- OrganizationDto organization = db.organizations().insert();
userSession.logIn("john").addPermission(ADMINISTER_QUALITY_GATES);
- QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(organization);
- QGateWithOrgDto qualityGate2 = db.qualityGates().insertQualityGate(organization);
- db.qualityGates().setDefaultQualityGate(organization, qualityGate2);
+ QualityGateDto qualityGate = db.qualityGates().insertQualityGate();
+ QualityGateDto qualityGate2 = db.qualityGates().insertQualityGate();
+ db.qualityGates().setDefaultQualityGate(qualityGate2);
ShowWsResponse response = ws.newRequest()
.setParam("name", qualityGate.getName())
- .setParam("organization", organization.getKey())
.executeProtobuf(ShowWsResponse.class);
Actions actions = response.getActions();
@Test
public void actions_on_default() {
- OrganizationDto organization = db.organizations().insert();
userSession.logIn("john").addPermission(ADMINISTER_QUALITY_GATES);
- QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(organization);
- db.qualityGates().setDefaultQualityGate(organization, qualityGate);
+ QualityGateDto qualityGate = db.qualityGates().insertQualityGate();
+ db.qualityGates().setDefaultQualityGate(qualityGate);
ShowWsResponse response = ws.newRequest()
.setParam("name", qualityGate.getName())
- .setParam("organization", organization.getKey())
.executeProtobuf(ShowWsResponse.class);
Actions actions = response.getActions();
@Test
public void actions_on_built_in() {
- OrganizationDto organization = db.organizations().insert();
userSession.logIn("john").addPermission(ADMINISTER_QUALITY_GATES);
- QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(organization, qg -> qg.setBuiltIn(true));
- QGateWithOrgDto qualityGate2 = db.qualityGates().insertQualityGate(organization, qg -> qg.setBuiltIn(false));
- db.qualityGates().setDefaultQualityGate(organization, qualityGate2);
+ QualityGateDto qualityGate = db.qualityGates().insertQualityGate(qg -> qg.setBuiltIn(true));
+ QualityGateDto qualityGate2 = db.qualityGates().insertQualityGate(qg -> qg.setBuiltIn(false));
+ db.qualityGates().setDefaultQualityGate(qualityGate2);
ShowWsResponse response = ws.newRequest()
.setParam("name", qualityGate.getName())
- .setParam("organization", organization.getKey())
.executeProtobuf(ShowWsResponse.class);
Actions actions = response.getActions();
@Test
public void actions_when_not_quality_gate_administer() {
- OrganizationDto organization = db.organizations().insert();
userSession.logIn("john").addPermission(ADMINISTER_QUALITY_PROFILES);
- QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(organization, qg -> qg.setBuiltIn(true));
- db.qualityGates().setDefaultQualityGate(organization, qualityGate);
+ QualityGateDto qualityGate = db.qualityGates().insertQualityGate(qg -> qg.setBuiltIn(true));
+ db.qualityGates().setDefaultQualityGate(qualityGate);
ShowWsResponse response = ws.newRequest()
.setParam("name", qualityGate.getName())
- .setParam("organization", organization.getKey())
.executeProtobuf(ShowWsResponse.class);
Actions actions = response.getActions();
assertThat(actions.getAssociateProjects()).isFalse();
}
- @Test
- public void show_on_paid_organization() {
- OrganizationDto organization = db.organizations().insert(o -> o.setSubscription(PAID));
- QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(organization);
- db.qualityGates().setDefaultQualityGate(organization, qualityGate);
- MetricDto metric = db.measures().insertMetric();
- db.qualityGates().addCondition(qualityGate, metric);
- UserDto user = db.users().insertUser();
-
- ShowWsResponse response = ws.newRequest()
- .setParam("name", qualityGate.getName())
- .setParam("organization", organization.getKey())
- .executeProtobuf(ShowWsResponse.class);
-
- assertThat(response.getConditionsList()).hasSize(1);
- }
-
@Test
public void fail_when_no_name_or_id() {
- OrganizationDto organization = db.organizations().insert();
- QualityGateDto qualityGate = db.qualityGates().insertQualityGate(organization);
+ QualityGateDto qualityGate = db.qualityGates().insertQualityGate();
expectedException.expect(IllegalArgumentException.class);
expectedException.expectMessage("Either 'id' or 'name' must be provided");
- ws.newRequest()
- .setParam("organization", organization.getKey())
- .execute();
+ ws.newRequest().execute();
}
@Test
public void fail_when_both_name_or_id() {
- OrganizationDto organization = db.organizations().insert();
- QualityGateDto qualityGate = db.qualityGates().insertQualityGate(organization);
+ QualityGateDto qualityGate = db.qualityGates().insertQualityGate();
expectedException.expect(IllegalArgumentException.class);
expectedException.expectMessage("Either 'id' or 'name' must be provided");
ws.newRequest()
.setParam("name", qualityGate.getName())
.setParam("id", qualityGate.getUuid())
- .setParam("organization", organization.getKey())
.execute();
}
@Test
public void fail_when_condition_is_on_disabled_metric() {
- OrganizationDto organization = db.organizations().insert();
- QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(organization);
- db.qualityGates().setDefaultQualityGate(organization, qualityGate);
+ QualityGateDto qualityGate = db.qualityGates().insertQualityGate();
+ db.qualityGates().setDefaultQualityGate(qualityGate);
MetricDto metric = db.measures().insertMetric();
db.qualityGates().addCondition(qualityGate, metric);
db.getDbClient().metricDao().disableCustomByKey(db.getSession(), metric.getKey());
ws.newRequest()
.setParam("name", qualityGate.getName())
- .setParam("organization", organization.getKey())
.execute();
}
@Test
public void fail_when_quality_name_does_not_exist() {
- OrganizationDto organization = db.organizations().insert();
- QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(organization);
+ QualityGateDto qualityGate = db.qualityGates().insertQualityGate();
expectedException.expect(NotFoundException.class);
expectedException.expectMessage("No quality gate has been found for name UNKNOWN");
ws.newRequest()
.setParam("name", "UNKNOWN")
- .setParam("organization", organization.getKey())
.execute();
}
@Test
public void fail_when_quality_id_does_not_exist() {
- OrganizationDto organization = db.organizations().insert();
- QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(organization);
+ QualityGateDto qualityGate = db.qualityGates().insertQualityGate();
expectedException.expect(NotFoundException.class);
expectedException.expectMessage("No quality gate has been found for id 123");
ws.newRequest()
.setParam("id", "123")
- .setParam("organization", organization.getKey())
- .execute();
- }
-
- @Test
- public void fail_when_organization_does_not_exist() {
- OrganizationDto organization = db.organizations().insert();
- QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(organization);
-
- expectedException.expect(NotFoundException.class);
- expectedException.expectMessage("No organization with key 'Unknown'");
-
- ws.newRequest()
- .setParam("name", qualityGate.getName())
- .setParam("organization", "Unknown")
- .execute();
- }
-
- @Test
- public void fail_when_quality_gate_belongs_to_another_organization() {
- OrganizationDto organization = db.organizations().insert();
- OrganizationDto otherOrganization = db.organizations().insert();
- QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(otherOrganization);
-
- expectedException.expect(NotFoundException.class);
- expectedException.expectMessage(format("No quality gate has been found for name %s", qualityGate.getName()));
-
- ws.newRequest()
- .setParam("name", qualityGate.getName())
- .setParam("organization", organization.getKey())
- .execute();
- }
-
- @Test
- public void fail_when_quality_gate_belongs_to_another_organization_using_id_parameter() {
- OrganizationDto organization = db.organizations().insert();
- OrganizationDto otherOrganization = db.organizations().insert();
- QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(otherOrganization);
-
- expectedException.expect(NotFoundException.class);
- expectedException.expectMessage(format("No quality gate has been found for id %s in organization %s", qualityGate.getUuid(), organization.getName()));
-
- ws.newRequest()
- .setParam("id", qualityGate.getUuid())
- .setParam("organization", organization.getKey())
.execute();
}
@Test
public void json_example() {
- OrganizationDto organization = db.organizations().insert();
userSession.logIn("admin").addPermission(ADMINISTER_QUALITY_GATES);
- QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(organization, qg -> qg.setName("My Quality Gate"));
- QGateWithOrgDto qualityGate2 = db.qualityGates().insertQualityGate(organization, qg -> qg.setName("My Quality Gate 2"));
- db.qualityGates().setDefaultQualityGate(organization, qualityGate2);
+ QualityGateDto qualityGate = db.qualityGates().insertQualityGate(qg -> qg.setName("My Quality Gate"));
+ QualityGateDto qualityGate2 = db.qualityGates().insertQualityGate(qg -> qg.setName("My Quality Gate 2"));
+ db.qualityGates().setDefaultQualityGate(qualityGate2);
MetricDto blockerViolationsMetric = db.measures().insertMetric(m -> m.setKey("blocker_violations"));
MetricDto criticalViolationsMetric = db.measures().insertMetric(m -> m.setKey("tests"));
db.qualityGates().addCondition(qualityGate, blockerViolationsMetric, c -> c.setOperator("GT").setErrorThreshold("0"));
String response = ws.newRequest()
.setParam("name", qualityGate.getName())
- .setParam("organization", organization.getKey())
.execute()
.getInput();
.extracting(Param::key, Param::isRequired)
.containsExactlyInAnyOrder(
tuple("id", false),
- tuple("name", false),
- tuple("organization", false));
+ tuple("name", false));
}
}
import org.sonar.db.DbSession;
import org.sonar.db.DbTester;
import org.sonar.db.metric.MetricDto;
-import org.sonar.db.organization.OrganizationDto;
-import org.sonar.db.qualitygate.QGateWithOrgDto;
import org.sonar.db.qualitygate.QualityGateConditionDto;
import org.sonar.db.qualitygate.QualityGateDto;
import org.sonar.server.component.TestComponentFinder;
import org.sonar.server.exceptions.BadRequestException;
import org.sonar.server.exceptions.ForbiddenException;
import org.sonar.server.exceptions.NotFoundException;
-import org.sonar.server.organization.TestDefaultOrganizationProvider;
import org.sonar.server.qualitygate.QualityGateConditionsUpdater;
import org.sonar.server.tester.UserSessionRule;
import org.sonar.server.ws.WsActionTester;
import static org.sonar.server.qualitygate.ws.QualityGatesWsParameters.PARAM_ID;
import static org.sonar.server.qualitygate.ws.QualityGatesWsParameters.PARAM_METRIC;
import static org.sonar.server.qualitygate.ws.QualityGatesWsParameters.PARAM_OPERATOR;
-import static org.sonar.server.qualitygate.ws.QualityGatesWsParameters.PARAM_ORGANIZATION;
@RunWith(DataProviderRunner.class)
public class UpdateConditionActionTest {
@Rule
public DbTester db = DbTester.create(System2.INSTANCE);
- private TestDefaultOrganizationProvider defaultOrganizationProvider = TestDefaultOrganizationProvider.from(db);
- private DbClient dbClient = db.getDbClient();
- private DbSession dbSession = db.getSession();
- private UpdateConditionAction underTest = new UpdateConditionAction(dbClient, new QualityGateConditionsUpdater(dbClient),
- new QualityGatesWsSupport(dbClient, userSession, defaultOrganizationProvider, TestComponentFinder.from(db)));
+ private final DbClient dbClient = db.getDbClient();
+ private final DbSession dbSession = db.getSession();
+ private final UpdateConditionAction underTest = new UpdateConditionAction(dbClient, new QualityGateConditionsUpdater(dbClient),
+ new QualityGatesWsSupport(dbClient, userSession, TestComponentFinder.from(db)));
- private WsActionTester ws = new WsActionTester(underTest);
+ private final WsActionTester ws = new WsActionTester(underTest);
@Test
public void update_error_condition() {
- OrganizationDto organization = db.organizations().insert();
userSession.addPermission(ADMINISTER_QUALITY_GATES);
- QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(organization);
+ QualityGateDto qualityGate = db.qualityGates().insertQualityGate();
MetricDto metric = insertMetric();
QualityGateConditionDto condition = db.qualityGates().addCondition(qualityGate, metric,
c -> c.setOperator("GT").setErrorThreshold("80"));
ws.newRequest()
- .setParam(PARAM_ORGANIZATION, organization.getKey())
.setParam(PARAM_ID, condition.getUuid())
.setParam(PARAM_METRIC, metric.getKey())
.setParam(PARAM_OPERATOR, "LT")
assertCondition(qualityGate, metric, "LT", "90");
}
- @Test
- public void default_organization_is_used_when_no_organization_parameter() {
- userSession.addPermission(ADMINISTER_QUALITY_GATES);
- QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(db.getDefaultOrganization());
- MetricDto metric = insertMetric();
- QualityGateConditionDto condition = db.qualityGates().addCondition(qualityGate, metric);
-
- ws.newRequest()
- .setParam(PARAM_ID, condition.getUuid())
- .setParam(PARAM_METRIC, metric.getKey())
- .setParam(PARAM_OPERATOR, "LT")
- .setParam(PARAM_ERROR, "10")
- .execute();
-
- assertCondition(qualityGate, metric, "LT", "10");
- }
-
@Test
public void test_response() {
- OrganizationDto organization = db.organizations().insert();
userSession.addPermission(ADMINISTER_QUALITY_GATES);
- QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(organization);
+ QualityGateDto qualityGate = db.qualityGates().insertQualityGate();
MetricDto metric = insertMetric();
QualityGateConditionDto condition = db.qualityGates().addCondition(qualityGate, metric,
c -> c.setOperator("GT").setErrorThreshold("80"));
CreateConditionResponse response = ws.newRequest()
- .setParam(PARAM_ORGANIZATION, organization.getKey())
.setParam(PARAM_ID, condition.getUuid())
.setParam(PARAM_METRIC, metric.getKey())
.setParam(PARAM_OPERATOR, "LT")
@Test
public void fail_to_update_built_in_quality_gate() {
- OrganizationDto organization = db.organizations().insert();
userSession.addPermission(ADMINISTER_QUALITY_GATES);
- QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(organization, qg -> qg.setBuiltIn(true));
+ QualityGateDto qualityGate = db.qualityGates().insertQualityGate(qg -> qg.setBuiltIn(true));
MetricDto metric = insertMetric();
QualityGateConditionDto condition = db.qualityGates().addCondition(qualityGate, metric);
expectedException.expectMessage(format("Operation forbidden for built-in Quality Gate '%s'", qualityGate.getName()));
ws.newRequest()
- .setParam(PARAM_ORGANIZATION, organization.getKey())
.setParam(PARAM_ID, condition.getUuid())
.setParam(PARAM_METRIC, metric.getKey())
.setParam(PARAM_OPERATOR, "LT")
@Test
public void fail_on_unknown_condition() {
- OrganizationDto organization = db.organizations().insert();
userSession.addPermission(ADMINISTER_QUALITY_GATES);
- QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(organization);
+ QualityGateDto qualityGate = db.qualityGates().insertQualityGate();
MetricDto metric = insertMetric();
db.qualityGates().addCondition(qualityGate, metric);
expectedException.expectMessage("No quality gate condition with uuid '123'");
ws.newRequest()
- .setParam(PARAM_ORGANIZATION, organization.getKey())
.setParam(PARAM_ID, "123")
.setParam(PARAM_METRIC, metric.getKey())
.setParam(PARAM_OPERATOR, "LT")
@Test
public void fail_when_condition_match_unknown_quality_gate() {
- OrganizationDto organization = db.organizations().insert();
userSession.addPermission(ADMINISTER_QUALITY_GATES);
MetricDto metric = insertMetric();
QualityGateConditionDto condition = new QualityGateConditionDto().setUuid("uuid")
expectedException.expectMessage(format("Condition '%s' is linked to an unknown quality gate '%s'", condition.getUuid(), 123L));
ws.newRequest()
- .setParam(PARAM_ORGANIZATION, organization.getKey())
.setParam(PARAM_ID, condition.getUuid())
.setParam(PARAM_METRIC, metric.getKey())
.setParam(PARAM_OPERATOR, "LT")
@Test
public void fail_with_unknown_operator() {
- OrganizationDto organization = db.organizations().insert();
userSession.addPermission(ADMINISTER_QUALITY_GATES);
- QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(organization);
+ QualityGateDto qualityGate = db.qualityGates().insertQualityGate();
MetricDto metric = db.measures().insertMetric(m -> m.setValueType(INT.name()).setHidden(false).setDirection(0));
QualityGateConditionDto condition = db.qualityGates().addCondition(qualityGate, metric,
c -> c.setOperator("LT").setErrorThreshold("80"));
expectedException.expectMessage("Value of parameter 'op' (ABC) must be one of: [LT, GT]");
ws.newRequest()
- .setParam(PARAM_ORGANIZATION, organization.getKey())
.setParam(PARAM_ID, condition.getUuid())
.setParam(PARAM_METRIC, metric.getKey())
.setParam(PARAM_OPERATOR, "ABC")
@Test
@UseDataProvider("update_invalid_operators_and_direction")
public void fail_with_invalid_operators_for_direction(String validOperator, String updateOperator, int direction) {
- OrganizationDto organization = db.organizations().insert();
userSession.addPermission(ADMINISTER_QUALITY_GATES);
- QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(organization);
+ QualityGateDto qualityGate = db.qualityGates().insertQualityGate();
MetricDto metric = db.measures().insertMetric(m -> m.setValueType(INT.name()).setHidden(false).setDirection(direction));
QualityGateConditionDto condition = db.qualityGates().addCondition(qualityGate, metric,
c -> c.setOperator(validOperator).setErrorThreshold("80"));
expectedException.expectMessage(format("Operator %s is not allowed for this metric.", updateOperator));
ws.newRequest()
- .setParam(PARAM_ORGANIZATION, organization.getKey())
.setParam(PARAM_ID, condition.getUuid())
.setParam(PARAM_METRIC, metric.getKey())
.setParam(PARAM_OPERATOR, updateOperator)
@Test
public void throw_ForbiddenException_if_not_gate_administrator() {
userSession.logIn();
- OrganizationDto organization = db.organizations().insert();
- QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(organization);
+ QualityGateDto qualityGate = db.qualityGates().insertQualityGate();
MetricDto metric = insertMetric();
QualityGateConditionDto condition = db.qualityGates().addCondition(qualityGate, metric);
expectedException.expectMessage("Insufficient privileges");
ws.newRequest()
- .setParam(PARAM_ORGANIZATION, organization.getKey())
.setParam(PARAM_ID, condition.getUuid())
.setParam(PARAM_METRIC, metric.getKey())
.setParam(PARAM_OPERATOR, "LT")
tuple("id", true),
tuple("metric", true),
tuple("error", true),
- tuple("op", false),
- tuple("organization", false));
+ tuple("op", false));
}
@DataProvider