2 * ============LICENSE_START=======================================================
3 * policy-yaml unit test
4 * ================================================================================
5 * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2019 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.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertTrue;
26 import static org.junit.Assert.fail;
29 import java.io.FileInputStream;
30 import java.io.FileNotFoundException;
31 import java.io.IOException;
32 import java.io.InputStream;
33 import java.util.ArrayList;
34 import java.util.List;
36 import org.junit.Rule;
37 import org.junit.Test;
38 import org.junit.rules.ExpectedException;
39 import org.onap.policy.controlloop.policy.ControlLoopPolicy;
40 import org.onap.policy.controlloop.policy.FinalResult;
42 public class ControlLoopCompilerTest {
45 public ExpectedException expectedException = ExpectedException.none();
48 public void testTest() throws Exception {
49 List<String> expectedOnErrorMessages = new ArrayList<>();
50 expectedOnErrorMessages.add("Operational Policy has an bad ID");
51 expectedOnErrorMessages.add("Policy id is set to a PolicyResult SUCCESS");
52 expectedOnErrorMessages.add("Policy id is set to a FinalResult FINAL_SUCCESS");
53 expectedOnErrorMessages.add("Policy actor is null");
54 expectedOnErrorMessages.add("Policy actor is invalid");
55 expectedOnErrorMessages.add("Policy recipe is null");
56 expectedOnErrorMessages.add("Policy recipe is invalid");
57 expectedOnErrorMessages.add("Policy recipe is invalid");
58 expectedOnErrorMessages.add("Policy recipe is invalid");
59 expectedOnErrorMessages.add("Policy target is null");
60 expectedOnErrorMessages.add("Policy target is invalid");
61 expectedOnErrorMessages.add("Policy success is neither another policy nor FINAL_SUCCESS");
62 expectedOnErrorMessages.add("Policy failure is neither another policy nor FINAL_FAILURE");
63 expectedOnErrorMessages.add("Policy failure retries is neither another policy nor FINAL_FAILURE_RETRIES");
64 expectedOnErrorMessages.add("Policy failure timeout is neither another policy nor FINAL_FAILURE_TIMEOUT");
65 expectedOnErrorMessages.add("Policy failure exception is neither another policy nor FINAL_FAILURE_EXCEPTION");
66 expectedOnErrorMessages.add("Policy failure guard is neither another policy nor FINAL_FAILURE_GUARD");
67 expectedOnErrorMessages.add("Unsupported version for this compiler");
68 expectedOnErrorMessages.add("controlLoop overall timeout is less than the sum of operational policy timeouts.");
70 TestControlLoopCompilerCallback testControlLoopCompilerCallback =
71 new TestControlLoopCompilerCallback(expectedOnErrorMessages);
72 ControlLoopPolicy controlLoopPolicy = this.test("src/test/resources/v1.0.0/test.yaml",
73 testControlLoopCompilerCallback);
74 assertEquals(22, controlLoopPolicy.getPolicies().size());
75 assertTrue(testControlLoopCompilerCallback.areAllExpectedOnErrorsReceived());
79 public void testSuccessConnectedToUnknownPolicy() throws Exception {
80 expectedException.expect(CompilerException.class);
81 expectedException.expectMessage(
82 "Operation Policy unique-policy-id-1-restart is connected to unknown policy unknown-policy");
83 this.test("src/test/resources/v1.0.0/bad_policy_success_connected_to_unknown_policy.yaml");
87 public void testFailureConnectedToUnknownPolicy() throws Exception {
88 expectedException.expect(CompilerException.class);
89 expectedException.expectMessage(
90 "Operation Policy unique-policy-id-1-restart is connected to unknown policy unknown-policy");
91 this.test("src/test/resources/v1.0.0/bad_policy_failure_connected_to_unknown_policy.yaml");
95 public void testFailureTimeoutToUnknownPolicy() throws Exception {
96 expectedException.expect(CompilerException.class);
97 expectedException.expectMessage(
98 "Operation Policy unique-policy-id-1-restart is connected to unknown policy unknown-policy");
99 this.test("src/test/resources/v1.0.0/bad_policy_failure_timeout_connected_to_unknown_policy.yaml");
103 public void testFailureRetriesToUnknownPolicy() throws Exception {
104 expectedException.expect(CompilerException.class);
105 expectedException.expectMessage(
106 "Operation Policy unique-policy-id-1-restart is connected to unknown policy unknown-policy");
107 this.test("src/test/resources/v1.0.0/bad_policy_failure_retries_connected_to_unknown_policy.yaml");
111 public void testFailureExceptionToUnknownPolicy() throws Exception {
112 expectedException.expect(CompilerException.class);
113 expectedException.expectMessage(
114 "Operation Policy unique-policy-id-1-restart is connected to unknown policy unknown-policy");
115 this.test("src/test/resources/v1.0.0/bad_policy_failure_exception_connected_to_unknown_policy.yaml");
119 public void testFailureGuardToUnknownPolicy() throws Exception {
120 expectedException.expect(CompilerException.class);
121 expectedException.expectMessage(
122 "Operation Policy unique-policy-id-1-restart is connected to unknown policy unknown-policy");
123 this.test("src/test/resources/v1.0.0/bad_policy_failure_guard_connected_to_unknown_policy.yaml");
127 public void testInvalidTriggerPolicyId() throws Exception {
128 expectedException.expect(CompilerException.class);
129 expectedException.expectMessage(
130 "Unexpected value for trigger_policy, should only be "
131 + FinalResult.FINAL_OPENLOOP.toString() + " or a valid Policy ID");
132 this.test("src/test/resources/v1.0.0/bad_trigger_1.yaml");
136 public void testNoTriggerPolicyId() throws Exception {
137 expectedException.expect(CompilerException.class);
138 this.test("src/test/resources/v1.0.0/bad_trigger_no_trigger_id.yaml");
142 public void testNoControlLoopName() throws Exception {
143 List<String> expectedOnErrorMessages = new ArrayList<>();
144 expectedOnErrorMessages.add("Missing controlLoopName");
145 expectedOnErrorMessages.add("Unsupported version for this compiler");
146 TestControlLoopCompilerCallback testControlLoopCompilerCallback =
147 new TestControlLoopCompilerCallback(expectedOnErrorMessages);
148 this.test("src/test/resources/v1.0.0/bad_control_loop_no_control_loop_name.yaml",
149 testControlLoopCompilerCallback);
150 assertTrue(testControlLoopCompilerCallback.areAllExpectedOnErrorsReceived());
154 public void testInvalidFinalResult() throws Exception {
155 expectedException.expect(CompilerException.class);
156 expectedException.expectMessage(
157 "Unexpected Final Result for trigger_policy, should only be FINAL_OPENLOOP or a valid Policy ID");
158 this.test("src/test/resources/v1.0.0/bad_trigger_2.yaml");
162 public void testCompileEmptyFile() throws Exception {
163 expectedException.expect(CompilerException.class);
164 expectedException.expectMessage("Could not parse yaml specification.");
165 this.test("src/test/resources/v1.0.0/empty.yaml");
168 public ControlLoopPolicy test(String testFile) throws Exception {
169 return test(testFile, null);
173 * Does the actual test.
175 * @param testFile test file
176 * @param controlLoopCompilerCallback callback method
177 * @return the policy object
178 * @throws Exception exception
180 public ControlLoopPolicy test(String testFile,
181 ControlLoopCompilerCallback controlLoopCompilerCallback) throws Exception {
182 try (InputStream is = new FileInputStream(new File(testFile))) {
183 return ControlLoopCompiler.compile(is, controlLoopCompilerCallback);
184 } catch (FileNotFoundException e) {
185 fail(e.getMessage());
186 } catch (IOException e) {
187 fail(e.getMessage());
188 } catch (Exception e) {
194 class TestControlLoopCompilerCallback implements ControlLoopCompilerCallback {
196 private List<String> expectedOnErrorMessages;
198 public TestControlLoopCompilerCallback(List<String> expectedOnErrorMessages) {
199 this.expectedOnErrorMessages = expectedOnErrorMessages;
203 public boolean onWarning(String message) {
208 public boolean onError(String message) {
209 if (!expectedOnErrorMessages.remove(message)) {
210 fail("Unexpected onError message: " + message);
215 public boolean areAllExpectedOnErrorsReceived() {
216 return expectedOnErrorMessages.size() == 0;