123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762 |
- /*
- * SonarQube
- * Copyright (C) 2009-2023 SonarSource SA
- * mailto:info AT sonarsource DOT com
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- */
- package org.sonar.db.measure;
-
- import java.nio.charset.StandardCharsets;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.Collections;
- import java.util.List;
- import java.util.Map;
- import java.util.Optional;
- import java.util.stream.IntStream;
- import org.apache.commons.lang.RandomStringUtils;
- import org.junit.Before;
- import org.junit.Rule;
- import org.junit.Test;
- import org.mockito.internal.util.collections.Sets;
- import org.sonar.api.utils.System2;
- import org.sonar.db.DbTester;
- import org.sonar.db.component.BranchType;
- import org.sonar.db.component.ComponentDto;
- import org.sonar.db.metric.MetricDto;
-
- import static java.util.Arrays.asList;
- import static java.util.Collections.emptyList;
- import static java.util.Collections.singleton;
- import static java.util.Collections.singletonList;
- import static org.assertj.core.api.Assertions.assertThat;
- import static org.assertj.core.groups.Tuple.tuple;
- import static org.sonar.api.measures.Metric.ValueType.DATA;
- import static org.sonar.api.measures.Metric.ValueType.INT;
- import static org.sonar.db.component.BranchDto.DEFAULT_MAIN_BRANCH_NAME;
- import static org.sonar.db.component.ComponentTesting.newFileDto;
- import static org.sonar.db.component.ComponentTesting.newPrivateProjectDto;
- import static org.sonar.db.measure.MeasureTesting.newLiveMeasure;
-
- public class LiveMeasureDaoTest {
-
- @Rule
- public DbTester db = DbTester.create(System2.INSTANCE);
-
- private final LiveMeasureDao underTest = db.getDbClient().liveMeasureDao();
- private MetricDto metric;
-
- @Before
- public void setUp() {
- metric = db.measures().insertMetric();
- }
-
- @Test
- public void selectByComponentUuidsAndMetricUuids() {
- LiveMeasureDto measure1 = newLiveMeasure().setMetricUuid(metric.getUuid());
- LiveMeasureDto measure2 = newLiveMeasure().setMetricUuid(metric.getUuid());
- underTest.insert(db.getSession(), measure1);
- underTest.insert(db.getSession(), measure2);
-
- List<LiveMeasureDto> selected = underTest.selectByComponentUuidsAndMetricUuids(db.getSession(),
- asList(measure1.getComponentUuid(), measure2.getComponentUuid()), singletonList(metric.getUuid()));
- assertThat(selected)
- .extracting(LiveMeasureDto::getComponentUuid, LiveMeasureDto::getProjectUuid, LiveMeasureDto::getMetricUuid, LiveMeasureDto::getValue, LiveMeasureDto::getDataAsString)
- .containsExactlyInAnyOrder(
- tuple(measure1.getComponentUuid(), measure1.getProjectUuid(), measure1.getMetricUuid(), measure1.getValue(), measure1.getDataAsString()),
- tuple(measure2.getComponentUuid(), measure2.getProjectUuid(), measure2.getMetricUuid(), measure2.getValue(), measure2.getDataAsString()));
-
- assertThat(underTest.selectByComponentUuidsAndMetricUuids(db.getSession(), emptyList(), singletonList(metric.getUuid()))).isEmpty();
- assertThat(underTest.selectByComponentUuidsAndMetricUuids(db.getSession(), singletonList(measure1.getComponentUuid()), emptyList())).isEmpty();
- }
-
- @Test
- public void selectByComponentUuidsAndMetricUuids_returns_empty_list_if_metric_does_not_match() {
- LiveMeasureDto measure = newLiveMeasure().setMetricUuid(metric.getUuid());
- underTest.insert(db.getSession(), measure);
-
- String otherMetricUuid = metric.getUuid() + "other";
- List<LiveMeasureDto> selected = underTest.selectByComponentUuidsAndMetricUuids(db.getSession(), singletonList(measure.getComponentUuid()), singletonList(otherMetricUuid));
-
- assertThat(selected).isEmpty();
- }
-
- @Test
- public void selectByComponentUuidsAndMetricUuids_returns_empty_list_if_component_does_not_match() {
- LiveMeasureDto measure = newLiveMeasure();
- underTest.insert(db.getSession(), measure);
-
- List<LiveMeasureDto> selected = underTest.selectByComponentUuidsAndMetricUuids(db.getSession(), singletonList("_missing_"), singletonList(measure.getMetricUuid()));
-
- assertThat(selected).isEmpty();
- }
-
- @Test
- public void selectByComponentUuidsAndMetricKeys() {
- LiveMeasureDto measure1 = newLiveMeasure().setMetricUuid(metric.getUuid());
- LiveMeasureDto measure2 = newLiveMeasure().setMetricUuid(metric.getUuid());
- underTest.insert(db.getSession(), measure1);
- underTest.insert(db.getSession(), measure2);
-
- List<LiveMeasureDto> selected = underTest.selectByComponentUuidsAndMetricKeys(db.getSession(), asList(measure1.getComponentUuid(), measure2.getComponentUuid()),
- singletonList(metric.getKey()));
- assertThat(selected)
- .extracting(LiveMeasureDto::getComponentUuid, LiveMeasureDto::getProjectUuid, LiveMeasureDto::getMetricUuid, LiveMeasureDto::getValue, LiveMeasureDto::getDataAsString)
- .containsExactlyInAnyOrder(
- tuple(measure1.getComponentUuid(), measure1.getProjectUuid(), measure1.getMetricUuid(), measure1.getValue(), measure1.getDataAsString()),
- tuple(measure2.getComponentUuid(), measure2.getProjectUuid(), measure2.getMetricUuid(), measure2.getValue(), measure2.getDataAsString()));
-
- assertThat(underTest.selectByComponentUuidsAndMetricKeys(db.getSession(), emptyList(), singletonList(metric.getKey()))).isEmpty();
- assertThat(underTest.selectByComponentUuidsAndMetricKeys(db.getSession(), singletonList(measure1.getComponentUuid()), emptyList())).isEmpty();
- }
-
- @Test
- public void selectByComponentUuidsAndMetricKeys_returns_empty_list_if_metric_does_not_match() {
- LiveMeasureDto measure = newLiveMeasure().setMetricUuid(metric.getUuid());
- underTest.insert(db.getSession(), measure);
-
- List<LiveMeasureDto> selected = underTest.selectByComponentUuidsAndMetricKeys(db.getSession(), singletonList(measure.getComponentUuid()), singletonList("_other_"));
-
- assertThat(selected).isEmpty();
- }
-
- @Test
- public void selectByComponentUuidsAndMetricKeys_returns_empty_list_if_component_does_not_match() {
- LiveMeasureDto measure = newLiveMeasure().setMetricUuid(metric.getUuid());
- underTest.insert(db.getSession(), measure);
-
- List<LiveMeasureDto> selected = underTest.selectByComponentUuidsAndMetricKeys(db.getSession(), singletonList("_missing_"), singletonList(metric.getKey()));
-
- assertThat(selected).isEmpty();
- }
-
- @Test
- public void selectForProjectsByMetricUuids() {
- MetricDto metric = db.measures().insertMetric();
- MetricDto metric2 = db.measures().insertMetric();
- ComponentDto project = db.components().insertPrivateProject();
- ComponentDto project2 = db.components().insertPrivateProject();
- underTest.insert(db.getSession(), newLiveMeasure(project, metric).setValue(3.14).setData((String) null));
- underTest.insert(db.getSession(), newLiveMeasure(project, metric2).setValue(4.54).setData((String) null));
- underTest.insert(db.getSession(), newLiveMeasure(project2, metric).setValue(99.99).setData((String) null));
-
- List<LiveMeasureDto> selected = underTest.selectForProjectsByMetricUuids(db.getSession(), List.of(metric.getUuid(), metric2.getUuid()));
- assertThat(selected)
- .extracting(LiveMeasureDto::getComponentUuid, LiveMeasureDto::getProjectUuid, LiveMeasureDto::getMetricUuid, LiveMeasureDto::getValue, LiveMeasureDto::getDataAsString)
- .containsExactlyInAnyOrder(
- tuple(project.uuid(), project.uuid(), metric.getUuid(), 3.14, null),
- tuple(project.uuid(), project.uuid(), metric2.getUuid(), 4.54, null),
- tuple(project2.uuid(), project2.uuid(), metric.getUuid(), 99.99, null));
- }
-
- @Test
- public void selectForProjectsByMetricUuids_whenMetricDoesNotMatch_shouldReturnEmptyList() {
- ComponentDto project = db.components().insertPrivateProject();
- underTest.insert(db.getSession(), newLiveMeasure(project, metric).setValue(3.14).setData((String) null));
- List<LiveMeasureDto> selected = underTest.selectForProjectsByMetricUuids(db.getSession(), singletonList("_other_"));
- assertThat(selected).isEmpty();
- }
-
- @Test
- public void selectForProjectsByMetricUuids_shouldReturnProjectWithTRKQualifierOnly() {
- MetricDto metric = db.measures().insertMetric();
- ComponentDto application = db.components().insertPrivateApplication();
- ComponentDto project = db.components().insertPrivateProject();
- ComponentDto project2 = db.components().insertPrivateProject();
- db.components().addApplicationProject(application, project, project2);
- underTest.insert(db.getSession(), newLiveMeasure(application, metric).setValue(3.14).setData((String) null));
- underTest.insert(db.getSession(), newLiveMeasure(project, metric).setValue(4.54).setData((String) null));
- underTest.insert(db.getSession(), newLiveMeasure(project2, metric).setValue(5.56).setData((String) null));
-
- List<LiveMeasureDto> selected = underTest.selectForProjectsByMetricUuids(db.getSession(), List.of(metric.getUuid()));
-
- assertThat(selected)
- .extracting(LiveMeasureDto::getProjectUuid)
- .containsExactlyInAnyOrder(project.uuid(), project2.uuid());
- }
-
- @Test
- public void selectByComponentUuidAndMetricKey() {
- LiveMeasureDto measure = newLiveMeasure().setMetricUuid(metric.getUuid());
- underTest.insert(db.getSession(), measure);
-
- Optional<LiveMeasureDto> selected = underTest.selectMeasure(db.getSession(), measure.getComponentUuid(), metric.getKey());
-
- assertThat(selected).isNotEmpty();
- assertThat(selected.get()).isEqualToComparingFieldByField(measure);
- }
-
- @Test
- public void selectByComponentUuidAndMetricKey_return_empty_if_component_does_not_match() {
- LiveMeasureDto measure = newLiveMeasure().setMetricUuid(metric.getUuid());
- underTest.insert(db.getSession(), measure);
-
- assertThat(underTest.selectMeasure(db.getSession(), "_missing_", metric.getKey())).isEmpty();
- }
-
- @Test
- public void selectByComponentUuidAndMetricKey_return_empty_if_metric_does_not_match() {
- LiveMeasureDto measure = newLiveMeasure().setMetricUuid(metric.getUuid());
- underTest.insert(db.getSession(), measure);
-
- assertThat(underTest.selectMeasure(db.getSession(), measure.getComponentUuid(), "_missing_")).isEmpty();
- }
-
- @Test
- public void selectByComponentUuidAndMetricKeys() {
- MetricDto metric2 = db.measures().insertMetric();
-
- LiveMeasureDto measure1 = newLiveMeasure().setMetricUuid(metric.getUuid()).setValue(1.0).setComponentUuid("uuid");
- LiveMeasureDto measure2 = newLiveMeasure().setMetricUuid(metric2.getUuid()).setValue(2.0).setComponentUuid("uuid");
-
- underTest.insert(db.getSession(), measure1);
- underTest.insert(db.getSession(), measure2);
-
- List<LiveMeasureDto> selected = underTest.selectByComponentUuidAndMetricKeys(db.getSession(), "uuid", asList(metric.getKey(), metric2.getKey()));
-
- assertThat(selected).hasSize(2);
- assertThat(selected).extracting(LiveMeasureDto::getMetricUuid, LiveMeasureDto::getValue)
- .containsExactlyInAnyOrder(tuple(metric.getUuid(), measure1.getValue()), tuple(metric2.getUuid(), measure2.getValue()));
- }
-
- @Test
- public void selectByComponentUuidAndMetricKeys_return_empty_if_component_does_not_match() {
- LiveMeasureDto measure = newLiveMeasure().setMetricUuid(metric.getUuid());
- underTest.insert(db.getSession(), measure);
-
- assertThat(underTest.selectByComponentUuidAndMetricKeys(db.getSession(), "_missing_", singletonList(metric.getKey()))).isEmpty();
- }
-
- @Test
- public void selectByComponentUuidAndMetricKeys_return_empty_if_no_metric_matches() {
- LiveMeasureDto measure = newLiveMeasure().setMetricUuid(metric.getUuid());
- underTest.insert(db.getSession(), measure);
-
- assertThat(underTest.selectByComponentUuidAndMetricKeys(db.getSession(), measure.getComponentUuid(), singletonList("_missing_"))).isEmpty();
- }
-
- @Test
- public void selectMeasure() {
- MetricDto metric = db.measures().insertMetric();
- LiveMeasureDto stored = newLiveMeasure().setMetricUuid(metric.getUuid());
- underTest.insert(db.getSession(), stored);
-
- // metric exists but not component
- assertThat(underTest.selectMeasure(db.getSession(), "_missing_", metric.getKey())).isEmpty();
-
- // component exists but not metric
- assertThat(underTest.selectMeasure(db.getSession(), stored.getComponentUuid(), "_missing_")).isEmpty();
-
- // component and metric don't match
- assertThat(underTest.selectMeasure(db.getSession(), "_missing_", "_missing_")).isEmpty();
-
- // matches
- assertThat(underTest.selectMeasure(db.getSession(), stored.getComponentUuid(), metric.getKey()).get())
- .isEqualToComparingFieldByField(stored);
- }
-
- @Test
- public void selectTreeByQuery() {
- List<LiveMeasureDto> results = new ArrayList<>();
- MetricDto metric = db.measures().insertMetric();
- ComponentDto project = db.components().insertPrivateProject();
- ComponentDto file = db.components().insertComponent(newFileDto(project));
- underTest.insert(db.getSession(), newLiveMeasure(file, metric).setValue(3.14));
-
- underTest.selectTreeByQuery(db.getSession(), project,
- MeasureTreeQuery.builder()
- .setMetricUuids(singleton(metric.getUuid()))
- .setStrategy(MeasureTreeQuery.Strategy.LEAVES).build(),
- context -> results.add(context.getResultObject()));
-
- assertThat(results).hasSize(1);
- LiveMeasureDto result = results.get(0);
- assertThat(result.getComponentUuid()).isEqualTo(file.uuid());
- assertThat(result.getMetricUuid()).isEqualTo(metric.getUuid());
- assertThat(result.getValue()).isEqualTo(3.14);
- }
-
- @Test
- public void scrollSelectByComponentUuidAndMetricKeys_for_non_empty_metric_set() {
- List<LiveMeasureDto> results = new ArrayList<>();
- MetricDto metric = db.measures().insertMetric();
- MetricDto metric2 = db.measures().insertMetric();
- ComponentDto project = db.components().insertPrivateProject();
- ComponentDto project2 = db.components().insertPrivateProject();
- underTest.insert(db.getSession(), newLiveMeasure(project, metric).setValue(3.14));
- underTest.insert(db.getSession(), newLiveMeasure(project, metric2).setValue(4.54));
- underTest.insert(db.getSession(), newLiveMeasure(project2, metric).setValue(99.99));
- underTest.scrollSelectByComponentUuidAndMetricKeys(db.getSession(), project.uuid(), Sets.newSet(metric.getKey(), metric2.getKey()),
- context -> results.add(context.getResultObject()));
-
- assertThat(results).hasSize(2);
- LiveMeasureDto result = results.stream().filter(lm -> lm.getMetricUuid().equals(metric.getUuid())).findFirst().get();
- assertThat(result.getComponentUuid()).isEqualTo(project.uuid());
- assertThat(result.getMetricUuid()).isEqualTo(metric.getUuid());
- assertThat(result.getValue()).isEqualTo(3.14);
- LiveMeasureDto result2 = results.stream().filter(lm -> lm.getMetricUuid().equals(metric2.getUuid())).findFirst().get();
- assertThat(result2.getComponentUuid()).isEqualTo(project.uuid());
- assertThat(result2.getMetricUuid()).isEqualTo(metric2.getUuid());
- assertThat(result2.getValue()).isEqualTo(4.54);
- }
-
- @Test
- public void scrollSelectByComponentUuidAndMetricKeys_for_empty_metric_set() {
- List<LiveMeasureDto> results = new ArrayList<>();
- ComponentDto project = db.components().insertPrivateProject();
- underTest.scrollSelectByComponentUuidAndMetricKeys(db.getSession(), project.uuid(), Sets.newSet(),
- context -> results.add(context.getResultObject()));
-
- assertThat(results).isEmpty();
- }
-
- @Test
- public void selectTreeByQuery_with_empty_results() {
- List<LiveMeasureDto> results = new ArrayList<>();
- underTest.selectTreeByQuery(db.getSession(), newPrivateProjectDto(),
- MeasureTreeQuery.builder().setStrategy(MeasureTreeQuery.Strategy.LEAVES).build(),
- context -> results.add(context.getResultObject()));
-
- assertThat(results).isEmpty();
- }
-
- @Test
- public void selectMeasure_map_fields() {
- MetricDto metric = db.measures().insertMetric();
- ComponentDto project = db.components().insertPrivateProject();
- ComponentDto file = db.components().insertComponent(newFileDto(project));
- underTest.insert(db.getSession(), newLiveMeasure(file, metric).setValue(3.14).setData("text_value"));
-
- LiveMeasureDto result = underTest.selectMeasure(db.getSession(), file.uuid(), metric.getKey()).orElseThrow(() -> new IllegalArgumentException("Measure not found"));
-
- assertThat(result).as("Fail to map fields of %s", result.toString()).extracting(
- LiveMeasureDto::getProjectUuid, LiveMeasureDto::getComponentUuid, LiveMeasureDto::getMetricUuid, LiveMeasureDto::getValue,
- LiveMeasureDto::getDataAsString, LiveMeasureDto::getTextValue)
- .contains(project.uuid(), file.uuid(), metric.getUuid(), 3.14, "text_value", "text_value");
- }
-
- @Test
- public void countNcloc() {
- MetricDto ncloc = db.measures().insertMetric(m -> m.setKey("ncloc").setValueType(INT.toString()));
- MetricDto lines = db.measures().insertMetric(m -> m.setKey("lines").setValueType(INT.toString()));
-
- ComponentDto simpleProject = db.components().insertPublicProject();
- db.measures().insertLiveMeasure(simpleProject, ncloc, m -> m.setValue(10d));
-
- ComponentDto projectWithBiggerBranch = db.components().insertPublicProject();
- ComponentDto bigBranch = db.components().insertProjectBranch(projectWithBiggerBranch, b -> b.setBranchType(BranchType.BRANCH));
- db.measures().insertLiveMeasure(projectWithBiggerBranch, ncloc, m -> m.setValue(100d));
- db.measures().insertLiveMeasure(bigBranch, ncloc, m -> m.setValue(200d));
-
- ComponentDto projectWithLinesButNoLoc = db.components().insertPublicProject();
- db.measures().insertLiveMeasure(projectWithLinesButNoLoc, lines, m -> m.setValue(365d));
- db.measures().insertLiveMeasure(projectWithLinesButNoLoc, ncloc, m -> m.setValue(0d));
-
- assertThat(underTest.sumNclocOfBiggestBranchForProject(db.getSession(), simpleProject.uuid())).isEqualTo(10L);
- assertThat(underTest.sumNclocOfBiggestBranchForProject(db.getSession(), projectWithBiggerBranch.uuid())).isEqualTo(200L);
- assertThat(underTest.sumNclocOfBiggestBranchForProject(db.getSession(), projectWithLinesButNoLoc.uuid())).isZero();
- }
-
- @Test
- public void get_branch_with_max_ncloc_per_project() {
- Map<String, MetricDto> metrics = setupMetrics();
- setupProjectsWithLoc(metrics.get("ncloc"), metrics.get("ncloc_language_distribution"), metrics.get("lines"));
-
- List<LargestBranchNclocDto> results = underTest.getLargestBranchNclocPerProject(db.getSession());
-
- assertThat(results).hasSize(5);
- assertLocForProject(results.get(0), "projectWithTieOnBranchSize", DEFAULT_MAIN_BRANCH_NAME, 250);
- assertLocForProject(results.get(1), "projectWithTieOnOtherBranches", "tieBranch1", 230);
- assertLocForProject(results.get(2), "projectWithBranchBiggerThanMaster", "notMasterBranch", 200);
- assertLocForProject(results.get(3), "simpleProject", DEFAULT_MAIN_BRANCH_NAME, 10);
- assertLocForProject(results.get(4), "projectWithLinesButNoLoc", DEFAULT_MAIN_BRANCH_NAME, 0);
- }
-
- @Test
- public void get_loc_language_distribution() {
- Map<String, MetricDto> metrics = setupMetrics();
- MetricDto ncloc = metrics.get("ncloc");
- MetricDto nclocLanguageDistribution = metrics.get("ncloc_language_distribution");
- Map<String, ComponentDto> components = setupProjectsWithLoc(ncloc, nclocLanguageDistribution, metrics.get("lines"));
-
- List<ProjectLocDistributionDto> results = underTest.selectLargestBranchesLocDistribution(db.getSession(), ncloc.getUuid(), nclocLanguageDistribution.getUuid());
-
- assertThat(results)
- .containsExactlyInAnyOrder(
- new ProjectLocDistributionDto(components.get("projectWithTieOnBranchSize").uuid(), components.get("projectWithTieOnBranchSize").uuid(), "java=250;js=0"),
- new ProjectLocDistributionDto(components.get("projectWithTieOnOtherBranches").uuid(), components.get("tieBranch1").uuid(), "java=230;js=0"),
- new ProjectLocDistributionDto(components.get("projectWithBranchBiggerThanMaster").uuid(), components.get("notMasterBranch").uuid(), "java=100;js=100"),
- new ProjectLocDistributionDto(components.get("simpleProject").uuid(), components.get("simpleProject").uuid(), "java=10;js=0"),
- new ProjectLocDistributionDto(components.get("projectWithLinesButNoLoc").uuid(), components.get("projectWithLinesButNoLoc").uuid(), "java=0;js=0"));
- }
-
- @Test
- public void countNcloc_empty() {
- db.measures().insertMetric(m -> m.setKey("ncloc").setValueType(INT.toString()));
- db.measures().insertMetric(m -> m.setKey("lines").setValueType(INT.toString()));
- long result = underTest.sumNclocOfBiggestBranchForProject(db.getSession(), "non-existing-project-uuid");
-
- assertThat(result).isZero();
- }
-
- @Test
- public void insert_data() {
- byte[] data = "text_value".getBytes(StandardCharsets.UTF_8);
- MetricDto metric = db.measures().insertMetric();
- ComponentDto project = db.components().insertPrivateProject();
- ComponentDto file = db.components().insertComponent(newFileDto(project));
- LiveMeasureDto measure = newLiveMeasure(file, metric).setData(data);
-
- underTest.insert(db.getSession(), measure);
-
- LiveMeasureDto result = underTest.selectMeasure(db.getSession(), file.uuid(), metric.getKey()).orElseThrow(() -> new IllegalArgumentException("Measure not found"));
- assertThat(new String(result.getData(), StandardCharsets.UTF_8)).isEqualTo("text_value");
- assertThat(result.getDataAsString()).isEqualTo("text_value");
- }
-
- @Test
- public void insertOrUpdate() {
- // insert
- LiveMeasureDto dto = newLiveMeasure();
- underTest.insertOrUpdate(db.getSession(), dto);
- verifyPersisted(dto);
- verifyTableSize(1);
-
- // update
- dto.setValue(dto.getValue() + 1);
- dto.setData(dto.getDataAsString() + "_new");
- underTest.insertOrUpdate(db.getSession(), dto);
- verifyPersisted(dto);
- verifyTableSize(1);
- }
-
- @Test
- public void deleteByComponentUuidExcludingMetricUuids() {
- LiveMeasureDto measure1 = newLiveMeasure().setComponentUuid("C1").setMetricUuid("1");
- LiveMeasureDto measure2 = newLiveMeasure().setComponentUuid("C1").setMetricUuid("2");
- LiveMeasureDto measure3 = newLiveMeasure().setComponentUuid("C1").setMetricUuid("3");
- LiveMeasureDto measureOtherComponent = newLiveMeasure().setComponentUuid("C2").setMetricUuid("3");
- underTest.insertOrUpdate(db.getSession(), measure1);
- underTest.insertOrUpdate(db.getSession(), measure2);
- underTest.insertOrUpdate(db.getSession(), measure3);
- underTest.insertOrUpdate(db.getSession(), measureOtherComponent);
-
- underTest.deleteByComponentUuidExcludingMetricUuids(db.getSession(), "C1", Arrays.asList("1", "2"));
-
- verifyTableSize(3);
- verifyPersisted(measure1);
- verifyPersisted(measure2);
- verifyPersisted(measureOtherComponent);
- }
-
- @Test
- public void deleteByComponentUuid() {
- LiveMeasureDto measure1 = newLiveMeasure().setComponentUuid("C1").setMetricUuid("1");
- LiveMeasureDto measure2 = newLiveMeasure().setComponentUuid("C1").setMetricUuid("2");
- LiveMeasureDto measure3 = newLiveMeasure().setComponentUuid("C1").setMetricUuid("3");
- LiveMeasureDto measureOtherComponent = newLiveMeasure().setComponentUuid("C2").setMetricUuid("3");
- underTest.insertOrUpdate(db.getSession(), measure1);
- underTest.insertOrUpdate(db.getSession(), measure2);
- underTest.insertOrUpdate(db.getSession(), measure3);
- underTest.insertOrUpdate(db.getSession(), measureOtherComponent);
-
- underTest.deleteByComponent(db.getSession(), "C1");
-
- verifyTableSize(1);
- verifyPersisted(measureOtherComponent);
- }
-
- @Test
- public void deleteByComponentUuidExcludingMetricUuids_with_empty_metrics() {
- LiveMeasureDto measure1 = newLiveMeasure().setComponentUuid("C1").setMetricUuid("1");
- LiveMeasureDto measure2 = newLiveMeasure().setComponentUuid("C1").setMetricUuid("2");
- LiveMeasureDto measureOnOtherComponent = newLiveMeasure().setComponentUuid("C2").setMetricUuid("2");
- underTest.insertOrUpdate(db.getSession(), measure1);
- underTest.insertOrUpdate(db.getSession(), measure2);
- underTest.insertOrUpdate(db.getSession(), measureOnOtherComponent);
-
- underTest.deleteByComponentUuidExcludingMetricUuids(db.getSession(), "C1", Collections.emptyList());
-
- verifyTableSize(1);
- verifyPersisted(measureOnOtherComponent);
- }
-
- @Test
- public void countProjectsHavingMeasure() {
- MetricDto metric1 = db.measures().insertMetric();
- MetricDto metric2 = db.measures().insertMetric();
- ComponentDto project1 = db.components().insertPrivateProject();
- ComponentDto project2 = db.components().insertPrivateProject();
- db.measures().insertLiveMeasure(project1, metric1);
- db.measures().insertLiveMeasure(project2, metric1);
- db.measures().insertLiveMeasure(project1, metric2);
-
- assertThat(underTest.countProjectsHavingMeasure(db.getSession(), metric1.getKey())).isEqualTo(2);
- assertThat(underTest.countProjectsHavingMeasure(db.getSession(), metric2.getKey())).isOne();
- assertThat(underTest.countProjectsHavingMeasure(db.getSession(), "unknown")).isZero();
- }
-
- @Test
- public void upsert_inserts_or_updates_row() {
- if (!db.getDbClient().getDatabase().getDialect().supportsUpsert()) {
- return;
- }
-
- // insert
- LiveMeasureDto dto = newLiveMeasure();
- int count = underTest.upsert(db.getSession(), dto);
- verifyPersisted(dto);
- verifyTableSize(1);
- assertThat(count).isOne();
-
- // update
- dto.setValue(dto.getValue() + 1);
- dto.setData(dto.getDataAsString() + "_new");
- count = underTest.upsert(db.getSession(), dto);
- assertThat(count).isOne();
- verifyPersisted(dto);
- verifyTableSize(1);
- }
-
- @Test
- public void upsert_does_not_update_row_if_values_are_not_changed() {
- if (!db.getDbClient().getDatabase().getDialect().supportsUpsert()) {
- return;
- }
-
- LiveMeasureDto dto = newLiveMeasure();
- underTest.upsert(db.getSession(), dto);
-
- // update
- int count = underTest.upsert(db.getSession(), dto);
- assertThat(count).isZero();
- verifyPersisted(dto);
- verifyTableSize(1);
- }
-
- @Test
- public void upsert_updates_row_if_lob_data_is_changed() {
- if (!db.getDbClient().getDatabase().getDialect().supportsUpsert()) {
- return;
- }
-
- LiveMeasureDto dto = newLiveMeasure().setData(RandomStringUtils.random(10_000));
- underTest.upsert(db.getSession(), dto);
-
- // update
- dto.setData(RandomStringUtils.random(dto.getDataAsString().length() + 10));
- int count = underTest.upsert(db.getSession(), dto);
- assertThat(count).isOne();
- verifyPersisted(dto);
- verifyTableSize(1);
- }
-
- @Test
- public void upsert_does_not_update_row_if_lob_data_is_not_changed() {
- if (!db.getDbClient().getDatabase().getDialect().supportsUpsert()) {
- return;
- }
- LiveMeasureDto dto = newLiveMeasure().setData(RandomStringUtils.random(10_000));
- underTest.upsert(db.getSession(), dto);
-
- // update
- int count = underTest.upsert(db.getSession(), dto);
- assertThat(count).isZero();
- verifyPersisted(dto);
- verifyTableSize(1);
- }
-
- @Test
- public void upsert_updates_row_if_lob_data_is_removed() {
- if (!db.getDbClient().getDatabase().getDialect().supportsUpsert()) {
- return;
- }
-
- LiveMeasureDto dto = newLiveMeasure().setData(RandomStringUtils.random(10_000));
- underTest.upsert(db.getSession(), dto);
-
- // update
- dto.setData((String) null);
- int count = underTest.upsert(db.getSession(), dto);
- assertThat(count).isOne();
- verifyPersisted(dto);
- verifyTableSize(1);
- }
-
- @Test
- public void upsert_updates_row_if_value_is_changed() {
- if (!db.getDbClient().getDatabase().getDialect().supportsUpsert()) {
- return;
- }
- LiveMeasureDto dto = newLiveMeasure().setValue(40.0);
- underTest.upsert(db.getSession(), dto);
-
- // update
- dto.setValue(50.0);
- int count = underTest.upsert(db.getSession(), dto);
- assertThat(count).isOne();
- verifyPersisted(dto);
- verifyTableSize(1);
- }
-
- @Test
- public void upsert_updates_row_if_value_is_removed() {
- if (!db.getDbClient().getDatabase().getDialect().supportsUpsert()) {
- return;
- }
- LiveMeasureDto dto = newLiveMeasure().setValue(40.0);
- underTest.upsert(db.getSession(), dto);
-
- // update
- dto.setValue(null);
- int count = underTest.upsert(db.getSession(), dto);
- assertThat(count).isOne();
- verifyPersisted(dto);
- verifyTableSize(1);
- }
-
- @Test
- public void upsert_updates_row_if_value_is_added() {
- if (!db.getDbClient().getDatabase().getDialect().supportsUpsert()) {
- return;
- }
- LiveMeasureDto dto = newLiveMeasure().setValue(null);
- underTest.upsert(db.getSession(), dto);
-
- // update
- dto.setValue(40.0);
- int count = underTest.upsert(db.getSession(), dto);
- assertThat(count).isOne();
- verifyPersisted(dto);
- verifyTableSize(1);
- }
-
- @Test
- public void upsert_multiple_rows() {
- if (!db.getDbClient().getDatabase().getDialect().supportsUpsert()) {
- return;
- }
-
- // insert 30
- List<LiveMeasureDto> inserted = new ArrayList<>();
- IntStream.range(0, 30).forEach(i -> inserted.add(newLiveMeasure()));
- for (LiveMeasureDto dto : inserted) {
- underTest.upsert(db.getSession(), dto);
- }
- verifyTableSize(30);
-
- // update 10 with new values, update 5 without any change and insert new 50
- List<LiveMeasureDto> upserted = new ArrayList<>();
- IntStream.range(0, 10).forEach(i -> {
- LiveMeasureDto d = inserted.get(i);
- upserted.add(d.setValue(d.getValue() + 123));
- });
- upserted.addAll(inserted.subList(10, 15));
- IntStream.range(0, 50).forEach(i -> upserted.add(newLiveMeasure()));
- for (LiveMeasureDto dto : upserted) {
- underTest.upsert(db.getSession(), dto);
- }
- db.getSession().commit();
- verifyTableSize(80);
- }
-
- private void verifyTableSize(int expectedSize) {
- assertThat(db.countRowsOfTable(db.getSession(), "live_measures")).isEqualTo(expectedSize);
- }
-
- private void verifyPersisted(LiveMeasureDto dto) {
- List<LiveMeasureDto> selected = underTest.selectByComponentUuidsAndMetricUuids(db.getSession(), singletonList(dto.getComponentUuid()), singletonList(dto.getMetricUuid()));
- assertThat(selected).hasSize(1);
- assertThat(selected.get(0)).isEqualToComparingOnlyGivenFields(dto,
- // do not compare the field "uuid", which is used only for insert, not select
- "componentUuid", "projectUuid", "metricUuid", "value", "textValue", "data");
- }
-
- private Map<String, MetricDto> setupMetrics() {
- MetricDto ncloc = db.measures().insertMetric(m -> m.setKey("ncloc").setValueType(INT.toString()));
- MetricDto nclocDistribution = db.measures().insertMetric(m -> m.setKey("ncloc_language_distribution").setValueType(DATA.toString()));
- MetricDto lines = db.measures().insertMetric(m -> m.setKey("lines").setValueType(INT.toString()));
- return Map.of("ncloc", ncloc,
- "ncloc_language_distribution", nclocDistribution,
- "lines", lines);
- }
-
- private Map<String, ComponentDto> setupProjectsWithLoc(MetricDto ncloc, MetricDto nclocDistribution, MetricDto lines) {
- ComponentDto simpleProject = addProjectWithMeasure("simpleProject", ncloc, 10d);
- addMeasureToComponent(simpleProject, nclocDistribution, "java=10;js=0");
-
- ComponentDto projectWithBranchBiggerThanMaster = addProjectWithMeasure("projectWithBranchBiggerThanMaster", ncloc, 100d);
- addMeasureToComponent(projectWithBranchBiggerThanMaster, nclocDistribution, "java=100;js=0");
-
- ComponentDto notMasterBranch = addBranchToProjectWithMeasure(projectWithBranchBiggerThanMaster, "notMasterBranch", ncloc, 200d);
- addMeasureToComponent(notMasterBranch, nclocDistribution, "java=100;js=100");
-
- ComponentDto projectWithLinesButNoLoc = addProjectWithMeasure("projectWithLinesButNoLoc", lines, 365d);
- addMeasureToComponent(projectWithLinesButNoLoc, nclocDistribution, "java=0;js=0");
- addMeasureToComponent(projectWithLinesButNoLoc, ncloc, 0d, false);
-
- ComponentDto projectWithTieOnBranchSize = addProjectWithMeasure("projectWithTieOnBranchSize", ncloc, 250d);
- addMeasureToComponent(projectWithTieOnBranchSize, nclocDistribution, "java=250;js=0");
- ComponentDto tieBranch = addBranchToProjectWithMeasure(projectWithTieOnBranchSize, "tieBranch", ncloc, 250d);
- addMeasureToComponent(tieBranch, nclocDistribution, "java=250;js=0");
-
- ComponentDto projectWithTieOnOtherBranches = addProjectWithMeasure("projectWithTieOnOtherBranches", ncloc, 220d);
- addMeasureToComponent(projectWithTieOnOtherBranches, nclocDistribution, "java=220;js=0");
- ComponentDto tieBranch1 = addBranchToProjectWithMeasure(projectWithTieOnOtherBranches, "tieBranch1", ncloc, 230d);
- addMeasureToComponent(tieBranch1, nclocDistribution, "java=230;js=0");
- ComponentDto tieBranch2 = addBranchToProjectWithMeasure(projectWithTieOnOtherBranches, "tieBranch2", ncloc, 230d);
- addMeasureToComponent(tieBranch2, nclocDistribution, "java=230;js=0");
-
- return Map.of("simpleProject", simpleProject,
- "projectWithBranchBiggerThanMaster", projectWithBranchBiggerThanMaster,
- "notMasterBranch", notMasterBranch,
- "projectWithLinesButNoLoc", projectWithLinesButNoLoc,
- "projectWithTieOnBranchSize", projectWithTieOnBranchSize,
- "tieBranch", tieBranch,
- "projectWithTieOnOtherBranches", projectWithTieOnOtherBranches,
- "tieBranch1", tieBranch1,
- "tieBranch2", tieBranch2);
- }
-
- private ComponentDto addProjectWithMeasure(String projectKey, MetricDto metric, double metricValue) {
- ComponentDto project = db.components().insertPublicProject(p -> p.setKey(projectKey));
- addMeasureToComponent(project, metric, metricValue, true);
- return project;
- }
-
- private void addMeasureToComponent(ComponentDto component, MetricDto metric, double metricValue, boolean addSnapshot) {
- db.measures().insertLiveMeasure(component, metric, m -> m.setValue(metricValue));
- if (addSnapshot) {
- db.components().insertSnapshot(component, t -> t.setLast(true));
- }
- }
-
- private void addMeasureToComponent(ComponentDto component, MetricDto metric, String metricValue) {
- db.measures().insertLiveMeasure(component, metric, m -> m.setData(metricValue));
- }
-
- private ComponentDto addBranchToProjectWithMeasure(ComponentDto project, String branchKey, MetricDto metric, double metricValue) {
- ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setBranchType(BranchType.BRANCH).setKey(branchKey));
- addMeasureToComponent(branch, metric, metricValue, true);
- return branch;
- }
-
- private void assertLocForProject(LargestBranchNclocDto result, String projectKey, String branchKey, long linesOfCode) {
- assertThat(result.getProjectKey()).isEqualTo(projectKey);
- assertThat(result.getBranchName()).isEqualTo(branchKey);
- assertThat(result.getLoc()).isEqualTo(linesOfCode);
- }
- }
|