public QualityProfileDto copyToName(DbSession dbSession, String fromKey, String toName) {
QualityProfileDto from = db.qualityProfileDao().selectOrFailByKey(dbSession, fromKey);
OrganizationDto organization = db.organizationDao().selectByUuid(dbSession, from.getOrganizationUuid())
- .orElseThrow(() -> new IllegalStateException("Organization with UUID [" + from.getOrganizationUuid() + "] does not exist"));
+ .orElseThrow(() -> new IllegalStateException("Organization with UUID [" + from.getOrganizationUuid() + "] does not exist"));
QualityProfileDto to = prepareTarget(dbSession, organization, from, toName);
File backupFile = temp.newFile();
try {
if (toProfile == null) {
// Do not delegate creation to QProfileBackuper because we need to keep
// the parent-child association, if exists.
- toProfile = factory.create(dbSession, organization, toProfileName);
+ toProfile = factory.checkAndCreate(dbSession, organization, toProfileName);
toProfile.setParentKee(from.getParentKee());
db.qualityProfileDao().update(dbSession, toProfile);
dbSession.commit();
private void verify(QualityProfileDto fromProfile, QProfileName toProfileName) {
if (!StringUtils.equals(fromProfile.getLanguage(), toProfileName.getLanguage())) {
throw new IllegalArgumentException(String.format(
- "Source and target profiles do not have the same language: %s and %s",
- fromProfile.getLanguage(), toProfileName.getLanguage()));
+ "Source and target profiles do not have the same language: %s and %s",
+ fromProfile.getLanguage(), toProfileName.getLanguage()));
}
if (fromProfile.getName().equals(toProfileName.getName())) {
throw new IllegalArgumentException(String.format("Source and target profiles are equal: %s",
- fromProfile.getName()));
+ fromProfile.getName()));
}
}
import java.util.Objects;
import java.util.Set;
import javax.annotation.CheckForNull;
+import javax.annotation.Nullable;
import org.apache.commons.lang.RandomStringUtils;
import org.apache.commons.lang.StringUtils;
import org.sonar.core.util.Slug;
// ------------- CREATION
QualityProfileDto getOrCreate(DbSession dbSession, OrganizationDto organization, QProfileName name) {
- Objects.requireNonNull(organization, "Organization is required, when creating a quality profile.");
+ requireNonNull(organization);
QualityProfileDto profile = db.qualityProfileDao().selectByNameAndLanguage(organization, name.getName(), name.getLanguage(), dbSession);
if (profile == null) {
profile = doCreate(dbSession, organization, name);
return profile;
}
- public QualityProfileDto create(DbSession dbSession, OrganizationDto organization, QProfileName name) {
- Objects.requireNonNull(organization, "Organization is required, when creating a quality profile.");
+ /**
+ * Create the quality profile in DB with the specified name.
+ *
+ * @throws BadRequestException if a quality profile with the specified name already exists
+ */
+ public QualityProfileDto checkAndCreate(DbSession dbSession, OrganizationDto organization, QProfileName name) {
+ requireNonNull(organization);
QualityProfileDto dto = db.qualityProfileDao().selectByNameAndLanguage(organization, name.getName(), name.getLanguage(), dbSession);
checkRequest(dto == null, "Quality profile already exists: %s", name);
return doCreate(dbSession, organization, name);
}
+ /**
+ * Create the quality profile in DB with the specified name.
+ *
+ * A DB error will be thrown if the quality profile already exists.
+ */
+ public QualityProfileDto create(DbSession dbSession, OrganizationDto organization, QProfileName name) {
+ return doCreate(dbSession, requireNonNull(organization), name);
+ }
+
+ private static OrganizationDto requireNonNull(@Nullable OrganizationDto organization) {
+ Objects.requireNonNull(organization, "Organization is required, when creating a quality profile.");
+ return organization;
+ }
+
private QualityProfileDto doCreate(DbSession dbSession, OrganizationDto organization, QProfileName name) {
if (StringUtils.isEmpty(name.getName())) {
throw BadRequestException.create("quality_profiles.profile_name_cant_be_blank");
private CreateWsResponse doHandle(DbSession dbSession, CreateRequest createRequest, Request request, OrganizationDto organization) {
QProfileResult result = new QProfileResult();
- QualityProfileDto profile = profileFactory.create(dbSession, organization,
+ QualityProfileDto profile = profileFactory.checkAndCreate(dbSession, organization,
QProfileName.createFor(createRequest.getLanguage(), createRequest.getProfileName()));
result.setProfile(profile);
for (ProfileImporter importer : importers) {
dbSession.close();
}
+ @Test
+ public void checkAndCreate() {
+ String uuid = organization.getUuid();
+
+ QualityProfileDto writtenDto = factory.checkAndCreate(dbSession, organization, new QProfileName("xoo", "P1"));
+ dbSession.commit();
+ dbSession.clearCache();
+ assertThat(writtenDto.getOrganizationUuid()).isEqualTo(uuid);
+ assertThat(writtenDto.getKey()).startsWith("xoo-p1-");
+ assertThat(writtenDto.getName()).isEqualTo("P1");
+ assertThat(writtenDto.getLanguage()).isEqualTo("xoo");
+ assertThat(writtenDto.getId()).isNotNull();
+
+ // reload the dto
+ QualityProfileDto readDto = db.qualityProfileDao().selectByNameAndLanguage("P1", "xoo", dbSession);
+ assertThat(readDto.getOrganizationUuid()).isEqualTo(uuid);
+ assertThat(readDto.getName()).isEqualTo("P1");
+ assertThat(readDto.getKey()).startsWith("xoo-p1");
+ assertThat(readDto.getLanguage()).isEqualTo("xoo");
+ assertThat(readDto.getId()).isNotNull();
+ assertThat(readDto.getParentKee()).isNull();
+
+ assertThat(db.qualityProfileDao().selectAll(dbSession, organization)).hasSize(1);
+ }
+
@Test
public void create() {
String uuid = organization.getUuid();
}
@Test
- public void fail_to_create_if_name_null() {
+ public void checkAndCreate_throws_BadRequestException_if_name_null() {
QProfileName name = new QProfileName("xoo", null);
expectBadRequestException("quality_profiles.profile_name_cant_be_blank");
- factory.create(dbSession, organization, name);
+ factory.checkAndCreate(dbSession, organization, name);
}
@Test
- public void fail_to_create_if_name_empty() {
+ public void checkAndCreate_throws_BadRequestException_if_name_empty() {
QProfileName name = new QProfileName("xoo", "");
expectBadRequestException("quality_profiles.profile_name_cant_be_blank");
- factory.create(dbSession, organization, name);
+ factory.checkAndCreate(dbSession, organization, name);
}
@Test
- public void fail_to_create_if_already_exists() {
+ public void checkAndCreate_throws_BadRequestException_if_already_exists() {
QProfileName name = new QProfileName("xoo", "P1");
- factory.create(dbSession, organization, name);
+ factory.checkAndCreate(dbSession, organization, name);
dbSession.commit();
dbSession.clearCache();
expectBadRequestException("Quality profile already exists: {lang=xoo, name=P1}");
+ factory.checkAndCreate(dbSession, organization, name);
+ }
+
+ @Test
+ public void create_throws_BadRequestException_if_name_null() {
+ QProfileName name = new QProfileName("xoo", null);
+
+ expectBadRequestException("quality_profiles.profile_name_cant_be_blank");
+
+ factory.create(dbSession, organization, name);
+ }
+
+ @Test
+ public void create_throws_BadRequestException_if_name_empty() {
+ QProfileName name = new QProfileName("xoo", "");
+
+ expectBadRequestException("quality_profiles.profile_name_cant_be_blank");
+
+ factory.create(dbSession, organization, name);
+ }
+
+ @Test
+ public void create_does_not_fail_if_already_exists() {
+ QProfileName name = new QProfileName("xoo", "P1");
factory.create(dbSession, organization, name);
+ dbSession.commit();
+ dbSession.clearCache();
+
+ assertThat(factory.create(dbSession, organization, name)).isNotNull();
}
@Test