From 0396b699b3a56c5a35edb373e73a1221086cac0c Mon Sep 17 00:00:00 2001 From: "Benjamin, Max (mb388a)" Date: Thu, 9 Aug 2018 08:58:47 -0400 Subject: ParameterizedTypeReference now passed in An argument must be added other wise we cannot return anything other than a linkedhashmap because of type erasure. https://stackoverflow.com/questions/21987295/using-spring-resttemplate-in-generic-method-with-generic-parameter Issue-ID: SO-820 Change-Id: I96b5bf9db8389beacfcd79e7a80ee22b3d470a27 Signed-off-by: Benjamin, Max (mb388a) --- .../java/org/onap/so/client/sdnc/BaseClient.java | 13 +++--- .../java/org/onap/so/client/sdnc/SDNCClient.java | 6 ++- .../java/org/onap/so/client/sniro/SniroClient.java | 5 ++- .../org/onap/so/client/sdnc/BaseClientTest.java | 50 ++++++++++++++++++++++ 4 files changed, 63 insertions(+), 11 deletions(-) create mode 100644 bpmn/so-bpmn-tasks/src/test/java/org/onap/so/client/sdnc/BaseClientTest.java (limited to 'bpmn/so-bpmn-tasks/src') diff --git a/bpmn/so-bpmn-tasks/src/main/java/org/onap/so/client/sdnc/BaseClient.java b/bpmn/so-bpmn-tasks/src/main/java/org/onap/so/client/sdnc/BaseClient.java index 5a63d2097b..50137cf985 100644 --- a/bpmn/so-bpmn-tasks/src/main/java/org/onap/so/client/sdnc/BaseClient.java +++ b/bpmn/so-bpmn-tasks/src/main/java/org/onap/so/client/sdnc/BaseClient.java @@ -52,20 +52,19 @@ public class BaseClient { this.targetUrl = targetUrl; } - public O get(I data, Object... uriVariables) throws RestClientException { - return run(data, HttpMethod.GET, uriVariables); + public O get(I data, ParameterizedTypeReference typeRef, Object... uriVariables) throws RestClientException { + return run(data, HttpMethod.GET, typeRef, uriVariables); } - public O post(I data, Object... uriVariables) throws RestClientException { - return run(data, HttpMethod.POST, uriVariables); + public O post(I data, ParameterizedTypeReference typeRef, Object... uriVariables) throws RestClientException { + return run(data, HttpMethod.POST, typeRef, uriVariables); } - public O run(I data, HttpMethod method, Object... uriVariables) throws RestClientException { + public O run(I data, HttpMethod method, ParameterizedTypeReference typeRef, Object... uriVariables) throws RestClientException { HttpEntity requestEntity = new HttpEntity(data, getHttpHeader()); RestTemplate restTemplate = new RestTemplate(); restTemplate.setRequestFactory(new BufferingClientHttpRequestFactory(new SimpleClientHttpRequestFactory())); - ParameterizedTypeReference output = new ParameterizedTypeReference() {}; - ResponseEntity responseEntity = restTemplate.exchange(getTargetUrl(), method, requestEntity, output, + ResponseEntity responseEntity = restTemplate.exchange(getTargetUrl(), method, requestEntity, typeRef, uriVariables); return responseEntity.getBody(); } diff --git a/bpmn/so-bpmn-tasks/src/main/java/org/onap/so/client/sdnc/SDNCClient.java b/bpmn/so-bpmn-tasks/src/main/java/org/onap/so/client/sdnc/SDNCClient.java index 1f0d654a85..9e60196426 100644 --- a/bpmn/so-bpmn-tasks/src/main/java/org/onap/so/client/sdnc/SDNCClient.java +++ b/bpmn/so-bpmn-tasks/src/main/java/org/onap/so/client/sdnc/SDNCClient.java @@ -21,6 +21,7 @@ package org.onap.so.client.sdnc; import java.util.LinkedHashMap; + import javax.ws.rs.core.UriBuilder; import org.onap.so.client.exception.BadResponseException; @@ -29,6 +30,7 @@ import org.onap.so.client.sdnc.beans.SDNCProperties; import org.onap.so.client.sdnc.endpoint.SDNCTopology; import org.onap.so.logger.MsoLogger; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.core.ParameterizedTypeReference; import org.springframework.http.HttpHeaders; import org.springframework.stereotype.Component; @@ -58,7 +60,7 @@ public class SDNCClient { HttpHeaders httpHeader = sdnCommonTasks.getHttpHeaders(properties.getAuth()); STOClient.setHttpHeader(httpHeader); msoLogger.info("Running SDNC CLIENT for TargetUrl: " + targetUrl); - LinkedHashMap output = STOClient.post(jsonRequest); + LinkedHashMap output = STOClient.post(jsonRequest, new ParameterizedTypeReference>() {}); msoLogger.info("Validating output..."); return sdnCommonTasks.validateSDNResponse(output); } @@ -84,7 +86,7 @@ public class SDNCClient { HttpHeaders httpHeader = sdnCommonTasks.getHttpHeaders(properties.getAuth()); STOClient.setHttpHeader(httpHeader); msoLogger.info("Running SDNC CLIENT..."); - LinkedHashMap output = STOClient.get(jsonRequest); + LinkedHashMap output = STOClient.get(jsonRequest, new ParameterizedTypeReference>() {}); msoLogger.info("Validating output..."); return sdnCommonTasks.validateSDNGetResponse(output); } diff --git a/bpmn/so-bpmn-tasks/src/main/java/org/onap/so/client/sniro/SniroClient.java b/bpmn/so-bpmn-tasks/src/main/java/org/onap/so/client/sniro/SniroClient.java index 979b9e5e80..cee94e28bd 100644 --- a/bpmn/so-bpmn-tasks/src/main/java/org/onap/so/client/sniro/SniroClient.java +++ b/bpmn/so-bpmn-tasks/src/main/java/org/onap/so/client/sniro/SniroClient.java @@ -31,6 +31,7 @@ import org.onap.so.client.sniro.beans.SniroConductorRequest; import org.onap.so.client.sniro.beans.SniroManagerRequest; import org.onap.so.logger.MsoLogger; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.core.ParameterizedTypeReference; import org.springframework.http.HttpHeaders; import org.springframework.http.MediaType; import org.springframework.stereotype.Component; @@ -76,7 +77,7 @@ public class SniroClient { baseClient.setTargetUrl(url); baseClient.setHttpHeader(header); - LinkedHashMap response = baseClient.post(homingRequest.toJsonString()); + LinkedHashMap response = baseClient.post(homingRequest.toJsonString(), new ParameterizedTypeReference>() {}); validator.validateDemandsResponse(response); log.trace("Completed Sniro Client Post Demands"); } @@ -106,7 +107,7 @@ public class SniroClient { baseClient.setTargetUrl(url); baseClient.setHttpHeader(header); - LinkedHashMap response = baseClient.post(releaseRequest.toJsonString()); + LinkedHashMap response = baseClient.post(releaseRequest.toJsonString(), new ParameterizedTypeReference>() {}); SniroValidator v = new SniroValidator(); v.validateReleaseResponse(response); log.trace("Completed Sniro Client Post Release"); diff --git a/bpmn/so-bpmn-tasks/src/test/java/org/onap/so/client/sdnc/BaseClientTest.java b/bpmn/so-bpmn-tasks/src/test/java/org/onap/so/client/sdnc/BaseClientTest.java new file mode 100644 index 0000000000..a564d8a21d --- /dev/null +++ b/bpmn/so-bpmn-tasks/src/test/java/org/onap/so/client/sdnc/BaseClientTest.java @@ -0,0 +1,50 @@ +package org.onap.so.client.sdnc; + +import static com.github.tomakehurst.wiremock.client.WireMock.aResponse; +import static com.github.tomakehurst.wiremock.client.WireMock.get; +import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo; +import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options; +import static org.hamcrest.CoreMatchers.equalTo; +import static org.junit.Assert.assertThat; + +import java.util.Map; + +import javax.ws.rs.core.UriBuilder; + +import org.junit.Rule; +import org.junit.Test; +import org.springframework.core.ParameterizedTypeReference; + +import com.github.tomakehurst.wiremock.junit.WireMockRule; + +import wiremock.org.apache.http.entity.ContentType; +public class BaseClientTest { + + + @Rule + public WireMockRule wm = new WireMockRule(options().dynamicPort()); + + @Test + public void verifyString() { + BaseClient client = new BaseClient<>(); + String response = "{\"hello\" : \"world\"}"; + client.setTargetUrl(UriBuilder.fromUri("http://localhost/test").port(wm.port()).build().toString()); + wm.stubFor(get(urlEqualTo("/test")) + .willReturn(aResponse().withStatus(200).withBody(response).withHeader("Content-Type", ContentType.APPLICATION_JSON.toString()))); + + String result = client.get("", new ParameterizedTypeReference() {}); + assertThat(result, equalTo(response)); + } + + @Test + public void verifyMap() { + BaseClient> client = new BaseClient<>(); + String response = "{\"hello\" : \"world\"}"; + client.setTargetUrl(UriBuilder.fromUri("http://localhost/test").port(wm.port()).build().toString()); + wm.stubFor(get(urlEqualTo("/test")) + .willReturn(aResponse().withStatus(200).withBody(response).withHeader("Content-Type", ContentType.APPLICATION_JSON.toString()))); + + Map result = client.get("", new ParameterizedTypeReference>() {}); + assertThat("world", equalTo(result.get("hello"))); + } +} -- cgit 1.2.3-korg