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