b6934386262fcd60042fcec8711800c54b519f20
[policy/drools-applications.git] /
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.*;
24
25 import java.io.File;
26 import java.io.FileInputStream;
27 import java.io.FileNotFoundException;
28 import java.io.IOException;
29 import java.io.InputStream;
30 import java.util.ArrayList;
31 import java.util.List;
32
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     @Rule
42     public ExpectedException expectedException = ExpectedException.none();
43
44     @Test 
45     public void testTest() throws Exception {
46         List<String> expectedOnErrorMessages = new ArrayList<>();
47         expectedOnErrorMessages.add("Operational Policy has an bad ID");
48         expectedOnErrorMessages.add("Policy id is set to a PolicyResult SUCCESS");
49         expectedOnErrorMessages.add("Policy id is set to a FinalResult FINAL_SUCCESS");
50         expectedOnErrorMessages.add("Policy actor is null");
51         expectedOnErrorMessages.add("Policy actor is invalid");
52         expectedOnErrorMessages.add("Policy recipe is null");
53         expectedOnErrorMessages.add("Policy recipe is invalid");
54         expectedOnErrorMessages.add("Policy recipe is invalid");
55         expectedOnErrorMessages.add("Policy recipe is invalid");
56         expectedOnErrorMessages.add("Policy target is null");
57         expectedOnErrorMessages.add("Policy target is invalid");
58         expectedOnErrorMessages.add("Policy success is neither another policy nor FINAL_SUCCESS");
59         expectedOnErrorMessages.add("Policy failure is neither another policy nor FINAL_FAILURE");
60         expectedOnErrorMessages.add("Policy failure retries is neither another policy nor FINAL_FAILURE_RETRIES");
61         expectedOnErrorMessages.add("Policy failure timeout is neither another policy nor FINAL_FAILURE_TIMEOUT");
62         expectedOnErrorMessages.add("Policy failure exception is neither another policy nor FINAL_FAILURE_EXCEPTION");
63         expectedOnErrorMessages.add("Policy failure guard is neither another policy nor FINAL_FAILURE_GUARD");
64         expectedOnErrorMessages.add("Unsupported version for this compiler");
65         expectedOnErrorMessages.add("controlLoop overall timeout is less than the sum of operational policy timeouts.");
66
67         TestControlLoopCompilerCallback testControlLoopCompilerCallback = 
68                         new TestControlLoopCompilerCallback(expectedOnErrorMessages);
69         ControlLoopPolicy controlLoopPolicy = this.test("src/test/resources/v1.0.0/test.yaml", 
70                         testControlLoopCompilerCallback);
71         assertEquals(22, controlLoopPolicy.getPolicies().size());
72         assertTrue(testControlLoopCompilerCallback.areAllExpectedOnErrorsReceived());
73     }
74
75     @Test
76     public void testSuccessConnectedToUnknownPolicy() throws Exception {
77         expectedException.expect(CompilerException.class);
78         expectedException.expectMessage(
79                         "Operation Policy unique-policy-id-1-restart is connected to unknown policy unknown-policy");
80         this.test("src/test/resources/v1.0.0/bad_policy_success_connected_to_unknown_policy.yaml");
81     }
82
83     @Test
84     public void testFailureConnectedToUnknownPolicy() throws Exception {
85         expectedException.expect(CompilerException.class);
86         expectedException.expectMessage(
87                         "Operation Policy unique-policy-id-1-restart is connected to unknown policy unknown-policy");
88         this.test("src/test/resources/v1.0.0/bad_policy_failure_connected_to_unknown_policy.yaml");
89     }
90
91     @Test
92     public void testFailureTimeoutToUnknownPolicy() throws Exception {
93         expectedException.expect(CompilerException.class);
94         expectedException.expectMessage(
95                         "Operation Policy unique-policy-id-1-restart is connected to unknown policy unknown-policy");
96         this.test("src/test/resources/v1.0.0/bad_policy_failure_timeout_connected_to_unknown_policy.yaml");
97     }
98
99     @Test
100     public void testFailureRetriesToUnknownPolicy() throws Exception {
101         expectedException.expect(CompilerException.class);
102         expectedException.expectMessage(
103                         "Operation Policy unique-policy-id-1-restart is connected to unknown policy unknown-policy");
104         this.test("src/test/resources/v1.0.0/bad_policy_failure_retries_connected_to_unknown_policy.yaml");
105     }
106
107     @Test
108     public void testFailureExceptionToUnknownPolicy() throws Exception {
109         expectedException.expect(CompilerException.class);
110         expectedException.expectMessage(
111                         "Operation Policy unique-policy-id-1-restart is connected to unknown policy unknown-policy");
112         this.test("src/test/resources/v1.0.0/bad_policy_failure_exception_connected_to_unknown_policy.yaml");
113     }
114
115     @Test
116     public void testFailureGuardToUnknownPolicy() throws Exception {
117         expectedException.expect(CompilerException.class);
118         expectedException.expectMessage(
119                         "Operation Policy unique-policy-id-1-restart is connected to unknown policy unknown-policy");
120         this.test("src/test/resources/v1.0.0/bad_policy_failure_guard_connected_to_unknown_policy.yaml");
121     }
122
123     @Test 
124     public void testInvalidTriggerPolicyId() throws Exception {
125         expectedException.expect(CompilerException.class);
126         expectedException.expectMessage(
127                         "Unexpected value for trigger_policy, should only be " 
128                         + FinalResult.FINAL_OPENLOOP.toString() + " or a valid Policy ID");
129         this.test("src/test/resources/v1.0.0/bad_trigger_1.yaml");
130     }
131
132     @Test 
133     public void testNoTriggerPolicyId() throws Exception {
134         expectedException.expect(CompilerException.class);
135         this.test("src/test/resources/v1.0.0/bad_trigger_no_trigger_id.yaml");
136     }
137
138     @Test 
139     public void testNoControlLoopName() throws Exception {
140         List<String> expectedOnErrorMessages = new ArrayList<>();
141         expectedOnErrorMessages.add("Missing controlLoopName");
142         expectedOnErrorMessages.add("Unsupported version for this compiler");
143         TestControlLoopCompilerCallback testControlLoopCompilerCallback = 
144                         new TestControlLoopCompilerCallback(expectedOnErrorMessages);
145         this.test("src/test/resources/v1.0.0/bad_control_loop_no_control_loop_name.yaml", 
146                         testControlLoopCompilerCallback);
147         assertTrue(testControlLoopCompilerCallback.areAllExpectedOnErrorsReceived());
148     }
149
150     @Test 
151     public void testInvalidFinalResult() throws Exception {
152         expectedException.expect(CompilerException.class);
153         expectedException.expectMessage(
154                      "Unexpected Final Result for trigger_policy, should only be FINAL_OPENLOOP or a valid Policy ID");
155         this.test("src/test/resources/v1.0.0/bad_trigger_2.yaml");
156     }
157
158     @Test 
159     public void testCompileEmptyFile() throws Exception {
160         expectedException.expect(CompilerException.class);
161         expectedException.expectMessage("Could not parse yaml specification.");
162         this.test("src/test/resources/v1.0.0/empty.yaml");
163     }
164
165     public ControlLoopPolicy test(String testFile) throws Exception {
166         return test(testFile, null);
167     }
168
169     public ControlLoopPolicy test(String testFile, 
170                     ControlLoopCompilerCallback controlLoopCompilerCallback) throws Exception {
171         try (InputStream is = new FileInputStream(new File(testFile))) {
172             return ControlLoopCompiler.compile(is, controlLoopCompilerCallback);
173         } catch (FileNotFoundException e) {
174             fail(e.getMessage());
175         } catch (IOException e) {
176             fail(e.getMessage());
177         } catch (Exception e) {
178             throw e;
179         }
180         return null;
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.size() == 0;
206         }
207
208     }
209
210 }