6603dcb82b3cec2ed636089699075c530d54a4c4
[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 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.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertTrue;
26 import static org.junit.Assert.fail;
27
28 import java.io.File;
29 import java.io.FileInputStream;
30 import java.io.InputStream;
31 import java.util.ArrayList;
32 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;
38
39 public class ControlLoopCompilerTest {
40
41     private static final String POLICY_RECIPE_IS_INVALID = "Policy recipe is invalid";
42     private static final String RESTART_UNKNOWN_POLICY =
43                     "Operation Policy unique-policy-id-1-restart is connected to unknown policy unknown-policy";
44     @Rule
45     public ExpectedException expectedException = ExpectedException.none();
46
47     @Test
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.");
69
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());
76     }
77
78     @Test
79     public void testSuccessConnectedToUnknownPolicy() throws Exception {
80         expectedException.expect(CompilerException.class);
81         expectedException.expectMessage(
82                         RESTART_UNKNOWN_POLICY);
83         this.test("src/test/resources/v1.0.0/bad_policy_success_connected_to_unknown_policy.yaml");
84     }
85
86     @Test
87     public void testFailureConnectedToUnknownPolicy() throws Exception {
88         expectedException.expect(CompilerException.class);
89         expectedException.expectMessage(
90                         RESTART_UNKNOWN_POLICY);
91         this.test("src/test/resources/v1.0.0/bad_policy_failure_connected_to_unknown_policy.yaml");
92     }
93
94     @Test
95     public void testFailureTimeoutToUnknownPolicy() throws Exception {
96         expectedException.expect(CompilerException.class);
97         expectedException.expectMessage(
98                         RESTART_UNKNOWN_POLICY);
99         this.test("src/test/resources/v1.0.0/bad_policy_failure_timeout_connected_to_unknown_policy.yaml");
100     }
101
102     @Test
103     public void testFailureRetriesToUnknownPolicy() throws Exception {
104         expectedException.expect(CompilerException.class);
105         expectedException.expectMessage(
106                         RESTART_UNKNOWN_POLICY);
107         this.test("src/test/resources/v1.0.0/bad_policy_failure_retries_connected_to_unknown_policy.yaml");
108     }
109
110     @Test
111     public void testFailureExceptionToUnknownPolicy() throws Exception {
112         expectedException.expect(CompilerException.class);
113         expectedException.expectMessage(
114                         RESTART_UNKNOWN_POLICY);
115         this.test("src/test/resources/v1.0.0/bad_policy_failure_exception_connected_to_unknown_policy.yaml");
116     }
117
118     @Test
119     public void testFailureGuardToUnknownPolicy() throws Exception {
120         expectedException.expect(CompilerException.class);
121         expectedException.expectMessage(
122                         RESTART_UNKNOWN_POLICY);
123         this.test("src/test/resources/v1.0.0/bad_policy_failure_guard_connected_to_unknown_policy.yaml");
124     }
125
126     @Test
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");
133     }
134
135     @Test
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");
139     }
140
141     @Test
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());
151     }
152
153     @Test
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");
159     }
160
161     @Test
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");
166     }
167
168     public ControlLoopPolicy test(String testFile) throws Exception {
169         return test(testFile, null);
170     }
171
172     /**
173      * Does the actual test.
174      *
175      * @param testFile test file
176      * @param controlLoopCompilerCallback callback method
177      * @return the policy object
178      * @throws Exception exception
179      */
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         }
185     }
186
187     class TestControlLoopCompilerCallback implements ControlLoopCompilerCallback {
188
189         private List<String> expectedOnErrorMessages;
190
191         public TestControlLoopCompilerCallback(List<String> expectedOnErrorMessages) {
192             this.expectedOnErrorMessages = expectedOnErrorMessages;
193         }
194
195         @Override
196         public boolean onWarning(String message) {
197             return true;
198         }
199
200         @Override
201         public boolean onError(String message) {
202             if (!expectedOnErrorMessages.remove(message)) {
203                 fail("Unexpected onError message: " + message);
204             }
205             return true;
206         }
207
208         public boolean areAllExpectedOnErrorsReceived() {
209             return expectedOnErrorMessages.isEmpty();
210         }
211
212     }
213
214 }