Search in sources :

Example 1 with SearchHistoryResponse

use of org.sonarqube.ws.Measures.SearchHistoryResponse in project sonarqube by SonarSource.

the class SearchHistoryAction method handle.

@Override
public void handle(Request request, Response response) throws Exception {
    SearchHistoryResponse searchHistoryResponse = Stream.of(request).map(SearchHistoryAction::toWsRequest).map(search()).map(result -> new SearchHistoryResponseFactory(result).apply()).collect(MoreCollectors.toOneElement());
    writeProtobuf(searchHistoryResponse, request, response);
}
Also used : ACTION_SEARCH_HISTORY(org.sonar.server.component.ws.MeasuresWsParameters.ACTION_SEARCH_HISTORY) ComponentFinder(org.sonar.server.component.ComponentFinder) STATUS_PROCESSED(org.sonar.db.component.SnapshotDto.STATUS_PROCESSED) SORT_ORDER(org.sonar.db.component.SnapshotQuery.SORT_ORDER) KEY_BRANCH_EXAMPLE_001(org.sonar.server.ws.KeyExamples.KEY_BRANCH_EXAMPLE_001) Date(java.util.Date) PARAM_FROM(org.sonar.server.component.ws.MeasuresWsParameters.PARAM_FROM) Function(java.util.function.Function) Scopes(org.sonar.api.resources.Scopes) DbSession(org.sonar.db.DbSession) MeasureDto(org.sonar.db.measure.MeasureDto) Request(org.sonar.api.server.ws.Request) SORT_FIELD(org.sonar.db.component.SnapshotQuery.SORT_FIELD) Param(org.sonar.api.server.ws.WebService.Param) WebService(org.sonar.api.server.ws.WebService) Response(org.sonar.api.server.ws.Response) PastMeasureQuery(org.sonar.db.measure.PastMeasureQuery) MoreCollectors(org.sonar.core.util.stream.MoreCollectors) Nullable(javax.annotation.Nullable) Optional.ofNullable(java.util.Optional.ofNullable) Set(java.util.Set) PARAM_COMPONENT(org.sonar.server.component.ws.MeasuresWsParameters.PARAM_COMPONENT) DateUtils.parseStartingDateOrDateTime(org.sonar.api.utils.DateUtils.parseStartingDateOrDateTime) PARAM_PULL_REQUEST(org.sonar.server.component.ws.MeasuresWsParameters.PARAM_PULL_REQUEST) KeyExamples(org.sonar.server.ws.KeyExamples) DateUtils.parseEndingDateOrDateTime(org.sonar.api.utils.DateUtils.parseEndingDateOrDateTime) PARAM_METRICS(org.sonar.server.component.ws.MeasuresWsParameters.PARAM_METRICS) Sets(com.google.common.collect.Sets) String.format(java.lang.String.format) DbClient(org.sonar.db.DbClient) List(java.util.List) ComponentDto(org.sonar.db.component.ComponentDto) Stream(java.util.stream.Stream) UserRole(org.sonar.api.web.UserRole) MetricDto(org.sonar.db.metric.MetricDto) Qualifiers(org.sonar.api.resources.Qualifiers) SearchHistoryResponse(org.sonarqube.ws.Measures.SearchHistoryResponse) KEY_PULL_REQUEST_EXAMPLE_001(org.sonar.server.ws.KeyExamples.KEY_PULL_REQUEST_EXAMPLE_001) Change(org.sonar.api.server.ws.Change) SnapshotQuery(org.sonar.db.component.SnapshotQuery) UserSession(org.sonar.server.user.UserSession) SnapshotDto(org.sonar.db.component.SnapshotDto) CheckForNull(javax.annotation.CheckForNull) PARAM_BRANCH(org.sonar.server.component.ws.MeasuresWsParameters.PARAM_BRANCH) PARAM_TO(org.sonar.server.component.ws.MeasuresWsParameters.PARAM_TO) WsUtils.writeProtobuf(org.sonar.server.ws.WsUtils.writeProtobuf) SearchHistoryResponse(org.sonarqube.ws.Measures.SearchHistoryResponse)

Example 2 with SearchHistoryResponse

use of org.sonarqube.ws.Measures.SearchHistoryResponse in project sonarqube by SonarSource.

the class SearchHistoryActionTest method analyses_but_no_measure.

