Skip to content

Commit fb50f56

Browse files
tomekl007pivovarit
authored andcommitted
BAEL-424 code for javaslang Try (eugenp#1068)
* BAEL-424 code for javaslang Try * BAEL-424 more api examples of try * BAEL-424 less verbose names of test, two exceptions types in pattern matching
1 parent a0781d1 commit fb50f56

6 files changed

Lines changed: 177 additions & 0 deletions

File tree

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.baeldung.javaslang.exception.handling;
2+
3+
import com.baeldung.javaslang.exception.handling.client.ClientException;
4+
import com.baeldung.javaslang.exception.handling.client.HttpClient;
5+
import com.baeldung.javaslang.exception.handling.client.Response;
6+
7+
public class JavaTryCatch {
8+
private HttpClient httpClient;
9+
10+
public JavaTryCatch(HttpClient httpClient) {
11+
this.httpClient = httpClient;
12+
}
13+
14+
public Response getResponse() {
15+
try {
16+
return httpClient.call();
17+
} catch (ClientException e) {
18+
return null;
19+
}
20+
}
21+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.baeldung.javaslang.exception.handling;
2+
3+
4+
import com.baeldung.javaslang.exception.handling.client.HttpClient;
5+
import com.baeldung.javaslang.exception.handling.client.Response;
6+
import javaslang.control.Try;
7+
8+
public class JavaslangTry {
9+
private final HttpClient httpClient;
10+
11+
public JavaslangTry(HttpClient httpClient) {
12+
this.httpClient = httpClient;
13+
}
14+
15+
public Try<Response> getResponse() {
16+
return Try.of(httpClient::call);
17+
}
18+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.baeldung.javaslang.exception.handling.client;
2+
3+
4+
public class ClientException extends Exception {
5+
public ClientException(String message) {
6+
super(message);
7+
}
8+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.baeldung.javaslang.exception.handling.client;
2+
3+
4+
public interface HttpClient {
5+
Response call() throws ClientException;
6+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.baeldung.javaslang.exception.handling.client;
2+
3+
public class Response {
4+
public final String id;
5+
6+
public Response(String id) {
7+
this.id = id;
8+
}
9+
}
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
package com.baeldung.javaslang.exception.handling;
2+
3+
import com.baeldung.javaslang.exception.handling.client.ClientException;
4+
import com.baeldung.javaslang.exception.handling.client.HttpClient;
5+
import com.baeldung.javaslang.exception.handling.client.Response;
6+
import javaslang.collection.Stream;
7+
import javaslang.control.Option;
8+
import javaslang.control.Try;
9+
import org.junit.Test;
10+
11+
import java.util.function.Function;
12+
import java.util.stream.Collectors;
13+
14+
import static javaslang.API.Case;
15+
import static javaslang.API.Match;
16+
import static javaslang.Predicates.instanceOf;
17+
import static org.junit.Assert.*;
18+
19+
public class JavaslangTryTest {
20+
21+
@Test
22+
public void givenHttpClient_whenMakeACall_shouldReturnSuccess() {
23+
//given
24+
Integer defaultChainedResult = 1;
25+
String id = "a";
26+
HttpClient httpClient = () -> new Response(id);
27+
28+
//when
29+
Try<Response> response = new JavaslangTry(httpClient).getResponse();
30+
Integer chainedResult = response
31+
.map(this::actionThatTakesResponse)
32+
.getOrElse(defaultChainedResult);
33+
Stream<String> stream = response.toStream().map(it -> it.id);
34+
35+
//then
36+
assertTrue(!stream.isEmpty());
37+
assertTrue(response.isSuccess());
38+
response.onSuccess(r -> assertEquals(id, r.id));
39+
response.andThen(r -> assertEquals(id, r.id));
40+
assertNotEquals(defaultChainedResult, chainedResult);
41+
}
42+
43+
@Test
44+
public void givenHttpClientFailure_whenMakeACall_shouldReturnFailure() {
45+
//given
46+
Integer defaultChainedResult = 1;
47+
HttpClient httpClient = () -> {
48+
throw new ClientException("problem");
49+
};
50+
51+
//when
52+
Try<Response> response = new JavaslangTry(httpClient).getResponse();
53+
Integer chainedResult = response
54+
.map(this::actionThatTakesResponse)
55+
.getOrElse(defaultChainedResult);
56+
Option<Response> optionalResponse = response.toOption();
57+
58+
//then
59+
assertTrue(optionalResponse.isEmpty());
60+
assertTrue(response.isFailure());
61+
response.onFailure(ex -> assertTrue(ex instanceof ClientException));
62+
assertEquals(defaultChainedResult, chainedResult);
63+
}
64+
65+
@Test
66+
public void givenHttpClientThatFailure_whenMakeACall_shouldReturnFailureAndNotRecover() {
67+
//given
68+
Response defaultResponse = new Response("b");
69+
HttpClient httpClient = () -> {
70+
throw new RuntimeException("critical problem");
71+
};
72+
73+
//when
74+
Try<Response> recovered = new JavaslangTry(httpClient).getResponse()
75+
.recover(r -> Match(r).of(
76+
Case(instanceOf(ClientException.class), defaultResponse)
77+
));
78+
79+
//then
80+
assertTrue(recovered.isFailure());
81+
82+
// recovered.getOrElseThrow(throwable -> {
83+
// throw new RuntimeException(throwable);
84+
// });
85+
}
86+
87+
@Test
88+
public void givenHttpClientThatFailure_whenMakeACall_shouldReturnFailureAndRecover() {
89+
//given
90+
Response defaultResponse = new Response("b");
91+
HttpClient httpClient = () -> {
92+
throw new ClientException("non critical problem");
93+
};
94+
95+
//when
96+
Try<Response> recovered = new JavaslangTry(httpClient).getResponse()
97+
.recover(r -> Match(r).of(
98+
Case(instanceOf(ClientException.class), defaultResponse),
99+
Case(instanceOf(IllegalArgumentException.class), defaultResponse)
100+
));
101+
102+
//then
103+
assertTrue(recovered.isSuccess());
104+
}
105+
106+
107+
public int actionThatTakesResponse(Response response) {
108+
return response.id.hashCode();
109+
}
110+
111+
public int actionThatTakesTryResponse(Try<Response> response, int defaultTransformation){
112+
return response.transform(responses -> response.map(it -> it.id.hashCode()).getOrElse(defaultTransformation));
113+
}
114+
115+
}

0 commit comments

Comments
 (0)