048aef270c5eed1dde3127c226f4bc545adb4c15
[policy/drools-applications.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * policy-yaml unit test
4  * ================================================================================
5  * Copyright (C) 2017 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 = new TestControlLoopCompilerCallback(expectedOnErrorMessages);
68                 ControlLoopPolicy controlLoopPolicy = this.test("src/test/resources/v1.0.0/test.yaml", testControlLoopCompilerCallback);
69                 assertEquals(22, controlLoopPolicy.getPolicies().size());
70                 assertTrue(testControlLoopCompilerCallback.areAllExpectedOnErrorsReceived());
71         }
72         
73         @Test
74         public void testSuccessConnectedToUnknownPolicy() throws Exception {
75                 expectedException.expect(CompilerException.class);
76                 expectedException.expectMessage("Operation Policy unique-policy-id-1-restart is connected to unknown policy 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("Operation Policy unique-policy-id-1-restart is connected to unknown policy unknown-policy");
84                 this.test("src/test/resources/v1.0.0/bad_policy_failure_connected_to_unknown_policy.yaml");     
85         }
86         
87         @Test
88         public void testFailureTimeoutToUnknownPolicy() throws Exception {
89                 expectedException.expect(CompilerException.class);
90                 expectedException.expectMessage("Operation Policy unique-policy-id-1-restart is connected to unknown policy unknown-policy");
91                 this.test("src/test/resources/v1.0.0/bad_policy_failure_timeout_connected_to_unknown_policy.yaml");     
92         }
93         
94         @Test
95         public void testFailureRetriesToUnknownPolicy() throws Exception {
96                 expectedException.expect(CompilerException.class);
97                 expectedException.expectMessage("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_retries_connected_to_unknown_policy.yaml");     
99         }
100         
101         @Test
102         public void testFailureExceptionToUnknownPolicy() throws Exception {
103                 expectedException.expect(CompilerException.class);
104                 expectedException.expectMessage("Operation Policy unique-policy-id-1-restart is connected to unknown policy unknown-policy");
105                 this.test("src/test/resources/v1.0.0/bad_policy_failure_exception_connected_to_unknown_policy.yaml");   
106         }
107         
108         @Test
109         public void testFailureGuardToUnknownPolicy() throws Exception {
110                 expectedException.expect(CompilerException.class);
111                 expectedException.expectMessage("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_guard_connected_to_unknown_policy.yaml");       
113         }
114         
115         @Test 
116         public void testInvalidTriggerPolicyId() throws Exception {
117                 expectedException.expect(CompilerException.class);
118                 expectedException.expectMessage("Unexpected value for trigger_policy, should only be " + FinalResult.FINAL_OPENLOOP.toString() + " or a valid Policy ID");
119                 this.test("src/test/resources/v1.0.0/bad_trigger_1.yaml");
120         }
121         
122         @Test 
123         public void testNoTriggerPolicyId() throws Exception {
124                 expectedException.expect(CompilerException.class);
125                 this.test("src/test/resources/v1.0.0/bad_trigger_no_trigger_id.yaml");
126         }
127         
128         @Test 
129         public void testNoControlLoopName() throws Exception {
130                 List<String> expectedOnErrorMessages = new ArrayList<>();
131         expectedOnErrorMessages.add("Missing controlLoopName");
132         expectedOnErrorMessages.add("Unsupported version for this compiler");
133         TestControlLoopCompilerCallback testControlLoopCompilerCallback = new TestControlLoopCompilerCallback(expectedOnErrorMessages);
134                 this.test("src/test/resources/v1.0.0/bad_control_loop_no_control_loop_name.yaml", testControlLoopCompilerCallback);
135             assertTrue(testControlLoopCompilerCallback.areAllExpectedOnErrorsReceived());
136         }
137         
138         @Test 
139         public void testInvalidFinalResult() throws Exception {
140                 expectedException.expect(CompilerException.class);
141                 expectedException.expectMessage("Unexpected Final Result for trigger_policy, should only be FINAL_OPENLOOP or a valid Policy ID");
142                 this.test("src/test/resources/v1.0.0/bad_trigger_2.yaml");
143         }
144         
145         @Test 
146         public void testCompileEmptyFile() throws Exception {
147                 expectedException.expect(CompilerException.class);
148                 expectedException.expectMessage("Could not parse yaml specification.");
149                 this.test("src/test/resources/v1.0.0/empty.yaml");
150         }
151         
152         public ControlLoopPolicy test(String testFile) throws Exception {
153                 return test(testFile, null);
154         }
155         
156         public ControlLoopPolicy test(String testFile, ControlLoopCompilerCallback controlLoopCompilerCallback) throws Exception {
157                 try (InputStream is = new FileInputStream(new File(testFile))) {
158                         return ControlLoopCompiler.compile(is, controlLoopCompilerCallback);
159                 } catch (FileNotFoundException e) {
160                         fail(e.getMessage());
161                 } catch (IOException e) {
162                         fail(e.getMessage());
163                 } catch (Exception e) {
164                         throw e;
165                 }
166                 return null;
167         }
168         
169         class TestControlLoopCompilerCallback implements ControlLoopCompilerCallback{
170                 
171                 private List<String> expectedOnErrorMessages;
172                 
173                 public TestControlLoopCompilerCallback(List<String> expectedOnErrorMessages){
174                         this.expectedOnErrorMessages = expectedOnErrorMessages;
175                 }
176
177                 @Override
178                 public boolean onWarning(String message) {
179                         return true;
180                 }
181
182                 @Override
183                 public boolean onError(String message) {
184                         if (!expectedOnErrorMessages.remove(message)){
185                             fail("Unexpected onError message: " + message);
186                         }
187                         return true;
188                 }
189                 
190                 public boolean areAllExpectedOnErrorsReceived(){
191                         return expectedOnErrorMessages.size() == 0;
192                 }
193                 
194         };
195
196 }
197
198