Search in sources :

Example 61 with SnapshotDto

use of org.sonar.db.component.SnapshotDto in project sonarqube by SonarSource.

the class MeasureDaoTest method test_selectLastMeasure.

@Test
public void test_selectLastMeasure() {
    MetricDto metric = db.measures().insertMetric();
    ComponentDto project = db.components().insertPrivateProject();
    ComponentDto file = db.components().insertComponent(newFileDto(project));
    SnapshotDto lastAnalysis = insertAnalysis(project.uuid(), true);
    SnapshotDto pastAnalysis = insertAnalysis(project.uuid(), false);
    MeasureDto pastMeasure = MeasureTesting.newMeasureDto(metric, file, pastAnalysis);
    MeasureDto lastMeasure = MeasureTesting.newMeasureDto(metric, file, lastAnalysis);
    underTest.insert(db.getSession(), pastMeasure);
    underTest.insert(db.getSession(), lastMeasure);
    MeasureDto selected = underTest.selectLastMeasure(db.getSession(), file.uuid(), metric.getKey()).get();
    assertThat(selected).isEqualToComparingFieldByField(lastMeasure);
    assertThat(underTest.selectLastMeasure(dbSession, "_missing_", metric.getKey())).isEmpty();
    assertThat(underTest.selectLastMeasure(dbSession, file.uuid(), "_missing_")).isEmpty();
    assertThat(underTest.selectLastMeasure(dbSession, "_missing_", "_missing_")).isEmpty();
}
Also used : MetricDto(org.sonar.db.metric.MetricDto) SnapshotDto(org.sonar.db.component.SnapshotDto) ComponentDto(org.sonar.db.component.ComponentDto) Test(org.junit.Test)

Example 62 with SnapshotDto

use of org.sonar.db.component.SnapshotDto in project sonarqube by SonarSource.

the class MeasureDaoTest method test_selects.

@Test
public void test_selects() {
    ComponentDto project1 = db.components().insertPrivateProject();
    ComponentDto module = db.components().insertComponent(newModuleDto(project1));
    db.components().insertComponent(newFileDto(module).setUuid("C1"));
    db.components().insertComponent(newFileDto(module).setUuid("C2"));
    SnapshotDto lastAnalysis = insertAnalysis(project1.uuid(), true);
    SnapshotDto pastAnalysis = insertAnalysis(project1.uuid(), false);
    ComponentDto project2 = db.components().insertPrivateProject();
    SnapshotDto project2LastAnalysis = insertAnalysis(project2.uuid(), true);
    // project 1
    insertMeasure("P1_M1", lastAnalysis.getUuid(), project1.uuid(), ncloc.getUuid());
    insertMeasure("P1_M2", lastAnalysis.getUuid(), project1.uuid(), coverage.getUuid());
    insertMeasure("P1_M3", pastAnalysis.getUuid(), project1.uuid(), ncloc.getUuid());
    // project 2
    insertMeasure("P2_M1", project2LastAnalysis.getUuid(), project2.uuid(), ncloc.getUuid());
    insertMeasure("P2_M2", project2LastAnalysis.getUuid(), project2.uuid(), coverage.getUuid());
    // component C1
    insertMeasure("M1", pastAnalysis.getUuid(), "C1", ncloc.getUuid());
    insertMeasure("M2", lastAnalysis.getUuid(), "C1", ncloc.getUuid());
    insertMeasure("M3", lastAnalysis.getUuid(), "C1", coverage.getUuid());
    // component C2
    insertMeasure("M6", lastAnalysis.getUuid(), "C2", ncloc.getUuid());
    db.commit();
    verifyNoMeasure("C1", ncloc.getKey(), "invalid_analysis");
    verifyNoMeasure("C1", "INVALID_KEY");
    verifyNoMeasure("C1", "INVALID_KEY", pastAnalysis.getUuid());
    verifyNoMeasure("MISSING_COMPONENT", ncloc.getKey());
    verifyNoMeasure("MISSING_COMPONENT", ncloc.getKey(), pastAnalysis.getUuid());
    // ncloc measure of component C1 of last analysis
    verifyMeasure("C1", ncloc.getKey(), "M2");
    // ncloc measure of component C1 of non last analysis
    verifyMeasure("C1", ncloc.getKey(), pastAnalysis.getUuid(), "M1");
    // ncloc measure of component C1 of last analysis by UUID
    verifyMeasure("C1", ncloc.getKey(), lastAnalysis.getUuid(), "M2");
    // missing measure of component C1 of last analysis
    verifyNoMeasure("C1", complexity.getKey());
    // missing measure of component C1 of non last analysis
    verifyNoMeasure("C1", complexity.getKey(), pastAnalysis.getUuid());
    // missing measure of component C1 of last analysis by UUID
    verifyNoMeasure("C1", complexity.getKey(), lastAnalysis.getUuid());
    // projects measures of last analysis
    verifyMeasure(project1.uuid(), ncloc.getKey(), "P1_M1");
    // projects measures of none last analysis
    verifyMeasure(project1.uuid(), ncloc.getKey(), pastAnalysis.getUuid(), "P1_M3");
}
Also used : SnapshotDto(org.sonar.db.component.SnapshotDto) ComponentDto(org.sonar.db.component.ComponentDto) Test(org.junit.Test)

