diff options
author | Tej, Tarun <tt3868@att.com> | 2017-09-15 00:52:13 -0400 |
---|---|---|
committer | Tej, Tarun <tt3868@att.com> | 2017-09-15 01:37:52 -0400 |
commit | e0385921034ae9ce860038ea65d2d13259f7cc4c (patch) | |
tree | fa9461d587584fca27fc9a4a81614c91dcac6231 /PolicyEngineUtils/src/test/java/org/onap | |
parent | e9e7bef3c130f7525a5ad250f365ee910bebe85a (diff) |
Adding Junits for policy engine
Adding Junits to Policy Engine Utils project, removed duplicates, fixed
few defects along the way and fixes to projects.
Issue-Id: POLICY-52
Change-Id: Ia0598300f9bd60f5372b9ef7d8984657478a1611
Signed-off-by: Tej, Tarun <tt3868@att.com>
Diffstat (limited to 'PolicyEngineUtils/src/test/java/org/onap')
-rw-r--r-- | PolicyEngineUtils/src/test/java/org/onap/policy/test/PolicyUtilsTest.java | 67 | ||||
-rw-r--r-- | PolicyEngineUtils/src/test/java/org/onap/policy/test/testBackUpMonitor.java | 121 | ||||
-rw-r--r-- | PolicyEngineUtils/src/test/java/org/onap/policy/utils/test/AAFClientTest.java | 90 | ||||
-rw-r--r-- | PolicyEngineUtils/src/test/java/org/onap/policy/utils/test/BusTest.java | 57 | ||||
-rw-r--r-- | PolicyEngineUtils/src/test/java/org/onap/policy/utils/test/Handler.java (renamed from PolicyEngineUtils/src/test/java/org/onap/policy/test/Handler.java) | 2 | ||||
-rw-r--r-- | PolicyEngineUtils/src/test/java/org/onap/policy/utils/test/NotificationStoreTest.java | 99 | ||||
-rw-r--r-- | PolicyEngineUtils/src/test/java/org/onap/policy/utils/test/PEDependencyTest.java | 59 | ||||
-rw-r--r-- | PolicyEngineUtils/src/test/java/org/onap/policy/utils/test/PolicyUtilsTest.java | 209 | ||||
-rw-r--r-- | PolicyEngineUtils/src/test/java/org/onap/policy/utils/test/testBackUpMonitor.java | 292 |
9 files changed, 807 insertions, 189 deletions
diff --git a/PolicyEngineUtils/src/test/java/org/onap/policy/test/PolicyUtilsTest.java b/PolicyEngineUtils/src/test/java/org/onap/policy/test/PolicyUtilsTest.java deleted file mode 100644 index 50ae3feae..000000000 --- a/PolicyEngineUtils/src/test/java/org/onap/policy/test/PolicyUtilsTest.java +++ /dev/null @@ -1,67 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * PolicyEngineUtils - * ================================================================================ - * Copyright (C) 2017 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.test; - -import static org.junit.Assert.assertEquals; - -import java.util.ArrayList; -import java.util.Collection; -import java.util.HashMap; -import java.util.Map; - -import org.junit.Test; -import org.onap.policy.api.NotificationType; -import org.onap.policy.api.PDPNotification; -import org.onap.policy.api.UpdateType; -import org.onap.policy.std.StdLoadedPolicy; -import org.onap.policy.std.StdPDPNotification; -import org.onap.policy.std.StdRemovedPolicy; -import org.onap.policy.utils.PolicyUtils; - -public class PolicyUtilsTest { - - @Test - public void testJsonConversions() throws Exception{ - StdPDPNotification notification = new StdPDPNotification(); - notification.setNotificationType(NotificationType.BOTH); - Collection<StdRemovedPolicy> removedPolicies = new ArrayList<>(); - Collection<StdLoadedPolicy> loadedPolicies = new ArrayList<>(); - StdRemovedPolicy removedPolicy = new StdRemovedPolicy(); - StdLoadedPolicy updatedPolicy = new StdLoadedPolicy(); - removedPolicy.setPolicyName("Test"); - removedPolicy.setVersionNo("1"); - removedPolicies.add(removedPolicy); - updatedPolicy.setPolicyName("Testing"); - updatedPolicy.setVersionNo("1"); - updatedPolicy.setUpdateType(UpdateType.NEW); - Map<String, String> matches = new HashMap<>(); - matches.put("key", "value"); - updatedPolicy.setMatches(matches); - loadedPolicies.add(updatedPolicy); - notification.setRemovedPolicies(removedPolicies); - notification.setLoadedPolicies(loadedPolicies); - - String json = PolicyUtils.objectToJsonString(notification); - PDPNotification getBackObject = PolicyUtils.jsonStringToObject(json, StdPDPNotification.class); - assertEquals(0,getBackObject.getNotificationType().compareTo(notification.getNotificationType())); - - } -} diff --git a/PolicyEngineUtils/src/test/java/org/onap/policy/test/testBackUpMonitor.java b/PolicyEngineUtils/src/test/java/org/onap/policy/test/testBackUpMonitor.java deleted file mode 100644 index 4cdb81873..000000000 --- a/PolicyEngineUtils/src/test/java/org/onap/policy/test/testBackUpMonitor.java +++ /dev/null @@ -1,121 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * PolicyEngineUtils - * ================================================================================ - * Copyright (C) 2017 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.test; - -import static org.junit.Assert.assertNull; -import java.util.Properties; - -import org.junit.Test; -import org.onap.policy.utils.BackUpMonitor; - - -public class testBackUpMonitor { - - @Test - public void backUpMonitorTestFail() throws Exception{ - Properties properties = new Properties(); - properties.setProperty("javax.persistence.jdbc.driver", "com.mysql.jdbc.Driver"); - properties.setProperty("javax.persistence.jdbc.url", "jdbc:mysql://localhost:3306/xacml"); - properties.setProperty("javax.persistence.jdbc.user", "policy_user"); - properties.setProperty("javax.persistence.jdbc.password", ""); - //properties.setProperty("ping_interval", "500000"); - BackUpMonitor bum = BackUpMonitor.getInstance(BackUpMonitor.ResourceNode.BRMS.toString(), "brms_test" , properties, new Handler()); - assertNull(bum); - } - - @Test - public void backUpMonitorTestFailNoUser() throws Exception{ - Properties properties = new Properties(); - properties.setProperty("javax.persistence.jdbc.driver", "com.mysql.jdbc.Driver"); - properties.setProperty("javax.persistence.jdbc.url", "jdbc:mysql://localhost:3306/xacml"); - properties.setProperty("javax.persistence.jdbc.user", ""); - properties.setProperty("javax.persistence.jdbc.password", "password"); - //properties.setProperty("ping_interval", "500000"); - BackUpMonitor bum = BackUpMonitor.getInstance(BackUpMonitor.ResourceNode.BRMS.toString(), "brms_test" , properties, new Handler()); - assertNull(bum); - } - - @Test - public void backUpMonitorTestFailNoURL() throws Exception{ - Properties properties = new Properties(); - properties.setProperty("javax.persistence.jdbc.driver", "com.mysql.jdbc.Driver"); - properties.setProperty("javax.persistence.jdbc.url", ""); - properties.setProperty("javax.persistence.jdbc.user", "test"); - properties.setProperty("javax.persistence.jdbc.password", "password"); - //properties.setProperty("ping_interval", "500000"); - BackUpMonitor bum = BackUpMonitor.getInstance(BackUpMonitor.ResourceNode.BRMS.toString(), "brms_test" , properties, new Handler()); - assertNull(bum); - } - - @Test - public void backUpMonitorTestFailNoDriver() throws Exception{ - Properties properties = new Properties(); - properties.setProperty("javax.persistence.jdbc.driver", ""); - properties.setProperty("javax.persistence.jdbc.url", "jdbc:mysql://localhost:3306/xacml"); - properties.setProperty("javax.persistence.jdbc.user", "test"); - properties.setProperty("javax.persistence.jdbc.password", "password"); - //properties.setProperty("ping_interval", "500000"); - BackUpMonitor bum = BackUpMonitor.getInstance(BackUpMonitor.ResourceNode.BRMS.toString(), "brms_test" , properties, new Handler()); - assertNull(bum); - } - - @Test - public void backUpMonitorTestFailNoNode() throws Exception{ - Properties properties = new Properties(); - properties.setProperty("javax.persistence.jdbc.driver", "com.mysql.jdbc.Driver"); - properties.setProperty("javax.persistence.jdbc.url", "jdbc:mysql://localhost:3306/xacml"); - properties.setProperty("javax.persistence.jdbc.user", "test"); - properties.setProperty("javax.persistence.jdbc.password", "password"); - //properties.setProperty("ping_interval", "500000"); - BackUpMonitor bum = BackUpMonitor.getInstance(null, "brms_test" , properties, new Handler()); - assertNull(bum); - } - - @Test - public void backUpMonitorTestFailNoResource() throws Exception{ - Properties properties = new Properties(); - properties.setProperty("javax.persistence.jdbc.driver", "com.mysql.jdbc.Driver"); - properties.setProperty("javax.persistence.jdbc.url", "jdbc:mysql://localhost:3306/xacml"); - properties.setProperty("javax.persistence.jdbc.user", "test"); - properties.setProperty("javax.persistence.jdbc.password", "password"); - //properties.setProperty("ping_interval", "500000"); - BackUpMonitor bum = BackUpMonitor.getInstance(BackUpMonitor.ResourceNode.BRMS.toString(), null , properties, new Handler()); - assertNull(bum); - } - - @Test - public void backUpMonitorTestFailNoProperties() throws Exception{ - BackUpMonitor bum = BackUpMonitor.getInstance(BackUpMonitor.ResourceNode.BRMS.toString(), "brms_test" , null, new Handler()); - assertNull(bum); - } - - @Test - public void backUpMonitorTestFailNoHandler() throws Exception{ - Properties properties = new Properties(); - properties.setProperty("javax.persistence.jdbc.driver", "com.mysql.jdbc.Driver"); - properties.setProperty("javax.persistence.jdbc.url", "jdbc:mysql://localhost:3306/xacml"); - properties.setProperty("javax.persistence.jdbc.user", "test"); - properties.setProperty("javax.persistence.jdbc.password", "password"); - //properties.setProperty("ping_interval", "500000"); - BackUpMonitor bum = BackUpMonitor.getInstance(BackUpMonitor.ResourceNode.BRMS.toString(), "brms_test" , properties, null); - assertNull(bum); - } -} diff --git a/PolicyEngineUtils/src/test/java/org/onap/policy/utils/test/AAFClientTest.java b/PolicyEngineUtils/src/test/java/org/onap/policy/utils/test/AAFClientTest.java new file mode 100644 index 000000000..2fa822a33 --- /dev/null +++ b/PolicyEngineUtils/src/test/java/org/onap/policy/utils/test/AAFClientTest.java @@ -0,0 +1,90 @@ +/*- + * ============LICENSE_START======================================================= + * PolicyEngineUtils + * ================================================================================ + * Copyright (C) 2017 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.utils.test; + +import static org.junit.Assert.assertFalse; + +import java.util.Properties; + +import org.junit.Before; +import org.junit.Test; +import org.onap.policy.utils.AAFPolicyClient; +import org.onap.policy.utils.AAFPolicyClientImpl; +import org.onap.policy.utils.AAFPolicyException; + +public class AAFClientTest { + AAFPolicyClient afClient; + String pass = "test"; + + @Before + public void setUp() throws AAFPolicyException{ + Properties props = new Properties(); + props.setProperty("ENVIRONMENT", "TEST"); + props.setProperty("aafClient.impl.className", AAFPolicyClientImpl.class.getName()); + afClient = AAFPolicyClient.getInstance(props); + } + + @Test + public void invalidClientTest() throws AAFPolicyException{ + assertFalse(afClient.checkAuth("test", pass)); + assertFalse(afClient.checkPerm("test", pass, "policy-engine.config", "*", "*")); + Properties props = new Properties(); + props.setProperty("aafClient.impl.className", AAFPolicyClientImpl.class.getName()); + afClient = AAFPolicyClient.getInstance(props); + assertFalse(afClient.checkAuth("test", pass)); + props.setProperty("ENVIRONMENT", "PROD"); + props.setProperty("aafClient.impl.className", AAFPolicyClientImpl.class.getName()); + afClient.updateProperties(props); + assertFalse(afClient.checkAuth("test", pass)); + props.setProperty("aaf_url", "test"); + afClient.updateProperties(props); + assertFalse(afClient.checkAuth("test", pass)); + props = new Properties(); + props.setProperty("ENVIRONMENT", "123"); + afClient.updateProperties(props); + assertFalse(afClient.checkAuth("test", pass)); + assertFalse(afClient.checkAuthPerm("test", pass, "decision", "*", "read")); + } + + @Test(expected = AAFPolicyException.class) + public void invalidAAFInstance() throws AAFPolicyException{ + Properties props = new Properties(); + props.setProperty("aafClient.impl.className", "errorClass"); + afClient = AAFPolicyClient.getInstance(props); + } + + @Test(expected = AAFPolicyException.class) + public void testPropNullException() throws AAFPolicyException { + afClient.updateProperties(null); + } + + @Test(expected = AAFPolicyException.class) + public void testPropEmptyException() throws AAFPolicyException { + afClient.updateProperties(new Properties()); + } + + @Test(expected = AAFPolicyException.class) + public void testAAFException() throws AAFPolicyException{ + new AAFPolicyException(); + new AAFPolicyException("error", new Exception()); + throw new AAFPolicyException("error", new Exception(), false, false); + } +} diff --git a/PolicyEngineUtils/src/test/java/org/onap/policy/utils/test/BusTest.java b/PolicyEngineUtils/src/test/java/org/onap/policy/utils/test/BusTest.java new file mode 100644 index 000000000..37170f077 --- /dev/null +++ b/PolicyEngineUtils/src/test/java/org/onap/policy/utils/test/BusTest.java @@ -0,0 +1,57 @@ +/*- + * ============LICENSE_START======================================================= + * PolicyEngineUtils + * ================================================================================ + * Copyright (C) 2017 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.utils.test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import java.net.MalformedURLException; +import java.util.Arrays; + +import org.junit.Test; +import org.onap.policy.utils.BusConsumer; +import org.onap.policy.utils.BusPublisher; + +import com.att.nsa.mr.client.MRClient.MRApiException; + +public class BusTest { + + @Test + public void busPublisherTest(){ + BusPublisher bus = new BusPublisher.DmaapPublisherWrapper(Arrays.asList("test"), "test", "test", "test"); + assertTrue(bus.send("test123", "Hello World!")); + assertEquals("DmaapPublisherWrapper [publisher.getAuthDate()=null, publisher.getAuthKey()=null, publisher.getHost()=test, publisher.getProtocolFlag()=HTTPAAF, publisher.getUsername()=test, publisher.getPendingMessageCount()=1]",bus.toString()); + bus.close(); + } + + @Test (expected = MRApiException.class) + public void busConsumerFailTest() throws MalformedURLException, MRApiException{ + new BusConsumer.DmaapConsumerWrapper(Arrays.asList("test"), "test", "test", "test", "test", "test", 1, 1).fetch(); + } + + @Test + public void busConsumerTest() throws MalformedURLException, MRApiException{ + BusConsumer bus = new BusConsumer.DmaapConsumerWrapper(Arrays.asList("test"), "test", "test", "test", "test", "test", 1, 1); + assertEquals(bus.toString(),"DmaapConsumerWrapper [consumer.getAuthDate()=null, consumer.getAuthKey()=null, consumer.getHost()=test:3904, consumer.getProtocolFlag()=HTTPAAF, consumer.getUsername()=test]"); + bus.close(); + } + +} diff --git a/PolicyEngineUtils/src/test/java/org/onap/policy/test/Handler.java b/PolicyEngineUtils/src/test/java/org/onap/policy/utils/test/Handler.java index 0f71858a3..91f467b60 100644 --- a/PolicyEngineUtils/src/test/java/org/onap/policy/test/Handler.java +++ b/PolicyEngineUtils/src/test/java/org/onap/policy/utils/test/Handler.java @@ -18,7 +18,7 @@ * ============LICENSE_END========================================================= */ -package org.onap.policy.test; +package org.onap.policy.utils.test; import org.onap.policy.api.PDPNotification; import org.onap.policy.utils.BackUpHandler; diff --git a/PolicyEngineUtils/src/test/java/org/onap/policy/utils/test/NotificationStoreTest.java b/PolicyEngineUtils/src/test/java/org/onap/policy/utils/test/NotificationStoreTest.java new file mode 100644 index 000000000..8ec646a0f --- /dev/null +++ b/PolicyEngineUtils/src/test/java/org/onap/policy/utils/test/NotificationStoreTest.java @@ -0,0 +1,99 @@ +/*- + * ============LICENSE_START======================================================= + * PolicyEngineUtils + * ================================================================================ + * Copyright (C) 2017 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.utils.test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.junit.Test; +import org.onap.policy.api.NotificationType; +import org.onap.policy.api.UpdateType; +import org.onap.policy.std.NotificationStore; +import org.onap.policy.std.StdLoadedPolicy; +import org.onap.policy.std.StdPDPNotification; +import org.onap.policy.std.StdRemovedPolicy; + +public class NotificationStoreTest { + + @Test + public void notificationTest(){ + StdPDPNotification notification = new StdPDPNotification(); + notification.setNotificationType(NotificationType.UPDATE); + List<StdLoadedPolicy> loadedPolicies = new ArrayList<>(); + StdLoadedPolicy loadedPolicy = new StdLoadedPolicy(); + loadedPolicy.setPolicyName("com.testing"); + loadedPolicy.setUpdateType(UpdateType.NEW); + loadedPolicy.setVersionNo("1"); + Map<String, String> matches = new HashMap<>(); + matches.put("test", "test"); + loadedPolicy.setMatches(matches); + loadedPolicies.add(loadedPolicy); + notification.setLoadedPolicies(loadedPolicies); + NotificationStore.recordNotification(notification); + assertEquals(notification, NotificationStore.getNotificationRecord()); + // Add new Notifications. + notification = new StdPDPNotification(); + notification.setNotificationType(NotificationType.BOTH); + loadedPolicies = new ArrayList<>(); + loadedPolicy = new StdLoadedPolicy(); + loadedPolicy.setPolicyName("com.testing"); + loadedPolicy.setUpdateType(UpdateType.UPDATE); + loadedPolicy.setVersionNo("2"); + matches = new HashMap<>(); + matches.put("test", "test"); + loadedPolicy.setMatches(matches); + loadedPolicies.add(loadedPolicy); + notification.setLoadedPolicies(loadedPolicies); + List<StdRemovedPolicy> removedPolicies = new ArrayList<>(); + StdRemovedPolicy removedPolicy = new StdRemovedPolicy(); + removedPolicy.setPolicyName("com.testing"); + removedPolicy.setVersionNo("1"); + notification.setRemovedPolicies(removedPolicies); + NotificationStore.recordNotification(notification); + assertNotNull(NotificationStore.getNotificationRecord()); + // Add new Notifications. + notification = new StdPDPNotification(); + notification.setNotificationType(NotificationType.BOTH); + loadedPolicies = new ArrayList<>(); + loadedPolicy = new StdLoadedPolicy(); + loadedPolicy.setPolicyName("com.test.xml"); + loadedPolicy.setUpdateType(UpdateType.NEW); + loadedPolicy.setVersionNo("2"); + matches = new HashMap<>(); + matches.put("test", "test"); + loadedPolicy.setMatches(matches); + loadedPolicies.add(loadedPolicy); + notification.setLoadedPolicies(loadedPolicies); + removedPolicies = new ArrayList<>(); + removedPolicy = new StdRemovedPolicy(); + removedPolicy.setPolicyName("com.testing.xml"); + removedPolicy.setVersionNo("2"); + notification.setRemovedPolicies(removedPolicies); + NotificationStore.recordNotification(notification); + assertNotNull(NotificationStore.getNotificationRecord()); + } + +} diff --git a/PolicyEngineUtils/src/test/java/org/onap/policy/utils/test/PEDependencyTest.java b/PolicyEngineUtils/src/test/java/org/onap/policy/utils/test/PEDependencyTest.java new file mode 100644 index 000000000..4cc6c90f2 --- /dev/null +++ b/PolicyEngineUtils/src/test/java/org/onap/policy/utils/test/PEDependencyTest.java @@ -0,0 +1,59 @@ +/*- + * ============LICENSE_START======================================================= + * PolicyEngineUtils + * ================================================================================ + * Copyright (C) 2017 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.utils.test; + +import static org.junit.Assert.assertEquals; + +import java.util.ArrayList; +import java.util.List; + +import org.apache.maven.model.Exclusion; +import org.junit.Test; +import org.onap.policy.api.PEDependency; + +public class PEDependencyTest { + + @Test + public void pojoTests(){ + PEDependency pe = new PEDependency(); + pe.setArtifactId("test"); + pe.setGroupId("test"); + pe.setVersion("1"); + pe.setType("jar"); + pe.setScope("test"); + pe.setClassifier("pom"); + List<Exclusion> exclusions = new ArrayList<>(); + Exclusion e = new Exclusion(); + e.setArtifactId("ex1"); + e.setGroupId("eG"); + exclusions.add(e); + pe.setExclusions(exclusions); + pe.getDependency(); + assertEquals(exclusions, pe.getExclusions()); + assertEquals("pom", pe.getClassifier()); + assertEquals("test", pe.getScope()); + assertEquals("jar", pe.getType()); + assertEquals("1", pe.getVersion()); + assertEquals("test", pe.getGroupId()); + assertEquals("test", pe.getArtifactId()); + } + +} diff --git a/PolicyEngineUtils/src/test/java/org/onap/policy/utils/test/PolicyUtilsTest.java b/PolicyEngineUtils/src/test/java/org/onap/policy/utils/test/PolicyUtilsTest.java new file mode 100644 index 000000000..586ab356d --- /dev/null +++ b/PolicyEngineUtils/src/test/java/org/onap/policy/utils/test/PolicyUtilsTest.java @@ -0,0 +1,209 @@ +/*- + * ============LICENSE_START======================================================= + * PolicyEngineUtils + * ================================================================================ + * Copyright (C) 2017 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.utils.test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotEquals; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; + +import java.util.ArrayList; +import java.util.Base64; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; + +import org.junit.Test; +import org.onap.policy.api.NotificationType; +import org.onap.policy.api.PDPNotification; +import org.onap.policy.api.UpdateType; +import org.onap.policy.std.StdLoadedPolicy; +import org.onap.policy.std.StdPDPNotification; +import org.onap.policy.std.StdRemovedPolicy; +import org.onap.policy.utils.PolicyUtils; +import org.onap.policy.utils.XMLErrorHandler; +import org.xml.sax.SAXException; +import org.xml.sax.SAXParseException; + +public class PolicyUtilsTest { + + private static final String ERROR = "The Value in Required Field will allow only '{0-9}, {a-z}, {A-Z}, _' following set of Combinations"; + private static final String SUCCESS = "success"; + + @Test + public void testJsonConversions() throws Exception{ + StdPDPNotification notification = new StdPDPNotification(); + notification.setNotificationType(NotificationType.BOTH); + Collection<StdRemovedPolicy> removedPolicies = new ArrayList<>(); + Collection<StdLoadedPolicy> loadedPolicies = new ArrayList<>(); + StdRemovedPolicy removedPolicy = new StdRemovedPolicy(); + StdLoadedPolicy updatedPolicy = new StdLoadedPolicy(); + removedPolicy.setPolicyName("Test"); + removedPolicy.setVersionNo("1"); + removedPolicies.add(removedPolicy); + updatedPolicy.setPolicyName("Testing"); + updatedPolicy.setVersionNo("1"); + updatedPolicy.setUpdateType(UpdateType.NEW); + Map<String, String> matches = new HashMap<>(); + matches.put("key", "value"); + updatedPolicy.setMatches(matches); + loadedPolicies.add(updatedPolicy); + notification.setRemovedPolicies(removedPolicies); + notification.setLoadedPolicies(loadedPolicies); + + String json = PolicyUtils.objectToJsonString(notification); + PDPNotification getBackObject = PolicyUtils.jsonStringToObject(json, StdPDPNotification.class); + assertEquals(0,getBackObject.getNotificationType().compareTo(notification.getNotificationType())); + + } + + private String encodedValue(String input){ + return new String(Base64.getEncoder().encode(input.getBytes())); + } + + @Test + public void testDecode() throws Exception{ + String value = "test"; + assertEquals(value, PolicyUtils.decode(encodedValue(value))); + assertNull(PolicyUtils.decode(null)); + assertNull(PolicyUtils.decode("")); + } + + @Test + public void testBasicEncoding() throws Exception{ + String userName = "test"; + String key = "pass"; + String[] decodedValue = PolicyUtils.decodeBasicEncoding("Basic "+encodedValue(userName+":"+key)); + assertEquals(userName,decodedValue[0]); + assertEquals(key,decodedValue[1]); + assertEquals(0, PolicyUtils.decodeBasicEncoding(encodedValue(userName+":"+key)).length); + assertEquals(0, PolicyUtils.decodeBasicEncoding(null).length); + } + + @Test + public void testSpecialCharValidator(){ + assertEquals(ERROR, PolicyUtils.policySpecialCharValidator("$TEST_")); + assertEquals(ERROR, PolicyUtils.policySpecialCharValidator("$TEST _")); + assertEquals(ERROR, PolicyUtils.policySpecialCharValidator("")); + assertEquals(ERROR, PolicyUtils.policySpecialCharValidator("TæST")); + assertEquals(SUCCESS, PolicyUtils.policySpecialCharValidator("TEST")); + } + + @Test + public void testSpecialCharWithSpaceValidator(){ + assertEquals(ERROR, PolicyUtils.policySpecialCharWithSpaceValidator("")); + assertEquals(ERROR, PolicyUtils.policySpecialCharWithSpaceValidator("$TEST _")); + assertEquals(SUCCESS, PolicyUtils.policySpecialCharWithSpaceValidator("TE ST")); + } + + @Test + public void testDescription() { + assertEquals(SUCCESS, PolicyUtils.descriptionValidator("Test")); + assertNotEquals(SUCCESS, PolicyUtils.descriptionValidator("@ModifiedBy:TesterB")); + assertNotEquals(SUCCESS, PolicyUtils.descriptionValidator("@CreatedBy:TesterA")); + } + + @Test + public void testNonAscii(){ + assertTrue(PolicyUtils.containsNonAsciiEmptyChars(null)); + assertTrue(PolicyUtils.containsNonAsciiEmptyChars("")); + assertTrue(PolicyUtils.containsNonAsciiEmptyChars("T æST")); + assertTrue(PolicyUtils.containsNonAsciiEmptyChars("TæST")); + assertFalse(PolicyUtils.containsNonAsciiEmptyChars("TEST")); + } + + @Test + public void testInteger(){ + assertFalse(PolicyUtils.isInteger(null)); + assertTrue(PolicyUtils.isInteger("123")); + assertFalse(PolicyUtils.isInteger("1a23")); + } + + @Test + public void testEmailAddress(){ + assertEquals(SUCCESS, PolicyUtils.validateEmailAddress("test@onap.org")); + assertNotEquals(SUCCESS, PolicyUtils.validateEmailAddress("test@onap")); + } + + @Test + public void testBRMSValidate(){ + String rule = "package com.sample;\n" + + "import com.sample.DroolsTest.Message;\n" + + "declare Params\n" + + "samPoll : int\n" + + "value : String\n" + + "end\n" + + "///This Rule will be generated by the UI.\n" + + "rule \"Create parameters structure\"\n" + + "salience 1000 \n" + + "when\n" + + "then\n" + + "Params params = new Params();\n" + + "params.setSamPoll(76);\n" + + "params.setValue(\"test\");\n" + + "insertLogical(params);\n" + + "end\n" + + "rule \"Rule 1: Check parameter structure access from when/then\"\n" + + "when\n" + + "$param: Params()\n" + + "Params($param.samPoll > 50)\n" + + "then\n" + + "System.out.println(\"Firing rule 1\");\n" + + "System.out.println($param);\n" + + "end\n"; + assertEquals(PolicyUtils.brmsRawValidate(rule),""); + assertTrue(PolicyUtils.brmsRawValidate("error").contains("[ERR")); + assertFalse(PolicyUtils.brmsRawValidate("package com.att.ecomp.policy.controlloop.p_${unique};").contains("[ERR")); + } + + @Test + public void testiIsJsonValid(){ + assertTrue(PolicyUtils.isJSONValid("{\"test\":\"test\"}")); + String value = "{\"test\":\"test\", \"t1\": {\"test\": 12 , \"t2\":\"34\"},\"t2\":[{\"test\":\"val\"}]}"; + assertTrue(PolicyUtils.isJSONValid(value)); + assertFalse(PolicyUtils.isJSONValid("{\"test\":\"test")); + } + + @Test + public void testIsXMLValid() throws SAXException{ + XMLErrorHandler error = new XMLErrorHandler(); + error.error(new SAXParseException(null, null)); + error.warning(new SAXParseException(null, null)); + assertTrue(PolicyUtils.isXMLValid("<test>123</test>")); + assertFalse(PolicyUtils.isXMLValid("<test>123</test")); + } + + @Test + public void testIsPropValid(){ + assertTrue(PolicyUtils.isPropValid("test=123\n\tval=123")); + assertFalse(PolicyUtils.isPropValid("test")); + assertFalse(PolicyUtils.isPropValid("test=")); + assertTrue(PolicyUtils.isPropValid("#test\n\nval=123")); + } + + @Test + public void testVersionStringToArray(){ + assertTrue(PolicyUtils.versionStringToArray(null).length==0); + assertTrue(PolicyUtils.versionStringToArray("").length==0); + assertTrue(PolicyUtils.versionStringToArray("1.2.3").length==3); + } +}
\ No newline at end of file diff --git a/PolicyEngineUtils/src/test/java/org/onap/policy/utils/test/testBackUpMonitor.java b/PolicyEngineUtils/src/test/java/org/onap/policy/utils/test/testBackUpMonitor.java new file mode 100644 index 000000000..afc3d0dce --- /dev/null +++ b/PolicyEngineUtils/src/test/java/org/onap/policy/utils/test/testBackUpMonitor.java @@ -0,0 +1,292 @@ +/*- + * ============LICENSE_START======================================================= + * PolicyEngineUtils + * ================================================================================ + * Copyright (C) 2017 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.utils.test; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; + +import java.util.ArrayList; +import java.util.Date; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Properties; +import java.util.concurrent.TimeUnit; + +import javax.persistence.EntityManager; +import javax.persistence.EntityTransaction; +import javax.persistence.Persistence; +import javax.persistence.PersistenceException; +import javax.persistence.Query; + +import org.eclipse.persistence.config.PersistenceUnitProperties; +import org.junit.After; +import org.junit.Test; +import org.onap.policy.api.NotificationType; +import org.onap.policy.api.UpdateType; +import org.onap.policy.jpa.BackUpMonitorEntity; +import org.onap.policy.std.NotificationStore; +import org.onap.policy.std.StdLoadedPolicy; +import org.onap.policy.std.StdPDPNotification; +import org.onap.policy.std.StdRemovedPolicy; +import org.onap.policy.utils.BackUpMonitor; +import org.onap.policy.utils.BackUpMonitor.ResourceNode; +import org.onap.policy.utils.BackUpMonitorException; +import org.onap.policy.utils.PolicyUtils; + +import com.fasterxml.jackson.core.JsonProcessingException; + + +public class testBackUpMonitor { + + @Test (expected = PersistenceException.class) + public void backUpMonitorTestFail() throws Exception{ + Properties properties = new Properties(); + properties.setProperty("javax.persistence.jdbc.driver", "com.mysql.jdbc.Driver"); + properties.setProperty("javax.persistence.jdbc.url", "jdbc:mysql://localhost:3306/xacml"); + properties.setProperty("javax.persistence.jdbc.user", "policy_user"); + properties.setProperty("javax.persistence.jdbc.password", ""); + BackUpMonitor.getInstance(BackUpMonitor.ResourceNode.BRMS.toString(), "brms_test" , properties, new Handler()); + } + + @Test + public void backUpMonitorTestFailNoUser() throws Exception{ + Properties properties = new Properties(); + properties.setProperty("javax.persistence.jdbc.driver", "com.mysql.jdbc.Driver"); + properties.setProperty("javax.persistence.jdbc.url", "jdbc:mysql://localhost:3306/xacml"); + properties.setProperty("javax.persistence.jdbc.user", ""); + properties.setProperty("javax.persistence.jdbc.password", "password"); + BackUpMonitor bum = BackUpMonitor.getInstance(BackUpMonitor.ResourceNode.BRMS.toString(), "brms_test" , properties, new Handler()); + assertNull(bum); + } + + @Test + public void backUpMonitorTestFailNoURL() throws Exception{ + Properties properties = new Properties(); + properties.setProperty("javax.persistence.jdbc.driver", "com.mysql.jdbc.Driver"); + properties.setProperty("javax.persistence.jdbc.url", ""); + properties.setProperty("javax.persistence.jdbc.user", "test"); + properties.setProperty("javax.persistence.jdbc.password", "password"); + properties.setProperty("ping_interval", "500"); + BackUpMonitor bum = BackUpMonitor.getInstance(BackUpMonitor.ResourceNode.BRMS.toString(), "brms_test" , properties, new Handler()); + assertNull(bum); + } + + @Test + public void backUpMonitorTestFailNoDriver() throws Exception{ + Properties properties = new Properties(); + properties.setProperty("javax.persistence.jdbc.driver", ""); + properties.setProperty("javax.persistence.jdbc.url", "jdbc:mysql://localhost:3306/xacml"); + properties.setProperty("javax.persistence.jdbc.user", "test"); + properties.setProperty("javax.persistence.jdbc.password", "password"); + properties.setProperty("ping_interval", "500"); + BackUpMonitor bum = BackUpMonitor.getInstance(BackUpMonitor.ResourceNode.BRMS.toString(), "brms_test" , properties, new Handler()); + assertNull(bum); + } + + @Test + public void backUpMonitorTestFailNoNode() throws Exception{ + Properties properties = new Properties(); + properties.setProperty("javax.persistence.jdbc.driver", "com.mysql.jdbc.Driver"); + properties.setProperty("javax.persistence.jdbc.url", "jdbc:mysql://localhost:3306/xacml"); + properties.setProperty("javax.persistence.jdbc.user", "test"); + properties.setProperty("javax.persistence.jdbc.password", "password"); + properties.setProperty("ping_interval", ""); + BackUpMonitor bum = BackUpMonitor.getInstance(null, "brms_test" , properties, new Handler()); + assertNull(bum); + } + + @Test + public void backUpMonitorTestFailNoResource() throws Exception{ + Properties properties = new Properties(); + properties.setProperty("javax.persistence.jdbc.driver", "com.mysql.jdbc.Driver"); + properties.setProperty("javax.persistence.jdbc.url", "jdbc:mysql://localhost:3306/xacml"); + properties.setProperty("javax.persistence.jdbc.user", "test"); + properties.setProperty("javax.persistence.jdbc.password", "password"); + BackUpMonitor bum = BackUpMonitor.getInstance(BackUpMonitor.ResourceNode.BRMS.toString(), null , properties, new Handler()); + assertNull(bum); + } + + @Test + public void backUpMonitorTestFailNoProperties() throws Exception{ + BackUpMonitor bum = BackUpMonitor.getInstance(BackUpMonitor.ResourceNode.BRMS.toString(), "brms_test" , null, new Handler()); + assertNull(bum); + } + + @Test + public void backUpMonitorTestFailNoHandler() throws Exception{ + Properties properties = new Properties(); + properties.setProperty("javax.persistence.jdbc.driver", "com.mysql.jdbc.Driver"); + properties.setProperty("javax.persistence.jdbc.url", "jdbc:mysql://localhost:3306/xacml"); + properties.setProperty("javax.persistence.jdbc.user", "test"); + properties.setProperty("javax.persistence.jdbc.password", "password"); + properties.setProperty("ping_interval", "500"); + BackUpMonitor bum = BackUpMonitor.getInstance(BackUpMonitor.ResourceNode.BRMS.toString(), "brms_test" , properties, null); + assertNull(bum); + } + + @Test + public void backUpMonitorPingError() throws BackUpMonitorException { + Properties properties = new Properties(); + properties.setProperty("javax.persistence.jdbc.driver", "org.h2.Driver"); + properties.setProperty("javax.persistence.jdbc.url", "jdbc:h2:file:./sql/xacmlTest"); + properties.setProperty("javax.persistence.jdbc.user", "sa"); + properties.setProperty("javax.persistence.jdbc.password", ""); + properties.setProperty("ping_interval", "123a"); + properties.setProperty(PersistenceUnitProperties.ECLIPSELINK_PERSISTENCE_XML, "META-INF/persistencePUtest.xml"); + BackUpMonitor bum = BackUpMonitor.getInstance(BackUpMonitor.ResourceNode.BRMS.toString(), "brms_test" , properties, new Handler()); + assertTrue(bum.getFlag()); + } + + @Test + public void backUpMonitorMasterTest() throws BackUpMonitorException, InterruptedException, JsonProcessingException { + Properties properties = new Properties(); + // Master Check. Initial Run. + properties.setProperty("javax.persistence.jdbc.driver", "org.h2.Driver"); + properties.setProperty("javax.persistence.jdbc.url", "jdbc:h2:file:./sql/xacmlTest"); + properties.setProperty("javax.persistence.jdbc.user", "sa"); + properties.setProperty("javax.persistence.jdbc.password", ""); + properties.setProperty("ping_interval", "500"); + properties.setProperty(PersistenceUnitProperties.ECLIPSELINK_PERSISTENCE_XML, "META-INF/persistencePUtest.xml"); + BackUpMonitor bum = BackUpMonitor.getInstance(BackUpMonitor.ResourceNode.BRMS.toString(), "brms_test" , properties, new Handler()); + createPolicyNotification(); + assertTrue(bum.getFlag()); + // Start a slave check. + startSlave(properties); + updatePolicyNotification(); + TimeUnit.MILLISECONDS.sleep(1500); + assertFalse(bum.getFlag()); + // Get Back to Master test + TimeUnit.MILLISECONDS.sleep(2000); + assertTrue(bum.getFlag()); + // No Master check. + changeALL(properties, "SLAVE"); + TimeUnit.MILLISECONDS.sleep(2000); + assertTrue(bum.getFlag()); + // No Master check. + changeALL(properties, "MASTER"); + TimeUnit.MILLISECONDS.sleep(2000); + assertTrue(bum.getFlag()); + + } + + private void updatePolicyNotification() { + StdPDPNotification notification = new StdPDPNotification(); + notification.setNotificationType(NotificationType.BOTH); + List<StdLoadedPolicy> loadedPolicies = new ArrayList<>(); + StdLoadedPolicy loadedPolicy = new StdLoadedPolicy(); + loadedPolicy.setPolicyName("com.testing"); + loadedPolicy.setUpdateType(UpdateType.UPDATE); + loadedPolicy.setVersionNo("2"); + Map<String, String> matches = new HashMap<>(); + matches.put("test", "test"); + loadedPolicy.setMatches(matches); + loadedPolicies.add(loadedPolicy); + notification.setLoadedPolicies(loadedPolicies); + List<StdRemovedPolicy> removedPolicies = new ArrayList<>(); + StdRemovedPolicy removedPolicy = new StdRemovedPolicy(); + removedPolicy.setPolicyName("com.testing"); + removedPolicy.setVersionNo("1"); + notification.setRemovedPolicies(removedPolicies); + NotificationStore.recordNotification(notification); + } + + private void createPolicyNotification() { + StdPDPNotification notification = new StdPDPNotification(); + notification.setNotificationType(NotificationType.UPDATE); + List<StdLoadedPolicy> loadedPolicies = new ArrayList<>(); + StdLoadedPolicy loadedPolicy = new StdLoadedPolicy(); + loadedPolicy.setPolicyName("com.testing"); + loadedPolicy.setUpdateType(UpdateType.NEW); + loadedPolicy.setVersionNo("1"); + Map<String, String> matches = new HashMap<>(); + matches.put("test", "test"); + loadedPolicy.setMatches(matches); + loadedPolicies.add(loadedPolicy); + notification.setLoadedPolicies(loadedPolicies); + NotificationStore.recordNotification(notification); + } + + private void changeALL(Properties properties, String flag) { + EntityManager em = Persistence.createEntityManagerFactory("PolicyEngineUtils", properties).createEntityManager(); + EntityTransaction et = em.getTransaction(); + et.begin(); + Query query = em.createQuery("select b from BackUpMonitorEntity b where b.resourceNodeName = :nn"); + query.setParameter("nn", ResourceNode.BRMS.toString()); + for(Object bMValue: query.getResultList()){ + BackUpMonitorEntity bmEntity = (BackUpMonitorEntity) bMValue; + bmEntity.setFlag(flag); + bmEntity.setTimeStamp(new Date()); + } + em.flush(); + et.commit(); + } + + private void startSlave(Properties properties) throws JsonProcessingException { + EntityManager em = Persistence.createEntityManagerFactory("PolicyEngineUtils", properties).createEntityManager(); + EntityTransaction et = em.getTransaction(); + et.begin(); + Query query = em.createQuery("select b from BackUpMonitorEntity b where b.resourceNodeName = :nn"); + query.setParameter("nn", ResourceNode.BRMS.toString()); + List<?> bMList = query.getResultList(); + BackUpMonitorEntity origBM = (BackUpMonitorEntity) bMList.get(0); + origBM.setFlag("SLAVE"); + origBM.setTimeStamp(new Date()); + BackUpMonitorEntity bMEntity = new BackUpMonitorEntity(); + bMEntity.setResoruceNodeName(ResourceNode.BRMS.toString()); + bMEntity.setResourceName("brms_test2"); + bMEntity.setFlag("MASTER"); + bMEntity.setTimeStamp(new Date()); + StdPDPNotification notification = new StdPDPNotification(); + notification.setNotificationType(NotificationType.UPDATE); + List<StdLoadedPolicy> loadedPolicies = new ArrayList<>(); + StdLoadedPolicy loadedPolicy = new StdLoadedPolicy(); + loadedPolicy.setPolicyName("com.test"); + loadedPolicy.setUpdateType(UpdateType.NEW); + loadedPolicy.setVersionNo("1"); + Map<String, String> matches = new HashMap<>(); + matches.put("test", "test"); + loadedPolicy.setMatches(matches); + loadedPolicies.add(loadedPolicy); + notification.setLoadedPolicies(loadedPolicies); + bMEntity.setNotificationRecord(PolicyUtils.objectToJsonString(notification)); + em.persist(bMEntity); + em.persist(origBM); + em.flush(); + et.commit(); + } + + @Test(expected = BackUpMonitorException.class) + public void testException() throws InterruptedException, BackUpMonitorException{ + BackUpMonitor.stop(); + new BackUpMonitorException(); + new BackUpMonitorException(new Exception()); + new BackUpMonitorException("error"); + new BackUpMonitorException("error", new Exception()); + throw new BackUpMonitorException("error", new Exception(), false, false); + } + + @After + public void setup() throws InterruptedException{ + BackUpMonitor.stop(); + } +} |