diff options
Diffstat (limited to 'common/src')
4 files changed, 76 insertions, 6 deletions
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 ece1333f83..9fce328b1d 100644 --- a/common/src/main/java/org/onap/so/client/RestClient.java +++ b/common/src/main/java/org/onap/so/client/RestClient.java @@ -34,10 +34,12 @@ 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; import javax.ws.rs.client.Invocation.Builder; +import javax.ws.rs.client.ResponseProcessingException; import javax.ws.rs.client.WebTarget; import javax.ws.rs.core.GenericType; import javax.ws.rs.core.MediaType; @@ -187,7 +189,7 @@ public abstract class RestClient { } protected Client getClient() { - return ClientBuilder.newBuilder().build(); + return ClientBuilder.newBuilder().readTimeout(props.getReadTimeout(), TimeUnit.MILLISECONDS).build(); } protected abstract ONAPComponentsList getTargetEntity(); @@ -201,7 +203,6 @@ public abstract class RestClient { metricLogClientFilter = new SOMetricLogClientFilter(); mdcSetup.setTargetEntity(getTargetEntity()); client.register(metricLogClientFilter); - if (!path.isPresent()) { webTarget = client.target(host.toString()); } else { @@ -225,6 +226,9 @@ public abstract class RestClient { result.add(e -> { return e.getCause() instanceof ConnectException; }); + result.add(e -> { + return e.getCause() instanceof ResponseProcessingException; + }); return result; } diff --git a/common/src/main/java/org/onap/so/client/RestClientSSL.java b/common/src/main/java/org/onap/so/client/RestClientSSL.java index 1e8953892e..8956e20a5a 100644 --- a/common/src/main/java/org/onap/so/client/RestClientSSL.java +++ b/common/src/main/java/org/onap/so/client/RestClientSSL.java @@ -24,6 +24,7 @@ import java.net.URI; import java.security.KeyStore; import java.security.NoSuchAlgorithmException; import java.util.Optional; +import java.util.concurrent.TimeUnit; import javax.net.ssl.SSLContext; import javax.ws.rs.client.Client; import javax.ws.rs.client.ClientBuilder; @@ -56,7 +57,8 @@ public abstract class RestClientSSL extends RestClient { } } // Use default SSL context - client = ClientBuilder.newBuilder().sslContext(SSLContext.getDefault()).build(); + client = ClientBuilder.newBuilder().sslContext(SSLContext.getDefault()) + .readTimeout(props.getReadTimeout(), TimeUnit.MILLISECONDS).build(); logger.info("RestClientSSL using default SSL context!"); } catch (NoSuchAlgorithmException e) { throw new RuntimeException(e); diff --git a/common/src/main/java/org/onap/so/client/RestProperties.java b/common/src/main/java/org/onap/so/client/RestProperties.java index 9e4e99cb4e..36da424f93 100644 --- a/common/src/main/java/org/onap/so/client/RestProperties.java +++ b/common/src/main/java/org/onap/so/client/RestProperties.java @@ -40,4 +40,13 @@ public interface RestProperties { public default boolean mapNotFoundToEmpty() { return false; } + + /** + * Time in milliseconds + * + * @return + */ + public default Long getReadTimeout() { + return Long.valueOf(60000); + } } 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 cd00a9e4de..c6e282c14a 100644 --- a/common/src/test/java/org/onap/so/client/RestClientTest.java +++ b/common/src/test/java/org/onap/so/client/RestClientTest.java @@ -21,6 +21,8 @@ package org.onap.so.client; +import static com.github.tomakehurst.wiremock.client.WireMock.aResponse; +import static com.github.tomakehurst.wiremock.client.WireMock.get; import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.doThrow; import static org.mockito.Mockito.spy; @@ -28,7 +30,13 @@ import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; import java.net.MalformedURLException; import java.net.SocketTimeoutException; +import java.net.URI; +import java.net.URISyntaxException; +import java.net.URL; +import java.util.Map; +import java.util.Optional; import javax.ws.rs.NotFoundException; +import javax.ws.rs.ProcessingException; import javax.ws.rs.WebApplicationException; import javax.ws.rs.core.UriBuilder; import javax.ws.rs.core.UriBuilderException; @@ -37,21 +45,24 @@ import org.junit.Test; import org.junit.rules.ExpectedException; import org.junit.runner.RunWith; import org.mockito.ArgumentMatchers; -import org.mockito.Mock; import org.mockito.junit.MockitoJUnitRunner; import org.onap.logging.filter.base.ONAPComponents; +import org.onap.logging.filter.base.ONAPComponentsList; +import com.github.tomakehurst.wiremock.core.WireMockConfiguration; +import com.github.tomakehurst.wiremock.junit.WireMockRule; @RunWith(MockitoJUnitRunner.class) public class RestClientTest { private final HttpClientFactory httpClientFactory = new HttpClientFactory(); - @Mock - private RestProperties props; @Rule public ExpectedException thrown = ExpectedException.none(); + @Rule + public WireMockRule wireMockRule = new WireMockRule(WireMockConfiguration.options().dynamicPort()); + @Test public void retries() throws Exception { RestClient spy = buildSpy(); @@ -80,6 +91,50 @@ public class RestClientTest { } + @Test + public void timeoutTest() throws URISyntaxException { + wireMockRule.stubFor(get("/chunked/delayed") + .willReturn(aResponse().withStatus(200).withBody("Hello world!").withChunkedDribbleDelay(2, 300))); + + + RestProperties props = new RestProperties() { + + @Override + public URL getEndpoint() throws MalformedURLException { + return new URL(String.format("http://localhost:%s", wireMockRule.port())); + } + + @Override + public String getSystemName() { + // TODO Auto-generated method stub + return null; + } + + @Override + public Long getReadTimeout() { + return Long.valueOf(100); + } + }; + RestClient client = new RestClient(props, Optional.of(new URI("/chunked/delayed"))) { + + @Override + protected void initializeHeaderMap(Map<String, String> headerMap) { + // TODO Auto-generated method stub + + } + + @Override + protected ONAPComponentsList getTargetEntity() { + return ONAPComponents.EXTERNAL; + } + + }; + + thrown.expect(ProcessingException.class); + client.get(); + + } + private RestClient buildSpy() throws MalformedURLException, IllegalArgumentException, UriBuilderException { RestClient client = httpClientFactory.newJsonClient(UriBuilder.fromUri("http://localhost/test").build().toURL(), ONAPComponents.BPMN); |