From dba536d3d0268a4a9b699416ac3b02d324e1af7b Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Fri, 22 Jun 2018 17:39:22 +0100 Subject: [PATCH] Add additional use case sample tests for Result --- src/test/java/net/kemitix/mon/ResultTest.java | 88 +++++++++++++++++++ 1 file changed, 88 insertions(+) diff --git a/src/test/java/net/kemitix/mon/ResultTest.java b/src/test/java/net/kemitix/mon/ResultTest.java index 0a66d71..82eeebc 100644 --- a/src/test/java/net/kemitix/mon/ResultTest.java +++ b/src/test/java/net/kemitix/mon/ResultTest.java @@ -1,5 +1,6 @@ package net.kemitix.mon; +import lombok.RequiredArgsConstructor; import net.kemitix.mon.result.Result; import org.assertj.core.api.WithAssertions; import org.junit.Test; @@ -103,4 +104,91 @@ public class ResultTest implements WithAssertions { //then assertThat(flatMap.isError()).isTrue(); } + + @Test + public void useCase_whenOkay_thenReturnSuccess() { + //given + final UseCase useCase = UseCase.isOkay(); + //when + final Result doubleResult = useCase.businessOperation("file a", "file bc"); + //then + assertThat(doubleResult.isOkay()).isTrue(); + doubleResult.match( + success -> assertThat(success).isEqualTo(7.5), + error -> fail("not an error") + ); + } + + @Test + public void useCase_whenFirstReadIsError_thenReturnError() { + //given + final UseCase useCase = UseCase.firstReadInvalid(); + //when + final Result doubleResult = useCase.businessOperation("file def", "file ghij"); + //then + assertThat(doubleResult.isOkay()).isFalse(); + doubleResult.match( + success -> fail("not okay"), + error -> assertThat(error) + .isInstanceOf(RuntimeException.class) + .hasMessage("file def") + ); + } + + @Test + public void useCase_whenSecondReadIsError_thenReturnError() { + //given + final UseCase useCase = UseCase.secondReadInvalid(); + //when + final Result doubleResult = useCase.businessOperation("file klmno", "file pqrstu"); + //then + assertThat(doubleResult.isOkay()).isFalse(); + doubleResult.match( + success -> fail("not okay"), + error -> assertThat(error) + .isInstanceOf(RuntimeException.class) + .hasMessage("file klmno") + ); + } + + @RequiredArgsConstructor + private static class UseCase { + + private final Boolean okay; + + static UseCase isOkay() { + return new UseCase(true); + } + + static UseCase firstReadInvalid() { + return new UseCase(false); + } + + static UseCase secondReadInvalid() { + return new UseCase(false); + } + + Result businessOperation(final String fileName1, final String fileName2) { + return readIntFromFile(fileName1).flatMap(intFromFile1 -> + adjustValue(intFromFile1).flatMap(adjustedIntFromFile1 -> + readIntFromFile(fileName2).flatMap(intFromFile2 -> + calculateAverage(adjustedIntFromFile1, intFromFile2)))); + } + + private Result calculateAverage(final Integer val1, final Integer val2) { + return Result.ok((double) (val1 + val2) / 2); + } + + private Result adjustValue(Integer value) { + return Result.ok(value + 2); + } + + private Result readIntFromFile(String fileName) { + if (okay) { + return Result.ok(fileName.length()); + } + return Result.error(new RuntimeException(fileName)); + } + + } } \ No newline at end of file