diff options
Diffstat (limited to 'controlloop/common/eventmanager/src/test/java')
4 files changed, 440 insertions, 0 deletions
diff --git a/controlloop/common/eventmanager/src/test/java/org/onap/policy/controlloop/Util.java b/controlloop/common/eventmanager/src/test/java/org/onap/policy/controlloop/Util.java new file mode 100644 index 000000000..bf44a8026 --- /dev/null +++ b/controlloop/common/eventmanager/src/test/java/org/onap/policy/controlloop/Util.java @@ -0,0 +1,67 @@ +/*- + * ============LICENSE_START======================================================= + * util + * ================================================================================ + * 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.controlloop; + +import static org.junit.Assert.fail; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.InputStream; +import java.nio.charset.StandardCharsets; + +import org.apache.commons.io.IOUtils; +import org.yaml.snakeyaml.Yaml; +import org.yaml.snakeyaml.constructor.Constructor; + +import org.onap.policy.controlloop.policy.ControlLoopPolicy; + +public final class Util { + + public static class Pair<A, B> { + public final A a; + public final B b; + + public Pair(A a, B b) { + this.a = a; + this.b = b; + } + } + + public static Pair<ControlLoopPolicy, String> loadYaml(String testFile) { + try (InputStream is = new FileInputStream(new File(testFile))) { + String contents = IOUtils.toString(is, StandardCharsets.UTF_8); + // + // Read the yaml into our Java Object + // + Yaml yaml = new Yaml(new Constructor(ControlLoopPolicy.class)); + Object obj = yaml.load(contents); + return new Pair<ControlLoopPolicy, String>((ControlLoopPolicy) obj, contents); + } catch (FileNotFoundException e) { + fail(e.getLocalizedMessage()); + } catch (IOException e) { + fail(e.getLocalizedMessage()); + } + return null; + } + +} 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 new file mode 100644 index 000000000..fa52e8557 --- /dev/null +++ b/controlloop/common/eventmanager/src/test/java/org/onap/policy/controlloop/eventmanager/ControlLoopEventManagerTest.java @@ -0,0 +1,33 @@ +/*- + * ============LICENSE_START======================================================= + * unit test + * ================================================================================ + * 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.controlloop.eventmanager; + + +import org.junit.Test; + +public class ControlLoopEventManagerTest { + + @Test + public void test() { + //fail("MICHAEL - Not yet implemented"); + } + +} diff --git a/controlloop/common/eventmanager/src/test/java/org/onap/policy/controlloop/eventmanager/ControlLoopOperationManagerTest.java b/controlloop/common/eventmanager/src/test/java/org/onap/policy/controlloop/eventmanager/ControlLoopOperationManagerTest.java new file mode 100644 index 000000000..1cb8b5a5d --- /dev/null +++ b/controlloop/common/eventmanager/src/test/java/org/onap/policy/controlloop/eventmanager/ControlLoopOperationManagerTest.java @@ -0,0 +1,248 @@ +/*- + * ============LICENSE_START======================================================= + * unit test + * ================================================================================ + * 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.controlloop.eventmanager; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import java.time.Instant; +import java.util.HashMap; +import java.util.UUID; + +import org.junit.Test; +import org.onap.policy.appc.Request; +import org.onap.policy.appc.Response; +import org.onap.policy.appc.ResponseCode; +import org.onap.policy.appc.ResponseValue; +import org.onap.policy.controlloop.ControlLoopEventStatus; + +import org.onap.policy.controlloop.VirtualControlLoopEvent; +import org.onap.policy.controlloop.ControlLoopException; +import org.onap.policy.controlloop.Util; +import org.onap.policy.controlloop.policy.ControlLoopPolicy; +import org.onap.policy.controlloop.policy.PolicyResult; +import org.onap.policy.controlloop.processor.ControlLoopProcessor; + +public class ControlLoopOperationManagerTest { + + private static VirtualControlLoopEvent onset; + static { + onset = new VirtualControlLoopEvent(); + onset.requestID = UUID.randomUUID(); + onset.target = "vserver.selflink"; + onset.closedLoopAlarmStart = Instant.now(); + onset.AAI = new HashMap<String, String>(); + onset.AAI.put("cloud-region.identity-url", "foo"); + onset.AAI.put("vserver.selflink", "bar"); + onset.AAI.put("vserver.is-closed-loop-disabled", "false"); + onset.AAI.put("generic-vnf.vnf-name", "testTriggerSource"); + onset.closedLoopEventStatus = ControlLoopEventStatus.ONSET; + } + + @Test + public void testRetriesFail() { + // + // Load up the policy + // + final Util.Pair<ControlLoopPolicy, String> pair = Util.loadYaml("src/test/resources/test.yaml"); + onset.closedLoopControlName = pair.a.controlLoop.controlLoopName; + try { + // + // Create a processor + // + ControlLoopProcessor processor = new ControlLoopProcessor(pair.b); + // + // create the manager + // + ControlLoopEventManager eventManager = new ControlLoopEventManager(onset.closedLoopControlName, onset.requestID); + + ControlLoopOperationManager manager = new ControlLoopOperationManager(onset, processor.getCurrentPolicy(), eventManager); + System.out.println(manager); + // + // + // + assertFalse(manager.isOperationComplete()); + assertFalse(manager.isOperationRunning()); + // + // Start + // + Object request = manager.startOperation(onset); + System.out.println(manager); + assertNotNull(request); + assertTrue(request instanceof Request); + assertTrue(((Request)request).CommonHeader.SubRequestID.contentEquals("1")); + assertFalse(manager.isOperationComplete()); + assertTrue(manager.isOperationRunning()); + // + // Accept + // + Response response = new Response((Request) request); + response.Status.Code = ResponseCode.ACCEPT.getValue(); + response.Status.Value = ResponseValue.ACCEPT.toString(); + // + // + // + PolicyResult result = manager.onResponse(response); + System.out.println(manager); + assertTrue(result == null); + assertFalse(manager.isOperationComplete()); + assertTrue(manager.isOperationRunning()); + // + // Now we are going to Fail it + // + response = new Response((Request) request); + response.Status.Code = ResponseCode.FAILURE.getValue(); + response.Status.Value = ResponseValue.FAILURE.toString(); + response.Status.Description = "AppC failed for some reason"; + result = manager.onResponse(response); + System.out.println(manager); + assertTrue(result.equals(PolicyResult.FAILURE)); + assertFalse(manager.isOperationComplete()); + assertFalse(manager.isOperationRunning()); + // + // Retry it + // + request = manager.startOperation(onset); + System.out.println(manager); + assertNotNull(request); + assertTrue(request instanceof Request); + assertTrue(((Request)request).CommonHeader.SubRequestID.contentEquals("2")); + assertFalse(manager.isOperationComplete()); + assertTrue(manager.isOperationRunning()); + // + // + // + response = new Response((Request) request); + System.out.println(manager); + response.Status.Code = ResponseCode.ACCEPT.getValue(); + response.Status.Value = ResponseValue.ACCEPT.toString(); + // + // + // + result = manager.onResponse(response); + System.out.println(manager); + assertTrue(result == null); + assertFalse(manager.isOperationComplete()); + assertTrue(manager.isOperationRunning()); + // + // Now we are going to Fail it + // + response = new Response((Request) request); + response.Status.Code = ResponseCode.FAILURE.getValue(); + response.Status.Value = ResponseValue.FAILURE.toString(); + response.Status.Description = "AppC failed for some reason"; + result = manager.onResponse(response); + System.out.println(manager); + assertTrue(result.equals(PolicyResult.FAILURE)); + // + // Should be complete now + // + assertTrue(manager.isOperationComplete()); + assertFalse(manager.isOperationRunning()); + assertNotNull(manager.getOperationResult()); + assertTrue(manager.getOperationResult().equals(PolicyResult.FAILURE_RETRIES)); + assertTrue(manager.getHistory().size() == 2); + } catch (ControlLoopException e) { + fail(e.getMessage()); + } + } + + @Test + public void testTimeout() { + // + // Load up the policy + // + final Util.Pair<ControlLoopPolicy, String> pair = Util.loadYaml("src/test/resources/test.yaml"); + onset.closedLoopControlName = pair.a.controlLoop.controlLoopName; + try { + // + // Create a processor + // + ControlLoopProcessor processor = new ControlLoopProcessor(pair.b); + // + // create the manager + // + ControlLoopEventManager eventManager = new ControlLoopEventManager(onset.closedLoopControlName, onset.requestID); + + ControlLoopOperationManager manager = new ControlLoopOperationManager(onset, processor.getCurrentPolicy(), eventManager); + // + // + // + System.out.println(manager); + assertFalse(manager.isOperationComplete()); + assertFalse(manager.isOperationRunning()); + // + // Start + // + Object request = manager.startOperation(onset); + System.out.println(manager); + assertNotNull(request); + assertTrue(request instanceof Request); + assertTrue(((Request)request).CommonHeader.SubRequestID.contentEquals("1")); + assertFalse(manager.isOperationComplete()); + assertTrue(manager.isOperationRunning()); + // + // Accept + // + Response response = new Response((Request) request); + response.Status.Code = ResponseCode.ACCEPT.getValue(); + response.Status.Value = ResponseValue.ACCEPT.toString(); + // + // + // + PolicyResult result = manager.onResponse(response); + System.out.println(manager); + assertTrue(result == null); + assertFalse(manager.isOperationComplete()); + assertTrue(manager.isOperationRunning()); + // + // Now we are going to simulate Timeout + // + manager.setOperationHasTimedOut(); + System.out.println(manager); + assertTrue(manager.isOperationComplete()); + assertFalse(manager.isOperationRunning()); + assertTrue(manager.getHistory().size() == 1); + assertTrue(manager.getOperationResult().equals(PolicyResult.FAILURE_TIMEOUT)); + // + // Now we are going to Fail the previous request + // + response = new Response((Request) request); + response.Status.Code = ResponseCode.FAILURE.getValue(); + response.Status.Value = ResponseValue.FAILURE.toString(); + response.Status.Description = "AppC failed for some reason"; + result = manager.onResponse(response); + System.out.println(manager); + // + // + // + assertTrue(manager.isOperationComplete()); + assertFalse(manager.isOperationRunning()); + assertTrue(manager.getHistory().size() == 1); + assertTrue(manager.getOperationResult().equals(PolicyResult.FAILURE_TIMEOUT)); + } catch (ControlLoopException e) { + fail(e.getMessage()); + } + } + +} diff --git a/controlloop/common/eventmanager/src/test/java/org/onap/policy/controlloop/processor/ControlLoopProcessorTest.java b/controlloop/common/eventmanager/src/test/java/org/onap/policy/controlloop/processor/ControlLoopProcessorTest.java new file mode 100644 index 000000000..2ed216658 --- /dev/null +++ b/controlloop/common/eventmanager/src/test/java/org/onap/policy/controlloop/processor/ControlLoopProcessorTest.java @@ -0,0 +1,92 @@ +/*- + * ============LICENSE_START======================================================= + * unit test + * ================================================================================ + * 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.controlloop.processor; + +import static org.junit.Assert.*; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.InputStream; +import java.nio.charset.StandardCharsets; + +import org.apache.commons.io.IOUtils; +import org.junit.Test; + +import org.onap.policy.controlloop.ControlLoopException; +import org.onap.policy.controlloop.policy.FinalResult; +import org.onap.policy.controlloop.policy.Policy; +import org.onap.policy.controlloop.policy.PolicyResult; + +public class ControlLoopProcessorTest { + + @Test + public void test() { + try (InputStream is = new FileInputStream(new File("src/test/resources/test.yaml"))) { + String result = IOUtils.toString(is, StandardCharsets.UTF_8); + this.testSuccess(result); + this.testFailure(result); + } catch (FileNotFoundException e) { + e.printStackTrace(); + fail(e.getMessage()); + } catch (IOException e) { + e.printStackTrace(); + fail(e.getMessage()); + } catch (ControlLoopException e) { + e.printStackTrace(); + fail(e.getMessage()); + } + } + + public void testSuccess(String yaml) throws ControlLoopException { + ControlLoopProcessor processor = new ControlLoopProcessor(yaml); + System.out.println("testSuccess: " + processor.getControlLoop().toString()); + while (true) { + FinalResult result = processor.checkIsCurrentPolicyFinal(); + if (result != null) { + System.out.println(result); + break; + } + Policy policy = processor.getCurrentPolicy(); + assertNotNull(policy); + System.out.println("current policy is: " + policy.id + " " + policy.name); + processor.nextPolicyForResult(PolicyResult.SUCCESS); + } + } + + public void testFailure(String yaml) throws ControlLoopException { + ControlLoopProcessor processor = new ControlLoopProcessor(yaml); + System.out.println("testFailure: " + processor.getControlLoop().toString()); + while (true) { + FinalResult result = processor.checkIsCurrentPolicyFinal(); + if (result != null) { + System.out.println(result); + break; + } + Policy policy = processor.getCurrentPolicy(); + assertNotNull(policy); + System.out.println("current policy is: " + policy.id + " " + policy.name); + processor.nextPolicyForResult(PolicyResult.FAILURE); + } + } + +} |