summaryrefslogtreecommitdiffstats
path: root/controlloop/common/eventmanager/src/test/java
diff options
context:
space:
mode:
authorJim Hahn <jrh3@att.com>2019-09-27 11:22:47 -0400
committerJim Hahn <jrh3@att.com>2019-10-22 16:06:10 -0400
commitac5c19ddbed1ff5905d16a6359ee23b4888c717a (patch)
treeacd501248ae73dcd90b2318695acc535a9bb9dcc /controlloop/common/eventmanager/src/test/java
parent636a1b2fcafa5249cf2bf380dfb6e20f6fe98691 (diff)
Modify drools-applications to use new Lock API
Modified code to use new Lock API. Also deleted TargetLock and PolicyGuard, as they are no longer needed. Issue-ID: POLICY-2113 Signed-off-by: Jim Hahn <jrh3@att.com> Change-Id: I5bc9b7732f9cfc6056789b2902d9f6f838b560be
Diffstat (limited to 'controlloop/common/eventmanager/src/test/java')
-rw-r--r--controlloop/common/eventmanager/src/test/java/org/onap/policy/controlloop/eventmanager/ControlLoopEventManagerTest.java226
-rw-r--r--controlloop/common/eventmanager/src/test/java/org/onap/policy/controlloop/eventmanager/LockCallbackWorkingMemoryTest.java96
2 files changed, 259 insertions, 63 deletions
diff --git a/controlloop/common/eventmanager/src/test/java/org/onap/policy/controlloop/eventmanager/ControlLoopEventManagerTest.java b/controlloop/common/eventmanager/src/test/java/org/onap/policy/controlloop/eventmanager/ControlLoopEventManagerTest.java
index 0b27ffa49..8efdb1fcb 100644
--- a/controlloop/common/eventmanager/src/test/java/org/onap/policy/controlloop/eventmanager/ControlLoopEventManagerTest.java
+++ b/controlloop/common/eventmanager/src/test/java/org/onap/policy/controlloop/eventmanager/ControlLoopEventManagerTest.java
@@ -25,12 +25,19 @@ import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
+import static org.mockito.Matchers.any;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
+import java.io.Serializable;
import java.nio.charset.StandardCharsets;
import java.time.Instant;
import java.util.HashMap;
@@ -65,14 +72,14 @@ import org.onap.policy.controlloop.VirtualControlLoopNotification;
import org.onap.policy.controlloop.eventmanager.ControlLoopEventManager.NewEventStatus;
import org.onap.policy.controlloop.policy.ControlLoopPolicy;
import org.onap.policy.controlloop.policy.PolicyResult;
+import org.onap.policy.drools.core.lock.Lock;
+import org.onap.policy.drools.core.lock.LockCallback;
import org.onap.policy.drools.system.PolicyEngineConstants;
-import org.onap.policy.guard.GuardResult;
-import org.onap.policy.guard.PolicyGuard;
-import org.onap.policy.guard.PolicyGuard.LockResult;
-import org.onap.policy.guard.TargetLock;
+import org.onap.policy.drools.utils.Pair;
import org.powermock.reflect.Whitebox;
public class ControlLoopEventManagerTest {
+ private static final String TARGET_LOCK_FIELD = "targetLock";
private static final String PROCESS_VSERVER_RESPONSE = "processVServerResponse";
private static final String ONSET_ONE = "onsetOne";
private static final String VSERVER_NAME = "vserver.vserver-name";
@@ -107,6 +114,7 @@ public class ControlLoopEventManagerTest {
public ExpectedException thrown = ExpectedException.none();
private VirtualControlLoopEvent onset;
+ private LockCallback callback;
/**
* Set up test class.
@@ -130,6 +138,8 @@ public class ControlLoopEventManagerTest {
*/
@Before
public void setUp() {
+ callback = mock(LockCallback.class);
+
onset = new VirtualControlLoopEvent();
onset.setClosedLoopControlName("ControlLoop-vUSP");
onset.setRequestId(UUID.randomUUID());
@@ -414,8 +424,6 @@ public class ControlLoopEventManagerTest {
assertNull(clem.getAbatementEvent());
assertNull(clem.getProcessor());
- assertEquals(true, clem.isActive());
- assertEquals(false, clem.releaseLock());
assertEquals(true, clem.isControlLoopTimedOut());
assertNull(clem.unlockCurrentOperation());
@@ -441,11 +449,6 @@ public class ControlLoopEventManagerTest {
@Test
public void testActivationYaml() throws IOException {
- InputStream is = new FileInputStream(new File(TEST_YAML));
- final String yamlString = IOUtils.toString(is, StandardCharsets.UTF_8);
-
- InputStream isBad = new FileInputStream(new File("src/test/resources/notutf8.yaml"));
- final String yamlStringBad = IOUtils.toString(isBad, StandardCharsets.UTF_8);
UUID requestId = UUID.randomUUID();
VirtualControlLoopEvent event = new VirtualControlLoopEvent();
@@ -470,10 +473,17 @@ public class ControlLoopEventManagerTest {
assertEquals(ControlLoopNotificationType.REJECTED, notificationEmpty.getNotification());
// Bad YAML should fail
+ InputStream isBad = new FileInputStream(new File("src/test/resources/notutf8.yaml"));
+ final String yamlStringBad = IOUtils.toString(isBad, StandardCharsets.UTF_8);
+
VirtualControlLoopNotification notificationBad = manager.activate(yamlStringBad, event);
assertNotNull(notificationBad);
assertEquals(ControlLoopNotificationType.REJECTED, notificationBad.getNotification());
+
+ InputStream is = new FileInputStream(new File(TEST_YAML));
+ final String yamlString = IOUtils.toString(is, StandardCharsets.UTF_8);
+
VirtualControlLoopNotification notification = manager.activate(yamlString, event);
assertNotNull(notification);
assertEquals(ControlLoopNotificationType.ACTIVE, notification.getNotification());
@@ -486,9 +496,6 @@ public class ControlLoopEventManagerTest {
@Test
public void testControlLoopFinal() throws Exception {
- InputStream is = new FileInputStream(new File(TEST_YAML));
- final String yamlString = IOUtils.toString(is, StandardCharsets.UTF_8);
-
UUID requestId = UUID.randomUUID();
VirtualControlLoopEvent event = new VirtualControlLoopEvent();
event.setClosedLoopControlName(TWO_ONSET_TEST);
@@ -509,6 +516,10 @@ public class ControlLoopEventManagerTest {
.hasMessage("No onset event for ControlLoopEventManager.");
manager.setActivated(false);
+
+ InputStream is = new FileInputStream(new File(TEST_YAML));
+ final String yamlString = IOUtils.toString(is, StandardCharsets.UTF_8);
+
VirtualControlLoopNotification notification = manager.activate(yamlString, event);
assertNotNull(notification);
assertEquals(ControlLoopNotificationType.ACTIVE, notification.getNotification());
@@ -552,9 +563,6 @@ public class ControlLoopEventManagerTest {
@Test
public void testProcessControlLoop() throws Exception {
- InputStream is = new FileInputStream(new File(TEST_YAML));
- final String yamlString = IOUtils.toString(is, StandardCharsets.UTF_8);
-
UUID requestId = UUID.randomUUID();
VirtualControlLoopEvent event = new VirtualControlLoopEvent();
event.setClosedLoopControlName(TWO_ONSET_TEST);
@@ -575,6 +583,10 @@ public class ControlLoopEventManagerTest {
.hasMessage("No onset event for ControlLoopEventManager.");
manager.setActivated(false);
+
+ InputStream is = new FileInputStream(new File(TEST_YAML));
+ final String yamlString = IOUtils.toString(is, StandardCharsets.UTF_8);
+
VirtualControlLoopNotification notification = manager.activate(yamlString, event);
assertNotNull(notification);
assertEquals(ControlLoopNotificationType.ACTIVE, notification.getNotification());
@@ -620,21 +632,10 @@ public class ControlLoopEventManagerTest {
@Test
public void testFinishOperation() throws Exception {
- InputStream is = new FileInputStream(new File("src/test/resources/testSOactor.yaml"));
- final String yamlString = IOUtils.toString(is, StandardCharsets.UTF_8);
-
InputStream isStd = new FileInputStream(new File(TEST_YAML));
final String yamlStringStd = IOUtils.toString(isStd, StandardCharsets.UTF_8);
- UUID requestId = UUID.randomUUID();
- VirtualControlLoopEvent event = new VirtualControlLoopEvent();
- event.setClosedLoopControlName(TWO_ONSET_TEST);
- event.setRequestId(requestId);
- event.setTarget(VNF_ID);
- event.setClosedLoopAlarmStart(Instant.now());
- event.setClosedLoopEventStatus(ControlLoopEventStatus.ONSET);
- event.setAai(new HashMap<>());
- event.getAai().put(VNF_ID, ONSET_ONE);
+ VirtualControlLoopEvent event = makeEvent();
ControlLoopEventManager manager = makeManager(event);
ControlLoopEventManager manager2 = manager;
@@ -646,15 +647,14 @@ public class ControlLoopEventManagerTest {
.hasMessage("No operation to finish.");
manager.setActivated(false);
+
+ InputStream is = new FileInputStream(new File("src/test/resources/testSOactor.yaml"));
+ final String yamlString = IOUtils.toString(is, StandardCharsets.UTF_8);
+
VirtualControlLoopNotification notification = manager.activate(yamlString, event);
assertNotNull(notification);
assertEquals(ControlLoopNotificationType.ACTIVE, notification.getNotification());
- assertThatThrownBy(manager2::lockCurrentOperation).isInstanceOf(ControlLoopException.class)
- .hasMessage("Do not have a current operation.");
-
- assertNull(manager.unlockCurrentOperation());
-
// serialize and de-serialize manager
manager = Serializer.roundTrip(manager);
@@ -662,23 +662,6 @@ public class ControlLoopEventManagerTest {
assertNotNull(clom);
assertNull(clom.getOperationResult());
- LockResult<GuardResult, TargetLock> lockLock = manager.lockCurrentOperation();
- assertNotNull(lockLock);
- assertEquals(GuardResult.LOCK_ACQUIRED, lockLock.getA());
-
- LockResult<GuardResult, TargetLock> lockLockAgain = manager.lockCurrentOperation();
- assertNotNull(lockLockAgain);
- assertEquals(GuardResult.LOCK_ACQUIRED, lockLockAgain.getA());
- assertEquals(lockLock.getB(), lockLockAgain.getB());
-
- assertEquals(lockLock.getB(), manager.unlockCurrentOperation());
- assertNull(manager.unlockCurrentOperation());
-
- lockLock = manager.lockCurrentOperation();
- assertNotNull(lockLock);
- PolicyGuard.unlockTarget(lockLock.getB());
- assertEquals(lockLock.getB(), manager.unlockCurrentOperation());
-
clom.startOperation(event);
// This call should be exception free
@@ -697,10 +680,98 @@ public class ControlLoopEventManagerTest {
}
@Test
- public void testOnNewEvent() throws Exception {
- InputStream is = new FileInputStream(new File(TEST_YAML));
+ public void testLockCurrentOperation_testUnlockCurrentOperation() throws Exception {
+ VirtualControlLoopEvent event = makeEvent();
+
+ ControlLoopEventManager manager = makeManager(event);
+
+ manager.setActivated(false);
+
+ InputStream is = new FileInputStream(new File("src/test/resources/testSOactor.yaml"));
final String yamlString = IOUtils.toString(is, StandardCharsets.UTF_8);
+ VirtualControlLoopNotification notification = manager.activate(yamlString, event);
+ assertNotNull(notification);
+ assertEquals(ControlLoopNotificationType.ACTIVE, notification.getNotification());
+
+ ControlLoopEventManager manager2 = manager;
+ assertThatThrownBy(() -> manager2.lockCurrentOperation(callback)).isInstanceOf(ControlLoopException.class)
+ .hasMessage("Do not have a current operation.");
+
+ assertNull(manager.unlockCurrentOperation());
+
+ ControlLoopOperationManager clom = manager.processControlLoop();
+ assertNotNull(clom);
+ assertNull(clom.getOperationResult());
+
+ Pair<Lock, Lock> lockPair = manager.lockCurrentOperation(callback);
+ assertNull(lockPair.first());
+ assertNotNull(lockPair.second());
+
+ // pseudo lock - session should NOT have been notified of the change
+ verify(callback, never()).lockAvailable(any());
+ verify(callback, never()).lockUnavailable(any());
+
+ // repeat - should cause an extension
+ Lock lock = lockPair.second();
+ lockPair = manager.lockCurrentOperation(callback);
+
+ /*
+ * even with a pseudo lock, the session should have been notified that it was
+ * extended
+ */
+
+ verify(callback).lockAvailable(lock);
+
+ assertSame(lock, manager.unlockCurrentOperation());
+
+ assertNull(lockPair.first());
+ assertNull(lockPair.second());
+
+ // force it to use a pseudo lock
+ manager.setUseTargetLock(false);
+ lockPair = manager.lockCurrentOperation(callback);
+ assertNull(lockPair.first());
+ assertNotNull(lockPair.second());
+
+ lock = lockPair.second();
+
+ lockPair = manager.lockCurrentOperation(callback);
+ assertNull(lockPair.first());
+ assertNull(lockPair.second());
+
+ // first lock uses a pseudo lock, so it will only update when extended
+ verify(callback).lockAvailable(lock);
+
+ // force it to re-create the lock due to change in resource ID
+ lock = mock(Lock.class);
+ when(lock.getResourceId()).thenReturn("different");
+ Whitebox.setInternalState(manager, TARGET_LOCK_FIELD, lock);
+
+ lockPair = manager.lockCurrentOperation(callback);
+ assertSame(lock, lockPair.first());
+ assertNotNull(lockPair.second());
+
+ lock = lockPair.second();
+
+ lockPair = manager.lockCurrentOperation(callback);
+ assertNull(lockPair.first());
+ assertNull(lockPair.second());
+
+ // first lock uses a pseudo lock, so it won't do an update
+ verify(callback).lockAvailable(lock);
+
+ assertSame(lock, manager.unlockCurrentOperation());
+ assertNull(manager.unlockCurrentOperation());
+
+ // try again - this time don't return the fact handle- no change in count
+ lockPair = manager.lockCurrentOperation(callback);
+ assertNull(lockPair.first());
+ assertNotNull(lockPair.second());
+ }
+
+ @Test
+ public void testOnNewEvent() throws Exception {
UUID requestId = UUID.randomUUID();
VirtualControlLoopEvent onsetEvent = new VirtualControlLoopEvent();
onsetEvent.setClosedLoopControlName(TWO_ONSET_TEST);
@@ -721,6 +792,10 @@ public class ControlLoopEventManagerTest {
abatedEvent.getAai().put(VNF_NAME, ONSET_ONE);
ControlLoopEventManager manager = makeManager(onsetEvent);
+
+ InputStream is = new FileInputStream(new File(TEST_YAML));
+ final String yamlString = IOUtils.toString(is, StandardCharsets.UTF_8);
+
VirtualControlLoopNotification notification = manager.activate(yamlString, onsetEvent);
assertNotNull(notification);
assertEquals(ControlLoopNotificationType.ACTIVE, notification.getNotification());
@@ -816,9 +891,6 @@ public class ControlLoopEventManagerTest {
@Test
public void testControlLoopTimeout() throws IOException {
- InputStream is = new FileInputStream(new File(TEST_YAML));
- final String yamlString = IOUtils.toString(is, StandardCharsets.UTF_8);
-
UUID requestId = UUID.randomUUID();
VirtualControlLoopEvent onsetEvent = new VirtualControlLoopEvent();
onsetEvent.setClosedLoopControlName(TWO_ONSET_TEST);
@@ -833,6 +905,9 @@ public class ControlLoopEventManagerTest {
assertTrue(0 == manager.getControlLoopTimeout(null));
assertTrue(120 == manager.getControlLoopTimeout(120));
+ InputStream is = new FileInputStream(new File(TEST_YAML));
+ final String yamlString = IOUtils.toString(is, StandardCharsets.UTF_8);
+
VirtualControlLoopNotification notification = manager.activate(yamlString, onsetEvent);
assertNotNull(notification);
assertEquals(ControlLoopNotificationType.ACTIVE, notification.getNotification());
@@ -842,9 +917,6 @@ public class ControlLoopEventManagerTest {
@Test
public void testControlLoopTimeout_ZeroTimeout() throws IOException {
- InputStream is = new FileInputStream(new File("src/test/resources/test-zero-timeout.yaml"));
- final String yamlString = IOUtils.toString(is, StandardCharsets.UTF_8);
-
UUID requestId = UUID.randomUUID();
VirtualControlLoopEvent onsetEvent = new VirtualControlLoopEvent();
onsetEvent.setClosedLoopControlName(TWO_ONSET_TEST);
@@ -857,6 +929,9 @@ public class ControlLoopEventManagerTest {
ControlLoopEventManager manager = makeManager(onsetEvent);
+ InputStream is = new FileInputStream(new File("src/test/resources/test-zero-timeout.yaml"));
+ final String yamlString = IOUtils.toString(is, StandardCharsets.UTF_8);
+
VirtualControlLoopNotification notification = manager.activate(yamlString, onsetEvent);
assertNotNull(notification);
assertEquals(ControlLoopNotificationType.ACTIVE, notification.getNotification());
@@ -867,9 +942,6 @@ public class ControlLoopEventManagerTest {
@Test
public void testControlLoopTimeout_NullTimeout() throws IOException {
- InputStream is = new FileInputStream(new File("src/test/resources/test-null-timeout.yaml"));
- final String yamlString = IOUtils.toString(is, StandardCharsets.UTF_8);
-
UUID requestId = UUID.randomUUID();
VirtualControlLoopEvent onsetEvent = new VirtualControlLoopEvent();
onsetEvent.setClosedLoopControlName(TWO_ONSET_TEST);
@@ -882,6 +954,9 @@ public class ControlLoopEventManagerTest {
ControlLoopEventManager manager = makeManager(onsetEvent);
+ InputStream is = new FileInputStream(new File("src/test/resources/test-null-timeout.yaml"));
+ final String yamlString = IOUtils.toString(is, StandardCharsets.UTF_8);
+
VirtualControlLoopNotification notification = manager.activate(yamlString, onsetEvent);
assertNotNull(notification);
assertEquals(ControlLoopNotificationType.ACTIVE, notification.getNotification());
@@ -1258,8 +1333,33 @@ public class ControlLoopEventManagerTest {
assertNotNull(aaiCqResponse);
}
+ private VirtualControlLoopEvent makeEvent() {
+ UUID requestId = UUID.randomUUID();
+ VirtualControlLoopEvent event = new VirtualControlLoopEvent();
+ event.setClosedLoopControlName(TWO_ONSET_TEST);
+ event.setRequestId(requestId);
+ event.setTarget(VNF_ID);
+ event.setClosedLoopAlarmStart(Instant.now());
+ event.setClosedLoopEventStatus(ControlLoopEventStatus.ONSET);
+ event.setAai(new HashMap<>());
+ event.getAai().put(VNF_ID, ONSET_ONE);
+ return event;
+ }
private ControlLoopEventManager makeManager(VirtualControlLoopEvent event) {
- return new ControlLoopEventManager(event.getClosedLoopControlName(), event.getRequestId());
+ return new MyManager(event.getClosedLoopControlName(), event.getRequestId());
+ }
+
+ private static class MyManager extends ControlLoopEventManager implements Serializable {
+ private static final long serialVersionUID = 1L;
+
+ public MyManager(String closedLoopControlName, UUID requestId) {
+ super(closedLoopControlName, requestId);
+ }
+
+ @Override
+ protected Lock createRealLock(String targetEntity, UUID requestId, int holdSec, LockCallback callback) {
+ return createPseudoLock(targetEntity, requestId, holdSec, callback);
+ }
}
}
diff --git a/controlloop/common/eventmanager/src/test/java/org/onap/policy/controlloop/eventmanager/LockCallbackWorkingMemoryTest.java b/controlloop/common/eventmanager/src/test/java/org/onap/policy/controlloop/eventmanager/LockCallbackWorkingMemoryTest.java
new file mode 100644
index 000000000..18ab15b5a
--- /dev/null
+++ b/controlloop/common/eventmanager/src/test/java/org/onap/policy/controlloop/eventmanager/LockCallbackWorkingMemoryTest.java
@@ -0,0 +1,96 @@
+/*-
+ * ============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.controlloop.eventmanager;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertSame;
+import static org.mockito.Matchers.any;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+import org.drools.core.WorkingMemory;
+import org.junit.Before;
+import org.junit.Test;
+import org.kie.api.runtime.rule.FactHandle;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+import org.onap.policy.drools.core.lock.Lock;
+
+public class LockCallbackWorkingMemoryTest {
+ private static final String MY_NAME = "my-name";
+
+ @Mock
+ private WorkingMemory workingMemory;
+
+ @Mock
+ private Lock lock;
+
+ @Mock
+ private FactHandle fact;
+
+ private LockCallbackWorkingMemory callback;
+
+
+ /**
+ * Initializes mocks and creates a call-back.
+ */
+ @Before
+ public void setUp() {
+ MockitoAnnotations.initMocks(this);
+
+ when(workingMemory.getFactHandle(lock)).thenReturn(fact);
+
+ callback = new LockCallbackWorkingMemory(MY_NAME, workingMemory);
+ }
+
+ @Test
+ public void testLockCallbackWorkingMemory() {
+ assertEquals(MY_NAME, callback.getName());
+ assertSame(workingMemory, callback.getWorkingMemory());
+ }
+
+ @Test
+ public void testLockAvailable() {
+ callback.lockAvailable(lock);
+ verify(workingMemory).update(fact, lock);
+
+ // "remove" from working memory
+ when(workingMemory.getFactHandle(lock)).thenReturn(null);
+ callback.lockAvailable(lock);
+
+ // should be no additional calls
+ verify(workingMemory).update(any(), any());
+ }
+
+ @Test
+ public void testLockUnavailable() {
+ callback.lockUnavailable(lock);
+ verify(workingMemory).update(fact, lock);
+
+ // "remove" from working memory
+ when(workingMemory.getFactHandle(lock)).thenReturn(null);
+ callback.lockUnavailable(lock);
+
+ // should be no additional calls
+ verify(workingMemory).update(any(), any());
+ }
+
+}