diff --git a/quartz-manager-parent/quartz-manager-common/src/main/java/it/fabioformosa/quartzmanager/api/common/utils/Try.java b/quartz-manager-parent/quartz-manager-common/src/main/java/it/fabioformosa/quartzmanager/api/common/utils/Try.java index 4495440..4fd255e 100644 --- a/quartz-manager-parent/quartz-manager-common/src/main/java/it/fabioformosa/quartzmanager/api/common/utils/Try.java +++ b/quartz-manager-parent/quartz-manager-common/src/main/java/it/fabioformosa/quartzmanager/api/common/utils/Try.java @@ -1,11 +1,14 @@ package it.fabioformosa.quartzmanager.api.common.utils; +import lombok.Getter; + import java.util.function.Function; /** * * @param success type */ +@Getter public class Try { private final Throwable failure; @@ -16,7 +19,7 @@ public class Try { this.success = success; } - private R getSuccess() { + public R getSuccess() { return success; } diff --git a/quartz-manager-parent/quartz-manager-common/src/test/java/it/fabioformosa/quartzmanager/api/common/utils/TryTest.java b/quartz-manager-parent/quartz-manager-common/src/test/java/it/fabioformosa/quartzmanager/api/common/utils/TryTest.java index 4ffba71..be555a9 100644 --- a/quartz-manager-parent/quartz-manager-common/src/test/java/it/fabioformosa/quartzmanager/api/common/utils/TryTest.java +++ b/quartz-manager-parent/quartz-manager-common/src/test/java/it/fabioformosa/quartzmanager/api/common/utils/TryTest.java @@ -14,15 +14,29 @@ class TryTest { } @Test - void givenAFunction_whenItRaisesAnException_thenItReturnsNull(){ + void givenAFunctionWhichRaisesAnException_whenSneakyThrowIsCalled_thenItReturnsNull(){ String hello = Optional.of("hello").map(Try.sneakyThrow(this::raiseExceptionIfHello)).orElse(null); Assertions.assertThat(hello).isNull(); } @Test - void givenAFunction_whenItDoesntRaisesAnException_thenItReturnsTheValue(){ + void givenAFunctionWhichDoesntRaiseAnException_whenSneakyThrowIsCalled_thenItReturnsTheValue(){ String hello = Optional.of("not hello").map(Try.sneakyThrow(this::raiseExceptionIfHello)).orElse(null); Assertions.assertThat(hello).isEqualTo("not hello"); } + @Test + void givenAFunctionWhichRaisesAnException_whenTryWithIsCalled_thenItReturnsAFailureObj(){ + Try aTry = Optional.of("hello").map(greet -> Try.with(this::raiseExceptionIfHello).apply(greet)).get(); + Assertions.assertThat(aTry.getFailure()).isNotNull(); + Assertions.assertThat(aTry.getFailure().getMessage()).isEqualTo("hello"); + } + + @Test + void givenAFunctionWhichDoesntRaiseAnException_whenTryWithIsCalled_thenItReturnsTheValue(){ + Try aTry = Optional.of("not hello").map(greet -> Try.with(this::raiseExceptionIfHello).apply(greet)).get(); + Assertions.assertThat(aTry.getFailure()).isNull(); + Assertions.assertThat(aTry.getSuccess()).isEqualTo("not hello"); + } + } diff --git a/quartz-manager-parent/quartz-manager-starter-api/src/test/java/it/fabioformosa/quartzmanager/api/dto/TriggerFiredBundleDTOTest.java b/quartz-manager-parent/quartz-manager-starter-api/src/test/java/it/fabioformosa/quartzmanager/api/dto/TriggerFiredBundleDTOTest.java index c592829..a301569 100644 --- a/quartz-manager-parent/quartz-manager-starter-api/src/test/java/it/fabioformosa/quartzmanager/api/dto/TriggerFiredBundleDTOTest.java +++ b/quartz-manager-parent/quartz-manager-starter-api/src/test/java/it/fabioformosa/quartzmanager/api/dto/TriggerFiredBundleDTOTest.java @@ -2,33 +2,24 @@ package it.fabioformosa.quartzmanager.api.dto; import org.assertj.core.api.Assertions; import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; import static org.junit.jupiter.api.Assertions.*; class TriggerFiredBundleDTOTest { - @Test - void givenARecursionOf1000_whenTheTriggerHasFired10_thenThePercentageIs10(){ + @CsvSource({ + "10, 100, 10", + "23, 1000, 2", + "26, 1000, 3" + }) + @ParameterizedTest + void givenARepeatCount_whenTheTriggerHasFiredXTimes_thenThePercentageIsCalculatedAccordingly(int timesTriggered, int repeatCount, int expectedPercentage){ TriggerFiredBundleDTO triggerFiredBundleDTO = TriggerFiredBundleDTO.builder().build(); - triggerFiredBundleDTO.setTimesTriggered(10); - triggerFiredBundleDTO.setRepeatCount(100); - Assertions.assertThat(triggerFiredBundleDTO.getPercentage()).isEqualTo(10); - } - - @Test - void givenARecursionOf1000_whenTheTriggerHasFired23_thenThePercentageIsRoundedDown(){ - TriggerFiredBundleDTO triggerFiredBundleDTO = TriggerFiredBundleDTO.builder().build(); - triggerFiredBundleDTO.setTimesTriggered(23); - triggerFiredBundleDTO.setRepeatCount(1000); - Assertions.assertThat(triggerFiredBundleDTO.getPercentage()).isEqualTo(2); - } - - @Test - void givenARecursionOf1000_whenTheTriggerHasFired26_thenThePercentageIsRoundedUp(){ - TriggerFiredBundleDTO triggerFiredBundleDTO = TriggerFiredBundleDTO.builder().build(); - triggerFiredBundleDTO.setTimesTriggered(26); - triggerFiredBundleDTO.setRepeatCount(1000); - Assertions.assertThat(triggerFiredBundleDTO.getPercentage()).isEqualTo(3); + triggerFiredBundleDTO.setTimesTriggered(timesTriggered); + triggerFiredBundleDTO.setRepeatCount(repeatCount); + Assertions.assertThat(triggerFiredBundleDTO.getPercentage()).isEqualTo(expectedPercentage); } @Test