@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())); SumNclocDbQuery query = SumNclocDbQuery.builder() .setOnlyPrivateProjects(false) .setOrganizationUuid(db.getDefaultOrganization().getUuid()) .build(); long result = underTest.sumNclocOfBiggestLongLivingBranch(db.getSession(), query); assertThat(result).isEqualTo(0L); }
@Test public void countNcloc() { OrganizationDto organization = db.organizations().insert(); 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().insertMainBranch(organization); db.measures().insertLiveMeasure(simpleProject, ncloc, m -> m.setValue(10d)); ComponentDto projectWithBiggerLongLivingBranch = db.components().insertMainBranch(organization); ComponentDto bigLongLivingLongBranch = db.components().insertProjectBranch(projectWithBiggerLongLivingBranch, b -> b.setBranchType(BranchType.LONG)); db.measures().insertLiveMeasure(projectWithBiggerLongLivingBranch, ncloc, m -> m.setValue(100d)); db.measures().insertLiveMeasure(bigLongLivingLongBranch, ncloc, m -> m.setValue(200d)); ComponentDto projectWithLinesButNoLoc = db.components().insertMainBranch(organization); db.measures().insertLiveMeasure(projectWithLinesButNoLoc, lines, m -> m.setValue(365d)); db.measures().insertLiveMeasure(projectWithLinesButNoLoc, ncloc, m -> m.setValue(0d)); SumNclocDbQuery query = SumNclocDbQuery.builder() .setOnlyPrivateProjects(false) .setOrganizationUuid(organization.getUuid()) .build(); long result = underTest.sumNclocOfBiggestLongLivingBranch(db.getSession(), query); assertThat(result).isEqualTo(10L + 200L); }
@Test public void countNcloc_and_exclude_project() { OrganizationDto organization = db.organizations().insert(); MetricDto ncloc = db.measures().insertMetric(m -> m.setKey("ncloc").setValueType(INT.toString())); ComponentDto simpleProject = db.components().insertMainBranch(organization); db.measures().insertLiveMeasure(simpleProject, ncloc, m -> m.setValue(10d)); ComponentDto projectWithBiggerLongLivingBranch = db.components().insertMainBranch(organization); ComponentDto bigLongLivingBranch = db.components().insertProjectBranch(projectWithBiggerLongLivingBranch, b -> b.setBranchType(BranchType.LONG)); db.measures().insertLiveMeasure(projectWithBiggerLongLivingBranch, ncloc, m -> m.setValue(100d)); db.measures().insertLiveMeasure(bigLongLivingBranch, ncloc, m -> m.setValue(200d)); ComponentDto projectToExclude = db.components().insertMainBranch(organization); ComponentDto projectToExcludeBranch = db.components().insertProjectBranch(projectToExclude, b -> b.setBranchType(BranchType.LONG)); db.measures().insertLiveMeasure(projectToExclude, ncloc, m -> m.setValue(300d)); db.measures().insertLiveMeasure(projectToExcludeBranch, ncloc, m -> m.setValue(400d)); SumNclocDbQuery query = SumNclocDbQuery.builder() .setOrganizationUuid(organization.getUuid()) .setProjectUuidToExclude(projectToExclude.uuid()) .setOnlyPrivateProjects(false) .build(); long result = underTest.sumNclocOfBiggestLongLivingBranch(db.getSession(), query); assertThat(result).isEqualTo(10L + 200L); }
private static Comparable getValueForComparison(Metric metric, String value) { Comparable valueToCompare = null; try { if (isADouble(metric)) { valueToCompare = Double.parseDouble(value); } else if (isAInteger(metric)) { valueToCompare = parseInteger(value); } else if (isAString(metric)) { valueToCompare = value; } else if (isABoolean(metric)) { valueToCompare = Integer.parseInt(value); } else if (isAWorkDuration(metric)) { valueToCompare = Long.parseLong(value); } else { throw new NotImplementedException(metric.getType().toString()); } } catch (NumberFormatException badValueFormat) { throw new IllegalArgumentException(String.format("Quality Gate: Unable to parse value '%s' to compare against %s", value, metric.getName())); } return valueToCompare; }
private static Comparable getMeasureValue(ResolvedCondition condition, Measure measure) { Metric metric = condition.metric(); if (isADouble(metric)) { return getValue(condition, measure); } if (isAInteger(metric)) { return parseInteger(condition, measure); } if (isAWorkDuration(metric)) { return parseLong(condition, measure); } if (condition.period() == null) { return getMeasureValueForStringOrBoolean(metric, measure); } throw new NotImplementedException(metric.getType().toString()); }
private static Comparable getMeasureValueForStringOrBoolean(Metric metric, Measure measure) { if (isAString(metric)) { return measure.getData(); } if (isABoolean(metric)) { return measure.getValue().intValue(); } throw new NotImplementedException(metric.getType().toString()); }
private void setNcloc(double ncloc) { ComponentDto project = db.components().insertMainBranch(); MetricDto nclocMetric = db.measures().insertMetric(m -> m.setValueType(INT.toString()).setKey(NCLOC_KEY)); db.measures().insertLiveMeasure(project, nclocMetric, m -> m.setValue(ncloc)); } }