diff options
author | Teryk Bellahsene <teryk.bellahsene@sonarsource.com> | 2017-01-24 14:55:29 +0100 |
---|---|---|
committer | Teryk Bellahsene <teryk.bellahsene@sonarsource.com> | 2017-01-24 17:37:23 +0100 |
commit | f55c90fa06e82cd70639eda3c2a6f686a13fbee7 (patch) | |
tree | 6dbcb0d7462aec99c2412e77aebafb0210d24ac1 /it | |
parent | a9c56c11f9889888311e1be3a1a100321c3f1ac0 (diff) | |
download | sonarqube-f55c90fa06e82cd70639eda3c2a6f686a13fbee7.tar.gz sonarqube-f55c90fa06e82cd70639eda3c2a6f686a13fbee7.zip |
SONAR-7305 Create WS api/measures/search_history
Diffstat (limited to 'it')
-rw-r--r-- | it/it-tests/src/test/java/it/dbCleaner/PurgeTest.java | 22 | ||||
-rw-r--r-- | it/it-tests/src/test/java/it/measureHistory/TimeMachineTest.java | 116 |
2 files changed, 66 insertions, 72 deletions
diff --git a/it/it-tests/src/test/java/it/dbCleaner/PurgeTest.java b/it/it-tests/src/test/java/it/dbCleaner/PurgeTest.java index 73501238752..1095e543667 100644 --- a/it/it-tests/src/test/java/it/dbCleaner/PurgeTest.java +++ b/it/it-tests/src/test/java/it/dbCleaner/PurgeTest.java @@ -35,10 +35,12 @@ import org.junit.ClassRule; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ErrorCollector; -import org.sonarqube.ws.client.GetRequest; -import org.sonarqube.ws.client.WsResponse; +import org.sonarqube.ws.WsMeasures; +import org.sonarqube.ws.WsMeasures.SearchHistoryResponse.HistoryValue; +import org.sonarqube.ws.client.measure.SearchHistoryRequest; import util.ItUtils; +import static java.util.Collections.singletonList; import static org.apache.commons.lang.time.DateUtils.addDays; import static org.assertj.core.api.Assertions.assertThat; import static org.hamcrest.Matchers.equalTo; @@ -205,14 +207,12 @@ public class PurgeTest { runProjectAnalysis(orchestrator, PROJECT_SAMPLE_PATH); // Check that only analysis from last thursday is kept (as it's the last one from previous week) - WsResponse response = newAdminWsClient(orchestrator).wsConnector().call( - new GetRequest("/api/timemachine/index") - .setParam("resource", PROJECT_KEY) - .setParam("metrics", "ncloc")) - .failIfNotSuccessful(); - String content = response.content(); - assertThat(content).contains(lastThursdayFormatted); - assertThat(content).doesNotContain(lastWednesdayFormatted); + WsMeasures.SearchHistoryResponse response = newAdminWsClient(orchestrator).measures().searchHistory(SearchHistoryRequest.builder() + .setComponent(PROJECT_KEY) + .setMetrics(singletonList("ncloc")) + .build()); + assertThat(response.getMeasuresCount()).isEqualTo(1); + assertThat(response.getMeasuresList().get(0).getHistoryList()).extracting(HistoryValue::getDate).doesNotContain(lastWednesdayFormatted, lastThursdayFormatted); } /** @@ -279,7 +279,7 @@ public class PurgeTest { scan(PROJECT_SAMPLE_PATH, ONE_DAY_AGO); // second analysis as NEW_* metrics - assertThat(count(COUNT_FILE_MEASURES)).isLessThan( 2 * fileMeasures); + assertThat(count(COUNT_FILE_MEASURES)).isLessThan(2 * fileMeasures); assertThat(count(COUNT_DIR_MEASURES)).isGreaterThan(2 * dirMeasures); } diff --git a/it/it-tests/src/test/java/it/measureHistory/TimeMachineTest.java b/it/it-tests/src/test/java/it/measureHistory/TimeMachineTest.java index 8e080218880..7c1434c68a2 100644 --- a/it/it-tests/src/test/java/it/measureHistory/TimeMachineTest.java +++ b/it/it-tests/src/test/java/it/measureHistory/TimeMachineTest.java @@ -24,33 +24,40 @@ import com.sonar.orchestrator.build.BuildResult; import com.sonar.orchestrator.build.SonarScanner; import com.sonar.orchestrator.locator.FileLocation; import it.Category1Suite; +import java.util.Arrays; import java.util.Date; import java.util.Map; import org.junit.AfterClass; import org.junit.BeforeClass; import org.junit.ClassRule; import org.junit.Test; -import org.sonar.wsclient.services.TimeMachine; -import org.sonar.wsclient.services.TimeMachineCell; -import org.sonar.wsclient.services.TimeMachineQuery; import org.sonarqube.ws.WsMeasures.Measure; +import org.sonarqube.ws.WsMeasures.SearchHistoryResponse; +import org.sonarqube.ws.WsMeasures.SearchHistoryResponse.HistoryValue; +import org.sonarqube.ws.client.measure.MeasuresService; +import org.sonarqube.ws.client.measure.SearchHistoryRequest; import util.ItUtils; import util.ItUtils.ComponentNavigation; -import static java.lang.Double.parseDouble; +import static java.util.Collections.singletonList; import static org.assertj.core.api.Assertions.assertThat; +import static util.ItUtils.formatDate; import static util.ItUtils.getComponentNavigation; import static util.ItUtils.getMeasuresByMetricKey; import static util.ItUtils.getMeasuresWithVariationsByMetricKey; +import static util.ItUtils.newAdminWsClient; import static util.ItUtils.projectDir; import static util.ItUtils.setServerProperty; public class TimeMachineTest { private static final String PROJECT = "sample"; + private static final String FIRST_ANALYSIS_DATE = "2014-10-19"; + private static final String SECOND_ANALYSIS_DATE = "2014-11-13"; @ClassRule public static Orchestrator orchestrator = Category1Suite.ORCHESTRATOR; + private static MeasuresService wsMeasures; @BeforeClass public static void initialize() { @@ -59,8 +66,10 @@ public class TimeMachineTest { orchestrator.getServer().restoreProfile(FileLocation.ofClasspath("/measureHistory/one-issue-per-line-profile.xml")); orchestrator.getServer().provisionProject("sample", "Sample"); orchestrator.getServer().associateProjectToQualityProfile("sample", "xoo", "one-issue-per-line"); - analyzeProject("measure/xoo-history-v1", "2014-10-19"); - analyzeProject("measure/xoo-history-v2", "2014-11-13"); + analyzeProject("measure/xoo-history-v1", FIRST_ANALYSIS_DATE); + analyzeProject("measure/xoo-history-v2", SECOND_ANALYSIS_DATE); + + wsMeasures = newAdminWsClient(orchestrator).measures(); } public static void initPeriods() { @@ -87,73 +96,38 @@ public class TimeMachineTest { @Test public void testHistoryOfIssues() { - TimeMachineQuery query = TimeMachineQuery.createForMetrics(PROJECT, "blocker_violations", "critical_violations", "major_violations", - "minor_violations", "info_violations"); - TimeMachine timemachine = orchestrator.getServer().getWsClient().find(query); - assertThat(timemachine.getCells().length).isEqualTo(2); - - TimeMachineCell cell1 = timemachine.getCells()[0]; - TimeMachineCell cell2 = timemachine.getCells()[1]; - - assertThat(cell1.getDate().getMonth()).isEqualTo(9); - assertThat(cell1.getValues()).isEqualTo(new Object[] {0L, 0L, 0L, 26L, 0L}); - - assertThat(cell2.getDate().getMonth()).isEqualTo(10); - assertThat(cell2.getValues()).isEqualTo(new Object[] {0L, 0L, 0L, 43L, 0L}); + SearchHistoryResponse response = searchHistory("blocker_violations", "critical_violations", "info_violations", "major_violations", "minor_violations"); + assertThat(response.getPaging().getTotal()).isEqualTo(2); + + assertHistory(response, "blocker_violations", "0", "0"); + assertHistory(response, "critical_violations", "0", "0"); + assertHistory(response, "info_violations", "0", "0"); + assertHistory(response, "major_violations", "0", "0"); + assertHistory(response, "minor_violations", "26", "43"); } @Test public void testHistoryOfMeasures() { - TimeMachineQuery query = TimeMachineQuery.createForMetrics(PROJECT, "lines", "ncloc"); - TimeMachine timemachine = orchestrator.getServer().getWsClient().find(query); - assertThat(timemachine.getCells().length).isEqualTo(2); - - TimeMachineCell cell1 = timemachine.getCells()[0]; - TimeMachineCell cell2 = timemachine.getCells()[1]; - - assertThat(cell1.getDate().getMonth()).isEqualTo(9); - assertThat(cell1.getValues()).isEqualTo(new Object[] {26L, 24L}); - - assertThat(cell2.getDate().getMonth()).isEqualTo(10); - assertThat(cell2.getValues()).isEqualTo(new Object[] {43L, 40L}); - } - - @Test - public void unknownMetrics() { - TimeMachine timemachine = orchestrator.getServer().getWsClient().find(TimeMachineQuery.createForMetrics(PROJECT, "notfound")); - assertThat(timemachine.getCells().length).isEqualTo(0); - - timemachine = orchestrator.getServer().getWsClient().find(TimeMachineQuery.createForMetrics(PROJECT, "lines", "notfound")); - assertThat(timemachine.getCells().length).isEqualTo(2); - for (TimeMachineCell cell : timemachine.getCells()) { - assertThat(cell.getValues().length).isEqualTo(1); - assertThat(cell.getValues()[0]).isInstanceOf(Long.class); - } + SearchHistoryResponse response = searchHistory("lines", "ncloc"); - timemachine = orchestrator.getServer().getWsClient().find(TimeMachineQuery.createForMetrics(PROJECT)); - assertThat(timemachine.getCells().length).isEqualTo(0); + assertThat(response.getPaging().getTotal()).isEqualTo(2); + assertHistory(response, "lines", "26", "43"); + assertHistory(response, "ncloc", "24", "40"); } @Test public void noDataForInterval() { Date now = new Date(); - TimeMachine timemachine = orchestrator.getServer().getWsClient().find(TimeMachineQuery.createForMetrics(PROJECT, "lines").setFrom(now).setTo(now)); - assertThat(timemachine.getCells().length).isEqualTo(0); - } - @Test - public void unknownResource() { - TimeMachine timemachine = orchestrator.getServer().getWsClient().find(TimeMachineQuery.createForMetrics("notfound:notfound", "lines")); - assertThat(timemachine).isNull(); - } + SearchHistoryResponse response = wsMeasures.searchHistory(SearchHistoryRequest.builder() + .setComponent(PROJECT) + .setMetrics(singletonList("lines")) + .setFrom(formatDate(now)) + .setTo(formatDate(now)) + .build()); - @Test - public void test_measure_variations() { - Map<String, Measure> measures = getMeasuresWithVariationsByMetricKey(orchestrator, PROJECT, "files", "ncloc", "violations"); - // variations from previous analysis - assertThat(parseDouble(measures.get("files").getPeriods().getPeriodsValue(0).getValue())).isEqualTo(1.0); - assertThat(parseDouble(measures.get("ncloc").getPeriods().getPeriodsValue(0).getValue())).isEqualTo(16.0); - assertThat(parseDouble(measures.get("violations").getPeriods().getPeriodsValue(0).getValue())).isGreaterThan(0.0); + assertThat(response.getPaging().getTotal()).isEqualTo(0); + assertThat(response.getMeasures(0).getHistoryList()).isEmpty(); } /** @@ -164,9 +138,29 @@ public class TimeMachineTest { Map<String, Measure> measures = getMeasuresWithVariationsByMetricKey(orchestrator, PROJECT, "violations", "new_violations"); assertThat(measures.get("violations")).isNotNull(); assertThat(measures.get("new_violations")).isNotNull(); + SearchHistoryResponse response = searchHistory("new_violations"); + assertThat(response.getMeasures(0).getHistoryCount()).isGreaterThan(0); measures = getMeasuresByMetricKey(orchestrator, PROJECT, "violations", "new_violations"); assertThat(measures.get("violations")).isNotNull(); assertThat(measures.get("new_violations")).isNull(); } + + private static SearchHistoryResponse searchHistory(String... metrics) { + return wsMeasures.searchHistory(SearchHistoryRequest.builder() + .setComponent(PROJECT) + .setMetrics(Arrays.asList(metrics)) + .build()); + } + + private static void assertHistory(SearchHistoryResponse response, String metric, String... expectedMeasures) { + for (SearchHistoryResponse.HistoryMeasure measures : response.getMeasuresList()) { + if (metric.equals(measures.getMetric())) { + assertThat(measures.getHistoryList()).extracting(HistoryValue::getValue).containsExactly(expectedMeasures); + return; + } + } + + throw new IllegalArgumentException("Metric not found"); + } } |