From 3387ae275aeb64113d48cd67b44db217b67e598e Mon Sep 17 00:00:00 2001 From: bobbymander Date: Fri, 30 Mar 2018 07:45:12 -0400 Subject: JUnit additions for XACML,PAP-REST Issue-ID: POLICY-605 Change-Id: I4b55a85eacf989ddb55743300e7f75e4b72882d0 Signed-off-by: bobbymander --- .../xacml/test/std/pap/StdPDPPIPConfigTest.java | 107 ++++++++++ .../xacml/test/std/pap/StdPDPPolicyTest.java | 143 ++++++++++++++ .../onap/policy/xacml/test/std/pap/StdPDPTest.java | 216 ++++++++++++++------- 3 files changed, 391 insertions(+), 75 deletions(-) create mode 100644 ONAP-XACML/src/test/java/org/onap/policy/xacml/test/std/pap/StdPDPPIPConfigTest.java create mode 100644 ONAP-XACML/src/test/java/org/onap/policy/xacml/test/std/pap/StdPDPPolicyTest.java (limited to 'ONAP-XACML') diff --git a/ONAP-XACML/src/test/java/org/onap/policy/xacml/test/std/pap/StdPDPPIPConfigTest.java b/ONAP-XACML/src/test/java/org/onap/policy/xacml/test/std/pap/StdPDPPIPConfigTest.java new file mode 100644 index 000000000..05b8ae7cc --- /dev/null +++ b/ONAP-XACML/src/test/java/org/onap/policy/xacml/test/std/pap/StdPDPPIPConfigTest.java @@ -0,0 +1,107 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP-XACML + * ================================================================================ + * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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.xacml.test.std.pap; + +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.CoreMatchers.not; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertThat; +import java.util.HashMap; +import java.util.Map; +import java.util.Properties; +import org.junit.Test; +import org.onap.policy.xacml.std.pap.StdPDPPIPConfig; + +public class StdPDPPIPConfigTest { + @Test + public void testConfig() { + // Setup test data + String id = "testID"; + String value = "testVal"; + Properties props = new Properties(); + props.setProperty(id + ".classname", value); + Map map = new HashMap(); + map.put(id, value); + + // Test constructors + StdPDPPIPConfig config = new StdPDPPIPConfig(); + assertNotNull(config); + StdPDPPIPConfig config2 = new StdPDPPIPConfig(id); + assertNotNull(config2); + StdPDPPIPConfig config3 = new StdPDPPIPConfig(id, value, value); + assertNotNull(config3); + StdPDPPIPConfig config4 = new StdPDPPIPConfig(id, props); + assertNotNull(config4); + StdPDPPIPConfig config5 = new StdPDPPIPConfig(id, props); + assertNotNull(config5); + + // Test set and get + config.setId(value); + assertEquals(value, config.getId()); + config.setName(value); + assertEquals(value, config.getName()); + config.setDescription(value); + assertEquals(value, config.getDescription()); + config.setClassname(value); + assertEquals(value, config.getClassname()); + config.setValues(map); + assertEquals(map, config.getConfiguration()); + assertEquals(true, config.isConfigured()); + config.setConfig(map); + assertEquals(map, config.getConfig()); + + // Test equals combinations + assertEquals(true, config4.equals(config5)); + assertEquals(true, config4.equals(config4)); + assertEquals(false, config4.equals(null)); + assertEquals(false, config4.equals(value)); + config4.setClassname(null); + config5.setClassname(value); + assertEquals(false, config4.equals(config5)); + config4.setClassname(id); + assertEquals(false, config4.equals(config5)); + config4.setClassname(value); + config5.setConfig(map); + assertEquals(false, config4.equals(config5)); + config4.setConfig(null); + assertEquals(false, config4.equals(config5)); + config4.setConfig(map); + config5.setDescription(value); + assertEquals(false, config4.equals(config5)); + config4.setDescription(id); + assertEquals(false, config4.equals(config5)); + config4.setDescription(value); + config4.setId(null); + assertEquals(false, config4.equals(config5)); + config4.setId(value); + assertEquals(false, config4.equals(config5)); + config4.setId(id); + config5.setName(value); + assertEquals(false, config4.equals(config5)); + config4.setName(id); + assertEquals(false, config4.equals(config5)); + assertThat(config.hashCode(), is(not(0))); + + // Test toString + assertThat(config.toString().length(), is(not(0))); + } +} diff --git a/ONAP-XACML/src/test/java/org/onap/policy/xacml/test/std/pap/StdPDPPolicyTest.java b/ONAP-XACML/src/test/java/org/onap/policy/xacml/test/std/pap/StdPDPPolicyTest.java new file mode 100644 index 000000000..ed7db3b8a --- /dev/null +++ b/ONAP-XACML/src/test/java/org/onap/policy/xacml/test/std/pap/StdPDPPolicyTest.java @@ -0,0 +1,143 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP-XACML + * ================================================================================ + * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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.xacml.test.std.pap; + +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.CoreMatchers.not; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertThat; +import java.io.IOException; +import java.net.ConnectException; +import java.net.URI; +import java.net.URISyntaxException; +import java.util.Properties; +import org.junit.Test; +import org.onap.policy.xacml.std.pap.StdPDPPolicy; +import com.att.research.xacml.api.pap.PAPException; + + +public class StdPDPPolicyTest { + @Test + public void testPolicy() throws URISyntaxException, IOException, PAPException { + // Set up test data + String value = "testVal"; + URI uri = new URI("http://localhost/"); + int[] array = {1, 1}; + + // Test constructors + StdPDPPolicy policy = new StdPDPPolicy(value, true); + assertNotNull(policy); + StdPDPPolicy policy2 = new StdPDPPolicy(value, true, value); + assertNotNull(policy2); + StdPDPPolicy policy4 = new StdPDPPolicy(); + assertNotNull(policy4); + StdPDPPolicy policy5 = new StdPDPPolicy(value, true, value, uri, false, value, value, "1"); + assertNotNull(policy5); + StdPDPPolicy policy6 = new StdPDPPolicy(value, true, value, uri, false); + assertNotNull(policy6); + StdPDPPolicy policy8 = new StdPDPPolicy(value, true, value, uri, false); + assertNotNull(policy8); + + // Test set and get + policy.setId(value); + assertEquals(value, policy.getId()); + policy.setName(value); + assertEquals(value, policy.getName()); + policy.setPolicyId(value); + assertEquals(value, policy.getPolicyId()); + policy.setDescription(value); + assertEquals(value, policy.getDescription()); + policy.setVersion("1"); + assertEquals("1", policy.getVersion()); + assertEquals(1, policy.getVersionInts()[0]); + assertEquals(true, policy.isRoot()); + policy.setValid(true); + assertEquals(true, policy.isValid()); + assertEquals(uri, policy5.getLocation()); + policy.setRoot(false); + assertEquals(false, policy.isRoot()); + policy.setLocation(uri); + assertEquals(uri, policy.getLocation()); + + // Test equals combinations + assertThat(policy.hashCode(), is(not(0))); + assertEquals(true, policy.equals(policy)); + assertEquals(false, policy.equals(null)); + assertEquals(false, policy.equals(value)); + assertEquals(true, policy6.equals(policy8)); + policy6.setId("1"); + assertEquals(false, policy6.equals(policy8)); + policy6.setId(null); + assertEquals(false, policy6.equals(policy8)); + policy8.setId(null); + assertEquals(true, policy6.equals(policy8)); + policy8.setPolicyId(value); + assertEquals(false, policy6.equals(policy8)); + policy6.setPolicyId("1"); + policy8.setPolicyId("2"); + assertEquals(false, policy6.equals(policy8)); + policy8.setPolicyId("1"); + policy6.setVersion("1"); + policy8.setVersion("2"); + assertEquals(false, policy6.equals(policy8)); + + // Test toString + assertThat(policy.toString().length(), is(not(0))); + assertEquals("1.1", StdPDPPolicy.versionArrayToString(array)); + assertEquals("", StdPDPPolicy.versionArrayToString(null)); + assertEquals(0, StdPDPPolicy.versionStringToArray(null).length); + } + + @Test(expected = ConnectException.class) + public void negTestStream() throws URISyntaxException, IOException, PAPException { + // Set up test data + String value = "testVal"; + URI uri = new URI("http://localhost/"); + StdPDPPolicy policy = new StdPDPPolicy(value, true, value, uri, false, value, value, "1"); + + // Negative test stream + policy.getStream(); + } + + @Test(expected = ConnectException.class) + public void negTestConstructor1() throws URISyntaxException, IOException { + // Set up test data + String value = "testVal"; + URI uri = new URI("http://localhost/"); + + // Test constructor + StdPDPPolicy policy = new StdPDPPolicy(value, true, value, uri); + assertNotNull(policy); + } + + @Test(expected = ConnectException.class) + public void negTestConstructor2() throws URISyntaxException, IOException { + // Set up test data + String value = "testVal"; + URI uri = new URI("http://localhost/"); + Properties props = new Properties(); + + // Test constructor + StdPDPPolicy policy = new StdPDPPolicy(value, true, uri, props); + assertNotNull(policy); + } +} diff --git a/ONAP-XACML/src/test/java/org/onap/policy/xacml/test/std/pap/StdPDPTest.java b/ONAP-XACML/src/test/java/org/onap/policy/xacml/test/std/pap/StdPDPTest.java index ab1fb88e7..e62f1fdb1 100644 --- a/ONAP-XACML/src/test/java/org/onap/policy/xacml/test/std/pap/StdPDPTest.java +++ b/ONAP-XACML/src/test/java/org/onap/policy/xacml/test/std/pap/StdPDPTest.java @@ -1,9 +1,8 @@ -package org.onap.policy.xacml.test.std.pap; /*- * ============LICENSE_START======================================================= * ONAP-XACML * ================================================================================ - * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -18,88 +17,155 @@ package org.onap.policy.xacml.test.std.pap; * limitations under the License. * ============LICENSE_END========================================================= */ -import static org.junit.Assert.assertTrue; +package org.onap.policy.xacml.test.std.pap; + +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.CoreMatchers.not; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertThat; +import static org.junit.Assert.assertTrue; +import java.util.HashSet; +import java.util.Properties; +import java.util.Set; import org.junit.Before; import org.junit.Test; import org.onap.policy.common.logging.flexlogger.FlexLogger; import org.onap.policy.common.logging.flexlogger.Logger; import org.onap.policy.xacml.std.pap.StdPDP; import org.onap.policy.xacml.std.pap.StdPDPStatus; +import com.att.research.xacml.api.pap.PDPPIPConfig; +import com.att.research.xacml.api.pap.PDPPolicy; public class StdPDPTest { - private static Logger logger = FlexLogger.getLogger(StdPDPTest.class); - - private StdPDP stdPDP; - - @Before - public void setUp(){ - - try { - stdPDP = new StdPDP(); - } catch (Exception e) { - logger.error(e); - } - } - - @Test - public void testGetId() { - try { - stdPDP.setId("testId"); - assertTrue(stdPDP.getId() != null); - } catch (Exception e) { - logger.error(e); - } - } - - @Test - public void testGetName() { - try { - stdPDP.setName("abc"); - assertTrue(stdPDP.getName() != null); - } catch (Exception e) { - logger.error(e); - } - } - - @Test - public void testGetDescription() { - try { - stdPDP.setDescription("somee here"); - assertTrue(stdPDP.getDescription() != null); - } catch (Exception e) { - logger.error(e); - } - } - - @Test - public void testGetStatus() { - try { - stdPDP.setStatus(new StdPDPStatus()); - assertTrue(stdPDP.getStatus() != null); - } catch (Exception e) { - logger.error(e); - } - } - - @Test - public void testGetPipConfigs() { - try { - assertTrue(stdPDP.getPipConfigs() != null); - } catch (Exception e) { - logger.error(e); - } - } - - @Test - public void testGetJmxPort() { - try { - stdPDP.setJmxPort(123); - assertTrue(stdPDP.getJmxPort() != null); - } catch (Exception e) { - logger.error(e); - } - } + private static Logger logger = FlexLogger.getLogger(StdPDPTest.class); + + private StdPDP stdPDP; + + @Before + public void setUp() { + + try { + stdPDP = new StdPDP(); + } catch (Exception e) { + logger.error(e); + } + } + + @Test + public void testGetId() { + try { + stdPDP.setId("testId"); + assertTrue(stdPDP.getId() != null); + } catch (Exception e) { + logger.error(e); + } + } + + @Test + public void testGetName() { + try { + stdPDP.setName("abc"); + assertTrue(stdPDP.getName() != null); + } catch (Exception e) { + logger.error(e); + } + } + + @Test + public void testGetDescription() { + try { + stdPDP.setDescription("somee here"); + assertTrue(stdPDP.getDescription() != null); + } catch (Exception e) { + logger.error(e); + } + } + + @Test + public void testGetStatus() { + try { + stdPDP.setStatus(new StdPDPStatus()); + assertTrue(stdPDP.getStatus() != null); + } catch (Exception e) { + logger.error(e); + } + } + + @Test + public void testGetPipConfigs() { + try { + assertTrue(stdPDP.getPipConfigs() != null); + } catch (Exception e) { + logger.error(e); + } + } + + @Test + public void testGetJmxPort() { + try { + stdPDP.setJmxPort(123); + assertTrue(stdPDP.getJmxPort() != null); + } catch (Exception e) { + logger.error(e); + } + } + + @Test + public void testPDP() { + // Set up test data + String id = "testID"; + String value = "testVal"; + Properties props = new Properties(); + props.setProperty(id + ".name", value); + props.setProperty(id + ".description", value); + props.setProperty(id + ".jmxport", "0"); + Set pipConfigs = new HashSet(); + Set policies = new HashSet(); + + // Test constructors + StdPDP pdp = new StdPDP(id, 0); + assertNotNull(pdp); + StdPDP pdp2 = new StdPDP(id, value, 0); + assertNotNull(pdp2); + StdPDP pdp3 = new StdPDP(id, value, value, 0); + assertNotNull(pdp3); + StdPDP pdp4 = new StdPDP(id, props); + assertNotNull(pdp4); + StdPDP pdp5 = new StdPDP(id, props); + assertNotNull(pdp5); + + // Test set and get + pdp.setPipConfigs(pipConfigs); + assertEquals(pipConfigs, pdp.getPipConfigs()); + pdp.setPolicies(policies); + assertEquals(policies, pdp.getPolicies()); + + // Test equals combinations + assertEquals(true, pdp4.equals(pdp5)); + assertEquals(true, pdp4.equals(pdp4)); + assertEquals(false, pdp4.equals(null)); + assertEquals(false, pdp4.equals(value)); + pdp4.setId(null); + assertEquals(false, pdp4.equals(value)); + pdp5.setId(id); + assertEquals(false, pdp4.equals(pdp5)); + pdp4.setId(value); + assertEquals(false, pdp4.equals(pdp5)); + assertThat(pdp.hashCode(), is(not(0))); + + // Test compare + assertEquals(-1, pdp4.compareTo(null)); + assertEquals(0, pdp4.compareTo(pdp4)); + pdp5.setName(null); + assertEquals(-1, pdp4.compareTo(pdp5)); + pdp4.setName(null); + pdp5.setName(value); + assertEquals(1, pdp4.compareTo(pdp5)); + // Test toString + assertThat(pdp.toString().length(), is(not(0))); + } } -- cgit 1.2.3-korg