From 31e122b8c77b933db7475b2889efcfba83e378b5 Mon Sep 17 00:00:00 2001 From: Parshad Patel Date: Thu, 27 Dec 2018 19:52:56 +0900 Subject: Rename test classes in policy/engine Make test classes name consitence by adding 'Test' at end of junit test classes and adding 'Support' or 'Dummy' at start of util or dummy type of test classes Issue-ID: POLICY-1281 Change-Id: I5fa65d0cfc95edc8f2fe0ca678a43d2011a39670 Signed-off-by: Parshad Patel --- .../pdp/rest/api/test/GetConfigServiceTest.java | 70 ++++ .../rest/api/test/GetDictionaryServiceTest.java | 231 ++++++++++++ .../policy/pdp/rest/api/test/getConfigTest.java | 70 ---- .../pdp/rest/api/test/getDictionaryTest.java | 231 ------------ .../pdp/rest/jmx/DummyServerContextImpl.java | 399 +++++++++++++++++++++ .../pdp/rest/jmx/PdpRestMBeanListenerTest.java | 2 +- .../policy/pdp/rest/jmx/ServerContextImpl.java | 399 --------------------- .../policy/std/test/DummyNotificationHandler.java | 126 +++++++ .../java/org/onap/policy/std/test/Handler.java | 126 ------- .../onap/policy/utils/test/BackUpMonitorTest.java | 292 +++++++++++++++ .../onap/policy/utils/test/DummyBackUpHandler.java | 38 ++ .../java/org/onap/policy/utils/test/Handler.java | 38 -- .../onap/policy/utils/test/testBackUpMonitor.java | 292 --------------- 13 files changed, 1157 insertions(+), 1157 deletions(-) create mode 100644 ONAP-PDP-REST/src/test/java/org/onap/policy/pdp/rest/api/test/GetConfigServiceTest.java create mode 100644 ONAP-PDP-REST/src/test/java/org/onap/policy/pdp/rest/api/test/GetDictionaryServiceTest.java delete mode 100644 ONAP-PDP-REST/src/test/java/org/onap/policy/pdp/rest/api/test/getConfigTest.java delete mode 100644 ONAP-PDP-REST/src/test/java/org/onap/policy/pdp/rest/api/test/getDictionaryTest.java create mode 100644 ONAP-PDP-REST/src/test/java/org/onap/policy/pdp/rest/jmx/DummyServerContextImpl.java delete mode 100644 ONAP-PDP-REST/src/test/java/org/onap/policy/pdp/rest/jmx/ServerContextImpl.java create mode 100644 PolicyEngineAPI/src/test/java/org/onap/policy/std/test/DummyNotificationHandler.java delete mode 100644 PolicyEngineAPI/src/test/java/org/onap/policy/std/test/Handler.java create mode 100644 PolicyEngineUtils/src/test/java/org/onap/policy/utils/test/BackUpMonitorTest.java create mode 100644 PolicyEngineUtils/src/test/java/org/onap/policy/utils/test/DummyBackUpHandler.java delete mode 100644 PolicyEngineUtils/src/test/java/org/onap/policy/utils/test/Handler.java delete mode 100644 PolicyEngineUtils/src/test/java/org/onap/policy/utils/test/testBackUpMonitor.java diff --git a/ONAP-PDP-REST/src/test/java/org/onap/policy/pdp/rest/api/test/GetConfigServiceTest.java b/ONAP-PDP-REST/src/test/java/org/onap/policy/pdp/rest/api/test/GetConfigServiceTest.java new file mode 100644 index 000000000..a3c1b2b05 --- /dev/null +++ b/ONAP-PDP-REST/src/test/java/org/onap/policy/pdp/rest/api/test/GetConfigServiceTest.java @@ -0,0 +1,70 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP-PDP-REST + * ================================================================================ + * 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.pdp.rest.api.test; + +import static org.junit.Assert.assertEquals; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.Collection; +import java.util.HashMap; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; + +import org.junit.Test; +import org.onap.policy.api.ConfigRequestParameters; +import org.onap.policy.api.PolicyConfigStatus; +import org.onap.policy.pdp.rest.api.models.PolicyConfig; +import org.onap.policy.pdp.rest.api.services.GetConfigService; + +public class GetConfigServiceTest { + private static final String TEST = "test"; + + @SuppressWarnings("unchecked") + @Test + public void filterMethodTest() throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException{ + ConfigRequestParameters configRequestParameters = new ConfigRequestParameters(); + GetConfigService getConfigService= new GetConfigService(configRequestParameters, null); + Method filter = GetConfigService.class.getDeclaredMethod("filterResults", Collection.class,ConfigRequestParameters.class); + filter.setAccessible(true); + List policyConfigs = new LinkedList<>(); + + List filterResults = (List) filter.invoke(getConfigService, policyConfigs,configRequestParameters); + assertEquals(PolicyConfigStatus.CONFIG_NOT_FOUND, filterResults.get(0).getPolicyConfigStatus()); + // Check again with some values + configRequestParameters.setPolicyName(TEST); + configRequestParameters.setOnapName(TEST); + configRequestParameters.setConfigName(TEST); + Map configAttributes = new HashMap<>(); + configAttributes.put(TEST, TEST); + configRequestParameters.setConfigAttributes(configAttributes); + PolicyConfig pConfig = new PolicyConfig(); + pConfig.setPolicyName(TEST); + Map matching = new HashMap<>(); + matching.put("ONAPName", TEST); + matching.put("ConfigName", TEST); + matching.put("TEST", TEST); + pConfig.setMatchingConditions(matching); + policyConfigs.add(pConfig); + filterResults = (List) filter.invoke(getConfigService, policyConfigs,configRequestParameters); + assertEquals(PolicyConfigStatus.CONFIG_NOT_FOUND, filterResults.get(0).getPolicyConfigStatus()); + } +} diff --git a/ONAP-PDP-REST/src/test/java/org/onap/policy/pdp/rest/api/test/GetDictionaryServiceTest.java b/ONAP-PDP-REST/src/test/java/org/onap/policy/pdp/rest/api/test/GetDictionaryServiceTest.java new file mode 100644 index 000000000..b5706a8ed --- /dev/null +++ b/ONAP-PDP-REST/src/test/java/org/onap/policy/pdp/rest/api/test/GetDictionaryServiceTest.java @@ -0,0 +1,231 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP-PDP-REST + * ================================================================================ + * 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.pdp.rest.api.test; + +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; + +import java.lang.reflect.Method; + +import org.junit.Test; +import org.onap.policy.api.DictionaryParameters; +import org.onap.policy.pdp.rest.api.services.GetDictionaryService; + +public class GetDictionaryServiceTest { + + @Test + public void dictionaryJsonTest() throws Exception{ + Method formatDictionary = GetDictionaryService.class.getDeclaredMethod("formatDictionaryJson", String.class); + formatDictionary.setAccessible(true); + String input="{\"key\":\"value\",\"key\":\"value\",\"key\":\"value\",\"key\":\"value\",\"key\":\"value\",\"key\":\"value\",\"key\":\"value\",\"key\":\"value\",\"key\":\"value\",\"key\":\"value\",\"key\":\"value\"," + + "\"key\":\"value\",\"key\":\"value\",\"key\":\"value\",\"key\":\"value\",\"key\":\"value\",\"key\":\"value\",\"key\":\"value\",\"key\":\"value\",\"key\":\"value\",\"key\":\"value\",\"key\":\"value\",\"key\":\"value\"," + + "\"key\":\"value\",\"key\":\"value\",\"key\":\"value\",\"key\":\"value\",\"key\":\"value\"}"; + DictionaryParameters dp = new DictionaryParameters(); + dp.setDictionary("test"); + GetDictionaryService gds = new GetDictionaryService(dp, null); + String result = (String) formatDictionary.invoke(gds, input); + assertNotNull(result); + // + dp.setDictionary("OnapName"); + gds = new GetDictionaryService(dp, null); + result = (String) formatDictionary.invoke(gds, input); + assertNotNull(result); + // + dp.setDictionary("Attribute"); + gds = new GetDictionaryService(dp, null); + result = (String) formatDictionary.invoke(gds, input); + assertNotNull(result); + // + dp.setDictionary("Action"); + gds = new GetDictionaryService(dp, null); + result = (String) formatDictionary.invoke(gds, input); + assertNotNull(result); + // + dp.setDictionary("BRMSParamTemplate"); + gds = new GetDictionaryService(dp, null); + result = (String) formatDictionary.invoke(gds, input); + assertNotNull(result); + // + dp.setDictionary("VSCLAction"); + gds = new GetDictionaryService(dp, null); + result = (String) formatDictionary.invoke(gds, input); + assertNotNull(result); + // + dp.setDictionary("VNFType"); + gds = new GetDictionaryService(dp, null); + result = (String) formatDictionary.invoke(gds, input); + assertNotNull(result); + // + dp.setDictionary("PEPOptions"); + gds = new GetDictionaryService(dp, null); + result = (String) formatDictionary.invoke(gds, input); + assertNotNull(result); + // + dp.setDictionary("Varbind"); + gds = new GetDictionaryService(dp, null); + result = (String) formatDictionary.invoke(gds, input); + assertNotNull(result); + // + dp.setDictionary("Service"); + gds = new GetDictionaryService(dp, null); + result = (String) formatDictionary.invoke(gds, input); + assertNotNull(result); + // + dp.setDictionary("Site"); + gds = new GetDictionaryService(dp, null); + result = (String) formatDictionary.invoke(gds, input); + assertNotNull(result); + // + dp.setDictionary("Settings"); + gds = new GetDictionaryService(dp, null); + result = (String) formatDictionary.invoke(gds, input); + assertNotNull(result); + // + dp.setDictionary("DescriptiveScope"); + gds = new GetDictionaryService(dp, null); + result = (String) formatDictionary.invoke(gds, input); + assertNotNull(result); + // + dp.setDictionary("Enforcer"); + gds = new GetDictionaryService(dp, null); + result = (String) formatDictionary.invoke(gds, input); + assertNotNull(result); + // + dp.setDictionary("ActionList"); + gds = new GetDictionaryService(dp, null); + result = (String) formatDictionary.invoke(gds, input); + assertNotNull(result); + // + dp.setDictionary("ProtocolList"); + gds = new GetDictionaryService(dp, null); + result = (String) formatDictionary.invoke(gds, input); + assertNotNull(result); + // + dp.setDictionary("Zone"); + gds = new GetDictionaryService(dp, null); + result = (String) formatDictionary.invoke(gds, input); + assertNotNull(result); + // + dp.setDictionary("SecurityZone"); + gds = new GetDictionaryService(dp, null); + result = (String) formatDictionary.invoke(gds, input); + assertNotNull(result); + // + dp.setDictionary("PrefixList"); + gds = new GetDictionaryService(dp, null); + result = (String) formatDictionary.invoke(gds, input); + assertNotNull(result); + // + dp.setDictionary("AddressGroup"); + gds = new GetDictionaryService(dp, null); + result = (String) formatDictionary.invoke(gds, input); + assertNotNull(result); + // + dp.setDictionary("ServiceGroup"); + gds = new GetDictionaryService(dp, null); + result = (String) formatDictionary.invoke(gds, input); + assertNotNull(result); + // + dp.setDictionary("ServiceList"); + gds = new GetDictionaryService(dp, null); + result = (String) formatDictionary.invoke(gds, input); + assertNotNull(result); + // + dp.setDictionary("TermList"); + gds = new GetDictionaryService(dp, null); + result = (String) formatDictionary.invoke(gds, input); + assertNotNull(result); + // + dp.setDictionary("RuleList"); + gds = new GetDictionaryService(dp, null); + result = (String) formatDictionary.invoke(gds, input); + assertNotNull(result); + // + dp.setDictionary("FirewallRuleList"); + gds = new GetDictionaryService(dp, null); + result = (String) formatDictionary.invoke(gds, input); + assertNotNull(result); + // + dp.setDictionary("Term"); + gds = new GetDictionaryService(dp, null); + result = (String) formatDictionary.invoke(gds, input); + assertNotNull(result); + // + dp.setDictionary("MicroServiceLocation"); + gds = new GetDictionaryService(dp, null); + result = (String) formatDictionary.invoke(gds, input); + assertNotNull(result); + // + dp.setDictionary("MicroServiceConfigName"); + gds = new GetDictionaryService(dp, null); + result = (String) formatDictionary.invoke(gds, input); + assertNotNull(result); + // + dp.setDictionary("DCAEUUID"); + gds = new GetDictionaryService(dp, null); + result = (String) formatDictionary.invoke(gds, input); + assertNotNull(result); + // + dp.setDictionary("MicroServiceModels"); + gds = new GetDictionaryService(dp, null); + result = (String) formatDictionary.invoke(gds, input); + assertNotNull(result); + // + dp.setDictionary("PolicyScopeService"); + gds = new GetDictionaryService(dp, null); + result = (String) formatDictionary.invoke(gds, input); + assertNotNull(result); + // + dp.setDictionary("PolicyScopeResource"); + gds = new GetDictionaryService(dp, null); + result = (String) formatDictionary.invoke(gds, input); + assertNotNull(result); + // + dp.setDictionary("PolicyScopeType"); + gds = new GetDictionaryService(dp, null); + result = (String) formatDictionary.invoke(gds, input); + assertNotNull(result); + // + dp.setDictionary("PolicyScopeClosedLoop"); + gds = new GetDictionaryService(dp, null); + result = (String) formatDictionary.invoke(gds, input); + assertNotNull(result); + // + dp.setDictionary("GroupPolicyScopeList"); + gds = new GetDictionaryService(dp, null); + result = (String) formatDictionary.invoke(gds, input); + assertNotNull(result); + // + dp.setDictionary("RiskType"); + gds = new GetDictionaryService(dp, null); + result = (String) formatDictionary.invoke(gds, input); + assertNotNull(result); + // + dp.setDictionary("SafePolicyWarning"); + gds = new GetDictionaryService(dp, null); + result = (String) formatDictionary.invoke(gds, input); + assertNotNull(result); + // + dp.setDictionary("MicroServiceDictionary"); + gds = new GetDictionaryService(dp, null); + result = (String) formatDictionary.invoke(gds, input); + assertNotNull(result); + } +} diff --git a/ONAP-PDP-REST/src/test/java/org/onap/policy/pdp/rest/api/test/getConfigTest.java b/ONAP-PDP-REST/src/test/java/org/onap/policy/pdp/rest/api/test/getConfigTest.java deleted file mode 100644 index 2d79b234a..000000000 --- a/ONAP-PDP-REST/src/test/java/org/onap/policy/pdp/rest/api/test/getConfigTest.java +++ /dev/null @@ -1,70 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * ONAP-PDP-REST - * ================================================================================ - * 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.pdp.rest.api.test; - -import static org.junit.Assert.assertEquals; - -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; -import java.util.Collection; -import java.util.HashMap; -import java.util.LinkedList; -import java.util.List; -import java.util.Map; - -import org.junit.Test; -import org.onap.policy.api.ConfigRequestParameters; -import org.onap.policy.api.PolicyConfigStatus; -import org.onap.policy.pdp.rest.api.models.PolicyConfig; -import org.onap.policy.pdp.rest.api.services.GetConfigService; - -public class getConfigTest { - private static final String TEST = "test"; - - @SuppressWarnings("unchecked") - @Test - public void filterMethodTest() throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException{ - ConfigRequestParameters configRequestParameters = new ConfigRequestParameters(); - GetConfigService getConfigService= new GetConfigService(configRequestParameters, null); - Method filter = GetConfigService.class.getDeclaredMethod("filterResults", Collection.class,ConfigRequestParameters.class); - filter.setAccessible(true); - List policyConfigs = new LinkedList<>(); - - List filterResults = (List) filter.invoke(getConfigService, policyConfigs,configRequestParameters); - assertEquals(PolicyConfigStatus.CONFIG_NOT_FOUND, filterResults.get(0).getPolicyConfigStatus()); - // Check again with some values - configRequestParameters.setPolicyName(TEST); - configRequestParameters.setOnapName(TEST); - configRequestParameters.setConfigName(TEST); - Map configAttributes = new HashMap<>(); - configAttributes.put(TEST, TEST); - configRequestParameters.setConfigAttributes(configAttributes); - PolicyConfig pConfig = new PolicyConfig(); - pConfig.setPolicyName(TEST); - Map matching = new HashMap<>(); - matching.put("ONAPName", TEST); - matching.put("ConfigName", TEST); - matching.put("TEST", TEST); - pConfig.setMatchingConditions(matching); - policyConfigs.add(pConfig); - filterResults = (List) filter.invoke(getConfigService, policyConfigs,configRequestParameters); - assertEquals(PolicyConfigStatus.CONFIG_NOT_FOUND, filterResults.get(0).getPolicyConfigStatus()); - } -} diff --git a/ONAP-PDP-REST/src/test/java/org/onap/policy/pdp/rest/api/test/getDictionaryTest.java b/ONAP-PDP-REST/src/test/java/org/onap/policy/pdp/rest/api/test/getDictionaryTest.java deleted file mode 100644 index 38dafee35..000000000 --- a/ONAP-PDP-REST/src/test/java/org/onap/policy/pdp/rest/api/test/getDictionaryTest.java +++ /dev/null @@ -1,231 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * ONAP-PDP-REST - * ================================================================================ - * 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.pdp.rest.api.test; - -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNull; - -import java.lang.reflect.Method; - -import org.junit.Test; -import org.onap.policy.api.DictionaryParameters; -import org.onap.policy.pdp.rest.api.services.GetDictionaryService; - -public class getDictionaryTest { - - @Test - public void dictionaryJsonTest() throws Exception{ - Method formatDictionary = GetDictionaryService.class.getDeclaredMethod("formatDictionaryJson", String.class); - formatDictionary.setAccessible(true); - String input="{\"key\":\"value\",\"key\":\"value\",\"key\":\"value\",\"key\":\"value\",\"key\":\"value\",\"key\":\"value\",\"key\":\"value\",\"key\":\"value\",\"key\":\"value\",\"key\":\"value\",\"key\":\"value\"," - + "\"key\":\"value\",\"key\":\"value\",\"key\":\"value\",\"key\":\"value\",\"key\":\"value\",\"key\":\"value\",\"key\":\"value\",\"key\":\"value\",\"key\":\"value\",\"key\":\"value\",\"key\":\"value\",\"key\":\"value\"," - + "\"key\":\"value\",\"key\":\"value\",\"key\":\"value\",\"key\":\"value\",\"key\":\"value\"}"; - DictionaryParameters dp = new DictionaryParameters(); - dp.setDictionary("test"); - GetDictionaryService gds = new GetDictionaryService(dp, null); - String result = (String) formatDictionary.invoke(gds, input); - assertNotNull(result); - // - dp.setDictionary("OnapName"); - gds = new GetDictionaryService(dp, null); - result = (String) formatDictionary.invoke(gds, input); - assertNotNull(result); - // - dp.setDictionary("Attribute"); - gds = new GetDictionaryService(dp, null); - result = (String) formatDictionary.invoke(gds, input); - assertNotNull(result); - // - dp.setDictionary("Action"); - gds = new GetDictionaryService(dp, null); - result = (String) formatDictionary.invoke(gds, input); - assertNotNull(result); - // - dp.setDictionary("BRMSParamTemplate"); - gds = new GetDictionaryService(dp, null); - result = (String) formatDictionary.invoke(gds, input); - assertNotNull(result); - // - dp.setDictionary("VSCLAction"); - gds = new GetDictionaryService(dp, null); - result = (String) formatDictionary.invoke(gds, input); - assertNotNull(result); - // - dp.setDictionary("VNFType"); - gds = new GetDictionaryService(dp, null); - result = (String) formatDictionary.invoke(gds, input); - assertNotNull(result); - // - dp.setDictionary("PEPOptions"); - gds = new GetDictionaryService(dp, null); - result = (String) formatDictionary.invoke(gds, input); - assertNotNull(result); - // - dp.setDictionary("Varbind"); - gds = new GetDictionaryService(dp, null); - result = (String) formatDictionary.invoke(gds, input); - assertNotNull(result); - // - dp.setDictionary("Service"); - gds = new GetDictionaryService(dp, null); - result = (String) formatDictionary.invoke(gds, input); - assertNotNull(result); - // - dp.setDictionary("Site"); - gds = new GetDictionaryService(dp, null); - result = (String) formatDictionary.invoke(gds, input); - assertNotNull(result); - // - dp.setDictionary("Settings"); - gds = new GetDictionaryService(dp, null); - result = (String) formatDictionary.invoke(gds, input); - assertNotNull(result); - // - dp.setDictionary("DescriptiveScope"); - gds = new GetDictionaryService(dp, null); - result = (String) formatDictionary.invoke(gds, input); - assertNotNull(result); - // - dp.setDictionary("Enforcer"); - gds = new GetDictionaryService(dp, null); - result = (String) formatDictionary.invoke(gds, input); - assertNotNull(result); - // - dp.setDictionary("ActionList"); - gds = new GetDictionaryService(dp, null); - result = (String) formatDictionary.invoke(gds, input); - assertNotNull(result); - // - dp.setDictionary("ProtocolList"); - gds = new GetDictionaryService(dp, null); - result = (String) formatDictionary.invoke(gds, input); - assertNotNull(result); - // - dp.setDictionary("Zone"); - gds = new GetDictionaryService(dp, null); - result = (String) formatDictionary.invoke(gds, input); - assertNotNull(result); - // - dp.setDictionary("SecurityZone"); - gds = new GetDictionaryService(dp, null); - result = (String) formatDictionary.invoke(gds, input); - assertNotNull(result); - // - dp.setDictionary("PrefixList"); - gds = new GetDictionaryService(dp, null); - result = (String) formatDictionary.invoke(gds, input); - assertNotNull(result); - // - dp.setDictionary("AddressGroup"); - gds = new GetDictionaryService(dp, null); - result = (String) formatDictionary.invoke(gds, input); - assertNotNull(result); - // - dp.setDictionary("ServiceGroup"); - gds = new GetDictionaryService(dp, null); - result = (String) formatDictionary.invoke(gds, input); - assertNotNull(result); - // - dp.setDictionary("ServiceList"); - gds = new GetDictionaryService(dp, null); - result = (String) formatDictionary.invoke(gds, input); - assertNotNull(result); - // - dp.setDictionary("TermList"); - gds = new GetDictionaryService(dp, null); - result = (String) formatDictionary.invoke(gds, input); - assertNotNull(result); - // - dp.setDictionary("RuleList"); - gds = new GetDictionaryService(dp, null); - result = (String) formatDictionary.invoke(gds, input); - assertNotNull(result); - // - dp.setDictionary("FirewallRuleList"); - gds = new GetDictionaryService(dp, null); - result = (String) formatDictionary.invoke(gds, input); - assertNotNull(result); - // - dp.setDictionary("Term"); - gds = new GetDictionaryService(dp, null); - result = (String) formatDictionary.invoke(gds, input); - assertNotNull(result); - // - dp.setDictionary("MicroServiceLocation"); - gds = new GetDictionaryService(dp, null); - result = (String) formatDictionary.invoke(gds, input); - assertNotNull(result); - // - dp.setDictionary("MicroServiceConfigName"); - gds = new GetDictionaryService(dp, null); - result = (String) formatDictionary.invoke(gds, input); - assertNotNull(result); - // - dp.setDictionary("DCAEUUID"); - gds = new GetDictionaryService(dp, null); - result = (String) formatDictionary.invoke(gds, input); - assertNotNull(result); - // - dp.setDictionary("MicroServiceModels"); - gds = new GetDictionaryService(dp, null); - result = (String) formatDictionary.invoke(gds, input); - assertNotNull(result); - // - dp.setDictionary("PolicyScopeService"); - gds = new GetDictionaryService(dp, null); - result = (String) formatDictionary.invoke(gds, input); - assertNotNull(result); - // - dp.setDictionary("PolicyScopeResource"); - gds = new GetDictionaryService(dp, null); - result = (String) formatDictionary.invoke(gds, input); - assertNotNull(result); - // - dp.setDictionary("PolicyScopeType"); - gds = new GetDictionaryService(dp, null); - result = (String) formatDictionary.invoke(gds, input); - assertNotNull(result); - // - dp.setDictionary("PolicyScopeClosedLoop"); - gds = new GetDictionaryService(dp, null); - result = (String) formatDictionary.invoke(gds, input); - assertNotNull(result); - // - dp.setDictionary("GroupPolicyScopeList"); - gds = new GetDictionaryService(dp, null); - result = (String) formatDictionary.invoke(gds, input); - assertNotNull(result); - // - dp.setDictionary("RiskType"); - gds = new GetDictionaryService(dp, null); - result = (String) formatDictionary.invoke(gds, input); - assertNotNull(result); - // - dp.setDictionary("SafePolicyWarning"); - gds = new GetDictionaryService(dp, null); - result = (String) formatDictionary.invoke(gds, input); - assertNotNull(result); - // - dp.setDictionary("MicroServiceDictionary"); - gds = new GetDictionaryService(dp, null); - result = (String) formatDictionary.invoke(gds, input); - assertNotNull(result); - } -} diff --git a/ONAP-PDP-REST/src/test/java/org/onap/policy/pdp/rest/jmx/DummyServerContextImpl.java b/ONAP-PDP-REST/src/test/java/org/onap/policy/pdp/rest/jmx/DummyServerContextImpl.java new file mode 100644 index 000000000..056b73199 --- /dev/null +++ b/ONAP-PDP-REST/src/test/java/org/onap/policy/pdp/rest/jmx/DummyServerContextImpl.java @@ -0,0 +1,399 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP-PDP-REST + * ================================================================================ + * 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.pdp.rest.jmx; + +import java.io.InputStream; +import java.net.MalformedURLException; +import java.net.URL; +import java.util.Enumeration; +import java.util.EventListener; +import java.util.Map; +import java.util.Set; + +import javax.servlet.Filter; +import javax.servlet.FilterRegistration; +import javax.servlet.RequestDispatcher; +import javax.servlet.Servlet; +import javax.servlet.ServletContext; +import javax.servlet.ServletException; +import javax.servlet.ServletRegistration; +import javax.servlet.ServletRegistration.Dynamic; +import javax.servlet.SessionCookieConfig; +import javax.servlet.SessionTrackingMode; +import javax.servlet.descriptor.JspConfigDescriptor; + +public class DummyServerContextImpl implements ServletContext { + + @Override + public String getContextPath() { + // TODO Auto-generated method stub + return null; + } + + @Override + public ServletContext getContext(String uripath) { + // TODO Auto-generated method stub + return null; + } + + @Override + public int getMajorVersion() { + // TODO Auto-generated method stub + return 0; + } + + @Override + public int getMinorVersion() { + // TODO Auto-generated method stub + return 0; + } + + @Override + public int getEffectiveMajorVersion() { + // TODO Auto-generated method stub + return 0; + } + + @Override + public int getEffectiveMinorVersion() { + // TODO Auto-generated method stub + return 0; + } + + @Override + public String getMimeType(String file) { + // TODO Auto-generated method stub + return null; + } + + @Override + public Set getResourcePaths(String path) { + // TODO Auto-generated method stub + return null; + } + + @Override + public URL getResource(String path) throws MalformedURLException { + // TODO Auto-generated method stub + return null; + } + + @Override + public InputStream getResourceAsStream(String path) { + // TODO Auto-generated method stub + return null; + } + + @Override + public RequestDispatcher getRequestDispatcher(String path) { + // TODO Auto-generated method stub + return null; + } + + @Override + public RequestDispatcher getNamedDispatcher(String name) { + // TODO Auto-generated method stub + return null; + } + + @Override + public Servlet getServlet(String name) throws ServletException { + // TODO Auto-generated method stub + return null; + } + + @Override + public Enumeration getServlets() { + // TODO Auto-generated method stub + return null; + } + + @Override + public Enumeration getServletNames() { + // TODO Auto-generated method stub + return null; + } + + @Override + public void log(String msg) { + // TODO Auto-generated method stub + + } + + @Override + public void log(Exception exception, String msg) { + // TODO Auto-generated method stub + + } + + @Override + public void log(String message, Throwable throwable) { + // TODO Auto-generated method stub + + } + + @Override + public String getRealPath(String path) { + // TODO Auto-generated method stub + return null; + } + + @Override + public String getServerInfo() { + // TODO Auto-generated method stub + return null; + } + + @Override + public String getInitParameter(String name) { + // TODO Auto-generated method stub + return null; + } + + @Override + public Enumeration getInitParameterNames() { + // TODO Auto-generated method stub + return null; + } + + @Override + public boolean setInitParameter(String name, String value) { + // TODO Auto-generated method stub + return false; + } + + @Override + public Object getAttribute(String name) { + // TODO Auto-generated method stub + return null; + } + + @Override + public Enumeration getAttributeNames() { + // TODO Auto-generated method stub + return null; + } + + @Override + public void setAttribute(String name, Object object) { + // TODO Auto-generated method stub + + } + + @Override + public void removeAttribute(String name) { + // TODO Auto-generated method stub + + } + + @Override + public String getServletContextName() { + // TODO Auto-generated method stub + return null; + } + + @Override + public Dynamic addServlet(String servletName, String className) { + // TODO Auto-generated method stub + return null; + } + + @Override + public Dynamic addServlet(String servletName, Servlet servlet) { + // TODO Auto-generated method stub + return null; + } + + @Override + public Dynamic addServlet(String servletName, Class servletClass) { + // TODO Auto-generated method stub + return null; + } + + @Override + public T createServlet(Class clazz) throws ServletException { + // TODO Auto-generated method stub + return null; + } + + @Override + public ServletRegistration getServletRegistration(String servletName) { + // TODO Auto-generated method stub + return null; + } + + @Override + public Map getServletRegistrations() { + // TODO Auto-generated method stub + return null; + } + + @Override + public javax.servlet.FilterRegistration.Dynamic addFilter(String filterName, String className) { + // TODO Auto-generated method stub + return null; + } + + @Override + public javax.servlet.FilterRegistration.Dynamic addFilter(String filterName, Filter filter) { + // TODO Auto-generated method stub + return null; + } + + @Override + public javax.servlet.FilterRegistration.Dynamic addFilter( + String filterName, Class filterClass) { + // TODO Auto-generated method stub + return null; + } + + @Override + public T createFilter(Class clazz) throws ServletException { + // TODO Auto-generated method stub + return null; + } + + @Override + public FilterRegistration getFilterRegistration(String filterName) { + // TODO Auto-generated method stub + return null; + } + + @Override + public Map getFilterRegistrations() { + // TODO Auto-generated method stub + return null; + } + + @Override + public SessionCookieConfig getSessionCookieConfig() { + // TODO Auto-generated method stub + return null; + } + + @Override + public void setSessionTrackingModes(Set sessionTrackingModes) { + // TODO Auto-generated method stub + + } + + @Override + public Set getDefaultSessionTrackingModes() { + // TODO Auto-generated method stub + return null; + } + + @Override + public Set getEffectiveSessionTrackingModes() { + // TODO Auto-generated method stub + return null; + } + + @Override + public void addListener(String className) { + // TODO Auto-generated method stub + + } + + @Override + public void addListener(T t) { + // TODO Auto-generated method stub + + } + + @Override + public void addListener(Class listenerClass) { + // TODO Auto-generated method stub + + } + + @Override + public T createListener(Class clazz) throws ServletException { + // TODO Auto-generated method stub + return null; + } + + @Override + public JspConfigDescriptor getJspConfigDescriptor() { + // TODO Auto-generated method stub + return null; + } + + @Override + public ClassLoader getClassLoader() { + // TODO Auto-generated method stub + return null; + } + + @Override + public void declareRoles(String... roleNames) { + // TODO Auto-generated method stub + + } + + @Override + public String getVirtualServerName() { + // TODO Auto-generated method stub + return null; + } + + @Override + public Dynamic addJspFile(String arg0, String arg1) { + // TODO Auto-generated method stub + return null; + } + + @Override + public String getRequestCharacterEncoding() { + // TODO Auto-generated method stub + return null; + } + + @Override + public String getResponseCharacterEncoding() { + // TODO Auto-generated method stub + return null; + } + + @Override + public int getSessionTimeout() { + // TODO Auto-generated method stub + return 0; + } + + @Override + public void setRequestCharacterEncoding(String arg0) { + // TODO Auto-generated method stub + + } + + @Override + public void setResponseCharacterEncoding(String arg0) { + // TODO Auto-generated method stub + + } + + @Override + public void setSessionTimeout(int arg0) { + // TODO Auto-generated method stub + + } +} diff --git a/ONAP-PDP-REST/src/test/java/org/onap/policy/pdp/rest/jmx/PdpRestMBeanListenerTest.java b/ONAP-PDP-REST/src/test/java/org/onap/policy/pdp/rest/jmx/PdpRestMBeanListenerTest.java index ea689e663..59e14db1e 100644 --- a/ONAP-PDP-REST/src/test/java/org/onap/policy/pdp/rest/jmx/PdpRestMBeanListenerTest.java +++ b/ONAP-PDP-REST/src/test/java/org/onap/policy/pdp/rest/jmx/PdpRestMBeanListenerTest.java @@ -37,7 +37,7 @@ public class PdpRestMBeanListenerTest { try { PdpRestMBeanListener listener = new PdpRestMBeanListener(); - ServerContextImpl source = new ServerContextImpl(); + DummyServerContextImpl source = new DummyServerContextImpl(); ServletContextEvent event = new ServletContextEvent(source); listener.contextInitialized(event); diff --git a/ONAP-PDP-REST/src/test/java/org/onap/policy/pdp/rest/jmx/ServerContextImpl.java b/ONAP-PDP-REST/src/test/java/org/onap/policy/pdp/rest/jmx/ServerContextImpl.java deleted file mode 100644 index 2f8683c5c..000000000 --- a/ONAP-PDP-REST/src/test/java/org/onap/policy/pdp/rest/jmx/ServerContextImpl.java +++ /dev/null @@ -1,399 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * ONAP-PDP-REST - * ================================================================================ - * 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.pdp.rest.jmx; - -import java.io.InputStream; -import java.net.MalformedURLException; -import java.net.URL; -import java.util.Enumeration; -import java.util.EventListener; -import java.util.Map; -import java.util.Set; - -import javax.servlet.Filter; -import javax.servlet.FilterRegistration; -import javax.servlet.RequestDispatcher; -import javax.servlet.Servlet; -import javax.servlet.ServletContext; -import javax.servlet.ServletException; -import javax.servlet.ServletRegistration; -import javax.servlet.ServletRegistration.Dynamic; -import javax.servlet.SessionCookieConfig; -import javax.servlet.SessionTrackingMode; -import javax.servlet.descriptor.JspConfigDescriptor; - -public class ServerContextImpl implements ServletContext { - - @Override - public String getContextPath() { - // TODO Auto-generated method stub - return null; - } - - @Override - public ServletContext getContext(String uripath) { - // TODO Auto-generated method stub - return null; - } - - @Override - public int getMajorVersion() { - // TODO Auto-generated method stub - return 0; - } - - @Override - public int getMinorVersion() { - // TODO Auto-generated method stub - return 0; - } - - @Override - public int getEffectiveMajorVersion() { - // TODO Auto-generated method stub - return 0; - } - - @Override - public int getEffectiveMinorVersion() { - // TODO Auto-generated method stub - return 0; - } - - @Override - public String getMimeType(String file) { - // TODO Auto-generated method stub - return null; - } - - @Override - public Set getResourcePaths(String path) { - // TODO Auto-generated method stub - return null; - } - - @Override - public URL getResource(String path) throws MalformedURLException { - // TODO Auto-generated method stub - return null; - } - - @Override - public InputStream getResourceAsStream(String path) { - // TODO Auto-generated method stub - return null; - } - - @Override - public RequestDispatcher getRequestDispatcher(String path) { - // TODO Auto-generated method stub - return null; - } - - @Override - public RequestDispatcher getNamedDispatcher(String name) { - // TODO Auto-generated method stub - return null; - } - - @Override - public Servlet getServlet(String name) throws ServletException { - // TODO Auto-generated method stub - return null; - } - - @Override - public Enumeration getServlets() { - // TODO Auto-generated method stub - return null; - } - - @Override - public Enumeration getServletNames() { - // TODO Auto-generated method stub - return null; - } - - @Override - public void log(String msg) { - // TODO Auto-generated method stub - - } - - @Override - public void log(Exception exception, String msg) { - // TODO Auto-generated method stub - - } - - @Override - public void log(String message, Throwable throwable) { - // TODO Auto-generated method stub - - } - - @Override - public String getRealPath(String path) { - // TODO Auto-generated method stub - return null; - } - - @Override - public String getServerInfo() { - // TODO Auto-generated method stub - return null; - } - - @Override - public String getInitParameter(String name) { - // TODO Auto-generated method stub - return null; - } - - @Override - public Enumeration getInitParameterNames() { - // TODO Auto-generated method stub - return null; - } - - @Override - public boolean setInitParameter(String name, String value) { - // TODO Auto-generated method stub - return false; - } - - @Override - public Object getAttribute(String name) { - // TODO Auto-generated method stub - return null; - } - - @Override - public Enumeration getAttributeNames() { - // TODO Auto-generated method stub - return null; - } - - @Override - public void setAttribute(String name, Object object) { - // TODO Auto-generated method stub - - } - - @Override - public void removeAttribute(String name) { - // TODO Auto-generated method stub - - } - - @Override - public String getServletContextName() { - // TODO Auto-generated method stub - return null; - } - - @Override - public Dynamic addServlet(String servletName, String className) { - // TODO Auto-generated method stub - return null; - } - - @Override - public Dynamic addServlet(String servletName, Servlet servlet) { - // TODO Auto-generated method stub - return null; - } - - @Override - public Dynamic addServlet(String servletName, Class servletClass) { - // TODO Auto-generated method stub - return null; - } - - @Override - public T createServlet(Class clazz) throws ServletException { - // TODO Auto-generated method stub - return null; - } - - @Override - public ServletRegistration getServletRegistration(String servletName) { - // TODO Auto-generated method stub - return null; - } - - @Override - public Map getServletRegistrations() { - // TODO Auto-generated method stub - return null; - } - - @Override - public javax.servlet.FilterRegistration.Dynamic addFilter(String filterName, String className) { - // TODO Auto-generated method stub - return null; - } - - @Override - public javax.servlet.FilterRegistration.Dynamic addFilter(String filterName, Filter filter) { - // TODO Auto-generated method stub - return null; - } - - @Override - public javax.servlet.FilterRegistration.Dynamic addFilter( - String filterName, Class filterClass) { - // TODO Auto-generated method stub - return null; - } - - @Override - public T createFilter(Class clazz) throws ServletException { - // TODO Auto-generated method stub - return null; - } - - @Override - public FilterRegistration getFilterRegistration(String filterName) { - // TODO Auto-generated method stub - return null; - } - - @Override - public Map getFilterRegistrations() { - // TODO Auto-generated method stub - return null; - } - - @Override - public SessionCookieConfig getSessionCookieConfig() { - // TODO Auto-generated method stub - return null; - } - - @Override - public void setSessionTrackingModes(Set sessionTrackingModes) { - // TODO Auto-generated method stub - - } - - @Override - public Set getDefaultSessionTrackingModes() { - // TODO Auto-generated method stub - return null; - } - - @Override - public Set getEffectiveSessionTrackingModes() { - // TODO Auto-generated method stub - return null; - } - - @Override - public void addListener(String className) { - // TODO Auto-generated method stub - - } - - @Override - public void addListener(T t) { - // TODO Auto-generated method stub - - } - - @Override - public void addListener(Class listenerClass) { - // TODO Auto-generated method stub - - } - - @Override - public T createListener(Class clazz) throws ServletException { - // TODO Auto-generated method stub - return null; - } - - @Override - public JspConfigDescriptor getJspConfigDescriptor() { - // TODO Auto-generated method stub - return null; - } - - @Override - public ClassLoader getClassLoader() { - // TODO Auto-generated method stub - return null; - } - - @Override - public void declareRoles(String... roleNames) { - // TODO Auto-generated method stub - - } - - @Override - public String getVirtualServerName() { - // TODO Auto-generated method stub - return null; - } - - @Override - public Dynamic addJspFile(String arg0, String arg1) { - // TODO Auto-generated method stub - return null; - } - - @Override - public String getRequestCharacterEncoding() { - // TODO Auto-generated method stub - return null; - } - - @Override - public String getResponseCharacterEncoding() { - // TODO Auto-generated method stub - return null; - } - - @Override - public int getSessionTimeout() { - // TODO Auto-generated method stub - return 0; - } - - @Override - public void setRequestCharacterEncoding(String arg0) { - // TODO Auto-generated method stub - - } - - @Override - public void setResponseCharacterEncoding(String arg0) { - // TODO Auto-generated method stub - - } - - @Override - public void setSessionTimeout(int arg0) { - // TODO Auto-generated method stub - - } -} diff --git a/PolicyEngineAPI/src/test/java/org/onap/policy/std/test/DummyNotificationHandler.java b/PolicyEngineAPI/src/test/java/org/onap/policy/std/test/DummyNotificationHandler.java new file mode 100644 index 000000000..cdc717597 --- /dev/null +++ b/PolicyEngineAPI/src/test/java/org/onap/policy/std/test/DummyNotificationHandler.java @@ -0,0 +1,126 @@ +/*- + * ============LICENSE_START======================================================= + * PolicyEngineAPI + * ================================================================================ + * 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.std.test; + +import java.util.Collection; + +import org.onap.policy.api.LoadedPolicy; +import org.onap.policy.api.NotificationHandler; +import org.onap.policy.api.NotificationType; +import org.onap.policy.api.PDPNotification; +import org.onap.policy.api.PolicyConfig; +import org.onap.policy.api.PolicyConfigException; +import org.onap.policy.api.PolicyConfigStatus; +import org.onap.policy.api.PolicyEngine; +import org.onap.policy.api.PolicyEngineException; +import org.onap.policy.api.RemovedPolicy; +import org.onap.policy.common.logging.flexlogger.FlexLogger; +import org.onap.policy.common.logging.flexlogger.Logger; + +public class DummyNotificationHandler implements NotificationHandler{ + + private static final Logger LOGGER = FlexLogger.getLogger(DummyNotificationHandler.class); + + @Override + public void notificationReceived(PDPNotification notification) { + System.out.println("Notification Received..."); + System.out.println(notification.getNotificationType()); + if(notification.getNotificationType().equals(NotificationType.REMOVE)){ + System.out.println("Removed Policies: \n"); + for(RemovedPolicy removedPolicy: notification.getRemovedPolicies()){ + System.out.println(removedPolicy.getPolicyName()); + System.out.println(removedPolicy.getVersionNo()); + } + }else if(notification.getNotificationType().equals(NotificationType.UPDATE)){ + System.out.println("Updated Policies: \n"); + for(LoadedPolicy updatedPolicy: notification.getLoadedPolicies()){ + System.out.println("policyName : " + updatedPolicy.getPolicyName()); + System.out.println("policyVersion :" + updatedPolicy.getVersionNo()); + if(updatedPolicy.getPolicyName().contains(".Config_")){ + System.out.println("Matches: " + updatedPolicy.getMatches()); + System.out.println("UpdateType: "+ updatedPolicy.getUpdateType()); + // Checking the Name is correct or not. + try { + PolicyEngine policyEngine = new PolicyEngine("config.properties"); + @SuppressWarnings("deprecation") + Collection policyConfigs = policyEngine.getConfigByPolicyName(updatedPolicy.getPolicyName()); + for(PolicyConfig policyConfig: policyConfigs){ + if(policyConfig.getPolicyConfigStatus().equals(PolicyConfigStatus.CONFIG_RETRIEVED)){ + System.out.println("Policy Retrieved with this Name notified. "); + }else{ + System.err.println("\n\n Fail to retrieve policy !!!!\n\n"); + } + // Also Test this case. + if(policyConfig.getPolicyName().equals(updatedPolicy.getPolicyName())){ + System.out.println("Policy Name is good. "); + }else{ + System.err.println("\n\n Fail to check Name \n\n"); + } + } + } catch (PolicyEngineException e) { + LOGGER.error("Exception Occured"+e); + } catch (PolicyConfigException e) { + LOGGER.error("Exception Occured"+e); + } + } + } + }else if(notification.getNotificationType().equals(NotificationType.BOTH)){ + System.out.println("Both updated and Removed Notification: \n"); + System.out.println("Removed Policies: \n"); + for(RemovedPolicy removedPolicy: notification.getRemovedPolicies()){ + System.out.println(removedPolicy.getPolicyName()); + System.out.println(removedPolicy.getVersionNo()); + } + System.out.println("Updated Policies: \n"); + for(LoadedPolicy updatedPolicy: notification.getLoadedPolicies()){ + System.out.println("policyName : " + updatedPolicy.getPolicyName()); + System.out.println("policyVersion :" + updatedPolicy.getVersionNo()); + System.out.println("Matches: " + updatedPolicy.getMatches()); + System.out.println("UpdateType: "+ updatedPolicy.getUpdateType()); + // Checking the Name is correct or not. + try { + PolicyEngine policyEngine = new PolicyEngine("config.properties"); + @SuppressWarnings("deprecation") + Collection policyConfigs = policyEngine.getConfigByPolicyName(updatedPolicy.getPolicyName()); + for(PolicyConfig policyConfig: policyConfigs){ + if(policyConfig.getPolicyConfigStatus().equals(PolicyConfigStatus.CONFIG_RETRIEVED)){ + System.out.println("Policy Retrieved with this Name notified. "); + }else{ + System.err.println("\n\n Fail to retrieve policy !!!!\n\n"); + } + // Also Test this case. + if(policyConfig.getPolicyName().equals(updatedPolicy.getPolicyName())){ + System.out.println("Policy Name is good. "); + }else{ + System.err.println("\n\n Fail to check Name \n\n"); + } + } + } catch (PolicyEngineException e) { + LOGGER.error("Exception Occured"+e); + } catch (PolicyConfigException e) { + LOGGER.error("Exception Occured"+e); + } + } + } + } + + +} diff --git a/PolicyEngineAPI/src/test/java/org/onap/policy/std/test/Handler.java b/PolicyEngineAPI/src/test/java/org/onap/policy/std/test/Handler.java deleted file mode 100644 index 388ca078d..000000000 --- a/PolicyEngineAPI/src/test/java/org/onap/policy/std/test/Handler.java +++ /dev/null @@ -1,126 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * PolicyEngineAPI - * ================================================================================ - * 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.std.test; - -import java.util.Collection; - -import org.onap.policy.api.LoadedPolicy; -import org.onap.policy.api.NotificationHandler; -import org.onap.policy.api.NotificationType; -import org.onap.policy.api.PDPNotification; -import org.onap.policy.api.PolicyConfig; -import org.onap.policy.api.PolicyConfigException; -import org.onap.policy.api.PolicyConfigStatus; -import org.onap.policy.api.PolicyEngine; -import org.onap.policy.api.PolicyEngineException; -import org.onap.policy.api.RemovedPolicy; -import org.onap.policy.common.logging.flexlogger.FlexLogger; -import org.onap.policy.common.logging.flexlogger.Logger; - -public class Handler implements NotificationHandler{ - - private static final Logger LOGGER = FlexLogger.getLogger(Handler.class); - - @Override - public void notificationReceived(PDPNotification notification) { - System.out.println("Notification Received..."); - System.out.println(notification.getNotificationType()); - if(notification.getNotificationType().equals(NotificationType.REMOVE)){ - System.out.println("Removed Policies: \n"); - for(RemovedPolicy removedPolicy: notification.getRemovedPolicies()){ - System.out.println(removedPolicy.getPolicyName()); - System.out.println(removedPolicy.getVersionNo()); - } - }else if(notification.getNotificationType().equals(NotificationType.UPDATE)){ - System.out.println("Updated Policies: \n"); - for(LoadedPolicy updatedPolicy: notification.getLoadedPolicies()){ - System.out.println("policyName : " + updatedPolicy.getPolicyName()); - System.out.println("policyVersion :" + updatedPolicy.getVersionNo()); - if(updatedPolicy.getPolicyName().contains(".Config_")){ - System.out.println("Matches: " + updatedPolicy.getMatches()); - System.out.println("UpdateType: "+ updatedPolicy.getUpdateType()); - // Checking the Name is correct or not. - try { - PolicyEngine policyEngine = new PolicyEngine("config.properties"); - @SuppressWarnings("deprecation") - Collection policyConfigs = policyEngine.getConfigByPolicyName(updatedPolicy.getPolicyName()); - for(PolicyConfig policyConfig: policyConfigs){ - if(policyConfig.getPolicyConfigStatus().equals(PolicyConfigStatus.CONFIG_RETRIEVED)){ - System.out.println("Policy Retrieved with this Name notified. "); - }else{ - System.err.println("\n\n Fail to retrieve policy !!!!\n\n"); - } - // Also Test this case. - if(policyConfig.getPolicyName().equals(updatedPolicy.getPolicyName())){ - System.out.println("Policy Name is good. "); - }else{ - System.err.println("\n\n Fail to check Name \n\n"); - } - } - } catch (PolicyEngineException e) { - LOGGER.error("Exception Occured"+e); - } catch (PolicyConfigException e) { - LOGGER.error("Exception Occured"+e); - } - } - } - }else if(notification.getNotificationType().equals(NotificationType.BOTH)){ - System.out.println("Both updated and Removed Notification: \n"); - System.out.println("Removed Policies: \n"); - for(RemovedPolicy removedPolicy: notification.getRemovedPolicies()){ - System.out.println(removedPolicy.getPolicyName()); - System.out.println(removedPolicy.getVersionNo()); - } - System.out.println("Updated Policies: \n"); - for(LoadedPolicy updatedPolicy: notification.getLoadedPolicies()){ - System.out.println("policyName : " + updatedPolicy.getPolicyName()); - System.out.println("policyVersion :" + updatedPolicy.getVersionNo()); - System.out.println("Matches: " + updatedPolicy.getMatches()); - System.out.println("UpdateType: "+ updatedPolicy.getUpdateType()); - // Checking the Name is correct or not. - try { - PolicyEngine policyEngine = new PolicyEngine("config.properties"); - @SuppressWarnings("deprecation") - Collection policyConfigs = policyEngine.getConfigByPolicyName(updatedPolicy.getPolicyName()); - for(PolicyConfig policyConfig: policyConfigs){ - if(policyConfig.getPolicyConfigStatus().equals(PolicyConfigStatus.CONFIG_RETRIEVED)){ - System.out.println("Policy Retrieved with this Name notified. "); - }else{ - System.err.println("\n\n Fail to retrieve policy !!!!\n\n"); - } - // Also Test this case. - if(policyConfig.getPolicyName().equals(updatedPolicy.getPolicyName())){ - System.out.println("Policy Name is good. "); - }else{ - System.err.println("\n\n Fail to check Name \n\n"); - } - } - } catch (PolicyEngineException e) { - LOGGER.error("Exception Occured"+e); - } catch (PolicyConfigException e) { - LOGGER.error("Exception Occured"+e); - } - } - } - } - - -} diff --git a/PolicyEngineUtils/src/test/java/org/onap/policy/utils/test/BackUpMonitorTest.java b/PolicyEngineUtils/src/test/java/org/onap/policy/utils/test/BackUpMonitorTest.java new file mode 100644 index 000000000..83362fd0d --- /dev/null +++ b/PolicyEngineUtils/src/test/java/org/onap/policy/utils/test/BackUpMonitorTest.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 BackUpMonitorTest { + + @Test (expected = PersistenceException.class) + public void backUpMonitorTestFail() throws Exception{ + Properties properties = new Properties(); + properties.setProperty("javax.persistence.jdbc.driver", "org.mariadb.jdbc.Driver"); + properties.setProperty("javax.persistence.jdbc.url", "jdbc:mariadb://localhost:3306/onap_sdk"); + properties.setProperty("javax.persistence.jdbc.user", "policy_user"); + properties.setProperty("javax.persistence.jdbc.password", ""); + BackUpMonitor.getInstance(BackUpMonitor.ResourceNode.BRMS.toString(), "brms_test" , properties, new DummyBackUpHandler()); + } + + @Test + public void backUpMonitorTestFailNoUser() throws Exception{ + Properties properties = new Properties(); + properties.setProperty("javax.persistence.jdbc.driver", "org.mariadb.jdbc.Driver"); + properties.setProperty("javax.persistence.jdbc.url", "jdbc:mariadb://localhost:3306/onap_sdk"); + 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 DummyBackUpHandler()); + assertNull(bum); + } + + @Test + public void backUpMonitorTestFailNoURL() throws Exception{ + Properties properties = new Properties(); + properties.setProperty("javax.persistence.jdbc.driver", "org.mariadb.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 DummyBackUpHandler()); + 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:mariadb://localhost:3306/onap_sdk"); + 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 DummyBackUpHandler()); + assertNull(bum); + } + + @Test + public void backUpMonitorTestFailNoNode() throws Exception{ + Properties properties = new Properties(); + properties.setProperty("javax.persistence.jdbc.driver", "org.mariadb.jdbc.Driver"); + properties.setProperty("javax.persistence.jdbc.url", "jdbc:mariadb://localhost:3306/onap_sdk"); + 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 DummyBackUpHandler()); + assertNull(bum); + } + + @Test + public void backUpMonitorTestFailNoResource() throws Exception{ + Properties properties = new Properties(); + properties.setProperty("javax.persistence.jdbc.driver", "org.mariadb.jdbc.Driver"); + properties.setProperty("javax.persistence.jdbc.url", "jdbc:mariadb://localhost:3306/onap_sdk"); + 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 DummyBackUpHandler()); + assertNull(bum); + } + + @Test + public void backUpMonitorTestFailNoProperties() throws Exception{ + BackUpMonitor bum = BackUpMonitor.getInstance(BackUpMonitor.ResourceNode.BRMS.toString(), "brms_test" , null, new DummyBackUpHandler()); + assertNull(bum); + } + + @Test + public void backUpMonitorTestFailNoHandler() throws Exception{ + Properties properties = new Properties(); + properties.setProperty("javax.persistence.jdbc.driver", "org.mariadb.jdbc.Driver"); + properties.setProperty("javax.persistence.jdbc.url", "jdbc:mariadb://localhost:3306/onap_sdk"); + 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 DummyBackUpHandler()); + 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 DummyBackUpHandler()); + 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 loadedPolicies = new ArrayList<>(); + StdLoadedPolicy loadedPolicy = new StdLoadedPolicy(); + loadedPolicy.setPolicyName("com.testing"); + loadedPolicy.setUpdateType(UpdateType.UPDATE); + loadedPolicy.setVersionNo("2"); + Map matches = new HashMap<>(); + matches.put("test", "test"); + loadedPolicy.setMatches(matches); + loadedPolicies.add(loadedPolicy); + notification.setLoadedPolicies(loadedPolicies); + List 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 loadedPolicies = new ArrayList<>(); + StdLoadedPolicy loadedPolicy = new StdLoadedPolicy(); + loadedPolicy.setPolicyName("com.testing"); + loadedPolicy.setUpdateType(UpdateType.NEW); + loadedPolicy.setVersionNo("1"); + Map 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.setResourceNodeName(ResourceNode.BRMS.toString()); + bMEntity.setResourceName("brms_test2"); + bMEntity.setFlag("MASTER"); + bMEntity.setTimeStamp(new Date()); + StdPDPNotification notification = new StdPDPNotification(); + notification.setNotificationType(NotificationType.UPDATE); + List loadedPolicies = new ArrayList<>(); + StdLoadedPolicy loadedPolicy = new StdLoadedPolicy(); + loadedPolicy.setPolicyName("com.test"); + loadedPolicy.setUpdateType(UpdateType.NEW); + loadedPolicy.setVersionNo("1"); + Map 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(); + } +} diff --git a/PolicyEngineUtils/src/test/java/org/onap/policy/utils/test/DummyBackUpHandler.java b/PolicyEngineUtils/src/test/java/org/onap/policy/utils/test/DummyBackUpHandler.java new file mode 100644 index 000000000..15fb33944 --- /dev/null +++ b/PolicyEngineUtils/src/test/java/org/onap/policy/utils/test/DummyBackUpHandler.java @@ -0,0 +1,38 @@ +/*- + * ============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 org.onap.policy.api.PDPNotification; +import org.onap.policy.utils.BackUpHandler; + +public class DummyBackUpHandler implements BackUpHandler{ + + @Override + public void notificationReceived(PDPNotification notification) { + System.out.println("Received Notification from PDP. "); + } + + @Override + public void runOnNotification(PDPNotification notification) { + System.out.println("Running main Notification Function. "); + } + +} diff --git a/PolicyEngineUtils/src/test/java/org/onap/policy/utils/test/Handler.java b/PolicyEngineUtils/src/test/java/org/onap/policy/utils/test/Handler.java deleted file mode 100644 index 91f467b60..000000000 --- a/PolicyEngineUtils/src/test/java/org/onap/policy/utils/test/Handler.java +++ /dev/null @@ -1,38 +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.utils.test; - -import org.onap.policy.api.PDPNotification; -import org.onap.policy.utils.BackUpHandler; - -public class Handler implements BackUpHandler{ - - @Override - public void notificationReceived(PDPNotification notification) { - System.out.println("Received Notification from PDP. "); - } - - @Override - public void runOnNotification(PDPNotification notification) { - System.out.println("Running main Notification Function. "); - } - -} 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 deleted file mode 100644 index 39434a7e7..000000000 --- a/PolicyEngineUtils/src/test/java/org/onap/policy/utils/test/testBackUpMonitor.java +++ /dev/null @@ -1,292 +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.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", "org.mariadb.jdbc.Driver"); - properties.setProperty("javax.persistence.jdbc.url", "jdbc:mariadb://localhost:3306/onap_sdk"); - 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", "org.mariadb.jdbc.Driver"); - properties.setProperty("javax.persistence.jdbc.url", "jdbc:mariadb://localhost:3306/onap_sdk"); - 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", "org.mariadb.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:mariadb://localhost:3306/onap_sdk"); - 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", "org.mariadb.jdbc.Driver"); - properties.setProperty("javax.persistence.jdbc.url", "jdbc:mariadb://localhost:3306/onap_sdk"); - 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", "org.mariadb.jdbc.Driver"); - properties.setProperty("javax.persistence.jdbc.url", "jdbc:mariadb://localhost:3306/onap_sdk"); - 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", "org.mariadb.jdbc.Driver"); - properties.setProperty("javax.persistence.jdbc.url", "jdbc:mariadb://localhost:3306/onap_sdk"); - 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 loadedPolicies = new ArrayList<>(); - StdLoadedPolicy loadedPolicy = new StdLoadedPolicy(); - loadedPolicy.setPolicyName("com.testing"); - loadedPolicy.setUpdateType(UpdateType.UPDATE); - loadedPolicy.setVersionNo("2"); - Map matches = new HashMap<>(); - matches.put("test", "test"); - loadedPolicy.setMatches(matches); - loadedPolicies.add(loadedPolicy); - notification.setLoadedPolicies(loadedPolicies); - List 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 loadedPolicies = new ArrayList<>(); - StdLoadedPolicy loadedPolicy = new StdLoadedPolicy(); - loadedPolicy.setPolicyName("com.testing"); - loadedPolicy.setUpdateType(UpdateType.NEW); - loadedPolicy.setVersionNo("1"); - Map 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.setResourceNodeName(ResourceNode.BRMS.toString()); - bMEntity.setResourceName("brms_test2"); - bMEntity.setFlag("MASTER"); - bMEntity.setTimeStamp(new Date()); - StdPDPNotification notification = new StdPDPNotification(); - notification.setNotificationType(NotificationType.UPDATE); - List loadedPolicies = new ArrayList<>(); - StdLoadedPolicy loadedPolicy = new StdLoadedPolicy(); - loadedPolicy.setPolicyName("com.test"); - loadedPolicy.setUpdateType(UpdateType.NEW); - loadedPolicy.setVersionNo("1"); - Map 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(); - } -} -- cgit 1.2.3-korg