diff options
author | Michal Kabaj <michal.kabaj@nokia.com> | 2018-12-17 17:10:24 +0100 |
---|---|---|
committer | Lukasz Muszkieta <lukasz.muszkieta@nokia.com> | 2019-01-04 12:02:36 +0000 |
commit | 61b78a7d5f1eb8f1c5955d6d1439ce9225a39b46 (patch) | |
tree | 4f6ab37c32786ad489f6d8f2130d1b03105f8c32 /common | |
parent | 85a47c701c3b34c58d2b121ba466d41ffb4fb7b0 (diff) |
HttpClientFactory to create HttpClient instances
-Replace constructor calls with existing factory
-Add create methods to factory for each required Media Type
Change-Id: Ibd03c10230c87a0413c0ec529e0ea9ac800444f9
Issue-ID: SO-1344
Signed-off-by: Michal Kabaj <michal.kabaj@nokia.com>
Diffstat (limited to 'common')
4 files changed, 27 insertions, 17 deletions
diff --git a/common/src/main/java/org/onap/so/client/HttpClient.java b/common/src/main/java/org/onap/so/client/HttpClient.java index 66e970a154..6f13a86237 100644 --- a/common/src/main/java/org/onap/so/client/HttpClient.java +++ b/common/src/main/java/org/onap/so/client/HttpClient.java @@ -34,10 +34,10 @@ public class HttpClient extends RestClient { protected final Logger log = LoggerFactory.getLogger(HttpClient.class); private TargetEntity targetEntity; - public HttpClient(URL host, String contentType, TargetEntity targetEntity) { + + HttpClient(URL host, String contentType, TargetEntity targetEntity) { super(host, contentType); this.targetEntity = targetEntity; - } @Override diff --git a/common/src/main/java/org/onap/so/client/HttpClientFactory.java b/common/src/main/java/org/onap/so/client/HttpClientFactory.java index 9705d7d0c4..d02c18ff56 100644 --- a/common/src/main/java/org/onap/so/client/HttpClientFactory.java +++ b/common/src/main/java/org/onap/so/client/HttpClientFactory.java @@ -20,11 +20,20 @@ package org.onap.so.client; import java.net.URL; +import javax.ws.rs.core.MediaType; import org.onap.so.utils.TargetEntity; public class HttpClientFactory { - public HttpClient create(URL host, String contentType, TargetEntity targetEntity) { - return new HttpClient(host, contentType, targetEntity); + public HttpClient newJsonClient(URL host, TargetEntity targetEntity) { + return new HttpClient(host, MediaType.APPLICATION_JSON, targetEntity); + } + + public HttpClient newXmlClient(URL host, TargetEntity targetEntity) { + return new HttpClient(host, MediaType.APPLICATION_XML, targetEntity); + } + + public HttpClient newTextXmlClient(URL host, TargetEntity targetEntity) { + return new HttpClient(host, MediaType.TEXT_XML, targetEntity); } } diff --git a/common/src/test/java/org/onap/so/client/HttpClientTest.java b/common/src/test/java/org/onap/so/client/HttpClientTest.java index 221005cb7c..75ce5f322f 100644 --- a/common/src/test/java/org/onap/so/client/HttpClientTest.java +++ b/common/src/test/java/org/onap/so/client/HttpClientTest.java @@ -30,22 +30,21 @@ import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo; import static com.github.tomakehurst.wiremock.client.WireMock.verify; import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig; +import com.github.tomakehurst.wiremock.junit.WireMockRule; import java.net.MalformedURLException; import java.net.URL; - import org.junit.Rule; import org.junit.Test; import org.onap.so.utils.TargetEntity; -import com.github.tomakehurst.wiremock.junit.WireMockRule; - public class HttpClientTest{ + private final HttpClientFactory httpClientFactory = new HttpClientFactory(); @Rule public WireMockRule wireMockRule = new WireMockRule(wireMockConfig().dynamicHttpsPort()); - @Test(expected = Test.None.class) + @Test public void testPost_success() throws MalformedURLException{ stubFor(post(urlEqualTo("/services/sdnc/post")) @@ -54,7 +53,7 @@ public class HttpClientTest{ .withBody(""))); URL url = new URL("http://localhost:" + wireMockConfig().portNumber() + "/services/sdnc/post"); - HttpClient client = new HttpClient(url, "application/json", TargetEntity.BPMN); + HttpClient client = httpClientFactory.newJsonClient(url, TargetEntity.BPMN); client.addBasicAuthHeader("97FF88AB352DA16E00DDD81E3876431DEF8744465DACA489EB3B3BE1F10F63EDA1715E626D0A4827A3E19CD88421BF", "123"); client.addAdditionalHeader("Accept", "application/json"); @@ -64,7 +63,7 @@ public class HttpClientTest{ verify(exactly(1), postRequestedFor(urlEqualTo("/services/sdnc/post"))); } - @Test(expected = Test.None.class) + @Test public void testPost_nullHeader() throws MalformedURLException{ stubFor(post(urlEqualTo("/services/sdnc/post")) @@ -73,7 +72,7 @@ public class HttpClientTest{ .withBody(""))); URL url = new URL("http://localhost:" + wireMockConfig().portNumber() + "/services/sdnc/post"); - HttpClient client = new HttpClient(url, "application/json", TargetEntity.BPMN); + HttpClient client = httpClientFactory.newJsonClient(url, TargetEntity.BPMN); client.addAdditionalHeader("Accept", "application/json"); client.addAdditionalHeader("id", null); @@ -84,7 +83,7 @@ public class HttpClientTest{ .withHeader("Accept", equalTo("application/json"))); } - @Test(expected = Test.None.class) + @Test public void testPost_nullBasicAuth() throws MalformedURLException{ stubFor(post(urlEqualTo("/services/sdnc/post")) @@ -93,7 +92,7 @@ public class HttpClientTest{ .withBody(""))); URL url = new URL("http://localhost:" + wireMockConfig().portNumber() + "/services/sdnc/post"); - HttpClient client = new HttpClient(url, "application/json", TargetEntity.BPMN); + HttpClient client = httpClientFactory.newJsonClient(url, TargetEntity.BPMN); client.addBasicAuthHeader("", "12345"); client.addAdditionalHeader("Accept", "application/json"); 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 6fca4a1820..06196fd73e 100644 --- a/common/src/test/java/org/onap/so/client/RestClientTest.java +++ b/common/src/test/java/org/onap/so/client/RestClientTest.java @@ -47,11 +47,12 @@ import org.mockito.junit.MockitoJUnitRunner; @RunWith(MockitoJUnitRunner.class) public class RestClientTest { - + + private final HttpClientFactory httpClientFactory = new HttpClientFactory(); @Mock private RestProperties props; - - + + @Test public void retries() throws Exception { RestClient spy = buildSpy(); @@ -82,7 +83,8 @@ public class RestClientTest { } private RestClient buildSpy() throws MalformedURLException, IllegalArgumentException, UriBuilderException { - RestClient client = new HttpClient(UriBuilder.fromUri("http://localhost/test").build().toURL(), "application/json", TargetEntity.BPMN); + RestClient client = httpClientFactory + .newJsonClient(UriBuilder.fromUri("http://localhost/test").build().toURL(), TargetEntity.BPMN); return spy(client); } |