|
| 1 | +package com.baeldung.vavr.future; |
| 2 | + |
| 3 | +import static io.vavr.API.$; |
| 4 | +import static io.vavr.API.Case; |
| 5 | +import static io.vavr.API.Match; |
| 6 | +import static io.vavr.Predicates.exists; |
| 7 | +import static io.vavr.Predicates.forAll; |
| 8 | +import static org.awaitility.Awaitility.await; |
| 9 | +import static org.junit.Assert.assertEquals; |
| 10 | +import static org.junit.Assert.assertTrue; |
| 11 | + |
| 12 | +import java.util.concurrent.CancellationException; |
| 13 | +import java.util.function.Predicate; |
| 14 | + |
| 15 | +import org.junit.Test; |
| 16 | + |
| 17 | +import io.vavr.collection.List; |
| 18 | +import io.vavr.concurrent.Future; |
| 19 | + |
| 20 | +public class FutureUnitTest { |
| 21 | + |
| 22 | + private final String SUCCESS = "Success"; |
| 23 | + private final String FAILURE = "Failure"; |
| 24 | + |
| 25 | + @Test |
| 26 | + public void givenFunctionReturnInteger_WhenCallWithFuture_ShouldReturnFunctionValue() { |
| 27 | + Future<Integer> future = Future.of(() -> 1); |
| 28 | + |
| 29 | + assertEquals(1, future.get() |
| 30 | + .intValue()); |
| 31 | + } |
| 32 | + |
| 33 | + @Test |
| 34 | + public void givenFunctionGetRemoteHttpResourceAsString_WhenCallSuccessWithFuture_ShouldReturnContentValueAsString() { |
| 35 | + String url = "http://resource"; |
| 36 | + String content = "Content from " + url; |
| 37 | + Future<String> future = Future.of(() -> getResource(url)); |
| 38 | + |
| 39 | + assertEquals(content, future.get()); |
| 40 | + } |
| 41 | + |
| 42 | + @Test |
| 43 | + public void givenFunctionThrowException_WhenCallWithFuture_ShouldReturnFailure() { |
| 44 | + Future<String> future = Future.of(() -> getResourceThrowException("")); |
| 45 | + future.await(); |
| 46 | + |
| 47 | + assertTrue(future.isFailure()); |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + public void givenAFutureReturnZero_WhenCheckFutureWithExistEvenValue_ShouldReturnRight() { |
| 52 | + Future<Integer> future = Future.of(() -> 2); |
| 53 | + boolean result = future.exists(i -> i % 2 == 0); |
| 54 | + |
| 55 | + assertTrue(result); |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + public void givenFunction_WhenCallWithFutureAndRegisterConsumerForSuccess_ShouldCallConsumerToStoreValue() { |
| 60 | + final int[] store = new int[] { 0 }; |
| 61 | + Future<Integer> future = Future.of(() -> 1); |
| 62 | + future.onSuccess(i -> { |
| 63 | + store[0] = i; |
| 64 | + }); |
| 65 | + await().until(() -> store[0] == 1); |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + public void givenFunctionThrowException_WhenCallWithFutureAndRegisterConsumerForFailer_ShouldCallConsumerToStoreException() { |
| 70 | + final Throwable[] store = new Throwable[] { null }; |
| 71 | + Future<String> future = Future.of(() -> getResourceThrowException("")); |
| 72 | + future.onFailure(err -> store[0] = err); |
| 73 | + await().until(() -> RuntimeException.class.isInstance(store[0])); |
| 74 | + } |
| 75 | + |
| 76 | + @Test |
| 77 | + public void givenAFuture_WhenAddAndThenConsumer_ShouldCallConsumerWithResultOfFutureAction() { |
| 78 | + int[] store1 = new int[1]; |
| 79 | + int[] store2 = new int[1]; |
| 80 | + Future<Integer> future = Future.of(() -> 1); |
| 81 | + Future<Integer> andThenFuture = future.andThen(i -> store1[0] = i.get() + 1) |
| 82 | + .andThen(i -> store2[0] = store1[0] + 1); |
| 83 | + andThenFuture.await(); |
| 84 | + |
| 85 | + assertEquals(2, store1[0]); |
| 86 | + assertEquals(3, store2[0]); |
| 87 | + } |
| 88 | + |
| 89 | + @Test |
| 90 | + public void givenAFailureFuture_WhenCallOrElseFunction_ShouldReturnNewFuture() { |
| 91 | + Future<Integer> future = Future.failed(new RuntimeException()); |
| 92 | + Future<Integer> future2 = future.orElse(Future.of(() -> 2)); |
| 93 | + |
| 94 | + assertEquals(2, future2.get() |
| 95 | + .intValue()); |
| 96 | + } |
| 97 | + |
| 98 | + @Test(expected = CancellationException.class) |
| 99 | + public void givenAFuture_WhenCallCancel_ShouldReturnCancellationException() { |
| 100 | + long waitTime = 1000; |
| 101 | + Future<Integer> future = Future.of(() -> { |
| 102 | + Thread.sleep(waitTime); |
| 103 | + return 1; |
| 104 | + }); |
| 105 | + future.cancel(); |
| 106 | + future.await(); |
| 107 | + future.get(); |
| 108 | + } |
| 109 | + |
| 110 | + @Test |
| 111 | + public void givenAFuture_WhenCallFallBackWithSuccessFuture_ShouldReturnFutureResult() { |
| 112 | + String expectedResult = "take this"; |
| 113 | + Future<String> future = Future.of(() -> expectedResult); |
| 114 | + Future<String> secondFuture = Future.of(() -> "take that"); |
| 115 | + Future<String> futureResult = future.fallbackTo(secondFuture); |
| 116 | + futureResult.await(); |
| 117 | + |
| 118 | + assertEquals(expectedResult, futureResult.get()); |
| 119 | + } |
| 120 | + |
| 121 | + @Test |
| 122 | + public void givenAFuture_WhenCallFallBackWithFailureFuture_ShouldReturnValueOfFallBackFuture() { |
| 123 | + String expectedResult = "take that"; |
| 124 | + Future<String> future = Future.failed(new RuntimeException()); |
| 125 | + Future<String> fallbackFuture = Future.of(() -> expectedResult); |
| 126 | + Future<String> futureResult = future.fallbackTo(fallbackFuture); |
| 127 | + futureResult.await(); |
| 128 | + |
| 129 | + assertEquals(expectedResult, futureResult.get()); |
| 130 | + } |
| 131 | + |
| 132 | + @Test |
| 133 | + public void givenGetResourceWithFuture_WhenWaitAndMatchWithPredicate_ShouldReturnSuccess() { |
| 134 | + String url = "http://resource"; |
| 135 | + Future<String> future = Future.of(() -> getResource(url)); |
| 136 | + future.await(); |
| 137 | + String s = Match(future).of(Case($(future0 -> future0.isSuccess()), SUCCESS), Case($(), FAILURE)); |
| 138 | + |
| 139 | + assertEquals(SUCCESS, s); |
| 140 | + } |
| 141 | + |
| 142 | + @Test |
| 143 | + public void givenAFailedFuture_WhenWaitAndMatchWithPredicateCheckSuccess_ShouldReturnFailed() { |
| 144 | + Future<Integer> future = Future.failed(new RuntimeException()); |
| 145 | + future.await(); |
| 146 | + String s = Match(future).of(Case($(future0 -> future0.isSuccess()), SUCCESS), Case($(), FAILURE)); |
| 147 | + |
| 148 | + assertEquals(FAILURE, s); |
| 149 | + } |
| 150 | + |
| 151 | + @Test |
| 152 | + public void givenAFuture_WhenMatchWithFuturePredicate_ShouldReturnSuccess() { |
| 153 | + Future<Integer> future = Future.of(() -> { |
| 154 | + Thread.sleep(10); |
| 155 | + return 1; |
| 156 | + }); |
| 157 | + Predicate<Future<Integer>> predicate = f -> f.exists(i -> i % 2 == 1); |
| 158 | + String s = Match(future).of(Case($(predicate), "Even"), Case($(), "Odd")); |
| 159 | + |
| 160 | + assertEquals("Even", s); |
| 161 | + } |
| 162 | + |
| 163 | + @Test |
| 164 | + public void givenAListOfFutureReturnFist3Integers_WhenMatchWithExistEvenNumberPredicate_ShouldReturnSuccess() { |
| 165 | + List<Future<Integer>> futures = getFutureOfFirst3Number(); |
| 166 | + Predicate<Future<Integer>> predicate0 = future -> future.exists(i -> i % 2 == 0); |
| 167 | + String s = Match(futures).of(Case($(exists(predicate0)), "Even"), Case($(), "Odd")); |
| 168 | + |
| 169 | + assertEquals("Even", s); |
| 170 | + } |
| 171 | + |
| 172 | + @Test |
| 173 | + public void givenAListOfFutureReturnFist3Integers_WhenMatchWithForAllNumberBiggerThanZeroPredicate_ShouldReturnSuccess() { |
| 174 | + List<Future<Integer>> futures = getFutureOfFirst3Number(); |
| 175 | + Predicate<Future<Integer>> predicate0 = future -> future.exists(i -> i > 0); |
| 176 | + String s = Match(futures).of(Case($(forAll(predicate0)), "Positive numbers"), Case($(), "None")); |
| 177 | + |
| 178 | + assertEquals("Positive numbers", s); |
| 179 | + } |
| 180 | + |
| 181 | + @Test |
| 182 | + public void givenAListOfFutureReturnFist3Integers_WhenMatchWithForAllNumberSmallerThanZeroPredicate_ShouldReturnFailed() { |
| 183 | + List<Future<Integer>> futures = getFutureOfFirst3Number(); |
| 184 | + Predicate<Future<Integer>> predicate0 = future -> future.exists(i -> i < 0); |
| 185 | + String s = Match(futures).of(Case($(forAll(predicate0)), "Negative numbers"), Case($(), "None")); |
| 186 | + |
| 187 | + assertEquals("None", s); |
| 188 | + } |
| 189 | + |
| 190 | + private String getResource(String url) throws InterruptedException { |
| 191 | + Thread.sleep(10); |
| 192 | + return "Content from " + url; |
| 193 | + } |
| 194 | + |
| 195 | + private String getResourceThrowException(String url) { |
| 196 | + throw new RuntimeException("Exception when get resource " + url); |
| 197 | + } |
| 198 | + |
| 199 | + private List<Future<Integer>> getFutureOfFirst3Number() { |
| 200 | + List<Future<Integer>> futures = List.of(Future.of(() -> 1), Future.of(() -> 2), Future.of(() -> 3)); |
| 201 | + return futures; |
| 202 | + } |
| 203 | +} |
0 commit comments