X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=POLICY-SDK-APP%2Fsrc%2Ftest%2Fjava%2Forg%2Fonap%2Fpolicy%2Fadmin%2FRESTfulPAPEngineTest.java;h=ec2643fed2f56c86a267061d281fa775e9504663;hb=796004c50a61b8b9d8353f4597a590cf81155ba5;hp=4b307f59489d3a21ce9a2c3ef673007f28735d1f;hpb=c88d4cc4e21e5659f0ab57a38f3ba29ab4b3c044;p=policy%2Fengine.git 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..ec2643fed 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 @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * ONAP Policy Engine * ================================================================================ - * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2018-2020 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Modifications Copyright (C) 2019 Samsung * ================================================================================ @@ -19,35 +19,30 @@ * limitations under the License. * ============LICENSE_END========================================================= */ + package org.onap.policy.admin; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.fail; +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; +import static org.junit.Assert.assertNull; + +import com.att.research.xacml.api.pap.PAPException; +import com.att.research.xacml.api.pap.PDPPolicy; +import com.att.research.xacml.util.XACMLProperties; 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 org.junit.Before; -import org.junit.Rule; +import java.net.URLConnection; +import javax.servlet.http.HttpServletResponse; +import org.junit.AfterClass; +import org.junit.BeforeClass; 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; -@RunWith(PowerMockRunner.class) -@PrepareForTest({URL.class, RESTfulPAPEngine.class}) public class RESTfulPAPEngineTest { - @Rule - public ExpectedException thrown = ExpectedException.none(); private RESTfulPAPEngine engine = null; private String name = "testName"; @@ -61,23 +56,49 @@ public class RESTfulPAPEngineTest { OnapPDP pdp = Mockito.mock(OnapPDP.class); InputStream policy; - @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); + /** + * BeforeClass does some simple code coverage and sets up the + * XACML properties. + */ + @BeforeClass + public static void setUpBeforeClass() { + // + // Test constructor with bad URL + // + assertThatExceptionOfType(PAPException.class).isThrownBy(() -> + new RESTfulPAPEngine(null)); + + XACMLProperties.reloadProperties(); + } + + @AfterClass + public static void tearDownAfterClass() { + XACMLProperties.reloadProperties(); + } + + private void setupConnection(int responseCode, String location) throws Exception { + // Mock connection HttpURLConnection connection = Mockito.mock(HttpURLConnection.class); - Mockito.when(url.openConnection()).thenReturn(connection); - Mockito.when(connection.getResponseCode()).thenReturn(HttpServletResponse.SC_NO_CONTENT); + + Mockito.when(connection.getResponseCode()).thenReturn(responseCode); + Mockito.when(connection.getHeaderField("Location")).thenReturn(location); + + InputStream mockInputStream = Mockito.mock(InputStream.class); + Mockito.when(connection.getInputStream()).thenReturn(mockInputStream); // Set the system property temporarily String systemKey = "xacml.properties"; - String oldProperty = System.getProperty(systemKey); + final String oldProperty = System.getProperty(systemKey); System.setProperty(systemKey, "src/test/resources/xacml.admin.properties"); // 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")); @@ -91,75 +112,89 @@ public class RESTfulPAPEngineTest { } @Test - public void testGroups() throws Exception { + public void testAllTheExceptions() throws Exception { + setupConnection(HttpServletResponse.SC_NO_CONTENT, "localhost:5678"); + engine.setDefaultGroup(group); - assertEquals(engine.getDefaultGroup(), null); + assertNull(engine.getDefaultGroup()); engine.newGroup(name, description); engine.removeGroup(group, newGroup); - assertEquals(engine.getPDPGroup(pdp), null); - assertEquals(engine.getPDPGroup(id), null); - assertEquals(engine.getPDP(id), null); - assertEquals(engine.getStatus(pdp), null); - - thrown.expect(NullPointerException.class); - engine.getOnapPDPGroups(); - fail("Expecting an exception."); - } + assertNull(engine.getPDPGroup(pdp)); + assertNull(engine.getPDPGroup(id)); + assertNull(engine.getPDP(id)); + assertNull(engine.getStatus(pdp)); - @Test - public void testUpdateGroup() throws PAPException { - thrown.expect(PAPException.class); - engine.updateGroup(group); - fail("Expecting an exception."); - } + assertThatExceptionOfType(NullPointerException.class).isThrownBy(() -> + engine.getOnapPDPGroups() + ); - @Test - public void testPDP() throws PAPException { - assertEquals(engine.getGroup(name), null); - engine.movePDP(pdp, newGroup); + assertThatExceptionOfType(PAPException.class).isThrownBy(() -> + engine.updateGroup(group) + ); - thrown.expect(PAPException.class); - engine.newPDP(id, newGroup, name, description, jmxport); - fail("Expecting an exception."); - } + assertThatExceptionOfType(PAPException.class).isThrownBy(() -> + engine.updateGroup(group, "testUserName") + ); - @Test - public void testUpdatePDP() throws PAPException { - thrown.expect(NullPointerException.class); - engine.updatePDP(pdp); - fail("Expecting an exception."); - } + assertNull(engine.getGroup(name)); + engine.movePDP(pdp, newGroup); - @Test - public void testRemovePDP() throws PAPException { - thrown.expect(NullPointerException.class); - engine.removePDP(pdp); - fail("Expecting an exception."); - } + assertThatExceptionOfType(PAPException.class).isThrownBy(() -> + engine.newPDP(id, newGroup, name, description, jmxport) + ); - @Test - public void testValidatePolicy() throws PAPException { - PolicyRestAdapter policyAdapter = new PolicyRestAdapter(); + assertThatExceptionOfType(NullPointerException.class).isThrownBy(() -> + engine.updatePDP(pdp) + ); - thrown.expect(PAPException.class); - engine.validatePolicyRequest(policyAdapter, policyType); - fail("Expecting an exception."); - } + assertThatExceptionOfType(NullPointerException.class).isThrownBy(() -> + engine.removePDP(pdp) + ); - @Test - public void testPublishPolicy() throws PAPException { - thrown.expect(PAPException.class); - engine.publishPolicy(id, name, false, policy, newGroup); - fail("Expecting an exception."); - } + assertThatExceptionOfType(PAPException.class).isThrownBy(() -> + engine.validatePolicyRequest(new PolicyRestAdapter(), policyType) + ); + + assertThatExceptionOfType(PAPException.class).isThrownBy(() -> + engine.publishPolicy(id, name, false, policy, newGroup) + ); - @Test - public void testCopy() throws PAPException { engine.copyFile(id, newGroup, policy); PDPPolicy pdpPolicy = Mockito.mock(PDPPolicy.class); - - thrown.expect(PAPException.class); - engine.copyPolicy(pdpPolicy, newGroup); - fail("Expecting an exception."); + assertThatExceptionOfType(PAPException.class).isThrownBy(() -> + engine.copyPolicy(pdpPolicy, newGroup) + ); + + assertThatExceptionOfType(PAPException.class).isThrownBy(() -> + engine.removePolicy(null, group) + ); + + assertThatExceptionOfType(PAPException.class).isThrownBy(() -> + engine.copyPolicy(null, null) + ); + + // + // Change the mockito to take a different path + // + assertThatExceptionOfType(PAPException.class).isThrownBy(() -> + setupConnection(HttpServletResponse.SC_FOUND, "localhost:5678") + ); + + assertThatExceptionOfType(PAPException.class).isThrownBy(() -> + setupConnection(200, "localhost:5678") + ); + + assertThatExceptionOfType(PAPException.class).isThrownBy(() -> + setupConnection(350, "localhost:5678") + ); + + assertThatExceptionOfType(PAPException.class).isThrownBy(() -> + setupConnection(500, "localhost:5678") + ); + + assertThatExceptionOfType(PAPException.class).isThrownBy(() -> + setupConnection(350, null) + ); } + }