From 9e949407c224324b76491d3a7df2df07b35aae23 Mon Sep 17 00:00:00 2001 From: Julien Lancelot Date: Wed, 14 May 2014 14:59:18 +0200 Subject: [PATCH] SONAR-5285 Manage type parameter to return coverage for unit tests, integration tests or overall tests --- .../sonar/server/test/CoverageService.java | 72 +++++++++++-------- .../server/test/ws/CoverageShowAction.java | 42 +++++------ .../server/test/CoverageServiceTest.java | 36 ++++++++-- .../test/ws/CoverageShowActionTest.java | 44 +++++++++--- .../show_coverage_for_integration_test.json | 9 +++ .../show_coverage_for_overall_test.json | 9 +++ 6 files changed, 146 insertions(+), 66 deletions(-) create mode 100644 sonar-server/src/test/resources/org/sonar/server/test/ws/CoverageShowActionTest/show_coverage_for_integration_test.json create mode 100644 sonar-server/src/test/resources/org/sonar/server/test/ws/CoverageShowActionTest/show_coverage_for_overall_test.json diff --git a/sonar-server/src/main/java/org/sonar/server/test/CoverageService.java b/sonar-server/src/main/java/org/sonar/server/test/CoverageService.java index 52ff6e42381..7a723a8e2ac 100644 --- a/sonar-server/src/main/java/org/sonar/server/test/CoverageService.java +++ b/sonar-server/src/main/java/org/sonar/server/test/CoverageService.java @@ -20,10 +20,12 @@ package org.sonar.server.test; +import com.google.common.collect.Maps; import org.sonar.api.ServerComponent; import org.sonar.api.measures.CoreMetrics; import org.sonar.api.test.MutableTestable; import org.sonar.api.test.Testable; +import org.sonar.api.utils.KeyValueFormat; import org.sonar.api.web.UserRole; import org.sonar.core.component.SnapshotPerspectives; import org.sonar.core.measure.db.MeasureDataDao; @@ -36,6 +38,10 @@ import java.util.Map; public class CoverageService implements ServerComponent { + public enum TYPE { + UT, IT, OVERALL + } + private final MeasureDataDao measureDataDao; private final SnapshotPerspectives snapshotPerspectives; @@ -48,48 +54,58 @@ public class CoverageService implements ServerComponent { UserSession.get().checkComponentPermission(UserRole.CODEVIEWER, fileKey); } - /** - * Warning - does not check permission - */ - @CheckForNull - public String getHitsData(String fileKey) { - return findDataFromComponent(fileKey, CoreMetrics.COVERAGE_LINE_HITS_DATA_KEY); + public Map getHits(String fileKey, CoverageService.TYPE type) { + switch (type) { + case IT: + return findDataFromComponent(fileKey, CoreMetrics.IT_COVERAGE_LINE_HITS_DATA_KEY); + case OVERALL: + return findDataFromComponent(fileKey, CoreMetrics.OVERALL_COVERAGE_LINE_HITS_DATA_KEY); + default: + return findDataFromComponent(fileKey, CoreMetrics.COVERAGE_LINE_HITS_DATA_KEY); + } } - /** - * Warning - does not check permission - */ - @CheckForNull - public String getConditionsData(String fileKey) { - return findDataFromComponent(fileKey, CoreMetrics.CONDITIONS_BY_LINE_KEY); + public Map getConditions(String fileKey, CoverageService.TYPE type) { + switch (type) { + case IT: + return findDataFromComponent(fileKey, CoreMetrics.IT_CONDITIONS_BY_LINE_KEY); + case OVERALL: + return findDataFromComponent(fileKey, CoreMetrics.OVERALL_CONDITIONS_BY_LINE_KEY); + default: + return findDataFromComponent(fileKey, CoreMetrics.CONDITIONS_BY_LINE_KEY); + } } - /** - * Warning - does not check permission - */ - @CheckForNull - public String getCoveredConditionsData(String fileKey) { - return findDataFromComponent(fileKey, CoreMetrics.COVERED_CONDITIONS_BY_LINE_KEY); + public Map getCoveredConditions(String fileKey, CoverageService.TYPE type) { + switch (type) { + case IT: + return findDataFromComponent(fileKey, CoreMetrics.IT_COVERED_CONDITIONS_BY_LINE_KEY); + case OVERALL: + return findDataFromComponent(fileKey, CoreMetrics.OVERALL_COVERED_CONDITIONS_BY_LINE_KEY); + default: + return findDataFromComponent(fileKey, CoreMetrics.COVERED_CONDITIONS_BY_LINE_KEY); + } } /** - * Warning - does not check permission + * Test cases is only return for unit tests */ - @CheckForNull - public Map getTestCasesByLines(String fileKey) { - Testable testable = snapshotPerspectives.as(MutableTestable.class, fileKey); - if (testable != null) { - return testable.testCasesByLines(); + public Map getTestCases(String fileKey, CoverageService.TYPE type) { + if (TYPE.UT.equals(type)) { + Testable testable = snapshotPerspectives.as(MutableTestable.class, fileKey); + if (testable != null) { + return testable.testCasesByLines(); + } } - return null; + return Maps.newHashMap(); } @CheckForNull - private String findDataFromComponent(String fileKey, String metricKey) { + private Map findDataFromComponent(String fileKey, String metricKey) { MeasureDataDto data = measureDataDao.findByComponentKeyAndMetricKey(fileKey, metricKey); if (data != null) { - return data.getData(); + return KeyValueFormat.parseIntInt(data.getData()); } - return null; + return Maps.newHashMap(); } } diff --git a/sonar-server/src/main/java/org/sonar/server/test/ws/CoverageShowAction.java b/sonar-server/src/main/java/org/sonar/server/test/ws/CoverageShowAction.java index cab2d31ef69..e385920a927 100644 --- a/sonar-server/src/main/java/org/sonar/server/test/ws/CoverageShowAction.java +++ b/sonar-server/src/main/java/org/sonar/server/test/ws/CoverageShowAction.java @@ -20,14 +20,12 @@ package org.sonar.server.test.ws; -import com.google.common.collect.Maps; import com.google.common.io.Resources; import org.apache.commons.lang.ObjectUtils; import org.sonar.api.server.ws.Request; import org.sonar.api.server.ws.RequestHandler; import org.sonar.api.server.ws.Response; import org.sonar.api.server.ws.WebService; -import org.sonar.api.utils.KeyValueFormat; import org.sonar.api.utils.text.JsonWriter; import org.sonar.server.test.CoverageService; @@ -35,6 +33,11 @@ import java.util.Map; public class CoverageShowAction implements RequestHandler { + private static final String KEY = "key"; + private static final String FROM = "from"; + private static final String TO = "to"; + private static final String TYPE = "type"; + private final CoverageService coverageService; public CoverageShowAction(CoverageService coverageService) { @@ -49,54 +52,51 @@ public class CoverageShowAction implements RequestHandler { .setHandler(this); action - .createParam("key") + .createParam(KEY) .setRequired(true) .setDescription("File key") .setExampleValue("my_project:/src/foo/Bar.php"); action - .createParam("from") + .createParam(FROM) .setDescription("First line to return. Starts at 1") .setExampleValue("10") .setDefaultValue("1"); action - .createParam("to") + .createParam(TO) .setDescription("Last line to return (inclusive)") .setExampleValue("20"); action - .createParam("type") + .createParam(TYPE) .setDescription("Type of coverage info to return :" + "
    " + "
  • UT : Unit Tests
  • " + "
  • IT : Integration Tests
  • " + "
  • OVERALL : Unit and Integration Tests
  • " + "
") - .setPossibleValues("UT", "IT", "OVERALL") - .setDefaultValue("UT"); + .setPossibleValues(CoverageService.TYPE.values()) + .setDefaultValue(CoverageService.TYPE.UT.name()); } @Override public void handle(Request request, Response response) { - String fileKey = request.mandatoryParam("key"); + String fileKey = request.mandatoryParam(KEY); coverageService.checkPermission(fileKey); - int from = Math.max(request.mandatoryParamAsInt("from"), 1); - int to = (Integer) ObjectUtils.defaultIfNull(request.paramAsInt("to"), Integer.MAX_VALUE); + int from = Math.max(request.mandatoryParamAsInt(FROM), 1); + int to = (Integer) ObjectUtils.defaultIfNull(request.paramAsInt(TO), Integer.MAX_VALUE); + CoverageService.TYPE type = CoverageService.TYPE.valueOf(request.param(TYPE)); JsonWriter json = response.newJsonWriter().beginObject(); - String hits = coverageService.getHitsData(fileKey); - Map testCases = coverageService.getTestCasesByLines(fileKey); - String conditions = coverageService.getConditionsData(fileKey); - String coveredConditions = coverageService.getCoveredConditionsData(fileKey); - if (hits != null) { - Map hitsByLine = KeyValueFormat.parseIntInt(hits); - Map testCasesByLine = testCases != null ? testCases : Maps.newHashMap(); - Map conditionsByLine = conditions != null ? KeyValueFormat.parseIntInt(conditions) : Maps.newHashMap(); - Map coveredConditionsByLine = coveredConditions != null ? KeyValueFormat.parseIntInt(coveredConditions) : Maps.newHashMap(); - writeCoverage(fileKey, hitsByLine, testCasesByLine, conditionsByLine, coveredConditionsByLine, from, to, json); + Map hits = coverageService.getHits(fileKey, type); + if (!hits.isEmpty()) { + Map testCases = coverageService.getTestCases(fileKey, type); + Map conditions = coverageService.getConditions(fileKey, type); + Map coveredConditions = coverageService.getCoveredConditions(fileKey, type); + writeCoverage(fileKey, hits, testCases, conditions, coveredConditions, from, to, json); } json.endObject().close(); diff --git a/sonar-server/src/test/java/org/sonar/server/test/CoverageServiceTest.java b/sonar-server/src/test/java/org/sonar/server/test/CoverageServiceTest.java index f9d2c3c6906..9a158755c59 100644 --- a/sonar-server/src/test/java/org/sonar/server/test/CoverageServiceTest.java +++ b/sonar-server/src/test/java/org/sonar/server/test/CoverageServiceTest.java @@ -33,6 +33,8 @@ import org.sonar.core.component.SnapshotPerspectives; import org.sonar.core.measure.db.MeasureDataDao; import org.sonar.server.user.MockUserSession; +import java.util.Collections; + import static org.fest.assertions.Assertions.assertThat; import static org.mockito.Matchers.anyString; import static org.mockito.Matchers.eq; @@ -70,26 +72,44 @@ public class CoverageServiceTest { @Test public void get_hits_data() throws Exception { - service.getHitsData(COMPONENT_KEY); + service.getHits(COMPONENT_KEY, CoverageService.TYPE.UT); verify(measureDataDao).findByComponentKeyAndMetricKey(COMPONENT_KEY, CoreMetrics.COVERAGE_LINE_HITS_DATA_KEY); + + service.getHits(COMPONENT_KEY, CoverageService.TYPE.IT); + verify(measureDataDao).findByComponentKeyAndMetricKey(COMPONENT_KEY, CoreMetrics.IT_COVERAGE_LINE_HITS_DATA_KEY); + + service.getHits(COMPONENT_KEY, CoverageService.TYPE.OVERALL); + verify(measureDataDao).findByComponentKeyAndMetricKey(COMPONENT_KEY, CoreMetrics.OVERALL_COVERAGE_LINE_HITS_DATA_KEY); } @Test public void not_get_hits_data_if_no_data() throws Exception { when(measureDataDao.findByComponentKeyAndMetricKey(eq(COMPONENT_KEY), anyString())).thenReturn(null); - assertThat(service.getHitsData(COMPONENT_KEY)).isNull(); + assertThat(service.getHits(COMPONENT_KEY, CoverageService.TYPE.UT)).isEqualTo(Collections.emptyMap()); } @Test public void get_conditions_data() throws Exception { - service.getConditionsData(COMPONENT_KEY); + service.getConditions(COMPONENT_KEY, CoverageService.TYPE.UT); verify(measureDataDao).findByComponentKeyAndMetricKey(COMPONENT_KEY, CoreMetrics.CONDITIONS_BY_LINE_KEY); + + service.getConditions(COMPONENT_KEY, CoverageService.TYPE.IT); + verify(measureDataDao).findByComponentKeyAndMetricKey(COMPONENT_KEY, CoreMetrics.IT_CONDITIONS_BY_LINE_KEY); + + service.getConditions(COMPONENT_KEY, CoverageService.TYPE.OVERALL); + verify(measureDataDao).findByComponentKeyAndMetricKey(COMPONENT_KEY, CoreMetrics.OVERALL_CONDITIONS_BY_LINE_KEY); } @Test - public void get_coverered_conditions_data() throws Exception { - service.getCoveredConditionsData(COMPONENT_KEY); + public void get_covered_conditions_data() throws Exception { + service.getCoveredConditions(COMPONENT_KEY, CoverageService.TYPE.UT); verify(measureDataDao).findByComponentKeyAndMetricKey(COMPONENT_KEY, CoreMetrics.COVERED_CONDITIONS_BY_LINE_KEY); + + service.getCoveredConditions(COMPONENT_KEY, CoverageService.TYPE.IT); + verify(measureDataDao).findByComponentKeyAndMetricKey(COMPONENT_KEY, CoreMetrics.IT_COVERED_CONDITIONS_BY_LINE_KEY); + + service.getCoveredConditions(COMPONENT_KEY, CoverageService.TYPE.OVERALL); + verify(measureDataDao).findByComponentKeyAndMetricKey(COMPONENT_KEY, CoreMetrics.OVERALL_COVERED_CONDITIONS_BY_LINE_KEY); } @Test @@ -97,15 +117,17 @@ public class CoverageServiceTest { MutableTestable testable = mock(MutableTestable.class); when(snapshotPerspectives.as(MutableTestable.class, COMPONENT_KEY)).thenReturn(testable); - service.getTestCasesByLines(COMPONENT_KEY); + service.getTestCases(COMPONENT_KEY, CoverageService.TYPE.UT); verify(testable).testCasesByLines(); + + assertThat(service.getTestCases(COMPONENT_KEY, CoverageService.TYPE.IT)).isEmpty(); } @Test public void not_get_test_cases_by_lines_if_no_testable() throws Exception { when(snapshotPerspectives.as(MutableTestable.class, COMPONENT_KEY)).thenReturn(null); - assertThat(service.getTestCasesByLines(COMPONENT_KEY)).isNull(); + assertThat(service.getTestCases(COMPONENT_KEY, CoverageService.TYPE.UT)).isEqualTo(Collections.emptyMap()); } } diff --git a/sonar-server/src/test/java/org/sonar/server/test/ws/CoverageShowActionTest.java b/sonar-server/src/test/java/org/sonar/server/test/ws/CoverageShowActionTest.java index 5da2ea3b625..e4c7c881cbd 100644 --- a/sonar-server/src/test/java/org/sonar/server/test/ws/CoverageShowActionTest.java +++ b/sonar-server/src/test/java/org/sonar/server/test/ws/CoverageShowActionTest.java @@ -47,12 +47,12 @@ public class CoverageShowActionTest { @Test public void show_coverage_for_unit_test() throws Exception { String fileKey = "src/Foo.java"; - when(coverageService.getHitsData(fileKey)).thenReturn("1=1;2=1;3=0;4=1;5=1"); - when(coverageService.getTestCasesByLines(fileKey)).thenReturn(ImmutableMap.of(4, 8, 1, 2)); - when(coverageService.getConditionsData(fileKey)).thenReturn("2=3;3=2"); - when(coverageService.getCoveredConditionsData(fileKey)).thenReturn("2=1;3=2"); + when(coverageService.getHits(fileKey, CoverageService.TYPE.UT)).thenReturn(ImmutableMap.of(1, 1, 2, 1, 3, 0, 4, 1, 5 , 1)); + when(coverageService.getTestCases(fileKey, CoverageService.TYPE.UT)).thenReturn(ImmutableMap.of(4, 8, 1, 2)); + when(coverageService.getConditions(fileKey, CoverageService.TYPE.UT)).thenReturn(ImmutableMap.of(2, 3, 3, 2)); + when(coverageService.getCoveredConditions(fileKey, CoverageService.TYPE.UT)).thenReturn(ImmutableMap.of(2, 1, 3, 2)); - WsTester.TestRequest request = tester.newGetRequest("api/coverage", "show").setParam("key", fileKey); + WsTester.TestRequest request = tester.newGetRequest("api/coverage", "show").setParam("key", fileKey).setParam("type", "UT"); request.execute().assertJson(getClass(), "show_coverage.json"); } @@ -60,14 +60,38 @@ public class CoverageShowActionTest { @Test public void show_coverage_for_unit_test_with_from_and_to() throws Exception { String fileKey = "src/Foo.java"; - when(coverageService.getHitsData(fileKey)).thenReturn("1=1;2=1;3=0;4=1;5=1"); - when(coverageService.getTestCasesByLines(fileKey)).thenReturn(ImmutableMap.of(4, 8, 1, 2)); - when(coverageService.getConditionsData(fileKey)).thenReturn("2=3;3=2"); - when(coverageService.getCoveredConditionsData(fileKey)).thenReturn("2=1;3=2"); + when(coverageService.getHits(fileKey, CoverageService.TYPE.UT)).thenReturn(ImmutableMap.of(1, 1, 2, 1, 3, 0, 4, 1, 5 , 1)); + when(coverageService.getTestCases(fileKey, CoverageService.TYPE.UT)).thenReturn(ImmutableMap.of(4, 8, 1, 2)); + when(coverageService.getConditions(fileKey, CoverageService.TYPE.UT)).thenReturn(ImmutableMap.of(2, 3, 3, 2)); + when(coverageService.getCoveredConditions(fileKey, CoverageService.TYPE.UT)).thenReturn(ImmutableMap.of(2, 1, 3, 2)); - WsTester.TestRequest request = tester.newGetRequest("api/coverage", "show").setParam("key", fileKey).setParam("from", "3").setParam("to", "4"); + WsTester.TestRequest request = tester.newGetRequest("api/coverage", "show").setParam("key", fileKey).setParam("from", "3").setParam("to", "4").setParam("type", "UT"); request.execute().assertJson(getClass(), "show_coverage_with_from_and_to.json"); } + @Test + public void show_coverage_for_integration_test() throws Exception { + String fileKey = "src/Foo.java"; + when(coverageService.getHits(fileKey, CoverageService.TYPE.IT)).thenReturn(ImmutableMap.of(1, 1, 2, 1, 3, 0, 4, 1, 5 , 1)); + when(coverageService.getConditions(fileKey, CoverageService.TYPE.IT)).thenReturn(ImmutableMap.of(2, 3, 3, 2)); + when(coverageService.getCoveredConditions(fileKey, CoverageService.TYPE.IT)).thenReturn(ImmutableMap.of(2, 1, 3, 2)); + + WsTester.TestRequest request = tester.newGetRequest("api/coverage", "show").setParam("key", fileKey).setParam("type", "IT"); + + request.execute().assertJson(getClass(), "show_coverage_for_integration_test.json"); + } + + @Test + public void show_coverage_for_overall_test() throws Exception { + String fileKey = "src/Foo.java"; + when(coverageService.getHits(fileKey, CoverageService.TYPE.OVERALL)).thenReturn(ImmutableMap.of(1, 1, 2, 1, 3, 0, 4, 1, 5 , 1)); + when(coverageService.getConditions(fileKey, CoverageService.TYPE.OVERALL)).thenReturn(ImmutableMap.of(2, 3, 3, 2)); + when(coverageService.getCoveredConditions(fileKey, CoverageService.TYPE.OVERALL)).thenReturn(ImmutableMap.of(2, 1, 3, 2)); + + WsTester.TestRequest request = tester.newGetRequest("api/coverage", "show").setParam("key", fileKey).setParam("type", "OVERALL"); + + request.execute().assertJson(getClass(), "show_coverage_for_overall_test.json"); + } + } diff --git a/sonar-server/src/test/resources/org/sonar/server/test/ws/CoverageShowActionTest/show_coverage_for_integration_test.json b/sonar-server/src/test/resources/org/sonar/server/test/ws/CoverageShowActionTest/show_coverage_for_integration_test.json new file mode 100644 index 00000000000..e3930121db1 --- /dev/null +++ b/sonar-server/src/test/resources/org/sonar/server/test/ws/CoverageShowActionTest/show_coverage_for_integration_test.json @@ -0,0 +1,9 @@ +{ + "coverage": [ + [1, true, null, null, null], + [2, true, null, 3, 1], + [3, false, null, 2, 2], + [4, true, null, null, null], + [5, true, null, null, null] + ] +} diff --git a/sonar-server/src/test/resources/org/sonar/server/test/ws/CoverageShowActionTest/show_coverage_for_overall_test.json b/sonar-server/src/test/resources/org/sonar/server/test/ws/CoverageShowActionTest/show_coverage_for_overall_test.json new file mode 100644 index 00000000000..e3930121db1 --- /dev/null +++ b/sonar-server/src/test/resources/org/sonar/server/test/ws/CoverageShowActionTest/show_coverage_for_overall_test.json @@ -0,0 +1,9 @@ +{ + "coverage": [ + [1, true, null, null, null], + [2, true, null, 3, 1], + [3, false, null, 2, 2], + [4, true, null, null, null], + [5, true, null, null, null] + ] +} -- 2.39.5