diff options
author | Jim Hahn <jrh3@att.com> | 2019-08-22 14:21:14 -0400 |
---|---|---|
committer | Jim Hahn <jrh3@att.com> | 2019-08-22 17:07:37 -0400 |
commit | 962dd9714c6d35cacdec0ee8e601431aa349001a (patch) | |
tree | 03ce05c9c64680f0c44fa22734294292a1a12064 /POLICY-SDK-APP/src | |
parent | e78f07ad32ffb52ed5d3e02295ed5282841870f3 (diff) |
Remove powermock to increase coverage
Removed powermock from the junit tests of these classes to increase
reported sonar coverage:
RESTfulPAPEngine
BrmsGateway
Typically added override methods to the classes under test, to replace
the overrides originally provided by powermock.
Also needed to add code to RESTfulPAPEngineTest to clear the XACML
properties before the test ran. Modified to use the existing
XACMLProperties.reloadProperties() method.
Also modified PolicyNotificationMailTest to use a host/port that
has no listener so that the test runs faster.
Fixed some newly introduced sonar issues.
Change-Id: I65e36b01e9506987032eb21baac808ed3dfd4f47
Issue-ID: POLICY-1937
Signed-off-by: Jim Hahn <jrh3@att.com>
Diffstat (limited to 'POLICY-SDK-APP/src')
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")); |