diff options
Diffstat (limited to 'controlloop/common/policy-yaml/src/test/java')
8 files changed, 1395 insertions, 32 deletions
diff --git a/controlloop/common/policy-yaml/src/test/java/org/onap/policy/controlloop/compiler/CompilerExceptionTest.java b/controlloop/common/policy-yaml/src/test/java/org/onap/policy/controlloop/compiler/CompilerExceptionTest.java new file mode 100644 index 000000000..0ec882fef --- /dev/null +++ b/controlloop/common/policy-yaml/src/test/java/org/onap/policy/controlloop/compiler/CompilerExceptionTest.java @@ -0,0 +1,30 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2018 Ericsson. 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() throws Exception { + test(CompilerException.class); + } + +} diff --git a/controlloop/common/policy-yaml/src/test/java/org/onap/policy/controlloop/compiler/ControlLoopCompilerTest.java b/controlloop/common/policy-yaml/src/test/java/org/onap/policy/controlloop/compiler/ControlLoopCompilerTest.java index ee5ed785d..048aef270 100644 --- a/controlloop/common/policy-yaml/src/test/java/org/onap/policy/controlloop/compiler/ControlLoopCompilerTest.java +++ b/controlloop/common/policy-yaml/src/test/java/org/onap/policy/controlloop/compiler/ControlLoopCompilerTest.java @@ -27,48 +27,135 @@ import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; +import java.util.ArrayList; +import java.util.List; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.onap.policy.controlloop.policy.ControlLoopPolicy; +import org.onap.policy.controlloop.policy.FinalResult; public class ControlLoopCompilerTest { + + @Rule + public ExpectedException expectedException = ExpectedException.none(); @Test - public void testTest() { - try { - this.test("src/test/resources/v1.0.0/test.yaml"); - } catch (Exception e) { - fail(e.getMessage()); - } + public void testTest() throws Exception { + List<String> 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 actor is invalid"); + expectedOnErrorMessages.add("Policy recipe is null"); + expectedOnErrorMessages.add("Policy recipe is invalid"); + expectedOnErrorMessages.add("Policy recipe is invalid"); + expectedOnErrorMessages.add("Policy recipe is invalid"); + 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 { + expectedException.expect(CompilerException.class); + expectedException.expectMessage("Operation Policy unique-policy-id-1-restart is connected to unknown policy unknown-policy"); + this.test("src/test/resources/v1.0.0/bad_policy_success_connected_to_unknown_policy.yaml"); + } + + @Test + public void testFailureConnectedToUnknownPolicy() throws Exception { + expectedException.expect(CompilerException.class); + expectedException.expectMessage("Operation Policy unique-policy-id-1-restart is connected to unknown policy unknown-policy"); + this.test("src/test/resources/v1.0.0/bad_policy_failure_connected_to_unknown_policy.yaml"); + } + + @Test + public void testFailureTimeoutToUnknownPolicy() throws Exception { + expectedException.expect(CompilerException.class); + expectedException.expectMessage("Operation Policy unique-policy-id-1-restart is connected to unknown policy unknown-policy"); + this.test("src/test/resources/v1.0.0/bad_policy_failure_timeout_connected_to_unknown_policy.yaml"); + } + + @Test + public void testFailureRetriesToUnknownPolicy() throws Exception { + expectedException.expect(CompilerException.class); + expectedException.expectMessage("Operation Policy unique-policy-id-1-restart is connected to unknown policy unknown-policy"); + this.test("src/test/resources/v1.0.0/bad_policy_failure_retries_connected_to_unknown_policy.yaml"); + } + + @Test + public void testFailureExceptionToUnknownPolicy() throws Exception { + expectedException.expect(CompilerException.class); + expectedException.expectMessage("Operation Policy unique-policy-id-1-restart is connected to unknown policy unknown-policy"); + this.test("src/test/resources/v1.0.0/bad_policy_failure_exception_connected_to_unknown_policy.yaml"); + } + + @Test + public void testFailureGuardToUnknownPolicy() throws Exception { + expectedException.expect(CompilerException.class); + expectedException.expectMessage("Operation Policy unique-policy-id-1-restart is connected to unknown policy unknown-policy"); + this.test("src/test/resources/v1.0.0/bad_policy_failure_guard_connected_to_unknown_policy.yaml"); } @Test - public void testBad1() { - try { - this.test("src/test/resources/v1.0.0/bad_trigger_1.yaml"); - } catch (Exception e) { - e.printStackTrace(); - } + public void testInvalidTriggerPolicyId() throws Exception { + expectedException.expect(CompilerException.class); + expectedException.expectMessage("Unexpected value for trigger_policy, should only be " + FinalResult.FINAL_OPENLOOP.toString() + " or a valid Policy ID"); + this.test("src/test/resources/v1.0.0/bad_trigger_1.yaml"); } + @Test - public void testBad2() { - try { - this.test("src/test/resources/v1.0.0/bad_trigger_2.yaml"); - } catch (Exception e) { - e.printStackTrace(); - } + public void testNoTriggerPolicyId() throws Exception { + expectedException.expect(CompilerException.class); + this.test("src/test/resources/v1.0.0/bad_trigger_no_trigger_id.yaml"); } @Test - public void testBad() { - try { - this.test("src/test/resources/v1.0.0/bad_policies_1.yaml"); - } catch (Exception e) { - } + public void testNoControlLoopName() throws Exception { + List<String> 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 { + expectedException.expect(CompilerException.class); + expectedException.expectMessage("Unexpected Final Result for trigger_policy, should only be FINAL_OPENLOOP or a valid Policy ID"); + this.test("src/test/resources/v1.0.0/bad_trigger_2.yaml"); + } + + @Test + public void testCompileEmptyFile() throws Exception { + expectedException.expect(CompilerException.class); + expectedException.expectMessage("Could not parse yaml specification."); + this.test("src/test/resources/v1.0.0/empty.yaml"); + } + + public ControlLoopPolicy test(String testFile) throws Exception { + return test(testFile, null); } - public void test(String testFile) throws Exception { + public ControlLoopPolicy test(String testFile, ControlLoopCompilerCallback controlLoopCompilerCallback) throws Exception { try (InputStream is = new FileInputStream(new File(testFile))) { - ControlLoopCompiler.compile(is, null); + return ControlLoopCompiler.compile(is, controlLoopCompilerCallback); } catch (FileNotFoundException e) { fail(e.getMessage()); } catch (IOException e) { @@ -76,6 +163,36 @@ public class ControlLoopCompilerTest { } catch (Exception e) { throw e; } + return null; } + + class TestControlLoopCompilerCallback implements ControlLoopCompilerCallback{ + + private List<String> expectedOnErrorMessages; + + public TestControlLoopCompilerCallback(List<String> 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.size() == 0; + } + + }; } + + diff --git a/controlloop/common/policy-yaml/src/test/java/org/onap/policy/controlloop/policy/ControlLoopPolicyBuilderTest.java b/controlloop/common/policy-yaml/src/test/java/org/onap/policy/controlloop/policy/ControlLoopPolicyBuilderTest.java index f1681247d..05d4e469d 100644 --- a/controlloop/common/policy-yaml/src/test/java/org/onap/policy/controlloop/policy/ControlLoopPolicyBuilderTest.java +++ b/controlloop/common/policy-yaml/src/test/java/org/onap/policy/controlloop/policy/ControlLoopPolicyBuilderTest.java @@ -20,8 +20,10 @@ 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.assertNull; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; @@ -31,8 +33,12 @@ import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.util.UUID; - +import org.junit.Ignore; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.onap.policy.aai.PNF; +import org.onap.policy.aai.PNFType; import org.onap.policy.controlloop.policy.builder.BuilderException; import org.onap.policy.controlloop.policy.builder.ControlLoopPolicyBuilder; import org.onap.policy.controlloop.policy.builder.Message; @@ -47,6 +53,9 @@ import org.yaml.snakeyaml.error.YAMLException; public class ControlLoopPolicyBuilderTest { + + @Rule + public ExpectedException expectedException = ExpectedException.none(); @Test public void testControlLoop() { @@ -85,18 +94,203 @@ public class ControlLoopPolicyBuilderTest { assertTrue(builder.getControlLoop().getResources().size() == 2); builder = builder.removeAllResources(); assertTrue(builder.getControlLoop().getResources().size() == 0); - // - // Test set abatement - // - assertFalse(builder.getControlLoop().getAbatement()); - builder = builder.setAbatement(true); - assertTrue(builder.getControlLoop().getAbatement()); } catch (BuilderException e) { fail(e.getMessage()); } } @Test + public void testAddNullService() throws BuilderException { + ControlLoopPolicyBuilder builder = ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400); + expectedException.expect(BuilderException.class); + expectedException.expectMessage("Service must not be null"); + builder.addService((Service)null); + } + + @Test + public void testAddInvalidService() throws BuilderException { + ControlLoopPolicyBuilder builder = ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400); + expectedException.expect(BuilderException.class); + expectedException.expectMessage("Invalid service - need either a serviceUUID or serviceName"); + builder.addService(new Service()); + } + + @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); + expectedException.expect(BuilderException.class); + expectedException.expectMessage("Resource must not be null"); + builder.addResource((Resource)null); + } + + + @Test + public void testAddInvalidResource() throws BuilderException { + ControlLoopPolicyBuilder builder = ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400); + expectedException.expect(BuilderException.class); + expectedException.expectMessage("Invalid resource - need either resourceUUID or resourceName"); + builder.addResource(new Resource()); + } + + @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().size() == 0); + } + + @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); + expectedException.expect(BuilderException.class); + expectedException.expectMessage("Resource must not be null"); + builder.removeResource((Resource)null); + } + + @Test + public void testRemoveResourceNoExistingResources() throws BuilderException { + ControlLoopPolicyBuilder builder = ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400); + expectedException.expect(BuilderException.class); + expectedException.expectMessage("No existing resources to remove"); + builder.removeResource(new Resource("resource1", ResourceType.VF)); + } + + @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); + expectedException.expect(BuilderException.class); + expectedException.expectMessage("Invalid resource - need either a resourceUUID or resourceName"); + builder.removeResource(new Resource()); + } + + @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"; + expectedException.expect(BuilderException.class); + expectedException.expectMessage("Unknown resource " + unknownResourceName); + builder.removeResource(new Resource(unknownResourceName, ResourceType.VF)); + } + + @Test + public void testControlLoopWithInitialResourceAndServices() { + try { + Resource vCTS = new Resource("vCTS", ResourceType.VF); + Service vSCP = new Service("vSCP"); + Service vUSP = new Service("vUSP"); + ControlLoopPolicyBuilder builder = ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400, vCTS, vSCP, vUSP); + assertTrue(builder.getControlLoop().getResources().size() == 1); + assertTrue(builder.getControlLoop().getServices().size() == 2); + } catch (BuilderException e) { + fail(e.getMessage()); + } + } + + @Test + public void testControlLoopWithInitialResourcesAndService() { + try { + Resource vCTS = new Resource("vCTS", ResourceType.VF); + Resource vCOM = new Resource("vCTS", ResourceType.VF); + Service vSCP = new Service("vSCP"); + ControlLoopPolicyBuilder builder = ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400, vSCP, vCTS, vCOM); + assertTrue(builder.getControlLoop().getServices().size() == 1); + assertTrue(builder.getControlLoop().getResources().size() == 2); + } catch (BuilderException e) { + fail(e.getMessage()); + } + } + + @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() { + try { + 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()); + } catch (BuilderException e) { + fail(e.getMessage()); + } + } + + @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); + expectedException.expect(BuilderException.class); + expectedException.expectMessage("PNF must not be null"); + builder.setPNF(null); + } + + @Test + public void testSetInvalidPnf() throws BuilderException { + ControlLoopPolicyBuilder builder = ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400); + expectedException.expect(BuilderException.class); + expectedException.expectMessage("Invalid PNF - need either pnfName or pnfType"); + builder.setPNF(new PNF()); + } + + @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 { + ControlLoopPolicyBuilder builder = ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400); + expectedException.expect(BuilderException.class); + expectedException.expectMessage("abatement must not be null"); + builder = builder.setAbatement(null); + } + + @Test public void testTimeout() { try { // @@ -192,6 +386,41 @@ public class ControlLoopPolicyBuilderTest { fail(e.getMessage()); } } + + @Test + public void testSetTriggerPolicyNullPolicyId() throws BuilderException { + ControlLoopPolicyBuilder builder = ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400); + expectedException.expect(BuilderException.class); + expectedException.expectMessage("Id must not be null"); + builder.setTriggerPolicy(null); + } + + @Test + public void testSetTriggerPolicyNoPoliciesExist() throws BuilderException { + ControlLoopPolicyBuilder builder = ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400); + final String unknownPolicyId = "100"; + expectedException.expect(BuilderException.class); + expectedException.expectMessage("Unknown policy " + unknownPolicyId); + builder.setTriggerPolicy(unknownPolicyId); + } + + @Test + public void testSetTriggerPolicyUnknownPolicy() throws BuilderException { + ControlLoopPolicyBuilder builder = ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400); + builder.setTriggerPolicy( + "Restart the VM", + "Upon getting the trigger event, restart the VM", + "APPC", + new Target(TargetType.VM), + "Restart", + null, + 2, + 300); + final String unknownPolicyId = "100"; + expectedException.expect(BuilderException.class); + expectedException.expectMessage("Unknown policy " + unknownPolicyId); + builder.setTriggerPolicy(unknownPolicyId); + } @Test public void testAddRemovePolicies() { @@ -220,14 +449,34 @@ public class ControlLoopPolicyBuilderTest { 600, triggerPolicy.getId(), 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( + "Do something", + "If the restart succeeds, do something else.", + "APPC", + new Target(TargetType.VM), + "SomethingElse", + null, + 1, + 600, + triggerPolicy.getId(), + PolicyResult.SUCCESS); + // + assertTrue(builder.getTriggerPolicy().getSuccess().equals(onSuccessPolicy1.getId())); + // // Test remove policy // @@ -272,6 +521,24 @@ public class ControlLoopPolicyBuilderTest { assertTrue(builder.getTriggerPolicy().getFailure().equals(onRestartFailurePolicy2.getId())); assertTrue(builder.getTriggerPolicy().getFailure_retries().equals(onRestartFailurePolicy2.getId())); assertTrue(builder.getTriggerPolicy().getFailure_timeout().equals(onRestartFailurePolicy2.getId())); + // + // Test set the policy result for success to an existing operational policy + // + onRestartFailurePolicy2 = 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(onRestartFailurePolicy2.getId())); + assertTrue(builder.getTriggerPolicy().getFailure_exception().equals(onRestartFailurePolicy2.getId())); + assertTrue(builder.getTriggerPolicy().getFailure_guard().equals(onRestartFailurePolicy2.getId())); + assertTrue(builder.getTriggerPolicy().getFailure_retries().equals(onRestartFailurePolicy2.getId())); + assertTrue(builder.getTriggerPolicy().getFailure_timeout().equals(onRestartFailurePolicy2.getId())); + assertTrue(builder.getTriggerPolicy().getSuccess().equals(onRestartFailurePolicy2.getId())); // // Test remove all existing operational policies @@ -283,6 +550,88 @@ public class ControlLoopPolicyBuilderTest { fail(e.getMessage()); } } + + @Test + public void testAddToUnknownPolicy() throws BuilderException { + ControlLoopPolicyBuilder builder = ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400); + final String policyId = "100"; + expectedException.expect(BuilderException.class); + expectedException.expectMessage("Unknown policy " + policyId); + + builder.setPolicyForPolicyResult( + "Rebuild VM", + "If the restart fails, rebuild it.", + "APPC", + new Target(TargetType.VM), + "Rebuild", + null, + 1, + 600, + policyId, + PolicyResult.FAILURE, + PolicyResult.FAILURE_RETRIES, + PolicyResult.FAILURE_TIMEOUT, + PolicyResult.FAILURE_GUARD); + } + + @Test + public void testAddExistingPolicyToUnknownPolicy() throws BuilderException { + ControlLoopPolicyBuilder builder = ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400); + Policy triggerPolicy = builder.setTriggerPolicy( + "Restart the VM", + "Upon getting the trigger event, restart the VM", + "APPC", + new Target(TargetType.VM), + "Restart", + null, + 2, + 300); + + + Policy onRestartFailurePolicy = builder.setPolicyForPolicyResult( + "Rebuild VM", + "If the restart fails, rebuild it.", + "APPC", + new Target(TargetType.VM), + "Rebuild", + null, + 1, + 600, + triggerPolicy.getId(), + PolicyResult.FAILURE); + + final String unknownPolicyId = "100"; + expectedException.expect(BuilderException.class); + expectedException.expectMessage(unknownPolicyId + " does not exist"); + + builder.setPolicyForPolicyResult( + onRestartFailurePolicy.getId(), + unknownPolicyId, + PolicyResult.FAILURE); + } + + @Test + public void testAddUnknownExistingPolicyToPolicy() throws BuilderException { + ControlLoopPolicyBuilder builder = ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400); + Policy triggerPolicy = builder.setTriggerPolicy( + "Restart the VM", + "Upon getting the trigger event, restart the VM", + "APPC", + new Target(TargetType.VM), + "Restart", + null, + 2, + 300); + + final String unknownPolicyId = "100"; + expectedException.expect(BuilderException.class); + expectedException.expectMessage("Operational policy " + unknownPolicyId + " does not exist"); + + builder.setPolicyForPolicyResult( + unknownPolicyId, + triggerPolicy.getId(), + PolicyResult.FAILURE); + } @Test public void testAddOperationsAccumulateParams() { diff --git a/controlloop/common/policy-yaml/src/test/java/org/onap/policy/controlloop/policy/ControlLoopTest.java b/controlloop/common/policy-yaml/src/test/java/org/onap/policy/controlloop/policy/ControlLoopTest.java new file mode 100644 index 000000000..0b2f62def --- /dev/null +++ b/controlloop/common/policy-yaml/src/test/java/org/onap/policy/controlloop/policy/ControlLoopTest.java @@ -0,0 +1,171 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2018 Ericsson. 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.policy; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import java.util.ArrayList; +import java.util.List; +import org.junit.Ignore; +import org.junit.Test; +import org.onap.policy.aai.PNF; +import org.onap.policy.sdc.Resource; +import org.onap.policy.sdc.ResourceType; +import org.onap.policy.sdc.Service; + +public class ControlLoopTest { + + 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("pnf 1"); + + 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() { + final PNF pnf = new PNF(); + pnf.setPNFName("pnf 1"); + + ControlLoop controlLoop1 = new ControlLoop(); + controlLoop1.setControlLoopName(controlLoopName); + controlLoop1.setVersion(version); + Service service1 = new Service("service1"); + Service service2 = new Service("service2"); + List<Service> 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<Resource> 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 controlLoop2_service1 = new Service("service1"); + Service controlLoop2_service2 = new Service("service2"); + List<Service> controlLoop2_services = new ArrayList<>(); + controlLoop2_services.add(controlLoop2_service1); + controlLoop2_services.add(controlLoop2_service2); + controlLoop2.setServices(controlLoop2_services); + Resource controlLoop2_resource1 = new Resource("resource1", ResourceType.VF); + Resource controlLoop2_resource2 = new Resource("resource2", ResourceType.VFC); + List<Resource> controlLoop2_resources = new ArrayList<>(); + controlLoop2_resources.add(controlLoop2_resource1); + controlLoop2_resources.add(controlLoop2_resource2); + controlLoop2.setResources(controlLoop2_resources); + controlLoop2.setPnf(pnf); + controlLoop2.setTrigger_policy(triggerPolicy); + controlLoop2.setTimeout(timeout); + controlLoop1.setAbatement(abatement); + + 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("pnf 1"); + + ControlLoop controlLoop1 = new ControlLoop(); + controlLoop1.setControlLoopName(controlLoopName); + controlLoop1.setVersion(version); + Service service1 = new Service("service1"); + Service service2 = new Service("service2"); + List<Service> 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<Resource> 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/controlloop/common/policy-yaml/src/test/java/org/onap/policy/controlloop/policy/OperationsAccumulateParamsTest.java b/controlloop/common/policy-yaml/src/test/java/org/onap/policy/controlloop/policy/OperationsAccumulateParamsTest.java new file mode 100644 index 000000000..5d627a906 --- /dev/null +++ b/controlloop/common/policy-yaml/src/test/java/org/onap/policy/controlloop/policy/OperationsAccumulateParamsTest.java @@ -0,0 +1,116 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2018 Ericsson. 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.policy; + +import static org.junit.Assert.*; + +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() { + String period = "15m"; + 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/controlloop/common/policy-yaml/src/test/java/org/onap/policy/controlloop/policy/guard/ConstraintTest.java b/controlloop/common/policy-yaml/src/test/java/org/onap/policy/controlloop/policy/guard/ConstraintTest.java new file mode 100644 index 000000000..d0aa2e675 --- /dev/null +++ b/controlloop/common/policy-yaml/src/test/java/org/onap/policy/controlloop/policy/guard/ConstraintTest.java @@ -0,0 +1,249 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2018 Ericsson. 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.policy.guard; + +import static org.junit.Assert.*; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.junit.Test; + +public class ConstraintTest { + + @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<String, String> timeWindow = new HashMap<>(); + timeWindow.put("timeWindowKey", "timeWindowValue"); + Constraint constraint = new Constraint(); + constraint.setTime_window(timeWindow); + assertEquals(timeWindow, constraint.getTime_window()); + } + + @Test + public void testGetAndSetActive_time_range() { + Map<String, String> activeTimeRange = new HashMap<>(); + activeTimeRange.put("timeWindowKey", "timeWindowValue"); + Constraint constraint = new Constraint(); + constraint.setActive_time_range(activeTimeRange);; + assertEquals(activeTimeRange, constraint.getActive_time_range()); + } + + @Test + public void testGetAndSetBlacklist() { + List<String> 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<String, String> 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<String> 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<String, String> timeWindow = new HashMap<>(); + List<String> 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<String, String> timeWindow = new HashMap<>(); + Map<String, String> activeTimeRange = new HashMap<>(); + activeTimeRange.put("timeWindowKey", "timeWindowValue"); + 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<String, String> timeWindow = new HashMap<>(); + Map<String, String> activeTimeRange = new HashMap<>(); + activeTimeRange.put("timeWindowKey", "timeWindowValue"); + List<String> 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<String, String> timeWindow = new HashMap<>(); + Map<String, String> activeTimeRange = new HashMap<>(); + activeTimeRange.put("timeWindowKey", "timeWindowValue"); + List<String> 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; + Map<String, String> 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<String, String> timeWindow = new HashMap<>(); + Map<String, String> activeTimeRange = new HashMap<>(); + activeTimeRange.put("timeWindowKey", "timeWindowValue"); + List<String> 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; + Map<String, String> timeWindow = new HashMap<>(); + Map<String, String> activeTimeRange = new HashMap<>(); + List<String> 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/controlloop/common/policy-yaml/src/test/java/org/onap/policy/controlloop/policy/guard/ControlLoopGuardTest.java b/controlloop/common/policy-yaml/src/test/java/org/onap/policy/controlloop/policy/guard/ControlLoopGuardTest.java index d1a35413e..711997dac 100644 --- a/controlloop/common/policy-yaml/src/test/java/org/onap/policy/controlloop/policy/guard/ControlLoopGuardTest.java +++ b/controlloop/common/policy-yaml/src/test/java/org/onap/policy/controlloop/policy/guard/ControlLoopGuardTest.java @@ -27,6 +27,7 @@ import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; +import java.util.LinkedList; import org.junit.Test; import org.slf4j.Logger; @@ -49,6 +50,69 @@ public class ControlLoopGuardTest { 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<GuardPolicy> 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(){ + Guard guard1 = new Guard(); + GuardPolicy guardPolicy1 = new GuardPolicy(); + GuardPolicy guardPolicy2 = new GuardPolicy(); + LinkedList<GuardPolicy> 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("")); + } public void test(String testFile) { try (InputStream is = new FileInputStream(new File(testFile))) { diff --git a/controlloop/common/policy-yaml/src/test/java/org/onap/policy/controlloop/policy/guard/GuardPolicyTest.java b/controlloop/common/policy-yaml/src/test/java/org/onap/policy/controlloop/policy/guard/GuardPolicyTest.java new file mode 100644 index 000000000..3bf801d57 --- /dev/null +++ b/controlloop/common/policy-yaml/src/test/java/org/onap/policy/controlloop/policy/guard/GuardPolicyTest.java @@ -0,0 +1,267 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2018 Ericsson. 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.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 { + + @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<Constraint> 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<Constraint> 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<Constraint> 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<Constraint> 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<Constraint> 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<Constraint> 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<Constraint> 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(){ + String id = "guard id"; + String name = "guard name"; + 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<Constraint> 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("")); + } +} |