@Test
  public void set_variation() {
    // Project
    SnapshotDto period1ProjectSnapshot = createForProject(PROJECT_DTO);
    dbClient.snapshotDao().insert(session, period1ProjectSnapshot);
    dbClient
        .measureDao()
        .insert(
            session,
            newMeasureDto(
                ISSUES_METRIC.getId(), PROJECT_DTO.getId(), period1ProjectSnapshot.getId(), 60d));

    // Directory
    ComponentDto directoryDto = ComponentTesting.newDirectory(PROJECT_DTO, "dir");
    dbClient.componentDao().insert(session, directoryDto);
    SnapshotDto period1DirectorySnapshot = createForComponent(directoryDto, period1ProjectSnapshot);
    dbClient.snapshotDao().insert(session, period1DirectorySnapshot);
    dbClient
        .measureDao()
        .insert(
            session,
            newMeasureDto(
                ISSUES_METRIC.getId(),
                directoryDto.getId(),
                period1DirectorySnapshot.getId(),
                10d));
    session.commit();

    periodsHolder.setPeriods(newPeriod(1, period1ProjectSnapshot));

    Component directory =
        ReportComponent.builder(Component.Type.DIRECTORY, 2).setUuid(directoryDto.uuid()).build();
    Component project =
        ReportComponent.builder(Component.Type.PROJECT, 1)
            .setUuid(PROJECT_DTO.uuid())
            .addChildren(directory)
            .build();
    treeRootHolder.setRoot(project);

    addRawMeasure(project, ISSUES_METRIC, Measure.newMeasureBuilder().create(80, null));
    addRawMeasure(directory, ISSUES_METRIC, Measure.newMeasureBuilder().create(20, null));

    underTest.execute();

    assertThat(
            measureRepository
                .getRawMeasure(project, ISSUES_METRIC)
                .get()
                .getVariations()
                .getVariation1())
        .isEqualTo(20d);
    assertThat(
            measureRepository
                .getRawMeasure(directory, ISSUES_METRIC)
                .get()
                .getVariations()
                .getVariation1())
        .isEqualTo(10d);
  }
  @Test
  public void set_variations_on_all_periods() {
    SnapshotDto period1ProjectSnapshot = createForProject(PROJECT_DTO).setLast(false);
    SnapshotDto period2ProjectSnapshot = createForProject(PROJECT_DTO).setLast(false);
    SnapshotDto period3ProjectSnapshot = createForProject(PROJECT_DTO).setLast(false);
    SnapshotDto period4ProjectSnapshot = createForProject(PROJECT_DTO).setLast(false);
    SnapshotDto period5ProjectSnapshot = createForProject(PROJECT_DTO).setLast(false);
    dbClient
        .snapshotDao()
        .insert(
            session,
            period1ProjectSnapshot,
            period2ProjectSnapshot,
            period3ProjectSnapshot,
            period4ProjectSnapshot,
            period5ProjectSnapshot);

    dbClient
        .measureDao()
        .insert(
            session,
            newMeasureDto(
                ISSUES_METRIC.getId(), PROJECT_DTO.getId(), period1ProjectSnapshot.getId(), 0d),
            newMeasureDto(
                ISSUES_METRIC.getId(), PROJECT_DTO.getId(), period2ProjectSnapshot.getId(), 20d),
            newMeasureDto(
                ISSUES_METRIC.getId(), PROJECT_DTO.getId(), period3ProjectSnapshot.getId(), 40d),
            newMeasureDto(
                ISSUES_METRIC.getId(), PROJECT_DTO.getId(), period4ProjectSnapshot.getId(), 80d),
            newMeasureDto(
                ISSUES_METRIC.getId(), PROJECT_DTO.getId(), period5ProjectSnapshot.getId(), 100d));
    session.commit();

    periodsHolder.setPeriods(
        newPeriod(1, period1ProjectSnapshot),
        newPeriod(2, period2ProjectSnapshot),
        newPeriod(3, period3ProjectSnapshot),
        newPeriod(4, period4ProjectSnapshot),
        newPeriod(5, period5ProjectSnapshot));

    treeRootHolder.setRoot(PROJECT);

    addRawMeasure(PROJECT, ISSUES_METRIC, Measure.newMeasureBuilder().create(80, null));

    underTest.execute();

    assertThat(measureRepository.getRawMeasures(PROJECT).keys()).hasSize(1);

    Measure measure = measureRepository.getRawMeasure(PROJECT, ISSUES_METRIC).get();
    assertThat(measure.hasVariations()).isTrue();
    assertThat(measure.getVariations().getVariation1()).isEqualTo(80d);
    assertThat(measure.getVariations().getVariation2()).isEqualTo(60d);
    assertThat(measure.getVariations().getVariation3()).isEqualTo(40d);
    assertThat(measure.getVariations().getVariation4()).isEqualTo(0d);
    assertThat(measure.getVariations().getVariation5()).isEqualTo(-20d);
  }
  @Test
  public void return_undefined_status_if_measure_is_not_found() {
    ComponentDto project = newProjectDto("project-uuid");
    dbClient.componentDao().insert(dbSession, project);
    SnapshotDto snapshot = dbClient.snapshotDao().insert(dbSession, newSnapshotForProject(project));
    dbSession.commit();

    ProjectStatusWsResponse result = newRequest(snapshot.getId().toString());

    assertThat(result.getProjectStatus().getStatus()).isEqualTo(Status.NONE);
    assertThat(result.getProjectStatus().getConditionsCount()).isEqualTo(0);
  }
