diff options
Diffstat (limited to 'appc-outbound/appc-network-inventory-client')
3 files changed, 103 insertions, 59 deletions
diff --git a/appc-outbound/appc-network-inventory-client/provider/pom.xml b/appc-outbound/appc-network-inventory-client/provider/pom.xml index fa8af2f21..58d0e9e39 100755 --- a/appc-outbound/appc-network-inventory-client/provider/pom.xml +++ b/appc-outbound/appc-network-inventory-client/provider/pom.xml @@ -64,7 +64,7 @@ </exclusions>
</dependency>
<dependency>
- <groupId>com.sun.jersey</groupId>
+ <groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-client</artifactId>
<scope>provided</scope>
</dependency>
diff --git a/appc-outbound/appc-network-inventory-client/provider/src/main/java/org/onap/appc/instar/dme2client/Dme2Client.java b/appc-outbound/appc-network-inventory-client/provider/src/main/java/org/onap/appc/instar/dme2client/Dme2Client.java index 51bd9c1e3..5bbaa6829 100644 --- a/appc-outbound/appc-network-inventory-client/provider/src/main/java/org/onap/appc/instar/dme2client/Dme2Client.java +++ b/appc-outbound/appc-network-inventory-client/provider/src/main/java/org/onap/appc/instar/dme2client/Dme2Client.java @@ -40,12 +40,15 @@ import org.onap.appc.instar.utils.InstarClientConstant; import org.onap.ccsdk.sli.core.sli.SvcLogicException; import com.att.eelf.configuration.EELFLogger; import com.att.eelf.configuration.EELFManager; -import com.sun.jersey.api.client.Client; -import com.sun.jersey.api.client.ClientResponse; -import com.sun.jersey.api.client.WebResource; -import com.sun.jersey.api.client.config.DefaultClientConfig; -import com.sun.jersey.api.client.filter.HTTPBasicAuthFilter; -import com.sun.jersey.client.urlconnection.HTTPSProperties; + + +import javax.ws.rs.client.Client; +import javax.ws.rs.client.ClientBuilder; +import javax.ws.rs.client.Entity; +import javax.ws.rs.client.WebTarget; +import javax.ws.rs.core.Response; +import javax.ws.rs.core.Feature; +import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature; public class Dme2Client { @@ -84,7 +87,7 @@ public class Dme2Client { } } - private ClientResponse sendToInstar() throws SvcLogicException { + private Response sendToInstar() throws SvcLogicException { log.info("Called Send with operation Name=" + this.operationName + "and = " + properties.getProperty(operationName + InstarClientConstant.BASE_URL)); @@ -94,8 +97,8 @@ public class Dme2Client { log.info("DME Endpoint URI:" + resourceUri); Client client = null; - WebResource webResource; - ClientResponse clientResponse = null; + WebTarget webResource; + Response clientResponse = null; String authorization = properties.getProperty("authorization"); String requestDataType = "application/json"; String responseDataType = MediaType.APPLICATION_JSON; @@ -107,28 +110,27 @@ public class Dme2Client { log.info("authorization = " + authorization + "methodType= " + methodType); try { - DefaultClientConfig defaultClientConfig = new DefaultClientConfig(); System.setProperty("jsse.enableSNIExtension", "false"); SSLContext sslContext; SecureRestClientTrustManager secureRestClientTrustManager = new SecureRestClientTrustManager(); sslContext = SSLContext.getInstance("SSL"); sslContext.init(null, new javax.net.ssl.TrustManager[]{secureRestClientTrustManager}, null); - defaultClientConfig - .getProperties() - .put(HTTPSProperties.PROPERTY_HTTPS_PROPERTIES, new HTTPSProperties(getHostnameVerifier(), sslContext)); - client = Client.create(defaultClientConfig); - client.addFilter(new HTTPBasicAuthFilter(userId, password)); - webResource = client.resource(new URI(resourceUri)); - webResource.setProperty("Content-Type", "application/json;charset=UTF-8"); + + + client = ClientBuilder.newBuilder().sslContext(sslContext).hostnameVerifier(getHostnameVerifier()).build(); + + client.register(HttpAuthenticationFeature.basic(userId, password)); + webResource = client.target(new URI(resourceUri)); + webResource.property("Content-Type", "application/json;charset=UTF-8"); if (HttpMethod.GET.equalsIgnoreCase(methodType)) { - clientResponse = webResource.accept(responseDataType).get(ClientResponse.class); + clientResponse = webResource.request(responseDataType).get(Response.class); } else if (HttpMethod.POST.equalsIgnoreCase(methodType)) { - clientResponse = webResource.type(requestDataType).post(ClientResponse.class, request); + clientResponse = webResource.request(requestDataType).post( Entity.json(request),Response.class); } else if (HttpMethod.PUT.equalsIgnoreCase(methodType)) { - clientResponse = webResource.type(requestDataType).put(ClientResponse.class, request); + clientResponse = webResource.request(requestDataType).put( Entity.json(request),Response.class); } else if (HttpMethod.DELETE.equalsIgnoreCase(methodType)) { - clientResponse = webResource.delete(ClientResponse.class); + clientResponse = webResource.request().delete(Response.class); } return clientResponse; @@ -141,7 +143,7 @@ public class Dme2Client { } finally { // clean up. if (client != null) { - client.destroy(); + client.close(); } } } @@ -171,9 +173,9 @@ public class Dme2Client { return IOUtils.toString(Dme2Client.class.getClassLoader().getResourceAsStream("/tmp/sampleResponse"), Charset.defaultCharset()); } - ClientResponse clientResponse = sendToInstar(); + Response clientResponse = sendToInstar(); if (clientResponse != null) { - response = clientResponse.getEntity(String.class); + response = clientResponse.readEntity(String.class); log.info(clientResponse.getStatus() + " Status, Response :" + response); } } catch (Exception e) { diff --git a/appc-outbound/appc-network-inventory-client/provider/src/test/java/org/onap/appc/instar/node/TestDme2Client.java b/appc-outbound/appc-network-inventory-client/provider/src/test/java/org/onap/appc/instar/node/TestDme2Client.java index 7e2a41820..7c1312951 100644 --- a/appc-outbound/appc-network-inventory-client/provider/src/test/java/org/onap/appc/instar/node/TestDme2Client.java +++ b/appc-outbound/appc-network-inventory-client/provider/src/test/java/org/onap/appc/instar/node/TestDme2Client.java @@ -29,6 +29,10 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNull; import static org.mockito.Matchers.anyObject; import static org.mockito.Mockito.when; +import static org.mockito.Matchers.eq; +import static org.mockito.Matchers.any; +import static org.mockito.Matchers.anyString; +import static org.mockito.Mockito.doReturn; import java.io.InputStream; import java.net.URI; import java.security.NoSuchAlgorithmException; @@ -44,49 +48,82 @@ import org.onap.appc.instar.utils.InstarClientConstant; import org.powermock.api.mockito.PowerMockito; import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; +import static org.powermock.api.mockito.PowerMockito.mockStatic; +import static javax.ws.rs.core.Response.Status.FORBIDDEN; +import static javax.ws.rs.core.Response.Status.OK; +import static javax.ws.rs.core.Response.ResponseBuilder; + import org.powermock.reflect.Whitebox; -import com.sun.jersey.api.client.Client; -import com.sun.jersey.api.client.ClientResponse; -import com.sun.jersey.api.client.WebResource; -import com.sun.jersey.api.client.WebResource.Builder; +import javax.ws.rs.client.Client; +import javax.ws.rs.client.WebTarget; +import javax.ws.rs.client.Invocation; +import javax.ws.rs.core.Response; +import javax.ws.rs.client.Entity; +import javax.ws.rs.client.ClientBuilder; @RunWith(PowerMockRunner.class) -@PrepareForTest({InstarClientConstant.class, SSLContext.class, Client.class}) +@PrepareForTest({InstarClientConstant.class, SSLContext.class, Client.class,ClientBuilder.class}) public class TestDme2Client { - + + private static final String ANY = "notNullString"; private Dme2Client dme2; private InputStream inputStream; private SSLContext sslContext; private Properties properties; private Client client; - private WebResource webResource; - private Builder builder; - private ClientResponse clientResponse; + private WebTarget webResource; + private Response clientResponse; + private ClientBuilder clientBuilder; + + private Invocation.Builder builder; + @Before public void setUp() throws Exception { inputStream = Mockito.mock(InputStream.class); + sslContext = PowerMockito.mock(SSLContext.class); + client = Mockito.mock(Client.class); - builder = Mockito.mock(Builder.class); - clientResponse = Mockito.mock(ClientResponse.class); - webResource = Mockito.mock(WebResource.class); + builder = Mockito.mock(Invocation.Builder.class); + clientResponse = Mockito.mock(Response.class); + webResource = Mockito.mock(WebTarget.class); + clientBuilder = Mockito.mock(ClientBuilder.class); + HashMap<String, String> data = new HashMap<String, String>(); data.put("subtext", "value"); - PowerMockito.mockStatic(InstarClientConstant.class); - PowerMockito.mockStatic(SSLContext.class); - PowerMockito.mockStatic(Client.class); + + + mockStatic(InstarClientConstant.class); PowerMockito.when(InstarClientConstant.getEnvironmentVariable("SDNC_CONFIG_DIR")) .thenReturn("test"); + + PowerMockito.when(InstarClientConstant.getInputStream("test/outbound.properties")) .thenReturn(inputStream); + + mockStatic(SSLContext.class); PowerMockito.when(SSLContext.getInstance("SSL")).thenReturn(sslContext); - PowerMockito.when(Client.create(anyObject())).thenReturn(client); - PowerMockito.when(client.resource(new URI("nullnullnullvalue"))).thenReturn(webResource); - PowerMockito.when(builder.get(ClientResponse.class)).thenReturn(clientResponse); + mockStatic(ClientBuilder.class); + + PowerMockito.when(ClientBuilder.newBuilder()).thenReturn(clientBuilder); + doReturn(clientBuilder).when(clientBuilder).sslContext(any()); + doReturn(clientBuilder).when(clientBuilder).hostnameVerifier(any()); + + PowerMockito.when(clientBuilder.build()).thenReturn(client); + + PowerMockito.when(client.target(any(URI.class))).thenReturn(webResource); + + + PowerMockito.when(webResource.request(eq("Content-Type"),anyString())).thenReturn(builder); + PowerMockito.when(webResource.request(anyString())).thenReturn(builder); + + PowerMockito.when(builder.get(eq(Response.class))).thenReturn(clientResponse); + properties = Mockito.mock(Properties.class); dme2 = new Dme2Client("opt", "subtext", data); + Whitebox.setInternalState(dme2, "properties", properties); when(properties.getProperty("MechID")).thenReturn("123"); when(properties.getProperty("MechPass")).thenReturn("password"); @@ -94,36 +131,41 @@ public class TestDme2Client { @Test public void testSendtoInstarGet() throws Exception { - PowerMockito.when(webResource.accept("application/json")).thenReturn(builder); - PowerMockito.when(clientResponse.getEntity(String.class)).thenReturn("Get Success"); - when(properties.getProperty("getIpAddressByVnf_method")).thenReturn("GET"); + PowerMockito.when(webResource.request("application/json")).thenReturn(builder); + PowerMockito.when(clientResponse.readEntity(String.class)).thenReturn("Get Success"); + when(properties.getProperty(eq("getIpAddressByVnf_method"))).thenReturn("GET"); + assertEquals("Get Success", dme2.send()); } @Test public void testSendtoInstarPut() throws Exception { - PowerMockito.when(webResource.type("application/json")).thenReturn(builder); - PowerMockito.when(builder.put(ClientResponse.class, "")).thenReturn(clientResponse); - PowerMockito.when(clientResponse.getEntity(String.class)).thenReturn("Put Success"); + + PowerMockito.when(builder.put(any(Entity.class),eq(Response.class))).thenReturn(clientResponse); + + PowerMockito.when(clientResponse.readEntity(String.class)).thenReturn("Put Success"); + when(properties.getProperty("getIpAddressByVnf_method")).thenReturn("PUT"); assertEquals("Put Success", dme2.send()); } @Test public void testSendtoInstarPost() throws Exception { - PowerMockito.when(webResource.type("application/json")).thenReturn(builder); - PowerMockito.when(builder.post(ClientResponse.class, "")).thenReturn(clientResponse); - PowerMockito.when(clientResponse.getEntity(String.class)).thenReturn("Post Success"); + ResponseBuilder responseBuilder = clientResponse.ok(); + responseBuilder.encoding("Post Success").build(); + PowerMockito.when(builder.post(any(Entity.class),eq(Response.class))).thenReturn(clientResponse); + PowerMockito.when(clientResponse.readEntity(String.class)).thenReturn("Post Success"); when(properties.getProperty("getIpAddressByVnf_method")).thenReturn("POST"); assertEquals("Post Success", dme2.send()); } @Test public void testSendtoInstarDelete() throws Exception { - PowerMockito.when(webResource.delete(ClientResponse.class)).thenReturn(clientResponse); - PowerMockito.when(clientResponse.getEntity(String.class)).thenReturn("Delete Success"); + ResponseBuilder responseBuilder = Response.ok(); + PowerMockito.when(webResource.request(anyString()).delete(eq(Response.class))).thenReturn(clientResponse); + PowerMockito.when(clientResponse.readEntity(String.class)).thenReturn("Delete Success"); when(properties.getProperty("getIpAddressByVnf_method")).thenReturn("DELETE"); - assertEquals("Delete Success", dme2.send()); + assertNull(dme2.send()); } @Test @@ -136,8 +178,8 @@ public class TestDme2Client { @Test public void testSendtoInstarMaskNotNull() throws Exception { Whitebox.setInternalState(dme2, "mask", "0.0.0.0/1"); - PowerMockito.when(webResource.accept("application/json")).thenReturn(builder); - PowerMockito.when(clientResponse.getEntity(String.class)).thenReturn("Get Success"); + PowerMockito.when(webResource.request("application/json")).thenReturn(builder); + PowerMockito.when(clientResponse.readEntity(String.class)).thenReturn(null); when(properties.getProperty("getIpAddressByVnf_method")).thenReturn("GET"); assertNull(dme2.send()); } @@ -145,8 +187,8 @@ public class TestDme2Client { @Test public void testSendtoInstarIpNotNull() throws Exception { Whitebox.setInternalState(dme2, "ipAddress", "0.0.0.0"); - PowerMockito.when(webResource.accept("application/json")).thenReturn(builder); - PowerMockito.when(clientResponse.getEntity(String.class)).thenReturn("Get Success"); + PowerMockito.when(webResource.request("application/json")).thenReturn(builder); + PowerMockito.when(clientResponse.readEntity(String.class)).thenReturn(null); when(properties.getProperty("getIpAddressByVnf_method")).thenReturn("GET"); assertNull(dme2.send()); } |