@Test
public void analyses_but_no_measure() {
    project = db.components().insertPrivateProject();
    analysis = db.components().insertSnapshot(project);
    userSession.addProjectPermission(UserRole.USER, project);
    SearchHistoryRequest request = SearchHistoryRequest.builder().setComponent(project.getDbKey()).setMetrics(singletonList(complexityMetric.getKey())).build();
    SearchHistoryResponse result = call(request);
    assertThat(result.getPaging()).extracting(Paging::getPageIndex, Paging::getPageSize, Paging::getTotal).containsExactly(1, 100, 1);
    assertThat(result.getMeasuresList()).hasSize(1);
    assertThat(result.getMeasures(0).getHistoryList()).extracting(HistoryValue::hasDate, HistoryValue::hasValue).containsExactly(tuple(true, false));
}
Also used : SearchHistoryResponse(org.sonarqube.ws.Measures.SearchHistoryResponse) SearchHistoryRequest(org.sonar.server.measure.ws.SearchHistoryAction.SearchHistoryRequest) Test(org.junit.Test)

Example 3 with SearchHistoryResponse

use of org.sonarqube.ws.Measures.SearchHistoryResponse in project sonarqube by SonarSource.

the class SearchHistoryActionTest method branch.

@Test
public void branch() {
    ComponentDto project = db.components().insertPrivateProject();
    userSession.addProjectPermission(UserRole.USER, project);
    ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setKey("my_branch"));
    ComponentDto file = db.components().insertComponent(newFileDto(branch));
    SnapshotDto analysis = db.components().insertSnapshot(branch);
    MeasureDto measure = db.measures().insertMeasure(file, analysis, nclocMetric, m -> m.setValue(2d));
    SearchHistoryResponse result = ws.newRequest().setParam(PARAM_COMPONENT, file.getKey()).setParam(PARAM_BRANCH, "my_branch").setParam(PARAM_METRICS, "ncloc").executeProtobuf(SearchHistoryResponse.class);
    assertThat(result.getMeasuresList()).extracting(HistoryMeasure::getMetric).hasSize(1);
    HistoryMeasure historyMeasure = result.getMeasures(0);
    assertThat(historyMeasure.getMetric()).isEqualTo(nclocMetric.getKey());
    assertThat(historyMeasure.getHistoryList()).extracting(m -> parseDouble(m.getValue())).containsExactlyInAnyOrder(measure.getValue());
}
Also used : MeasureDto(org.sonar.db.measure.MeasureDto) MeasureTesting.newMeasureDto(org.sonar.db.measure.MeasureTesting.newMeasureDto) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) DbSession(org.sonar.db.DbSession) MeasureDto(org.sonar.db.measure.MeasureDto) PULL_REQUEST(org.sonar.db.component.BranchType.PULL_REQUEST) Collections.singletonList(java.util.Collections.singletonList) MetricTesting.newMetricDto(org.sonar.db.metric.MetricTesting.newMetricDto) Param(org.sonar.api.server.ws.WebService.Param) WebService(org.sonar.api.server.ws.WebService) SnapshotTesting.newAnalysis(org.sonar.db.component.SnapshotTesting.newAnalysis) Arrays.asList(java.util.Arrays.asList) DateUtils.formatDateTime(org.sonar.api.utils.DateUtils.formatDateTime) Double.parseDouble(java.lang.Double.parseDouble) DbTester(org.sonar.db.DbTester) System2(org.sonar.api.utils.System2) HistoryMeasure(org.sonarqube.ws.Measures.SearchHistoryResponse.HistoryMeasure) NotFoundException(org.sonar.server.exceptions.NotFoundException) String.format(java.lang.String.format) DbClient(org.sonar.db.DbClient) List(java.util.List) ComponentDto(org.sonar.db.component.ComponentDto) ForbiddenException(org.sonar.server.exceptions.ForbiddenException) PARAM_TO(org.sonar.server.component.ws.MeasuresWsParameters.PARAM_TO) HistoryValue(org.sonarqube.ws.Measures.SearchHistoryResponse.HistoryValue) ComponentTesting.newFileDto(org.sonar.db.component.ComponentTesting.newFileDto) SearchHistoryRequest(org.sonar.server.measure.ws.SearchHistoryAction.SearchHistoryRequest) PARAM_FROM(org.sonar.server.component.ws.MeasuresWsParameters.PARAM_FROM) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) MeasureTesting.newMeasureDto(org.sonar.db.measure.MeasureTesting.newMeasureDto) TestComponentFinder(org.sonar.server.component.TestComponentFinder) MoreCollectors(org.sonar.core.util.stream.MoreCollectors) Before(org.junit.Before) UserSessionRule(org.sonar.server.tester.UserSessionRule) LongStream(java.util.stream.LongStream) Assertions.tuple(org.assertj.core.api.Assertions.tuple) Optional.ofNullable(java.util.Optional.ofNullable) TestRequest(org.sonar.server.ws.TestRequest) JsonAssert.assertJson(org.sonar.test.JsonAssert.assertJson) Paging(org.sonarqube.ws.Common.Paging) Test(org.junit.Test) STATUS_UNPROCESSED(org.sonar.db.component.SnapshotDto.STATUS_UNPROCESSED) PARAM_COMPONENT(org.sonar.server.component.ws.MeasuresWsParameters.PARAM_COMPONENT) WsActionTester(org.sonar.server.ws.WsActionTester) PARAM_PULL_REQUEST(org.sonar.server.component.ws.MeasuresWsParameters.PARAM_PULL_REQUEST) ValueType(org.sonar.api.measures.Metric.ValueType) PARAM_METRICS(org.sonar.server.component.ws.MeasuresWsParameters.PARAM_METRICS) ComponentTesting.newPrivateProjectDto(org.sonar.db.component.ComponentTesting.newPrivateProjectDto) Rule(org.junit.Rule) UserRole(org.sonar.api.web.UserRole) MetricDto(org.sonar.db.metric.MetricDto) SearchHistoryResponse(org.sonarqube.ws.Measures.SearchHistoryResponse) DateUtils.parseDateTime(org.sonar.api.utils.DateUtils.parseDateTime) ComponentDbTester.toProjectDto(org.sonar.db.component.ComponentDbTester.toProjectDto) SnapshotDto(org.sonar.db.component.SnapshotDto) PARAM_BRANCH(org.sonar.server.component.ws.MeasuresWsParameters.PARAM_BRANCH) SnapshotDto(org.sonar.db.component.SnapshotDto) SearchHistoryResponse(org.sonarqube.ws.Measures.SearchHistoryResponse) ComponentDto(org.sonar.db.component.ComponentDto) HistoryMeasure(org.sonarqube.ws.Measures.SearchHistoryResponse.HistoryMeasure) Test(org.junit.Test)

