diff options
Diffstat (limited to 'POLICY-SDK-APP/src/test')
3 files changed, 148 insertions, 156 deletions
diff --git a/POLICY-SDK-APP/src/test/java/org/onap/policy/admin/CheckPDPTest.java b/POLICY-SDK-APP/src/test/java/org/onap/policy/admin/CheckPDPTest.java deleted file mode 100644 index 358b69f1f..000000000 --- a/POLICY-SDK-APP/src/test/java/org/onap/policy/admin/CheckPDPTest.java +++ /dev/null @@ -1,88 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * ONAP Policy Engine - * ================================================================================ - * Copyright (C) 2017, 2019 AT&T Intellectual Property. All rights reserved. - * ================================================================================ - * Modifications Copyright (C) 2019 Samsung - * ================================================================================ - * 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 - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * ============LICENSE_END========================================================= - */ - -package org.onap.policy.admin; - -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; - -import java.io.File; - -import org.junit.FixMethodOrder; -import org.junit.Test; -import org.junit.runners.MethodSorters; - -@FixMethodOrder(MethodSorters.NAME_ASCENDING) -public class CheckPDPTest { - - @Test - public final void test1NoPropertySet() { - try { - System.clearProperty("xacml.rest.pdp.idfile"); - assertFalse(CheckPDP.validateID("http://localhost:8082/pdp/")); - - System.setProperty("xacml.rest.pdp.idfile", - new File(".").getCanonicalPath() + File.separator + "src" + File.separator + "test" + File.separator - + "resources" + File.separator + "idonotexist.properties"); - assertFalse(CheckPDP.validateID("http://localhost:8082/pdp/")); - - System.setProperty("xacml.rest.pdp.idfile", - new File(".").getCanonicalPath() + File.separator + "src" + File.separator + "test" + File.separator - + "resources" + File.separator + "doesnothaveproperties.atall"); - assertFalse(CheckPDP.validateID("http://localhost:8082/pdp/")); - - System.setProperty("xacml.rest.pdp.idfile", new File(".").getCanonicalPath() + File.separator + "src" - + File.separator + "test" + File.separator + "resources" + File.separator + "testbad.properties"); - assertFalse(CheckPDP.validateID("http://localhost:8082/pdp/")); - - System.setProperty("xacml.rest.pdp.idfile", new File(".").getCanonicalPath() + File.separator + "src" - + File.separator + "test" + File.separator + "resources" + File.separator + "empty.properties"); - assertFalse(CheckPDP.validateID("http://localhost:8082/pdp/")); - - System.setProperty("xacml.rest.pdp.idfile", - new File(".").getCanonicalPath() + File.separator + "src" + File.separator + "test" + File.separator - + "resources" + File.separator + "testnotenoughvalues.properties"); - assertFalse(CheckPDP.validateID("http://localhost:8082/pdp/")); - - assertNull(CheckPDP.getPdpMap()); - assertNull(CheckPDP.getEncoding("http://localhost:8082/pdp/")); - - } catch (Exception e) { - fail("Error occured in CheckPDP test"); - } - } - - @Test - public final void test2CheckPDP() { - try { - System.setProperty("xacml.rest.pdp.idfile", new File(".").getCanonicalPath() + File.separator + "src" - + File.separator + "test" + File.separator + "resources" + File.separator + "test.properties"); - assertTrue(CheckPDP.validateID("http://localhost:8082/pdp/")); - assertTrue(CheckPDP.getPdpMap().containsKey("http://localhost:8082/pdp/")); - assertTrue(CheckPDP.getEncoding("http://localhost:8082/pdp/").equals("dGVzdHBkcDphbHBoYTQ1Ng==")); - } catch (Exception e) { - fail("Error occured in CheckPDP test"); - } - } -} diff --git a/POLICY-SDK-APP/src/test/java/org/onap/policy/admin/CheckPdpPropertiesTest.java b/POLICY-SDK-APP/src/test/java/org/onap/policy/admin/CheckPdpPropertiesTest.java new file mode 100644 index 000000000..afb6fc8b0 --- /dev/null +++ b/POLICY-SDK-APP/src/test/java/org/onap/policy/admin/CheckPdpPropertiesTest.java @@ -0,0 +1,82 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP Policy Engine + * ================================================================================ + * Copyright (C) 2017, 2019 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Modifications Copyright (C) 2019 Samsung + * ================================================================================ + * 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.admin; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; + +import com.att.research.xacml.util.XACMLProperties; +import java.io.File; +import java.io.IOException; +import org.junit.FixMethodOrder; +import org.junit.Test; +import org.junit.runners.MethodSorters; + +@FixMethodOrder(MethodSorters.NAME_ASCENDING) +public class CheckPdpPropertiesTest { + + @Test + public final void test1NoPropertySet() throws IOException { + System.setProperty(XACMLProperties.XACML_PROPERTIES_NAME, "noexistenfile.properties"); + assertFalse(CheckPdpProperties.validateId("http://localhost:8082/pdp/")); + + System.clearProperty(XACMLProperties.XACML_PROPERTIES_NAME); + System.clearProperty("xacml.rest.pdp.idfile"); + assertFalse(CheckPdpProperties.validateId("http://localhost:8082/pdp/")); + + System.setProperty("xacml.rest.pdp.idfile", new File(".").getCanonicalPath() + File.separator + "src" + + File.separator + "test" + File.separator + "resources" + File.separator + "idonotexist.properties"); + assertFalse(CheckPdpProperties.validateId("http://localhost:8082/pdp/")); + + System.setProperty("xacml.rest.pdp.idfile", + new File(".").getCanonicalPath() + File.separator + "src" + File.separator + "test" + File.separator + + "resources" + File.separator + "doesnothaveproperties.atall"); + assertFalse(CheckPdpProperties.validateId("http://localhost:8082/pdp/")); + + System.setProperty("xacml.rest.pdp.idfile", new File(".").getCanonicalPath() + File.separator + "src" + + File.separator + "test" + File.separator + "resources" + File.separator + "testbad.properties"); + assertFalse(CheckPdpProperties.validateId("http://localhost:8082/pdp/")); + + System.setProperty("xacml.rest.pdp.idfile", new File(".").getCanonicalPath() + File.separator + "src" + + File.separator + "test" + File.separator + "resources" + File.separator + "empty.properties"); + assertFalse(CheckPdpProperties.validateId("http://localhost:8082/pdp/")); + + System.setProperty("xacml.rest.pdp.idfile", + new File(".").getCanonicalPath() + File.separator + "src" + File.separator + "test" + File.separator + + "resources" + File.separator + "testnotenoughvalues.properties"); + assertFalse(CheckPdpProperties.validateId("http://localhost:8082/pdp/")); + + assertNull(CheckPdpProperties.getPdpMap()); + assertNull(CheckPdpProperties.getEncoding("http://localhost:8082/pdp/")); + } + + @Test + public final void test2CheckPdp() throws IOException { + System.setProperty("xacml.rest.pdp.idfile", new File(".").getCanonicalPath() + File.separator + "src" + + File.separator + "test" + File.separator + "resources" + File.separator + "test.properties"); + assertTrue(CheckPdpProperties.validateId("http://localhost:8082/pdp/")); + assertTrue(CheckPdpProperties.getPdpMap().containsKey("http://localhost:8082/pdp/")); + assertTrue(CheckPdpProperties.getEncoding("http://localhost:8082/pdp/").equals("dGVzdHBkcDphbHBoYTQ1Ng==")); + } +} 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 e28ca1281..993c322d5 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 @@ -22,8 +22,8 @@ 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; @@ -38,19 +38,14 @@ 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.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; public class RESTfulPAPEngineTest { - @Rule - public ExpectedException thrown = ExpectedException.none(); private RESTfulPAPEngine engine = null; private String name = "testName"; @@ -64,8 +59,18 @@ public class RESTfulPAPEngineTest { OnapPDP pdp = Mockito.mock(OnapPDP.class); InputStream policy; + /** + * 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(); } @@ -74,22 +79,22 @@ public class RESTfulPAPEngineTest { XACMLProperties.reloadProperties(); } - @Before - public void runConstructor() throws Exception { + private void setupConnection(int responseCode) throws Exception { // Mock connection HttpURLConnection connection = Mockito.mock(HttpURLConnection.class); - Mockito.when(connection.getResponseCode()).thenReturn(HttpServletResponse.SC_NO_CONTENT); + Mockito.when(connection.getResponseCode()).thenReturn(responseCode); + Mockito.when(connection.getHeaderField("Location")).thenReturn("localhost:5678"); // 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) { @Override - protected URLConnection makeConnection(String fullURL) throws IOException { + protected URLConnection makeConnection(String fullUrl) throws IOException { return connection; } }; @@ -106,75 +111,68 @@ public class RESTfulPAPEngineTest { } @Test - public void testGroups() throws Exception { + public void testAllTheExceptions() throws Exception { + setupConnection(HttpServletResponse.SC_NO_CONTENT); + 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); + assertThatExceptionOfType(PAPException.class).isThrownBy(() -> + engine.updateGroup(group) + ); + + assertNull(engine.getGroup(name)); engine.movePDP(pdp, newGroup); - thrown.expect(PAPException.class); - engine.newPDP(id, newGroup, name, description, jmxport); - fail("Expecting an exception."); - } + assertThatExceptionOfType(PAPException.class).isThrownBy(() -> + engine.newPDP(id, newGroup, name, description, jmxport) + ); - @Test - public void testUpdatePDP() throws PAPException { - thrown.expect(NullPointerException.class); - engine.updatePDP(pdp); - fail("Expecting an exception."); - } + assertThatExceptionOfType(NullPointerException.class).isThrownBy(() -> + engine.updatePDP(pdp) + ); - @Test - public void testRemovePDP() throws PAPException { - thrown.expect(NullPointerException.class); - engine.removePDP(pdp); - fail("Expecting an exception."); - } + assertThatExceptionOfType(NullPointerException.class).isThrownBy(() -> + engine.removePDP(pdp) + ); - @Test - public void testValidatePolicy() throws PAPException { - PolicyRestAdapter policyAdapter = new PolicyRestAdapter(); + assertThatExceptionOfType(PAPException.class).isThrownBy(() -> + engine.validatePolicyRequest(new PolicyRestAdapter(), policyType) + ); - thrown.expect(PAPException.class); - engine.validatePolicyRequest(policyAdapter, policyType); - fail("Expecting an exception."); - } + assertThatExceptionOfType(PAPException.class).isThrownBy(() -> + engine.publishPolicy(id, name, false, policy, newGroup) + ); - @Test - public void testPublishPolicy() throws PAPException { - thrown.expect(PAPException.class); - engine.publishPolicy(id, name, false, policy, newGroup); - fail("Expecting an exception."); - } - - @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) + ); } } |