aboutsummaryrefslogtreecommitdiffstats
path: root/server
diff options
context:
space:
mode:
authorJean-Baptiste Lievremont <jean-baptiste.lievremont@sonarsource.com>2015-04-10 09:48:51 +0200
committerJean-Baptiste Lievremont <jean-baptiste.lievremont@sonarsource.com>2015-04-10 09:48:51 +0200
commit9b872aaff0c0c20a4607dfe93accb19fdc2fd221 (patch)
treea0862ff40cf97ad3d72cee55055ab1a98923a78c /server
parent1e4dd3abcc7807e65f3e6773cbaa0aa1c8a79ef3 (diff)
downloadsonarqube-9b872aaff0c0c20a4607dfe93accb19fdc2fd221.tar.gz
sonarqube-9b872aaff0c0c20a4607dfe93accb19fdc2fd221.zip
SONAR-6301 Show copied profile properties in WS response
Diffstat (limited to 'server')
-rw-r--r--server/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileCopier.java18
-rw-r--r--server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/QProfileCopyAction.java21
-rw-r--r--server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/QProfileCopyActionTest.java15
-rw-r--r--server/sonar-server/src/test/resources/org/sonar/server/qualityprofile/ws/QProfileCopyActionTest/copy_nominal.json8
4 files changed, 49 insertions, 13 deletions
diff --git a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileCopier.java b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileCopier.java
index 7265157d57a..ed6c893aac8 100644
--- a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileCopier.java
+++ b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileCopier.java
@@ -45,15 +45,19 @@ public class QProfileCopier implements ServerComponent {
this.temp = temp;
}
- public void copyToName(String fromProfileKey, String toName) {
- QProfileName to = prepareTarget(fromProfileKey, toName);
+ public QualityProfileDto copyToName(String fromProfileKey, String toName) {
+ QualityProfileDto to = prepareTarget(fromProfileKey, toName);
File backupFile = temp.newFile();
- backup(fromProfileKey, backupFile);
- restore(backupFile, to);
- FileUtils.deleteQuietly(backupFile);
+ try {
+ backup(fromProfileKey, backupFile);
+ restore(backupFile, QProfileName.createFor(to.getLanguage(), to.getName()));
+ return to;
+ } finally {
+ FileUtils.deleteQuietly(backupFile);
+ }
}
- private QProfileName prepareTarget(String fromProfileKey, String toName) {
+ private QualityProfileDto prepareTarget(String fromProfileKey, String toName) {
DbSession dbSession = db.openSession(false);
try {
QualityProfileDto fromProfile = db.qualityProfileDao().getNonNullByKey(dbSession, fromProfileKey);
@@ -68,7 +72,7 @@ public class QProfileCopier implements ServerComponent {
db.qualityProfileDao().update(dbSession, toProfile);
dbSession.commit();
}
- return toProfileName;
+ return toProfile;
} finally {
dbSession.close();
diff --git a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/QProfileCopyAction.java b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/QProfileCopyAction.java
index f1a3a4819d4..7281dad8939 100644
--- a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/QProfileCopyAction.java
+++ b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/QProfileCopyAction.java
@@ -19,11 +19,14 @@
*/
package org.sonar.server.qualityprofile.ws;
+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.server.ws.WebService.NewAction;
import org.sonar.core.permission.GlobalPermissions;
+import org.sonar.core.qualityprofile.db.QualityProfileDto;
import org.sonar.server.qualityprofile.QProfileCopier;
import org.sonar.server.user.UserSession;
@@ -33,9 +36,11 @@ public class QProfileCopyAction implements BaseQProfileWsAction {
private static final String PARAM_PROFILE_KEY = "fromKey";
private final QProfileCopier profileCopier;
+ private final Languages languages;
- public QProfileCopyAction(QProfileCopier profileCopier) {
+ public QProfileCopyAction(QProfileCopier profileCopier, Languages languages) {
this.profileCopier = profileCopier;
+ this.languages = languages;
}
@Override
@@ -64,8 +69,18 @@ public class QProfileCopyAction implements BaseQProfileWsAction {
String newName = request.mandatoryParam(PARAM_PROFILE_NAME);
String profileKey = request.mandatoryParam(PARAM_PROFILE_KEY);
- profileCopier.copyToName(profileKey, newName);
+ QualityProfileDto copiedProfile = profileCopier.copyToName(profileKey, newName);
- response.noContent();
+ String languageKey = copiedProfile.getLanguage();
+ Language language = languages.get(copiedProfile.getLanguage());
+ 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", copiedProfile.getParentKee() != null)
+ .endObject().close();
}
}
diff --git a/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/QProfileCopyActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/QProfileCopyActionTest.java
index 500208c7528..872bc690f23 100644
--- a/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/QProfileCopyActionTest.java
+++ b/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/QProfileCopyActionTest.java
@@ -25,13 +25,16 @@ import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import org.sonar.core.permission.GlobalPermissions;
+import org.sonar.core.qualityprofile.db.QualityProfileDto;
import org.sonar.server.exceptions.ForbiddenException;
+import org.sonar.server.language.LanguageTesting;
import org.sonar.server.qualityprofile.QProfileCopier;
import org.sonar.server.user.MockUserSession;
import org.sonar.server.ws.WsTester;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
@RunWith(MockitoJUnitRunner.class)
public class QProfileCopyActionTest {
@@ -48,19 +51,25 @@ public class QProfileCopyActionTest {
mock(RuleActivationActions.class),
mock(BulkRuleActivationActions.class),
mock(ProjectAssociationActions.class),
- new QProfileCopyAction(qProfileCopier)));
+ new QProfileCopyAction(qProfileCopier, LanguageTesting.newLanguages("xoo"))));
}
@Test
public void copy_nominal() throws Exception {
MockUserSession.set().setLogin("obiwan").setGlobalPermissions(GlobalPermissions.QUALITY_PROFILE_ADMIN);
- String fromProfileKey = "sonar-way-xoo2-23456";
+ String fromProfileKey = "xoo-sonar-way-23456";
String toName = "Other Sonar Way";
+
+ when(qProfileCopier.copyToName(fromProfileKey, toName)).thenReturn(
+ QualityProfileDto.createFor("xoo-other-sonar-way-12345")
+ .setName(toName)
+ .setLanguage("xoo"));
+
tester.newPostRequest("api/qualityprofiles", "copy")
.setParam("fromKey", fromProfileKey)
.setParam("toName", toName)
- .execute().assertNoContent();
+ .execute().assertJson(getClass(), "copy_nominal.json");
verify(qProfileCopier).copyToName(fromProfileKey, toName);
}
diff --git a/server/sonar-server/src/test/resources/org/sonar/server/qualityprofile/ws/QProfileCopyActionTest/copy_nominal.json b/server/sonar-server/src/test/resources/org/sonar/server/qualityprofile/ws/QProfileCopyActionTest/copy_nominal.json
new file mode 100644
index 00000000000..bbf7ad61dc2
--- /dev/null
+++ b/server/sonar-server/src/test/resources/org/sonar/server/qualityprofile/ws/QProfileCopyActionTest/copy_nominal.json
@@ -0,0 +1,8 @@
+{
+ "key": "xoo-other-sonar-way-12345",
+ "name": "Other Sonar Way",
+ "language": "xoo",
+ "languageName": "Xoo",
+ "isDefault": false,
+ "isInherited": false
+} \ No newline at end of file