From 658d2377a96284739b283b855b80dd80196dc91f Mon Sep 17 00:00:00 2001 From: liamfallon Date: Thu, 12 Mar 2020 12:15:04 +0000 Subject: Clean up minor checkstyle/sonar issues A few minor eclipse warnings and checkstyle warnings in policy models. Issue-ID: POLICY-1910 Change-Id: I612c6514fa7d88cb8658a7ee1618869a525e48d7 Signed-off-by: liamfallon --- .../builder/impl/ControlLoopPolicyBuilderImpl.java | 66 +- .../compiler/ControlLoopCompilerTest.java | 96 ++- .../policy/ControlLoopPolicyBuilderTest.java | 663 +++++++++++---------- .../controlloop/policy/ControlLoopPolicyTest.java | 50 +- .../src/test/resources/v1.0.0/policy_Test.yaml | 32 +- .../src/test/resources/v1.0.0/policy_vService.yaml | 19 +- .../v2.0.0/policy_ONAP_demo_vFirewall.yaml | 4 +- 7 files changed, 462 insertions(+), 468 deletions(-) (limited to 'models-interactions/model-yaml/src') diff --git a/models-interactions/model-yaml/src/main/java/org/onap/policy/controlloop/policy/builder/impl/ControlLoopPolicyBuilderImpl.java b/models-interactions/model-yaml/src/main/java/org/onap/policy/controlloop/policy/builder/impl/ControlLoopPolicyBuilderImpl.java index 779e3096f..f1a9540c0 100644 --- a/models-interactions/model-yaml/src/main/java/org/onap/policy/controlloop/policy/builder/impl/ControlLoopPolicyBuilderImpl.java +++ b/models-interactions/model-yaml/src/main/java/org/onap/policy/controlloop/policy/builder/impl/ControlLoopPolicyBuilderImpl.java @@ -3,14 +3,14 @@ * policy-yaml * ================================================================================ * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved. - * Modifications Copyright (C) 2019 Nordix Foundation. + * 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. @@ -22,6 +22,7 @@ package org.onap.policy.controlloop.policy.builder.impl; import com.google.common.base.Strings; + import java.util.LinkedList; import java.util.UUID; @@ -42,20 +43,17 @@ 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.Service; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import org.yaml.snakeyaml.DumperOptions; import org.yaml.snakeyaml.DumperOptions.FlowStyle; import org.yaml.snakeyaml.Yaml; public class ControlLoopPolicyBuilderImpl implements ControlLoopPolicyBuilder { private static final String UNKNOWN_POLICY = "Unknown policy "; - private static Logger logger = LoggerFactory.getLogger(ControlLoopPolicyBuilderImpl.class.getName()); private ControlLoopPolicy controlLoopPolicy; /** * Constructor. - * + * * @param controlLoopName control loop id * @param timeout timeout value */ @@ -69,15 +67,15 @@ public class ControlLoopPolicyBuilderImpl implements ControlLoopPolicyBuilder { /** * Constructor. - * + * * @param controlLoopName control loop id * @param timeout timeout value * @param resource resource * @param services services * @throws BuilderException builder exception */ - public ControlLoopPolicyBuilderImpl(String controlLoopName, Integer timeout, Resource resource, Service... services) - throws BuilderException { + public ControlLoopPolicyBuilderImpl(String controlLoopName, Integer timeout, Resource resource, + Service... services) throws BuilderException { this(controlLoopName, timeout); this.addResource(resource); this.addService(services); @@ -90,15 +88,15 @@ public class ControlLoopPolicyBuilderImpl implements ControlLoopPolicyBuilder { /** * Constructor. - * + * * @param controlLoopName control loop id * @param timeout timeout * @param service service * @param resources resources * @throws BuilderException builder exception */ - public ControlLoopPolicyBuilderImpl(String controlLoopName, Integer timeout, Service service, Resource[] resources) - throws BuilderException { + public ControlLoopPolicyBuilderImpl(String controlLoopName, Integer timeout, Service service, + Resource[] resources) throws BuilderException { this(controlLoopName, timeout); this.addService(service); this.addResource(resources); @@ -116,7 +114,7 @@ public class ControlLoopPolicyBuilderImpl implements ControlLoopPolicyBuilder { if (service == null) { throw new BuilderException("Service must not be null"); } - if (service.getServiceUUID() == null && Strings.isNullOrEmpty(service.getServiceName())) { + if (service.getServiceUuid() == null && Strings.isNullOrEmpty(service.getServiceName())) { throw new BuilderException("Invalid service - need either a serviceUUID or serviceName"); } if (controlLoopPolicy.getControlLoop().getServices() == null) { @@ -136,7 +134,7 @@ public class ControlLoopPolicyBuilderImpl implements ControlLoopPolicyBuilder { if (service == null) { throw new BuilderException("Service must not be null"); } - if (service.getServiceUUID() == null && Strings.isNullOrEmpty(service.getServiceName())) { + if (service.getServiceUuid() == null && Strings.isNullOrEmpty(service.getServiceName())) { throw new BuilderException("Invalid service - need either a serviceUUID or serviceName"); } boolean removed = controlLoopPolicy.getControlLoop().getServices().remove(service); @@ -153,7 +151,6 @@ public class ControlLoopPolicyBuilderImpl implements ControlLoopPolicyBuilder { return this; } - @Override public ControlLoopPolicyBuilder addResource(Resource... resources) throws BuilderException { for (Resource resource : resources) { @@ -199,8 +196,7 @@ public class ControlLoopPolicyBuilderImpl implements ControlLoopPolicyBuilder { } @Override - public Policy setTriggerPolicy(PolicyParam policyParam) - throws BuilderException { + public Policy setTriggerPolicy(PolicyParam policyParam) throws BuilderException { Policy trigger = new Policy(policyParam); @@ -228,8 +224,7 @@ public class ControlLoopPolicyBuilderImpl implements ControlLoopPolicyBuilder { } @Override - public Policy setPolicyForPolicyResult(PolicyParam policyParam, PolicyResult... results) - throws BuilderException { + public Policy setPolicyForPolicyResult(PolicyParam policyParam, PolicyResult... results) throws BuilderException { // // Find the existing policy // @@ -240,17 +235,19 @@ public class ControlLoopPolicyBuilderImpl implements ControlLoopPolicyBuilder { // // Create the new Policy // - Policy newPolicy = new Policy( - PolicyParam.builder().id(UUID.randomUUID().toString()) - .name(policyParam.getName()) - .description(policyParam.getDescription()) - .actor(policyParam.getActor()) - .payload(policyParam.getPayload()) - .target(policyParam.getTarget()) - .recipe(policyParam.getRecipe()) - .retries(policyParam.getRetries()) - .timeout(policyParam.getTimeout()) - .build()); + // @formatter:off + Policy newPolicy = new Policy(PolicyParam.builder() + .id(UUID.randomUUID().toString()) + .name(policyParam.getName()) + .description(policyParam.getDescription()) + .actor(policyParam.getActor()) + .payload(policyParam.getPayload()) + .target(policyParam.getTarget()) + .recipe(policyParam.getRecipe()) + .retries(policyParam.getRetries()) + .timeout(policyParam.getTimeout()) + .build()); + // @formatter:on // // Connect the results // @@ -290,7 +287,7 @@ public class ControlLoopPolicyBuilderImpl implements ControlLoopPolicyBuilder { @Override public Policy setPolicyForPolicyResult(String policyResultId, String policyId, PolicyResult... results) - throws BuilderException { + throws BuilderException { // // Find the existing policy // @@ -368,7 +365,6 @@ public class ControlLoopPolicyBuilderImpl implements ControlLoopPolicyBuilder { try { ControlLoopCompiler.compile(controlLoopPolicy, callback); } catch (CompilerException e) { - logger.error(e.getMessage() + e); callback.results.addMessage(new MessageImpl(e.getMessage(), MessageLevel.EXCEPTION)); } // @@ -434,7 +430,7 @@ public class ControlLoopPolicyBuilderImpl implements ControlLoopPolicyBuilder { @Override public boolean isOpenLoop() { return this.controlLoopPolicy.getControlLoop().getTrigger_policy() - .equals(FinalResult.FINAL_OPENLOOP.toString()); + .equals(FinalResult.FINAL_OPENLOOP.toString()); } @Override @@ -529,7 +525,7 @@ public class ControlLoopPolicyBuilderImpl implements ControlLoopPolicyBuilder { @Override public Policy addOperationsAccumulateParams(String policyId, OperationsAccumulateParams operationsAccumulateParams) - throws BuilderException { + throws BuilderException { Policy existingPolicy = this.findPolicy(policyId); if (existingPolicy == null) { throw new BuilderException(UNKNOWN_POLICY + policyId); 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 index 6fc3e1acb..98c00eb2f 100644 --- 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 @@ -3,7 +3,7 @@ * policy-yaml unit test * ================================================================================ * Copyright (C) 2017-2019 AT&T Intellectual Property. All rights reserved. - * Modifications Copyright (C) 2019 Nordix Foundation. + * 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. @@ -21,6 +21,7 @@ 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; @@ -30,17 +31,14 @@ import java.io.FileInputStream; 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 { private static final String RESTART_UNKNOWN_POLICY = - "Operation Policy unique-policy-id-1-restart is connected to unknown policy unknown-policy"; - @Rule - public ExpectedException expectedException = ExpectedException.none(); + "Operation Policy unique-policy-id-1-restart is connected to unknown policy unknown-policy"; @Test public void testTest() throws Exception { @@ -62,74 +60,68 @@ public class ControlLoopCompilerTest { 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); + 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( - RESTART_UNKNOWN_POLICY); - this.test("src/test/resources/v1.0.0/bad_policy_success_connected_to_unknown_policy.yaml"); + 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 { - expectedException.expect(CompilerException.class); - expectedException.expectMessage( - RESTART_UNKNOWN_POLICY); - this.test("src/test/resources/v1.0.0/bad_policy_failure_connected_to_unknown_policy.yaml"); + 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 { - expectedException.expect(CompilerException.class); - expectedException.expectMessage( - RESTART_UNKNOWN_POLICY); - this.test("src/test/resources/v1.0.0/bad_policy_failure_timeout_connected_to_unknown_policy.yaml"); + 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 { - expectedException.expect(CompilerException.class); - expectedException.expectMessage( - RESTART_UNKNOWN_POLICY); - this.test("src/test/resources/v1.0.0/bad_policy_failure_retries_connected_to_unknown_policy.yaml"); + 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 { - expectedException.expect(CompilerException.class); - expectedException.expectMessage( - RESTART_UNKNOWN_POLICY); - this.test("src/test/resources/v1.0.0/bad_policy_failure_exception_connected_to_unknown_policy.yaml"); + 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 { - expectedException.expect(CompilerException.class); - expectedException.expectMessage( - RESTART_UNKNOWN_POLICY); - this.test("src/test/resources/v1.0.0/bad_policy_failure_guard_connected_to_unknown_policy.yaml"); + 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 { - 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"); + 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 { - expectedException.expect(CompilerException.class); - this.test("src/test/resources/v1.0.0/bad_trigger_no_trigger_id.yaml"); + assertThatThrownBy(() -> { + this.test("src/test/resources/v1.0.0/bad_trigger_no_trigger_id.yaml"); + }).isInstanceOf(CompilerException.class); } @Test @@ -138,25 +130,25 @@ public class ControlLoopCompilerTest { expectedOnErrorMessages.add("Missing controlLoopName"); expectedOnErrorMessages.add("Unsupported version for this compiler"); TestControlLoopCompilerCallback testControlLoopCompilerCallback = - new TestControlLoopCompilerCallback(expectedOnErrorMessages); + new TestControlLoopCompilerCallback(expectedOnErrorMessages); this.test("src/test/resources/v1.0.0/bad_control_loop_no_control_loop_name.yaml", - testControlLoopCompilerCallback); + 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"); + 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 { - expectedException.expect(CompilerException.class); - expectedException.expectMessage("Could not parse yaml specification."); - this.test("src/test/resources/v1.0.0/empty.yaml"); + 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 { @@ -171,8 +163,8 @@ public class ControlLoopCompilerTest { * @return the policy object * @throws Exception exception */ - public ControlLoopPolicy test(String testFile, - ControlLoopCompilerCallback controlLoopCompilerCallback) throws Exception { + public ControlLoopPolicy test(String testFile, ControlLoopCompilerCallback controlLoopCompilerCallback) + throws Exception { try (InputStream is = new FileInputStream(new File(testFile))) { return ControlLoopCompiler.compile(is, controlLoopCompilerCallback); } @@ -202,7 +194,5 @@ public class ControlLoopCompilerTest { public boolean areAllExpectedOnErrorsReceived() { return expectedOnErrorMessages.isEmpty(); } - } - } 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 index f646712fd..81520b578 100644 --- 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 @@ -3,7 +3,7 @@ * policy-yaml unit test * ================================================================================ * Copyright (C) 2017-2020 AT&T Intellectual Property. All rights reserved. - * Modifications Copyright (C) 2019 Nordix Foundation. + * 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. @@ -32,10 +32,9 @@ import java.io.File; import java.io.FileInputStream; 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.aai.domain.yang.Pnf; import org.onap.policy.controlloop.policy.builder.BuilderException; import org.onap.policy.controlloop.policy.builder.ControlLoopPolicyBuilder; @@ -49,7 +48,6 @@ 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"; @@ -60,8 +58,6 @@ public class ControlLoopPolicyBuilderTest { 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."; - @Rule - public ExpectedException expectedException = ExpectedException.none(); @Test public void testControlLoop() throws BuilderException { @@ -69,7 +65,7 @@ public class ControlLoopPolicyBuilderTest { // Create a builder for our policy // ControlLoopPolicyBuilder builder = - ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400); + ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400); // // Test add services // @@ -105,25 +101,28 @@ public class ControlLoopPolicyBuilderTest { @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); + 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); - expectedException.expect(BuilderException.class); - expectedException.expectMessage("Invalid service - need either a serviceUUID or serviceName"); - builder.addService(new Service()); + 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); + ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400); UUID uuid = UUID.randomUUID(); Service serviceWithUuid = new Service(uuid); builder.addService(serviceWithUuid); @@ -133,26 +132,28 @@ public class ControlLoopPolicyBuilderTest { @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); - } + 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); - expectedException.expect(BuilderException.class); - expectedException.expectMessage("Invalid resource - need either resourceUUID or resourceName"); - builder.addResource(new Resource()); + 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); + ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400); UUID uuid = UUID.randomUUID(); Resource resourceWithUuid = new Resource(uuid); builder.addResource(resourceWithUuid); @@ -165,44 +166,49 @@ public class ControlLoopPolicyBuilderTest { @Test public void testRemoveNullResource() throws BuilderException { ControlLoopPolicyBuilder builder = - ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400); + 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); + + 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); - expectedException.expect(BuilderException.class); - expectedException.expectMessage("No existing resources to remove"); - builder.removeResource(new Resource(RESOURCE1, ResourceType.VF)); + 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); + 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()); + + 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); + 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)); + + assertThatThrownBy(() -> { + builder.removeResource(new Resource(unknownResourceName, ResourceType.VF)); + }).isInstanceOf(BuilderException.class).hasMessage("Unknown resource " + unknownResourceName); } @Test @@ -210,8 +216,8 @@ public class ControlLoopPolicyBuilderTest { 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); + ControlLoopPolicyBuilder builder = + ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400, cts, scp, usp); assertTrue(builder.getControlLoop().getResources().size() == 1); assertTrue(builder.getControlLoop().getServices().size() == 2); } @@ -221,8 +227,8 @@ public class ControlLoopPolicyBuilderTest { 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); + ControlLoopPolicyBuilder builder = + ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400, scp, cts, com); assertTrue(builder.getControlLoop().getServices().size() == 1); assertTrue(builder.getControlLoop().getResources().size() == 2); } @@ -238,9 +244,9 @@ public class ControlLoopPolicyBuilderTest { // 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); + // pnf.setPnfType(PnfType.ENODEB); ControlLoopPolicyBuilder builder = - ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400, pnf); + ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400, pnf); assertEquals(pnf, builder.getControlLoop().getPnf()); builder.removePnf(); @@ -252,11 +258,11 @@ public class ControlLoopPolicyBuilderTest { // Fails for the same reason as the above test case public void testSetAndRemovePnf() throws BuilderException { ControlLoopPolicyBuilder builder = - ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400); + ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400); assertNull(builder.getControlLoop().getPnf()); Pnf pnf = new Pnf(); - //pnf.setPnfType(PnfType.ENODEB); + // pnf.setPnfType(PnfType.ENODEB); builder.setPnf(pnf); assertEquals(pnf, builder.getControlLoop().getPnf()); @@ -267,25 +273,27 @@ public class ControlLoopPolicyBuilderTest { @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); + 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); - expectedException.expect(BuilderException.class); - expectedException.expectMessage("Invalid PNF - need either pnfName or pnfType"); - builder.setPnf(new Pnf()); + 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); + ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400); assertFalse(builder.getControlLoop().getAbatement()); builder = builder.setAbatement(true); assertTrue(builder.getControlLoop().getAbatement()); @@ -293,11 +301,12 @@ public class ControlLoopPolicyBuilderTest { @Test public void testSetNullAbatement() throws BuilderException { - ControlLoopPolicyBuilder builder = + assertThatThrownBy(() -> { + ControlLoopPolicyBuilder builder = ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400); - expectedException.expect(BuilderException.class); - expectedException.expectMessage("abatement must not be null"); - builder = builder.setAbatement(null); + + builder = builder.setAbatement(null); + }).isInstanceOf(BuilderException.class).hasMessage("abatement must not be null"); } @Test @@ -306,7 +315,7 @@ public class ControlLoopPolicyBuilderTest { // Create a builder for our policy // ControlLoopPolicyBuilder builder = - ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400); + ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400); // // Test setTimeout // @@ -316,38 +325,38 @@ public class ControlLoopPolicyBuilderTest { // // Test calculateTimeout // - 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()); + // @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); + 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); + ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400); // // Test isOpenLoop // @@ -355,35 +364,33 @@ public class ControlLoopPolicyBuilderTest { // // Test set initial trigger policy // - 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()); + // @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()); + 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 // @@ -399,78 +406,77 @@ public class ControlLoopPolicyBuilderTest { @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.setExistingTriggerPolicy(null); + 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); + ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400); final String unknownPolicyId = "100"; - expectedException.expect(BuilderException.class); - expectedException.expectMessage(UNKNOWN_POLICY + unknownPolicyId); - builder.setExistingTriggerPolicy(unknownPolicyId); + + 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); - 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()); + 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"; - expectedException.expect(BuilderException.class); - expectedException.expectMessage(UNKNOWN_POLICY + unknownPolicyId); - builder.setExistingTriggerPolicy(unknownPolicyId); + + 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); - 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()); + 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); + 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())); @@ -481,19 +487,20 @@ public class ControlLoopPolicyBuilderTest { // // 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); - // + 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())); // @@ -502,55 +509,52 @@ public class ControlLoopPolicyBuilderTest { 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())); + 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 // - 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: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())); + 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); + 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); + 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())); @@ -568,129 +572,136 @@ public class ControlLoopPolicyBuilderTest { @Test public void testAddToUnknownPolicy() throws BuilderException { ControlLoopPolicyBuilder builder = - ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400); + ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400); final String policyId = "100"; - expectedException.expect(BuilderException.class); - expectedException.expectMessage(UNKNOWN_POLICY + policyId); - builder.setPolicyForPolicyResult( + 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, + .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); - 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); - + 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"; - expectedException.expect(BuilderException.class); - expectedException.expectMessage(unknownPolicyId + " does not exist"); - builder.setPolicyForPolicyResult(onRestartFailurePolicy.getId(), unknownPolicyId, PolicyResult.FAILURE); + 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); - 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()); + 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"; - expectedException.expect(BuilderException.class); - expectedException.expectMessage("Operational policy " + unknownPolicyId + " does not exist"); - builder.setPolicyForPolicyResult(unknownPolicyId, triggerPolicy.getId(), PolicyResult.FAILURE); + 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); + 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()); + 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)); + 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); + ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 800); // // Set the first invalid trigger policy // + // @formatter:off final Policy policy1 = builder.setTriggerPolicy( - PolicyParam.builder() + PolicyParam.builder() .id(UUID.randomUUID().toString()) .name(RESTART_VM) .description(TRIGGER_RESTART) @@ -699,8 +710,10 @@ public class ControlLoopPolicyBuilderTest { .recipe(null) .payload(null) .retries(2) - .timeout(300).build()); + .timeout(300) + .build()); Results results = builder.buildSpecification(); + // @formatter:on // // Check that ERRORs are in results for invalid policy arguments // @@ -732,32 +745,33 @@ public class ControlLoopPolicyBuilderTest { // // Set a valid trigger policy // - 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()); + // @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()); + 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 // @@ -765,7 +779,7 @@ public class ControlLoopPolicyBuilderTest { boolean unreachable = false; for (Message m : results.getMessages()) { if (m.getMessage().equals("Policy " + policy1a.getId() + " is not reachable.") - && m.getLevel() == MessageLevel.WARNING) { + && m.getLevel() == MessageLevel.WARNING) { unreachable = true; break; } @@ -775,12 +789,12 @@ public class ControlLoopPolicyBuilderTest { // Set policy1a for the failure results of policy2 // policy1a = builder.setPolicyForPolicyResult(policy1a.getId(), policy2.getId(), PolicyResult.FAILURE, - PolicyResult.FAILURE_RETRIES, PolicyResult.FAILURE_TIMEOUT); + 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) { + .equals(m.getMessage()) && m.getLevel() == MessageLevel.ERROR) { invalidTimeout = true; break; } @@ -797,7 +811,7 @@ public class ControlLoopPolicyBuilderTest { 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) { + && m.getLevel() == MessageLevel.WARNING) { unreachable = true; break; } @@ -805,7 +819,6 @@ public class ControlLoopPolicyBuilderTest { assertTrue(unreachable); } - @Test public void test1() throws Exception { this.test("src/test/resources/v1.0.0/policy_Test.yaml"); @@ -842,20 +855,20 @@ public class ControlLoopPolicyBuilderTest { // Now we're going to try to use the builder to build this. // ControlLoopPolicyBuilder builder = ControlLoopPolicyBuilder.Factory.buildControlLoop( - policyTobuild.getControlLoop().getControlLoopName(), policyTobuild.getControlLoop().getTimeout()); + 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()])); + .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()])); + .toArray(new Resource[policyTobuild.getControlLoop().getResources().size()])); } // // Set pnf @@ -879,12 +892,11 @@ public class ControlLoopPolicyBuilderTest { } private void setTriggerPolicies(ControlLoopPolicy policyTobuild, ControlLoopPolicyBuilder builder) - throws BuilderException { + throws BuilderException { for (Policy policy : policyTobuild.getPolicies()) { if (policy.getId() == policyTobuild.getControlLoop().getTrigger_policy()) { - builder.setTriggerPolicy( - PolicyParam.builder() - .id(UUID.randomUUID().toString()) + // @formatter:off + builder.setTriggerPolicy(PolicyParam.builder().id(UUID.randomUUID().toString()) .name(policy.getName()) .description(policy.getDescription()) .actor(policy.getActor()) @@ -892,9 +904,10 @@ public class ControlLoopPolicyBuilderTest { .recipe(policy.getRecipe()) .payload(null) .retries(policy.getRetry()) - .timeout(policy.getTimeout()).build()); + .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 index 0b61991f1..43428c6f9 100644 --- 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 @@ -3,7 +3,7 @@ * policy-yaml unit test * ================================================================================ * Copyright (C) 2017-2019 AT&T Intellectual Property. All rights reserved. - * Modifications Copyright (C) 2019 Nordix Foundation. + * 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. @@ -23,20 +23,14 @@ package org.onap.policy.controlloop.policy; import static org.junit.Assert.assertEquals; 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.io.InputStreamReader; + import org.junit.Test; -import org.onap.policy.common.utils.io.Serializer; +import org.onap.policy.common.utils.coder.YamlJsonTranslator; 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 ControlLoopPolicyTest { private static final Logger logger = LoggerFactory.getLogger(ControlLoopPolicyTest.class); @@ -62,9 +56,8 @@ public class ControlLoopPolicyTest { } @Test - public void testvFirewall() { - // Chenfei to fix this. - // this.test("src/test/resources/v2.0.0/policy_ONAP_demo_vFirewall.yaml"); + public void testvFirewall() throws Exception { + this.test("src/test/resources/v2.0.0/policy_ONAP_demo_vFirewall.yaml"); } @Test @@ -89,37 +82,30 @@ public class ControlLoopPolicyTest { * @throws Exception if an error occurs */ public void test(String testFile) throws Exception { - try (InputStream is = new FileInputStream(new File(testFile))) { + try (InputStreamReader fileInputStream = new InputStreamReader(new FileInputStream(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); - dump(obj); + ControlLoopPolicy controlLoopPolicy1 = + new YamlJsonTranslator().fromYaml(fileInputStream, ControlLoopPolicy.class); + assertNotNull(controlLoopPolicy1); + dump(controlLoopPolicy1); + // // 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); + String dumpedYaml = new YamlJsonTranslator().toYaml(controlLoopPolicy1); logger.debug(dumpedYaml); // // Read that string back into our java object // - Object newObject = yaml.load(dumpedYaml); - dump(newObject); - assertNotNull(newObject); - assertTrue(newObject instanceof ControlLoopPolicy); - assertEquals(obj, newObject); + ControlLoopPolicy controlLoopPolicy2 = + new YamlJsonTranslator().fromYaml(dumpedYaml, ControlLoopPolicy.class); + assertNotNull(controlLoopPolicy2); + dump(controlLoopPolicy2); // test serialization - ControlLoopPolicy policy = (ControlLoopPolicy) obj; - ControlLoopPolicy policy2 = Serializer.roundTrip(policy); - assertTrue(policy.equals(policy2)); + assertEquals(controlLoopPolicy1, controlLoopPolicy2); } } diff --git a/models-interactions/model-yaml/src/test/resources/v1.0.0/policy_Test.yaml b/models-interactions/model-yaml/src/test/resources/v1.0.0/policy_Test.yaml index 73486b08b..de2590c15 100644 --- a/models-interactions/model-yaml/src/test/resources/v1.0.0/policy_Test.yaml +++ b/models-interactions/model-yaml/src/test/resources/v1.0.0/policy_Test.yaml @@ -1,5 +1,5 @@ # Copyright 2018 AT&T Intellectual Property. All rights reserved -# Modifications Copyright (C) 2019 Nordix Foundation. +# 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. @@ -15,9 +15,9 @@ controlLoop: controlLoopName: ControlLoop-TEST-5dfa8fce-bd7a-4424-b60d-ee2ad2f254a3 version: 1.0.0 - services: + services: - serviceName: Foo Service - resources: + resources: - resourceName: Bar VNF resourceType: VF trigger_policy: unique-policy-id-1-restart @@ -29,7 +29,8 @@ policies: description: actor: APPC recipe: Restart - target: VM + target: + type: VM retry: 2 timeout: 300 success: unique-policy-id-2-healthcheck-restart @@ -37,13 +38,14 @@ policies: failure_timeout: unique-policy-id-3-rebuild failure_retries: unique-policy-id-3-rebuild failure_exception: final_failure_exception - + - id: unique-policy-id-2-healthcheck-restart name: HealthCheck Policy description: actor: APPC recipe: HealthCheck - target: VM + target: + type: VM retry: 2 timeout: 300 success: final_success @@ -51,13 +53,14 @@ policies: failure_timeout: unique-policy-id-3-rebuild failure_retries: unique-policy-id-3-rebuild failure_exception: final_failure_exception - + - id: unique-policy-id-3-rebuild name: Rebuild Policy description: actor: APPC recipe: Rebuild - target: VM + target: + type: VM retry: 0 timeout: 600 success: unique-policy-id-4-healthcheck-rebuild @@ -65,13 +68,14 @@ policies: failure_timeout: unique-policy-id-5-migrate failure_retries: unique-policy-id-5-migrate failure_exception: final_failure_exception - + - id: unique-policy-id-4-healthcheck-rebuild name: HealthCheck the Rebuild Policy description: actor: APPC recipe: HealthCheck - target: VM + target: + type: VM retry: 2 timeout: 300 success: final_success @@ -79,13 +83,14 @@ policies: failure_timeout: unique-policy-id-5-migrate failure_retries: unique-policy-id-5-migrate failure_exception: final_failure_exception - + - id: unique-policy-id-5-migrate name: Migrate Policy description: actor: APPC recipe: Migrate - target: VM + target: + type: VM retry: 0 timeout: 600 success: unique-policy-id-6-healthcheck-migrate @@ -99,7 +104,8 @@ policies: description: actor: APPC recipe: HealthCheck - target: VM + target: + type: VM retry: 2 timeout: 300 success: final_success diff --git a/models-interactions/model-yaml/src/test/resources/v1.0.0/policy_vService.yaml b/models-interactions/model-yaml/src/test/resources/v1.0.0/policy_vService.yaml index 009a49c19..44c081cb7 100644 --- a/models-interactions/model-yaml/src/test/resources/v1.0.0/policy_vService.yaml +++ b/models-interactions/model-yaml/src/test/resources/v1.0.0/policy_vService.yaml @@ -1,5 +1,5 @@ # Copyright 2018 AT&T Intellectual Property. All rights reserved -# Modifications Copyright (C) 2019 Nordix Foundation. +# 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. @@ -15,9 +15,9 @@ controlLoop: version: 1.0.0 controlLoopName: ControlLoop-vUSP-vCTS-cbed919f-2212-4ef7-8051-fe6308da1bda - services: + services: - serviceName: vUSP - resources: + resources: - resourceName: vCTS resourceType: VF - resourceName: vCOM @@ -37,7 +37,8 @@ policies: description: actor: APPC recipe: Restart - target: VM + target: + type: VM retry: 2 timeout: 300 success: final_success @@ -45,13 +46,14 @@ policies: failure_timeout: unique-policy-id-2-rebuild failure_retries: unique-policy-id-2-rebuild failure_exception: final_failure_exception - + - id: unique-policy-id-2-rebuild name: Rebuild Policy description: actor: APPC recipe: Rebuild - target: VM + target: + type: VM retry: 0 timeout: 600 success: final_success @@ -59,13 +61,14 @@ policies: failure_timeout: unique-policy-id-3-migrate failure_retries: unique-policy-id-3-migrate failure_exception: final_failure_exception - + - id: unique-policy-id-3-migrate name: Migrate Policy description: actor: APPC recipe: Migrate - target: VM + target: + type: VM retry: 0 timeout: 600 success: final_success diff --git a/models-interactions/model-yaml/src/test/resources/v2.0.0/policy_ONAP_demo_vFirewall.yaml b/models-interactions/model-yaml/src/test/resources/v2.0.0/policy_ONAP_demo_vFirewall.yaml index 00b9b4cb1..feaba64cf 100644 --- a/models-interactions/model-yaml/src/test/resources/v2.0.0/policy_ONAP_demo_vFirewall.yaml +++ b/models-interactions/model-yaml/src/test/resources/v2.0.0/policy_ONAP_demo_vFirewall.yaml @@ -1,5 +1,5 @@ # Copyright 2018-2019 AT&T Intellectual Property. All rights reserved -# Modifications Copyright (C) 2019 Nordix Foundation. +# 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. @@ -60,7 +60,7 @@ policies: target: resourceID: Eace933104d443b496b8.nodes.heat.vpg payload: - generic-vnf.vnf-id: {generic-vnf.vnf-id} + generic-vnf.vnf-id: '{generic-vnf.vnf-id}' streams: '{"active-streams":5}' retry: 0 timeout: 300 -- cgit 1.2.3-korg