diff options
author | Julien Lancelot <julien.lancelot@sonarsource.com> | 2014-07-22 09:47:37 +0200 |
---|---|---|
committer | Julien Lancelot <julien.lancelot@sonarsource.com> | 2014-07-22 10:19:45 +0200 |
commit | ba7c312b6f37fb70a747583838beb7e6f65b0d51 (patch) | |
tree | abc3c9cb8f83b375c437139c5407e600b4b23c7c /server | |
parent | f3072deb3eca40dde35d2182f05bc2b6c35c051b (diff) | |
download | sonarqube-ba7c312b6f37fb70a747583838beb7e6f65b0d51.tar.gz sonarqube-ba7c312b6f37fb70a747583838beb7e6f65b0d51.zip |
SONAR-5474 Update the /api/components/app to support the overall coverage
Diffstat (limited to 'server')
5 files changed, 131 insertions, 3 deletions
diff --git a/server/sonar-server/src/main/java/org/sonar/server/component/ws/ComponentAppAction.java b/server/sonar-server/src/main/java/org/sonar/server/component/ws/ComponentAppAction.java index f853ce26652..6f10598e69c 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/component/ws/ComponentAppAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/component/ws/ComponentAppAction.java @@ -190,7 +190,7 @@ public class ComponentAppAction implements RequestHandler { if (measuresByMetricKey.get(CoreMetrics.SCM_AUTHORS_BY_LINE_KEY) != null) { tabs.add("scm"); } - if (measuresByMetricKey.get(CoreMetrics.COVERAGE_KEY) != null) { + if (hasCoverage(measuresByMetricKey)) { tabs.add("coverage"); } if (measuresByMetricKey.get(CoreMetrics.DUPLICATED_LINES_KEY) != null) { @@ -201,6 +201,12 @@ public class ComponentAppAction implements RequestHandler { } } + private boolean hasCoverage(Map<String, MeasureDto> measuresByMetricKey) { + return measuresByMetricKey.get(CoreMetrics.OVERALL_COVERAGE_KEY) != null + || measuresByMetricKey.get(CoreMetrics.IT_COVERAGE_KEY) != null + || measuresByMetricKey.get(CoreMetrics.COVERAGE_KEY) != null; + } + private void appendPermissions(JsonWriter json, ComponentDto component, UserSession userSession) { boolean hasBrowsePermission = userSession.hasComponentPermission(UserRole.USER, component.key()); json.prop("canMarkAsFavourite", userSession.isLoggedIn() && hasBrowsePermission); @@ -212,7 +218,7 @@ public class ComponentAppAction implements RequestHandler { json.name("measures").beginObject(); json.prop("fNcloc", formatMeasureOrVariation(measuresByMetricKey.get(CoreMetrics.NCLOC_KEY), periodIndex)); - json.prop("fCoverage", formatMeasureOrVariation(measuresByMetricKey.get(CoreMetrics.COVERAGE_KEY), periodIndex)); + json.prop("fCoverage", formatMeasureOrVariation(coverageMeasure(measuresByMetricKey), periodIndex)); json.prop("fDuplicationDensity", formatMeasureOrVariation(measuresByMetricKey.get(CoreMetrics.DUPLICATED_LINES_DENSITY_KEY), periodIndex)); json.prop("fDebt", formatMeasureOrVariation(measuresByMetricKey.get(CoreMetrics.TECHNICAL_DEBT_KEY), periodIndex)); json.prop("fSqaleRating", formatMeasureOrVariation(measuresByMetricKey.get(CoreMetrics.SQALE_RATING_KEY), periodIndex)); @@ -226,6 +232,19 @@ public class ComponentAppAction implements RequestHandler { json.endObject(); } + private MeasureDto coverageMeasure(Map<String, MeasureDto> measuresByMetricKey) { + MeasureDto overallCoverage = measuresByMetricKey.get(CoreMetrics.OVERALL_COVERAGE_KEY); + MeasureDto itCoverage = measuresByMetricKey.get(CoreMetrics.IT_COVERAGE_KEY); + MeasureDto utCoverage = measuresByMetricKey.get(CoreMetrics.COVERAGE_KEY); + if (overallCoverage != null) { + return overallCoverage; + } else if (utCoverage != null) { + return utCoverage; + } else { + return itCoverage; + } + } + private void appendPeriods(JsonWriter json, List<Period> periodList) { json.name("periods").beginArray(); for (Period period : periodList) { @@ -334,7 +353,7 @@ public class ComponentAppAction implements RequestHandler { Map<String, MeasureDto> measuresByMetricKey = newHashMap(); String fileKey = component.getKey(); for (MeasureDto measureDto : dbClient.measureDao().findByComponentKeyAndMetricKeys(fileKey, - newArrayList(CoreMetrics.NCLOC_KEY, CoreMetrics.COVERAGE_KEY, + newArrayList(CoreMetrics.NCLOC_KEY, CoreMetrics.COVERAGE_KEY, CoreMetrics.IT_COVERAGE_KEY, CoreMetrics.OVERALL_COVERAGE_KEY, CoreMetrics.DUPLICATED_LINES_KEY, CoreMetrics.DUPLICATED_LINES_DENSITY_KEY, CoreMetrics.TECHNICAL_DEBT_KEY, CoreMetrics.TESTS_KEY, CoreMetrics.SCM_AUTHORS_BY_LINE_KEY, CoreMetrics.SQALE_RATING_KEY, CoreMetrics.SQALE_DEBT_RATIO_KEY), session)) { diff --git a/server/sonar-server/src/test/java/org/sonar/server/component/ws/ComponentAppActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/component/ws/ComponentAppActionTest.java index 25ee502d97a..c24e6e36ebf 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/component/ws/ComponentAppActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/component/ws/ComponentAppActionTest.java @@ -321,6 +321,43 @@ public class ComponentAppActionTest { } @Test + public void app_with_overall_measure() throws Exception { + MockUserSession.set().addComponentPermission(UserRole.USER, SUB_PROJECT_KEY, COMPONENT_KEY); + addComponent(); + + addMeasure(CoreMetrics.OVERALL_COVERAGE_KEY, 90.1); + addMeasure(CoreMetrics.COVERAGE_KEY, 95.4); + addMeasure(CoreMetrics.IT_COVERAGE_KEY, 85.2); + + WsTester.TestRequest request = tester.newGetRequest("api/components", "app").setParam("key", COMPONENT_KEY); + request.execute().assertJson(getClass(), "app_with_overall_measure.json"); + } + + @Test + public void app_with_ut_measure() throws Exception { + MockUserSession.set().addComponentPermission(UserRole.USER, SUB_PROJECT_KEY, COMPONENT_KEY); + addComponent(); + + addMeasure(CoreMetrics.COVERAGE_KEY, 95.4); + addMeasure(CoreMetrics.IT_COVERAGE_KEY, 85.2); + + WsTester.TestRequest request = tester.newGetRequest("api/components", "app").setParam("key", COMPONENT_KEY); + request.execute().assertJson(getClass(), "app_with_ut_measure.json"); + } + + @Test + public void app_with_it_measure() throws Exception { + MockUserSession.set().addComponentPermission(UserRole.USER, SUB_PROJECT_KEY, COMPONENT_KEY); + addComponent(); + + + addMeasure(CoreMetrics.IT_COVERAGE_KEY, 85.2); + + WsTester.TestRequest request = tester.newGetRequest("api/components", "app").setParam("key", COMPONENT_KEY); + request.execute().assertJson(getClass(), "app_with_it_measure.json"); + } + + @Test public void app_with_periods() throws Exception { MockUserSession.set().addComponentPermission(UserRole.USER, SUB_PROJECT_KEY, COMPONENT_KEY); diff --git a/server/sonar-server/src/test/resources/org/sonar/server/component/ws/ComponentAppActionTest/app_with_it_measure.json b/server/sonar-server/src/test/resources/org/sonar/server/component/ws/ComponentAppActionTest/app_with_it_measure.json new file mode 100644 index 00000000000..c1b0319485d --- /dev/null +++ b/server/sonar-server/src/test/resources/org/sonar/server/component/ws/ComponentAppActionTest/app_with_it_measure.json @@ -0,0 +1,24 @@ +{ + "key": "org.codehaus.sonar:sonar-plugin-api:src/main/java/org/sonar/api/Plugin.java", + "longName": "src/main/java/org/sonar/api/Plugin.java", + "path": "src/main/java/org/sonar/api/Plugin.java", + "name": "Plugin.java", + "q": "FIL", + "subProject": "org.codehaus.sonar:sonar-plugin-api", + "subProjectName": "SonarQube :: Plugin API", + "project": "org.codehaus.sonar:sonar", + "projectName": "SonarQube", + "fav": false, + "canMarkAsFavourite": false, + "canBulkChange": false, + "canCreateManualIssue": false, + "periods": [], + "severities": [], + "rules": [], + "measures": { + "fCoverage": "85.2%" + }, + "tabs": [ + "coverage" + ] +} diff --git a/server/sonar-server/src/test/resources/org/sonar/server/component/ws/ComponentAppActionTest/app_with_overall_measure.json b/server/sonar-server/src/test/resources/org/sonar/server/component/ws/ComponentAppActionTest/app_with_overall_measure.json new file mode 100644 index 00000000000..071014db274 --- /dev/null +++ b/server/sonar-server/src/test/resources/org/sonar/server/component/ws/ComponentAppActionTest/app_with_overall_measure.json @@ -0,0 +1,24 @@ +{ + "key": "org.codehaus.sonar:sonar-plugin-api:src/main/java/org/sonar/api/Plugin.java", + "longName": "src/main/java/org/sonar/api/Plugin.java", + "path": "src/main/java/org/sonar/api/Plugin.java", + "name": "Plugin.java", + "q": "FIL", + "subProject": "org.codehaus.sonar:sonar-plugin-api", + "subProjectName": "SonarQube :: Plugin API", + "project": "org.codehaus.sonar:sonar", + "projectName": "SonarQube", + "fav": false, + "canMarkAsFavourite": false, + "canBulkChange": false, + "canCreateManualIssue": false, + "periods": [], + "severities": [], + "rules": [], + "measures": { + "fCoverage": "90.1%" + }, + "tabs": [ + "coverage" + ] +} diff --git a/server/sonar-server/src/test/resources/org/sonar/server/component/ws/ComponentAppActionTest/app_with_ut_measure.json b/server/sonar-server/src/test/resources/org/sonar/server/component/ws/ComponentAppActionTest/app_with_ut_measure.json new file mode 100644 index 00000000000..294005bebf3 --- /dev/null +++ b/server/sonar-server/src/test/resources/org/sonar/server/component/ws/ComponentAppActionTest/app_with_ut_measure.json @@ -0,0 +1,24 @@ +{ + "key": "org.codehaus.sonar:sonar-plugin-api:src/main/java/org/sonar/api/Plugin.java", + "longName": "src/main/java/org/sonar/api/Plugin.java", + "path": "src/main/java/org/sonar/api/Plugin.java", + "name": "Plugin.java", + "q": "FIL", + "subProject": "org.codehaus.sonar:sonar-plugin-api", + "subProjectName": "SonarQube :: Plugin API", + "project": "org.codehaus.sonar:sonar", + "projectName": "SonarQube", + "fav": false, + "canMarkAsFavourite": false, + "canBulkChange": false, + "canCreateManualIssue": false, + "periods": [], + "severities": [], + "rules": [], + "measures": { + "fCoverage": "95.4%" + }, + "tabs": [ + "coverage" + ] +} |