Example 63 with SnapshotDto

use of org.sonar.db.component.SnapshotDto in project sonarqube by SonarSource.

the class ProjectMeasuresIndexerIteratorTest method return_application_measure.

@Test
public void return_application_measure() {
    ComponentDto project = dbTester.components().insertPrivateApplication(c -> c.setDbKey("App-Key").setName("App Name"));
    SnapshotDto analysis = dbTester.components().insertSnapshot(project);
    MetricDto metric1 = dbTester.measures().insertMetric(m -> m.setValueType(INT.name()).setKey("ncloc"));
    MetricDto metric2 = dbTester.measures().insertMetric(m -> m.setValueType(INT.name()).setKey("coverage"));
    dbTester.measures().insertLiveMeasure(project, metric1, m -> m.setValue(10d));
    dbTester.measures().insertLiveMeasure(project, metric2, m -> m.setValue(20d));
    Map<String, ProjectMeasures> docsById = createResultSetAndReturnDocsById();
    assertThat(docsById).hasSize(1);
    ProjectMeasures doc = docsById.get(project.uuid());
    assertThat(doc).isNotNull();
    assertThat(doc.getProject().getUuid()).isEqualTo(project.uuid());
    assertThat(doc.getProject().getKey()).isEqualTo("App-Key");
    assertThat(doc.getProject().getName()).isEqualTo("App Name");
    assertThat(doc.getProject().getAnalysisDate()).isNotNull().isEqualTo(analysis.getCreatedAt());
    assertThat(doc.getMeasures().getNumericMeasures()).containsOnly(entry(metric1.getKey(), 10d), entry(metric2.getKey(), 20d));
}
Also used : MetricDto(org.sonar.db.metric.MetricDto) ProjectMeasures(org.sonar.db.measure.ProjectMeasuresIndexerIterator.ProjectMeasures) SnapshotDto(org.sonar.db.component.SnapshotDto) ComponentDto(org.sonar.db.component.ComponentDto) Test(org.junit.Test)

Example 64 with SnapshotDto

use of org.sonar.db.component.SnapshotDto in project sonarqube by SonarSource.

the class DuplicationDaoTest method selectCandidates_returns_block_from_last_snapshot_only_of_component_with_language_and_if_not_specified_analysis.

