Sonar Fixes policy/models, removing model-yaml
[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 import org.junit.Test;
35 import org.onap.policy.controlloop.policy.ControlLoopPolicy;
36 import org.onap.policy.controlloop.policy.FinalResult;
37
38 public class ControlLoopCompilerTest {
39     private static final String RESTART_UNKNOWN_POLICY =
40         "Operation Policy unique-policy-id-1-restart is connected to unknown policy unknown-policy";
41
42     @Test
43     public void testTest() throws Exception {
44         List<String> expectedOnErrorMessages = new ArrayList<>();
45         expectedOnErrorMessages.add("Operational Policy has an bad ID");
46         expectedOnErrorMessages.add("Policy id is set to a PolicyResult SUCCESS");
47         expectedOnErrorMessages.add("Policy id is set to a FinalResult FINAL_SUCCESS");
48         expectedOnErrorMessages.add("Policy actor is null");
49         expectedOnErrorMessages.add("Policy recipe is null");
50         expectedOnErrorMessages.add("Policy target is null");
51         expectedOnErrorMessages.add("Policy target is invalid");
52         expectedOnErrorMessages.add("Policy success is neither another policy nor FINAL_SUCCESS");
53         expectedOnErrorMessages.add("Policy failure is neither another policy nor FINAL_FAILURE");
54         expectedOnErrorMessages.add("Policy failure retries is neither another policy nor FINAL_FAILURE_RETRIES");
55         expectedOnErrorMessages.add("Policy failure timeout is neither another policy nor FINAL_FAILURE_TIMEOUT");
56         expectedOnErrorMessages.add("Policy failure exception is neither another policy nor FINAL_FAILURE_EXCEPTION");
57         expectedOnErrorMessages.add("Policy failure guard is neither another policy nor FINAL_FAILURE_GUARD");
58         expectedOnErrorMessages.add("Unsupported version for this compiler");
59         expectedOnErrorMessages.add("controlLoop overall timeout is less than the sum of operational policy timeouts.");
60
61         TestControlLoopCompilerCallback testControlLoopCompilerCallback =
62             new TestControlLoopCompilerCallback(expectedOnErrorMessages);
63         ControlLoopPolicy controlLoopPolicy =
64             this.test("src/test/resources/v1.0.0/test.yaml", testControlLoopCompilerCallback);
65         assertEquals(22, controlLoopPolicy.getPolicies().size());
66         assertTrue(testControlLoopCompilerCallback.areAllExpectedOnErrorsReceived());
67     }
68
69     @Test
70     public void testSuccessConnectedToUnknownPolicy() throws Exception {
71         assertThatThrownBy(() -> {
72             this.test("src/test/resources/v1.0.0/bad_policy_success_connected_to_unknown_policy.yaml");
73         }).isInstanceOf(CompilerException.class).hasMessage(RESTART_UNKNOWN_POLICY);
74     }
75
76     @Test
77     public void testFailureConnectedToUnknownPolicy() throws Exception {
78         assertThatThrownBy(() -> {
79             this.test("src/test/resources/v1.0.0/bad_policy_failure_connected_to_unknown_policy.yaml");
80         }).isInstanceOf(CompilerException.class).hasMessage(RESTART_UNKNOWN_POLICY);
81     }
82
83     @Test
84     public void testFailureTimeoutToUnknownPolicy() throws Exception {
85         assertThatThrownBy(() -> {
86             this.test("src/test/resources/v1.0.0/bad_policy_failure_timeout_connected_to_unknown_policy.yaml");
87         }).isInstanceOf(CompilerException.class).hasMessage(RESTART_UNKNOWN_POLICY);
88     }
89
90     @Test
91     public void testFailureRetriesToUnknownPolicy() throws Exception {
92         assertThatThrownBy(() -> {
93             this.test("src/test/resources/v1.0.0/bad_policy_failure_retries_connected_to_unknown_policy.yaml");
94         }).isInstanceOf(CompilerException.class).hasMessage(RESTART_UNKNOWN_POLICY);
95     }
96
97     @Test
98     public void testFailureExceptionToUnknownPolicy() throws Exception {
99         assertThatThrownBy(() -> {
100             this.test("src/test/resources/v1.0.0/bad_policy_failure_exception_connected_to_unknown_policy.yaml");
101         }).isInstanceOf(CompilerException.class).hasMessage(RESTART_UNKNOWN_POLICY);
102     }
103
104     @Test
105     public void testFailureGuardToUnknownPolicy() throws Exception {
106         assertThatThrownBy(() -> {
107             this.test("src/test/resources/v1.0.0/bad_policy_failure_guard_connected_to_unknown_policy.yaml");
108         }).isInstanceOf(CompilerException.class).hasMessage(RESTART_UNKNOWN_POLICY);
109     }
110
111     @Test
112     public void testInvalidTriggerPolicyId() throws Exception {
113         assertThatThrownBy(() -> {
114             this.test("src/test/resources/v1.0.0/bad_trigger_1.yaml");
115         }).isInstanceOf(CompilerException.class).hasMessage("Unexpected value for trigger_policy, should only be "
116             + FinalResult.FINAL_OPENLOOP.toString() + " or a valid Policy ID");
117     }
118
119     @Test
120     public void testNoTriggerPolicyId() throws Exception {
121         assertThatThrownBy(() -> {
122             this.test("src/test/resources/v1.0.0/bad_trigger_no_trigger_id.yaml");
123         }).isInstanceOf(CompilerException.class);
124     }
125
126     @Test
127     public void testNoControlLoopName() throws Exception {
128         List<String> expectedOnErrorMessages = new ArrayList<>();
129         expectedOnErrorMessages.add("Missing controlLoopName");
130         expectedOnErrorMessages.add("Unsupported version for this compiler");
131         TestControlLoopCompilerCallback testControlLoopCompilerCallback =
132             new TestControlLoopCompilerCallback(expectedOnErrorMessages);
133         this.test("src/test/resources/v1.0.0/bad_control_loop_no_control_loop_name.yaml",
134             testControlLoopCompilerCallback);
135         assertTrue(testControlLoopCompilerCallback.areAllExpectedOnErrorsReceived());
136     }
137
138     @Test
139     public void testInvalidFinalResult() throws Exception {
140         assertThatThrownBy(() -> {
141             this.test("src/test/resources/v1.0.0/bad_trigger_2.yaml");
142         }).isInstanceOf(CompilerException.class).hasMessage(
143             "Unexpected Final Result for trigger_policy, should only be FINAL_OPENLOOP or a valid Policy ID");
144     }
145
146     @Test
147     public void testCompileEmptyFile() throws Exception {
148         assertThatThrownBy(() -> {
149             this.test("src/test/resources/v1.0.0/empty.yaml");
150         }).isInstanceOf(CompilerException.class).hasMessage("Could not parse yaml specification.");
151     }
152
153     public ControlLoopPolicy test(String testFile) throws Exception {
154         return test(testFile, null);
155     }
156
157     /**
158      * Does the actual test.
159      *
160      * @param testFile test file
161      * @param controlLoopCompilerCallback callback method
162      * @return the policy object
163      * @throws Exception exception
164      */
165     public ControlLoopPolicy test(String testFile, ControlLoopCompilerCallback controlLoopCompilerCallback)
166         throws Exception {
167         try (InputStream is = new FileInputStream(new File(testFile))) {
168             return ControlLoopCompiler.compile(is, controlLoopCompilerCallback);
169         }
170     }
171
172     class TestControlLoopCompilerCallback implements ControlLoopCompilerCallback {
173
174         private List<String> expectedOnErrorMessages;
175
176         public TestControlLoopCompilerCallback(List<String> expectedOnErrorMessages) {
177             this.expectedOnErrorMessages = expectedOnErrorMessages;
178         }
179
180         @Override
181         public boolean onWarning(String message) {
182             return true;
183         }
184
185         @Override
186         public boolean onError(String message) {
187             if (!expectedOnErrorMessages.remove(message)) {
188                 fail("Unexpected onError message: " + message);
189             }
190             return true;
191         }
192
193         public boolean areAllExpectedOnErrorsReceived() {
194             return expectedOnErrorMessages.isEmpty();
195         }
196     }
197 }