98c00eb2f932ceab756b15fb264c7f189b6e6243
[policy/models.git] / models-interactions / model-yaml / src / test / java / org / onap / policy / controlloop / compiler / ControlLoopCompilerTest.java
1 /*-
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
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
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=========================================================
20  */
21
22 package org.onap.policy.controlloop.compiler;
23
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;
28
29 import java.io.File;
30 import java.io.FileInputStream;
31 import java.io.InputStream;
32 import java.util.ArrayList;
33 import java.util.List;
34
35 import org.junit.Test;
36 import org.onap.policy.controlloop.policy.ControlLoopPolicy;
37 import org.onap.policy.controlloop.policy.FinalResult;
38
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";
42
43     @Test
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.");
61
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());
68     }
69
70     @Test
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);
75     }
76
77     @Test
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);
82     }
83
84     @Test
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);
89     }
90
91     @Test
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);
96     }
97
98     @Test
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);
103     }
104
105     @Test
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);
110     }
111
112     @Test
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");
118     }
119
120     @Test
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);
125     }
126
127     @Test
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());
137     }
138
139     @Test
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");
145     }
146
147     @Test
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.");
152     }
153
154     public ControlLoopPolicy test(String testFile) throws Exception {
155         return test(testFile, null);
156     }
157
158     /**
159      * Does the actual test.
160      *
161      * @param testFile test file
162      * @param controlLoopCompilerCallback callback method
163      * @return the policy object
164      * @throws Exception exception
165      */
166     public ControlLoopPolicy test(String testFile, ControlLoopCompilerCallback controlLoopCompilerCallback)
167         throws Exception {
168         try (InputStream is = new FileInputStream(new File(testFile))) {
169             return ControlLoopCompiler.compile(is, controlLoopCompilerCallback);
170         }
171     }
172
173     class TestControlLoopCompilerCallback implements ControlLoopCompilerCallback {
174
175         private List<String> expectedOnErrorMessages;
176
177         public TestControlLoopCompilerCallback(List<String> expectedOnErrorMessages) {
178             this.expectedOnErrorMessages = expectedOnErrorMessages;
179         }
180
181         @Override
182         public boolean onWarning(String message) {
183             return true;
184         }
185
186         @Override
187         public boolean onError(String message) {
188             if (!expectedOnErrorMessages.remove(message)) {
189                 fail("Unexpected onError message: " + message);
190             }
191             return true;
192         }
193
194         public boolean areAllExpectedOnErrorsReceived() {
195             return expectedOnErrorMessages.isEmpty();
196         }
197     }
198 }