@@ -61,17 +61,6 @@ public class QProfileBackuper { | |||
this.db = db; | |||
} | |||
/** | |||
* @deprecated use {@link #backup(DbSession, QualityProfileDto, Writer)} instead | |||
*/ | |||
@Deprecated | |||
public void backup(String key, Writer writer) { | |||
try (DbSession dbSession = db.openSession(false)) { | |||
QualityProfileDto profile = db.qualityProfileDao().selectOrFailByKey(dbSession, key); | |||
backup(dbSession, profile, writer); | |||
} | |||
} | |||
public void backup(DbSession dbSession, QualityProfileDto profileDto, Writer writer) { | |||
List<ActiveRuleDto> activeRules = db.activeRuleDao().selectByProfileKey(dbSession, profileDto.getKey()); | |||
activeRules.sort(BackupActiveRuleComparator.INSTANCE); | |||
@@ -109,7 +98,7 @@ public class QProfileBackuper { | |||
* @param toProfileName the target profile. If <code>null</code>, then use the | |||
* lang and name declared in the backup | |||
*/ | |||
public BulkChangeResult restore(Reader reader, @Nullable QProfileName toProfileName) { | |||
public BulkChangeResult restore(DbSession dbSession, Reader reader, @Nullable QProfileName toProfileName) { | |||
try { | |||
String profileLang = null; | |||
String profileName = null; | |||
@@ -136,7 +125,7 @@ public class QProfileBackuper { | |||
} | |||
QProfileName target = (QProfileName) ObjectUtils.defaultIfNull(toProfileName, new QProfileName(profileLang, profileName)); | |||
return reset.reset(target, ruleActivations); | |||
return reset.reset(dbSession, target, ruleActivations); | |||
} catch (XMLStreamException e) { | |||
throw new IllegalStateException("Fail to restore Quality profile backup", e); | |||
} |
@@ -25,9 +25,7 @@ import java.io.InputStreamReader; | |||
import java.io.OutputStreamWriter; | |||
import java.io.Reader; | |||
import java.io.Writer; | |||
import java.nio.charset.StandardCharsets; | |||
import org.apache.commons.io.FileUtils; | |||
import org.apache.commons.io.IOUtils; | |||
import org.apache.commons.lang.StringUtils; | |||
import org.sonar.api.server.ServerSide; | |||
import org.sonar.api.utils.TempFolder; | |||
@@ -36,6 +34,8 @@ import org.sonar.db.DbSession; | |||
import org.sonar.db.qualityprofile.QualityProfileDto; | |||
import org.sonar.server.qualityprofile.ws.QProfileWsSupport; | |||
import static java.nio.charset.StandardCharsets.UTF_8; | |||
@ServerSide | |||
public class QProfileCopier { | |||
@@ -53,38 +53,32 @@ public class QProfileCopier { | |||
this.qProfileWsSupport = qProfileWsSupport; | |||
} | |||
public QualityProfileDto copyToName(String fromProfileKey, String toName) { | |||
QualityProfileDto to = prepareTarget(fromProfileKey, toName); | |||
public QualityProfileDto copyToName(DbSession dbSession, String fromProfileKey, String toName) { | |||
QualityProfileDto from = db.qualityProfileDao().selectOrFailByKey(dbSession, fromProfileKey); | |||
QualityProfileDto to = prepareTarget(dbSession, from, toName); | |||
File backupFile = temp.newFile(); | |||
try { | |||
backup(fromProfileKey, backupFile); | |||
restore(backupFile, QProfileName.createFor(to.getLanguage(), to.getName())); | |||
backup(dbSession, from, backupFile); | |||
restore(dbSession, backupFile, QProfileName.createFor(to.getLanguage(), to.getName())); | |||
return to; | |||
} finally { | |||
org.sonar.core.util.FileUtils.deleteQuietly(backupFile); | |||
} | |||
} | |||
private QualityProfileDto prepareTarget(String fromProfileKey, String toName) { | |||
DbSession dbSession = db.openSession(false); | |||
try { | |||
QualityProfileDto fromProfile = db.qualityProfileDao().selectOrFailByKey(dbSession, fromProfileKey); | |||
QProfileName toProfileName = new QProfileName(fromProfile.getLanguage(), toName); | |||
verify(fromProfile, toProfileName); | |||
QualityProfileDto toProfile = db.qualityProfileDao().selectByNameAndLanguage(toProfileName.getName(), toProfileName.getLanguage(), dbSession); | |||
if (toProfile == null) { | |||
// Do not delegate creation to QProfileBackuper because we need to keep | |||
// the parent-child association, if exists. | |||
toProfile = factory.create(dbSession, qProfileWsSupport.getDefaultOrganization(dbSession), toProfileName); | |||
toProfile.setParentKee(fromProfile.getParentKee()); | |||
db.qualityProfileDao().update(dbSession, toProfile); | |||
dbSession.commit(); | |||
} | |||
return toProfile; | |||
} finally { | |||
dbSession.close(); | |||
private QualityProfileDto prepareTarget(DbSession dbSession, QualityProfileDto from, String toName) { | |||
QProfileName toProfileName = new QProfileName(from.getLanguage(), toName); | |||
verify(from, toProfileName); | |||
QualityProfileDto toProfile = db.qualityProfileDao().selectByNameAndLanguage(toProfileName.getName(), toProfileName.getLanguage(), dbSession); | |||
if (toProfile == null) { | |||
// Do not delegate creation to QProfileBackuper because we need to keep | |||
// the parent-child association, if exists. | |||
toProfile = factory.create(dbSession, qProfileWsSupport.getDefaultOrganization(dbSession), toProfileName); | |||
toProfile.setParentKee(from.getParentKee()); | |||
db.qualityProfileDao().update(dbSession, toProfile); | |||
dbSession.commit(); | |||
} | |||
return toProfile; | |||
} | |||
private void verify(QualityProfileDto fromProfile, QProfileName toProfileName) { | |||
@@ -99,27 +93,19 @@ public class QProfileCopier { | |||
} | |||
} | |||
private void backup(String profileKey, File backupFile) { | |||
Writer writer = null; | |||
try { | |||
writer = new OutputStreamWriter(FileUtils.openOutputStream(backupFile), StandardCharsets.UTF_8); | |||
backuper.backup(profileKey, writer); | |||
private void backup(DbSession dbSession, QualityProfileDto profile, File backupFile) { | |||
try (Writer writer = new OutputStreamWriter(FileUtils.openOutputStream(backupFile), UTF_8)) { | |||
backuper.backup(dbSession, profile, writer); | |||
} catch (IOException e) { | |||
throw new IllegalStateException("Fail to open temporary backup file: " + backupFile, e); | |||
} finally { | |||
IOUtils.closeQuietly(writer); | |||
} | |||
} | |||
private void restore(File backupFile, QProfileName profileName) { | |||
Reader reader = null; | |||
try { | |||
reader = new InputStreamReader(FileUtils.openInputStream(backupFile), StandardCharsets.UTF_8); | |||
backuper.restore(reader, profileName); | |||
private void restore(DbSession dbSession, File backupFile, QProfileName profileName) { | |||
try (Reader reader = new InputStreamReader(FileUtils.openInputStream(backupFile), UTF_8)) { | |||
backuper.restore(dbSession, reader, profileName); | |||
} catch (IOException e) { | |||
throw new IllegalStateException("Fail to create temporary backup file: " + backupFile, e); | |||
} finally { | |||
IOUtils.closeQuietly(reader); | |||
} | |||
} | |||
} |
@@ -77,48 +77,38 @@ public class QProfileReset { | |||
* Reset built-in profiles for the given language. Missing profiles are created and | |||
* existing ones are updated. | |||
*/ | |||
public void resetLanguage(String language) { | |||
DbSession dbSession = db.openSession(false); | |||
try { | |||
ListMultimap<QProfileName, RulesProfile> profilesByName = loadDefinitionsGroupedByName(language); | |||
for (Map.Entry<QProfileName, Collection<RulesProfile>> entry : profilesByName.asMap().entrySet()) { | |||
QProfileName profileName = entry.getKey(); | |||
QualityProfileDto profile = factory.getOrCreate(dbSession, qProfileWsSupport.getDefaultOrganization(dbSession), profileName); | |||
List<RuleActivation> activations = Lists.newArrayList(); | |||
for (RulesProfile def : entry.getValue()) { | |||
for (ActiveRule activeRule : def.getActiveRules()) { | |||
RuleActivation activation = new RuleActivation(RuleKey.of(activeRule.getRepositoryKey(), activeRule.getRuleKey())); | |||
activation.setSeverity(activeRule.getSeverity().name()); | |||
if (!activeRule.getActiveRuleParams().isEmpty()) { | |||
for (ActiveRuleParam param : activeRule.getActiveRuleParams()) { | |||
activation.setParameter(param.getParamKey(), param.getValue()); | |||
} | |||
} else { | |||
for (RuleParamDto param : db.ruleDao().selectRuleParamsByRuleKey(dbSession, activeRule.getRule().ruleKey())) { | |||
activation.setParameter(param.getName(), param.getDefaultValue()); | |||
} | |||
public void resetLanguage(DbSession dbSession, String language) { | |||
ListMultimap<QProfileName, RulesProfile> profilesByName = loadDefinitionsGroupedByName(language); | |||
for (Map.Entry<QProfileName, Collection<RulesProfile>> entry : profilesByName.asMap().entrySet()) { | |||
QProfileName profileName = entry.getKey(); | |||
QualityProfileDto profile = factory.getOrCreate(dbSession, qProfileWsSupport.getDefaultOrganization(dbSession), profileName); | |||
List<RuleActivation> activations = Lists.newArrayList(); | |||
for (RulesProfile def : entry.getValue()) { | |||
for (ActiveRule activeRule : def.getActiveRules()) { | |||
RuleActivation activation = new RuleActivation(RuleKey.of(activeRule.getRepositoryKey(), activeRule.getRuleKey())); | |||
activation.setSeverity(activeRule.getSeverity().name()); | |||
if (!activeRule.getActiveRuleParams().isEmpty()) { | |||
for (ActiveRuleParam param : activeRule.getActiveRuleParams()) { | |||
activation.setParameter(param.getParamKey(), param.getValue()); | |||
} | |||
} else { | |||
for (RuleParamDto param : db.ruleDao().selectRuleParamsByRuleKey(dbSession, activeRule.getRule().ruleKey())) { | |||
activation.setParameter(param.getName(), param.getDefaultValue()); | |||
} | |||
activations.add(activation); | |||
} | |||
activations.add(activation); | |||
} | |||
doReset(dbSession, profile, activations); | |||
} | |||
} finally { | |||
dbSession.close(); | |||
doReset(dbSession, profile, activations); | |||
} | |||
} | |||
/** | |||
* Reset the profile, which is created if it does not exist | |||
*/ | |||
BulkChangeResult reset(QProfileName profileName, Collection<RuleActivation> activations) { | |||
DbSession dbSession = db.openSession(false); | |||
try { | |||
QualityProfileDto profile = factory.getOrCreate(dbSession, qProfileWsSupport.getDefaultOrganization(dbSession), profileName); | |||
return doReset(dbSession, profile, activations); | |||
} finally { | |||
dbSession.close(); | |||
} | |||
BulkChangeResult reset(DbSession dbSession, QProfileName profileName, Collection<RuleActivation> activations) { | |||
QualityProfileDto profile = factory.getOrCreate(dbSession, qProfileWsSupport.getDefaultOrganization(dbSession), profileName); | |||
return doReset(dbSession, profile, activations); | |||
} | |||
/** |
@@ -25,6 +25,8 @@ import org.sonar.api.server.ws.Request; | |||
import org.sonar.api.server.ws.Response; | |||
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.qualityprofile.QualityProfileDto; | |||
import org.sonar.server.qualityprofile.QProfileCopier; | |||
@@ -33,11 +35,13 @@ public class CopyAction implements QProfileWsAction { | |||
private static final String PARAM_PROFILE_NAME = "toName"; | |||
private static final String PARAM_PROFILE_KEY = "fromKey"; | |||
private final DbClient dbClient; | |||
private final QProfileCopier profileCopier; | |||
private final Languages languages; | |||
private final QProfileWsSupport qProfileWsSupport; | |||
public CopyAction(QProfileCopier profileCopier, Languages languages, QProfileWsSupport qProfileWsSupport) { | |||
public CopyAction(DbClient dbClient, QProfileCopier profileCopier, Languages languages, QProfileWsSupport qProfileWsSupport) { | |||
this.dbClient = dbClient; | |||
this.profileCopier = profileCopier; | |||
this.languages = languages; | |||
this.qProfileWsSupport = qProfileWsSupport; | |||
@@ -69,20 +73,22 @@ public class CopyAction implements QProfileWsAction { | |||
String newName = request.mandatoryParam(PARAM_PROFILE_NAME); | |||
String profileKey = request.mandatoryParam(PARAM_PROFILE_KEY); | |||
QualityProfileDto copiedProfile = profileCopier.copyToName(profileKey, newName); | |||
try (DbSession dbSession = dbClient.openSession(false)) { | |||
QualityProfileDto copiedProfile = profileCopier.copyToName(dbSession, profileKey, newName); | |||
String languageKey = copiedProfile.getLanguage(); | |||
Language language = languages.get(copiedProfile.getLanguage()); | |||
String parentKey = copiedProfile.getParentKee(); | |||
response.newJsonWriter() | |||
.beginObject() | |||
.prop("key", copiedProfile.getKey()) | |||
.prop("name", copiedProfile.getName()) | |||
.prop("language", languageKey) | |||
.prop("languageName", language == null ? null : language.getName()) | |||
.prop("isDefault", copiedProfile.isDefault()) | |||
.prop("isInherited", parentKey != null) | |||
.prop("parentKey", parentKey) | |||
.endObject().close(); | |||
String languageKey = copiedProfile.getLanguage(); | |||
Language language = languages.get(copiedProfile.getLanguage()); | |||
String parentKey = copiedProfile.getParentKee(); | |||
response.newJsonWriter() | |||
.beginObject() | |||
.prop("key", copiedProfile.getKey()) | |||
.prop("name", copiedProfile.getName()) | |||
.prop("language", languageKey) | |||
.prop("languageName", language == null ? null : language.getName()) | |||
.prop("isDefault", copiedProfile.isDefault()) | |||
.prop("isInherited", parentKey != null) | |||
.prop("parentKey", parentKey) | |||
.endObject().close(); | |||
} | |||
} | |||
} |
@@ -29,6 +29,8 @@ import org.sonar.api.server.ws.Request; | |||
import org.sonar.api.server.ws.Response; | |||
import org.sonar.api.server.ws.WebService; | |||
import org.sonar.api.utils.text.JsonWriter; | |||
import org.sonar.db.DbClient; | |||
import org.sonar.db.DbSession; | |||
import org.sonar.db.qualityprofile.QualityProfileDto; | |||
import org.sonar.server.qualityprofile.BulkChangeResult; | |||
import org.sonar.server.qualityprofile.QProfileBackuper; | |||
@@ -44,11 +46,14 @@ import static com.google.common.base.Preconditions.checkArgument; | |||
public class OldRestoreAction implements WsAction { | |||
private static final String PARAM_BACKUP = "backup"; | |||
private final DbClient dbClient; | |||
private final QProfileBackuper backuper; | |||
private final Languages languages; | |||
private final QProfileWsSupport qProfileWsSupport; | |||
public OldRestoreAction(QProfileBackuper backuper, Languages languages, QProfileWsSupport qProfileWsSupport) { | |||
public OldRestoreAction(DbClient dbClient, QProfileBackuper backuper, Languages languages, QProfileWsSupport qProfileWsSupport) { | |||
this.dbClient = dbClient; | |||
this.backuper = backuper; | |||
this.languages = languages; | |||
this.qProfileWsSupport = qProfileWsSupport; | |||
@@ -77,10 +82,10 @@ public class OldRestoreAction implements WsAction { | |||
InputStream backup = request.paramAsInputStream(PARAM_BACKUP); | |||
InputStreamReader reader = null; | |||
try { | |||
try (DbSession dbSession = dbClient.openSession(false)) { | |||
checkArgument(backup != null, "A backup file must be provided"); | |||
reader = new InputStreamReader(backup, StandardCharsets.UTF_8); | |||
BulkChangeResult result = backuper.restore(reader, null); | |||
BulkChangeResult result = backuper.restore(dbSession, reader, null); | |||
writeResponse(response.newJsonWriter(), result); | |||
} finally { | |||
IOUtils.closeQuietly(reader); |
@@ -21,26 +21,33 @@ package org.sonar.server.qualityprofile.ws; | |||
import java.io.InputStream; | |||
import java.io.InputStreamReader; | |||
import java.nio.charset.StandardCharsets; | |||
import org.apache.commons.io.IOUtils; | |||
import org.sonar.api.resources.Language; | |||
import org.sonar.api.resources.Languages; | |||
import org.sonar.api.server.ws.Request; | |||
import org.sonar.api.server.ws.Response; | |||
import org.sonar.api.server.ws.WebService; | |||
import org.sonar.api.utils.text.JsonWriter; | |||
import org.sonar.db.DbClient; | |||
import org.sonar.db.DbSession; | |||
import org.sonar.db.qualityprofile.QualityProfileDto; | |||
import org.sonar.server.qualityprofile.BulkChangeResult; | |||
import org.sonar.server.qualityprofile.QProfileBackuper; | |||
import static com.google.common.base.Preconditions.checkArgument; | |||
import static java.nio.charset.StandardCharsets.UTF_8; | |||
public class RestoreAction implements QProfileWsAction { | |||
private static final String PARAM_BACKUP = "backup"; | |||
private final DbClient dbClient; | |||
private final QProfileBackuper backuper; | |||
private final Languages languages; | |||
private final QProfileWsSupport wsSupport; | |||
public RestoreAction(QProfileBackuper backuper, Languages languages, QProfileWsSupport wsSupport) { | |||
public RestoreAction(DbClient dbClient, QProfileBackuper backuper, Languages languages, QProfileWsSupport wsSupport) { | |||
this.dbClient = dbClient; | |||
this.backuper = backuper; | |||
this.languages = languages; | |||
this.wsSupport = wsSupport; | |||
@@ -66,10 +73,17 @@ public class RestoreAction implements QProfileWsAction { | |||
public void handle(Request request, Response response) throws Exception { | |||
wsSupport.checkQProfileAdminPermission(); | |||
try (InputStream backup = request.paramAsInputStream(PARAM_BACKUP); | |||
InputStreamReader reader = new InputStreamReader(backup, StandardCharsets.UTF_8)) { | |||
BulkChangeResult result = backuper.restore(reader, null); | |||
InputStream backup = request.paramAsInputStream(PARAM_BACKUP); | |||
InputStreamReader reader = null; | |||
try (DbSession dbSession = dbClient.openSession(false)) { | |||
checkArgument(backup != null, "A backup file must be provided"); | |||
reader = new InputStreamReader(backup, UTF_8); | |||
BulkChangeResult result = backuper.restore(dbSession, reader, null); | |||
writeResponse(response.newJsonWriter(), result); | |||
} finally { | |||
IOUtils.closeQuietly(reader); | |||
IOUtils.closeQuietly(backup); | |||
} | |||
} | |||
@@ -23,6 +23,8 @@ import org.sonar.api.resources.Languages; | |||
import org.sonar.api.server.ws.Request; | |||
import org.sonar.api.server.ws.Response; | |||
import org.sonar.api.server.ws.WebService; | |||
import org.sonar.db.DbClient; | |||
import org.sonar.db.DbSession; | |||
import org.sonar.server.qualityprofile.QProfileReset; | |||
import static org.sonar.server.util.LanguageParamUtils.getExampleValue; | |||
@@ -30,11 +32,13 @@ import static org.sonar.server.util.LanguageParamUtils.getLanguageKeys; | |||
public class RestoreBuiltInAction implements QProfileWsAction { | |||
private final DbClient dbClient; | |||
private final QProfileReset reset; | |||
private final Languages languages; | |||
private final QProfileWsSupport qProfileWsSupport; | |||
public RestoreBuiltInAction(QProfileReset reset, Languages languages, QProfileWsSupport qProfileWsSupport) { | |||
public RestoreBuiltInAction(DbClient dbClient, QProfileReset reset, Languages languages, QProfileWsSupport qProfileWsSupport) { | |||
this.dbClient = dbClient; | |||
this.reset = reset; | |||
this.languages = languages; | |||
this.qProfileWsSupport = qProfileWsSupport; | |||
@@ -60,7 +64,9 @@ public class RestoreBuiltInAction implements QProfileWsAction { | |||
qProfileWsSupport.checkQProfileAdminPermission(); | |||
String language = request.mandatoryParam("language"); | |||
reset.resetLanguage(language); | |||
try (DbSession dbSession = dbClient.openSession(false)) { | |||
reset.resetLanguage(dbSession, language); | |||
} | |||
response.noContent(); | |||
} | |||
} |
@@ -36,9 +36,7 @@ import org.sonar.api.rule.Severity; | |||
import org.sonar.api.server.rule.RuleParamType; | |||
import org.sonar.db.DbClient; | |||
import org.sonar.db.DbSession; | |||
import org.sonar.db.RowNotFoundException; | |||
import org.sonar.db.organization.OrganizationDto; | |||
import org.sonar.db.organization.OrganizationTesting; | |||
import org.sonar.db.qualityprofile.ActiveRuleDao; | |||
import org.sonar.db.qualityprofile.ActiveRuleDto; | |||
import org.sonar.db.qualityprofile.ActiveRuleParamDto; | |||
@@ -122,7 +120,8 @@ public class QProfileBackuperMediumTest { | |||
ruleIndexer.index(); | |||
// create profile P1 with rules x2 and x1 activated | |||
db.qualityProfileDao().insert(dbSession, newXooP1(organization)); | |||
QualityProfileDto profile = newXooP1(organization); | |||
db.qualityProfileDao().insert(dbSession, profile); | |||
RuleActivation activation1 = new RuleActivation(XOO_X2).setSeverity("MINOR"); | |||
RuleActivation activation2 = new RuleActivation(XOO_X1); | |||
RuleActivation activation3 = new RuleActivation(blahRuleKey); | |||
@@ -136,24 +135,16 @@ public class QProfileBackuperMediumTest { | |||
activeRuleIndexer.index(); | |||
StringWriter output = new StringWriter(); | |||
tester.get(QProfileBackuper.class).backup(XOO_P1_KEY, output); | |||
tester.get(QProfileBackuper.class).backup(dbSession, profile, output); | |||
String expectedXml = Resources.toString(getClass().getResource("QProfileBackuperMediumTest/expected-backup.xml"), StandardCharsets.UTF_8); | |||
assertThat(output.toString()).isXmlEqualTo(expectedXml); | |||
} | |||
@Test | |||
public void fail_to_backup_unknown_profile() { | |||
thrown.expect(RowNotFoundException.class); | |||
thrown.expectMessage("Quality profile not found: unknown"); | |||
tester.get(QProfileBackuper.class).backup("unknown", new StringWriter()); | |||
} | |||
@Test | |||
public void restore_and_create_profile() throws Exception { | |||
// Backup file declares profile P1 on xoo | |||
tester.get(QProfileBackuper.class).restore(new StringReader( | |||
tester.get(QProfileBackuper.class).restore(dbSession, new StringReader( | |||
Resources.toString(getClass().getResource("QProfileBackuperMediumTest/restore.xml"), StandardCharsets.UTF_8)), | |||
null); | |||
@@ -195,7 +186,7 @@ public class QProfileBackuperMediumTest { | |||
// restore backup, which activates only x1 | |||
// -> update x1 and deactivate x2 | |||
tester.get(QProfileBackuper.class).restore(new StringReader( | |||
tester.get(QProfileBackuper.class).restore(dbSession, new StringReader( | |||
Resources.toString(getClass().getResource("QProfileBackuperMediumTest/restore.xml"), StandardCharsets.UTF_8)), null); | |||
// Check in db | |||
@@ -233,7 +224,7 @@ public class QProfileBackuperMediumTest { | |||
activeRuleIndexer.index(); | |||
// restore backup of child profile -> overrides x1 | |||
tester.get(QProfileBackuper.class).restore(new StringReader( | |||
tester.get(QProfileBackuper.class).restore(dbSession, new StringReader( | |||
Resources.toString(getClass().getResource("QProfileBackuperMediumTest/restore-child.xml"), StandardCharsets.UTF_8)), null); | |||
// parent profile is unchanged | |||
@@ -281,7 +272,7 @@ public class QProfileBackuperMediumTest { | |||
activeRuleIndexer.index(); | |||
// restore backup of parent profile -> update x1 and propagates to child | |||
tester.get(QProfileBackuper.class).restore(new StringReader( | |||
tester.get(QProfileBackuper.class).restore(dbSession, new StringReader( | |||
Resources.toString(getClass().getResource("QProfileBackuperMediumTest/restore-parent.xml"), StandardCharsets.UTF_8)), null); | |||
// parent profile is updated | |||
@@ -330,7 +321,7 @@ public class QProfileBackuperMediumTest { | |||
activeRuleIndexer.index(); | |||
// backup of child profile contains x2 but not x1 | |||
tester.get(QProfileBackuper.class).restore(new StringReader( | |||
tester.get(QProfileBackuper.class).restore(dbSession, new StringReader( | |||
Resources.toString(getClass().getResource("QProfileBackuperMediumTest/keep_other_inherited_rules.xml"), StandardCharsets.UTF_8)), XOO_P2_NAME); | |||
// x1 and x2 | |||
@@ -340,7 +331,7 @@ public class QProfileBackuperMediumTest { | |||
@Test | |||
public void fail_to_restore_if_not_xml_backup() throws Exception { | |||
try { | |||
tester.get(QProfileBackuper.class).restore(new StringReader( | |||
tester.get(QProfileBackuper.class).restore(dbSession, new StringReader( | |||
Resources.toString(getClass().getResource("QProfileBackuperMediumTest/not-xml-backup.txt"), StandardCharsets.UTF_8)), null); | |||
fail(); | |||
} catch (IllegalStateException e) { | |||
@@ -352,7 +343,7 @@ public class QProfileBackuperMediumTest { | |||
@Test | |||
public void fail_to_restore_if_bad_xml_format() throws Exception { | |||
try { | |||
tester.get(QProfileBackuper.class).restore(new StringReader( | |||
tester.get(QProfileBackuper.class).restore(dbSession, new StringReader( | |||
Resources.toString(getClass().getResource("QProfileBackuperMediumTest/bad-xml-backup.xml"), StandardCharsets.UTF_8)), null); | |||
fail(); | |||
} catch (IllegalArgumentException e) { | |||
@@ -363,7 +354,7 @@ public class QProfileBackuperMediumTest { | |||
@Test | |||
public void fail_to_restore_if_duplicate_rule() throws Exception { | |||
try { | |||
tester.get(QProfileBackuper.class).restore(new StringReader( | |||
tester.get(QProfileBackuper.class).restore(dbSession, new StringReader( | |||
Resources.toString(getClass().getResource("QProfileBackuperMediumTest/duplicates-xml-backup.xml"), StandardCharsets.UTF_8)), null); | |||
fail(); | |||
} catch (IllegalArgumentException e) { | |||
@@ -373,7 +364,7 @@ public class QProfileBackuperMediumTest { | |||
@Test | |||
public void restore_and_override_profile_name() throws Exception { | |||
tester.get(QProfileBackuper.class).restore(new StringReader( | |||
tester.get(QProfileBackuper.class).restore(dbSession, new StringReader( | |||
Resources.toString(getClass().getResource("QProfileBackuperMediumTest/restore.xml"), StandardCharsets.UTF_8)), | |||
XOO_P3_NAME); | |||
@@ -387,7 +378,7 @@ public class QProfileBackuperMediumTest { | |||
@Test | |||
public void restore_profile_with_zero_rules() throws Exception { | |||
tester.get(QProfileBackuper.class).restore(new StringReader( | |||
tester.get(QProfileBackuper.class).restore(dbSession, new StringReader( | |||
Resources.toString(getClass().getResource("QProfileBackuperMediumTest/empty.xml"), StandardCharsets.UTF_8)), | |||
null); | |||
@@ -39,7 +39,6 @@ import org.sonar.db.qualityprofile.QualityProfileDto; | |||
import org.sonar.db.rule.RuleDto; | |||
import org.sonar.db.rule.RuleParamDto; | |||
import org.sonar.db.rule.RuleTesting; | |||
import org.sonar.server.qualityprofile.index.ActiveRuleIndex; | |||
import org.sonar.server.qualityprofile.index.ActiveRuleIndexer; | |||
import org.sonar.server.rule.index.RuleIndex; | |||
import org.sonar.server.rule.index.RuleIndexer; | |||
@@ -60,7 +59,6 @@ public class QProfileCopierMediumTest { | |||
private DbClient db; | |||
private DbSession dbSession; | |||
private ActiveRuleIndex index; | |||
private RuleActivator ruleActivator; | |||
private QProfileCopier copier; | |||
private RuleIndexer ruleIndexer; | |||
@@ -73,7 +71,6 @@ public class QProfileCopierMediumTest { | |||
db = tester.get(DbClient.class); | |||
dbSession = db.openSession(false); | |||
ruleActivator = tester.get(RuleActivator.class); | |||
index = tester.get(ActiveRuleIndex.class); | |||
copier = tester.get(QProfileCopier.class); | |||
ruleIndexer = tester.get(RuleIndexer.class); | |||
activeRuleIndexer = tester.get(ActiveRuleIndexer.class); | |||
@@ -111,7 +108,7 @@ public class QProfileCopierMediumTest { | |||
activeRuleIndexer.index(); | |||
// target does not exist | |||
copier.copyToName(QProfileTesting.XOO_P1_KEY, QProfileTesting.XOO_P2_NAME.getName()); | |||
copier.copyToName(dbSession, QProfileTesting.XOO_P1_KEY, QProfileTesting.XOO_P2_NAME.getName()); | |||
verifyOneActiveRule(QProfileTesting.XOO_P2_NAME, Severity.BLOCKER, null, ImmutableMap.of("max", "7")); | |||
} | |||
@@ -141,7 +138,7 @@ public class QProfileCopierMediumTest { | |||
activeRuleIndexer.index(); | |||
// copy -> reset x1 and deactivate x2 | |||
copier.copyToName(QProfileTesting.XOO_P1_KEY, QProfileTesting.XOO_P2_NAME.getName()); | |||
copier.copyToName(dbSession, QProfileTesting.XOO_P1_KEY, QProfileTesting.XOO_P2_NAME.getName()); | |||
verifyOneActiveRule(QProfileTesting.XOO_P2_KEY, Severity.BLOCKER, null, ImmutableMap.of("max", "7")); | |||
} | |||
@@ -161,7 +158,7 @@ public class QProfileCopierMediumTest { | |||
activeRuleIndexer.index(); | |||
// copy child -> profile2 is created with parent P1 | |||
copier.copyToName(QProfileTesting.XOO_P1_KEY, QProfileTesting.XOO_P2_NAME.getName()); | |||
copier.copyToName(dbSession, QProfileTesting.XOO_P1_KEY, QProfileTesting.XOO_P2_NAME.getName()); | |||
verifyOneActiveRule(QProfileTesting.XOO_P2_KEY, Severity.BLOCKER, ActiveRuleDto.INHERITED, ImmutableMap.of("max", "7")); | |||
QualityProfileDto profile2Dto = db.qualityProfileDao().selectByKey(dbSession, QProfileTesting.XOO_P2_KEY); | |||
@@ -179,7 +176,7 @@ public class QProfileCopierMediumTest { | |||
activeRuleIndexer.index(); | |||
try { | |||
copier.copyToName(QProfileTesting.XOO_P1_KEY, QProfileTesting.XOO_P1_NAME.getName()); | |||
copier.copyToName(dbSession, QProfileTesting.XOO_P1_KEY, QProfileTesting.XOO_P1_NAME.getName()); | |||
fail(); | |||
} catch (IllegalArgumentException e) { | |||
assertThat(e).hasMessage("Source and target profiles are equal: P1"); |
@@ -139,7 +139,7 @@ public class QProfileResetMediumTest { | |||
assertThat(activeRuleParamDtos.get(0).getKey()).isEqualTo("acceptWhitespace"); | |||
assertThat(activeRuleParamDtos.get(0).getValue()).isEqualTo("false"); | |||
reset.resetLanguage(ServerTester.Xoo.KEY); | |||
reset.resetLanguage(dbSession, ServerTester.Xoo.KEY); | |||
dbSession.commit(); | |||
// Severity and parameter value come back to origin after reset | |||
@@ -188,7 +188,7 @@ public class QProfileResetMediumTest { | |||
} | |||
}, defProfile); | |||
reset.resetLanguage(ServerTester.Xoo.KEY); | |||
reset.resetLanguage(dbSession, ServerTester.Xoo.KEY); | |||
// Parameter value come back to origin after reset | |||
ActiveRuleDto activeRuleDto = tester.get(ActiveRuleDao.class).selectOrFailByKey(dbSession, activeRuleKey); |
@@ -26,6 +26,7 @@ import org.junit.rules.ExpectedException; | |||
import org.junit.runner.RunWith; | |||
import org.mockito.Mock; | |||
import org.mockito.runners.MockitoJUnitRunner; | |||
import org.sonar.db.DbSession; | |||
import org.sonar.db.DbTester; | |||
import org.sonar.db.qualityprofile.QualityProfileDto; | |||
import org.sonar.server.exceptions.ForbiddenException; | |||
@@ -37,6 +38,8 @@ import org.sonar.server.qualityprofile.QProfileCopier; | |||
import org.sonar.server.tester.UserSessionRule; | |||
import org.sonar.server.ws.WsTester; | |||
import static org.mockito.Matchers.any; | |||
import static org.mockito.Matchers.eq; | |||
import static org.mockito.Mockito.mock; | |||
import static org.mockito.Mockito.verify; | |||
import static org.mockito.Mockito.when; | |||
@@ -68,7 +71,7 @@ public class CopyActionTest { | |||
tester = new WsTester(new QProfilesWs( | |||
mock(RuleActivationActions.class), | |||
mock(BulkRuleActivationActions.class), | |||
new CopyAction(qProfileCopier, LanguageTesting.newLanguages("xoo"), new QProfileWsSupport(db.getDbClient(), userSessionRule, defaultOrganizationProvider)))); | |||
new CopyAction(db.getDbClient(), qProfileCopier, LanguageTesting.newLanguages("xoo"), new QProfileWsSupport(db.getDbClient(), userSessionRule, defaultOrganizationProvider)))); | |||
} | |||
@Test | |||
@@ -78,7 +81,7 @@ public class CopyActionTest { | |||
String fromProfileKey = "xoo-sonar-way-23456"; | |||
String toName = "Other Sonar Way"; | |||
when(qProfileCopier.copyToName(fromProfileKey, toName)).thenReturn( | |||
when(qProfileCopier.copyToName(any(DbSession.class), eq(fromProfileKey), eq(toName))).thenReturn( | |||
QualityProfileDto.createFor("xoo-other-sonar-way-12345") | |||
.setName(toName) | |||
.setLanguage("xoo")); | |||
@@ -88,7 +91,7 @@ public class CopyActionTest { | |||
.setParam("toName", toName) | |||
.execute().assertJson(getClass(), "copy_nominal.json"); | |||
verify(qProfileCopier).copyToName(fromProfileKey, toName); | |||
verify(qProfileCopier).copyToName(any(DbSession.class), eq(fromProfileKey), eq(toName)); | |||
} | |||
@Test | |||
@@ -98,7 +101,7 @@ public class CopyActionTest { | |||
String fromProfileKey = "xoo-sonar-way-23456"; | |||
String toName = "Other Sonar Way"; | |||
when(qProfileCopier.copyToName(fromProfileKey, toName)).thenReturn( | |||
when(qProfileCopier.copyToName(any(DbSession.class), eq(fromProfileKey), eq(toName))).thenReturn( | |||
QualityProfileDto.createFor("xoo-other-sonar-way-12345") | |||
.setName(toName) | |||
.setLanguage("xoo") | |||
@@ -109,7 +112,7 @@ public class CopyActionTest { | |||
.setParam("toName", toName) | |||
.execute().assertJson(getClass(), "copy_with_parent.json"); | |||
verify(qProfileCopier).copyToName(fromProfileKey, toName); | |||
verify(qProfileCopier).copyToName(any(DbSession.class), eq(fromProfileKey), eq(toName)); | |||
} | |||
@Test |
@@ -23,6 +23,7 @@ import org.junit.Before; | |||
import org.junit.Test; | |||
import org.sonar.api.resources.Languages; | |||
import org.sonar.api.server.ws.WebService; | |||
import org.sonar.db.DbClient; | |||
import org.sonar.server.qualityprofile.QProfileBackuper; | |||
import org.sonar.server.ws.RemovedWebServiceHandler; | |||
import org.sonar.server.ws.WsTester; | |||
@@ -37,7 +38,7 @@ public class ProfilesWsTest { | |||
@Before | |||
public void setUp() { | |||
ws = new WsTester(new ProfilesWs( | |||
new OldRestoreAction(mock(QProfileBackuper.class), mock(Languages.class), mock(QProfileWsSupport.class)))); | |||
new OldRestoreAction(mock(DbClient.class), mock(QProfileBackuper.class), mock(Languages.class), mock(QProfileWsSupport.class)))); | |||
} | |||
@Test |
@@ -69,16 +69,16 @@ public class QProfilesWsTest { | |||
new RemoveProjectAction(projectAssociationParameters, null, null, dbClient), | |||
new CreateAction(null, null, null, languages, wsSupport, userSessionRule, null, importers), | |||
new ImportersAction(importers), | |||
new RestoreBuiltInAction(null, languages, wsSupport), | |||
new RestoreBuiltInAction(dbClient, null, languages, wsSupport), | |||
new SearchAction(null, languages), | |||
new SetDefaultAction(languages, null, null, wsSupport), | |||
new ProjectsAction(null, userSessionRule), | |||
new ChangelogAction(null, mock(QProfileFactory.class), languages, dbClient), | |||
new ChangeParentAction(dbClient, null, null, languages, wsSupport), | |||
new CompareAction(null, null, languages), | |||
new CopyAction(null, languages, wsSupport), | |||
new DeleteAction(languages, null, null, userSessionRule, wsSupport), | |||
new ExportAction(null, null, mock(QProfileExporters.class), languages, wsSupport), | |||
new CopyAction(dbClient, null, languages, wsSupport), | |||
new ExportersAction(), | |||
new InheritanceAction(null, null, null, null, languages), | |||
new RenameAction(null, wsSupport, dbClient, userSessionRule))).controller(QProfilesWs.API_ENDPOINT); |
@@ -25,6 +25,7 @@ import org.junit.Test; | |||
import org.junit.rules.ExpectedException; | |||
import org.sonar.api.resources.Languages; | |||
import org.sonar.api.server.ws.WebService; | |||
import org.sonar.db.DbSession; | |||
import org.sonar.db.DbTester; | |||
import org.sonar.db.organization.OrganizationDto; | |||
import org.sonar.db.qualityprofile.QualityProfileDto; | |||
@@ -63,7 +64,7 @@ public class RestoreActionTest { | |||
private DefaultOrganizationProvider defaultOrganizationProvider = TestDefaultOrganizationProvider.from(db); | |||
private QProfileWsSupport wsSupport = new QProfileWsSupport(db.getDbClient(), userSession, defaultOrganizationProvider); | |||
private Languages languages = LanguageTesting.newLanguages(A_LANGUAGE); | |||
private WsActionTester tester = new WsActionTester(new RestoreAction(backuper, languages, wsSupport)); | |||
private WsActionTester tester = new WsActionTester(new RestoreAction(db.getDbClient(), backuper, languages, wsSupport)); | |||
@Test | |||
public void test_definition() { | |||
@@ -84,13 +85,13 @@ public class RestoreActionTest { | |||
QualityProfileDto profile = QualityProfileDto.createFor("P1") | |||
.setDefault(false).setLanguage("xoo").setName("Sonar way"); | |||
BulkChangeResult restoreResult = new BulkChangeResult(profile); | |||
when(backuper.restore(any(Reader.class), any(QProfileName.class))).thenReturn(restoreResult); | |||
when(backuper.restore(any(DbSession.class), any(Reader.class), any(QProfileName.class))).thenReturn(restoreResult); | |||
logInAsQProfileAdministrator(db.getDefaultOrganization()); | |||
TestResponse response = restore("<backup/>"); | |||
JsonAssert.assertJson(response.getInput()).isSimilarTo(getClass().getResource("RestoreActionTest/restore_profile.json")); | |||
verify(backuper).restore(any(Reader.class), any(QProfileName.class)); | |||
verify(backuper).restore(any(DbSession.class), any(Reader.class), any(QProfileName.class)); | |||
} | |||
@Test |
@@ -23,6 +23,7 @@ import org.junit.Rule; | |||
import org.junit.Test; | |||
import org.junit.rules.ExpectedException; | |||
import org.sonar.api.resources.Languages; | |||
import org.sonar.db.DbSession; | |||
import org.sonar.db.DbTester; | |||
import org.sonar.server.exceptions.ForbiddenException; | |||
import org.sonar.server.exceptions.UnauthorizedException; | |||
@@ -35,6 +36,8 @@ import org.sonar.server.ws.TestResponse; | |||
import org.sonar.server.ws.WsActionTester; | |||
import static org.assertj.core.api.Assertions.assertThat; | |||
import static org.mockito.Matchers.any; | |||
import static org.mockito.Matchers.eq; | |||
import static org.mockito.Mockito.mock; | |||
import static org.mockito.Mockito.verify; | |||
import static org.sonar.db.permission.OrganizationPermission.ADMINISTER_QUALITY_PROFILES; | |||
@@ -54,14 +57,14 @@ public class RestoreBuiltInActionTest { | |||
private DefaultOrganizationProvider defaultOrganizationProvider = TestDefaultOrganizationProvider.fromUuid("ORG1"); | |||
private QProfileWsSupport wsSupport = new QProfileWsSupport(db.getDbClient(), userSession, defaultOrganizationProvider); | |||
private WsActionTester tester = new WsActionTester(new RestoreBuiltInAction(reset, languages, wsSupport)); | |||
private WsActionTester tester = new WsActionTester(new RestoreBuiltInAction(db.getDbClient(), reset, languages, wsSupport)); | |||
@Test | |||
public void return_empty_result_when_no_info_or_warning() { | |||
logInAsQProfileAdministrator(); | |||
TestResponse response = tester.newRequest().setParam("language", "xoo").execute(); | |||
verify(reset).resetLanguage("xoo"); | |||
verify(reset).resetLanguage(any(DbSession.class), eq("xoo")); | |||
assertThat(response.getStatus()).isEqualTo(204); | |||
} | |||