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