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 285dad3..27f7f19 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 @@ -2,25 +2,29 @@ package it.fabioformosa.quartzmanager.api.common.utils; import java.util.function.Function; +/** + * + * @param success type + */ public class Try { private final Throwable failure; private final R success; - public Try(Throwable failure, R success) { + private Try(Throwable failure, R success) { this.failure = failure; this.success = success; } - public R getSuccess() { + private R getSuccess() { return success; } - public static Try success(R r){ + private static Try success(R r){ return new Try<>(null, r); } - public static Try failure(Throwable e){ + private static Try failure(Throwable e){ return new Try<>(e, null); } @@ -38,11 +42,11 @@ public class Try { return t -> Try.with(checkedFunction).apply(t).getSuccess(); } - public boolean isSuccess(){ + private boolean isSuccess(){ return this.failure == null; } - public boolean isFailure(){ + private boolean isFailure(){ return this.failure != null; } 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 new file mode 100644 index 0000000..4ffba71 --- /dev/null +++ b/quartz-manager-parent/quartz-manager-common/src/test/java/it/fabioformosa/quartzmanager/api/common/utils/TryTest.java @@ -0,0 +1,28 @@ +package it.fabioformosa.quartzmanager.api.common.utils; + +import org.assertj.core.api.Assertions; +import org.junit.jupiter.api.Test; + +import java.util.Optional; + +class TryTest { + + String raiseExceptionIfHello(String greetings) throws Exception { + if("hello".equals(greetings)) + throw new Exception("hello"); + return greetings; + } + + @Test + void givenAFunction_whenItRaisesAnException_thenItReturnsNull(){ + String hello = Optional.of("hello").map(Try.sneakyThrow(this::raiseExceptionIfHello)).orElse(null); + Assertions.assertThat(hello).isNull(); + } + + @Test + void givenAFunction_whenItDoesntRaisesAnException_thenItReturnsTheValue(){ + String hello = Optional.of("not hello").map(Try.sneakyThrow(this::raiseExceptionIfHello)).orElse(null); + Assertions.assertThat(hello).isEqualTo("not hello"); + } + +}