diff options
author | 2019-08-23 12:55:45 +0000 | |
---|---|---|
committer | 2019-08-23 12:55:45 +0000 | |
commit | b2341e2ee94605af08728a38f835e9545ab60cac (patch) | |
tree | 8606acc976901748f310d1c8c2c8b98ab2cdc9a2 /POLICY-SDK-APP | |
parent | ca8e0c7e1182faf67a1049d521e3d5ac2c899cff (diff) | |
parent | 962dd9714c6d35cacdec0ee8e601431aa349001a (diff) |
Merge "Remove powermock to increase coverage"
Diffstat (limited to 'POLICY-SDK-APP')
3 files changed, 42 insertions, 18 deletions
diff --git a/POLICY-SDK-APP/src/main/java/org/onap/policy/admin/RESTfulPAPEngine.java b/POLICY-SDK-APP/src/main/java/org/onap/policy/admin/RESTfulPAPEngine.java index a500c1dbe..fc47179c1 100644 --- a/POLICY-SDK-APP/src/main/java/org/onap/policy/admin/RESTfulPAPEngine.java +++ b/POLICY-SDK-APP/src/main/java/org/onap/policy/admin/RESTfulPAPEngine.java @@ -36,7 +36,9 @@ import java.io.InputStream; import java.io.OutputStream; import java.io.UnsupportedEncodingException; import java.net.HttpURLConnection; +import java.net.MalformedURLException; import java.net.URL; +import java.net.URLConnection; import java.net.URLEncoder; import java.nio.charset.StandardCharsets; import java.util.Arrays; @@ -379,12 +381,10 @@ public class RESTfulPAPEngine extends StdPDPItemSetChangeNotifier implements PAP } } - URL url = new URL(fullURL); - // // Open up the connection // - connection = (HttpURLConnection) url.openConnection(); + connection = (HttpURLConnection) makeConnection(fullURL); // // Setup our method and headers // @@ -527,4 +527,10 @@ public class RESTfulPAPEngine extends StdPDPItemSetChangeNotifier implements PAP LOGGER.info("JSON response from PAP: " + json); return json; } + + // these may be overridden by junit tests + + protected URLConnection makeConnection(String fullURL) throws IOException { + return new URL(fullURL).openConnection(); + } } diff --git a/POLICY-SDK-APP/src/test/java/org/onap/policy/admin/PolicyNotificationMailTest.java b/POLICY-SDK-APP/src/test/java/org/onap/policy/admin/PolicyNotificationMailTest.java index 95296f4b0..e270583bb 100644 --- a/POLICY-SDK-APP/src/test/java/org/onap/policy/admin/PolicyNotificationMailTest.java +++ b/POLICY-SDK-APP/src/test/java/org/onap/policy/admin/PolicyNotificationMailTest.java @@ -29,6 +29,7 @@ import java.util.List; import org.junit.Before; import org.junit.Test; import static org.mockito.Mockito.when; +import org.onap.policy.common.utils.network.NetworkUtil; import org.onap.policy.controller.PolicyController; import org.onap.policy.rest.dao.CommonClassDao; import org.onap.policy.rest.jpa.PolicyVersion; @@ -46,11 +47,17 @@ public class PolicyNotificationMailTest { PolicyController.setjUnit(true); PolicyController.setSmtpApplicationName("Test"); PolicyController.setSmtpEmailExtension("test.com"); - PolicyController.setSmtpHost("test"); - PolicyController.setSmtpPort("23"); + PolicyController.setSmtpHost("localhost"); PolicyController.setSmtpPassword("test"); PolicyController.setSmtpUsername("test"); + /* + * Allocate a port to which the mail sender should connect, but don't actually + * start a listener on the port so that connection attempts will be immediately + * rejected. + */ + PolicyController.setSmtpPort(String.valueOf(NetworkUtil.allocPort())); + version = new PolicyVersion(); version.setPolicyName("com/Config_Test"); version.setModifiedBy("xyz"); diff --git a/POLICY-SDK-APP/src/test/java/org/onap/policy/admin/RESTfulPAPEngineTest.java b/POLICY-SDK-APP/src/test/java/org/onap/policy/admin/RESTfulPAPEngineTest.java index 4b307f594..e8434d46d 100644 --- a/POLICY-SDK-APP/src/test/java/org/onap/policy/admin/RESTfulPAPEngineTest.java +++ b/POLICY-SDK-APP/src/test/java/org/onap/policy/admin/RESTfulPAPEngineTest.java @@ -23,28 +23,27 @@ package org.onap.policy.admin; import static org.junit.Assert.assertEquals; import static org.junit.Assert.fail; + import java.io.ByteArrayInputStream; +import java.io.IOException; import java.io.InputStream; -import java.net.URL; -import javax.servlet.http.HttpServletResponse; import java.net.HttpURLConnection; +import java.net.URLConnection; +import javax.servlet.http.HttpServletResponse; +import org.junit.AfterClass; import org.junit.Before; +import org.junit.BeforeClass; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; -import org.junit.runner.RunWith; import org.mockito.Mockito; import org.onap.policy.rest.adapter.PolicyRestAdapter; import org.onap.policy.xacml.api.pap.OnapPDP; import org.onap.policy.xacml.api.pap.OnapPDPGroup; -import org.powermock.api.mockito.PowerMockito; -import org.powermock.core.classloader.annotations.PrepareForTest; -import org.powermock.modules.junit4.PowerMockRunner; import com.att.research.xacml.api.pap.PAPException; import com.att.research.xacml.api.pap.PDPPolicy; +import com.att.research.xacml.util.XACMLProperties; -@RunWith(PowerMockRunner.class) -@PrepareForTest({URL.class, RESTfulPAPEngine.class}) public class RESTfulPAPEngineTest { @Rule public ExpectedException thrown = ExpectedException.none(); @@ -61,13 +60,20 @@ public class RESTfulPAPEngineTest { OnapPDP pdp = Mockito.mock(OnapPDP.class); InputStream policy; + @BeforeClass + public static void setUpBeforeClass() { + XACMLProperties.reloadProperties(); + } + + @AfterClass + public static void tearDownAfterClass() { + XACMLProperties.reloadProperties(); + } + @Before public void runConstructor() throws Exception { - // Mock url and connection - URL url = PowerMockito.mock(URL.class); - PowerMockito.whenNew(URL.class).withArguments(Mockito.any()).thenReturn(url); + // Mock connection HttpURLConnection connection = Mockito.mock(HttpURLConnection.class); - Mockito.when(url.openConnection()).thenReturn(connection); Mockito.when(connection.getResponseCode()).thenReturn(HttpServletResponse.SC_NO_CONTENT); // Set the system property temporarily @@ -77,7 +83,12 @@ public class RESTfulPAPEngineTest { // Test constructor String urlName = "localhost:1234"; - engine = new RESTfulPAPEngine(urlName); + engine = new RESTfulPAPEngine(urlName) { + @Override + protected URLConnection makeConnection(String fullURL) throws IOException { + return connection; + } + }; // Initialize policy policy = new ByteArrayInputStream(policyContent.getBytes("UTF-8")); |