diff options
author | Benjamin, Max <max.benjamin@att.com> | 2019-06-26 11:26:55 -0400 |
---|---|---|
committer | Max Benjamin <max.benjamin@att.com> | 2019-06-27 16:09:30 +0000 |
commit | 577bd39ac9e9495b0c6a38e6f7c567acfcba1c42 (patch) | |
tree | 267d3ce07458957189630ddf015338279256f2a6 | |
parent | 5c58eeca5ffd8b080b9af7619a903eb0acff3a0d (diff) |
Update failsafe dependency to 2.0.1
Updated calls to failsafe 2.0.1 API
Change-Id: I2c86ca02d56a07262e330ee5f8f088d1a1f5b4a7
Issue-ID: SO-2057
Signed-off-by: Benjamin, Max (mb388a) <mb388a@us.att.com>
6 files changed, 48 insertions, 34 deletions
diff --git a/adapters/mso-adapter-utils/src/main/java/org/onap/so/cloud/authentication/KeystoneV3Authentication.java b/adapters/mso-adapter-utils/src/main/java/org/onap/so/cloud/authentication/KeystoneV3Authentication.java index 42d200a130..16906957a7 100644 --- a/adapters/mso-adapter-utils/src/main/java/org/onap/so/cloud/authentication/KeystoneV3Authentication.java +++ b/adapters/mso-adapter-utils/src/main/java/org/onap/so/cloud/authentication/KeystoneV3Authentication.java @@ -20,10 +20,10 @@ package org.onap.so.cloud.authentication; +import java.time.Duration; import java.util.ArrayList; import java.util.Arrays; import java.util.List; -import java.util.concurrent.TimeUnit; import java.util.function.Predicate; import org.onap.so.config.beans.PoConfig; import org.onap.so.db.catalog.beans.CloudIdentity; @@ -89,8 +89,7 @@ public class KeystoneV3Authentication { return result; } - protected RetryPolicy createRetryPolicy() { - RetryPolicy policy = new RetryPolicy(); + protected RetryPolicy<OpenStackResponse> createRetryPolicy() { List<Predicate<Throwable>> result = new ArrayList<>(); result.add(e -> { return e.getCause() instanceof OpenStackResponseException @@ -102,10 +101,8 @@ public class KeystoneV3Authentication { }); Predicate<Throwable> pred = result.stream().reduce(Predicate::or).orElse(x -> false); - - policy.retryOn(error -> pred.test(error)); - - policy.withDelay(poConfig.getRetryDelay(), TimeUnit.SECONDS).withMaxRetries(poConfig.getRetryCount()); + RetryPolicy<OpenStackResponse> policy = new RetryPolicy<OpenStackResponse>().handleIf(error -> pred.test(error)) + .withDelay(Duration.ofSeconds(poConfig.getRetryDelay())).withMaxRetries(poConfig.getRetryCount()); return policy; } diff --git a/common/pom.xml b/common/pom.xml index 4490b63c3b..a52897170a 100644 --- a/common/pom.xml +++ b/common/pom.xml @@ -111,7 +111,7 @@ <dependency> <groupId>net.jodah</groupId> <artifactId>failsafe</artifactId> - <version>1.1.0</version> + <version>2.0.1</version> </dependency> <dependency> <groupId>org.springframework.security</groupId> diff --git a/common/src/main/java/org/onap/so/client/RestClient.java b/common/src/main/java/org/onap/so/client/RestClient.java index 0b3aa651a0..d3a4d444f7 100644 --- a/common/src/main/java/org/onap/so/client/RestClient.java +++ b/common/src/main/java/org/onap/so/client/RestClient.java @@ -26,6 +26,7 @@ import java.net.SocketTimeoutException; import java.net.URI; import java.net.URL; import java.security.GeneralSecurityException; +import java.time.Duration; import java.util.ArrayList; import java.util.Base64; import java.util.HashMap; @@ -33,7 +34,6 @@ import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.Optional; -import java.util.concurrent.TimeUnit; import java.util.function.Predicate; import javax.ws.rs.client.Client; import javax.ws.rs.client.ClientBuilder; @@ -275,22 +275,20 @@ public abstract class RestClient { } public Response method(String method, Object entity) { - RetryPolicy policy = new RetryPolicy(); List<Predicate<Throwable>> items = retryOn(); Predicate<Throwable> pred = items.stream().reduce(Predicate::or).orElse(x -> false); - policy.retryOn(error -> pred.test(error)); + RetryPolicy<Object> policy = + new RetryPolicy<>().handleIf(pred).withDelay(Duration.ofMillis(this.props.getDelayBetweenRetries())) + .withMaxRetries(this.props.getRetries()); - policy.withDelay(this.props.getDelayBetweenRetries(), TimeUnit.MILLISECONDS) - .withMaxRetries(this.props.getRetries()); - - return Failsafe.with(policy).get(buildRequest(method, entity)); + return Failsafe.with(policy).get(() -> buildRequest(method, entity)); } - protected RestRequest buildRequest(String method, Object entity) { - return new RestRequest(this, method, entity); + protected Response buildRequest(String method, Object entity) throws Exception { + return new RestRequest(this, method, entity).get(); } private <T> Optional<T> format(Response response, Class<T> resultClass) { diff --git a/common/src/main/java/org/onap/so/client/RestRequest.java b/common/src/main/java/org/onap/so/client/RestRequest.java index 9d2fa42d00..9e6e818171 100644 --- a/common/src/main/java/org/onap/so/client/RestRequest.java +++ b/common/src/main/java/org/onap/so/client/RestRequest.java @@ -23,15 +23,15 @@ package org.onap.so.client; import java.util.Optional; -import java.util.concurrent.Callable; import javax.ws.rs.HttpMethod; import javax.ws.rs.NotFoundException; import javax.ws.rs.client.Entity; import javax.ws.rs.core.Response; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import net.jodah.failsafe.function.CheckedSupplier; -public class RestRequest implements Callable<Response> { +public class RestRequest implements CheckedSupplier<Response> { private static final Logger logger = LoggerFactory.getLogger(RestRequest.class); @@ -46,7 +46,7 @@ public class RestRequest implements Callable<Response> { } @Override - public Response call() throws Exception { + public Response get() throws Exception { final Response response; if ("GET".equals(method)) { response = this.client.getBuilder().accept(this.client.getAccept()).get(); diff --git a/common/src/main/java/org/onap/so/rest/exceptions/ExhaustedRetriesException.java b/common/src/main/java/org/onap/so/rest/exceptions/ExhaustedRetriesException.java new file mode 100644 index 0000000000..6c91516b84 --- /dev/null +++ b/common/src/main/java/org/onap/so/rest/exceptions/ExhaustedRetriesException.java @@ -0,0 +1,19 @@ +package org.onap.so.rest.exceptions; + +public class ExhaustedRetriesException extends RuntimeException { + + private static final long serialVersionUID = -8303091412739222943L; + + public ExhaustedRetriesException(String s) { + super(s); + } + + public ExhaustedRetriesException(Throwable t) { + super(t); + } + + public ExhaustedRetriesException(String s, Throwable t) { + super(s, t); + } + +} diff --git a/common/src/test/java/org/onap/so/client/RestClientTest.java b/common/src/test/java/org/onap/so/client/RestClientTest.java index b550079607..745dee23e8 100644 --- a/common/src/test/java/org/onap/so/client/RestClientTest.java +++ b/common/src/test/java/org/onap/so/client/RestClientTest.java @@ -22,24 +22,24 @@ package org.onap.so.client; import static org.mockito.ArgumentMatchers.any; -import static org.mockito.Mockito.doReturn; -import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.doThrow; import static org.mockito.Mockito.spy; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; import java.net.MalformedURLException; import java.net.SocketTimeoutException; import javax.ws.rs.NotFoundException; import javax.ws.rs.WebApplicationException; import javax.ws.rs.core.UriBuilder; import javax.ws.rs.core.UriBuilderException; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; import org.junit.runner.RunWith; -import org.mockito.Mock; import org.mockito.ArgumentMatchers; -import org.onap.so.utils.TargetEntity; +import org.mockito.Mock; import org.mockito.junit.MockitoJUnitRunner; +import org.onap.so.utils.TargetEntity; @RunWith(MockitoJUnitRunner.class) public class RestClientTest { @@ -49,34 +49,34 @@ public class RestClientTest { @Mock private RestProperties props; + @Rule + public ExpectedException thrown = ExpectedException.none(); @Test public void retries() throws Exception { RestClient spy = buildSpy(); - RestRequest mockCallable = mock(RestRequest.class); - when(mockCallable.call()).thenThrow(new WebApplicationException(new SocketTimeoutException())); - doReturn(mockCallable).when(spy).buildRequest(any(String.class), ArgumentMatchers.isNull()); + doThrow(new WebApplicationException(new SocketTimeoutException())).when(spy).buildRequest(any(String.class), + ArgumentMatchers.isNull()); try { spy.get(); } catch (Exception e) { - // we expect an exception, ignore it + // ignore this exception for this test } - verify(mockCallable, times(3)).call(); + verify(spy, times(3)).buildRequest(any(String.class), ArgumentMatchers.isNull()); } @Test public void exceptionDoNotRetry() throws Exception { RestClient spy = buildSpy(); - RestRequest mockCallable = mock(RestRequest.class); - when(mockCallable.call()).thenThrow(new WebApplicationException(new NotFoundException())); - doReturn(mockCallable).when(spy).buildRequest(any(String.class), ArgumentMatchers.isNull()); + doThrow(new WebApplicationException(new NotFoundException())).when(spy).buildRequest(any(String.class), + ArgumentMatchers.isNull()); try { spy.get(); } catch (Exception e) { // we expect an exception, ignore it } - verify(mockCallable, times(1)).call(); + verify(spy, times(1)).buildRequest(any(String.class), ArgumentMatchers.isNull()); } |