@Test
public void selectCandidates_returns_block_from_last_snapshot_only_of_component_with_language_and_if_not_specified_analysis() {
    ComponentDto project1 = db.components().insertPrivateProject();
    ComponentDto fooFile = db.components().insertComponent(ComponentTesting.newFileDto(project1).setLanguage("foo").setEnabled(true));
    ComponentDto fooFile1 = db.components().insertComponent(ComponentTesting.newFileDto(project1).setLanguage("foo").setEnabled(true));
    ComponentDto disabledFooFile = db.components().insertComponent(ComponentTesting.newFileDto(project1).setLanguage("foo").setEnabled(false));
    ComponentDto barFile = db.components().insertComponent(ComponentTesting.newFileDto(project1).setLanguage("bar").setEnabled(true));
    ComponentDto noLanguageFile = db.components().insertComponent(ComponentTesting.newFileDto(project1).setLanguage(null).setEnabled(true));
    SnapshotDto newAnalysis = db.components().insertSnapshot(project1, t -> t.setLast(false));
    SnapshotDto lastAnalysis = db.components().insertSnapshot(project1, t -> t.setLast(true));
    SnapshotDto notLastAnalysis = db.components().insertSnapshot(project1, t -> t.setLast(false));
    for (String hash : Arrays.asList("aa", "bb")) {
        for (ComponentDto component : Arrays.asList(project1, fooFile, fooFile1, disabledFooFile, barFile, noLanguageFile)) {
            insert(component, lastAnalysis, hash, 0, 1, 2);
            insert(component, notLastAnalysis, hash, 0, 4, 5);
            insert(component, newAnalysis, hash, 0, 6, 7);
        }
    }
    for (String hash : Arrays.asList("aa", "bb")) {
        assertThat(dao.selectCandidates(dbSession, newAnalysis.getUuid(), "foo", singletonList(hash))).containsOnly(tuple(fooFile.uuid(), fooFile.getKey(), lastAnalysis.getUuid(), hash), tuple(fooFile1.uuid(), fooFile1.getKey(), lastAnalysis.getUuid(), hash));
        assertThat(dao.selectCandidates(dbSession, newAnalysis.getUuid(), "bar", singletonList(hash))).containsOnly(tuple(barFile.uuid(), barFile.getKey(), lastAnalysis.getUuid(), hash));
        assertThat(dao.selectCandidates(dbSession, newAnalysis.getUuid(), "donut", singletonList(hash))).isEmpty();
    }
    for (List<String> hashes : Arrays.asList(of("aa", "bb"), of("bb", "aa"), of("aa", "bb", "cc"))) {
        assertThat(dao.selectCandidates(dbSession, newAnalysis.getUuid(), "foo", hashes)).containsOnly(tuple(fooFile.uuid(), fooFile.getKey(), lastAnalysis.getUuid(), "aa"), tuple(fooFile.uuid(), fooFile.getKey(), lastAnalysis.getUuid(), "bb"), tuple(fooFile1.uuid(), fooFile1.getKey(), lastAnalysis.getUuid(), "aa"), tuple(fooFile1.uuid(), fooFile1.getKey(), lastAnalysis.getUuid(), "bb"));
        assertThat(dao.selectCandidates(dbSession, newAnalysis.getUuid(), "bar", hashes)).containsOnly(tuple(barFile.uuid(), barFile.getKey(), lastAnalysis.getUuid(), "aa"), tuple(barFile.uuid(), barFile.getKey(), lastAnalysis.getUuid(), "bb"));
        assertThat(dao.selectCandidates(dbSession, newAnalysis.getUuid(), "donut", hashes)).isEmpty();
    }
    assertThat(dao.selectCandidates(dbSession, lastAnalysis.getUuid(), "foo", singletonList("aa"))).isEmpty();
    assertThat(dao.selectCandidates(dbSession, lastAnalysis.getUuid(), "bar", singletonList("aa"))).isEmpty();
    assertThat(dao.selectCandidates(dbSession, lastAnalysis.getUuid(), "donut", singletonList("aa"))).isEmpty();
}
Also used : SnapshotDto(org.sonar.db.component.SnapshotDto) ComponentDto(org.sonar.db.component.ComponentDto) Test(org.junit.Test)

Example 65 with SnapshotDto

use of org.sonar.db.component.SnapshotDto in project sonarqube by SonarSource.

the class DuplicationDaoTest method insert.

@Test
public void insert() {
    ComponentDto project = newPrivateProjectDto();
    SnapshotDto analysis = db.components().insertProjectAndSnapshot(project);
    insert(project, analysis, "bb", 0, 1, 2);
    List<Map<String, Object>> rows = db.select("select " + "uuid as \"UUID\", analysis_uuid as \"ANALYSIS\", component_uuid as \"COMPONENT\", hash as \"HASH\", " + "index_in_file as \"INDEX\", start_line as \"START\", end_line as \"END\"" + " from duplications_index");
    Assertions.assertThat(rows).hasSize(1);
    Map<String, Object> row = rows.get(0);
    Assertions.assertThat(row.get("UUID")).isNotNull();
    Assertions.assertThat(row).containsEntry("ANALYSIS", analysis.getUuid()).containsEntry("COMPONENT", project.uuid()).containsEntry("HASH", "bb").containsEntry("INDEX", 0L).containsEntry("START", 1L).containsEntry("END", 2L);
}
Also used : SnapshotDto(org.sonar.db.component.SnapshotDto) ComponentDto(org.sonar.db.component.ComponentDto) Map(java.util.Map) Test(org.junit.Test)

Aggregations

SnapshotDto (org.sonar.db.component.SnapshotDto)326 Test (org.junit.Test)257 ComponentDto (org.sonar.db.component.ComponentDto)219 MetricDto (org.sonar.db.metric.MetricDto)54 TestComputationStepContext (org.sonar.ce.task.step.TestComputationStepContext)31 EventDto (org.sonar.db.event.EventDto)30 ProjectDto (org.sonar.db.project.ProjectDto)27 DbSession (org.sonar.db.DbSession)26 SnapshotTesting.newAnalysis (org.sonar.db.component.SnapshotTesting.newAnalysis)21 BranchDto (org.sonar.db.component.BranchDto)20 SearchEventsWsResponse (org.sonarqube.ws.Developers.SearchEventsWsResponse)20 MetricTesting.newMetricDto (org.sonar.db.metric.MetricTesting.newMetricDto)17 UseDataProvider (com.tngtech.java.junit.dataprovider.UseDataProvider)14 DbClient (org.sonar.db.DbClient)14 NotFoundException (org.sonar.server.exceptions.NotFoundException)14 List (java.util.List)13 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)13 Rule (org.junit.Rule)13 UserRole (org.sonar.api.web.UserRole)13 DbTester (org.sonar.db.DbTester)13