diff options
author | Pamela Dragosh <pdragosh@research.att.com> | 2019-07-25 13:06:20 +0000 |
---|---|---|
committer | Gerrit Code Review <gerrit@onap.org> | 2019-07-25 13:06:20 +0000 |
commit | ce1121d3cbe7e7706c1cd3f653436a7bdc07c2bb (patch) | |
tree | e835b767798bb96319d94c546997f176d4c6e4d3 /feature-active-standby-management/src/test/java | |
parent | 3f0f00395ebcfa9fd37765efddb82573588d1263 (diff) | |
parent | 7602a4380488d872b25cd718176b23369bf9e472 (diff) |
Merge "Add coverage to feature-active-standby-management"
Diffstat (limited to 'feature-active-standby-management/src/test/java')
4 files changed, 491 insertions, 1 deletions
diff --git a/feature-active-standby-management/src/test/java/org/onap/policy/drools/activestandby/ActiveStandbyPropertiesTest.java b/feature-active-standby-management/src/test/java/org/onap/policy/drools/activestandby/ActiveStandbyPropertiesTest.java new file mode 100644 index 00000000..058aa5e8 --- /dev/null +++ b/feature-active-standby-management/src/test/java/org/onap/policy/drools/activestandby/ActiveStandbyPropertiesTest.java @@ -0,0 +1,53 @@ +/* + * ============LICENSE_START======================================================= + * ONAP + * ================================================================================ + * Copyright (C) 2019 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.drools.activestandby; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertSame; + +import java.util.Properties; +import org.junit.Before; +import org.junit.Test; + +public class ActiveStandbyPropertiesTest { + + private Properties props; + + /** + * Initializes objects. + */ + @Before + public void setUp() { + props = new Properties(); + props.setProperty("abc", "hello"); + props.setProperty("def", "world"); + } + + @Test + public void testInitProperties_testGetProperty_testGetProperties() { + ActiveStandbyProperties.initProperties(props); + + assertSame(props, ActiveStandbyProperties.getProperties()); + + assertEquals("hello", ActiveStandbyProperties.getProperty("abc")); + assertEquals("world", ActiveStandbyProperties.getProperty("def")); + } +} diff --git a/feature-active-standby-management/src/test/java/org/onap/policy/drools/activestandby/DroolsPdpObjectTest.java b/feature-active-standby-management/src/test/java/org/onap/policy/drools/activestandby/DroolsPdpObjectTest.java new file mode 100644 index 00000000..310f9110 --- /dev/null +++ b/feature-active-standby-management/src/test/java/org/onap/policy/drools/activestandby/DroolsPdpObjectTest.java @@ -0,0 +1,153 @@ +/* + * ============LICENSE_START======================================================= + * ONAP + * ================================================================================ + * Copyright (C) 2019 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.drools.activestandby; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import java.util.Date; +import lombok.Getter; +import lombok.Setter; +import org.junit.Before; +import org.junit.Test; +import org.onap.policy.drools.activestandby.DroolsPdp; +import org.onap.policy.drools.activestandby.DroolsPdpObject; + +public class DroolsPdpObjectTest { + private static final String PDP_ID = "my-id"; + private static final String PDP_ID2 = "my-id2"; + private static final String SITE = "my-site"; + private static final String SITE2 = "my-site2"; + private static final int PRIORITY = 11; + private static final int PRIORITY2 = 12; + + private MyPdp pdp; + + @Before + public void setUp() { + pdp = makePdp(PDP_ID, SITE, PRIORITY); + } + + @Test + public void testEqualsObject() { + // self + assertTrue(pdp.equals(pdp)); + + // same id + MyPdp pdp2 = new MyPdp(); + pdp2.setPdpId(PDP_ID); + assertTrue(pdp.equals(pdp2)); + + // different id + pdp2.setPdpId(PDP_ID2); + assertFalse(pdp.equals(pdp2)); + + // different type of object + assertFalse(pdp.equals("")); + } + + @Test + public void testNullSafeCompare() { + // self, when null + pdp.setSiteName(null); + assertEquals(0, pdp.comparePriority(pdp)); + + // both null + MyPdp pdp2 = makePdp(PDP_ID, null, PRIORITY); + assertEquals(0, pdp.comparePriority(pdp2)); + + // left null + pdp2 = makePdp(PDP_ID, SITE, PRIORITY); + assertEquals(-1, pdp.comparePriority(pdp2)); + + // right null - note: args are reversed + pdp2 = makePdp(PDP_ID, SITE, PRIORITY); + assertEquals(1, pdp2.comparePriority(pdp)); + } + + @Test + public void testComparePriorityDroolsPdp() { + // self + assertEquals(0, pdp.comparePriority(pdp)); + + // same + MyPdp pdp2 = makePdp(PDP_ID, SITE, PRIORITY); + assertEquals(0, pdp.comparePriority(pdp2)); + + // different site + pdp2 = makePdp(PDP_ID, SITE2, PRIORITY); + assertEquals(SITE.compareTo(SITE2), pdp.comparePriority(pdp2)); + + // different priority + pdp2 = makePdp(PDP_ID, SITE, PRIORITY2); + assertEquals(PRIORITY - PRIORITY2, pdp.comparePriority(pdp2)); + + // different id + pdp2 = makePdp(PDP_ID2, SITE, PRIORITY); + assertEquals(PDP_ID.compareTo(PDP_ID2), pdp.comparePriority(pdp2)); + } + + @Test + public void testComparePriorityDroolsPdpString() { + final int result = 1000; + + // override other comparison method so we know if it's called + MyPdp pdp2 = new MyPdp() { + @Override + public int comparePriority(DroolsPdp other) { + return result; + } + }; + + pdp2.setPdpId(PDP_ID); + pdp2.setSiteName(SITE2); + pdp2.setPriority(PRIORITY); + + // should use overridden comparison method + assertEquals(result, pdp2.comparePriority(pdp, null)); + assertEquals(result, pdp2.comparePriority(pdp, "")); + + // should use normal comparison method + assertEquals(SITE2.compareTo(SITE), pdp2.comparePriority(pdp, SITE)); + } + + private MyPdp makePdp(String id, String site, int priority) { + MyPdp pdp2 = new MyPdp(); + + pdp2.setSiteName(site); + pdp2.setPdpId(id); + pdp2.setPriority(priority); + + return pdp2; + } + + @Getter + @Setter + private class MyPdp extends DroolsPdpObject { + private String pdpId; + private boolean designated; + private int priority; + private Date updatedDate; + private String siteName; + private Date designatedDate; + } +} diff --git a/feature-active-standby-management/src/test/java/org/onap/policy/drools/activestandby/PmStandbyStateChangeNotifierTest.java b/feature-active-standby-management/src/test/java/org/onap/policy/drools/activestandby/PmStandbyStateChangeNotifierTest.java new file mode 100644 index 00000000..28d3b439 --- /dev/null +++ b/feature-active-standby-management/src/test/java/org/onap/policy/drools/activestandby/PmStandbyStateChangeNotifierTest.java @@ -0,0 +1,284 @@ +/* + * ============LICENSE_START======================================================= + * ONAP + * ================================================================================ + * Copyright (C) 2019 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.drools.activestandby; + +import static org.junit.Assert.assertEquals; +import static org.mockito.Matchers.any; +import static org.mockito.Matchers.eq; +import static org.mockito.Mockito.doThrow; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import java.util.Properties; +import java.util.Timer; +import java.util.TimerTask; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.mockito.ArgumentCaptor; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.onap.policy.common.im.StateManagement; +import org.onap.policy.drools.system.PolicyEngine; + +public class PmStandbyStateChangeNotifierTest { + private static final String UNSUPPORTED_STATUS = "unsupported status"; + private static final String PDP_ID = "my-pdp"; + private static final long UPDATE_INTERVAL = 100; + private static final long WAIT_INTERVAL = 2 * UPDATE_INTERVAL + 2000; + + @Mock + private PolicyEngine engmgr; + + @Mock + private Timer timer; + + @Mock + private StateManagement mgmt; + + private PmStandbyStateChangeNotifier notifier; + + /** + * Initializes the properties. + */ + @BeforeClass + public static void setUpBeforeClass() { + Properties props = new Properties(); + props.setProperty(ActiveStandbyProperties.NODE_NAME, PDP_ID); + props.setProperty(ActiveStandbyProperties.PDP_UPDATE_INTERVAL, String.valueOf(UPDATE_INTERVAL)); + + ActiveStandbyProperties.initProperties(props); + } + + /** + * Initializes objects, including the notifier. + */ + + @Before + public void setUp() { + MockitoAnnotations.initMocks(this); + + notifier = new MyNotifier(); + } + + @Test + public void testHandleStateChange_Null() { + notifier.update(mgmt, null); + verify(engmgr).deactivate(); + assertEquals(StateManagement.NULL_VALUE, notifier.getPreviousStandbyStatus()); + + // repeat - nothing else should be done + when(mgmt.getStandbyStatus()).thenReturn(StateManagement.NULL_VALUE); + notifier.update(mgmt, null); + verify(engmgr, times(1)).deactivate(); + assertEquals(StateManagement.NULL_VALUE, notifier.getPreviousStandbyStatus()); + } + + @Test + public void testHandleStateChange_Null_Ex() { + doThrow(new MyException()).when(engmgr).deactivate(); + + // should not throw an exception + notifier.update(mgmt, null); + assertEquals(PmStandbyStateChangeNotifier.NONE, notifier.getPreviousStandbyStatus()); + } + + @Test + public void testHandleStateChange_HotOrCold() { + when(mgmt.getStandbyStatus()).thenReturn(StateManagement.HOT_STANDBY); + notifier.update(mgmt, null); + verify(engmgr).deactivate(); + assertEquals(PmStandbyStateChangeNotifier.HOTSTANDBY_OR_COLDSTANDBY, notifier.getPreviousStandbyStatus()); + + // repeat - nothing else should be done + when(mgmt.getStandbyStatus()).thenReturn(StateManagement.COLD_STANDBY); + notifier.update(mgmt, null); + verify(engmgr, times(1)).deactivate(); + assertEquals(PmStandbyStateChangeNotifier.HOTSTANDBY_OR_COLDSTANDBY, notifier.getPreviousStandbyStatus()); + } + + @Test + public void testHandleStateChange_HotOrCold_Ex() { + doThrow(new MyException()).when(engmgr).deactivate(); + + // should not throw an exception + when(mgmt.getStandbyStatus()).thenReturn(StateManagement.HOT_STANDBY); + notifier.update(mgmt, null); + assertEquals(PmStandbyStateChangeNotifier.NONE, notifier.getPreviousStandbyStatus()); + } + + @Test + public void testHandleStateChange_ProvidingService() { + when(mgmt.getStandbyStatus()).thenReturn(StateManagement.PROVIDING_SERVICE); + notifier.update(mgmt, null); + verify(engmgr, never()).activate(); + assertEquals(PmStandbyStateChangeNotifier.NONE, notifier.getPreviousStandbyStatus()); + + ArgumentCaptor<TimerTask> captor = ArgumentCaptor.forClass(TimerTask.class); + verify(timer).schedule(captor.capture(), eq(WAIT_INTERVAL)); + + // execute the timer task + captor.getValue().run(); + + verify(engmgr).activate(); + assertEquals(StateManagement.PROVIDING_SERVICE, notifier.getPreviousStandbyStatus()); + + // repeat - nothing else should be done + notifier.update(mgmt, null); + verify(engmgr, never()).deactivate(); + verify(engmgr, times(1)).activate(); + verify(timer, times(1)).schedule(captor.capture(), eq(WAIT_INTERVAL)); + assertEquals(StateManagement.PROVIDING_SERVICE, notifier.getPreviousStandbyStatus()); + } + + @Test + public void testHandleStateChange_ProvidingService_BeforeActivation() { + when(mgmt.getStandbyStatus()).thenReturn(StateManagement.PROVIDING_SERVICE); + notifier.update(mgmt, null); + + // repeat - nothing else should be done + notifier.update(mgmt, null); + verify(engmgr, never()).deactivate(); + verify(engmgr, never()).activate(); + + verify(timer, times(1)).schedule(any(), eq(WAIT_INTERVAL)); + assertEquals(PmStandbyStateChangeNotifier.NONE, notifier.getPreviousStandbyStatus()); + } + + @Test + public void testHandleStateChange_ProvidingService_Ex() { + notifier = new MyNotifier() { + @Override + protected Timer makeTimer() { + throw new MyException(); + } + }; + + when(mgmt.getStandbyStatus()).thenReturn(StateManagement.PROVIDING_SERVICE); + notifier.update(mgmt, null); + + assertEquals(PmStandbyStateChangeNotifier.NONE, notifier.getPreviousStandbyStatus()); + } + + @Test + public void testHandleStateChange_Unsupported() { + when(mgmt.getStandbyStatus()).thenReturn(UNSUPPORTED_STATUS); + notifier.update(mgmt, null); + + verify(engmgr).deactivate(); + assertEquals(PmStandbyStateChangeNotifier.UNSUPPORTED, notifier.getPreviousStandbyStatus()); + + // repeat - nothing else should be done + notifier.update(mgmt, null); + verify(engmgr, times(1)).deactivate(); + assertEquals(PmStandbyStateChangeNotifier.UNSUPPORTED, notifier.getPreviousStandbyStatus()); + } + + @Test + public void testHandleStateChange_Unsupported_Ex() { + doThrow(new MyException()).when(engmgr).deactivate(); + + // should not throw an exception + when(mgmt.getStandbyStatus()).thenReturn(UNSUPPORTED_STATUS); + notifier.update(mgmt, null); + assertEquals(PmStandbyStateChangeNotifier.NONE, notifier.getPreviousStandbyStatus()); + } + + @Test + public void testCancelTimer() { + when(mgmt.getStandbyStatus()).thenReturn(StateManagement.PROVIDING_SERVICE); + notifier.update(mgmt, null); + + when(mgmt.getStandbyStatus()).thenReturn(null); + notifier.update(mgmt, null); + + verify(timer).cancel(); + } + + @Test + public void testDelayActivateClass() { + when(mgmt.getStandbyStatus()).thenReturn(StateManagement.PROVIDING_SERVICE); + notifier.update(mgmt, null); + verify(engmgr, never()).activate(); + assertEquals(PmStandbyStateChangeNotifier.NONE, notifier.getPreviousStandbyStatus()); + + ArgumentCaptor<TimerTask> captor = ArgumentCaptor.forClass(TimerTask.class); + verify(timer).schedule(captor.capture(), eq(WAIT_INTERVAL)); + + // execute the timer task + captor.getValue().run(); + + verify(engmgr).activate(); + assertEquals(StateManagement.PROVIDING_SERVICE, notifier.getPreviousStandbyStatus()); + } + + @Test + public void testDelayActivateClass_Ex() { + when(mgmt.getStandbyStatus()).thenReturn(StateManagement.PROVIDING_SERVICE); + notifier.update(mgmt, null); + verify(engmgr, never()).activate(); + assertEquals(PmStandbyStateChangeNotifier.NONE, notifier.getPreviousStandbyStatus()); + + ArgumentCaptor<TimerTask> captor = ArgumentCaptor.forClass(TimerTask.class); + verify(timer).schedule(captor.capture(), eq(WAIT_INTERVAL)); + + doThrow(new MyException()).when(engmgr).activate(); + + // execute the timer task + captor.getValue().run(); + + assertEquals(PmStandbyStateChangeNotifier.NONE, notifier.getPreviousStandbyStatus()); + } + + @Test + public void testGetPolicyEngineManager() { + // use real object with real method - no exception expected + new PmStandbyStateChangeNotifier().getPolicyEngineManager(); + } + + @Test + public void testMakeTimer() { + // use real object with real method + new PmStandbyStateChangeNotifier().makeTimer().cancel(); + } + + private class MyNotifier extends PmStandbyStateChangeNotifier { + @Override + protected PolicyEngine getPolicyEngineManager() { + return engmgr; + } + + @Override + protected Timer makeTimer() { + return timer; + } + } + + private static class MyException extends RuntimeException { + private static final long serialVersionUID = 1L; + + public MyException() { + super("expected exception"); + } + } +} diff --git a/feature-active-standby-management/src/test/java/org/onap/policy/drools/controller/test/AllSeemsWellTest.java b/feature-active-standby-management/src/test/java/org/onap/policy/drools/controller/test/AllSeemsWellTest.java index 719fb62f..b5b89941 100644 --- a/feature-active-standby-management/src/test/java/org/onap/policy/drools/controller/test/AllSeemsWellTest.java +++ b/feature-active-standby-management/src/test/java/org/onap/policy/drools/controller/test/AllSeemsWellTest.java @@ -235,7 +235,7 @@ public class AllSeemsWellTest { // discovered by the ActiveStandbyFeature when the election handler initializes. StateManagementFeatureApi stateManagementFeatureApi = null; - for (StateManagementFeatureApi feature : stateManagementFeatureApi.impl.getList()) { + for (StateManagementFeatureApi feature : StateManagementFeatureApi.impl.getList()) { ((PolicySessionFeatureApi) feature).globalInit(null, configDir); stateManagementFeatureApi = feature; logger.debug("testAllSeemsWell stateManagementFeature.getResourceName(): {}", |