RandomDuration { public static Duration getRandomDurationMinuteBased( final Duration minDuration, final Duration maxDuration) { if (minDuration.toMinutes() >= maxDuration.toMinutes()) { return minDuration; } final var maxMinutes = maxDuration.minus(minDuration).toMinutes(); final var threadLocalRandom = ThreadLocalRandom.current(); final var randomMinutes = threadLocalRandom.nextLong(0, maxMinutes); return minDuration.plusMinutes(randomMinutes); } private  RandomDuration(); static Duration getRandomDurationMinuteBased(
      final Duration minDuration, final Duration maxDuration);  }
@Test public void shouldGetMinDurationWhenDiffIsOnlySeconds() { final Duration minDuration = Duration.ofMinutes(1); final Duration maxDuration = Duration.ofSeconds(90); final var randomDuration = RandomDuration.getRandomDurationMinuteBased(minDuration, maxDuration); assertThat(randomDuration).isEqualTo(minDuration); }
@Test public void shouldGetMinDurationWhenMaxDurationIsEquals() { final Duration minDuration = Duration.ofMinutes(1); final var randomDuration = RandomDuration.getRandomDurationMinuteBased(minDuration, minDuration); assertThat(randomDuration).isEqualTo(minDuration); }
@Test public void shouldGetRandomDuration() { final Duration minDuration = Duration.ofMinutes(1); final Duration maxDuration = Duration.ofMinutes(10); final var randomDuration = RandomDuration.getRandomDurationMinuteBased(minDuration, maxDuration); assertThat(randomDuration).isBetween(minDuration, maxDuration); }
@Test public void shouldGetRandomDurationOnNegativeMin() { final Duration minDuration = Duration.ofMinutes(-1); final Duration maxDuration = Duration.ofMinutes(10); final var randomDuration = RandomDuration.getRandomDurationMinuteBased(minDuration, maxDuration); assertThat(randomDuration).isBetween(minDuration, maxDuration); }
@Test public void shouldGetRandomDurationOnNegativeDurations() { final Duration minDuration = Duration.ofMinutes(-10); final Duration maxDuration = Duration.ofMinutes(-1); final var randomDuration = RandomDuration.getRandomDurationMinuteBased(minDuration, maxDuration); assertThat(randomDuration).isBetween(minDuration, maxDuration); }
@Test public void shouldGetMinDurationWhenMaxDurationIsSmaller() { final Duration minDuration = Duration.ofMinutes(10); final Duration maxDuration = Duration.ofMinutes(1); final var randomDuration = RandomDuration.getRandomDurationMinuteBased(minDuration, maxDuration); assertThat(randomDuration).isEqualTo(minDuration); }