2 * ============LICENSE_START=======================================================
3 * policy-yaml unit test
4 * ================================================================================
5 * Copyright (C) 2017-2019 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2019-2020 Nordix Foundation.
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.controlloop.compiler;
24 import static org.assertj.core.api.Assertions.assertThatThrownBy;
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertTrue;
27 import static org.junit.Assert.fail;
30 import java.io.FileInputStream;
31 import java.io.InputStream;
32 import java.util.ArrayList;
33 import java.util.List;
35 import org.junit.Test;
36 import org.onap.policy.controlloop.policy.ControlLoopPolicy;
37 import org.onap.policy.controlloop.policy.FinalResult;
39 public class ControlLoopCompilerTest {
40 private static final String RESTART_UNKNOWN_POLICY =
41 "Operation Policy unique-policy-id-1-restart is connected to unknown policy unknown-policy";
44 public void testTest() throws Exception {
45 List<String> expectedOnErrorMessages = new ArrayList<>();
46 expectedOnErrorMessages.add("Operational Policy has an bad ID");
47 expectedOnErrorMessages.add("Policy id is set to a PolicyResult SUCCESS");
48 expectedOnErrorMessages.add("Policy id is set to a FinalResult FINAL_SUCCESS");
49 expectedOnErrorMessages.add("Policy actor is null");
50 expectedOnErrorMessages.add("Policy recipe is null");
51 expectedOnErrorMessages.add("Policy target is null");
52 expectedOnErrorMessages.add("Policy target is invalid");
53 expectedOnErrorMessages.add("Policy success is neither another policy nor FINAL_SUCCESS");
54 expectedOnErrorMessages.add("Policy failure is neither another policy nor FINAL_FAILURE");
55 expectedOnErrorMessages.add("Policy failure retries is neither another policy nor FINAL_FAILURE_RETRIES");
56 expectedOnErrorMessages.add("Policy failure timeout is neither another policy nor FINAL_FAILURE_TIMEOUT");
57 expectedOnErrorMessages.add("Policy failure exception is neither another policy nor FINAL_FAILURE_EXCEPTION");
58 expectedOnErrorMessages.add("Policy failure guard is neither another policy nor FINAL_FAILURE_GUARD");
59 expectedOnErrorMessages.add("Unsupported version for this compiler");
60 expectedOnErrorMessages.add("controlLoop overall timeout is less than the sum of operational policy timeouts.");
62 TestControlLoopCompilerCallback testControlLoopCompilerCallback =
63 new TestControlLoopCompilerCallback(expectedOnErrorMessages);
64 ControlLoopPolicy controlLoopPolicy =
65 this.test("src/test/resources/v1.0.0/test.yaml", testControlLoopCompilerCallback);
66 assertEquals(22, controlLoopPolicy.getPolicies().size());
67 assertTrue(testControlLoopCompilerCallback.areAllExpectedOnErrorsReceived());
71 public void testSuccessConnectedToUnknownPolicy() throws Exception {
72 assertThatThrownBy(() -> {
73 this.test("src/test/resources/v1.0.0/bad_policy_success_connected_to_unknown_policy.yaml");
74 }).isInstanceOf(CompilerException.class).hasMessage(RESTART_UNKNOWN_POLICY);
78 public void testFailureConnectedToUnknownPolicy() throws Exception {
79 assertThatThrownBy(() -> {
80 this.test("src/test/resources/v1.0.0/bad_policy_failure_connected_to_unknown_policy.yaml");
81 }).isInstanceOf(CompilerException.class).hasMessage(RESTART_UNKNOWN_POLICY);
85 public void testFailureTimeoutToUnknownPolicy() throws Exception {
86 assertThatThrownBy(() -> {
87 this.test("src/test/resources/v1.0.0/bad_policy_failure_timeout_connected_to_unknown_policy.yaml");
88 }).isInstanceOf(CompilerException.class).hasMessage(RESTART_UNKNOWN_POLICY);
92 public void testFailureRetriesToUnknownPolicy() throws Exception {
93 assertThatThrownBy(() -> {
94 this.test("src/test/resources/v1.0.0/bad_policy_failure_retries_connected_to_unknown_policy.yaml");
95 }).isInstanceOf(CompilerException.class).hasMessage(RESTART_UNKNOWN_POLICY);
99 public void testFailureExceptionToUnknownPolicy() throws Exception {
100 assertThatThrownBy(() -> {
101 this.test("src/test/resources/v1.0.0/bad_policy_failure_exception_connected_to_unknown_policy.yaml");
102 }).isInstanceOf(CompilerException.class).hasMessage(RESTART_UNKNOWN_POLICY);
106 public void testFailureGuardToUnknownPolicy() throws Exception {
107 assertThatThrownBy(() -> {
108 this.test("src/test/resources/v1.0.0/bad_policy_failure_guard_connected_to_unknown_policy.yaml");
109 }).isInstanceOf(CompilerException.class).hasMessage(RESTART_UNKNOWN_POLICY);
113 public void testInvalidTriggerPolicyId() throws Exception {
114 assertThatThrownBy(() -> {
115 this.test("src/test/resources/v1.0.0/bad_trigger_1.yaml");
116 }).isInstanceOf(CompilerException.class).hasMessage("Unexpected value for trigger_policy, should only be "
117 + FinalResult.FINAL_OPENLOOP.toString() + " or a valid Policy ID");
121 public void testNoTriggerPolicyId() throws Exception {
122 assertThatThrownBy(() -> {
123 this.test("src/test/resources/v1.0.0/bad_trigger_no_trigger_id.yaml");
124 }).isInstanceOf(CompilerException.class);
128 public void testNoControlLoopName() throws Exception {
129 List<String> expectedOnErrorMessages = new ArrayList<>();
130 expectedOnErrorMessages.add("Missing controlLoopName");
131 expectedOnErrorMessages.add("Unsupported version for this compiler");
132 TestControlLoopCompilerCallback testControlLoopCompilerCallback =
133 new TestControlLoopCompilerCallback(expectedOnErrorMessages);
134 this.test("src/test/resources/v1.0.0/bad_control_loop_no_control_loop_name.yaml",
135 testControlLoopCompilerCallback);
136 assertTrue(testControlLoopCompilerCallback.areAllExpectedOnErrorsReceived());
140 public void testInvalidFinalResult() throws Exception {
141 assertThatThrownBy(() -> {
142 this.test("src/test/resources/v1.0.0/bad_trigger_2.yaml");
143 }).isInstanceOf(CompilerException.class).hasMessage(
144 "Unexpected Final Result for trigger_policy, should only be FINAL_OPENLOOP or a valid Policy ID");
148 public void testCompileEmptyFile() throws Exception {
149 assertThatThrownBy(() -> {
150 this.test("src/test/resources/v1.0.0/empty.yaml");
151 }).isInstanceOf(CompilerException.class).hasMessage("Could not parse yaml specification.");
154 public ControlLoopPolicy test(String testFile) throws Exception {
155 return test(testFile, null);
159 * Does the actual test.
161 * @param testFile test file
162 * @param controlLoopCompilerCallback callback method
163 * @return the policy object
164 * @throws Exception exception
166 public ControlLoopPolicy test(String testFile, ControlLoopCompilerCallback controlLoopCompilerCallback)
168 try (InputStream is = new FileInputStream(new File(testFile))) {
169 return ControlLoopCompiler.compile(is, controlLoopCompilerCallback);
173 class TestControlLoopCompilerCallback implements ControlLoopCompilerCallback {
175 private List<String> expectedOnErrorMessages;
177 public TestControlLoopCompilerCallback(List<String> expectedOnErrorMessages) {
178 this.expectedOnErrorMessages = expectedOnErrorMessages;
182 public boolean onWarning(String message) {
187 public boolean onError(String message) {
188 if (!expectedOnErrorMessages.remove(message)) {
189 fail("Unexpected onError message: " + message);
194 public boolean areAllExpectedOnErrorsReceived() {
195 return expectedOnErrorMessages.isEmpty();