2 * ============LICENSE_START=======================================================
3 * policy-yaml unit test
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.controlloop.compiler;
23 import static org.junit.Assert.*;
26 import java.io.FileInputStream;
27 import java.io.FileNotFoundException;
28 import java.io.IOException;
29 import java.io.InputStream;
30 import java.util.ArrayList;
31 import java.util.List;
33 import org.junit.Rule;
34 import org.junit.Test;
35 import org.junit.rules.ExpectedException;
36 import org.onap.policy.controlloop.policy.ControlLoopPolicy;
37 import org.onap.policy.controlloop.policy.FinalResult;
39 public class ControlLoopCompilerTest {
42 public ExpectedException expectedException = ExpectedException.none();
45 public void testTest() throws Exception {
46 List<String> expectedOnErrorMessages = new ArrayList<>();
47 expectedOnErrorMessages.add("Operational Policy has an bad ID");
48 expectedOnErrorMessages.add("Policy id is set to a PolicyResult SUCCESS");
49 expectedOnErrorMessages.add("Policy id is set to a FinalResult FINAL_SUCCESS");
50 expectedOnErrorMessages.add("Policy actor is null");
51 expectedOnErrorMessages.add("Policy actor is invalid");
52 expectedOnErrorMessages.add("Policy recipe is null");
53 expectedOnErrorMessages.add("Policy recipe is invalid");
54 expectedOnErrorMessages.add("Policy recipe is invalid");
55 expectedOnErrorMessages.add("Policy recipe is invalid");
56 expectedOnErrorMessages.add("Policy target is null");
57 expectedOnErrorMessages.add("Policy target is invalid");
58 expectedOnErrorMessages.add("Policy success is neither another policy nor FINAL_SUCCESS");
59 expectedOnErrorMessages.add("Policy failure is neither another policy nor FINAL_FAILURE");
60 expectedOnErrorMessages.add("Policy failure retries is neither another policy nor FINAL_FAILURE_RETRIES");
61 expectedOnErrorMessages.add("Policy failure timeout is neither another policy nor FINAL_FAILURE_TIMEOUT");
62 expectedOnErrorMessages.add("Policy failure exception is neither another policy nor FINAL_FAILURE_EXCEPTION");
63 expectedOnErrorMessages.add("Policy failure guard is neither another policy nor FINAL_FAILURE_GUARD");
64 expectedOnErrorMessages.add("Unsupported version for this compiler");
65 expectedOnErrorMessages.add("controlLoop overall timeout is less than the sum of operational policy timeouts.");
67 TestControlLoopCompilerCallback testControlLoopCompilerCallback = new TestControlLoopCompilerCallback(expectedOnErrorMessages);
68 ControlLoopPolicy controlLoopPolicy = this.test("src/test/resources/v1.0.0/test.yaml", testControlLoopCompilerCallback);
69 assertEquals(22, controlLoopPolicy.getPolicies().size());
70 assertTrue(testControlLoopCompilerCallback.areAllExpectedOnErrorsReceived());
74 public void testSuccessConnectedToUnknownPolicy() throws Exception {
75 expectedException.expect(CompilerException.class);
76 expectedException.expectMessage("Operation Policy unique-policy-id-1-restart is connected to unknown policy unknown-policy");
77 this.test("src/test/resources/v1.0.0/bad_policy_success_connected_to_unknown_policy.yaml");
81 public void testFailureConnectedToUnknownPolicy() throws Exception {
82 expectedException.expect(CompilerException.class);
83 expectedException.expectMessage("Operation Policy unique-policy-id-1-restart is connected to unknown policy unknown-policy");
84 this.test("src/test/resources/v1.0.0/bad_policy_failure_connected_to_unknown_policy.yaml");
88 public void testFailureTimeoutToUnknownPolicy() throws Exception {
89 expectedException.expect(CompilerException.class);
90 expectedException.expectMessage("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_timeout_connected_to_unknown_policy.yaml");
95 public void testFailureRetriesToUnknownPolicy() throws Exception {
96 expectedException.expect(CompilerException.class);
97 expectedException.expectMessage("Operation Policy unique-policy-id-1-restart is connected to unknown policy unknown-policy");
98 this.test("src/test/resources/v1.0.0/bad_policy_failure_retries_connected_to_unknown_policy.yaml");
102 public void testFailureExceptionToUnknownPolicy() throws Exception {
103 expectedException.expect(CompilerException.class);
104 expectedException.expectMessage("Operation Policy unique-policy-id-1-restart is connected to unknown policy unknown-policy");
105 this.test("src/test/resources/v1.0.0/bad_policy_failure_exception_connected_to_unknown_policy.yaml");
109 public void testFailureGuardToUnknownPolicy() throws Exception {
110 expectedException.expect(CompilerException.class);
111 expectedException.expectMessage("Operation Policy unique-policy-id-1-restart is connected to unknown policy unknown-policy");
112 this.test("src/test/resources/v1.0.0/bad_policy_failure_guard_connected_to_unknown_policy.yaml");
116 public void testInvalidTriggerPolicyId() throws Exception {
117 expectedException.expect(CompilerException.class);
118 expectedException.expectMessage("Unexpected value for trigger_policy, should only be " + FinalResult.FINAL_OPENLOOP.toString() + " or a valid Policy ID");
119 this.test("src/test/resources/v1.0.0/bad_trigger_1.yaml");
123 public void testNoTriggerPolicyId() throws Exception {
124 expectedException.expect(CompilerException.class);
125 this.test("src/test/resources/v1.0.0/bad_trigger_no_trigger_id.yaml");
129 public void testNoControlLoopName() throws Exception {
130 List<String> expectedOnErrorMessages = new ArrayList<>();
131 expectedOnErrorMessages.add("Missing controlLoopName");
132 expectedOnErrorMessages.add("Unsupported version for this compiler");
133 TestControlLoopCompilerCallback testControlLoopCompilerCallback = new TestControlLoopCompilerCallback(expectedOnErrorMessages);
134 this.test("src/test/resources/v1.0.0/bad_control_loop_no_control_loop_name.yaml", testControlLoopCompilerCallback);
135 assertTrue(testControlLoopCompilerCallback.areAllExpectedOnErrorsReceived());
139 public void testInvalidFinalResult() throws Exception {
140 expectedException.expect(CompilerException.class);
141 expectedException.expectMessage("Unexpected Final Result for trigger_policy, should only be FINAL_OPENLOOP or a valid Policy ID");
142 this.test("src/test/resources/v1.0.0/bad_trigger_2.yaml");
146 public void testCompileEmptyFile() throws Exception {
147 expectedException.expect(CompilerException.class);
148 expectedException.expectMessage("Could not parse yaml specification.");
149 this.test("src/test/resources/v1.0.0/empty.yaml");
152 public ControlLoopPolicy test(String testFile) throws Exception {
153 return test(testFile, null);
156 public ControlLoopPolicy test(String testFile, ControlLoopCompilerCallback controlLoopCompilerCallback) throws Exception {
157 try (InputStream is = new FileInputStream(new File(testFile))) {
158 return ControlLoopCompiler.compile(is, controlLoopCompilerCallback);
159 } catch (FileNotFoundException e) {
160 fail(e.getMessage());
161 } catch (IOException e) {
162 fail(e.getMessage());
163 } catch (Exception e) {
169 class TestControlLoopCompilerCallback implements ControlLoopCompilerCallback{
171 private List<String> expectedOnErrorMessages;
173 public TestControlLoopCompilerCallback(List<String> expectedOnErrorMessages){
174 this.expectedOnErrorMessages = expectedOnErrorMessages;
178 public boolean onWarning(String message) {
183 public boolean onError(String message) {
184 if (!expectedOnErrorMessages.remove(message)){
185 fail("Unexpected onError message: " + message);
190 public boolean areAllExpectedOnErrorsReceived(){
191 return expectedOnErrorMessages.size() == 0;