summaryrefslogtreecommitdiffstats
path: root/controlloop/common/controller-usecases/src/test
diff options
context:
space:
mode:
authorJim Hahn <jrh3@att.com>2021-05-03 15:02:17 -0400
committerJim Hahn <jrh3@att.com>2021-05-04 18:17:10 -0400
commit7cc5fd31420bf71b41853a66c0a0b66bd9495ef3 (patch)
tree2ce679e75ece92137646c81ccc1306f7ca29948d /controlloop/common/controller-usecases/src/test
parentcbdf437729ca4d010147acfb208ecd90ef65777c (diff)
Refactor common code from UsecasesEventManager
Created ClEventManagerWithSteps, as a subclass of ControlLoopEventManager. It contains the "Steps" to be performed and also deals with outcomes. Created ClEventManagerWithEvent, as a subclass of that. It contains a VirtualControlLoopEvent object, and uses that to populate data used by the superclass. Updates per review comments: - made loadPolicy() protected - refactored another level to manage operation outcomes Issue-ID: POLICY-3262 Change-Id: Ibf5dd114746ae26e04fe37d562273fc81dd8cfbe Signed-off-by: Jim Hahn <jrh3@att.com>
Diffstat (limited to 'controlloop/common/controller-usecases/src/test')
-rw-r--r--controlloop/common/controller-usecases/src/test/java/org/onap/policy/drools/apps/controller/usecases/UsecasesEventManagerTest.java410
1 files changed, 8 insertions, 402 deletions
diff --git a/controlloop/common/controller-usecases/src/test/java/org/onap/policy/drools/apps/controller/usecases/UsecasesEventManagerTest.java b/controlloop/common/controller-usecases/src/test/java/org/onap/policy/drools/apps/controller/usecases/UsecasesEventManagerTest.java
index 7160f372f..318d6b7cc 100644
--- a/controlloop/common/controller-usecases/src/test/java/org/onap/policy/drools/apps/controller/usecases/UsecasesEventManagerTest.java
+++ b/controlloop/common/controller-usecases/src/test/java/org/onap/policy/drools/apps/controller/usecases/UsecasesEventManagerTest.java
@@ -22,17 +22,13 @@ package org.onap.policy.drools.apps.controller.usecases;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatCode;
-import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
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.ArgumentMatchers.any;
-import static org.mockito.ArgumentMatchers.anyLong;
-import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@@ -45,7 +41,6 @@ import java.util.Map;
import java.util.TreeMap;
import java.util.UUID;
import java.util.concurrent.ExecutorService;
-import java.util.concurrent.ForkJoinPool;
import org.drools.core.WorkingMemory;
import org.junit.Before;
import org.junit.Test;
@@ -56,17 +51,14 @@ import org.mockito.junit.MockitoJUnitRunner;
import org.onap.policy.common.utils.coder.Coder;
import org.onap.policy.common.utils.coder.CoderException;
import org.onap.policy.common.utils.coder.StandardYamlCoder;
-import org.onap.policy.common.utils.io.Serializer;
import org.onap.policy.common.utils.resources.ResourceUtils;
import org.onap.policy.controlloop.ControlLoopEventStatus;
import org.onap.policy.controlloop.ControlLoopException;
import org.onap.policy.controlloop.ControlLoopResponse;
import org.onap.policy.controlloop.ControlLoopTargetType;
import org.onap.policy.controlloop.VirtualControlLoopEvent;
-import org.onap.policy.controlloop.VirtualControlLoopNotification;
import org.onap.policy.controlloop.actorserviceprovider.ActorService;
import org.onap.policy.controlloop.actorserviceprovider.Operation;
-import org.onap.policy.controlloop.actorserviceprovider.OperationFinalResult;
import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
import org.onap.policy.controlloop.actorserviceprovider.OperationProperties;
import org.onap.policy.controlloop.actorserviceprovider.OperationResult;
@@ -77,7 +69,6 @@ import org.onap.policy.controlloop.actorserviceprovider.spi.Actor;
import org.onap.policy.controlloop.drl.legacy.ControlLoopParams;
import org.onap.policy.controlloop.eventmanager.ActorConstants;
import org.onap.policy.controlloop.ophistory.OperationHistoryDataManager;
-import org.onap.policy.drools.apps.controller.usecases.UsecasesEventManager.NewEventStatus;
import org.onap.policy.drools.apps.controller.usecases.step.AaiCqStep2;
import org.onap.policy.drools.apps.controller.usecases.step.AaiGetPnfStep2;
import org.onap.policy.drools.apps.controller.usecases.step.AaiGetTenantStep2;
@@ -104,13 +95,10 @@ public class UsecasesEventManagerTest {
private static final String SIMPLE_ACTOR = "First";
private static final String SIMPLE_OPERATION = "OperationA";
private static final String MY_TARGET = "my-target";
- private static final String EVENT_MGR_MULTI_YAML =
- "../eventmanager/src/test/resources/eventManager/event-mgr-multi.yaml";
private static final String EVENT_MGR_SIMPLE_YAML =
"../eventmanager/src/test/resources/eventManager/event-mgr-simple.yaml";
private static final Coder yamlCoder = new StandardYamlCoder();
private static final String OUTCOME_MSG = "my outcome message";
- private static final String MY_SINK = "my-topic-sink";
@Mock
private PolicyEngine engineMgr;
@@ -221,163 +209,6 @@ public class UsecasesEventManagerTest {
}
@Test
- public void testIsActive() throws Exception {
- mgr = new UsecasesEventManager(params, event, workMem);
- assertTrue(mgr.isActive());
-
- // deserialized manager should be inactive
- UsecasesEventManager mgr2 = Serializer.roundTrip(mgr);
- assertFalse(mgr2.isActive());
- }
-
- @Test
- public void testDestroy_testGetSteps() {
- // add some steps to the queue
- mgr.getSteps().add(stepa);
- mgr.getSteps().add(stepb);
-
- mgr.destroy();
-
- verify(stepa).cancel();
- verify(stepb).cancel();
-
- // if superclass destroy() was invoked, then freeLock() should have been submitted
- // to the executor
- verify(executor).execute(any());
- }
-
- @Test
- public void testOnStart() throws ControlLoopException {
- OperationOutcome outcome = makeOutcome();
-
- mgr.start();
- mgr.onStart(outcome);
-
- assertSame(outcome, mgr.getOutcomes().poll());
- assertThat(mgr.getOutcomes()).isEmpty();
-
- verify(workMem).update(factHandle, mgr);
- }
-
- @Test
- public void testOnComplete() throws ControlLoopException {
- OperationOutcome outcome = makeCompletedOutcome();
-
- mgr.start();
- mgr.onComplete(outcome);
-
- assertSame(outcome, mgr.getOutcomes().poll());
- assertThat(mgr.getOutcomes()).isEmpty();
-
- verify(workMem).update(factHandle, mgr);
- }
-
- @Test
- public void testToString() {
- assertNotNull(mgr.toString());
- }
-
- @Test
- public void testStart() throws ControlLoopException {
- // start it
- mgr.start();
-
- // cannot re-start
- assertThatCode(() -> mgr.start()).isInstanceOf(IllegalStateException.class)
- .hasMessage("manager already started");
- }
-
- /**
- * Tests start() when the manager is not in working memory.
- */
- @Test
- public void testStartNotInWorkingMemory() throws ControlLoopException {
- when(workMem.getFactHandle(any())).thenReturn(null);
-
- assertThatCode(() -> mgr.start()).isInstanceOf(IllegalStateException.class)
- .hasMessage("manager is not in working memory");
- }
-
- /**
- * Tests start() when the manager is not active.
- */
- @Test
- public void testStartInactive() throws Exception {
- // make an inactive manager by deserializing it
- mgr = Serializer.roundTrip(new UsecasesEventManager(params, event, workMem));
-
- // cannot re-start
- assertThatCode(() -> mgr.start()).isInstanceOf(IllegalStateException.class)
- .hasMessage("manager is no longer active");
- }
-
- @Test
- public void testAbort() {
- mgr.abort(UsecasesEventManager.State.DONE, OperationFinalResult.FINAL_FAILURE_GUARD, "some message");
-
- assertEquals(UsecasesEventManager.State.DONE, mgr.getState());
- assertEquals(OperationFinalResult.FINAL_FAILURE_GUARD, mgr.getFinalResult());
- assertEquals("some message", mgr.getFinalMessage());
-
- // try null state
- assertThatThrownBy(() -> mgr.abort(null, OperationFinalResult.FINAL_FAILURE_GUARD, ""))
- .isInstanceOf(NullPointerException.class).hasMessageContaining("finalState");
- }
-
- @Test
- public void testLoadNextPolicy_testGetFullHistory_testGetPartialHistory() throws Exception {
- loadPolicy(EVENT_MGR_MULTI_YAML);
- mgr = new MyManager(params, event, workMem);
-
- // start and load step for first policy
- mgr.start();
- assertEquals("OperationA", mgr.getSteps().poll().getOperationName());
- assertNull(mgr.getFinalResult());
-
- // add an outcome
- OperationOutcome outcome = makeOutcome();
- mgr.addToHistory(outcome);
-
- // indicate success and load next policy
- mgr.loadNextPolicy(OperationResult.SUCCESS);
- assertEquals("OperationB", mgr.getSteps().poll().getOperationName());
- assertNull(mgr.getFinalResult());
-
- // loadPolicy() should clear the partial history, but not the full history
- assertThat(mgr.getPartialHistory()).isEmpty();
- assertThat(mgr.getFullHistory()).hasSize(1);
-
- // indicate failure - should go to final failure
- mgr.loadNextPolicy(OperationResult.FAILURE);
- assertEquals(OperationFinalResult.FINAL_FAILURE, mgr.getFinalResult());
- }
-
- @Test
- public void testLoadPolicy() throws ControlLoopException {
- // start() will invoke loadPolicy()
- mgr.start();
-
- assertNull(mgr.getFinalResult());
-
- Step2 step = mgr.getSteps().peek();
- assertNotNull(step);
- assertEquals("First", step.getActorName());
- assertEquals("OperationA", step.getOperationName());
-
- ControlLoopOperationParams params2 = step.getParams();
- assertSame(actors, params2.getActorService());
- assertSame(REQ_ID, params2.getRequestId());
- assertSame(ForkJoinPool.commonPool(), params2.getExecutor());
- assertNotNull(params2.getTargetType());
- assertNotNull(params2.getTargetEntityIds());
- assertEquals(Integer.valueOf(300), params2.getTimeoutSec());
- assertEquals(Integer.valueOf(0), params2.getRetry());
- assertThat(params2.getPayload()).isEmpty();
- assertNotNull(params2.getStartCallback());
- assertNotNull(params2.getCompleteCallback());
- }
-
- @Test
public void testLoadPreprocessorSteps() {
stepa = new Step2(mgr, ControlLoopOperationParams.builder().build(), event) {
@Override
@@ -409,39 +240,6 @@ public class UsecasesEventManagerTest {
}
/**
- * Tests loadPreprocessorSteps() when there are too many steps in the queue.
- */
- @Test
- public void testLoadPreprocessorStepsTooManySteps() {
- loadStepsWithProperties(OperationProperties.AAI_PNF);
-
- Deque<Step2> steps = mgr.getSteps();
- stepa = steps.getFirst();
- steps.clear();
-
- // load up a bunch of steps
- for (int nsteps = 0; nsteps < UsecasesEventManager.MAX_STEPS; ++nsteps) {
- steps.add(stepa);
- }
-
- // should fail
- assertThatIllegalStateException().isThrownBy(() -> mgr.loadPreprocessorSteps()).withMessage("too many steps");
-
- // add another step, should still fail
- steps.add(stepa);
- assertThatIllegalStateException().isThrownBy(() -> mgr.loadPreprocessorSteps()).withMessage("too many steps");
-
- // remove two steps - should now succeed
- steps.remove();
- steps.remove();
-
- int nsteps = steps.size();
-
- mgr.loadPreprocessorSteps();
- assertEquals(nsteps + 1, steps.size());
- }
-
- /**
* Tests loadPreprocessorSteps() when no additional steps are needed.
*/
@Test
@@ -563,44 +361,6 @@ public class UsecasesEventManagerTest {
}
@Test
- public void testExecuteStep() {
- mgr.bumpAttempts();
-
- // no steps to execute
- assertFalse(mgr.executeStep());
- assertEquals(0, mgr.getAttempts());
-
- // add a step to the queue
- mgr.getSteps().add(stepa);
-
- // step returns false
- when(stepa.start(anyLong())).thenReturn(false);
- assertFalse(mgr.executeStep());
-
- // step returns true
- when(stepa.start(anyLong())).thenReturn(true);
- assertTrue(mgr.executeStep());
- }
-
- @Test
- public void testNextStep() {
- mgr.getSteps().add(stepa);
-
- mgr.nextStep();
-
- assertThat(mgr.getSteps()).isEmpty();
- }
-
- @Test
- public void testBumpAttempts() {
- assertEquals(0, mgr.getAttempts());
-
- mgr.bumpAttempts();
- mgr.bumpAttempts();
- assertEquals(2, mgr.getAttempts());
- }
-
- @Test
public void testIsAbort() {
OperationOutcome outcome = makeCompletedOutcome();
outcome.setResult(OperationResult.FAILURE);
@@ -619,137 +379,6 @@ public class UsecasesEventManagerTest {
}
@Test
- public void testAddToHistory() throws ControlLoopException {
- mgr.start();
-
- // add a "start" outcome
- OperationOutcome outcome = makeOutcome();
- mgr.addToHistory(outcome);
-
- assertThat(mgr.getPartialHistory()).hasSize(1);
- assertThat(mgr.getFullHistory()).hasSize(1);
-
- // add a "completion" outcome - should replace the start
- outcome = makeCompletedOutcome();
- mgr.addToHistory(outcome);
-
- assertThat(mgr.getPartialHistory()).hasSize(1);
- assertThat(mgr.getFullHistory()).hasSize(1);
- assertSame(outcome, mgr.getPartialHistory().peek().getOutcome());
- assertSame(outcome, mgr.getFullHistory().peek().getOutcome());
-
- // add another start
- outcome = makeOutcome();
- mgr.addToHistory(outcome);
-
- assertThat(mgr.getPartialHistory()).hasSize(2);
- assertThat(mgr.getFullHistory()).hasSize(2);
- assertSame(outcome, mgr.getPartialHistory().peekLast().getOutcome());
- assertSame(outcome, mgr.getFullHistory().peekLast().getOutcome());
-
- // remove the last item from the full history and then add a "completion"
- mgr.getFullHistory().removeLast();
- outcome = makeCompletedOutcome();
- mgr.addToHistory(outcome);
- assertThat(mgr.getPartialHistory()).hasSize(2);
- assertThat(mgr.getFullHistory()).hasSize(2);
-
- // add another "start"
- outcome = makeOutcome();
- mgr.addToHistory(outcome);
- assertThat(mgr.getPartialHistory()).hasSize(3);
- assertThat(mgr.getFullHistory()).hasSize(3);
-
- // add a "completion" for a different actor - should NOT replace the start
- outcome = makeCompletedOutcome();
- outcome.setActor("different-actor");
- mgr.addToHistory(outcome);
- assertThat(mgr.getPartialHistory()).hasSize(4);
- assertThat(mgr.getFullHistory()).hasSize(4);
- assertSame(outcome, mgr.getPartialHistory().peekLast().getOutcome());
- assertSame(outcome, mgr.getFullHistory().peekLast().getOutcome());
- }
-
- @Test
- public void testMakeNotification() throws Exception {
- loadPolicy(EVENT_MGR_MULTI_YAML);
- mgr = new MyManager(params, event, workMem);
-
- // before started
- assertNotNull(mgr.makeNotification());
-
- mgr.start();
-
- mgr.addToHistory(makeCompletedOutcome());
- mgr.addToHistory(makeCompletedOutcome());
- mgr.addToHistory(makeCompletedOutcome());
-
- // check notification while running
- VirtualControlLoopNotification notif = mgr.makeNotification();
- assertThat(notif.getMessage()).contains(SIMPLE_ACTOR);
- assertThat(notif.getHistory()).hasSize(3);
-
- // indicate success and load the next policy - should clear the partial history
- mgr.loadNextPolicy(OperationResult.SUCCESS);
-
- // check notification
- notif = mgr.makeNotification();
- assertNull(notif.getMessage());
- assertThat(notif.getHistory()).isEmpty();
-
- // add outcomes and check again
- mgr.addToHistory(makeCompletedOutcome());
- mgr.addToHistory(makeCompletedOutcome());
-
- notif = mgr.makeNotification();
- assertNotNull(notif.getMessage());
-
- // should only have history for last two outcomes
- assertThat(notif.getHistory()).hasSize(2);
-
- // indicate failure - should go to final state
- mgr.loadNextPolicy(OperationResult.FAILURE);
-
- // check notification
- notif = mgr.makeNotification();
- assertNull(notif.getMessage());
-
- // should be no history
- assertThat(notif.getHistory()).isEmpty();
-
- // null case
- assertThatThrownBy(() -> mgr.loadNextPolicy(null)).isInstanceOf(NullPointerException.class)
- .hasMessageContaining("lastResult");
- }
-
- @Test
- public void testDeliver() {
- mgr.deliver(MY_SINK, null, "null notification", "null rule");
- verify(engineMgr, never()).deliver(any(), any());
-
- mgr.deliver(MY_SINK, "publishA", "A notification", "A rule");
- verify(engineMgr).deliver(MY_SINK, "publishA");
-
- // cause deliver() to throw an exception
- when(engineMgr.deliver(any(), any())).thenThrow(new IllegalStateException("expected exception"));
- assertThatCode(() -> mgr.deliver(MY_SINK, "publishB", "B notification", "B rule")).doesNotThrowAnyException();
- }
-
- @Test
- public void testGetOperationMessage() throws ControlLoopException {
- // no history yet
- assertNull(mgr.getOperationMessage());
-
- // add an outcome
- mgr.start();
- OperationOutcome outcome = makeOutcome();
- mgr.addToHistory(outcome);
-
- assertThat(mgr.getOperationMessage()).contains("actor=" + SIMPLE_ACTOR)
- .contains("operation=" + SIMPLE_OPERATION);
- }
-
- @Test
public void testStoreInDataBase() throws ControlLoopException {
mgr.start();
OperationOutcome outcome = makeOutcome();
@@ -806,27 +435,11 @@ public class UsecasesEventManagerTest {
}
@Test
- public void testOnNewEvent() {
- VirtualControlLoopEvent event2 = new VirtualControlLoopEvent(event);
- assertEquals(NewEventStatus.FIRST_ONSET, mgr.onNewEvent(event2));
-
- event2.setPayload("other payload");
- assertEquals(NewEventStatus.SUBSEQUENT_ONSET, mgr.onNewEvent(event2));
- assertEquals(NewEventStatus.SUBSEQUENT_ONSET, mgr.onNewEvent(event2));
- assertEquals(NewEventStatus.FIRST_ONSET, mgr.onNewEvent(event));
-
- event2.setClosedLoopEventStatus(ControlLoopEventStatus.ABATED);
- assertEquals(NewEventStatus.FIRST_ABATEMENT, mgr.onNewEvent(event2));
-
- assertEquals(NewEventStatus.SUBSEQUENT_ABATEMENT, mgr.onNewEvent(event2));
- assertEquals(NewEventStatus.SUBSEQUENT_ABATEMENT, mgr.onNewEvent(event2));
-
- event2.setClosedLoopEventStatus(null);
- assertEquals(NewEventStatus.SYNTAX_ERROR, mgr.onNewEvent(event2));
- }
-
- @Test
public void testCheckEventSyntax() {
+ /*
+ * only need to check one success and one failure from the super class method
+ */
+
// initially, it's valid
assertThatCode(() -> mgr.checkEventSyntax(event)).doesNotThrowAnyException();
@@ -838,27 +451,19 @@ public class UsecasesEventManagerTest {
assertThatCode(() -> mgr.checkEventSyntax(event)).isInstanceOf(ControlLoopException.class)
.hasMessage("No target field");
- // abated supersedes previous errors - so it shouldn't throw an exception
- event.setClosedLoopEventStatus(ControlLoopEventStatus.ABATED);
- assertThatCode(() -> mgr.checkEventSyntax(event)).doesNotThrowAnyException();
-
event.setRequestId(null);
assertThatCode(() -> mgr.checkEventSyntax(event)).isInstanceOf(ControlLoopException.class)
.hasMessage("No request ID");
-
- event.setClosedLoopControlName(null);
- assertThatCode(() -> mgr.checkEventSyntax(event)).isInstanceOf(ControlLoopException.class)
- .hasMessage("No control loop name");
}
@Test
public void testValidateStatus() {
+ /*
+ * only need to check one success and one failure from the super class method
+ */
event.setClosedLoopEventStatus(ControlLoopEventStatus.ONSET);
assertThatCode(() -> mgr.checkEventSyntax(event)).doesNotThrowAnyException();
- event.setClosedLoopEventStatus(ControlLoopEventStatus.ABATED);
- assertThatCode(() -> mgr.checkEventSyntax(event)).doesNotThrowAnyException();
-
event.setClosedLoopEventStatus(null);
assertThatCode(() -> mgr.checkEventSyntax(event)).isInstanceOf(ControlLoopException.class)
.hasMessage("Invalid value in closedLoopEventStatus");
@@ -985,6 +590,7 @@ public class UsecasesEventManagerTest {
}
+
private Map<String, String> addAai(Map<String, String> original, String key, String value) {
Map<String, String> map = new TreeMap<>(original);
map.put(key, value);