diff options
author | Lathishbabu Ganesan <lathishbabu.ganesan@ericsson.com> | 2019-02-06 10:38:14 -0500 |
---|---|---|
committer | Takamune Cho <takamune.cho@att.com> | 2019-02-14 17:05:58 +0000 |
commit | 932c4a380114ef29e83c2b11fad92da12bff6390 (patch) | |
tree | 6f2a1bf74353cf590b110a47fc05154d2fd8b7f2 /appc-outbound | |
parent | 19bb17c4b16ee955cdec6db9ddc2af25845df062 (diff) |
Added test cases for DME2 Client
Increased the coverage from 0% to 90%
Issue-ID: APPC-1395
Change-Id: I9651ab4326e500559e5bd9b99abdff84d93cfa1d
Signed-off-by: Lathishbabu Ganesan <lathishbabu.ganesan@ericsson.com>
Diffstat (limited to 'appc-outbound')
3 files changed, 142 insertions, 22 deletions
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 b6c35e505..51bd9c1e3 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 @@ -6,6 +6,8 @@ * ================================================================================ * Copyright (C) 2017 Amdocs * ============================================================================= + * Modifications Copyright (C) 2019 Ericsson + * ============================================================================= * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -23,15 +25,6 @@ package org.onap.appc.instar.dme2client; -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 java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.net.URI; @@ -45,6 +38,14 @@ import javax.ws.rs.core.MediaType; import org.apache.commons.io.IOUtils; 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; public class Dme2Client { @@ -64,12 +65,12 @@ public class Dme2Client { this.ipAddress = data.get("ipAddress"); this.mask = data.get("mask"); } - String propDir = System.getenv(SDNC_CONFIG_DIR_VAR); + String propDir = InstarClientConstant.getEnvironmentVariable(SDNC_CONFIG_DIR_VAR); if (propDir == null) { throw new IOException("Cannot find Property file -" + SDNC_CONFIG_DIR_VAR); } String propFile = propDir + InstarClientConstant.OUTBOUND_PROPERTIES; - InputStream propStream = new FileInputStream(propFile); + InputStream propStream = InstarClientConstant.getInputStream(propFile); try { properties.load(propStream); } catch (Exception e) { @@ -117,7 +118,6 @@ public class Dme2Client { .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"); diff --git a/appc-outbound/appc-network-inventory-client/provider/src/main/java/org/onap/appc/instar/utils/InstarClientConstant.java b/appc-outbound/appc-network-inventory-client/provider/src/main/java/org/onap/appc/instar/utils/InstarClientConstant.java index 713d2ae47..da3672843 100644 --- a/appc-outbound/appc-network-inventory-client/provider/src/main/java/org/onap/appc/instar/utils/InstarClientConstant.java +++ b/appc-outbound/appc-network-inventory-client/provider/src/main/java/org/onap/appc/instar/utils/InstarClientConstant.java @@ -6,6 +6,8 @@ * ================================================================================ * Copyright (C) 2017 Amdocs * ============================================================================= + * Modifications Copyright (C) 2019 Ericsson + * ============================================================================= * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -23,6 +25,10 @@ package org.onap.appc.instar.utils; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.InputStream; + public class InstarClientConstant { public static final String INPUT_PARAM_RESPONSE_PRIFIX = "responsePrefix"; @@ -74,5 +80,13 @@ public class InstarClientConstant { public static final String OUTBOUND_PROPERTIES = "/outbound.properties"; private InstarClientConstant() {} + + public static String getEnvironmentVariable(String env) { + return System.getenv(env); + } + + public static InputStream getInputStream(String file) throws FileNotFoundException { + return new FileInputStream(file); + } } 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 9a3e855ab..7e2a41820 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 @@ -6,6 +6,8 @@ * ================================================================================ * Copyright (C) 2017 Amdocs * ============================================================================= + * Modifications Copyright (C) 2019 Ericsson + * ============================================================================= * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -23,25 +25,129 @@ package org.onap.appc.instar.node; -import static org.junit.Assert.assertTrue; - +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 java.io.InputStream; +import java.net.URI; +import java.security.NoSuchAlgorithmException; import java.util.HashMap; +import java.util.Properties; +import javax.net.ssl.SSLContext; +import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; +import org.mockito.Mockito; import org.onap.appc.instar.dme2client.Dme2Client; +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 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; @RunWith(PowerMockRunner.class) +@PrepareForTest({InstarClientConstant.class, SSLContext.class, Client.class}) public class TestDme2Client { - @Test(expected=Exception.class) - public void testSendtoInstar() throws Exception { + private Dme2Client dme2; + private InputStream inputStream; + private SSLContext sslContext; + private Properties properties; + private Client client; + private WebResource webResource; + private Builder builder; + private ClientResponse clientResponse; + + @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); + HashMap<String, String> data = new HashMap<String, String>(); + data.put("subtext", "value"); + PowerMockito.mockStatic(InstarClientConstant.class); + PowerMockito.mockStatic(SSLContext.class); + PowerMockito.mockStatic(Client.class); + PowerMockito.when(InstarClientConstant.getEnvironmentVariable("SDNC_CONFIG_DIR")) + .thenReturn("test"); + PowerMockito.when(InstarClientConstant.getInputStream("test/outbound.properties")) + .thenReturn(inputStream); + 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); + 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"); + } + + @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"); + 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"); + 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"); + 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"); + when(properties.getProperty("getIpAddressByVnf_method")).thenReturn("DELETE"); + assertEquals("Delete Success", dme2.send()); + } + + @Test + public void testSendtoInstarException() throws Exception { + PowerMockito.when(SSLContext.getInstance("SSL")).thenThrow(new NoSuchAlgorithmException()); + when(properties.getProperty("getIpAddressByVnf_method")).thenReturn("DELETE"); + assertNull(dme2.send()); + } + + @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"); + when(properties.getProperty("getIpAddressByVnf_method")).thenReturn("GET"); + assertNull(dme2.send()); + } - HashMap<String,String> data = new HashMap<String,String>(); - data.put("subtext","value"); - PowerMockito.mockStatic(System.class); - PowerMockito.when((System.getenv("test"))).thenReturn("test"); - Dme2Client dme2 = new Dme2Client("opt","subtext",data); - } + @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"); + when(properties.getProperty("getIpAddressByVnf_method")).thenReturn("GET"); + assertNull(dme2.send()); + } } |