/** * Add the given duration to the current one. */ public Duration add(Duration with) { return Duration.create(durationInMinutes + with.durationInMinutes); }
/** * Create a Duration object from a number of minutes */ public Duration create(long minutes) { return Duration.create(minutes); }
/** * Subtract the given duration to the current one. */ public Duration subtract(Duration with) { return Duration.create(durationInMinutes - with.durationInMinutes); }
/** * Multiply the duration with the given factor. */ public Duration multiply(int factor) { return Duration.create(durationInMinutes * factor); }
@CheckForNull public Duration effort() { Number effort = getNullableField(IssueIndexDefinition.FIELD_ISSUE_EFFORT); return (effort != null) ? Duration.create(effort.longValue()) : null; }
@Test public void decode() { assertThat(Duration.decode(" 15 d 23 h 42min ", HOURS_IN_DAY)).isEqualTo(Duration.create(15 * ONE_DAY_IN_MINUTES + 23 * ONE_HOUR_IN_MINUTES + 42 * ONE_MINUTE)); assertThat(Duration.decode("15d23h42min", HOURS_IN_DAY)).isEqualTo(Duration.create(15 * ONE_DAY_IN_MINUTES + 23 * ONE_HOUR_IN_MINUTES + 42 * ONE_MINUTE)); assertThat(Duration.decode("23h", HOURS_IN_DAY)).isEqualTo(Duration.create(23 * ONE_HOUR_IN_MINUTES)); assertThat(Duration.decode("15d", HOURS_IN_DAY)).isEqualTo(Duration.create(15 * ONE_DAY_IN_MINUTES)); assertThat(Duration.decode("42min", HOURS_IN_DAY)).isEqualTo(Duration.create(42 * ONE_MINUTE)); assertThat(Duration.decode("0min", HOURS_IN_DAY)).isEqualTo(Duration.create(0)); assertThat(Duration.decode("25h61min", HOURS_IN_DAY)).isEqualTo(Duration.create(25 * ONE_HOUR_IN_MINUTES + 61)); }
@Test public void encode() { assertThat(Duration.create(2 * ONE_DAY_IN_MINUTES + 5 * ONE_HOUR_IN_MINUTES + 46 * ONE_MINUTE).encode(HOURS_IN_DAY)).isEqualTo("2d5h46min"); assertThat(Duration.create(ONE_DAY_IN_MINUTES).encode(HOURS_IN_DAY)).isEqualTo("1d"); assertThat(Duration.create(ONE_HOUR_IN_MINUTES).encode(HOURS_IN_DAY)).isEqualTo("1h"); assertThat(Duration.create(HOURS_IN_DAY * ONE_HOUR_IN_MINUTES).encode(HOURS_IN_DAY)).isEqualTo("1d"); assertThat(Duration.create(ONE_MINUTE).encode(HOURS_IN_DAY)).isEqualTo("1min"); assertThat(Duration.create(0).encode(HOURS_IN_DAY)).isEqualTo("0min"); }
@Test public void format() { assertThat(underTest.format(Duration.create(5 * ONE_DAY))).isEqualTo("5d"); assertThat(underTest.format(Duration.create(2 * ONE_HOUR))).isEqualTo("2h"); assertThat(underTest.format(Duration.create(ONE_MINUTE))).isEqualTo("1min"); assertThat(underTest.format(Duration.create(5 * ONE_DAY + 2 * ONE_HOUR))).isEqualTo("5d 2h"); assertThat(underTest.format(Duration.create(2 * ONE_HOUR + ONE_MINUTE))).isEqualTo("2h 1min"); assertThat(underTest.format(Duration.create(5 * ONE_DAY + 2 * ONE_HOUR + ONE_MINUTE))).isEqualTo("5d 2h"); }
@Test public void create_from_duration_in_minutes() { Duration duration = Duration.create(ONE_DAY_IN_MINUTES + ONE_HOUR_IN_MINUTES + ONE_MINUTE); assertThat(duration.toMinutes()).isEqualTo(ONE_DAY_IN_MINUTES + ONE_HOUR_IN_MINUTES + ONE_MINUTE); }
@Test public void display_zero_without_unit() { assertThat(underTest.format(Duration.create(0))).isEqualTo("0"); }
@Test public void test_equals_and_hashcode() throws Exception { Duration duration = Duration.create(ONE_DAY_IN_MINUTES + ONE_HOUR_IN_MINUTES + ONE_MINUTE); Duration durationWithSameValue = Duration.create(ONE_DAY_IN_MINUTES + ONE_HOUR_IN_MINUTES + ONE_MINUTE); Duration durationWithDifferentValue = Duration.create(ONE_DAY_IN_MINUTES + ONE_HOUR_IN_MINUTES); assertThat(duration).isEqualTo(duration); assertThat(durationWithSameValue).isEqualTo(duration); assertThat(durationWithDifferentValue).isNotEqualTo(duration); assertThat(duration).isNotEqualTo(null); assertThat(duration.hashCode()).isEqualTo(duration.hashCode()); assertThat(durationWithSameValue.hashCode()).isEqualTo(duration.hashCode()); assertThat(durationWithDifferentValue.hashCode()).isNotEqualTo(duration.hashCode()); }
@Test public void set_debt() { underTest.setDebt(Duration.create(55)); assertThat(underTest.getFieldValue(EFFORT + ".count")).isEqualTo("55min"); }
private DefaultIssue oldBugIssue(long effort) { return newCodeSmellIssueWithoutEffort() .setEffort(Duration.create(effort)) .setType(RuleType.BUG) .setCreationDate(new Date(OLD_ISSUES_DATES[random.nextInt(OLD_ISSUES_DATES.length)])); }
private DefaultIssue oldCodeSmellIssue(long effort) { return newCodeSmellIssueWithoutEffort() .setEffort(Duration.create(effort)) .setType(RuleType.CODE_SMELL) .setCreationDate(new Date(OLD_ISSUES_DATES[random.nextInt(OLD_ISSUES_DATES.length)])); }
private DefaultIssue oldVulnerabilityIssue(long effort) { return newCodeSmellIssueWithoutEffort() .setEffort(Duration.create(effort)) .setType(RuleType.VULNERABILITY) .setCreationDate(new Date(OLD_ISSUES_DATES[random.nextInt(OLD_ISSUES_DATES.length)])); }
@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); }
@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(); }
@Test public void copy_effort_for_external_issues() { issue.setGap(null); issue.setIsFromExternalRuleEngine(true); issue.setEffort(Duration.create(20l)); rule.setFunction(null); assertThat(underTest.calculate(issue).toMinutes()).isEqualTo(20l); }
private void sendNewIssuesNotification(NewIssuesStatistics statistics, Component project, long analysisDate, NotificationStatistics notificationStatistics) { NewIssuesStatistics.Stats globalStatistics = statistics.globalStatistics(); NewIssuesNotification notification = newIssuesNotificationFactory .newNewIssuesNotification() .setProject(project.getKey(), project.getName(), getBranchName(), getPullRequest()) .setProjectVersion(project.getProjectAttributes().getVersion()) .setAnalysisDate(new Date(analysisDate)) .setStatistics(project.getName(), globalStatistics) .setDebt(Duration.create(globalStatistics.effort().getOnLeak())); notificationStatistics.newIssuesDeliveries += service.deliver(notification); notificationStatistics.newIssues++; }