use of org.sonar.api.utils.Duration in project sonarqube by SonarSource.
the class IssueFieldsSetterTest method set_past_technical_debt.
@Test
public void set_past_technical_debt() {
Duration newDebt = Duration.create(15 * 8 * 60);
Duration previousDebt = Duration.create(10 * 8 * 60);
issue.setEffort(newDebt);
boolean updated = underTest.setPastEffort(issue, previousDebt, context);
assertThat(updated).isTrue();
assertThat(issue.effort()).isEqualTo(newDebt);
assertThat(issue.mustSendNotifications()).isFalse();
FieldDiffs.Diff diff = issue.currentChange().get(TECHNICAL_DEBT);
assertThat(diff.oldValue()).isEqualTo(10L * 8 * 60);
assertThat(diff.newValue()).isEqualTo(15L * 8 * 60);
}
use of org.sonar.api.utils.Duration in project sonarqube by SonarSource.
the class IssueFieldsSetterTest method set_past_technical_debt_without_previous_value.
@Test
public void set_past_technical_debt_without_previous_value() {
Duration newDebt = Duration.create(15 * 8 * 60);
issue.setEffort(newDebt);
boolean updated = underTest.setPastEffort(issue, null, context);
assertThat(updated).isTrue();
assertThat(issue.effort()).isEqualTo(newDebt);
assertThat(issue.mustSendNotifications()).isFalse();
FieldDiffs.Diff diff = issue.currentChange().get(TECHNICAL_DEBT);
assertThat(diff.oldValue()).isNull();
assertThat(diff.newValue()).isEqualTo(15L * 8 * 60);
}
use of org.sonar.api.utils.Duration in project sonarqube by SonarSource.
the class IssueFieldsSetterTest method set_past_technical_debt_with_null_new_value.
@Test
public void set_past_technical_debt_with_null_new_value() {
issue.setEffort(null);
Duration previousDebt = Duration.create(10 * 8 * 60);
boolean updated = underTest.setPastEffort(issue, previousDebt, context);
assertThat(updated).isTrue();
assertThat(issue.effort()).isNull();
assertThat(issue.mustSendNotifications()).isFalse();
FieldDiffs.Diff diff = issue.currentChange().get(TECHNICAL_DEBT);
assertThat(diff.oldValue()).isEqualTo(10L * 8 * 60);
assertThat(diff.newValue()).isNull();
}
use of org.sonar.api.utils.Duration in project sonarqube by SonarSource.
the class DebtCalculator method calculate.
@CheckForNull
public Duration calculate(DefaultIssue issue) {
if (issue.isFromExternalRuleEngine()) {
return issue.effort();
}
Rule rule = ruleRepository.getByKey(issue.ruleKey());
DebtRemediationFunction fn = rule.getRemediationFunction();
if (fn != null) {
verifyEffortToFix(issue, fn);
Duration debt = Duration.create(0);
String gapMultiplier = fn.gapMultiplier();
if (fn.type().usesGapMultiplier() && !Strings.isNullOrEmpty(gapMultiplier)) {
int effortToFixValue = MoreObjects.firstNonNull(issue.gap(), 1).intValue();
// TODO convert to Duration directly in Rule#remediationFunction -> better performance + error handling
debt = durations.decode(gapMultiplier).multiply(effortToFixValue);
}
String baseEffort = fn.baseEffort();
if (fn.type().usesBaseEffort() && !Strings.isNullOrEmpty(baseEffort)) {
// TODO convert to Duration directly in Rule#remediationFunction -> better performance + error handling
debt = debt.add(durations.decode(baseEffort));
}
return debt;
}
return null;
}
use of org.sonar.api.utils.Duration in project sonarqube by SonarSource.
the class SendIssueNotificationsStepTest method send_global_new_issues_notification_only_for_non_backdated_issues.
@Test
public void send_global_new_issues_notification_only_for_non_backdated_issues() {
Random random = new Random();
Integer[] efforts = IntStream.range(0, 1 + random.nextInt(10)).mapToObj(i -> 10_000 * i).toArray(Integer[]::new);
Integer[] backDatedEfforts = IntStream.range(0, 1 + random.nextInt(10)).mapToObj(i -> 10 + random.nextInt(100)).toArray(Integer[]::new);
Duration expectedEffort = Duration.create(stream(efforts).mapToInt(i -> i).sum());
List<DefaultIssue> issues = concat(stream(efforts).map(effort -> createIssue().setType(randomRuleType).setEffort(Duration.create(effort)).setCreationDate(new Date(ANALYSE_DATE))), stream(backDatedEfforts).map(effort -> createIssue().setType(randomRuleType).setEffort(Duration.create(effort)).setCreationDate(new Date(ANALYSE_DATE - FIVE_MINUTES_IN_MS)))).collect(toList());
shuffle(issues);
DiskCache.CacheAppender issueCache = this.protoIssueCache.newAppender();
issues.forEach(issueCache::append);
issueCache.close();
analysisMetadataHolder.setProject(new Project(PROJECT.getUuid(), PROJECT.getKey(), PROJECT.getName(), null, emptyList()));
when(notificationService.hasProjectSubscribersForTypes(PROJECT.getUuid(), NOTIF_TYPES)).thenReturn(true);
TestComputationStepContext context = new TestComputationStepContext();
underTest.execute(context);
verify(notificationService).deliver(newIssuesNotificationMock);
ArgumentCaptor<NewIssuesStatistics.Stats> statsCaptor = forClass(NewIssuesStatistics.Stats.class);
verify(newIssuesNotificationMock).setStatistics(eq(PROJECT.getName()), statsCaptor.capture());
verify(newIssuesNotificationMock).setDebt(expectedEffort);
NewIssuesStatistics.Stats stats = statsCaptor.getValue();
assertThat(stats.hasIssues()).isTrue();
// just checking all issues have been added to the stats
DistributedMetricStatsInt severity = stats.getDistributedMetricStats(NewIssuesStatistics.Metric.RULE_TYPE);
assertThat(severity.getOnCurrentAnalysis()).isEqualTo(efforts.length);
assertThat(severity.getTotal()).isEqualTo(backDatedEfforts.length + efforts.length);
verifyStatistics(context, 1, 0, 0);
}
Aggregations