Example 4 with SearchHistoryResponse

use of org.sonarqube.ws.Measures.SearchHistoryResponse in project sonarqube by SonarSource.

the class SearchHistoryActionTest method return_measures.

@Test
public void return_measures() {
    SnapshotDto laterAnalysis = dbClient.snapshotDao().insert(dbSession, newAnalysis(project).setCreatedAt(analysis.getCreatedAt() + 42_000));
    ComponentDto file = db.components().insertComponent(newFileDto(project));
    dbClient.measureDao().insert(dbSession, newMeasureDto(complexityMetric, project, analysis).setValue(101d), newMeasureDto(complexityMetric, project, laterAnalysis).setValue(100d), newMeasureDto(complexityMetric, file, analysis).setValue(42d), newMeasureDto(nclocMetric, project, analysis).setValue(201d), newMeasureDto(newViolationMetric, project, analysis).setVariation(5d), newMeasureDto(newViolationMetric, project, laterAnalysis).setVariation(10d));
    db.commit();
    SearchHistoryRequest request = SearchHistoryRequest.builder().setComponent(project.getDbKey()).setMetrics(asList(complexityMetric.getKey(), nclocMetric.getKey(), newViolationMetric.getKey())).build();
    SearchHistoryResponse result = call(request);
    assertThat(result.getPaging()).extracting(Paging::getPageIndex, Paging::getPageSize, Paging::getTotal).containsExactly(1, 100, 2);
    assertThat(result.getMeasuresList()).extracting(HistoryMeasure::getMetric).hasSize(3).containsExactly(complexityMetric.getKey(), nclocMetric.getKey(), newViolationMetric.getKey());
    String analysisDate = formatDateTime(analysis.getCreatedAt());
    String laterAnalysisDate = formatDateTime(laterAnalysis.getCreatedAt());
    // complexity measures
    HistoryMeasure complexityMeasures = result.getMeasures(0);
    assertThat(complexityMeasures.getMetric()).isEqualTo(complexityMetric.getKey());
    assertThat(complexityMeasures.getHistoryList()).extracting(HistoryValue::getDate, HistoryValue::getValue).containsExactly(tuple(analysisDate, "101"), tuple(laterAnalysisDate, "100"));
    // ncloc measures
    HistoryMeasure nclocMeasures = result.getMeasures(1);
    assertThat(nclocMeasures.getMetric()).isEqualTo(nclocMetric.getKey());
    assertThat(nclocMeasures.getHistoryList()).extracting(HistoryValue::getDate, HistoryValue::getValue, HistoryValue::hasValue).containsExactly(tuple(analysisDate, "201", true), tuple(laterAnalysisDate, "", false));
    // new_violation measures
    HistoryMeasure newViolationMeasures = result.getMeasures(2);
    assertThat(newViolationMeasures.getMetric()).isEqualTo(newViolationMetric.getKey());
    assertThat(newViolationMeasures.getHistoryList()).extracting(HistoryValue::getDate, HistoryValue::getValue).containsExactly(tuple(analysisDate, "5"), tuple(laterAnalysisDate, "10"));
}
Also used : SnapshotDto(org.sonar.db.component.SnapshotDto) SearchHistoryResponse(org.sonarqube.ws.Measures.SearchHistoryResponse) ComponentDto(org.sonar.db.component.ComponentDto) HistoryMeasure(org.sonarqube.ws.Measures.SearchHistoryResponse.HistoryMeasure) SearchHistoryRequest(org.sonar.server.measure.ws.SearchHistoryAction.SearchHistoryRequest) Test(org.junit.Test)

