import org.sonar.db.permission.UserPermissionDto;
import org.sonar.db.permission.template.PermissionTemplateCharacteristicDto;
import org.sonar.db.permission.template.PermissionTemplateDto;
+import org.sonar.db.qualitygate.QualityGateDto;
import org.sonar.db.qualityprofile.DefaultQProfileDto;
import org.sonar.db.qualityprofile.OrgQProfileDto;
import org.sonar.db.user.GroupDto;
throw new KeyConflictException(format("Organization key '%s' is already used", key));
}
- OrganizationDto organization = insertOrganization(dbSession, newOrganization);
+ QualityGateDto builtInQualityGate = dbClient.qualityGateDao().selectBuiltIn(dbSession);
+ OrganizationDto organization = insertOrganization(dbSession, newOrganization, builtInQualityGate);
insertOrganizationMember(dbSession, organization, userCreator.getId());
+ dbClient.qualityGateDao().associate(dbSession, uuidFactory.create(), organization, builtInQualityGate);
GroupDto ownerGroup = insertOwnersGroup(dbSession, organization);
GroupDto defaultGroup = defaultGroupCreator.create(dbSession, organization.getUuid());
insertDefaultTemplateOnGroups(dbSession, organization, ownerGroup, defaultGroup);
newOrganization.getKey(),
newUser.getLogin());
- OrganizationDto organization = insertOrganization(dbSession, newOrganization,
+ QualityGateDto builtInQualityGate = dbClient.qualityGateDao().selectBuiltIn(dbSession);
+ OrganizationDto organization = insertOrganization(dbSession, newOrganization, builtInQualityGate,
dto -> dto.setGuarded(true).setUserId(newUser.getId()));
insertOrganizationMember(dbSession, organization, newUser.getId());
GroupDto defaultGroup = defaultGroupCreator.create(dbSession, organization.getUuid());
+ dbClient.qualityGateDao().associate(dbSession, uuidFactory.create(), organization, builtInQualityGate);
OrganizationPermission.all()
.forEach(p -> insertUserPermissions(dbSession, newUser, organization, p));
insertPersonalOrgDefaultTemplate(dbSession, organization, defaultGroup);
organizationValidation.checkAvatar(newOrganization.getAvatar());
}
- private OrganizationDto insertOrganization(DbSession dbSession, NewOrganization newOrganization, Consumer<OrganizationDto>... extendCreation) {
+ private OrganizationDto insertOrganization(DbSession dbSession, NewOrganization newOrganization, QualityGateDto builtInQualityGate, Consumer<OrganizationDto>... extendCreation) {
OrganizationDto res = new OrganizationDto()
.setUuid(uuidFactory.create())
.setName(newOrganization.getName())
.setKey(newOrganization.getKey())
.setDescription(newOrganization.getDescription())
.setUrl(newOrganization.getUrl())
- // TODO .setDefaultQualityGateUuid("" + qualityGateFinder.getBuiltInQualityGate(dbSession).getId())
+ .setDefaultQualityGateUuid(builtInQualityGate.getUuid())
.setAvatarUrl(newOrganization.getAvatar());
Arrays.stream(extendCreation).forEach(c -> c.accept(res));
dbClient.organizationDao().insert(dbSession, res, false);
import org.sonar.db.permission.template.PermissionTemplateCharacteristicDto;
import org.sonar.db.permission.template.PermissionTemplateDto;
import org.sonar.db.permission.template.PermissionTemplateGroupDto;
+import org.sonar.db.qualitygate.QualityGateDto;
import org.sonar.db.qualityprofile.QProfileDto;
import org.sonar.db.qualityprofile.RulesProfileDto;
import org.sonar.db.user.GroupDto;
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;
@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();
db.commit();
}
+ @Test
+ public void create_associates_to_built_in_quality_gate() throws OrganizationCreation.KeyConflictException {
+ QualityGateDto builtInQualityGate = insertBuiltInQualityGate();
+ builtInQProfileRepositoryRule.initialize();
+
+ underTest.create(dbSession, someUser, FULL_POPULATED_NEW_ORGANIZATION);
+
+ 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 createForUser_has_no_effect_if_setting_for_feature_is_not_set() {
checkSizeOfTables();
@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);
builtIn1.getQProfileName(), builtIn2.getQProfileName());
}
+ @Test
+ public void createForUser_associates_to_built_in_quality_gate() {
+ QualityGateDto builtInQualityGate = insertBuiltInQualityGate();
+ UserDto user = db.users().insertUser(A_LOGIN);
+ when(organizationValidation.generateKeyFrom(A_LOGIN)).thenReturn(SLUG_OF_A_LOGIN);
+ enableCreatePersonalOrg(true);
+ builtInQProfileRepositoryRule.initialize();
+
+ underTest.createForUser(dbSession, user);
+
+ OrganizationDto organization = dbClient.organizationDao().selectByKey(dbSession, SLUG_OF_A_LOGIN).get();
+ assertThat(dbClient.qualityGateDao().selectDefault(dbSession, organization).getUuid()).isEqualTo(builtInQualityGate.getUuid());
+ }
+
private void enableCreatePersonalOrg(boolean flag) {
settings.setProperty(CorePropertyDefinitions.ORGANIZATIONS_CREATE_PERSONAL_ORG, flag);
}
.containsOnly(user.getLogin());
}
+ private QualityGateDto insertBuiltInQualityGate(){
+ return db.qualityGates().insertBuiltInQualityGate();
+ }
+
}