import org.sonar.api.resources.Project;
import org.sonar.api.resources.Resource;
import org.sonar.api.utils.WorkDuration;
-import org.sonar.api.utils.WorkDurationFactory;
import org.sonar.batch.components.Period;
import org.sonar.batch.components.TimeMachineConfiguration;
import org.sonar.batch.debt.IssueChangelogDebtCalculator;
import javax.annotation.Nullable;
-
import java.util.Collection;
import java.util.Date;
import java.util.List;
private final ResourcePerspectives perspectives;
private final TimeMachineConfiguration timeMachineConfiguration;
private final IssueChangelogDebtCalculator issueChangelogDebtCalculator;
- private final WorkDurationFactory workDurationFactory;
public NewTechnicalDebtDecorator(ResourcePerspectives perspectives, TimeMachineConfiguration timeMachineConfiguration,
- IssueChangelogDebtCalculator issueChangelogDebtCalculator, WorkDurationFactory workDurationFactory) {
+ IssueChangelogDebtCalculator issueChangelogDebtCalculator) {
this.perspectives = perspectives;
this.timeMachineConfiguration = timeMachineConfiguration;
this.issueChangelogDebtCalculator = issueChangelogDebtCalculator;
- this.workDurationFactory = workDurationFactory;
}
public boolean shouldExecuteOnProject(Project project) {
}
private Double calculateNewTechnicalDebtValue(Collection<Issue> issues, @Nullable Date periodDate) {
- WorkDuration duration = workDurationFactory.createFromWorkingLong(0l);
+ WorkDuration duration = null;
for (Issue issue : issues) {
WorkDuration debt = issueChangelogDebtCalculator.calculateNewTechnicalDebt(issue, periodDate);
if (debt != null) {
- duration = duration.add(debt);
+ duration = duration != null ? duration.add(debt) : debt;
}
}
- return duration.toWorkingDays();
+ return duration != null ? duration.toWorkingDays() : 0d;
}
private boolean shouldSaveNewMetrics(DecoratorContext context) {
package org.sonar.plugins.core.technicaldebt;
import org.apache.commons.lang.ObjectUtils;
+import org.apache.commons.lang.time.DateUtils;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
when(perspectives.as(Issuable.class, resource)).thenReturn(issuable);
rightNow = new Date();
- elevenDaysAgo = org.apache.commons.lang.time.DateUtils.addDays(rightNow, -11);
- tenDaysAgo = org.apache.commons.lang.time.DateUtils.addDays(rightNow, -10);
- nineDaysAgo = org.apache.commons.lang.time.DateUtils.addDays(rightNow, -9);
- fiveDaysAgo = org.apache.commons.lang.time.DateUtils.addDays(rightNow, -5);
- fourDaysAgo = org.apache.commons.lang.time.DateUtils.addDays(rightNow, -4);
+ elevenDaysAgo = DateUtils.addDays(rightNow, -11);
+ tenDaysAgo = DateUtils.addDays(rightNow, -10);
+ nineDaysAgo = DateUtils.addDays(rightNow, -9);
+ fiveDaysAgo = DateUtils.addDays(rightNow, -5);
+ fourDaysAgo = DateUtils.addDays(rightNow, -4);
when(timeMachineConfiguration.periods()).thenReturn(newArrayList(new Period(1, fiveDaysAgo), new Period(2, tenDaysAgo)));
WorkDurationFactory workDurationFactory = new WorkDurationFactory(settings);
- decorator = new NewTechnicalDebtDecorator(perspectives, timeMachineConfiguration, new IssueChangelogDebtCalculator(workDurationFactory), workDurationFactory);
+ decorator = new NewTechnicalDebtDecorator(perspectives, timeMachineConfiguration, new IssueChangelogDebtCalculator(workDurationFactory));
}
@Test
import javax.annotation.CheckForNull;
import javax.annotation.Nullable;
-
import java.util.*;
import static com.google.common.collect.Lists.newArrayList;
return diffs;
}
+ @CheckForNull
private WorkDuration newValue(FieldDiffs fieldDiffs) {
for (Map.Entry<String, FieldDiffs.Diff> entry : fieldDiffs.diffs().entrySet()) {
if (entry.getKey().equals(IssueUpdater.TECHNICAL_DEBT)) {
Long newValue = entry.getValue().newValueLong();
- return newValue != null ? workDurationFactory.createFromWorkingLong(newValue) : workDurationFactory.createFromWorkingLong(0l);
+ return workDurationFactory.createFromWorkingLong(newValue);
}
}
- return workDurationFactory.createFromWorkingLong(0l);
+ return null;
}
+ @CheckForNull
private WorkDuration oldValue(FieldDiffs fieldDiffs) {
for (Map.Entry<String, FieldDiffs.Diff> entry : fieldDiffs.diffs().entrySet()) {
if (entry.getKey().equals(IssueUpdater.TECHNICAL_DEBT)) {
Long value = entry.getValue().oldValueLong();
- return value != null ? workDurationFactory.createFromWorkingLong(value) : workDurationFactory.createFromWorkingLong(0l);
+ return workDurationFactory.createFromWorkingLong(value);
}
}
- return workDurationFactory.createFromWorkingLong(0l);
+ return null;
}
private boolean isAfter(@Nullable Date currentDate, @Nullable Date pastDate) {
package org.sonar.api.utils;
+import com.google.common.annotations.VisibleForTesting;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;
import javax.annotation.Nullable;
-
import java.io.Serializable;
/**
return minutes;
}
- public int hoursInDay() {
+ @VisibleForTesting
+ int hoursInDay() {
return hoursInDay;
}