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