@SafeVarargs public final LiveMeasureDto insertLiveMeasure(ComponentDto component, MetricDto metric, Consumer<LiveMeasureDto>... consumers) { LiveMeasureDto dto = newLiveMeasure(component, metric); Arrays.stream(consumers).forEach(c -> c.accept(dto)); dbClient.liveMeasureDao().insert(dbSession, dto); dbSession.commit(); return dto; }
@SafeVarargs public final MeasureDto insertMeasure(ComponentDto component, SnapshotDto analysis, MetricDto metricDto, Consumer<MeasureDto>... consumers) { MeasureDto measureDto = newMeasureDto(metricDto, component, analysis); Arrays.stream(consumers).forEach(c -> c.accept(measureDto)); dbClient.measureDao().insert(dbSession, measureDto); dbSession.commit(); return measureDto; }
private void insertMeasure(String id, String analysisUuid, String componentUuid, int metricId) { MeasureDto measure = MeasureTesting.newMeasure() .setAnalysisUuid(analysisUuid) .setComponentUuid(componentUuid) .setMetricId(metricId) // as ids can't be forced when inserting measures, the field "data" // is used to store a virtual id. It is used then in assertions. .setData(id); db.getDbClient().measureDao().insert(db.getSession(), measure); }
@Test public void selectByComponentUuidsAndMetricIds_returns_empty_list_if_component_does_not_match() { LiveMeasureDto measure = newLiveMeasure(); underTest.insert(db.getSession(), measure); List<LiveMeasureDto> selected = underTest.selectByComponentUuidsAndMetricIds(db.getSession(), singletonList("_missing_"), singletonList(measure.getMetricId())); assertThat(selected).isEmpty(); }
@Test public void return_metrics() { dbClient.measureDao().insert(dbSession, newMeasureDto(complexityMetric, project, analysis).setValue(42.0d)); db.commit(); SearchHistoryResponse result = call(); assertThat(result.getMeasuresList()).hasSize(3) .extracting(HistoryMeasure::getMetric) .containsExactly(complexityMetric.getKey(), nclocMetric.getKey(), newViolationMetric.getKey()); }
@Test public void selectByComponentUuidsAndMetricKeys_returns_empty_list_if_component_does_not_match() { LiveMeasureDto measure = newLiveMeasure().setMetricId(metric.getId()); underTest.insert(db.getSession(), measure); List<LiveMeasureDto> selected = underTest.selectByComponentUuidsAndMetricKeys(db.getSession(), singletonList("_missing_"), singletonList(metric.getKey())); assertThat(selected).isEmpty(); }
@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(); }
@Test public void selectByComponentUuidsAndMetricKeys_returns_empty_list_if_metric_does_not_match() { LiveMeasureDto measure = newLiveMeasure().setMetricId(metric.getId()); underTest.insert(db.getSession(), measure); List<LiveMeasureDto> selected = underTest.selectByComponentUuidsAndMetricKeys(db.getSession(), singletonList(measure.getComponentUuid()), singletonList("_other_")); assertThat(selected).isEmpty(); }
@Test public void test_json_example() throws IOException { OrganizationDto organization = db.organizations().insert(); ComponentDto project = db.components().insertPrivateProject(organization); userSession.addProjectPermission(UserRole.USER, project); MetricDto gateDetailsMetric = insertGateDetailMetric(); SnapshotDto snapshot = dbClient.snapshotDao().insert(dbSession, newAnalysis(project) .setPeriodMode("last_version") .setPeriodParam("2015-12-07") .setPeriodDate(956789123987L)); dbClient.measureDao().insert(dbSession, newMeasureDto(gateDetailsMetric, project, snapshot) .setData(IOUtils.toString(getClass().getResource("ProjectStatusActionTest/measure_data.json")))); dbSession.commit(); String response = ws.newRequest() .setParam("analysisId", snapshot.getUuid()) .execute().getInput(); assertJson(response).isSimilarTo(getClass().getResource("project_status-example.json")); }
@Test public void selectByComponentUuidsAndMetricIds_returns_empty_list_if_metric_does_not_match() { LiveMeasureDto measure = newLiveMeasure().setMetricId(metric.getId()); underTest.insert(db.getSession(), measure); int otherMetricId = metric.getId() + 100; List<LiveMeasureDto> selected = underTest.selectByComponentUuidsAndMetricIds(db.getSession(), singletonList(measure.getComponentUuid()), singletonList(otherMetricId)); assertThat(selected).isEmpty(); }
@Test public void test_selectMeasure() { 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); assertThat(underTest.selectMeasure(db.getSession(), lastAnalysis.getUuid(), file.uuid(), metric.getKey()).get()) .isEqualToComparingFieldByField(lastMeasure); assertThat(underTest.selectMeasure(db.getSession(), pastAnalysis.getUuid(), file.uuid(), metric.getKey()).get()) .isEqualToComparingFieldByField(pastMeasure); assertThat(underTest.selectMeasure(db.getSession(), "_missing_", file.uuid(), metric.getKey())).isEmpty(); assertThat(underTest.selectMeasure(db.getSession(), pastAnalysis.getUuid(), "_missing_", metric.getKey())).isEmpty(); assertThat(underTest.selectMeasure(db.getSession(), pastAnalysis.getUuid(), file.uuid(), "_missing_")).isEmpty(); }
@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 deleteByComponentUuidExcludingMetricIds_with_empty_metrics() { LiveMeasureDto measure1 = newLiveMeasure().setComponentUuid("C1").setMetricId(1); LiveMeasureDto measure2 = newLiveMeasure().setComponentUuid("C1").setMetricId(2); LiveMeasureDto measureOnOtherComponent = newLiveMeasure().setComponentUuid("C2").setMetricId(2); underTest.insertOrUpdate(db.getSession(), measure1); underTest.insertOrUpdate(db.getSession(), measure2); underTest.insertOrUpdate(db.getSession(), measureOnOtherComponent); int count = underTest.deleteByComponentUuidExcludingMetricIds(db.getSession(), "C1", Collections.emptyList()); assertThat(count).isEqualTo(2); verifyTableSize(1); verifyPersisted(measureOnOtherComponent); }
@Test public void selectByComponentUuidAndMetricKey() { LiveMeasureDto measure = newLiveMeasure().setMetricId(metric.getId()); underTest.insert(db.getSession(), measure); Optional<LiveMeasureDto> selected = underTest.selectByComponentUuidAndMetricKey(db.getSession(), measure.getComponentUuid(), metric.getKey()); assertThat(selected).isNotEmpty(); assertThat(selected.get()).isEqualToComparingFieldByField(measure); }
@Test public void deleteByComponentUuidExcludingMetricIds() { LiveMeasureDto measure1 = newLiveMeasure().setComponentUuid("C1").setMetricId(1); LiveMeasureDto measure2 = newLiveMeasure().setComponentUuid("C1").setMetricId(2); LiveMeasureDto measure3 = newLiveMeasure().setComponentUuid("C1").setMetricId(3); LiveMeasureDto measureOtherComponent = newLiveMeasure().setComponentUuid("C2").setMetricId(3); underTest.insertOrUpdate(db.getSession(), measure1); underTest.insertOrUpdate(db.getSession(), measure2); underTest.insertOrUpdate(db.getSession(), measure3); underTest.insertOrUpdate(db.getSession(), measureOtherComponent); int count = underTest.deleteByComponentUuidExcludingMetricIds(db.getSession(), "C1", Arrays.asList(1, 2)); verifyTableSize(3); verifyPersisted(measure1); verifyPersisted(measure2); verifyPersisted(measureOtherComponent); assertThat(count).isEqualTo(1); }
@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() .setMetricIds(singleton(metric.getId())) .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.getMetricId()).isEqualTo(metric.getId()); assertThat(result.getValue()).isEqualTo(3.14); }
@Test public void selectByComponentUuidsAndMetricIds() { LiveMeasureDto measure1 = newLiveMeasure().setMetricId(metric.getId()); LiveMeasureDto measure2 = newLiveMeasure().setMetricId(metric.getId()); underTest.insert(db.getSession(), measure1); underTest.insert(db.getSession(), measure2); List<LiveMeasureDto> selected = underTest.selectByComponentUuidsAndMetricIds(db.getSession(), asList(measure1.getComponentUuid(), measure2.getComponentUuid()), singletonList(metric.getId())); assertThat(selected) .extracting(LiveMeasureDto::getComponentUuid, LiveMeasureDto::getProjectUuid, LiveMeasureDto::getMetricId, LiveMeasureDto::getValue, LiveMeasureDto::getDataAsString) .containsExactlyInAnyOrder( tuple(measure1.getComponentUuid(), measure1.getProjectUuid(), measure1.getMetricId(), measure1.getValue(), measure1.getDataAsString()), tuple(measure2.getComponentUuid(), measure2.getProjectUuid(), measure2.getMetricId(), measure2.getValue(), measure2.getDataAsString())); assertThat(underTest.selectByComponentUuidsAndMetricIds(db.getSession(), emptyList(), singletonList(metric.getId()))).isEmpty(); assertThat(underTest.selectByComponentUuidsAndMetricIds(db.getSession(), singletonList(measure1.getComponentUuid()), emptyList())).isEmpty(); }
@Test public void selectByComponentUuidsAndMetricKeys() { LiveMeasureDto measure1 = newLiveMeasure().setMetricId(metric.getId()); LiveMeasureDto measure2 = newLiveMeasure().setMetricId(metric.getId()); 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::getMetricId, LiveMeasureDto::getValue, LiveMeasureDto::getDataAsString) .containsExactlyInAnyOrder( tuple(measure1.getComponentUuid(), measure1.getProjectUuid(), measure1.getMetricId(), measure1.getValue(), measure1.getDataAsString()), tuple(measure2.getComponentUuid(), measure2.getProjectUuid(), measure2.getMetricId(), 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 selectByComponentUuidAndMetricKey_return_empty_if_metric_does_not_match() { LiveMeasureDto measure = newLiveMeasure().setMetricId(metric.getId()); underTest.insert(db.getSession(), measure); assertThat(underTest.selectByComponentUuidAndMetricKey(db.getSession(), measure.getComponentUuid(), "_missing_")).isEmpty(); }
@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).isEqualTo(0); verifyPersisted(dto); verifyTableSize(1); }