{"action":"created","comment":{"url":"https://api.github.com/repos/eugenp/tutorials/pulls/comments/134332926","pull_request_review_id":57612728,"id":134332926,"diff_hunk":"@@ -0,0 +1,209 @@\n+package com.baeldung.vavr.future;\n+\n+import static io.vavr.API.$;\n+import static io.vavr.API.Case;\n+import static io.vavr.API.Match;\n+import static io.vavr.Predicates.exists;\n+import static io.vavr.Predicates.forAll;\n+import static org.junit.Assert.assertEquals;\n+import static org.junit.Assert.assertTrue;\n+\n+import java.util.concurrent.CancellationException;\n+import java.util.function.Predicate;\n+\n+import org.junit.Test;\n+\n+import io.vavr.collection.List;\n+import io.vavr.concurrent.Future;\n+\n+public class FutureUnitTest {\n+\n+    private final String SUCCESS = \"Success\";\n+    private final String FAILURE = \"Failure\";\n+\n+    @Test\n+    public void givenFunctionReturnInteger_WhenCallWithFuture_ShouldReturnFunctionValue() {\n+        Future<Integer> future = Future.of(() -> 1);\n+\n+        assertEquals(1, future.get()\n+            .intValue());\n+    }\n+\n+    @Test\n+    public void givenFunctionGetRemoteHttpResourceAsString_WhenCallSuccessWithFuture_ShouldReturnContentValueAsString() {\n+        String url = \"http://resource\";\n+        String content = \"Content from \" + url;\n+        Future<String> future = Future.of(() -> getResource(url));\n+\n+        assertEquals(content, future.get());\n+    }\n+\n+    @Test\n+    public void givenFunctionThrowException_WhenCallWithFuture_ShouldReturnFailure() {\n+        Future<String> future = Future.of(() -> getResourceThrowException(\"\"));\n+        future.await();\n+\n+        assertTrue(future.isFailure());\n+    }\n+\n+    @Test\n+    public void givenAFutureReturnZero_WhenCheckFutureWithExistEvenValue_ShouldReturnRight() {\n+        Future<Integer> future = Future.of(() -> 2);\n+        boolean result = future.exists(i -> i % 2 == 0);\n+\n+        assertTrue(result);\n+    }\n+\n+    @Test\n+    public void givenFunction_WhenCallWithFutureAndRegisterConsumerForSuccess_ShouldCallConsumerToStoreValue() {\n+        final int[] store = new int[] { 0 };\n+        Future<Integer> future = Future.of(() -> 1);\n+        future.onSuccess(i -> {\n+            store[0] = i;\n+        });\n+        while (store[0] == 0) {\n+        }\n+\n+        assertEquals(1, store[0]);\n+    }\n+\n+    @Test\n+    public void givenFunctionThrowException_WhenCallWithFutureAndRegisterConsumerForFailer_ShouldCallConsumerToStoreException() {\n+        final Throwable[] store = new Throwable[] { null };\n+        Future<String> future = Future.of(() -> getResourceThrowException(\"\"));\n+        future.onFailure(err -> store[0] = err);\n+        while (store[0] == null) {\n+        }\n+\n+        assertEquals(store[0].getClass(), RuntimeException.class);\n+    }\n+\n+    @Test\n+    public void givenAFuture_WhenAddAndThenConsumer_ShouldCallConsumerWithResultOfFutureAction() {\n+        int[] store1 = new int[1];\n+        int[] store2 = new int[1];\n+        Future<Integer> future = Future.of(() -> 1);\n+        future.andThen(i -> store1[0] = i.get() + 1)\n+            .andThen(i -> store2[0] = store1[0] + 1);\n+        while (store2[0] == 0) {\n+        }\n+\n+        assertEquals(2, store1[0]);\n+        assertEquals(3, store2[0]);\n+    }\n+\n+    @Test\n+    public void givenAFailureFuture_WhenCallOrElseFunction_ShouldReturnNewFuture() {\n+        Future<Integer> future = Future.failed(new RuntimeException());\n+        Future<Integer> future2 = future.orElse(Future.of(() -> 2));\n+\n+        assertEquals(2, future2.get()\n+            .intValue());\n+    }\n+\n+    @Test\n+    public void givenAFuture_WhenCallCancel_ShouldReturnCancellationException() {\n+        long waitTime = 1000;\n+        Future<Integer> future = Future.of(() -> {\n+            Thread.sleep(waitTime);\n+            return 1;\n+        });\n+        future.cancel();\n+        future.await();\n+        assertEquals(CancellationException.class, future.getCause().get().getClass());","path":"vavr/src/test/java/com/baeldung/vavr/future/FutureUnitTest.java","position":113,"original_position":113,"commit_id":"b03319a1eb5322970e83d9c17820554cbcb7f578","original_commit_id":"b03319a1eb5322970e83d9c17820554cbcb7f578","user":{"login":"pivovarit","id":2182533,"avatar_url":"https://avatars0.githubusercontent.com/u/2182533?v=4","gravatar_id":"","url":"https://api.github.com/users/pivovarit","html_url":"https://github.com/pivovarit","followers_url":"https://api.github.com/users/pivovarit/followers","following_url":"https://api.github.com/users/pivovarit/following{/other_user}","gists_url":"https://api.github.com/users/pivovarit/gists{/gist_id}","starred_url":"https://api.github.com/users/pivovarit/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/pivovarit/subscriptions","organizations_url":"https://api.github.com/users/pivovarit/orgs","repos_url":"https://api.github.com/users/pivovarit/repos","events_url":"https://api.github.com/users/pivovarit/events{/privacy}","received_events_url":"https://api.github.com/users/pivovarit/received_events","type":"User","site_admin":false},"body":"Does not Future simply rethrow the exception when you call get()?","created_at":"2017-08-21T21:14:00Z","updated_at":"2017-08-21T21:15:47Z","html_url":"https://github.com/eugenp/tutorials/pull/2478#discussion_r134332926","pull_request_url":"https://api.github.com/repos/eugenp/tutorials/pulls/2478","_links":{"self":{"href":"https://api.github.com/repos/eugenp/tutorials/pulls/comments/134332926"},"html":{"href":"https://github.com/eugenp/tutorials/pull/2478#discussion_r134332926"},"pull_request":{"href":"https://api.github.com/repos/eugenp/tutorials/pulls/2478"}}},"pull_request":{"url":"https://api.github.com/repos/eugenp/tutorials/pulls/2478","id":136781640,"html_url":"https://github.com/eugenp/tutorials/pull/2478","diff_url":"https://github.com/eugenp/tutorials/pull/2478.diff","patch_url":"https://github.com/eugenp/tutorials/pull/2478.patch","issue_url":"https://api.github.com/repos/eugenp/tutorials/issues/2478","number":2478,"state":"open","locked":false,"title":"BAEL-1080 Introduction to Future in Vavr","user":{"login":"hunghoang","id":5298855,"avatar_url":"https://avatars2.githubusercontent.com/u/5298855?v=4","gravatar_id":"","url":"https://api.github.com/users/hunghoang","html_url":"https://github.com/hunghoang","followers_url":"https://api.github.com/users/hunghoang/followers","following_url":"https://api.github.com/users/hunghoang/following{/other_user}","gists_url":"https://api.github.com/users/hunghoang/gists{/gist_id}","starred_url":"https://api.github.com/users/hunghoang/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/hunghoang/subscriptions","organizations_url":"https://api.github.com/users/hunghoang/orgs","repos_url":"https://api.github.com/users/hunghoang/repos","events_url":"https://api.github.com/users/hunghoang/events{/privacy}","received_events_url":"https://api.github.com/users/hunghoang/received_events","type":"User","site_admin":false},"body":"","created_at":"2017-08-21T15:10:20Z","updated_at":"2017-08-21T21:15:47Z","closed_at":null,"merged_at":null,"merge_commit_sha":"6821642eabf2c978b460014599ba0fdb4d587aec","assignee":null,"assignees":[],"requested_reviewers":[],"milestone":null,"commits_url":"https://api.github.com/repos/eugenp/tutorials/pulls/2478/commits","review_comments_url":"https://api.github.com/repos/eugenp/tutorials/pulls/2478/comments","review_comment_url":"https://api.github.com/repos/eugenp/tutorials/pulls/comments{/number}","comments_url":"https://api.github.com/repos/eugenp/tutorials/issues/2478/comments","statuses_url":"https://api.github.com/repos/eugenp/tutorials/statuses/b03319a1eb5322970e83d9c17820554cbcb7f578","head":{"label":"hunghoang:master","ref":"master","sha":"b03319a1eb5322970e83d9c17820554cbcb7f578","user":{"login":"hunghoang","id":5298855,"avatar_url":"https://avatars2.githubusercontent.com/u/5298855?v=4","gravatar_id":"","url":"https://api.github.com/users/hunghoang","html_url":"https://github.com/hunghoang","followers_url":"https://api.github.com/users/hunghoang/followers","following_url":"https://api.github.com/users/hunghoang/following{/other_user}","gists_url":"https://api.github.com/users/hunghoang/gists{/gist_id}","starred_url":"https://api.github.com/users/hunghoang/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/hunghoang/subscriptions","organizations_url":"https://api.github.com/users/hunghoang/orgs","repos_url":"https://api.github.com/users/hunghoang/repos","events_url":"https://api.github.com/users/hunghoang/events{/privacy}","received_events_url":"https://api.github.com/users/hunghoang/received_events","type":"User","site_admin":false},"repo":{"id":92748181,"name":"tutorials","full_name":"hunghoang/tutorials","owner":{"login":"hunghoang","id":5298855,"avatar_url":"https://avatars2.githubusercontent.com/u/5298855?v=4","gravatar_id":"","url":"https://api.github.com/users/hunghoang","html_url":"https://github.com/hunghoang","followers_url":"https://api.github.com/users/hunghoang/followers","following_url":"https://api.github.com/users/hunghoang/following{/other_user}","gists_url":"https://api.github.com/users/hunghoang/gists{/gist_id}","starred_url":"https://api.github.com/users/hunghoang/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/hunghoang/subscriptions","organizations_url":"https://api.github.com/users/hunghoang/orgs","repos_url":"https://api.github.com/users/hunghoang/repos","events_url":"https://api.github.com/users/hunghoang/events{/privacy}","received_events_url":"https://api.github.com/users/hunghoang/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/hunghoang/tutorials","description":"The \"REST With Spring\" Classes:","fork":true,"url":"https://api.github.com/repos/hunghoang/tutorials","forks_url":"https://api.github.com/repos/hunghoang/tutorials/forks","keys_url":"https://api.github.com/repos/hunghoang/tutorials/keys{/key_id}","collaborators_url":"https://api.github.com/repos/hunghoang/tutorials/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/hunghoang/tutorials/teams","hooks_url":"https://api.github.com/repos/hunghoang/tutorials/hooks","issue_events_url":"https://api.github.com/repos/hunghoang/tutorials/issues/events{/number}","events_url":"https://api.github.com/repos/hunghoang/tutorials/events","assignees_url":"https://api.github.com/repos/hunghoang/tutorials/assignees{/user}","branches_url":"https://api.github.com/repos/hunghoang/tutorials/branches{/branch}","tags_url":"https://api.github.com/repos/hunghoang/tutorials/tags","blobs_url":"https://api.github.com/repos/hunghoang/tutorials/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/hunghoang/tutorials/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/hunghoang/tutorials/git/refs{/sha}","trees_url":"https://api.github.com/repos/hunghoang/tutorials/git/trees{/sha}","statuses_url":"https://api.github.com/repos/hunghoang/tutorials/statuses/{sha}","languages_url":"https://api.github.com/repos/hunghoang/tutorials/languages","stargazers_url":"https://api.github.com/repos/hunghoang/tutorials/stargazers","contributors_url":"https://api.github.com/repos/hunghoang/tutorials/contributors","subscribers_url":"https://api.github.com/repos/hunghoang/tutorials/subscribers","subscription_url":"https://api.github.com/repos/hunghoang/tutorials/subscription","commits_url":"https://api.github.com/repos/hunghoang/tutorials/commits{/sha}","git_commits_url":"https://api.github.com/repos/hunghoang/tutorials/git/commits{/sha}","comments_url":"https://api.github.com/repos/hunghoang/tutorials/comments{/number}","issue_comment_url":"https://api.github.com/repos/hunghoang/tutorials/issues/comments{/number}","contents_url":"https://api.github.com/repos/hunghoang/tutorials/contents/{+path}","compare_url":"https://api.github.com/repos/hunghoang/tutorials/compare/{base}...{head}","merges_url":"https://api.github.com/repos/hunghoang/tutorials/merges","archive_url":"https://api.github.com/repos/hunghoang/tutorials/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/hunghoang/tutorials/downloads","issues_url":"https://api.github.com/repos/hunghoang/tutorials/issues{/number}","pulls_url":"https://api.github.com/repos/hunghoang/tutorials/pulls{/number}","milestones_url":"https://api.github.com/repos/hunghoang/tutorials/milestones{/number}","notifications_url":"https://api.github.com/repos/hunghoang/tutorials/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/hunghoang/tutorials/labels{/name}","releases_url":"https://api.github.com/repos/hunghoang/tutorials/releases{/id}","deployments_url":"https://api.github.com/repos/hunghoang/tutorials/deployments","created_at":"2017-05-29T14:37:06Z","updated_at":"2017-05-29T14:37:20Z","pushed_at":"2017-08-21T15:08:55Z","git_url":"git://github.com/hunghoang/tutorials.git","ssh_url":"git@github.com:hunghoang/tutorials.git","clone_url":"https://github.com/hunghoang/tutorials.git","svn_url":"https://github.com/hunghoang/tutorials","homepage":"http://bit.ly/restwithspring","size":65962,"stargazers_count":0,"watchers_count":0,"language":"XSLT","has_issues":false,"has_projects":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"eugenp:master","ref":"master","sha":"84cbc580e5460309f6d7da71fe266e84456ec05c","user":{"login":"eugenp","id":1022859,"avatar_url":"https://avatars1.githubusercontent.com/u/1022859?v=4","gravatar_id":"","url":"https://api.github.com/users/eugenp","html_url":"https://github.com/eugenp","followers_url":"https://api.github.com/users/eugenp/followers","following_url":"https://api.github.com/users/eugenp/following{/other_user}","gists_url":"https://api.github.com/users/eugenp/gists{/gist_id}","starred_url":"https://api.github.com/users/eugenp/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/eugenp/subscriptions","organizations_url":"https://api.github.com/users/eugenp/orgs","repos_url":"https://api.github.com/users/eugenp/repos","events_url":"https://api.github.com/users/eugenp/events{/privacy}","received_events_url":"https://api.github.com/users/eugenp/received_events","type":"User","site_admin":false},"repo":{"id":9754983,"name":"tutorials","full_name":"eugenp/tutorials","owner":{"login":"eugenp","id":1022859,"avatar_url":"https://avatars1.githubusercontent.com/u/1022859?v=4","gravatar_id":"","url":"https://api.github.com/users/eugenp","html_url":"https://github.com/eugenp","followers_url":"https://api.github.com/users/eugenp/followers","following_url":"https://api.github.com/users/eugenp/following{/other_user}","gists_url":"https://api.github.com/users/eugenp/gists{/gist_id}","starred_url":"https://api.github.com/users/eugenp/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/eugenp/subscriptions","organizations_url":"https://api.github.com/users/eugenp/orgs","repos_url":"https://api.github.com/users/eugenp/repos","events_url":"https://api.github.com/users/eugenp/events{/privacy}","received_events_url":"https://api.github.com/users/eugenp/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/eugenp/tutorials","description":"The \"REST With Spring\" Classes:","fork":false,"url":"https://api.github.com/repos/eugenp/tutorials","forks_url":"https://api.github.com/repos/eugenp/tutorials/forks","keys_url":"https://api.github.com/repos/eugenp/tutorials/keys{/key_id}","collaborators_url":"https://api.github.com/repos/eugenp/tutorials/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/eugenp/tutorials/teams","hooks_url":"https://api.github.com/repos/eugenp/tutorials/hooks","issue_events_url":"https://api.github.com/repos/eugenp/tutorials/issues/events{/number}","events_url":"https://api.github.com/repos/eugenp/tutorials/events","assignees_url":"https://api.github.com/repos/eugenp/tutorials/assignees{/user}","branches_url":"https://api.github.com/repos/eugenp/tutorials/branches{/branch}","tags_url":"https://api.github.com/repos/eugenp/tutorials/tags","blobs_url":"https://api.github.com/repos/eugenp/tutorials/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/eugenp/tutorials/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/eugenp/tutorials/git/refs{/sha}","trees_url":"https://api.github.com/repos/eugenp/tutorials/git/trees{/sha}","statuses_url":"https://api.github.com/repos/eugenp/tutorials/statuses/{sha}","languages_url":"https://api.github.com/repos/eugenp/tutorials/languages","stargazers_url":"https://api.github.com/repos/eugenp/tutorials/stargazers","contributors_url":"https://api.github.com/repos/eugenp/tutorials/contributors","subscribers_url":"https://api.github.com/repos/eugenp/tutorials/subscribers","subscription_url":"https://api.github.com/repos/eugenp/tutorials/subscription","commits_url":"https://api.github.com/repos/eugenp/tutorials/commits{/sha}","git_commits_url":"https://api.github.com/repos/eugenp/tutorials/git/commits{/sha}","comments_url":"https://api.github.com/repos/eugenp/tutorials/comments{/number}","issue_comment_url":"https://api.github.com/repos/eugenp/tutorials/issues/comments{/number}","contents_url":"https://api.github.com/repos/eugenp/tutorials/contents/{+path}","compare_url":"https://api.github.com/repos/eugenp/tutorials/compare/{base}...{head}","merges_url":"https://api.github.com/repos/eugenp/tutorials/merges","archive_url":"https://api.github.com/repos/eugenp/tutorials/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/eugenp/tutorials/downloads","issues_url":"https://api.github.com/repos/eugenp/tutorials/issues{/number}","pulls_url":"https://api.github.com/repos/eugenp/tutorials/pulls{/number}","milestones_url":"https://api.github.com/repos/eugenp/tutorials/milestones{/number}","notifications_url":"https://api.github.com/repos/eugenp/tutorials/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/eugenp/tutorials/labels{/name}","releases_url":"https://api.github.com/repos/eugenp/tutorials/releases{/id}","deployments_url":"https://api.github.com/repos/eugenp/tutorials/deployments","created_at":"2013-04-29T18:26:36Z","updated_at":"2017-08-21T19:10:45Z","pushed_at":"2017-08-21T21:08:48Z","git_url":"git://github.com/eugenp/tutorials.git","ssh_url":"git@github.com:eugenp/tutorials.git","clone_url":"https://github.com/eugenp/tutorials.git","svn_url":"https://github.com/eugenp/tutorials","homepage":"http://bit.ly/restwithspring","size":65967,"stargazers_count":2475,"watchers_count":2475,"language":"XSLT","has_issues":true,"has_projects":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":4468,"mirror_url":null,"open_issues_count":44,"forks":4468,"open_issues":44,"watchers":2475,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/eugenp/tutorials/pulls/2478"},"html":{"href":"https://github.com/eugenp/tutorials/pull/2478"},"issue":{"href":"https://api.github.com/repos/eugenp/tutorials/issues/2478"},"comments":{"href":"https://api.github.com/repos/eugenp/tutorials/issues/2478/comments"},"review_comments":{"href":"https://api.github.com/repos/eugenp/tutorials/pulls/2478/comments"},"review_comment":{"href":"https://api.github.com/repos/eugenp/tutorials/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/eugenp/tutorials/pulls/2478/commits"},"statuses":{"href":"https://api.github.com/repos/eugenp/tutorials/statuses/b03319a1eb5322970e83d9c17820554cbcb7f578"}}}}
{"actor":{"display_login":"pivovarit"}}