diff options
Diffstat (limited to 'controlloop/m2/base/src/test')
4 files changed, 522 insertions, 0 deletions
diff --git a/controlloop/m2/base/src/test/java/org/onap/policy/m2/base/ActorOperationTest.java b/controlloop/m2/base/src/test/java/org/onap/policy/m2/base/ActorOperationTest.java new file mode 100644 index 000000000..4cbd05e03 --- /dev/null +++ b/controlloop/m2/base/src/test/java/org/onap/policy/m2/base/ActorOperationTest.java @@ -0,0 +1,116 @@ +/*- + * ============LICENSE_START======================================================= + * m2/base + * ================================================================================ + * Copyright (C) 2020 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.m2.base; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; + +import org.junit.Test; + +import org.onap.policy.controlloop.ControlLoopEvent; +import org.onap.policy.controlloop.ControlLoopException; +import org.onap.policy.controlloop.policy.Policy; +import org.onap.policy.controlloop.policy.PolicyResult; + +public class ActorOperationTest { + + public static final String ACTOR_NAME = "test"; + public static final String STATE = "COMPLETE"; + + public static class TestOperation implements Operation { + + @Override + public Object getRequest() throws ControlLoopException { + return "request"; + } + + @Override + public Policy getPolicy() { + return null; + } + + @Override + public String getState() { + return STATE; + } + + @Override + public int getAttempt() { + return 0; + } + + @Override + public PolicyResult getResult() { + return PolicyResult.SUCCESS; + } + + @Override + public String getMessage() { + return "success"; + } + + @Override + public void incomingMessage(Object object) { + return; + } + + @Override + public void timeout() { + return; + } + + } + + public static class TestActor implements Actor { + + @Override + public String getName() { + return ACTOR_NAME; + } + + @Override + public Operation createOperation(Transaction transaction, Policy policy, ControlLoopEvent onset, int attempt) { + return new TestOperation(); + } + + } + + @Test + public void getNameTest() { + Actor actor = new TestActor(); + assertEquals(ACTOR_NAME, actor.getName()); + } + + @Test + public void operationTest() throws ControlLoopException { + Actor actor = new TestActor(); + Operation operation = actor.createOperation(null, null, null, 0); + assertNotNull(operation); + assertEquals("request", operation.getRequest()); + assertNull(operation.getPolicy()); + assertEquals(STATE, operation.getState()); + assertEquals(0, operation.getAttempt()); + assertEquals(PolicyResult.SUCCESS, operation.getResult()); + assertEquals("success", operation.getMessage()); + } + +} diff --git a/controlloop/m2/base/src/test/java/org/onap/policy/m2/base/GuardAdjunctTest.java b/controlloop/m2/base/src/test/java/org/onap/policy/m2/base/GuardAdjunctTest.java new file mode 100644 index 000000000..eacfc8f6f --- /dev/null +++ b/controlloop/m2/base/src/test/java/org/onap/policy/m2/base/GuardAdjunctTest.java @@ -0,0 +1,103 @@ +/*- + * ============LICENSE_START======================================================= + * m2/base + * ================================================================================ + * Copyright (C) 2020 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.m2.base; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import java.time.Instant; +import java.util.UUID; + +import org.junit.BeforeClass; +import org.junit.Test; + +import org.onap.policy.controlloop.ControlLoopOperation; +import org.onap.policy.controlloop.policy.Policy; +import org.onap.policy.guard.GuardContext; +import org.powermock.reflect.Whitebox; + +public class GuardAdjunctTest { + private static final String ADJUNCT_CONTEXT_FIELD = "context"; + private static final String ADJUNCT_TRANSACTION_FIELD = "transaction"; + + private static GuardAdjunct adjunct; + private static Transaction transaction; + private static GuardContext context; + + /** + * Class-level initialization. + */ + @BeforeClass + public static void setup() { + transaction = new Transaction(null, "testCL", UUID.randomUUID(), null); + context = mock(GuardContext.class); + + GuardAdjunct.create(transaction, context); + adjunct = transaction.getAdjunct(GuardAdjunct.class); + } + + @Test + public void createGuardAdjunctTest() { + assertEquals(context, adjunct.get()); + } + + @Test + public void asyncQueryTest() { + Policy policy = new Policy(); + policy.setActor("APPCLCM"); + policy.setRecipe("test"); + + assertTrue(adjunct.asyncQuery(policy, "testTarget", UUID.randomUUID().toString())); + + GuardContext savedContext = Whitebox.getInternalState(adjunct, ADJUNCT_CONTEXT_FIELD); + Whitebox.setInternalState(adjunct, ADJUNCT_CONTEXT_FIELD, (GuardContext)null); + + try { + assertFalse(adjunct.asyncQuery(policy, "testTarget", UUID.randomUUID().toString())); + } finally { + Whitebox.setInternalState(adjunct, ADJUNCT_CONTEXT_FIELD, savedContext); + } + } + + @Test + public void asyncCreateDbEntryTest() { + ControlLoopOperation op = new ControlLoopOperation(); + op.setStart(Instant.now().minusSeconds(1)); + op.setEnd(Instant.now()); + op.setActor("testActor"); + op.setOperation("testOperation"); + op.setSubRequestId("0"); + op.setMessage("test"); + op.setOutcome("success"); + + adjunct.asyncCreateDbEntry(op, "testTarget"); + verify(context, times(1)).asyncCreateDbEntry(op.getStart(), op.getEnd(), + transaction.getClosedLoopControlName(), op.getActor(), + op.getOperation(), "testTarget", transaction.getRequestId().toString(), + op.getSubRequestId(), op.getMessage(), op.getOutcome()); + + } +} diff --git a/controlloop/m2/base/src/test/java/org/onap/policy/m2/base/TransactionTest.java b/controlloop/m2/base/src/test/java/org/onap/policy/m2/base/TransactionTest.java new file mode 100644 index 000000000..3c186ce6d --- /dev/null +++ b/controlloop/m2/base/src/test/java/org/onap/policy/m2/base/TransactionTest.java @@ -0,0 +1,230 @@ +/*- + * ============LICENSE_START======================================================= + * m2/base + * ================================================================================ + * Copyright (C) 2020 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.m2.base; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import java.time.Instant; +import java.util.LinkedList; +import java.util.UUID; + +import org.drools.core.WorkingMemory; +import org.drools.core.impl.StatefulKnowledgeSessionImpl; +import org.junit.Ignore; +import org.junit.Test; + +import org.onap.policy.controlloop.ControlLoopEventStatus; +import org.onap.policy.controlloop.ControlLoopNotification; +import org.onap.policy.controlloop.ControlLoopTargetType; +import org.onap.policy.controlloop.VirtualControlLoopEvent; +import org.onap.policy.controlloop.policy.ControlLoop; +import org.onap.policy.controlloop.policy.ControlLoopPolicy; +import org.onap.policy.controlloop.policy.FinalResult; +import org.onap.policy.controlloop.policy.Policy; + +public class TransactionTest { + + private VirtualControlLoopEvent setControlLoopEvent(UUID requestId, String closedLoopControlName, + Instant eventStart, String targetType, String target) { + + VirtualControlLoopEvent event = new VirtualControlLoopEvent(); + event.setClosedLoopControlName(closedLoopControlName); + event.setClosedLoopEventStatus(ControlLoopEventStatus.ONSET); + event.setRequestId(requestId); + event.setClosedLoopAlarmStart(eventStart); + event.setTarget(target); + event.setTargetType(targetType); + event.getAai().put("vserver.is-closed-loop-disabled", "false"); + event.getAai().put("complex.state", "NJ"); + event.getAai().put("vserver.l-interface.interface-name", "89ee9ee6-1e96-4063-b690-aa5ca9f73b32"); + event.getAai().put("vserver.l-interface.l3-interface-ipv4-address-list.l3-inteface-ipv4-address", + "135.144.3.49"); + event.getAai().put("vserver.l-interface.l3-interface-ipv6-address-list.l3-inteface-ipv6-address", null); + event.getAai().put("vserver.in-maint", "N"); + event.getAai().put("complex.city", "AAIDefault"); + event.getAai().put("vserver.vserver-id", "aa7a24f9-8791-491f-b31a-c8ba5ad9e2aa"); + event.getAai().put("vserver.l-interface.network-name", "vUSP_DPA3_OAM_3750"); + event.getAai().put("vserver.vserver-name", "ctsf0002vm013"); + event.getAai().put("generic-vnf.vnf-name", "ctsf0002v"); + event.getAai().put("generic-vnf.service-id", "e433710f-9217-458d-a79d-1c7aff376d89"); + event.getAai().put("vserver.selflink", "https://compute-aic.dpa3.cci.att.com:8774/v2/d0719b845a804b368f8ac0bba39e188b/servers/aa7a24f9-8791-491f-b31a-c8ba5ad9e2aa"); + event.getAai().put("generic-vnf.vnf-type", "vUSP - vCTS"); + event.getAai().put("tenant.tenant-id", "d0719b845a804b368f8ac0bba39e188b"); + event.getAai().put("cloud-region.identity-url", ""); + event.getAai().put("vserver.prov-status", "PROV"); + event.getAai().put("complex.physical-location-id", "LSLEILAA"); + + return event; + } + + private ControlLoopPolicy createControlLoop() { + + ControlLoop controlLoop = new ControlLoop(); + controlLoop.setControlLoopName("cltest"); + controlLoop.setTrigger_policy("testid"); + controlLoop.setTimeout(15); + + Policy policy = new Policy(); + policy.setActor("APPCLCM"); + policy.setId("testid"); + policy.setName("policytest"); + policy.setRecipe("restart"); + policy.setRetry(1); + policy.setTimeout(10); + policy.setSuccess(FinalResult.FINAL_SUCCESS.toString()); + policy.setFailure(FinalResult.FINAL_FAILURE.toString()); + policy.setFailure_exception(FinalResult.FINAL_FAILURE_EXCEPTION.toString()); + policy.setFailure_guard(FinalResult.FINAL_FAILURE_GUARD.toString()); + policy.setFailure_retries(FinalResult.FINAL_FAILURE_RETRIES.toString()); + policy.setFailure_timeout(FinalResult.FINAL_FAILURE_TIMEOUT.toString()); + + LinkedList<Policy> policies = new LinkedList<>(); + policies.add(policy); + + ControlLoopPolicy controlLoopPolicy = new ControlLoopPolicy(); + controlLoopPolicy.setControlLoop(controlLoop); + controlLoopPolicy.setPolicies(policies); + + return controlLoopPolicy; + } + + @Test + public void validControlLoopEventTest() { + VirtualControlLoopEvent event = setControlLoopEvent(UUID.randomUUID(), + "cltest", Instant.now(), ControlLoopTargetType.VM, "vserver.vserver-name"); + Transaction transaction = new Transaction(null, "clvusptest", event.getRequestId(), createControlLoop()); + assertTrue(transaction.isControlLoopEventValid(event)); + } + + @Test + public void noRequestIdControlLoopEventTest() { + VirtualControlLoopEvent event = setControlLoopEvent(null, + "cltest", Instant.now(), ControlLoopTargetType.VM, "vserver.vserver-name"); + Transaction transaction = new Transaction(null, "clvusptest", event.getRequestId(), createControlLoop()); + assertFalse(transaction.isControlLoopEventValid(event)); + assertEquals("No requestID", transaction.getNotificationMessage()); + } + + @Test + public void noTargetTypeControlLoopEventTest() { + VirtualControlLoopEvent event = setControlLoopEvent(UUID.randomUUID(), + "cltest", Instant.now(), null, "vserver.vserver-name"); + Transaction transaction = new Transaction(null, "clvusptest", event.getRequestId(), createControlLoop()); + assertFalse(transaction.isControlLoopEventValid(event)); + assertEquals("No targetType", transaction.getNotificationMessage()); + } + + @Test + public void noTargetControlLoopEventTest() { + VirtualControlLoopEvent event = setControlLoopEvent(UUID.randomUUID(), + "cltest", Instant.now(), ControlLoopTargetType.VM, null); + Transaction transaction = new Transaction(null, "clvusptest", event.getRequestId(), createControlLoop()); + assertFalse(transaction.isControlLoopEventValid(event)); + assertEquals("No target field", transaction.getNotificationMessage()); + } + + @Test + public void getClosedLoopControlNameTest() { + Transaction transaction = new Transaction(null, "clvusptest", UUID.randomUUID(), createControlLoop()); + assertEquals("clvusptest", transaction.getClosedLoopControlName()); + } + + @Test + public void getRequestIdTest() { + UUID requestId = UUID.randomUUID(); + Transaction transaction = new Transaction(null, "clvusptest", requestId, createControlLoop()); + assertEquals(requestId, transaction.getRequestId()); + } + + @Test + public void getWorkingMemoryTest() { + // Create mock working session + StatefulKnowledgeSessionImpl mockWorkingMemory = mock(StatefulKnowledgeSessionImpl.class); + Transaction transaction = new Transaction(mockWorkingMemory, "clvusptest", + UUID.randomUUID(), createControlLoop()); + assertEquals(mockWorkingMemory, transaction.getWorkingMemory()); + } + + @Test + public void getStateCompleteTest() { + Transaction transaction = new Transaction(null, "clvusptest", UUID.randomUUID(), createControlLoop()); + assertEquals("COMPLETE", transaction.getState()); + } + + @Test + public void getFinalResultTest() { + Transaction transaction = new Transaction(null, "clvusptest", UUID.randomUUID(), createControlLoop()); + assertEquals(null, transaction.getFinalResult()); + } + + @Test + public void finalResultFailureTest() { + Transaction transaction = new Transaction(null, "clvusptest", UUID.randomUUID(), createControlLoop()); + assertFalse(transaction.finalResultFailure()); + } + + @Test + public void getTimeoutTest() { + Transaction transaction = new Transaction(null, "clvusptest", UUID.randomUUID(), createControlLoop()); + assertEquals("15s", transaction.getTimeout()); + } + + @Test + public void getOperationTimeoutTest() { + Transaction transaction = new Transaction(null, "clvusptest", UUID.randomUUID(), createControlLoop()); + VirtualControlLoopEvent onset = setControlLoopEvent(UUID.randomUUID(), + "cltest", null, ControlLoopTargetType.VM, "vserver.vserver-name"); + transaction.setControlLoopEvent(onset); + assertEquals("10s", transaction.getOperationTimeout()); + } + + @Test + public void getCurrentPolicy() { + ControlLoopPolicy controlLoopPolicy = createControlLoop(); + Transaction transaction = new Transaction(null, "clvusptest", UUID.randomUUID(), controlLoopPolicy); + VirtualControlLoopEvent onset = setControlLoopEvent(UUID.randomUUID(), + "cltest", null, ControlLoopTargetType.VM, "vserver.vserver-name"); + transaction.setControlLoopEvent(onset); + assertEquals(controlLoopPolicy.getPolicies().get(0), transaction.getCurrentPolicy()); + } + + @Test + public void getOperationTest() { + Transaction transaction = new Transaction(null, "clvusptest", UUID.randomUUID(), createControlLoop()); + assertEquals(null, transaction.getCurrentOperation()); + } + + @Test + public void getNotificationTest() { + Transaction transaction = new Transaction(null, "clvusptest", UUID.randomUUID(), createControlLoop()); + VirtualControlLoopEvent onset = setControlLoopEvent(UUID.randomUUID(), + "cltest", null, ControlLoopTargetType.VM, "vserver.vserver-name"); + transaction.setControlLoopEvent(onset); + ControlLoopNotification notification = transaction.getNotification(onset); + assertEquals(onset.getClosedLoopControlName(), notification.getClosedLoopControlName()); + assertEquals(onset.getRequestId(), notification.getRequestId()); + assertEquals(onset.getTarget(), notification.getTarget()); + } +} diff --git a/controlloop/m2/base/src/test/java/org/onap/policy/m2/base/UtilTest.java b/controlloop/m2/base/src/test/java/org/onap/policy/m2/base/UtilTest.java new file mode 100644 index 000000000..53d97209b --- /dev/null +++ b/controlloop/m2/base/src/test/java/org/onap/policy/m2/base/UtilTest.java @@ -0,0 +1,73 @@ +/*- + * ============LICENSE_START======================================================= + * m2/base + * ================================================================================ + * Copyright (C) 2020 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.m2.base; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.mockito.Matchers.any; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import java.util.Properties; + +import org.junit.BeforeClass; +import org.junit.Test; +import org.onap.policy.common.endpoints.event.comm.TopicEndpointManager; +import org.onap.policy.drools.core.PolicyContainer; +import org.onap.policy.drools.core.PolicySession; +import org.onap.policy.drools.system.PolicyController; + +public class UtilTest { + + private static PolicySession session; + + /** + * Class-level initialization. + */ + @BeforeClass + public static void setup() { + PolicyContainer container = mock(PolicyContainer.class); + when(container.getGroupId()).thenReturn("org.onap.policy"); + when(container.getArtifactId()).thenReturn("test"); + when(container.getVersion()).thenReturn("1.0.0"); + + session = mock(PolicySession.class); + when(session.getPolicyContainer()).thenReturn(container); + } + + @Test + public void deliverTest() { + Properties prop = new Properties(); + prop.put("noop.sink.topics", "testTopic"); + TopicEndpointManager.getManager().addTopicSinks(prop); + + // throws an exception: + // java.lang.IllegalStateException: Policy Engine is stopped + // if policy engine is started, it still throws an exception: + // java.lang.IllegalArgumentException: no reverse coder has been found + //assertTrue(Util.deliver("testTopic", "test")); + } + + @Test(expected = IllegalStateException.class) + public void deliverNoTopicTest() { + Util.deliver("noTopic", "test"); + } +} |