Example 5 with SearchHistoryResponse

use of org.sonarqube.ws.Measures.SearchHistoryResponse in project sonarqube by SonarSource.

the class SearchHistoryActionTest method inclusive_from_and_to_dates.

@Test
public void inclusive_from_and_to_dates() {
    project = db.components().insertPrivateProject();
    userSession.addProjectPermission(UserRole.USER, project);
    List<String> analysisDates = LongStream.rangeClosed(1, 9).mapToObj(i -> dbClient.snapshotDao().insert(dbSession, newAnalysis(project).setCreatedAt(System2.INSTANCE.now() + i * 1_000_000_000L))).peek(a -> dbClient.measureDao().insert(dbSession, newMeasureDto(complexityMetric, project, a).setValue(Double.valueOf(a.getCreatedAt())))).map(a -> formatDateTime(a.getCreatedAt())).collect(MoreCollectors.toList());
    db.commit();
    SearchHistoryRequest request = SearchHistoryRequest.builder().setComponent(project.getDbKey()).setMetrics(asList(complexityMetric.getKey(), nclocMetric.getKey(), newViolationMetric.getKey())).setFrom(analysisDates.get(1)).setTo(analysisDates.get(3)).build();
    SearchHistoryResponse result = call(request);
    assertThat(result.getPaging()).extracting(Paging::getPageIndex, Paging::getPageSize, Paging::getTotal).containsExactly(1, 100, 3);
    assertThat(result.getMeasures(0).getHistoryList()).extracting(HistoryValue::getDate).containsExactly(analysisDates.get(1), analysisDates.get(2), analysisDates.get(3));
}
Also used : Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) DbSession(org.sonar.db.DbSession) MeasureDto(org.sonar.db.measure.MeasureDto) PULL_REQUEST(org.sonar.db.component.BranchType.PULL_REQUEST) Collections.singletonList(java.util.Collections.singletonList) MetricTesting.newMetricDto(org.sonar.db.metric.MetricTesting.newMetricDto) Param(org.sonar.api.server.ws.WebService.Param) WebService(org.sonar.api.server.ws.WebService) SnapshotTesting.newAnalysis(org.sonar.db.component.SnapshotTesting.newAnalysis) Arrays.asList(java.util.Arrays.asList) DateUtils.formatDateTime(org.sonar.api.utils.DateUtils.formatDateTime) Double.parseDouble(java.lang.Double.parseDouble) DbTester(org.sonar.db.DbTester) System2(org.sonar.api.utils.System2) HistoryMeasure(org.sonarqube.ws.Measures.SearchHistoryResponse.HistoryMeasure) NotFoundException(org.sonar.server.exceptions.NotFoundException) String.format(java.lang.String.format) DbClient(org.sonar.db.DbClient) List(java.util.List) ComponentDto(org.sonar.db.component.ComponentDto) ForbiddenException(org.sonar.server.exceptions.ForbiddenException) PARAM_TO(org.sonar.server.component.ws.MeasuresWsParameters.PARAM_TO) HistoryValue(org.sonarqube.ws.Measures.SearchHistoryResponse.HistoryValue) ComponentTesting.newFileDto(org.sonar.db.component.ComponentTesting.newFileDto) SearchHistoryRequest(org.sonar.server.measure.ws.SearchHistoryAction.SearchHistoryRequest) PARAM_FROM(org.sonar.server.component.ws.MeasuresWsParameters.PARAM_FROM) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) MeasureTesting.newMeasureDto(org.sonar.db.measure.MeasureTesting.newMeasureDto) TestComponentFinder(org.sonar.server.component.TestComponentFinder) MoreCollectors(org.sonar.core.util.stream.MoreCollectors) Before(org.junit.Before) UserSessionRule(org.sonar.server.tester.UserSessionRule) LongStream(java.util.stream.LongStream) Assertions.tuple(org.assertj.core.api.Assertions.tuple) Optional.ofNullable(java.util.Optional.ofNullable) TestRequest(org.sonar.server.ws.TestRequest) JsonAssert.assertJson(org.sonar.test.JsonAssert.assertJson) Paging(org.sonarqube.ws.Common.Paging) Test(org.junit.Test) STATUS_UNPROCESSED(org.sonar.db.component.SnapshotDto.STATUS_UNPROCESSED) PARAM_COMPONENT(org.sonar.server.component.ws.MeasuresWsParameters.PARAM_COMPONENT) WsActionTester(org.sonar.server.ws.WsActionTester) PARAM_PULL_REQUEST(org.sonar.server.component.ws.MeasuresWsParameters.PARAM_PULL_REQUEST) ValueType(org.sonar.api.measures.Metric.ValueType) PARAM_METRICS(org.sonar.server.component.ws.MeasuresWsParameters.PARAM_METRICS) ComponentTesting.newPrivateProjectDto(org.sonar.db.component.ComponentTesting.newPrivateProjectDto) Rule(org.junit.Rule) UserRole(org.sonar.api.web.UserRole) MetricDto(org.sonar.db.metric.MetricDto) SearchHistoryResponse(org.sonarqube.ws.Measures.SearchHistoryResponse) DateUtils.parseDateTime(org.sonar.api.utils.DateUtils.parseDateTime) ComponentDbTester.toProjectDto(org.sonar.db.component.ComponentDbTester.toProjectDto) SnapshotDto(org.sonar.db.component.SnapshotDto) PARAM_BRANCH(org.sonar.server.component.ws.MeasuresWsParameters.PARAM_BRANCH) SearchHistoryResponse(org.sonarqube.ws.Measures.SearchHistoryResponse) SearchHistoryRequest(org.sonar.server.measure.ws.SearchHistoryAction.SearchHistoryRequest) Test(org.junit.Test)

