import org.sonar.db.user.UserMembershipQuery;
import org.sonar.server.es.EsTester;
import org.sonar.server.es.SearchOptions;
+import org.sonar.server.qualitygate.QualityGateFinder;
import org.sonar.server.qualityprofile.BuiltInQProfile;
import org.sonar.server.qualityprofile.BuiltInQProfileRepositoryRule;
import org.sonar.server.qualityprofile.QProfileName;
private UserIndexer userIndexer = new UserIndexer(dbClient, es.client());
private UserIndex userIndex = new UserIndex(es.client(), system2);
private DefaultGroupCreator defaultGroupCreator = new DefaultGroupCreatorImpl(dbClient);
+ private QualityGateFinder qualityGateFinder = new QualityGateFinder(dbClient);
private OrganizationCreationImpl underTest = new OrganizationCreationImpl(dbClient, system2, uuidFactory, organizationValidation, settings.asConfig(), userIndexer,
builtInQProfileRepositoryRule, defaultGroupCreator);
@Test
public void create_fails_with_ISE_if_BuiltInQProfileRepository_has_not_been_initialized() throws OrganizationCreation.KeyConflictException {
- insertBuiltInQualityGate();
expectedException.expect(IllegalStateException.class);
expectedException.expectMessage("initialize must be called first");
@Test
public void create_creates_unguarded_organization_with_properties_from_NewOrganization_arg() throws OrganizationCreation.KeyConflictException {
- insertBuiltInQualityGate();
builtInQProfileRepositoryRule.initialize();
underTest.create(dbSession, someUser, FULL_POPULATED_NEW_ORGANIZATION);
@Test
public void create_creates_owners_group_with_all_permissions_for_new_organization_and_add_current_user_to_it() throws OrganizationCreation.KeyConflictException {
- insertBuiltInQualityGate();
UserDto user = db.users().insertUser();
builtInQProfileRepositoryRule.initialize();
@Test
public void create_creates_members_group_and_add_current_user_to_it() throws OrganizationCreation.KeyConflictException {
- insertBuiltInQualityGate();
UserDto user = db.users().insertUser();
builtInQProfileRepositoryRule.initialize();
@Test
public void create_does_not_require_description_url_and_avatar_to_be_non_null() throws OrganizationCreation.KeyConflictException {
- insertBuiltInQualityGate();
builtInQProfileRepositoryRule.initialize();
underTest.create(dbSession, someUser, newOrganizationBuilder()
@Test
public void create_creates_default_template_for_new_organization() throws OrganizationCreation.KeyConflictException {
- insertBuiltInQualityGate();
builtInQProfileRepositoryRule.initialize();
underTest.create(dbSession, someUser, FULL_POPULATED_NEW_ORGANIZATION);
@Test
public void create_add_current_user_as_member_of_organization() throws OrganizationCreation.KeyConflictException {
- insertBuiltInQualityGate();
UserDto user = db.users().insertUser();
builtInQProfileRepositoryRule.initialize();
userIndexer.commitAndIndex(db.getSession(), someUser);
@Test
public void create_associates_to_built_in_quality_profiles() throws OrganizationCreation.KeyConflictException {
- insertBuiltInQualityGate();
BuiltInQProfile builtIn1 = builtInQProfileRepositoryRule.add(newLanguage("foo"), "qp1", true);
BuiltInQProfile builtIn2 = builtInQProfileRepositoryRule.add(newLanguage("foo"), "qp2");
builtInQProfileRepositoryRule.initialize();
@Test
public void create_associates_to_built_in_quality_gate() throws OrganizationCreation.KeyConflictException {
- QualityGateDto builtInQualityGate = insertBuiltInQualityGate();
+ QualityGateDto builtInQualityGate = qualityGateFinder.getBuiltInQualityGate(dbSession);
builtInQProfileRepositoryRule.initialize();
underTest.create(dbSession, someUser, FULL_POPULATED_NEW_ORGANIZATION);
@Test
public void createForUser_creates_guarded_organization_with_key_name_and_description_generated_from_user_login_and_name_and_associated_to_user() {
- insertBuiltInQualityGate();
UserDto user = db.users().insertUser(A_LOGIN);
when(organizationValidation.generateKeyFrom(A_LOGIN)).thenReturn(SLUG_OF_A_LOGIN);
enableCreatePersonalOrg(true);
@Test
public void createForUser_gives_all_permissions_for_new_organization_to_current_user() throws OrganizationCreation.KeyConflictException {
- insertBuiltInQualityGate();
UserDto user = db.users().insertUser(dto -> dto.setLogin(A_LOGIN).setName(A_NAME));
when(organizationValidation.generateKeyFrom(A_LOGIN)).thenReturn(SLUG_OF_A_LOGIN);
enableCreatePersonalOrg(true);
@Test
public void createForUser_creates_members_group_and_add_current_user_to_it() throws OrganizationCreation.KeyConflictException {
- insertBuiltInQualityGate();
UserDto user = db.users().insertUser(dto -> dto.setLogin(A_LOGIN).setName(A_NAME));
when(organizationValidation.generateKeyFrom(A_LOGIN)).thenReturn(SLUG_OF_A_LOGIN);
enableCreatePersonalOrg(true);
@Test
public void createForUser_creates_default_template_for_new_organization() throws OrganizationCreation.KeyConflictException {
- insertBuiltInQualityGate();
UserDto user = db.users().insertUser(dto -> dto.setLogin(A_LOGIN).setName(A_NAME));
when(organizationValidation.generateKeyFrom(A_LOGIN)).thenReturn(SLUG_OF_A_LOGIN);
enableCreatePersonalOrg(true);
@Test
public void createForUser_add_current_user_as_member_of_organization() throws OrganizationCreation.KeyConflictException {
- insertBuiltInQualityGate();
UserDto user = db.users().insertUser(dto -> dto.setLogin(A_LOGIN).setName(A_NAME));
when(organizationValidation.generateKeyFrom(A_LOGIN)).thenReturn(SLUG_OF_A_LOGIN);
enableCreatePersonalOrg(true);
@Test
public void createForUser_does_not_fail_if_name_is_too_long_for_an_organization_name() {
- insertBuiltInQualityGate();
String nameTooLong = STRING_64_CHARS + "b";
UserDto user = db.users().insertUser(dto -> dto.setName(nameTooLong).setLogin(A_LOGIN));
when(organizationValidation.generateKeyFrom(A_LOGIN)).thenReturn(SLUG_OF_A_LOGIN);
@Test
public void createForUser_does_not_fail_if_name_is_empty_and_login_is_too_long_for_an_organization_name() {
- insertBuiltInQualityGate();
String login = STRING_64_CHARS + "b";
UserDto user = db.users().insertUser(dto -> dto.setName("").setLogin(login));
when(organizationValidation.generateKeyFrom(login)).thenReturn(SLUG_OF_A_LOGIN);
@Test
public void createForUser_does_not_fail_if_name_is_null_and_login_is_too_long_for_an_organization_name() {
- insertBuiltInQualityGate();
String login = STRING_64_CHARS + "b";
UserDto user = db.users().insertUser(dto -> dto.setName(null).setLogin(login));
when(organizationValidation.generateKeyFrom(login)).thenReturn(SLUG_OF_A_LOGIN);
@Test
public void createForUser_associates_to_built_in_quality_profiles() throws OrganizationCreation.KeyConflictException {
- insertBuiltInQualityGate();
UserDto user = db.users().insertUser(A_LOGIN);
when(organizationValidation.generateKeyFrom(A_LOGIN)).thenReturn(SLUG_OF_A_LOGIN);
enableCreatePersonalOrg(true);
@Test
public void createForUser_associates_to_built_in_quality_gate() {
- QualityGateDto builtInQualityGate = insertBuiltInQualityGate();
+ QualityGateDto builtInQualityGate = qualityGateFinder.getBuiltInQualityGate(dbSession);
UserDto user = db.users().insertUser(A_LOGIN);
when(organizationValidation.generateKeyFrom(A_LOGIN)).thenReturn(SLUG_OF_A_LOGIN);
enableCreatePersonalOrg(true);
.containsOnly(user.getLogin());
}
- private QualityGateDto insertBuiltInQualityGate(){
- return db.qualityGates().insertBuiltInQualityGate();
- }
}
private RegisterQualityGates underTest = new RegisterQualityGates(dbClient, qualityGateUpdater, qualityGateConditionsUpdater, qualityGateFinder,
UuidFactoryFast.getInstance(), System2.INSTANCE);
+ private QualityGateDto builtInQG;
@Before
public void setup() {
insertMetrics();
+ builtInQG = qualityGateFinder.getBuiltInQualityGate(dbSession);
}
@After
@Test
public void register_default_gate() {
+ dbClient.qualityGateDao().delete(builtInQG, dbSession);
+ dbSession.commit();
+
underTest.start();
verifyCorrectBuiltInQualityGate();
@Test
public void upgrade_empty_quality_gate() {
- QualityGateDto builtin = new QualityGateDto().setName(BUILT_IN_NAME).setBuiltIn(true).setUuid(Uuids.createFast());
- qualityGateDao.insert(dbSession, builtin);
dbSession.commit();
underTest.start();
@Test
public void upgrade_should_remove_deleted_condition() {
- QualityGateDto builtin = new QualityGateDto().setName(BUILT_IN_NAME).setBuiltIn(true).setUuid(Uuids.createFast());
- qualityGateDao.insert(dbSession, builtin);
-
- createBuiltInConditions(builtin);
+ createBuiltInConditions(builtInQG);
// Add another condition
- qualityGateConditionsUpdater.createCondition(dbSession, builtin,
+ qualityGateConditionsUpdater.createCondition(dbSession, builtInQG,
NEW_SECURITY_REMEDIATION_EFFORT_KEY, OPERATOR_GREATER_THAN, null, "5", LEAK_PERIOD);
dbSession.commit();
@Test
public void upgrade_should_add_missing_condition() {
- QualityGateDto builtin = new QualityGateDto().setName(BUILT_IN_NAME).setBuiltIn(true).setUuid(Uuids.createFast());
- qualityGateDao.insert(dbSession, builtin);
-
- List<QualityGateConditionDto> builtInConditions = createBuiltInConditions(builtin);
+ List<QualityGateConditionDto> builtInConditions = createBuiltInConditions(builtInQG);
// Remove a condition
QualityGateConditionDto conditionToBeDeleted = builtInConditions.get(new Random().nextInt(builtInConditions.size()));
@Test
public void should_set_SonarWay_as_builtin_when_not_set() {
+ dbClient.qualityGateDao().delete(builtInQG, dbSession);
QualityGateDto builtin = new QualityGateDto().setName(BUILT_IN_NAME).setBuiltIn(false).setUuid(Uuids.createFast());
qualityGateDao.insert(dbSession, builtin);
-
createBuiltInConditions(builtin);
dbSession.commit();
@Test
public void should_not_update_builtin_quality_gate_if_already_uptodate() {
- QualityGateDto builtin = new QualityGateDto().setName(BUILT_IN_NAME).setBuiltIn(true).setUuid(Uuids.createFast());
- qualityGateDao.insert(dbSession, builtin);
-
- createBuiltInConditions(builtin);
+ createBuiltInConditions(builtInQG);
dbSession.commit();
underTest.start();
@Test
public void ensure_only_one_built_in_quality_gate() {
+ dbClient.qualityGateDao().delete(builtInQG, dbSession);
String qualityGateName = "IncorrectQualityGate";
QualityGateDto builtin = new QualityGateDto().setName(qualityGateName).setBuiltIn(true).setUuid(Uuids.createFast());
qualityGateDao.insert(dbSession, builtin);
@Test
public void ensure_only_that_builtin_is_set_as_default_when_no_default_quality_gate() {
- QualityGateDto builtin = new QualityGateDto().setName(BUILT_IN_NAME).setBuiltIn(true).setUuid(Uuids.createFast());
- qualityGateDao.insert(dbSession, builtin);
- dbSession.commit();
underTest.start();
assertThat(qualityGateFinder.getDefault(dbSession)).isPresent();
- assertThat(qualityGateFinder.getDefault(dbSession).get().getId()).isEqualTo(builtin.getId());
+ assertThat(qualityGateFinder.getDefault(dbSession).get().getId()).isEqualTo(builtInQG.getId());
assertThat(
logTester.logs(LoggerLevel.INFO).contains("Built-in quality gate [Sonar way] has been set as default")).isTrue();
@Test
public void builtin_quality_gate_with_incorrect_metricId_should_not_throw_an_exception() {
- QualityGateDto builtin = new QualityGateDto().setName(BUILT_IN_NAME).setBuiltIn(true).setUuid(Uuids.createFast());
- qualityGateDao.insert(dbSession, builtin);
QualityGateConditionDto conditionDto = new QualityGateConditionDto()
.setMetricId(-1) // This Id does not exist
.setOperator(OPERATOR_GREATER_THAN)
tuple(newDuplication.getId().longValue(), OPERATOR_GREATER_THAN, null, "3", 1));
}
- private List<QualityGateConditionDto> createBuiltInConditions(QualityGateDto builtin) {
+ private List<QualityGateConditionDto> createBuiltInConditions(QualityGateDto qg) {
List<QualityGateConditionDto> conditions = new ArrayList<>();
- conditions.add(qualityGateConditionsUpdater.createCondition(dbSession, builtin,
+ conditions.add(qualityGateConditionsUpdater.createCondition(dbSession, qg,
NEW_SECURITY_RATING_KEY, OPERATOR_GREATER_THAN, null, "1", LEAK_PERIOD));
- conditions.add(qualityGateConditionsUpdater.createCondition(dbSession, builtin,
+ conditions.add(qualityGateConditionsUpdater.createCondition(dbSession, qg,
NEW_RELIABILITY_RATING_KEY, OPERATOR_GREATER_THAN, null, "1", LEAK_PERIOD));
- conditions.add(qualityGateConditionsUpdater.createCondition(dbSession, builtin,
+ conditions.add(qualityGateConditionsUpdater.createCondition(dbSession, qg,
NEW_MAINTAINABILITY_RATING_KEY, OPERATOR_GREATER_THAN, null, "1", LEAK_PERIOD));
- conditions.add(qualityGateConditionsUpdater.createCondition(dbSession, builtin,
+ conditions.add(qualityGateConditionsUpdater.createCondition(dbSession, qg,
NEW_COVERAGE_KEY, OPERATOR_LESS_THAN, null, "80", LEAK_PERIOD));
- conditions.add(qualityGateConditionsUpdater.createCondition(dbSession, builtin,
+ conditions.add(qualityGateConditionsUpdater.createCondition(dbSession, qg,
NEW_DUPLICATED_LINES_DENSITY_KEY, OPERATOR_GREATER_THAN, null, "3", LEAK_PERIOD));
return conditions;