diff options
author | Julien Lancelot <julien.lancelot@sonarsource.com> | 2016-12-05 12:03:29 +0100 |
---|---|---|
committer | Julien Lancelot <julien.lancelot@sonarsource.com> | 2016-12-05 18:10:32 +0100 |
commit | b267206b694fbc98fffaaefa9c18fb2250f4ee18 (patch) | |
tree | d3067b95c87818e91767fd77bf093ee87654c89f /server | |
parent | 25cb6851756588388c4746ad60e32ed339abdf92 (diff) | |
download | sonarqube-b267206b694fbc98fffaaefa9c18fb2250f4ee18.tar.gz sonarqube-b267206b694fbc98fffaaefa9c18fb2250f4ee18.zip |
SONAR-8450 Return quality profiles in api/navigation/component WS
Diffstat (limited to 'server')
5 files changed, 95 insertions, 0 deletions
diff --git a/server/sonar-server/src/main/java/org/sonar/server/ui/ws/ComponentNavigationAction.java b/server/sonar-server/src/main/java/org/sonar/server/ui/ws/ComponentNavigationAction.java index cbbcf30fcb5..953acd1a905 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/ui/ws/ComponentNavigationAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/ui/ws/ComponentNavigationAction.java @@ -43,16 +43,19 @@ import org.sonar.db.DbClient; import org.sonar.db.DbSession; import org.sonar.db.component.ComponentDto; import org.sonar.db.component.SnapshotDto; +import org.sonar.db.measure.MeasureQuery; import org.sonar.db.property.PropertyDto; import org.sonar.db.property.PropertyQuery; import org.sonar.server.ce.ws.ActivityAction; import org.sonar.server.component.ComponentFinder; import org.sonar.server.exceptions.ForbiddenException; +import org.sonar.server.qualityprofile.QPMeasureData; import org.sonar.server.ui.ViewProxy; import org.sonar.server.ui.Views; import org.sonar.server.user.UserSession; import static java.util.Locale.ENGLISH; +import static org.sonar.api.measures.CoreMetrics.QUALITY_PROFILES_KEY; import static org.sonar.api.web.UserRole.ADMIN; import static org.sonar.api.web.UserRole.USER; import static org.sonar.core.permission.GlobalPermissions.QUALITY_PROFILE_ADMIN; @@ -113,6 +116,7 @@ public class ComponentNavigationAction implements NavigationWsAction { JsonWriter json = response.newJsonWriter(); json.beginObject(); writeComponent(json, session, component, analysis.orElse(null)); + writeProfiles(json, session, component); if (userSession.hasComponentUuidPermission(ADMIN, component.projectUuid()) || userSession.hasPermission(QUALITY_PROFILE_ADMIN)) { writeConfiguration(json, component); } @@ -148,6 +152,18 @@ public class ComponentNavigationAction implements NavigationWsAction { return componentFavourites.size() == 1; } + private void writeProfiles(JsonWriter json, DbSession session, ComponentDto component) { + json.name("qualityProfiles").beginArray(); + dbClient.measureDao().selectSingle(session, MeasureQuery.builder().setComponentUuid(component.projectUuid()).setMetricKey(QUALITY_PROFILES_KEY).build()).ifPresent( + measureDto -> QPMeasureData.fromJson(measureDto.getData()).getProfiles().forEach( + profile -> json.beginObject() + .prop("key", profile.getQpKey()) + .prop("name", profile.getQpName()) + .prop("language", profile.getLanguageKey()) + .endObject())); + json.endArray(); + } + private void writeExtensions(JsonWriter json, ComponentDto component, List<ViewProxy<Page>> pages) { json.name("extensions").beginArray(); for (ViewProxy<Page> page : pages) { diff --git a/server/sonar-server/src/main/resources/org/sonar/server/ui/ws/example-component.json b/server/sonar-server/src/main/resources/org/sonar/server/ui/ws/example-component.json index 491e0d21e5a..797ea206a78 100644 --- a/server/sonar-server/src/main/resources/org/sonar/server/ui/ws/example-component.json +++ b/server/sonar-server/src/main/resources/org/sonar/server/ui/ws/example-component.json @@ -8,6 +8,18 @@ "isFavorite": true, "version": "5.2-SNAPSHOT", "snapshotDate": "2015-04-16T14:40:32+02:00", + "qualityProfiles": [ + { + "key": "qp1", + "name": "Sonar Way Java", + "language": "java" + }, + { + "key": "qp2", + "name": "Sonar Way Xoo", + "language": "xoo" + } + ], "extensions": [ { "name": "My Resource Plugin", diff --git a/server/sonar-server/src/test/java/org/sonar/server/ui/ws/ComponentNavigationActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/ui/ws/ComponentNavigationActionTest.java index 26f9b47dc63..463dbd1c4bf 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/ui/ws/ComponentNavigationActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/ui/ws/ComponentNavigationActionTest.java @@ -19,6 +19,9 @@ */ package org.sonar.server.ui.ws; +import java.util.Arrays; +import java.util.Date; +import java.util.List; import org.junit.Before; import org.junit.Rule; import org.junit.Test; @@ -41,6 +44,8 @@ import org.sonar.db.DbClient; import org.sonar.db.DbTester; import org.sonar.db.component.ComponentDbTester; import org.sonar.db.component.ComponentDto; +import org.sonar.db.component.SnapshotDto; +import org.sonar.db.metric.MetricDto; import org.sonar.db.property.PropertyDbTester; import org.sonar.db.property.PropertyDto; import org.sonar.db.user.UserDbTester; @@ -48,6 +53,8 @@ import org.sonar.db.user.UserDto; import org.sonar.server.component.ComponentFinder; import org.sonar.server.exceptions.ForbiddenException; import org.sonar.server.exceptions.NotFoundException; +import org.sonar.server.qualityprofile.QPMeasureData; +import org.sonar.server.qualityprofile.QualityProfile; import org.sonar.server.tester.UserSessionRule; import org.sonar.server.ui.Views; import org.sonar.server.ws.WsActionTester; @@ -57,6 +64,7 @@ import static org.mockito.Matchers.any; import static org.mockito.Matchers.eq; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; +import static org.sonar.api.measures.CoreMetrics.QUALITY_PROFILES_KEY; import static org.sonar.core.permission.GlobalPermissions.QUALITY_PROFILE_ADMIN; import static org.sonar.core.permission.GlobalPermissions.SYSTEM_ADMIN; import static org.sonar.db.component.ComponentTesting.newDirectory; @@ -64,6 +72,8 @@ import static org.sonar.db.component.ComponentTesting.newFileDto; import static org.sonar.db.component.ComponentTesting.newModuleDto; import static org.sonar.db.component.ComponentTesting.newProjectDto; import static org.sonar.db.component.SnapshotTesting.newAnalysis; +import static org.sonar.db.measure.MeasureTesting.newMeasureDto; +import static org.sonar.db.metric.MetricTesting.newMetricDto; import static org.sonar.test.JsonAssert.assertJson; public class ComponentNavigationActionTest { @@ -160,6 +170,28 @@ public class ComponentNavigationActionTest { } @Test + public void return_quality_profiles() throws Exception { + init(); + componentDbTester.insertComponent(PROJECT); + SnapshotDto analysis = componentDbTester.insertSnapshot(newAnalysis(PROJECT)); + addQualityProfiles(PROJECT, analysis, + createQProfile("qp1", "Sonar Way Java", "java"), + createQProfile("qp2", "Sonar Way Xoo", "xoo")); + userSessionRule.addProjectUuidPermissions(UserRole.USER, PROJECT.uuid()); + + executeAndVerify(PROJECT.key(), "return_quality_profiles.json"); + } + + @Test + public void return_empty_quality_profiles_when_no_measure() throws Exception { + init(); + componentDbTester.insertComponent(PROJECT); + userSessionRule.addProjectUuidPermissions(UserRole.USER, PROJECT.uuid()); + + executeAndVerify(PROJECT.key(), "return_empty_quality_profiles_when_no_measure.json"); + } + + @Test public void return_extensions() throws Exception { init(createViews()); componentDbTester.insertProjectAndSnapshot(PROJECT); @@ -307,6 +339,24 @@ public class ComponentNavigationActionTest { verify(execute(componentKey), expectedJson); } + private void addQualityProfiles(ComponentDto project, SnapshotDto analysis, QualityProfile... qps) { + MetricDto metricDto = newMetricDto().setKey(QUALITY_PROFILES_KEY); + dbClient.metricDao().insert(dbTester.getSession(), metricDto); + dbClient.measureDao().insert(dbTester.getSession(), + newMeasureDto(metricDto, project, analysis) + .setData(qualityProfilesToJson(qps))); + dbTester.commit(); + } + + private static QualityProfile createQProfile(String qpKey, String qpName, String languageKey) { + return new QualityProfile(qpKey, qpName, languageKey, new Date()); + } + + private static String qualityProfilesToJson(QualityProfile... qps) { + List<QualityProfile> qualityProfiles = Arrays.asList(qps); + return QPMeasureData.toJson(new QPMeasureData(qualityProfiles)); + } + private View[] createViews() { @NavigationSection(NavigationSection.RESOURCE) @ResourceScope(Scopes.PROJECT) diff --git a/server/sonar-server/src/test/resources/org/sonar/server/ui/ws/ComponentNavigationActionTest/return_empty_quality_profiles_when_no_measure.json b/server/sonar-server/src/test/resources/org/sonar/server/ui/ws/ComponentNavigationActionTest/return_empty_quality_profiles_when_no_measure.json new file mode 100644 index 00000000000..d825b579149 --- /dev/null +++ b/server/sonar-server/src/test/resources/org/sonar/server/ui/ws/ComponentNavigationActionTest/return_empty_quality_profiles_when_no_measure.json @@ -0,0 +1,3 @@ +{ + "qualityProfiles": [] +} diff --git a/server/sonar-server/src/test/resources/org/sonar/server/ui/ws/ComponentNavigationActionTest/return_quality_profiles.json b/server/sonar-server/src/test/resources/org/sonar/server/ui/ws/ComponentNavigationActionTest/return_quality_profiles.json new file mode 100644 index 00000000000..10865b08326 --- /dev/null +++ b/server/sonar-server/src/test/resources/org/sonar/server/ui/ws/ComponentNavigationActionTest/return_quality_profiles.json @@ -0,0 +1,14 @@ +{ + "qualityProfiles": [ + { + "key": "qp1", + "name": "Sonar Way Java", + "language": "java" + }, + { + "key": "qp2", + "name": "Sonar Way Xoo", + "language": "xoo" + } + ] +} |