#62 solved a couple of sonar smells

This commit is contained in:
Fabio Formosa
2022-12-03 17:10:34 +01:00
parent f7054b160f
commit 3063e08eb3
3 changed files with 32 additions and 24 deletions

View File

@@ -1,11 +1,14 @@
package it.fabioformosa.quartzmanager.api.common.utils;
import lombok.Getter;
import java.util.function.Function;
/**
*
* @param <R> success type
*/
@Getter
public class Try<R> {
private final Throwable failure;
@@ -16,7 +19,7 @@ public class Try<R> {
this.success = success;
}
private R getSuccess() {
public R getSuccess() {
return success;
}

View File

@@ -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<String> 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<String> 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");
}
}