From 09c388b5c9781383a0577bf8c2f574f806abe85c Mon Sep 17 00:00:00 2001 From: Pamela Dragosh Date: Mon, 24 Aug 2020 08:05:15 -0400 Subject: Remove deprecated SDC and Model-yaml Deprecated as we now use TOSCA and the SDC catalog objects isn't useful for control loop design and implementation. Issue-ID: POLICY-2428 Change-Id: Ib4adfbf25ba70c3cad47a8494333a1f20a5c4e23 Signed-off-by: Pamela Dragosh --- .../compiler/CompilerExceptionTest.java | 33 - .../compiler/ControlLoopCompilerTest.java | 197 ----- .../compiler/ControlLoopGuardCompilerTest.java | 82 -- .../policy/ControlLoopPolicyBuilderTest.java | 912 --------------------- .../controlloop/policy/ControlLoopPolicyTest.java | 115 --- .../policy/controlloop/policy/ControlLoopTest.java | 186 ----- .../policy/OperationsAccumulateParamsTest.java | 125 --- .../onap/policy/controlloop/policy/PolicyTest.java | 221 ----- .../controlloop/policy/guard/ConstraintTest.java | 259 ------ .../policy/guard/ControlLoopGuardBuilderTest.java | 203 ----- .../policy/guard/ControlLoopGuardTest.java | 160 ---- .../controlloop/policy/guard/GuardPolicyTest.java | 274 ------- 12 files changed, 2767 deletions(-) delete mode 100644 models-interactions/model-yaml/src/test/java/org/onap/policy/controlloop/compiler/CompilerExceptionTest.java delete mode 100644 models-interactions/model-yaml/src/test/java/org/onap/policy/controlloop/compiler/ControlLoopCompilerTest.java delete mode 100644 models-interactions/model-yaml/src/test/java/org/onap/policy/controlloop/compiler/ControlLoopGuardCompilerTest.java delete mode 100644 models-interactions/model-yaml/src/test/java/org/onap/policy/controlloop/policy/ControlLoopPolicyBuilderTest.java delete mode 100644 models-interactions/model-yaml/src/test/java/org/onap/policy/controlloop/policy/ControlLoopPolicyTest.java delete mode 100644 models-interactions/model-yaml/src/test/java/org/onap/policy/controlloop/policy/ControlLoopTest.java delete mode 100644 models-interactions/model-yaml/src/test/java/org/onap/policy/controlloop/policy/OperationsAccumulateParamsTest.java delete mode 100644 models-interactions/model-yaml/src/test/java/org/onap/policy/controlloop/policy/PolicyTest.java delete mode 100644 models-interactions/model-yaml/src/test/java/org/onap/policy/controlloop/policy/guard/ConstraintTest.java delete mode 100644 models-interactions/model-yaml/src/test/java/org/onap/policy/controlloop/policy/guard/ControlLoopGuardBuilderTest.java delete mode 100644 models-interactions/model-yaml/src/test/java/org/onap/policy/controlloop/policy/guard/ControlLoopGuardTest.java delete mode 100644 models-interactions/model-yaml/src/test/java/org/onap/policy/controlloop/policy/guard/GuardPolicyTest.java (limited to 'models-interactions/model-yaml/src/test/java') diff --git a/models-interactions/model-yaml/src/test/java/org/onap/policy/controlloop/compiler/CompilerExceptionTest.java b/models-interactions/model-yaml/src/test/java/org/onap/policy/controlloop/compiler/CompilerExceptionTest.java deleted file mode 100644 index e3ab76b6b..000000000 --- a/models-interactions/model-yaml/src/test/java/org/onap/policy/controlloop/compiler/CompilerExceptionTest.java +++ /dev/null @@ -1,33 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2018 Ericsson. All rights reserved. - * Modifications Copyright (C) 2019 Nordix Foundation. - * Modifications 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.compiler; - -import org.junit.Test; -import org.onap.policy.common.utils.test.ExceptionsTester; - -public class CompilerExceptionTest extends ExceptionsTester { - - @Test - public void test() { - test(CompilerException.class); - } - -} diff --git a/models-interactions/model-yaml/src/test/java/org/onap/policy/controlloop/compiler/ControlLoopCompilerTest.java b/models-interactions/model-yaml/src/test/java/org/onap/policy/controlloop/compiler/ControlLoopCompilerTest.java deleted file mode 100644 index 71d67f07b..000000000 --- a/models-interactions/model-yaml/src/test/java/org/onap/policy/controlloop/compiler/ControlLoopCompilerTest.java +++ /dev/null @@ -1,197 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * policy-yaml unit test - * ================================================================================ - * Copyright (C) 2017-2019 AT&T Intellectual Property. All rights reserved. - * Modifications Copyright (C) 2019-2020 Nordix Foundation. - * ================================================================================ - * 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.compiler; - -import static org.assertj.core.api.Assertions.assertThatThrownBy; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; - -import java.io.File; -import java.io.FileInputStream; -import java.io.InputStream; -import java.util.ArrayList; -import java.util.List; -import org.junit.Test; -import org.onap.policy.controlloop.policy.ControlLoopPolicy; -import org.onap.policy.controlloop.policy.FinalResult; - -public class ControlLoopCompilerTest { - private static final String RESTART_UNKNOWN_POLICY = - "Operation Policy unique-policy-id-1-restart is connected to unknown policy unknown-policy"; - - @Test - public void testTest() throws Exception { - List expectedOnErrorMessages = new ArrayList<>(); - expectedOnErrorMessages.add("Operational Policy has an bad ID"); - expectedOnErrorMessages.add("Policy id is set to a PolicyResult SUCCESS"); - expectedOnErrorMessages.add("Policy id is set to a FinalResult FINAL_SUCCESS"); - expectedOnErrorMessages.add("Policy actor is null"); - expectedOnErrorMessages.add("Policy recipe is null"); - expectedOnErrorMessages.add("Policy target is null"); - expectedOnErrorMessages.add("Policy target is invalid"); - expectedOnErrorMessages.add("Policy success is neither another policy nor FINAL_SUCCESS"); - expectedOnErrorMessages.add("Policy failure is neither another policy nor FINAL_FAILURE"); - expectedOnErrorMessages.add("Policy failure retries is neither another policy nor FINAL_FAILURE_RETRIES"); - expectedOnErrorMessages.add("Policy failure timeout is neither another policy nor FINAL_FAILURE_TIMEOUT"); - expectedOnErrorMessages.add("Policy failure exception is neither another policy nor FINAL_FAILURE_EXCEPTION"); - expectedOnErrorMessages.add("Policy failure guard is neither another policy nor FINAL_FAILURE_GUARD"); - expectedOnErrorMessages.add("Unsupported version for this compiler"); - expectedOnErrorMessages.add("controlLoop overall timeout is less than the sum of operational policy timeouts."); - - TestControlLoopCompilerCallback testControlLoopCompilerCallback = - new TestControlLoopCompilerCallback(expectedOnErrorMessages); - ControlLoopPolicy controlLoopPolicy = - this.test("src/test/resources/v1.0.0/test.yaml", testControlLoopCompilerCallback); - assertEquals(22, controlLoopPolicy.getPolicies().size()); - assertTrue(testControlLoopCompilerCallback.areAllExpectedOnErrorsReceived()); - } - - @Test - public void testSuccessConnectedToUnknownPolicy() throws Exception { - assertThatThrownBy(() -> { - this.test("src/test/resources/v1.0.0/bad_policy_success_connected_to_unknown_policy.yaml"); - }).isInstanceOf(CompilerException.class).hasMessage(RESTART_UNKNOWN_POLICY); - } - - @Test - public void testFailureConnectedToUnknownPolicy() throws Exception { - assertThatThrownBy(() -> { - this.test("src/test/resources/v1.0.0/bad_policy_failure_connected_to_unknown_policy.yaml"); - }).isInstanceOf(CompilerException.class).hasMessage(RESTART_UNKNOWN_POLICY); - } - - @Test - public void testFailureTimeoutToUnknownPolicy() throws Exception { - assertThatThrownBy(() -> { - this.test("src/test/resources/v1.0.0/bad_policy_failure_timeout_connected_to_unknown_policy.yaml"); - }).isInstanceOf(CompilerException.class).hasMessage(RESTART_UNKNOWN_POLICY); - } - - @Test - public void testFailureRetriesToUnknownPolicy() throws Exception { - assertThatThrownBy(() -> { - this.test("src/test/resources/v1.0.0/bad_policy_failure_retries_connected_to_unknown_policy.yaml"); - }).isInstanceOf(CompilerException.class).hasMessage(RESTART_UNKNOWN_POLICY); - } - - @Test - public void testFailureExceptionToUnknownPolicy() throws Exception { - assertThatThrownBy(() -> { - this.test("src/test/resources/v1.0.0/bad_policy_failure_exception_connected_to_unknown_policy.yaml"); - }).isInstanceOf(CompilerException.class).hasMessage(RESTART_UNKNOWN_POLICY); - } - - @Test - public void testFailureGuardToUnknownPolicy() throws Exception { - assertThatThrownBy(() -> { - this.test("src/test/resources/v1.0.0/bad_policy_failure_guard_connected_to_unknown_policy.yaml"); - }).isInstanceOf(CompilerException.class).hasMessage(RESTART_UNKNOWN_POLICY); - } - - @Test - public void testInvalidTriggerPolicyId() throws Exception { - assertThatThrownBy(() -> { - this.test("src/test/resources/v1.0.0/bad_trigger_1.yaml"); - }).isInstanceOf(CompilerException.class).hasMessage("Unexpected value for trigger_policy, should only be " - + FinalResult.FINAL_OPENLOOP.toString() + " or a valid Policy ID"); - } - - @Test - public void testNoTriggerPolicyId() throws Exception { - assertThatThrownBy(() -> { - this.test("src/test/resources/v1.0.0/bad_trigger_no_trigger_id.yaml"); - }).isInstanceOf(CompilerException.class); - } - - @Test - public void testNoControlLoopName() throws Exception { - List expectedOnErrorMessages = new ArrayList<>(); - expectedOnErrorMessages.add("Missing controlLoopName"); - expectedOnErrorMessages.add("Unsupported version for this compiler"); - TestControlLoopCompilerCallback testControlLoopCompilerCallback = - new TestControlLoopCompilerCallback(expectedOnErrorMessages); - this.test("src/test/resources/v1.0.0/bad_control_loop_no_control_loop_name.yaml", - testControlLoopCompilerCallback); - assertTrue(testControlLoopCompilerCallback.areAllExpectedOnErrorsReceived()); - } - - @Test - public void testInvalidFinalResult() throws Exception { - assertThatThrownBy(() -> { - this.test("src/test/resources/v1.0.0/bad_trigger_2.yaml"); - }).isInstanceOf(CompilerException.class).hasMessage( - "Unexpected Final Result for trigger_policy, should only be FINAL_OPENLOOP or a valid Policy ID"); - } - - @Test - public void testCompileEmptyFile() throws Exception { - assertThatThrownBy(() -> { - this.test("src/test/resources/v1.0.0/empty.yaml"); - }).isInstanceOf(CompilerException.class).hasMessage("Could not parse yaml specification."); - } - - public ControlLoopPolicy test(String testFile) throws Exception { - return test(testFile, null); - } - - /** - * Does the actual test. - * - * @param testFile test file - * @param controlLoopCompilerCallback callback method - * @return the policy object - * @throws Exception exception - */ - public ControlLoopPolicy test(String testFile, ControlLoopCompilerCallback controlLoopCompilerCallback) - throws Exception { - try (InputStream is = new FileInputStream(new File(testFile))) { - return ControlLoopCompiler.compile(is, controlLoopCompilerCallback); - } - } - - class TestControlLoopCompilerCallback implements ControlLoopCompilerCallback { - - private List expectedOnErrorMessages; - - public TestControlLoopCompilerCallback(List expectedOnErrorMessages) { - this.expectedOnErrorMessages = expectedOnErrorMessages; - } - - @Override - public boolean onWarning(String message) { - return true; - } - - @Override - public boolean onError(String message) { - if (!expectedOnErrorMessages.remove(message)) { - fail("Unexpected onError message: " + message); - } - return true; - } - - public boolean areAllExpectedOnErrorsReceived() { - return expectedOnErrorMessages.isEmpty(); - } - } -} diff --git a/models-interactions/model-yaml/src/test/java/org/onap/policy/controlloop/compiler/ControlLoopGuardCompilerTest.java b/models-interactions/model-yaml/src/test/java/org/onap/policy/controlloop/compiler/ControlLoopGuardCompilerTest.java deleted file mode 100644 index b963a830d..000000000 --- a/models-interactions/model-yaml/src/test/java/org/onap/policy/controlloop/compiler/ControlLoopGuardCompilerTest.java +++ /dev/null @@ -1,82 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * policy-yaml unit test - * ================================================================================ - * Copyright (C) 2017-2019 AT&T Intellectual Property. All rights reserved. - * Modifications Copyright (C) 2019 Nordix Foundation. - * ================================================================================ - * 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.compiler; - -import static org.assertj.core.api.Assertions.assertThatThrownBy; - -import java.io.File; -import java.io.FileInputStream; -import java.io.InputStream; -import org.junit.Test; -import org.onap.policy.controlloop.guard.compiler.ControlLoopGuardCompiler; - -public class ControlLoopGuardCompilerTest { - - private static final String ACTOR_ERROR = "Unable to find property 'actor'"; - - @Test - public void testTest1() throws Exception { - this.test("src/test/resources/v2.0.0-guard/policy_guard_ONAP_demo_vDNS.yaml"); - } - - @Test - public void testTest2() throws Exception { - this.test("src/test/resources/v2.0.0-guard/policy_guard_appc_restart.yaml"); - } - - @Test - public void testBad1() { - assertThatThrownBy(() -> this.test("src/test/resources/v2.0.0-guard/no_guard_policy.yaml")) - .hasMessage("Guard policies should not be null"); - } - - @Test - public void testBad2() { - assertThatThrownBy(() -> this.test("src/test/resources/v2.0.0-guard/duplicate_guard_policy.yaml")) - .hasMessageContaining(ACTOR_ERROR); - } - - @Test - public void testBad3() { - assertThatThrownBy(() -> this.test("src/test/resources/v2.0.0-guard/no_guard_constraint.yaml")) - .hasMessageContaining(ACTOR_ERROR); - } - - @Test - public void testBad4() { - assertThatThrownBy(() -> this.test("src/test/resources/v2.0.0-guard/duplicate_guard_constraint.yaml")) - .hasMessageContaining(ACTOR_ERROR); - } - - /** - * Does the actual test. - * - * @param testFile input test file - * @throws Exception exception thrown - */ - public void test(String testFile) throws Exception { - try (InputStream is = new FileInputStream(new File(testFile))) { - ControlLoopGuardCompiler.compile(is, null); - } - } - -} diff --git a/models-interactions/model-yaml/src/test/java/org/onap/policy/controlloop/policy/ControlLoopPolicyBuilderTest.java b/models-interactions/model-yaml/src/test/java/org/onap/policy/controlloop/policy/ControlLoopPolicyBuilderTest.java deleted file mode 100644 index 86823400c..000000000 --- a/models-interactions/model-yaml/src/test/java/org/onap/policy/controlloop/policy/ControlLoopPolicyBuilderTest.java +++ /dev/null @@ -1,912 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * policy-yaml unit test - * ================================================================================ - * Copyright (C) 2017-2020 AT&T Intellectual Property. All rights reserved. - * Modifications Copyright (C) 2019-2020 Nordix Foundation. - * ================================================================================ - * 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.policy; - -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.assertTrue; - -import java.io.File; -import java.io.FileInputStream; -import java.io.InputStream; -import java.util.UUID; -import org.junit.Ignore; -import org.junit.Test; -import org.onap.aai.domain.yang.Pnf; -import org.onap.policy.controlloop.policy.builder.BuilderException; -import org.onap.policy.controlloop.policy.builder.ControlLoopPolicyBuilder; -import org.onap.policy.controlloop.policy.builder.Message; -import org.onap.policy.controlloop.policy.builder.MessageLevel; -import org.onap.policy.controlloop.policy.builder.Results; -import org.onap.policy.sdc.Resource; -import org.onap.policy.sdc.ResourceType; -import org.onap.policy.sdc.Service; -import org.yaml.snakeyaml.Yaml; -import org.yaml.snakeyaml.constructor.Constructor; -import org.yaml.snakeyaml.error.YAMLException; - -public class ControlLoopPolicyBuilderTest { - - private static final String RESOURCE1 = "resource1"; - private static final String TRIGGER_RESTART = "Upon getting the trigger event, restart the VM"; - private static final String UNKNOWN_POLICY = "Unknown policy "; - private static final String RESTART = "Restart"; - private static final String RESTART_VM = "Restart the VM"; - private static final String REBUILD = "Rebuild"; - private static final String REBUILD_VM = "Rebuild VM"; - private static final String REBUILD_RESTART = "If the restart fails, rebuild it."; - - @Test - public void testControlLoop() throws BuilderException { - // - // Create a builder for our policy - // - ControlLoopPolicyBuilder builder = - ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400); - // - // Test add services - // - Service scp = new Service("vSCP"); - Service usp = new Service("vUSP"); - Service trinity = new Service("Trinity"); - builder = builder.addService(scp, usp, trinity); - assertTrue(builder.getControlLoop().getServices().size() == 3); - // - // Test remove services - // - builder = builder.removeService(scp); - assertTrue(builder.getControlLoop().getServices().size() == 2); - builder = builder.removeAllServices(); - assertTrue(builder.getControlLoop().getServices().isEmpty()); - // - // Test add resources - // - Resource cts = new Resource("vCTS", ResourceType.VF); - Resource com = new Resource("vCTS", ResourceType.VF); - Resource rar = new Resource("vCTS", ResourceType.VF); - builder = builder.addResource(cts, com, rar); - assertTrue(builder.getControlLoop().getResources().size() == 3); - // - // Test remove resources - // - builder = builder.removeResource(cts); - assertTrue(builder.getControlLoop().getResources().size() == 2); - builder = builder.removeAllResources(); - assertTrue(builder.getControlLoop().getResources().isEmpty()); - } - - @Test - public void testAddNullService() throws BuilderException { - ControlLoopPolicyBuilder builder = - ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400); - - assertThatThrownBy(() -> { - builder.addService((Service) null); - }).isInstanceOf(BuilderException.class).hasMessage("Service must not be null"); - } - - @Test - public void testAddInvalidService() throws BuilderException { - ControlLoopPolicyBuilder builder = - ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400); - - assertThatThrownBy(() -> { - builder.addService(new Service()); - }).isInstanceOf(BuilderException.class) - .hasMessage("Invalid service - need either a serviceUUID or serviceName"); - } - - @Test - public void testAddServiceWithUuid() throws BuilderException { - ControlLoopPolicyBuilder builder = - ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400); - UUID uuid = UUID.randomUUID(); - Service serviceWithUuid = new Service(uuid); - builder.addService(serviceWithUuid); - assertTrue(builder.getControlLoop().getServices().size() == 1); - } - - @Test - public void testAddNullResource() throws BuilderException { - ControlLoopPolicyBuilder builder = - ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400); - - assertThatThrownBy(() -> { - builder.addResource((Resource) null); - }).isInstanceOf(BuilderException.class).hasMessage("Resource must not be null"); - } - - @Test - public void testAddInvalidResource() throws BuilderException { - ControlLoopPolicyBuilder builder = - ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400); - - assertThatThrownBy(() -> { - builder.addResource(new Resource()); - }).isInstanceOf(BuilderException.class) - .hasMessage("Invalid resource - need either resourceUUID or resourceName"); - } - - @Test - public void testAddAndRemoveResourceWithUuid() throws BuilderException { - ControlLoopPolicyBuilder builder = - ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400); - UUID uuid = UUID.randomUUID(); - Resource resourceWithUuid = new Resource(uuid); - builder.addResource(resourceWithUuid); - assertTrue(builder.getControlLoop().getResources().size() == 1); - - builder.removeResource(resourceWithUuid); - assertTrue(builder.getControlLoop().getResources().isEmpty()); - } - - @Test - public void testRemoveNullResource() throws BuilderException { - ControlLoopPolicyBuilder builder = - ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400); - Resource resource = new Resource(RESOURCE1, ResourceType.VF); - builder.addResource(resource); - - assertThatThrownBy(() -> { - builder.removeResource((Resource) null); - }).isInstanceOf(BuilderException.class).hasMessage("Resource must not be null"); - } - - @Test - public void testRemoveResourceNoExistingResources() throws BuilderException { - ControlLoopPolicyBuilder builder = - ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400); - - assertThatThrownBy(() -> { - builder.removeResource(new Resource(RESOURCE1, ResourceType.VF)); - }).isInstanceOf(BuilderException.class).hasMessage("No existing resources to remove"); - } - - @Test - public void testRemoveInvalidResource() throws BuilderException { - ControlLoopPolicyBuilder builder = - ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400); - Resource resource = new Resource(RESOURCE1, ResourceType.VF); - builder.addResource(resource); - - assertThatThrownBy(() -> { - builder.removeResource(new Resource()); - }).isInstanceOf(BuilderException.class) - .hasMessage("Invalid resource - need either a resourceUUID or resourceName"); - } - - @Test - public void testRemoveUnknownResource() throws BuilderException { - ControlLoopPolicyBuilder builder = - ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400); - Resource resource = new Resource(RESOURCE1, ResourceType.VF); - builder.addResource(resource); - final String unknownResourceName = "reource2"; - - assertThatThrownBy(() -> { - builder.removeResource(new Resource(unknownResourceName, ResourceType.VF)); - }).isInstanceOf(BuilderException.class).hasMessage("Unknown resource " + unknownResourceName); - } - - @Test - public void testControlLoopWithInitialResourceAndServices() throws BuilderException { - Resource cts = new Resource("vCTS", ResourceType.VF); - Service scp = new Service("vSCP"); - Service usp = new Service("vUSP"); - ControlLoopPolicyBuilder builder = - ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400, cts, scp, usp); - assertTrue(builder.getControlLoop().getResources().size() == 1); - assertTrue(builder.getControlLoop().getServices().size() == 2); - } - - @Test - public void testControlLoopWithInitialResourcesAndService() throws BuilderException { - Resource cts = new Resource("vCTS", ResourceType.VF); - Resource com = new Resource("vCTS", ResourceType.VF); - Service scp = new Service("vSCP"); - ControlLoopPolicyBuilder builder = - ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400, scp, cts, com); - assertTrue(builder.getControlLoop().getServices().size() == 1); - assertTrue(builder.getControlLoop().getResources().size() == 2); - } - - @Test - @Ignore - // I'VE MARKED THIS TEST CASE AS IGNORE BECAUSE THE TEST CASE FAILS - // This test case fails because builder.getControlLoop() returns an instance of ControlLoop - // copied using - // the ControlLoop(ControlLoop controlLoop) constructor. - // This constructor does not copy the value of pnf into the newly created object - // On the face of it, this looks like a bug, but perhaps there is a reason for this - // PLEASE ADVISE IF THE BEHAVIOUR IS INCORRECT OR THE TEST CASE IS INVALID - public void testControlLoopForPnf() throws BuilderException { - Pnf pnf = new Pnf(); - // pnf.setPnfType(PnfType.ENODEB); - ControlLoopPolicyBuilder builder = - ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400, pnf); - assertEquals(pnf, builder.getControlLoop().getPnf()); - - builder.removePnf(); - assertNull(builder.getControlLoop().getPnf()); - } - - @Test - @Ignore - // Fails for the same reason as the above test case - public void testSetAndRemovePnf() throws BuilderException { - ControlLoopPolicyBuilder builder = - ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400); - assertNull(builder.getControlLoop().getPnf()); - - Pnf pnf = new Pnf(); - // pnf.setPnfType(PnfType.ENODEB); - builder.setPnf(pnf); - assertEquals(pnf, builder.getControlLoop().getPnf()); - - builder.removePnf(); - assertNull(builder.getControlLoop().getPnf()); - } - - @Test - public void testSetNullPnf() throws BuilderException { - ControlLoopPolicyBuilder builder = - ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400); - - assertThatThrownBy(() -> { - builder.setPnf(null); - }).isInstanceOf(BuilderException.class).hasMessage("PNF must not be null"); - } - - @Test - public void testSetInvalidPnf() throws BuilderException { - ControlLoopPolicyBuilder builder = - ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400); - - assertThatThrownBy(() -> { - builder.setPnf(new Pnf()); - }).isInstanceOf(BuilderException.class).hasMessage("Invalid PNF - need either pnfName or pnfType"); - } - - @Test - public void testSetAbatement() throws BuilderException { - ControlLoopPolicyBuilder builder = - ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400); - assertFalse(builder.getControlLoop().getAbatement()); - builder = builder.setAbatement(true); - assertTrue(builder.getControlLoop().getAbatement()); - } - - @Test - public void testSetNullAbatement() throws BuilderException { - assertThatThrownBy(() -> { - ControlLoopPolicyBuilder builder = - ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400); - - builder = builder.setAbatement(null); - }).isInstanceOf(BuilderException.class).hasMessage("abatement must not be null"); - } - - @Test - public void testTimeout() throws BuilderException { - // - // Create a builder for our policy - // - ControlLoopPolicyBuilder builder = - ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400); - // - // Test setTimeout - // - assertTrue(builder.getControlLoop().getTimeout() == 2400); - builder = builder.setTimeout(800); - assertTrue(builder.getControlLoop().getTimeout() == 800); - // - // Test calculateTimeout - // - // @formatter:off - Policy trigger = builder.setTriggerPolicy(PolicyParam.builder().id(UUID.randomUUID().toString()) - .name(RESTART_VM) - .description(TRIGGER_RESTART) - .actor("APPC").target(new Target(TargetType.VM)) - .recipe(RESTART) - .payload(null) - .retries(2) - .timeout(300) - .build()); - - @SuppressWarnings("unused") - Policy onRestartFailurePolicy = builder.setPolicyForPolicyResult( - PolicyParam.builder() - .name(REBUILD_VM) - .description("If the restart fails, rebuild it").actor("APPC") - .target(new Target(TargetType.VM)) - .recipe(REBUILD) - .payload(null) - .retries(1) - .timeout(600) - .id(trigger.getId()) - .build(), - PolicyResult.FAILURE, PolicyResult.FAILURE_RETRIES, PolicyResult.FAILURE_TIMEOUT); - assertEquals(Integer.valueOf(300 + 600), builder.calculateTimeout()); - // @formatter:on - } - - @Test - public void testTriggerPolicyMethods() throws BuilderException { - ControlLoopPolicyBuilder builder = - ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400); - // - // Test isOpenLoop - // - assertTrue(builder.isOpenLoop()); - // - // Test set initial trigger policy - // - // @formatter:off - Policy triggerPolicy1 = builder.setTriggerPolicy(PolicyParam.builder().id(UUID.randomUUID().toString()) - .name(RESTART_VM) - .description(TRIGGER_RESTART) - .actor("APPC") - .target(new Target(TargetType.VM)) - .recipe(RESTART) - .payload(null) - .retries(2) - .timeout(300) - .build()); - assertFalse(builder.isOpenLoop()); - assertEquals(builder.getControlLoop().getTrigger_policy(), triggerPolicy1.getId()); - // - // Set trigger policy to a new policy - // - @SuppressWarnings("unused") - Policy triggerPolicy2 = builder.setTriggerPolicy(PolicyParam.builder().id(UUID.randomUUID().toString()) - .name("Rebuild the VM") - .description("Upon getting the trigger event, rebuild the VM").actor("APPC") - .target(new Target(TargetType.VM)) - .recipe(REBUILD) - .payload(null) - .retries(2) - .timeout(300) - .build()); - // @formatter:on - // - // Test set trigger policy to another existing policy - // - @SuppressWarnings("unused") - ControlLoop cl = builder.setExistingTriggerPolicy(triggerPolicy1.getId()); - assertTrue(builder.getControlLoop().getTrigger_policy().equals(triggerPolicy1.getId())); - // - // Test get trigger policy - // - assertTrue(builder.getTriggerPolicy().equals(triggerPolicy1)); - } - - @Test - public void testSetTriggerPolicyNullPolicyId() throws BuilderException { - ControlLoopPolicyBuilder builder = - ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400); - - assertThatThrownBy(() -> { - builder.setExistingTriggerPolicy(null); - }).isInstanceOf(BuilderException.class).hasMessage("Id must not be null"); - } - - @Test - public void testSetTriggerPolicyNoPoliciesExist() throws BuilderException { - ControlLoopPolicyBuilder builder = - ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400); - final String unknownPolicyId = "100"; - - assertThatThrownBy(() -> { - builder.setExistingTriggerPolicy(unknownPolicyId); - }).isInstanceOf(BuilderException.class).hasMessage(UNKNOWN_POLICY + unknownPolicyId); - } - - @Test - public void testSetTriggerPolicyUnknownPolicy() throws BuilderException { - ControlLoopPolicyBuilder builder = - ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400); - // @formatter:off - builder.setTriggerPolicy(PolicyParam.builder().id(UUID.randomUUID().toString()) - .name(RESTART_VM) - .description(TRIGGER_RESTART) - .actor("APPC") - .target(new Target(TargetType.VM)) - .recipe(RESTART) - .payload(null) - .retries(2) - .timeout(300) - .build()); - // @formatter:on - final String unknownPolicyId = "100"; - - assertThatThrownBy(() -> { - builder.setExistingTriggerPolicy(unknownPolicyId); - }).isInstanceOf(BuilderException.class).hasMessage(UNKNOWN_POLICY + unknownPolicyId); - } - - @Test - public void testAddRemovePolicies() throws BuilderException { - ControlLoopPolicyBuilder builder = - ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400); - // @formatter:off - Policy triggerPolicy = builder.setTriggerPolicy(PolicyParam.builder().id(UUID.randomUUID().toString()) - .name(RESTART_VM) - .description(TRIGGER_RESTART) - .actor("APPC") - .target(new Target(TargetType.VM)) - .recipe(RESTART) - .payload(null) - .retries(2) - .timeout(300) - .build()); - // - // Test create a policy and chain it to the results of trigger policy - // - Policy onRestartFailurePolicy1 = builder.setPolicyForPolicyResult(PolicyParam.builder() - .name(REBUILD_VM) - .description(REBUILD_RESTART) - .actor("APPC") - .target(new Target(TargetType.VM)) - .recipe(REBUILD).payload(null) - .retries(1) - .timeout(600) - .id(triggerPolicy.getId()) - .build(), - PolicyResult.FAILURE, PolicyResult.FAILURE_EXCEPTION, PolicyResult.FAILURE_RETRIES, - PolicyResult.FAILURE_TIMEOUT, PolicyResult.FAILURE_GUARD); - // - assertTrue(builder.getTriggerPolicy().getFailure().equals(onRestartFailurePolicy1.getId())); - assertTrue(builder.getTriggerPolicy().getFailure_exception().equals(onRestartFailurePolicy1.getId())); - assertTrue(builder.getTriggerPolicy().getFailure_retries().equals(onRestartFailurePolicy1.getId())); - assertTrue(builder.getTriggerPolicy().getFailure_timeout().equals(onRestartFailurePolicy1.getId())); - assertTrue(builder.getTriggerPolicy().getFailure_guard().equals(onRestartFailurePolicy1.getId())); - - // - // Test create a policy and chain it to the results of trigger policy success - // - Policy onSuccessPolicy1 = builder.setPolicyForPolicyResult(PolicyParam.builder() - .name("Do something") - .description("If the restart succeeds, do something else.") - .actor("APPC") - .target(new Target(TargetType.VM)) - .recipe("SomethingElse") - .payload(null) - .retries(1) - .timeout(600) - .id(triggerPolicy.getId()) - .build(), - PolicyResult.SUCCESS); - // - // @formatter:on - assertTrue(builder.getTriggerPolicy().getSuccess().equals(onSuccessPolicy1.getId())); - - // - // Test remove policy - // - boolean removed = builder.removePolicy(onRestartFailurePolicy1.getId()); - assertTrue(removed); - assertTrue(builder.getTriggerPolicy().getFailure().equals(FinalResult.FINAL_FAILURE.toString())); - assertTrue( - builder.getTriggerPolicy().getFailure_retries().equals(FinalResult.FINAL_FAILURE_RETRIES.toString())); - assertTrue( - builder.getTriggerPolicy().getFailure_timeout().equals(FinalResult.FINAL_FAILURE_TIMEOUT.toString())); - assertTrue(builder.getTriggerPolicy().getFailure_guard().equals(FinalResult.FINAL_FAILURE_GUARD.toString())); - // - // Create another policy and chain it to the results of trigger policy - // - // @formatter:off - final Policy onRestartFailurePolicy2 = builder.setPolicyForPolicyResult( - PolicyParam.builder() - .name(REBUILD_VM) - .description(REBUILD_RESTART) - .actor("APPC") - .target(new Target(TargetType.VM)) - .recipe(REBUILD) - .payload(null) - .retries(2) - .timeout(600) - .id(triggerPolicy.getId()) - .build(), - PolicyResult.FAILURE, PolicyResult.FAILURE_RETRIES, PolicyResult.FAILURE_TIMEOUT); - // @formatter:on - // - // Test reset policy results - // - triggerPolicy = builder.resetPolicyResults(triggerPolicy.getId()); - assertTrue(builder.getTriggerPolicy().getFailure().equals(FinalResult.FINAL_FAILURE.toString())); - assertTrue( - builder.getTriggerPolicy().getFailure_retries().equals(FinalResult.FINAL_FAILURE_RETRIES.toString())); - assertTrue( - builder.getTriggerPolicy().getFailure_timeout().equals(FinalResult.FINAL_FAILURE_TIMEOUT.toString())); - // - // Test set the policy results to an existing operational policy - // - Policy onRestartFailurePolicy3 = builder.setPolicyForPolicyResult(onRestartFailurePolicy2.getId(), - triggerPolicy.getId(), PolicyResult.FAILURE, PolicyResult.FAILURE_RETRIES, PolicyResult.FAILURE_TIMEOUT); - assertTrue(builder.getTriggerPolicy().getFailure().equals(onRestartFailurePolicy3.getId())); - assertTrue(builder.getTriggerPolicy().getFailure_retries().equals(onRestartFailurePolicy3.getId())); - assertTrue(builder.getTriggerPolicy().getFailure_timeout().equals(onRestartFailurePolicy3.getId())); - // - // Test set the policy result for success to an existing operational policy - // - Policy onRestartFailurePolicy4 = builder.setPolicyForPolicyResult(onRestartFailurePolicy2.getId(), - triggerPolicy.getId(), PolicyResult.FAILURE, PolicyResult.FAILURE_EXCEPTION, PolicyResult.FAILURE_GUARD, - PolicyResult.FAILURE_RETRIES, PolicyResult.FAILURE_TIMEOUT, PolicyResult.SUCCESS); - assertTrue(builder.getTriggerPolicy().getFailure().equals(onRestartFailurePolicy4.getId())); - assertTrue(builder.getTriggerPolicy().getFailure_exception().equals(onRestartFailurePolicy4.getId())); - assertTrue(builder.getTriggerPolicy().getFailure_guard().equals(onRestartFailurePolicy4.getId())); - assertTrue(builder.getTriggerPolicy().getFailure_retries().equals(onRestartFailurePolicy4.getId())); - assertTrue(builder.getTriggerPolicy().getFailure_timeout().equals(onRestartFailurePolicy4.getId())); - assertTrue(builder.getTriggerPolicy().getSuccess().equals(onRestartFailurePolicy4.getId())); - - // - // Test remove all existing operational policies - // - builder = builder.removeAllPolicies(); - assertTrue(builder.getControlLoop().getTrigger_policy().equals(FinalResult.FINAL_OPENLOOP.toString())); - } - - @Test - public void testAddToUnknownPolicy() throws BuilderException { - ControlLoopPolicyBuilder builder = - ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400); - final String policyId = "100"; - - assertThatThrownBy(() -> { - // @formatter:off - builder.setPolicyForPolicyResult( - PolicyParam.builder() - .name(REBUILD_VM) - .description(REBUILD_RESTART) - .actor("APPC") - .target(new Target(TargetType.VM)) - .recipe(REBUILD) - .payload(null) - .retries(1) - .timeout(600) - .id(policyId) - .build(), - PolicyResult.FAILURE, PolicyResult.FAILURE_RETRIES, PolicyResult.FAILURE_TIMEOUT, - PolicyResult.FAILURE_GUARD); - // @formatter:on - }).isInstanceOf(BuilderException.class).hasMessage(UNKNOWN_POLICY + policyId); - - } - - @Test - public void testAddExistingPolicyToUnknownPolicy() throws BuilderException { - ControlLoopPolicyBuilder builder = - ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400); - // @formatter:off - Policy triggerPolicy = builder.setTriggerPolicy(PolicyParam.builder() - .id(UUID.randomUUID().toString()) - .name(RESTART_VM) - .description(TRIGGER_RESTART) - .actor("APPC") - .target(new Target(TargetType.VM)) - .recipe(RESTART) - .payload(null) - .retries(2) - .timeout(300) - .build()); - - Policy onRestartFailurePolicy = builder.setPolicyForPolicyResult(PolicyParam.builder() - .name(REBUILD_VM) - .description(REBUILD_RESTART) - .actor("APPC") - .target(new Target(TargetType.VM)) - .recipe(REBUILD) - .payload(null) - .retries(1) - .timeout(600) - .id(triggerPolicy.getId()) - .build(), - PolicyResult.FAILURE); - - // @formatter:on - final String unknownPolicyId = "100"; - - assertThatThrownBy(() -> { - builder.setPolicyForPolicyResult(onRestartFailurePolicy.getId(), unknownPolicyId, PolicyResult.FAILURE); - }).isInstanceOf(BuilderException.class).hasMessage(unknownPolicyId + " does not exist"); - - } - - @Test - public void testAddUnknownExistingPolicyToPolicy() throws BuilderException { - ControlLoopPolicyBuilder builder = - ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400); - // @formatter:off - Policy triggerPolicy = builder.setTriggerPolicy(PolicyParam.builder() - .id(UUID.randomUUID().toString()) - .name(RESTART_VM) - .description(TRIGGER_RESTART) - .actor("APPC") - .target(new Target(TargetType.VM)) - .recipe(RESTART) - .payload(null) - .retries(2) - .timeout(300) - .build()); - // @formatter:on - - final String unknownPolicyId = "100"; - - assertThatThrownBy(() -> { - builder.setPolicyForPolicyResult(unknownPolicyId, triggerPolicy.getId(), PolicyResult.FAILURE); - }).isInstanceOf(BuilderException.class).hasMessage("Operational policy " + unknownPolicyId + " does not exist"); - - } - - @Test - public void testAddOperationsAccumulateParams() throws BuilderException { - ControlLoopPolicyBuilder builder = - ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400); - // @formatter:off - Policy triggerPolicy = - builder.setTriggerPolicy(PolicyParam.builder() - .id(UUID.randomUUID().toString()) - .name("Restart the eNodeB") - .description("Upon getting the trigger event, restart the eNodeB") - .actor("RANController") - .target(new Target(TargetType.PNF)) - .recipe(RESTART) - .payload(null) - .retries(2) - .timeout(300) - .build()); - // @formatter:on - // - // Add the operationsAccumulateParams - // - triggerPolicy = - builder.addOperationsAccumulateParams(triggerPolicy.getId(), new OperationsAccumulateParams("15m", 5)); - assertNotNull(builder.getTriggerPolicy().getOperationsAccumulateParams()); - assertEquals("15m", builder.getTriggerPolicy().getOperationsAccumulateParams().getPeriod()); - assertTrue(builder.getTriggerPolicy().getOperationsAccumulateParams().getLimit() == 5); - } - - @Test - public void testBuildSpecification() throws BuilderException { - // - // Create the builder - // - ControlLoopPolicyBuilder builder = - ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 800); - // - // Set the first invalid trigger policy - // - // @formatter:off - final Policy policy1 = builder.setTriggerPolicy( - PolicyParam.builder() - .id(UUID.randomUUID().toString()) - .name(RESTART_VM) - .description(TRIGGER_RESTART) - .actor(null) - .target(null) - .recipe(null) - .payload(null) - .retries(2) - .timeout(300) - .build()); - Results results = builder.buildSpecification(); - // @formatter:on - // - // Check that ERRORs are in results for invalid policy arguments - // - boolean invalidActor = false; - boolean invalidRecipe = false; - boolean invalidTarget = false; - for (Message m : results.getMessages()) { - if ("Policy actor is null".equals(m.getMessage()) && m.getLevel() == MessageLevel.ERROR) { - invalidActor = true; - } - if ("Policy recipe is null".equals(m.getMessage()) && m.getLevel() == MessageLevel.ERROR) { - invalidRecipe = true; - } - if ("Policy target is null".equals(m.getMessage()) && m.getLevel() == MessageLevel.ERROR) { - invalidTarget = true; - } - } - // - assertTrue(invalidActor); - assertTrue(invalidRecipe); - assertTrue(invalidTarget); - // - // Remove the invalid policy - // - // @SuppressWarnings("unused") - boolean removed = builder.removePolicy(policy1.getId()); - assertTrue(removed); - assertTrue(builder.getTriggerPolicy() == null); - // - // Set a valid trigger policy - // - // @formatter:off - Policy policy1a = builder.setTriggerPolicy(PolicyParam.builder() - .id(UUID.randomUUID().toString()) - .name(REBUILD_VM) - .description(REBUILD_RESTART) - .actor("APPC") - .target(new Target(TargetType.VM)) - .recipe(REBUILD) - .payload(null) - .retries(1) - .timeout(600) - .build()); - // - // Set a second valid trigger policy - // - final Policy policy2 = builder.setTriggerPolicy(PolicyParam.builder() - .id(UUID.randomUUID().toString()) - .name(RESTART_VM) - .description(TRIGGER_RESTART) - .actor("APPC") - .target(new Target(TargetType.VM)) - .recipe(RESTART) - .payload(null) - .retries(2) - .timeout(300) - .build()); - // @formatter:on - // - // Now, we have policy1 unreachable - // - results = builder.buildSpecification(); - boolean unreachable = false; - for (Message m : results.getMessages()) { - if (m.getMessage().equals("Policy " + policy1a.getId() + " is not reachable.") - && m.getLevel() == MessageLevel.WARNING) { - unreachable = true; - break; - } - } - assertTrue(unreachable); - // - // Set policy1a for the failure results of policy2 - // - policy1a = builder.setPolicyForPolicyResult(policy1a.getId(), policy2.getId(), PolicyResult.FAILURE, - PolicyResult.FAILURE_RETRIES, PolicyResult.FAILURE_TIMEOUT); - results = builder.buildSpecification(); - boolean invalidTimeout = false; - for (Message m : results.getMessages()) { - if ("controlLoop overall timeout is less than the sum of operational policy timeouts." - .equals(m.getMessage()) && m.getLevel() == MessageLevel.ERROR) { - invalidTimeout = true; - break; - } - } - assertTrue(invalidTimeout); - // - // Remove policy2 (revert controlLoop back to open loop) - // - removed = builder.removePolicy(policy2.getId()); - // - // ControlLoop is open loop now, but it still has policies (policy1) - // - results = builder.buildSpecification(); - unreachable = false; - for (Message m : results.getMessages()) { - if ("Open Loop policy contains policies. The policies will never be invoked.".equals(m.getMessage()) - && m.getLevel() == MessageLevel.WARNING) { - unreachable = true; - break; - } - } - assertTrue(unreachable); - } - - @Test - public void test1() throws Exception { - this.test("src/test/resources/v1.0.0/policy_Test.yaml"); - } - - @Test - public void testEvilYaml() throws Exception { - try (InputStream is = new FileInputStream(new File("src/test/resources/v1.0.0/test_evil.yaml"))) { - // - // Attempt to read the yaml into our Java Object - // - Yaml yaml = new Yaml(new Constructor(ControlLoopPolicy.class)); - assertThatThrownBy(() -> yaml.load(is)).isInstanceOf(YAMLException.class); - } - } - - /** - * Does the actual test. - * - * @param testFile input file - * @throws Exception if an error occurs - */ - public void test(String testFile) throws Exception { - try (InputStream is = new FileInputStream(new File(testFile))) { - // - // Read the yaml into our Java Object - // - Yaml yaml = new Yaml(new Constructor(ControlLoopPolicy.class)); - Object obj = yaml.load(is); - assertNotNull(obj); - assertTrue(obj instanceof ControlLoopPolicy); - ControlLoopPolicy policyTobuild = (ControlLoopPolicy) obj; - // - // Now we're going to try to use the builder to build this. - // - ControlLoopPolicyBuilder builder = ControlLoopPolicyBuilder.Factory.buildControlLoop( - policyTobuild.getControlLoop().getControlLoopName(), policyTobuild.getControlLoop().getTimeout()); - // - // Add services - // - if (policyTobuild.getControlLoop().getServices() != null) { - builder = builder.addService(policyTobuild.getControlLoop().getServices() - .toArray(new Service[policyTobuild.getControlLoop().getServices().size()])); - } - // - // Add resources - // - if (policyTobuild.getControlLoop().getResources() != null) { - builder = builder.addResource(policyTobuild.getControlLoop().getResources() - .toArray(new Resource[policyTobuild.getControlLoop().getResources().size()])); - } - // - // Set pnf - // - if (policyTobuild.getControlLoop().getPnf() != null) { - builder = builder.setPnf(policyTobuild.getControlLoop().getPnf()); - } - // - // Add the policies and be sure to set the trigger policy - // - if (policyTobuild.getPolicies() != null) { - setTriggerPolicies(policyTobuild, builder); - } - - // Question : how to change policy ID and results by using builder ?? - - @SuppressWarnings("unused") - Results results = builder.buildSpecification(); - } - - } - - private void setTriggerPolicies(ControlLoopPolicy policyTobuild, ControlLoopPolicyBuilder builder) - throws BuilderException { - for (Policy policy : policyTobuild.getPolicies()) { - if (policy.getId() == policyTobuild.getControlLoop().getTrigger_policy()) { - // @formatter:off - builder.setTriggerPolicy(PolicyParam.builder().id(UUID.randomUUID().toString()) - .name(policy.getName()) - .description(policy.getDescription()) - .actor(policy.getActor()) - .target(policy.getTarget()) - .recipe(policy.getRecipe()) - .payload(null) - .retries(policy.getRetry()) - .timeout(policy.getTimeout()) - .build()); - // @formatter:on - } - } - } -} diff --git a/models-interactions/model-yaml/src/test/java/org/onap/policy/controlloop/policy/ControlLoopPolicyTest.java b/models-interactions/model-yaml/src/test/java/org/onap/policy/controlloop/policy/ControlLoopPolicyTest.java deleted file mode 100644 index 71a80dbf7..000000000 --- a/models-interactions/model-yaml/src/test/java/org/onap/policy/controlloop/policy/ControlLoopPolicyTest.java +++ /dev/null @@ -1,115 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * policy-yaml unit test - * ================================================================================ - * Copyright (C) 2017-2019 AT&T Intellectual Property. All rights reserved. - * Modifications Copyright (C) 2019-2020 Nordix Foundation. - * ================================================================================ - * 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.policy; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; - -import java.io.FileInputStream; -import java.io.InputStreamReader; -import org.junit.Test; -import org.onap.policy.common.utils.coder.YamlJsonTranslator; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -public class ControlLoopPolicyTest { - private static final Logger logger = LoggerFactory.getLogger(ControlLoopPolicyTest.class); - - @Test - public void test1() throws Exception { - this.test("src/test/resources/v1.0.0/policy_Test.yaml"); - } - - @Test - public void testvService1() throws Exception { - this.test("src/test/resources/v1.0.0/policy_vService.yaml"); - } - - @Test - public void testOpenLoop() throws Exception { - this.test("src/test/resources/v1.0.0/policy_OpenLoop.yaml"); - } - - @Test - public void testvdns() throws Exception { - this.test("src/test/resources/v2.0.0/policy_ONAP_demo_vDNS.yaml"); - } - - @Test - public void testvFirewall() throws Exception { - this.test("src/test/resources/v2.0.0/policy_ONAP_demo_vFirewall.yaml"); - } - - @Test - public void testvcpe() throws Exception { - this.test("src/test/resources/v2.0.0/policy_ONAP_UseCase_vCPE.yaml"); - } - - @Test - public void testvpci() throws Exception { - this.test("src/test/resources/v2.0.0/policy_ONAP_UseCase_vPCI.yaml"); - } - - @Test - public void testvolte() throws Exception { - this.test("src/test/resources/v2.0.0/policy_ONAP_UseCase_VOLTE.yaml"); - } - - /** - * Does the actual test. - * - * @param testFile input file - * @throws Exception if an error occurs - */ - public void test(String testFile) throws Exception { - try (InputStreamReader fileInputStream = new InputStreamReader(new FileInputStream(testFile))) { - // - // Read the yaml into our Java Object - // - ControlLoopPolicy controlLoopPolicy1 = - new YamlJsonTranslator().fromYaml(fileInputStream, ControlLoopPolicy.class); - assertNotNull(controlLoopPolicy1); - dump(controlLoopPolicy1); - - // - // Now dump it to a yaml string - // - String dumpedYaml = new YamlJsonTranslator().toYaml(controlLoopPolicy1); - logger.debug(dumpedYaml); - // - // Read that string back into our java object - // - ControlLoopPolicy controlLoopPolicy2 = - new YamlJsonTranslator().fromYaml(dumpedYaml, ControlLoopPolicy.class); - assertNotNull(controlLoopPolicy2); - dump(controlLoopPolicy2); - - // test serialization - assertEquals(controlLoopPolicy1, controlLoopPolicy2); - } - } - - public void dump(Object obj) { - logger.debug("Dumping ", obj.getClass().getName()); - logger.debug("{}", obj); - } -} diff --git a/models-interactions/model-yaml/src/test/java/org/onap/policy/controlloop/policy/ControlLoopTest.java b/models-interactions/model-yaml/src/test/java/org/onap/policy/controlloop/policy/ControlLoopTest.java deleted file mode 100644 index f247baa0c..000000000 --- a/models-interactions/model-yaml/src/test/java/org/onap/policy/controlloop/policy/ControlLoopTest.java +++ /dev/null @@ -1,186 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2018 Ericsson. All rights reserved. - * Modifications Copyright (C) 2018-2019 AT&T Intellectual Property. All rights reserved. - * Modifications Copyright (C) 2019 Nordix Foundation. - * ================================================================================ - * 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.policy; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; - -import java.io.IOException; -import java.util.ArrayList; -import java.util.List; -import org.junit.Ignore; -import org.junit.Test; -import org.onap.aai.domain.yang.Pnf; -import org.onap.policy.common.utils.io.Serializer; -import org.onap.policy.sdc.Resource; -import org.onap.policy.sdc.ResourceType; -import org.onap.policy.sdc.Service; - -public class ControlLoopTest { - - private static final String SERVICE2 = "service2"; - private static final String SERVICE1 = "service1"; - private static final String RESOURCE2 = "resource2"; - private static final String RESOURCE1 = "resource1"; - private static final String PNF1 = "pnf 1"; - private String controlLoopName = "control loop 1"; - private String version = "1.0.1"; - private String triggerPolicy = FinalResult.FINAL_OPENLOOP.toString(); - private Integer timeout = 100; - private Boolean abatement = false; - - @Test - public void testEqualsSameInstance() { - ControlLoop controlLoop1 = new ControlLoop(); - assertTrue(controlLoop1.equals(controlLoop1)); - } - - @Test - public void testEqualsNull() { - ControlLoop controlLoop1 = new ControlLoop(); - assertFalse(controlLoop1.equals(null)); - } - - @Test - public void testEqualsInstanceOfDiffClass() { - ControlLoop controlLoop1 = new ControlLoop(); - assertFalse(controlLoop1.equals("")); - } - - @Test - public void testEqualsNoServicesAndResourcesOrTimeout() { - final Pnf pnf = new Pnf(); - pnf.setPnfName(PNF1); - - ControlLoop controlLoop1 = new ControlLoop(); - controlLoop1.setControlLoopName(controlLoopName); - controlLoop1.setVersion(version); - controlLoop1.setPnf(pnf); - controlLoop1.setTrigger_policy(triggerPolicy); - controlLoop1.setAbatement(abatement); - - ControlLoop controlLoop2 = new ControlLoop(); - controlLoop2.setControlLoopName(controlLoopName); - controlLoop2.setVersion(version); - controlLoop2.setPnf(pnf); - controlLoop2.setTrigger_policy(triggerPolicy); - controlLoop2.setAbatement(abatement); - - assertTrue(controlLoop1.equals(controlLoop2)); - } - - @Test - public void testEquals() throws IOException { - final Pnf pnf = new Pnf(); - pnf.setPnfName(PNF1); - - ControlLoop controlLoop1 = new ControlLoop(); - controlLoop1.setControlLoopName(controlLoopName); - controlLoop1.setVersion(version); - Service service1 = new Service(SERVICE1); - Service service2 = new Service(SERVICE2); - List services = new ArrayList<>(); - services.add(service1); - services.add(service2); - controlLoop1.setServices(services); - Resource resource1 = new Resource(RESOURCE1, ResourceType.VF); - Resource resource2 = new Resource(RESOURCE2, ResourceType.VFC); - List resources = new ArrayList<>(); - resources.add(resource1); - resources.add(resource2); - controlLoop1.setResources(resources); - controlLoop1.setPnf(pnf); - controlLoop1.setTrigger_policy(triggerPolicy); - controlLoop1.setTimeout(timeout); - controlLoop1.setAbatement(abatement); - - ControlLoop controlLoop2 = new ControlLoop(); - controlLoop2.setControlLoopName(controlLoopName); - controlLoop2.setVersion(version); - Service controlLoop2Service1 = new Service(SERVICE1); - Service controlLoop2Service2 = new Service(SERVICE2); - List controlLoop2Services = new ArrayList<>(); - controlLoop2Services.add(controlLoop2Service1); - controlLoop2Services.add(controlLoop2Service2); - controlLoop2.setServices(controlLoop2Services); - Resource controlLoop2Resource1 = new Resource(RESOURCE1, ResourceType.VF); - Resource controlLoop2Resource2 = new Resource(RESOURCE2, ResourceType.VFC); - List controlLoop2Resources = new ArrayList<>(); - controlLoop2Resources.add(controlLoop2Resource1); - controlLoop2Resources.add(controlLoop2Resource2); - controlLoop2.setResources(controlLoop2Resources); - controlLoop2.setPnf(pnf); - controlLoop2.setTrigger_policy(triggerPolicy); - controlLoop2.setTimeout(timeout); - controlLoop1.setAbatement(abatement); - - assertTrue(controlLoop1.equals(controlLoop2)); - assertEquals(controlLoop1.hashCode(), controlLoop2.hashCode()); - - controlLoop2 = Serializer.roundTrip(controlLoop1); - assertTrue(controlLoop1.equals(controlLoop2)); - assertEquals(controlLoop1.hashCode(), controlLoop2.hashCode()); - } - - @Test - @Ignore - // I'VE MARKED THIS TEST CASE AS IGNORE BECAUSE THE TEST CASE FAILS - // This test case fails because the ControlLoop(ControlLoop controlLoop) constructor. - // does not copy the value of pnf and version into the newly created object - // PLEASE ADVISE IF THE EXISTING BEHAVIOUR IS CORRECT - public void testControlLoop() { - final Pnf pnf = new Pnf(); - pnf.setPnfName(PNF1); - - ControlLoop controlLoop1 = new ControlLoop(); - controlLoop1.setControlLoopName(controlLoopName); - controlLoop1.setVersion(version); - Service service1 = new Service(SERVICE1); - Service service2 = new Service(SERVICE2); - List services = new ArrayList<>(); - services.add(service1); - services.add(service2); - controlLoop1.setServices(services); - Resource resource1 = new Resource(RESOURCE1, ResourceType.VF); - Resource resource2 = new Resource(RESOURCE2, ResourceType.VFC); - List resources = new ArrayList<>(); - resources.add(resource1); - resources.add(resource2); - controlLoop1.setResources(resources); - controlLoop1.setPnf(pnf); - controlLoop1.setTrigger_policy(triggerPolicy); - controlLoop1.setAbatement(abatement); - - ControlLoop controlLoop2 = new ControlLoop(controlLoop1); - - assertEquals(controlLoop1.getControlLoopName(), controlLoop2.getControlLoopName()); - assertEquals(controlLoop1.getVersion(), controlLoop2.getVersion()); - assertEquals(controlLoop1.getServices(), controlLoop2.getServices()); - assertEquals(controlLoop1.getResources(), controlLoop2.getResources()); - assertEquals(controlLoop1.getPnf(), controlLoop2.getPnf()); - assertEquals(controlLoop1.getTrigger_policy(), controlLoop2.getTrigger_policy()); - assertEquals(controlLoop1.getAbatement(), controlLoop2.getAbatement()); - - assertTrue(controlLoop1.equals(controlLoop2)); - } - -} diff --git a/models-interactions/model-yaml/src/test/java/org/onap/policy/controlloop/policy/OperationsAccumulateParamsTest.java b/models-interactions/model-yaml/src/test/java/org/onap/policy/controlloop/policy/OperationsAccumulateParamsTest.java deleted file mode 100644 index d06f3c010..000000000 --- a/models-interactions/model-yaml/src/test/java/org/onap/policy/controlloop/policy/OperationsAccumulateParamsTest.java +++ /dev/null @@ -1,125 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2018 Ericsson. All rights reserved. - * Modifications Copyright (C) 2018 AT&T Intellectual Property. All rights reserved. - * Modifications Copyright (C) 2019 Nordix Foundation. - * ================================================================================ - * 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.policy; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertTrue; - -import org.junit.Test; - -public class OperationsAccumulateParamsTest { - - @Test - public void testConstructor() { - OperationsAccumulateParams operationsAccumulateParams = new OperationsAccumulateParams(); - assertNull(operationsAccumulateParams.getPeriod()); - assertNull(operationsAccumulateParams.getLimit()); - } - - @Test - public void testConstructorOperationsAccumulateParams() { - String period = "15m"; - Integer limit = 10; - OperationsAccumulateParams operationsAccumulateParams1 = - new OperationsAccumulateParams(period, limit); - OperationsAccumulateParams operationsAccumulateParams2 = - new OperationsAccumulateParams(operationsAccumulateParams1); - assertEquals(period, operationsAccumulateParams1.getPeriod()); - assertEquals(limit, operationsAccumulateParams2.getLimit()); - } - - @Test - public void testOperationsAccumulateParamsStringInteger() { - String period = "15m"; - Integer limit = 10; - OperationsAccumulateParams operationsAccumulateParams = new OperationsAccumulateParams(period, limit); - assertEquals(period, operationsAccumulateParams.getPeriod()); - assertEquals(limit, operationsAccumulateParams.getLimit()); - } - - @Test - public void testSetAndGetPeriod() { - String period = "15m"; - OperationsAccumulateParams operationsAccumulateParams = new OperationsAccumulateParams(); - operationsAccumulateParams.setPeriod(period); - assertEquals(period, operationsAccumulateParams.getPeriod()); - } - - @Test - public void testSetLimit() { - Integer limit = 10; - OperationsAccumulateParams operationsAccumulateParams = new OperationsAccumulateParams(); - operationsAccumulateParams.setLimit(limit); - assertEquals(limit, operationsAccumulateParams.getLimit()); - } - - @Test - public void testToString() { - String period = "15m"; - Integer limit = 10; - OperationsAccumulateParams operationsAccumulateParams = new OperationsAccumulateParams(period, limit); - assertEquals("OperationsAccumulateParams [period=15m, limit=10]", operationsAccumulateParams.toString()); - } - - @Test - public void testEqualsAndHashCode() { - final String period = "15m"; - final Integer limit = 10; - OperationsAccumulateParams operationsAccumulateParams1 = new OperationsAccumulateParams(); - OperationsAccumulateParams operationsAccumulateParams2 = new OperationsAccumulateParams(); - - assertTrue(operationsAccumulateParams1.equals(operationsAccumulateParams2)); - - operationsAccumulateParams1.setPeriod(period); - assertFalse(operationsAccumulateParams1.equals(operationsAccumulateParams2)); - operationsAccumulateParams2.setPeriod(period); - assertTrue(operationsAccumulateParams1.equals(operationsAccumulateParams2)); - assertEquals(operationsAccumulateParams1.hashCode(), operationsAccumulateParams2.hashCode()); - - operationsAccumulateParams1.setLimit(limit);; - assertFalse(operationsAccumulateParams1.equals(operationsAccumulateParams2)); - operationsAccumulateParams2.setLimit(limit); - assertTrue(operationsAccumulateParams1.equals(operationsAccumulateParams2)); - assertEquals(operationsAccumulateParams1.hashCode(), operationsAccumulateParams2.hashCode()); - } - - - @Test - public void testEqualsSameObject() { - OperationsAccumulateParams operationsAccumulateParams = new OperationsAccumulateParams(); - assertTrue(operationsAccumulateParams.equals(operationsAccumulateParams)); - } - - @Test - public void testEqualsNull() { - OperationsAccumulateParams operationsAccumulateParams = new OperationsAccumulateParams(); - assertFalse(operationsAccumulateParams.equals(null)); - } - - @Test - public void testEqualsInstanceOfDiffClass() { - OperationsAccumulateParams operationsAccumulateParams = new OperationsAccumulateParams(); - assertFalse(operationsAccumulateParams.equals("")); - } - -} diff --git a/models-interactions/model-yaml/src/test/java/org/onap/policy/controlloop/policy/PolicyTest.java b/models-interactions/model-yaml/src/test/java/org/onap/policy/controlloop/policy/PolicyTest.java deleted file mode 100644 index e87a421c4..000000000 --- a/models-interactions/model-yaml/src/test/java/org/onap/policy/controlloop/policy/PolicyTest.java +++ /dev/null @@ -1,221 +0,0 @@ -/* - * ============LICENSE_START======================================================= - * policy-yaml unit test - * ================================================================================ - * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved. - * Modifications Copyright (C) 2019 Nordix Foundation. - * ================================================================================ - * 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.policy; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; - -import java.io.IOException; -import java.util.Map; -import java.util.TreeMap; -import org.junit.Before; -import org.junit.Test; -import org.onap.policy.common.utils.io.Serializer; - -public class PolicyTest { - private Policy policy; - - @Before - public void setUp() { - policy = new Policy(); - } - - @Test - public void testHashCode() { - assertTrue(policy.hashCode() != 0); - - policy.setActor("a"); - int hc1 = policy.hashCode(); - - policy.setActor("b"); - assertTrue(hc1 != policy.hashCode()); - } - - @Test - public void test() throws IOException { - OperationsAccumulateParams operationsAccumulateParams = new OperationsAccumulateParams(); - operationsAccumulateParams.setLimit(10); - - Map payload = new TreeMap<>(); - payload.put("mykey", "myvalue"); - - Target target = new Target(); - target.setResourceID("myresource"); - - policy.setActor("act"); - policy.setDescription("desc"); - policy.setFailure("fail"); - policy.setFailure_exception("failex"); - policy.setFailure_guard("failguard"); - policy.setFailure_retries("failretry"); - policy.setFailure_timeout("failtimeout"); - policy.setId("myid"); - policy.setName("myname"); - policy.setOperationsAccumulateParams(operationsAccumulateParams); - policy.setPayload(payload); - policy.setRecipe("myrecipe"); - policy.setRetry(20); - policy.setSuccess("succ"); - policy.setTarget(target); - policy.setTimeout(30); - - assertEquals("act", policy.getActor()); - assertEquals("desc", policy.getDescription()); - assertEquals("fail", policy.getFailure()); - assertEquals("failex", policy.getFailure_exception()); - assertEquals("failguard", policy.getFailure_guard()); - assertEquals("failretry", policy.getFailure_retries()); - assertEquals("failtimeout", policy.getFailure_timeout()); - assertEquals("myid", policy.getId()); - assertEquals("myname", policy.getName()); - assertEquals(operationsAccumulateParams, policy.getOperationsAccumulateParams()); - assertEquals(payload, policy.getPayload()); - assertEquals("myrecipe", policy.getRecipe()); - assertEquals(20, policy.getRetry().intValue()); - assertEquals("succ", policy.getSuccess()); - assertEquals(target, policy.getTarget()); - assertEquals(30, policy.getTimeout().intValue()); - - assertTrue(policy.equals(policy)); - assertTrue(policy.hashCode() != new Policy().hashCode()); - assertFalse(policy.equals(new Policy())); - - Policy policy2 = Serializer.roundTrip(policy); - assertTrue(policy.equals(policy2)); - assertEquals(policy.hashCode(), policy2.hashCode()); - - policy2 = new Policy(policy); - assertTrue(policy.equals(policy2)); - assertEquals(policy.hashCode(), policy2.hashCode()); - } - - @Test - public void testPolicyString() { - policy = new Policy("justId"); - assertEquals("justId", policy.getId()); - } - - @Test - public void testPolicyStringStringStringMapOfStringStringTarget() { - Map payload = new TreeMap<>(); - payload.put("mykeyB", "myvalueB"); - - Target target = new Target(); - target.setResourceID("myresourceB"); - - policy = new Policy("nameB", "actorB", "recipeB", payload, target); - assertEquals("nameB", policy.getName()); - assertEquals("actorB", policy.getActor()); - assertEquals("recipeB", policy.getRecipe()); - assertEquals(payload, policy.getPayload()); - assertEquals(target, policy.getTarget()); - - assertTrue(policy.hashCode() != new Policy().hashCode()); - } - - @Test - public void testPolicyStringStringStringMapOfStringStringTargetIntegerInteger() { - Map payload = new TreeMap<>(); - payload.put("mykeyC", "myvalueC"); - - Target target = new Target(); - target.setResourceID("myresourceC"); - - policy = new Policy("nameC", "actorC", "recipeC", payload, target, 201, 202); - assertEquals("nameC", policy.getName()); - assertEquals("actorC", policy.getActor()); - assertEquals("recipeC", policy.getRecipe()); - assertEquals(payload, policy.getPayload()); - assertEquals(target, policy.getTarget()); - assertEquals(201, policy.getRetry().intValue()); - assertEquals(202, policy.getTimeout().intValue()); - - assertTrue(policy.hashCode() != new Policy().hashCode()); - } - - @Test - public void testPolicyStringStringStringStringMapOfStringStringTargetStringIntegerInteger() { - Map payload = new TreeMap<>(); - payload.put("mykeyD", "myvalueD"); - - Target target = new Target(); - target.setResourceID("myresourceD"); - - policy = new Policy( - PolicyParam.builder().id("idD") - .name("nameD") - .description("descD") - .actor("actorD") - .payload(payload) - .target(target) - .recipe("recipeD") - .retries(301) - .timeout(302) - .build()); - assertEquals("idD", policy.getId()); - assertEquals("nameD", policy.getName()); - assertEquals("descD", policy.getDescription()); - assertEquals("actorD", policy.getActor()); - assertEquals(payload, policy.getPayload()); - assertEquals(target, policy.getTarget()); - assertEquals("recipeD", policy.getRecipe()); - assertEquals(301, policy.getRetry().intValue()); - assertEquals(302, policy.getTimeout().intValue()); - - assertTrue(policy.hashCode() != new Policy().hashCode()); - } - - @Test - public void testIsValid() { - assertFalse(policy.isValid()); - - Target target = new Target(); - target.setResourceID("myresourceV"); - - policy = new Policy("nameV", "actorV", "recipeV", null, target); - assertEquals(null, policy.getPayload()); - assertTrue(policy.isValid()); - } - - @Test - public void testToString() { - assertNotNull(policy.toString()); - } - - @Test - public void testEqualsObject() { - assertTrue(policy.equals(policy)); - - policy.setId("idE"); - assertFalse(policy.equals(new Policy())); - - Policy policy2 = new Policy(); - policy2.setId(policy.getId()); - assertTrue(policy.equals(policy2)); - - policy2.setId("idX"); - assertFalse(policy.equals(policy2)); - } - -} diff --git a/models-interactions/model-yaml/src/test/java/org/onap/policy/controlloop/policy/guard/ConstraintTest.java b/models-interactions/model-yaml/src/test/java/org/onap/policy/controlloop/policy/guard/ConstraintTest.java deleted file mode 100644 index 973caf9b4..000000000 --- a/models-interactions/model-yaml/src/test/java/org/onap/policy/controlloop/policy/guard/ConstraintTest.java +++ /dev/null @@ -1,259 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2018 Ericsson. All rights reserved. - * Modifications Copyright (C) 2018-2019 AT&T Intellectual Property. All rights reserved. - * Modifications Copyright (C) 2019 Nordix Foundation. - * ================================================================================ - * 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.policy.guard; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertTrue; - -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import org.junit.Test; - -public class ConstraintTest { - - private static final String TIME_WINDOW_VALUE = "timeWindowValue"; - private static final String TIME_WINDOW_KEY = "timeWindowKey"; - private static final String BLACKLIST_ITEM = "blacklist item"; - - @Test - public void testConstraint() { - Constraint constraint = new Constraint(); - - assertNull(constraint.getFreq_limit_per_target()); - assertNull(constraint.getTime_window()); - assertNull(constraint.getActive_time_range()); - assertNull(constraint.getBlacklist()); - } - - @Test - public void testGetAndSetFreq_limit_per_target() { - Integer freqLimitPerTarget = 10; - Constraint constraint = new Constraint(); - constraint.setFreq_limit_per_target(freqLimitPerTarget); - assertEquals(freqLimitPerTarget, constraint.getFreq_limit_per_target()); - } - - @Test - public void testGetAndSetTime_window() { - Map timeWindow = new HashMap<>(); - timeWindow.put(TIME_WINDOW_KEY, TIME_WINDOW_VALUE); - Constraint constraint = new Constraint(); - constraint.setTime_window(timeWindow); - assertEquals(timeWindow, constraint.getTime_window()); - } - - @Test - public void testGetAndSetActive_time_range() { - Map activeTimeRange = new HashMap<>(); - activeTimeRange.put(TIME_WINDOW_KEY, TIME_WINDOW_VALUE); - Constraint constraint = new Constraint(); - constraint.setActive_time_range(activeTimeRange);; - assertEquals(activeTimeRange, constraint.getActive_time_range()); - } - - @Test - public void testGetAndSetBlacklist() { - List blacklist = new ArrayList<>(); - blacklist.add(BLACKLIST_ITEM); - Constraint constraint = new Constraint(); - constraint.setBlacklist(blacklist); - assertEquals(blacklist, constraint.getBlacklist()); - } - - @Test - public void testConstraintIntegerMapOfStringString() { - Integer freqLimitPerTarget = 10; - Map timeWindow = new HashMap<>(); - - Constraint constraint = new Constraint(freqLimitPerTarget, timeWindow); - - assertEquals(freqLimitPerTarget, constraint.getFreq_limit_per_target()); - assertEquals(timeWindow, constraint.getTime_window()); - assertNull(constraint.getActive_time_range()); - assertNull(constraint.getBlacklist()); - } - - @Test - public void testConstraintListOfString() { - List blacklist = new ArrayList<>(); - blacklist.add(BLACKLIST_ITEM); - Constraint constraint = new Constraint(blacklist); - - assertNull(constraint.getFreq_limit_per_target()); - assertNull(constraint.getTime_window()); - assertNull(constraint.getActive_time_range()); - assertEquals(blacklist, constraint.getBlacklist()); - } - - @Test - public void testConstraintIntegerMapOfStringStringListOfString() { - Integer freqLimitPerTarget = 10; - Map timeWindow = new HashMap<>(); - List blacklist = new ArrayList<>(); - blacklist.add(BLACKLIST_ITEM); - Constraint constraint = new Constraint(freqLimitPerTarget, timeWindow, blacklist); - - assertEquals(freqLimitPerTarget, constraint.getFreq_limit_per_target()); - assertEquals(timeWindow, constraint.getTime_window()); - assertNull(constraint.getActive_time_range()); - assertEquals(blacklist, constraint.getBlacklist()); - } - - @Test - public void testConstraintIntegerMapOfStringStringMapOfStringString() { - Integer freqLimitPerTarget = 10; - Map timeWindow = new HashMap<>(); - Map activeTimeRange = new HashMap<>(); - activeTimeRange.put(TIME_WINDOW_KEY, TIME_WINDOW_VALUE); - Constraint constraint = new Constraint(freqLimitPerTarget, timeWindow, activeTimeRange); - - assertEquals(freqLimitPerTarget, constraint.getFreq_limit_per_target()); - assertEquals(timeWindow, constraint.getTime_window()); - assertEquals(activeTimeRange, constraint.getActive_time_range()); - assertNull(constraint.getBlacklist()); - - } - - @Test - public void testConstraintIntegerMapOfStringStringMapOfStringStringListOfString() { - Integer freqLimitPerTarget = 10; - Map timeWindow = new HashMap<>(); - Map activeTimeRange = new HashMap<>(); - activeTimeRange.put(TIME_WINDOW_KEY, TIME_WINDOW_VALUE); - List blacklist = new ArrayList<>(); - blacklist.add(BLACKLIST_ITEM); - Constraint constraint = new Constraint(freqLimitPerTarget, timeWindow, activeTimeRange, blacklist); - - assertEquals(freqLimitPerTarget, constraint.getFreq_limit_per_target()); - assertEquals(timeWindow, constraint.getTime_window()); - assertEquals(activeTimeRange, constraint.getActive_time_range()); - assertEquals(blacklist, constraint.getBlacklist()); - } - - @Test - public void testConstraintConstraint() { - Integer freqLimitPerTarget = 10; - Map timeWindow = new HashMap<>(); - Map activeTimeRange = new HashMap<>(); - activeTimeRange.put(TIME_WINDOW_KEY, TIME_WINDOW_VALUE); - List blacklist = new ArrayList<>(); - blacklist.add(BLACKLIST_ITEM); - Constraint constraint1 = new Constraint(freqLimitPerTarget, timeWindow, activeTimeRange, blacklist); - Constraint constraint2 = new Constraint(constraint1); - - assertEquals(freqLimitPerTarget, constraint2.getFreq_limit_per_target()); - assertEquals(timeWindow, constraint2.getTime_window()); - assertEquals(activeTimeRange, constraint2.getActive_time_range()); - assertEquals(blacklist, constraint2.getBlacklist()); - } - - @Test - public void testIsValid() { - Integer freqLimitPerTarget = 10; - final Map timeWindow = new HashMap<>(); - - Constraint constraint = new Constraint(); - assertTrue(constraint.isValid()); - - constraint.setFreq_limit_per_target(freqLimitPerTarget); - assertFalse(constraint.isValid()); - - constraint.setTime_window(timeWindow); - assertTrue(constraint.isValid()); - - constraint.setFreq_limit_per_target(null); - assertFalse(constraint.isValid()); - } - - @Test - public void testToString() { - Integer freqLimitPerTarget = 10; - Map timeWindow = new HashMap<>(); - Map activeTimeRange = new HashMap<>(); - activeTimeRange.put(TIME_WINDOW_KEY, TIME_WINDOW_VALUE); - List blacklist = new ArrayList<>(); - blacklist.add(BLACKLIST_ITEM); - Constraint constraint = new Constraint(freqLimitPerTarget, timeWindow, activeTimeRange, blacklist); - - assertEquals(constraint.toString(), "Constraint [freq_limit_per_target=" + freqLimitPerTarget + ", time_window=" - + timeWindow + ", active_time_range=" + activeTimeRange + ", blacklist=" + blacklist + "]"); - } - - @Test - public void testEquals() { - Integer freqLimitPerTarget = 10; - final Map timeWindow = new HashMap<>(); - final Map activeTimeRange = new HashMap<>(); - List blacklist = new ArrayList<>(); - blacklist.add(BLACKLIST_ITEM); - - Constraint constraint1 = new Constraint(); - Constraint constraint2 = new Constraint(); - assertTrue(constraint1.equals(constraint2)); - - constraint1.setFreq_limit_per_target(freqLimitPerTarget); - assertFalse(constraint1.equals(constraint2)); - constraint2.setFreq_limit_per_target(freqLimitPerTarget); - assertTrue(constraint1.equals(constraint2)); - assertEquals(constraint1.hashCode(), constraint2.hashCode()); - - constraint1.setTime_window(timeWindow); - assertFalse(constraint1.equals(constraint2)); - constraint2.setTime_window(timeWindow); - assertTrue(constraint1.equals(constraint2)); - assertEquals(constraint1.hashCode(), constraint2.hashCode()); - - constraint1.setActive_time_range(activeTimeRange); - assertFalse(constraint1.equals(constraint2)); - constraint2.setActive_time_range(activeTimeRange); - assertTrue(constraint1.equals(constraint2)); - assertEquals(constraint1.hashCode(), constraint2.hashCode()); - - constraint1.setBlacklist(blacklist); - assertFalse(constraint1.equals(constraint2)); - constraint2.setBlacklist(blacklist); - assertTrue(constraint1.equals(constraint2)); - assertEquals(constraint1.hashCode(), constraint2.hashCode()); - } - - @Test - public void testEqualsSameObject() { - Constraint constraint = new Constraint(); - assertTrue(constraint.equals(constraint)); - } - - @Test - public void testEqualsNull() { - Constraint constraint = new Constraint(); - assertFalse(constraint.equals(null)); - } - - @Test - public void testEqualsInstanceOfDiffClass() { - Constraint constraint = new Constraint(); - assertFalse(constraint.equals("")); - } - -} diff --git a/models-interactions/model-yaml/src/test/java/org/onap/policy/controlloop/policy/guard/ControlLoopGuardBuilderTest.java b/models-interactions/model-yaml/src/test/java/org/onap/policy/controlloop/policy/guard/ControlLoopGuardBuilderTest.java deleted file mode 100644 index a2148120b..000000000 --- a/models-interactions/model-yaml/src/test/java/org/onap/policy/controlloop/policy/guard/ControlLoopGuardBuilderTest.java +++ /dev/null @@ -1,203 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * policy-yaml unit test - * ================================================================================ - * Copyright (C) 2017-2019 AT&T Intellectual Property. All rights reserved. - * Modifications Copyright (C) 2019 Nordix Foundation. - * ================================================================================ - * 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.policy.guard; - -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; - -import java.io.File; -import java.io.FileInputStream; -import java.io.InputStream; -import java.util.HashMap; -import java.util.LinkedList; -import java.util.List; -import java.util.Map; -import org.junit.Test; -import org.onap.policy.controlloop.policy.builder.BuilderException; -import org.onap.policy.controlloop.policy.builder.Message; -import org.onap.policy.controlloop.policy.builder.MessageLevel; -import org.onap.policy.controlloop.policy.builder.Results; -import org.onap.policy.controlloop.policy.guard.builder.ControlLoopGuardBuilder; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.yaml.snakeyaml.Yaml; -import org.yaml.snakeyaml.constructor.Constructor; - -public class ControlLoopGuardBuilderTest { - private static final Logger logger = LoggerFactory.getLogger(ControlLoopGuardBuilderTest.class); - - @Test - public void testControlLoopGuard() throws BuilderException { - // - // Create a builder - // - ControlLoopGuardBuilder builder = ControlLoopGuardBuilder.Factory.buildControlLoopGuard(new Guard()); - // - // Assert there is no guard policies yet - // - Results results = builder.buildSpecification(); - boolean noGuardPolicies = false; - for (Message m : results.getMessages()) { - if ("ControlLoop Guard should have at least one guard policies".equals(m.getMessage()) - && m.getLevel() == MessageLevel.ERROR) { - noGuardPolicies = true; - break; - } - } - assertTrue(noGuardPolicies); - // - // Add a guard policy without limit constraint - // - String clname = "CL_vUSP123"; - List targets = new LinkedList<>(); - targets.add("s1"); - targets.add("s2"); - targets.add("s3"); - MatchParameters matchParameters = new MatchParameters(clname, "APPC", "Restart", targets); - GuardPolicy policy1 = new GuardPolicy("id123", "guardpolicy1", "description aaa", matchParameters); - builder = builder.addGuardPolicy(policy1); - // - // Assert there is no limit constraint associated with the only guard policy - // - results = builder.buildSpecification(); - boolean noConstraint = false; - for (Message m : results.getMessages()) { - if ("Guard policy guardpolicy1 does not have any limit constraint".equals(m.getMessage()) - && m.getLevel() == MessageLevel.ERROR) { - noConstraint = true; - break; - } - } - assertTrue(noConstraint); - // - // Add a constraint to policy1 - // - Map activeTimeRange = new HashMap<>(); - activeTimeRange.put("start", "00:00:00-05:00"); - activeTimeRange.put("end", "23:59:59-05:00"); - List blacklist = new LinkedList<>(); - blacklist.add("eNodeB_common_id1"); - blacklist.add("eNodeB_common_id2"); - Map timeWindow = new HashMap<>(); - timeWindow.put("value", "10"); - timeWindow.put("units", "minute"); - Constraint cons = new Constraint(5, timeWindow, activeTimeRange, blacklist); - builder = builder.addLimitConstraint(policy1.getId(), cons); - // - // Add a duplicate constraint to policy1 - // - builder = builder.addLimitConstraint(policy1.getId(), cons); - // - // Assert there are duplicate constraints associated with the only guard policy - // - results = builder.buildSpecification(); - boolean duplicateConstraint = false; - for (Message m : results.getMessages()) { - if ("Guard policy guardpolicy1 has duplicate limit constraints".equals(m.getMessage()) - && m.getLevel() == MessageLevel.WARNING) { - duplicateConstraint = true; - break; - } - } - assertTrue(duplicateConstraint); - // - // Remove the duplicate constraint - // - builder = builder.removeLimitConstraint(policy1.getId(), cons); - // - // Add a duplicate guard policy - // - builder = builder.addGuardPolicy(policy1); - builder = builder.addLimitConstraint(policy1.getId(), cons); - // - // Assert there are duplicate guard policies - // - results = builder.buildSpecification(); - boolean duplicateGuardPolicy = false; - for (Message m : results.getMessages()) { - if ("There are duplicate guard policies".equals(m.getMessage()) - && m.getLevel() == MessageLevel.WARNING) { - duplicateGuardPolicy = true; - break; - } - } - assertTrue(duplicateGuardPolicy); - // - // Remove the duplicate guard policy - // - builder = builder.removeGuardPolicy(policy1); - // - // Assert there are no Error/Warning message - // - results = builder.buildSpecification(); - assertTrue(results.getMessages().size() == 1); - } - - @Test - public void test1() throws Exception { - this.test("src/test/resources/v2.0.0-guard/policy_guard_ONAP_demo_vDNS.yaml"); - } - - @Test - public void test2() throws Exception { - this.test("src/test/resources/v2.0.0-guard/policy_guard_appc_restart.yaml"); - } - - /** - * Do the actual test. - * - * @param testFile input test file - * @throws Exception if an error occurs - */ - public void test(String testFile) throws Exception { - try (InputStream is = new FileInputStream(new File(testFile))) { - // - // Read the yaml into our Java Object - // - Yaml yaml = new Yaml(new Constructor(ControlLoopGuard.class)); - Object obj = yaml.load(is); - assertNotNull(obj); - assertTrue(obj instanceof ControlLoopGuard); - ControlLoopGuard guardTobuild = (ControlLoopGuard) obj; - // - // Now we're going to try to use the builder to build this. - // - ControlLoopGuardBuilder builder = - ControlLoopGuardBuilder.Factory.buildControlLoopGuard(guardTobuild.getGuard()); - // - // Add guard policy - // - if (guardTobuild.getGuards() != null) { - builder = builder.addGuardPolicy(guardTobuild.getGuards().toArray( - new GuardPolicy[guardTobuild.getGuards().size()])); - } - // - // Build the specification - // - Results results = builder.buildSpecification(); - // - // Print out the specification - // - logger.debug(results.getSpecification()); - } - } -} diff --git a/models-interactions/model-yaml/src/test/java/org/onap/policy/controlloop/policy/guard/ControlLoopGuardTest.java b/models-interactions/model-yaml/src/test/java/org/onap/policy/controlloop/policy/guard/ControlLoopGuardTest.java deleted file mode 100644 index 3d4cb37dd..000000000 --- a/models-interactions/model-yaml/src/test/java/org/onap/policy/controlloop/policy/guard/ControlLoopGuardTest.java +++ /dev/null @@ -1,160 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * policy-yaml unit test - * ================================================================================ - * Copyright (C) 2017-2019 AT&T Intellectual Property. All rights reserved. - * Modifications Copyright (C) 2019 Nordix Foundation. - * ================================================================================ - * 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.policy.guard; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; - -import java.io.File; -import java.io.FileInputStream; -import java.io.InputStream; -import java.util.LinkedList; -import org.junit.Test; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.yaml.snakeyaml.DumperOptions; -import org.yaml.snakeyaml.DumperOptions.FlowStyle; -import org.yaml.snakeyaml.Yaml; -import org.yaml.snakeyaml.constructor.Constructor; - - -public class ControlLoopGuardTest { - private static final Logger logger = LoggerFactory.getLogger(ControlLoopGuardTest.class); - - @Test - public void testGuardvdns() throws Exception { - this.test("src/test/resources/v2.0.0-guard/policy_guard_ONAP_demo_vDNS.yaml"); - } - - @Test - public void testGuardvusp() throws Exception { - this.test("src/test/resources/v2.0.0-guard/policy_guard_appc_restart.yaml"); - } - - @Test - public void testConstructorControlLoopGuard() { - Guard guard1 = new Guard(); - GuardPolicy guardPolicy1 = new GuardPolicy(); - GuardPolicy guardPolicy2 = new GuardPolicy(); - LinkedList guardPolicies = new LinkedList<>(); - guardPolicies.add(guardPolicy1); - guardPolicies.add(guardPolicy2); - - ControlLoopGuard controlLoopGuard1 = new ControlLoopGuard(); - controlLoopGuard1.setGuard(guard1); - controlLoopGuard1.setGuards(guardPolicies); - ControlLoopGuard controlLoopGuard2 = new ControlLoopGuard(controlLoopGuard1); - - assertEquals(guard1, controlLoopGuard2.getGuard()); - assertEquals(guardPolicies, controlLoopGuard2.getGuards()); - } - - @Test - public void testEqualsAndHashCode() { - final Guard guard1 = new Guard(); - GuardPolicy guardPolicy1 = new GuardPolicy(); - GuardPolicy guardPolicy2 = new GuardPolicy(); - LinkedList guardPolicies = new LinkedList<>(); - guardPolicies.add(guardPolicy1); - guardPolicies.add(guardPolicy2); - - ControlLoopGuard controlLoopGuard1 = new ControlLoopGuard(); - ControlLoopGuard controlLoopGuard2 = new ControlLoopGuard(); - - assertTrue(controlLoopGuard1.equals(controlLoopGuard2)); - assertEquals(controlLoopGuard1.hashCode(), controlLoopGuard2.hashCode()); - - controlLoopGuard1.setGuard(guard1); - assertFalse(controlLoopGuard1.equals(controlLoopGuard2)); - controlLoopGuard2.setGuard(guard1); - assertTrue(controlLoopGuard1.equals(controlLoopGuard2)); - assertEquals(controlLoopGuard1.hashCode(), controlLoopGuard2.hashCode()); - - controlLoopGuard1.setGuards(guardPolicies); - assertFalse(controlLoopGuard1.equals(controlLoopGuard2)); - controlLoopGuard2.setGuards(guardPolicies); - assertTrue(controlLoopGuard1.equals(controlLoopGuard2)); - assertEquals(controlLoopGuard1.hashCode(), controlLoopGuard2.hashCode()); - } - - @Test - public void testEqualsSameObject() { - ControlLoopGuard controlLoopGuard = new ControlLoopGuard(); - assertTrue(controlLoopGuard.equals(controlLoopGuard)); - } - - @Test - public void testEqualsNull() { - ControlLoopGuard controlLoopGuard = new ControlLoopGuard(); - assertFalse(controlLoopGuard.equals(null)); - } - - @Test - public void testEqualsInstanceOfDiffClass() { - ControlLoopGuard controlLoopGuard = new ControlLoopGuard(); - assertFalse(controlLoopGuard.equals("")); - } - - /** - * Does the actual test. - * - * @param testFile input file - * @throws Exception if an error occurs - */ - public void test(String testFile) throws Exception { - try (InputStream is = new FileInputStream(new File(testFile))) { - // - // Read the yaml into our Java Object - // - Yaml yaml = new Yaml(new Constructor(ControlLoopGuard.class)); - Object obj = yaml.load(is); - assertNotNull(obj); - assertTrue(obj instanceof ControlLoopGuard); - dump(obj); - // - // Now dump it to a yaml string - // - DumperOptions options = new DumperOptions(); - options.setDefaultFlowStyle(FlowStyle.BLOCK); - options.setPrettyFlow(true); - yaml = new Yaml(options); - String dumpedYaml = yaml.dump(obj); - logger.debug(dumpedYaml); - // - // Read that string back into our java object - // - Object newObject = yaml.load(dumpedYaml); - dump(newObject); - assertNotNull(newObject); - assertTrue(newObject instanceof ControlLoopGuard); - - assertEquals(obj, newObject); - } - } - - public void dump(Object obj) { - logger.debug("Dumping {}", obj.getClass().getName()); - logger.debug("{}", obj); - } -} diff --git a/models-interactions/model-yaml/src/test/java/org/onap/policy/controlloop/policy/guard/GuardPolicyTest.java b/models-interactions/model-yaml/src/test/java/org/onap/policy/controlloop/policy/guard/GuardPolicyTest.java deleted file mode 100644 index 9bdcd6062..000000000 --- a/models-interactions/model-yaml/src/test/java/org/onap/policy/controlloop/policy/guard/GuardPolicyTest.java +++ /dev/null @@ -1,274 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2018 Ericsson. All rights reserved. - * Modifications Copyright (C) 2018-2019 AT&T Intellectual Property. All rights reserved. - * Modifications Copyright (C) 2019 Nordix Foundation. - * ================================================================================ - * 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.policy.guard; - -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.assertTrue; - -import java.util.LinkedList; -import java.util.List; -import org.junit.Test; - -public class GuardPolicyTest { - - private static final String GUARD_DESCRIPTION = "guard description"; - private static final String GUARD_ID = "guard id"; - private static final String GUARD_NAME = "guard name"; - - @Test - public void testConstructor() { - GuardPolicy guardPolicy = new GuardPolicy(); - - assertNotNull(guardPolicy.getId()); - assertNull(guardPolicy.getName()); - assertNull(guardPolicy.getDescription()); - assertNull(guardPolicy.getMatch_parameters()); - assertNull(guardPolicy.getLimit_constraints()); - } - - @Test - public void testConstructorString() { - String id = GUARD_ID; - GuardPolicy guardPolicy = new GuardPolicy(id); - - assertEquals(id, guardPolicy.getId()); - assertNull(guardPolicy.getName()); - assertNull(guardPolicy.getDescription()); - assertNull(guardPolicy.getMatch_parameters()); - assertNull(guardPolicy.getLimit_constraints()); - } - - @Test - public void testConstructorStringStringStringMatchParameters() { - String id = GUARD_ID; - String name = GUARD_NAME; - String description = GUARD_DESCRIPTION; - MatchParameters matchParameters = new MatchParameters(); - List limitConstraints = new LinkedList<>(); - limitConstraints.add(new Constraint()); - GuardPolicy guardPolicy = new GuardPolicy(id, name, description, matchParameters); - - assertNotNull(guardPolicy.getId()); - assertEquals(name, guardPolicy.getName()); - assertEquals(description, guardPolicy.getDescription()); - assertEquals(matchParameters, guardPolicy.getMatch_parameters()); - assertNull(guardPolicy.getLimit_constraints()); - } - - @Test - public void testConstructorStringMatchParametersList() { - String name = GUARD_NAME; - MatchParameters matchParameters = new MatchParameters(); - List limitConstraints = new LinkedList<>(); - limitConstraints.add(new Constraint()); - GuardPolicy guardPolicy = new GuardPolicy(name, matchParameters, limitConstraints); - - assertNotNull(guardPolicy.getId()); - assertEquals(name, guardPolicy.getName()); - assertNull(guardPolicy.getDescription()); - assertEquals(matchParameters, guardPolicy.getMatch_parameters()); - assertEquals(limitConstraints, guardPolicy.getLimit_constraints()); - } - - @Test - public void testConstructorStringStringMatchParametersList() { - String name = GUARD_NAME; - String description = GUARD_DESCRIPTION; - MatchParameters matchParameters = new MatchParameters(); - List limitConstraints = new LinkedList<>(); - limitConstraints.add(new Constraint()); - GuardPolicy guardPolicy = new GuardPolicy(name, description, matchParameters, limitConstraints); - - assertNotNull(guardPolicy.getId()); - assertEquals(name, guardPolicy.getName()); - assertEquals(description, guardPolicy.getDescription()); - assertEquals(matchParameters, guardPolicy.getMatch_parameters()); - assertEquals(limitConstraints, guardPolicy.getLimit_constraints()); - } - - @Test - public void testConstructorStringStringStringMatchParametersList() { - String id = GUARD_ID; - String name = GUARD_NAME; - String description = GUARD_DESCRIPTION; - MatchParameters matchParameters = new MatchParameters(); - List limitConstraints = new LinkedList<>(); - limitConstraints.add(new Constraint()); - GuardPolicy guardPolicy = new GuardPolicy(id, name, description, matchParameters, limitConstraints); - - assertEquals(id, guardPolicy.getId()); - assertEquals(name, guardPolicy.getName()); - assertEquals(description, guardPolicy.getDescription()); - assertEquals(matchParameters, guardPolicy.getMatch_parameters()); - assertEquals(limitConstraints, guardPolicy.getLimit_constraints()); - } - - @Test - public void testConstructorGuardPolicy() { - String id = GUARD_ID; - String name = GUARD_NAME; - String description = GUARD_DESCRIPTION; - MatchParameters matchParameters = new MatchParameters(); - List limitConstraints = new LinkedList<>(); - limitConstraints.add(new Constraint()); - GuardPolicy guardPolicy1 = new GuardPolicy(id, name, description, matchParameters, limitConstraints); - - GuardPolicy guardPolicy2 = new GuardPolicy(guardPolicy1); - - - assertEquals(id, guardPolicy2.getId()); - assertEquals(name, guardPolicy2.getName()); - assertEquals(description, guardPolicy2.getDescription()); - assertEquals(matchParameters, guardPolicy2.getMatch_parameters()); - assertEquals(limitConstraints, guardPolicy2.getLimit_constraints()); - } - - @Test - public void testSetAndGetId() { - String id = GUARD_ID; - GuardPolicy guardPolicy = new GuardPolicy(); - guardPolicy.setId(id); - assertEquals(id, guardPolicy.getId()); - } - - @Test - public void testSetAndGetName() { - String name = GUARD_NAME; - GuardPolicy guardPolicy = new GuardPolicy(); - guardPolicy.setName(name); - assertEquals(name, guardPolicy.getName()); - } - - @Test - public void testSetAndGetDescription() { - String description = GUARD_DESCRIPTION; - GuardPolicy guardPolicy = new GuardPolicy(); - guardPolicy.setDescription(description); - assertEquals(description, guardPolicy.getDescription()); - } - - @Test - public void testSetAndGetMatchParameters() { - MatchParameters matchParameters = new MatchParameters(); - GuardPolicy guardPolicy = new GuardPolicy(); - guardPolicy.setMatch_parameters(matchParameters); - assertEquals(matchParameters, guardPolicy.getMatch_parameters()); - } - - @Test - public void testSetAndGetLimitConstraints() { - LinkedList limitConstraints = new LinkedList<>(); - limitConstraints.add(new Constraint()); - GuardPolicy guardPolicy = new GuardPolicy(); - guardPolicy.setLimit_constraints(limitConstraints); - assertEquals(limitConstraints, guardPolicy.getLimit_constraints()); - } - - @Test - public void testIsValid() { - GuardPolicy guardPolicy = new GuardPolicy(); - assertFalse(guardPolicy.isValid()); - - guardPolicy.setName(GUARD_NAME); - assertTrue(guardPolicy.isValid()); - - guardPolicy.setId(null); - assertFalse(guardPolicy.isValid()); - } - - @Test - public void testToString() { - String id = GUARD_ID; - String name = GUARD_NAME; - String description = GUARD_DESCRIPTION; - MatchParameters matchParameters = new MatchParameters(); - List limitConstraints = new LinkedList<>(); - limitConstraints.add(new Constraint()); - GuardPolicy guardPolicy = new GuardPolicy(id, name, description, matchParameters, limitConstraints); - - assertEquals(guardPolicy.toString(), "Policy [id=guard id, name=guard name, description=guard description, " - + "match_parameters=MatchParameters [controlLoopName=null, actor=null, recipe=null, targets=null], " - + "limitConstraints=[Constraint [freq_limit_per_target=null, time_window=null, active_time_range=null," - + " blacklist=null]]]", guardPolicy.toString()); - } - - @Test - public void testEquals() { - final String id = GUARD_ID; - final String name = GUARD_NAME; - final String description = GUARD_DESCRIPTION; - GuardPolicy guardPolicy1 = new GuardPolicy(id); - GuardPolicy guardPolicy2 = new GuardPolicy(); - assertFalse(guardPolicy1.equals(guardPolicy2)); - - guardPolicy2.setId(id); - assertTrue(guardPolicy1.equals(guardPolicy2)); - assertEquals(guardPolicy1.hashCode(), guardPolicy2.hashCode()); - - guardPolicy1.setName(name); - assertFalse(guardPolicy1.equals(guardPolicy2)); - guardPolicy2.setName(name); - assertTrue(guardPolicy1.equals(guardPolicy2)); - assertEquals(guardPolicy1.hashCode(), guardPolicy2.hashCode()); - - guardPolicy1.setDescription(description); - assertFalse(guardPolicy1.equals(guardPolicy2)); - guardPolicy2.setDescription(description); - assertTrue(guardPolicy1.equals(guardPolicy2)); - assertEquals(guardPolicy1.hashCode(), guardPolicy2.hashCode()); - - MatchParameters matchParameters = new MatchParameters(); - guardPolicy1.setMatch_parameters(matchParameters); - assertFalse(guardPolicy1.equals(guardPolicy2)); - guardPolicy2.setMatch_parameters(matchParameters); - assertTrue(guardPolicy1.equals(guardPolicy2)); - assertEquals(guardPolicy1.hashCode(), guardPolicy2.hashCode()); - - LinkedList limitConstraints = new LinkedList<>(); - limitConstraints.add(new Constraint()); - guardPolicy1.setLimit_constraints(limitConstraints); - assertFalse(guardPolicy1.equals(guardPolicy2)); - guardPolicy2.setLimit_constraints(limitConstraints); - assertTrue(guardPolicy1.equals(guardPolicy2)); - assertEquals(guardPolicy1.hashCode(), guardPolicy2.hashCode()); - } - - @Test - public void testEqualsSameObject() { - GuardPolicy guardPolicy = new GuardPolicy(); - assertTrue(guardPolicy.equals(guardPolicy)); - } - - @Test - public void testEqualsNull() { - GuardPolicy guardPolicy = new GuardPolicy(); - assertFalse(guardPolicy.equals(null)); - } - - @Test - public void testEqualsInstanceOfDiffClass() { - GuardPolicy guardPolicy = new GuardPolicy(); - assertFalse(guardPolicy.equals("")); - } -} -- cgit 1.2.3-korg