Aggregations

SearchHistoryResponse (org.sonarqube.ws.Measures.SearchHistoryResponse)12 Test (org.junit.Test)11 SearchHistoryRequest (org.sonar.server.measure.ws.SearchHistoryAction.SearchHistoryRequest)11 ComponentDto (org.sonar.db.component.ComponentDto)7 SnapshotDto (org.sonar.db.component.SnapshotDto)6 HistoryMeasure (org.sonarqube.ws.Measures.SearchHistoryResponse.HistoryMeasure)6 String.format (java.lang.String.format)5 List (java.util.List)5 Optional.ofNullable (java.util.Optional.ofNullable)5 WebService (org.sonar.api.server.ws.WebService)5 Param (org.sonar.api.server.ws.WebService.Param)5 UserRole (org.sonar.api.web.UserRole)5 MoreCollectors (org.sonar.core.util.stream.MoreCollectors)5 DbClient (org.sonar.db.DbClient)5 DbSession (org.sonar.db.DbSession)5 MeasureDto (org.sonar.db.measure.MeasureDto)5 MetricDto (org.sonar.db.metric.MetricDto)5 Double.parseDouble (java.lang.Double.parseDouble)4 Arrays.asList (java.util.Arrays.asList)4 Collections.singletonList (java.util.Collections.singletonList)4