Example #4
0
  private long insertNewProjectInDbAndReturnSnapshotId(int id) {
    String suffix = String.valueOf(id);
    ComponentDto project =
        ComponentTesting.newProjectDto("project-uuid-" + suffix).setKey("project-key-" + suffix);
    RuleDto rule = RuleTesting.newDto(RuleKey.of("sonarqube", "rule-" + suffix));
    dbClient.ruleDao().insert(dbSession, rule);
    IssueDto issue = IssueTesting.newDto(rule, project, project).setKee("issue-key-" + suffix);
    dbClient.componentDao().insert(dbSession, project);
    SnapshotDto snapshot =
        dbClient.snapshotDao().insert(dbSession, SnapshotTesting.newAnalysis(project));
    dbClient.issueDao().insert(dbSession, issue);
    dbSession.commit();

    return snapshot.getId();
  }
  @Test
  public void do_nothing_when_no_raw_measure() {
    SnapshotDto period1ProjectSnapshot = createForProject(PROJECT_DTO);
    dbClient.snapshotDao().insert(session, period1ProjectSnapshot);
    dbClient
        .measureDao()
        .insert(
            session,
            newMeasureDto(
                ISSUES_METRIC.getId(), PROJECT_DTO.getId(), period1ProjectSnapshot.getId(), 60d));
    session.commit();

    periodsHolder.setPeriods(newPeriod(1, period1ProjectSnapshot));

    treeRootHolder.setRoot(PROJECT);

    underTest.execute();

    assertThat(measureRepository.getRawMeasures(PROJECT).keys()).isEmpty();
  }
  @Test
  public void return_undefined_status_if_measure_data_is_not_well_formatted() {
    userSession.setGlobalPermissions(GlobalPermissions.SCAN_EXECUTION);
    ComponentDto project = newProjectDto("project-uuid");
    dbClient.componentDao().insert(dbSession, project);
    SnapshotDto snapshot = dbClient.snapshotDao().insert(dbSession, newSnapshotForProject(project));
    MetricDto metric =
        dbClient
            .metricDao()
            .insert(
                dbSession,
                newMetricDto().setEnabled(true).setKey(CoreMetrics.QUALITY_GATE_DETAILS_KEY));
    MeasureDto measure = newMeasureDto(metric, snapshot.getId()).setData("");
    dbClient.measureDao().insert(dbSession, measure);
    dbSession.commit();

    ProjectStatusWsResponse result = newRequest(String.valueOf(snapshot.getId()));

    assertThat(result.getProjectStatus().getStatus()).isEqualTo(Status.NONE);
    assertThat(result.getProjectStatus().getConditionsCount()).isEqualTo(0);
  }
  @Test
  public void json_example() throws IOException {
    ComponentDto project = newProjectDto("project-uuid");
    dbClient.componentDao().insert(dbSession, project);
    SnapshotDto snapshot =
        dbClient
            .snapshotDao()
            .insert(
                dbSession,
                newSnapshotForProject(project)
                    .setPeriodMode(1, "last_period")
                    .setPeriodDate(1, 956789123456L)
                    .setPeriodMode(2, "last_version")
                    .setPeriodParam(2, "2015-12-07")
                    .setPeriodDate(2, 956789123987L)
                    .setPeriodMode(3, "last_analysis")
                    .setPeriodMode(5, "last_30_days")
                    .setPeriodParam(5, "2015-11-07"));
    MetricDto metric =
        dbClient
            .metricDao()
            .insert(
                dbSession,
                newMetricDto().setEnabled(true).setKey(CoreMetrics.QUALITY_GATE_DETAILS_KEY));
    dbClient
        .measureDao()
        .insert(
            dbSession,
            newMeasureDto(metric, snapshot.getId())
                .setData(
                    IOUtils.toString(
                        getClass().getResource("ProjectStatusActionTest/measure_data.json"))));
    dbSession.commit();

    String response =
        ws.newRequest().setParam("analysisId", snapshot.getId().toString()).execute().getInput();

    assertJson(response).isSimilarTo(getClass().getResource("project_status-example.json"));
  }
  @Test
  public void update_existing_document_when_indexing_one_project() throws Exception {
    String uuid = "PROJECT-UUID";
    esTester.putDocuments(
        INDEX_PROJECT_MEASURES,
        TYPE_PROJECT_MEASURES,
        new ProjectMeasuresDoc()
            .setId(uuid)
            .setKey("Old Key")
            .setName("Old Name")
            .setAnalysedAt(new Date(1_000_000L)));
    ComponentDto project = newProjectDto(uuid).setKey("New key").setName("New name");
    SnapshotDto analysis = componentDbTester.insertProjectAndSnapshot(project);

    underTest.index(project.uuid());

    assertThat(esTester.getIds(INDEX_PROJECT_MEASURES, TYPE_PROJECT_MEASURES)).containsOnly(uuid);
    SearchRequestBuilder request =
        esTester
            .client()
            .prepareSearch(INDEX_PROJECT_MEASURES)
            .setTypes(TYPE_PROJECT_MEASURES)
            .setQuery(
                boolQuery()
                    .must(matchAllQuery())
                    .filter(
                        boolQuery()
                            .must(termQuery("_id", uuid))
                            .must(termQuery(ProjectMeasuresIndexDefinition.FIELD_KEY, "New key"))
                            .must(termQuery(ProjectMeasuresIndexDefinition.FIELD_NAME, "New name"))
                            .must(
                                termQuery(
                                    ProjectMeasuresIndexDefinition.FIELD_ANALYSED_AT,
                                    new Date(analysis.getCreatedAt())))));
    assertThat(request.get().getHits()).hasSize(1);
  }
 private static Period newPeriod(int index, SnapshotDto snapshotDto) {
   return new Period(index, "mode", null, snapshotDto.getCreatedAt(), snapshotDto.getId());
 }
  @Test
  public void set_variation_on_all_numeric_metrics() {
    SnapshotDto period1ProjectSnapshot = createForProject(PROJECT_DTO);
    dbClient.snapshotDao().insert(session, period1ProjectSnapshot);
    dbClient
        .measureDao()
        .insert(
            session,
            newMeasureDto(
                ISSUES_METRIC.getId(), PROJECT_DTO.getId(), period1ProjectSnapshot.getId(), 60d),
            newMeasureDto(
                DEBT_METRIC.getId(), PROJECT_DTO.getId(), period1ProjectSnapshot.getId(), 10d),
            newMeasureDto(
                FILE_COMPLEXITY_METRIC.getId(),
                PROJECT_DTO.getId(),
                period1ProjectSnapshot.getId(),
                2d),
            newMeasureDto(
                BUILD_BREAKER_METRIC.getId(),
                PROJECT_DTO.getId(),
                period1ProjectSnapshot.getId(),
                1d));
    session.commit();

    periodsHolder.setPeriods(newPeriod(1, period1ProjectSnapshot));

    treeRootHolder.setRoot(PROJECT);

    addRawMeasure(PROJECT, ISSUES_METRIC, Measure.newMeasureBuilder().create(80, null));
    addRawMeasure(PROJECT, DEBT_METRIC, Measure.newMeasureBuilder().create(5L, null));
    addRawMeasure(PROJECT, FILE_COMPLEXITY_METRIC, Measure.newMeasureBuilder().create(3d, null));
    addRawMeasure(PROJECT, BUILD_BREAKER_METRIC, Measure.newMeasureBuilder().create(false, null));

    underTest.execute();

    assertThat(measureRepository.getRawMeasures(PROJECT).keys()).hasSize(4);

    assertThat(
            measureRepository
                .getRawMeasure(PROJECT, ISSUES_METRIC)
                .get()
                .getVariations()
                .getVariation1())
        .isEqualTo(20d);
    assertThat(
            measureRepository
                .getRawMeasure(PROJECT, DEBT_METRIC)
                .get()
                .getVariations()
                .getVariation1())
        .isEqualTo(-5d);
    assertThat(
            measureRepository
                .getRawMeasure(PROJECT, FILE_COMPLEXITY_METRIC)
                .get()
                .getVariations()
                .getVariation1())
        .isEqualTo(1d);
    assertThat(
            measureRepository
                .getRawMeasure(PROJECT, BUILD_BREAKER_METRIC)
                .get()
                .getVariations()
                .getVariation1())
        .isEqualTo(-1d);
  }