Merge "Add debugging of REST call"
[policy/drools-applications.git] / controlloop / common / policy-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-2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.controlloop.compiler;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertTrue;
25 import static org.junit.Assert.fail;
26
27 import java.io.File;
28 import java.io.FileInputStream;
29 import java.io.FileNotFoundException;
30 import java.io.IOException;
31 import java.io.InputStream;
32 import java.util.ArrayList;
33 import java.util.List;
34
35 import org.junit.Rule;
36 import org.junit.Test;
37 import org.junit.rules.ExpectedException;
38 import org.onap.policy.controlloop.policy.ControlLoopPolicy;
39 import org.onap.policy.controlloop.policy.FinalResult;
40
41 public class ControlLoopCompilerTest {
42
43     @Rule
44     public ExpectedException expectedException = ExpectedException.none();
45
46     @Test 
47     public void testTest() throws Exception {
48         List<String> expectedOnErrorMessages = new ArrayList<>();
49         expectedOnErrorMessages.add("Operational Policy has an bad ID");
50         expectedOnErrorMessages.add("Policy id is set to a PolicyResult SUCCESS");
51         expectedOnErrorMessages.add("Policy id is set to a FinalResult FINAL_SUCCESS");
52         expectedOnErrorMessages.add("Policy actor is null");
53         expectedOnErrorMessages.add("Policy actor is invalid");
54         expectedOnErrorMessages.add("Policy recipe is null");
55         expectedOnErrorMessages.add("Policy recipe is invalid");
56         expectedOnErrorMessages.add("Policy recipe is invalid");
57         expectedOnErrorMessages.add("Policy recipe is invalid");
58         expectedOnErrorMessages.add("Policy target is null");
59         expectedOnErrorMessages.add("Policy target is invalid");
60         expectedOnErrorMessages.add("Policy success is neither another policy nor FINAL_SUCCESS");
61         expectedOnErrorMessages.add("Policy failure is neither another policy nor FINAL_FAILURE");
62         expectedOnErrorMessages.add("Policy failure retries is neither another policy nor FINAL_FAILURE_RETRIES");
63         expectedOnErrorMessages.add("Policy failure timeout is neither another policy nor FINAL_FAILURE_TIMEOUT");
64         expectedOnErrorMessages.add("Policy failure exception is neither another policy nor FINAL_FAILURE_EXCEPTION");
65         expectedOnErrorMessages.add("Policy failure guard is neither another policy nor FINAL_FAILURE_GUARD");
66         expectedOnErrorMessages.add("Unsupported version for this compiler");
67         expectedOnErrorMessages.add("controlLoop overall timeout is less than the sum of operational policy timeouts.");
68
69         TestControlLoopCompilerCallback testControlLoopCompilerCallback = 
70                         new TestControlLoopCompilerCallback(expectedOnErrorMessages);
71         ControlLoopPolicy controlLoopPolicy = this.test("src/test/resources/v1.0.0/test.yaml", 
72                         testControlLoopCompilerCallback);
73         assertEquals(22, controlLoopPolicy.getPolicies().size());
74         assertTrue(testControlLoopCompilerCallback.areAllExpectedOnErrorsReceived());
75     }
76
77     @Test
78     public void testSuccessConnectedToUnknownPolicy() throws Exception {
79         expectedException.expect(CompilerException.class);
80         expectedException.expectMessage(
81                         "Operation Policy unique-policy-id-1-restart is connected to unknown policy unknown-policy");
82         this.test("src/test/resources/v1.0.0/bad_policy_success_connected_to_unknown_policy.yaml");
83     }
84
85     @Test
86     public void testFailureConnectedToUnknownPolicy() throws Exception {
87         expectedException.expect(CompilerException.class);
88         expectedException.expectMessage(
89                         "Operation Policy unique-policy-id-1-restart is connected to unknown policy unknown-policy");
90         this.test("src/test/resources/v1.0.0/bad_policy_failure_connected_to_unknown_policy.yaml");
91     }
92
93     @Test
94     public void testFailureTimeoutToUnknownPolicy() throws Exception {
95         expectedException.expect(CompilerException.class);
96         expectedException.expectMessage(
97                         "Operation Policy unique-policy-id-1-restart is connected to unknown policy unknown-policy");
98         this.test("src/test/resources/v1.0.0/bad_policy_failure_timeout_connected_to_unknown_policy.yaml");
99     }
100
101     @Test
102     public void testFailureRetriesToUnknownPolicy() throws Exception {
103         expectedException.expect(CompilerException.class);
104         expectedException.expectMessage(
105                         "Operation Policy unique-policy-id-1-restart is connected to unknown policy unknown-policy");
106         this.test("src/test/resources/v1.0.0/bad_policy_failure_retries_connected_to_unknown_policy.yaml");
107     }
108
109     @Test
110     public void testFailureExceptionToUnknownPolicy() throws Exception {
111         expectedException.expect(CompilerException.class);
112         expectedException.expectMessage(
113                         "Operation Policy unique-policy-id-1-restart is connected to unknown policy unknown-policy");
114         this.test("src/test/resources/v1.0.0/bad_policy_failure_exception_connected_to_unknown_policy.yaml");
115     }
116
117     @Test
118     public void testFailureGuardToUnknownPolicy() throws Exception {
119         expectedException.expect(CompilerException.class);
120         expectedException.expectMessage(
121                         "Operation Policy unique-policy-id-1-restart is connected to unknown policy unknown-policy");
122         this.test("src/test/resources/v1.0.0/bad_policy_failure_guard_connected_to_unknown_policy.yaml");
123     }
124
125     @Test 
126     public void testInvalidTriggerPolicyId() throws Exception {
127         expectedException.expect(CompilerException.class);
128         expectedException.expectMessage(
129                         "Unexpected value for trigger_policy, should only be " 
130                         + FinalResult.FINAL_OPENLOOP.toString() + " or a valid Policy ID");
131         this.test("src/test/resources/v1.0.0/bad_trigger_1.yaml");
132     }
133
134     @Test 
135     public void testNoTriggerPolicyId() throws Exception {
136         expectedException.expect(CompilerException.class);
137         this.test("src/test/resources/v1.0.0/bad_trigger_no_trigger_id.yaml");
138     }
139
140     @Test 
141     public void testNoControlLoopName() throws Exception {
142         List<String> expectedOnErrorMessages = new ArrayList<>();
143         expectedOnErrorMessages.add("Missing controlLoopName");
144         expectedOnErrorMessages.add("Unsupported version for this compiler");
145         TestControlLoopCompilerCallback testControlLoopCompilerCallback = 
146                         new TestControlLoopCompilerCallback(expectedOnErrorMessages);
147         this.test("src/test/resources/v1.0.0/bad_control_loop_no_control_loop_name.yaml", 
148                         testControlLoopCompilerCallback);
149         assertTrue(testControlLoopCompilerCallback.areAllExpectedOnErrorsReceived());
150     }
151
152     @Test 
153     public void testInvalidFinalResult() throws Exception {
154         expectedException.expect(CompilerException.class);
155         expectedException.expectMessage(
156                      "Unexpected Final Result for trigger_policy, should only be FINAL_OPENLOOP or a valid Policy ID");
157         this.test("src/test/resources/v1.0.0/bad_trigger_2.yaml");
158     }
159
160     @Test 
161     public void testCompileEmptyFile() throws Exception {
162         expectedException.expect(CompilerException.class);
163         expectedException.expectMessage("Could not parse yaml specification.");
164         this.test("src/test/resources/v1.0.0/empty.yaml");
165     }
166
167     public ControlLoopPolicy test(String testFile) throws Exception {
168         return test(testFile, null);
169     }
170
171     /**
172      * Does the actual test.
173      * 
174      * @param testFile test file
175      * @param controlLoopCompilerCallback callback method
176      * @return the policy object
177      * @throws Exception exception
178      */
179     public ControlLoopPolicy test(String testFile, 
180                     ControlLoopCompilerCallback controlLoopCompilerCallback) throws Exception {
181         try (InputStream is = new FileInputStream(new File(testFile))) {
182             return ControlLoopCompiler.compile(is, controlLoopCompilerCallback);
183         } catch (FileNotFoundException e) {
184             fail(e.getMessage());
185         } catch (IOException e) {
186             fail(e.getMessage());
187         } catch (Exception e) {
188             throw e;
189         }
190         return null;
191     }
192
193     class TestControlLoopCompilerCallback implements ControlLoopCompilerCallback {
194
195         private List<String> expectedOnErrorMessages;
196
197         public TestControlLoopCompilerCallback(List<String> expectedOnErrorMessages) {
198             this.expectedOnErrorMessages = expectedOnErrorMessages;
199         }
200
201         @Override
202         public boolean onWarning(String message) {
203             return true;
204         }
205
206         @Override
207         public boolean onError(String message) {
208             if (!expectedOnErrorMessages.remove(message)) {
209                 fail("Unexpected onError message: " + message);
210             }
211             return true;
212         }
213
214         public boolean areAllExpectedOnErrorsReceived() {
215             return expectedOnErrorMessages.size() == 0;
216         }
217
218     }
219
220 }