BAEL-424 code for javaslang Try (#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
This commit is contained in:
Tomasz Lelek 2017-01-31 21:46:46 +01:00 committed by Grzegorz Piwowarek
parent a0781d1d88
commit fb50f56cfd
6 changed files with 177 additions and 0 deletions

View File

@ -0,0 +1,21 @@
package com.baeldung.javaslang.exception.handling;
import com.baeldung.javaslang.exception.handling.client.ClientException;
import com.baeldung.javaslang.exception.handling.client.HttpClient;
import com.baeldung.javaslang.exception.handling.client.Response;
public class JavaTryCatch {
private HttpClient httpClient;
public JavaTryCatch(HttpClient httpClient) {
this.httpClient = httpClient;
}
public Response getResponse() {
try {
return httpClient.call();
} catch (ClientException e) {
return null;
}
}
}

View File

@ -0,0 +1,18 @@
package com.baeldung.javaslang.exception.handling;
import com.baeldung.javaslang.exception.handling.client.HttpClient;
import com.baeldung.javaslang.exception.handling.client.Response;
import javaslang.control.Try;
public class JavaslangTry {
private final HttpClient httpClient;
public JavaslangTry(HttpClient httpClient) {
this.httpClient = httpClient;
}
public Try<Response> getResponse() {
return Try.of(httpClient::call);
}
}

View File

@ -0,0 +1,8 @@
package com.baeldung.javaslang.exception.handling.client;
public class ClientException extends Exception {
public ClientException(String message) {
super(message);
}
}

View File

@ -0,0 +1,6 @@
package com.baeldung.javaslang.exception.handling.client;
public interface HttpClient {
Response call() throws ClientException;
}

View File

@ -0,0 +1,9 @@
package com.baeldung.javaslang.exception.handling.client;
public class Response {
public final String id;
public Response(String id) {
this.id = id;
}
}

View File

@ -0,0 +1,115 @@
package com.baeldung.javaslang.exception.handling;
import com.baeldung.javaslang.exception.handling.client.ClientException;
import com.baeldung.javaslang.exception.handling.client.HttpClient;
import com.baeldung.javaslang.exception.handling.client.Response;
import javaslang.collection.Stream;
import javaslang.control.Option;
import javaslang.control.Try;
import org.junit.Test;
import java.util.function.Function;
import java.util.stream.Collectors;
import static javaslang.API.Case;
import static javaslang.API.Match;
import static javaslang.Predicates.instanceOf;
import static org.junit.Assert.*;
public class JavaslangTryTest {
@Test
public void givenHttpClient_whenMakeACall_shouldReturnSuccess() {
//given
Integer defaultChainedResult = 1;
String id = "a";
HttpClient httpClient = () -> new Response(id);
//when
Try<Response> response = new JavaslangTry(httpClient).getResponse();
Integer chainedResult = response
.map(this::actionThatTakesResponse)
.getOrElse(defaultChainedResult);
Stream<String> stream = response.toStream().map(it -> it.id);
//then
assertTrue(!stream.isEmpty());
assertTrue(response.isSuccess());
response.onSuccess(r -> assertEquals(id, r.id));
response.andThen(r -> assertEquals(id, r.id));
assertNotEquals(defaultChainedResult, chainedResult);
}
@Test
public void givenHttpClientFailure_whenMakeACall_shouldReturnFailure() {
//given
Integer defaultChainedResult = 1;
HttpClient httpClient = () -> {
throw new ClientException("problem");
};
//when
Try<Response> response = new JavaslangTry(httpClient).getResponse();
Integer chainedResult = response
.map(this::actionThatTakesResponse)
.getOrElse(defaultChainedResult);
Option<Response> optionalResponse = response.toOption();
//then
assertTrue(optionalResponse.isEmpty());
assertTrue(response.isFailure());
response.onFailure(ex -> assertTrue(ex instanceof ClientException));
assertEquals(defaultChainedResult, chainedResult);
}
@Test
public void givenHttpClientThatFailure_whenMakeACall_shouldReturnFailureAndNotRecover() {
//given
Response defaultResponse = new Response("b");
HttpClient httpClient = () -> {
throw new RuntimeException("critical problem");
};
//when
Try<Response> recovered = new JavaslangTry(httpClient).getResponse()
.recover(r -> Match(r).of(
Case(instanceOf(ClientException.class), defaultResponse)
));
//then
assertTrue(recovered.isFailure());
// recovered.getOrElseThrow(throwable -> {
// throw new RuntimeException(throwable);
// });
}
@Test
public void givenHttpClientThatFailure_whenMakeACall_shouldReturnFailureAndRecover() {
//given
Response defaultResponse = new Response("b");
HttpClient httpClient = () -> {
throw new ClientException("non critical problem");
};
//when
Try<Response> recovered = new JavaslangTry(httpClient).getResponse()
.recover(r -> Match(r).of(
Case(instanceOf(ClientException.class), defaultResponse),
Case(instanceOf(IllegalArgumentException.class), defaultResponse)
));
//then
assertTrue(recovered.isSuccess());
}
public int actionThatTakesResponse(Response response) {
return response.id.hashCode();
}
public int actionThatTakesTryResponse(Try<Response> response, int defaultTransformation){
return response.transform(responses -> response.map(it -> it.id.hashCode()).getOrElse(defaultTransformation));
}
}