1426865c6af76914ca1c3f9d21b04b961f75165b
[policy/drools-applications.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP
4  * ================================================================================
5  * Copyright (C) 2020 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.tdjam;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertFalse;
25 import static org.junit.Assert.assertNull;
26 import static org.junit.Assert.assertSame;
27 import static org.junit.Assert.assertTrue;
28 import static org.junit.Assert.fail;
29 import static org.onap.policy.drools.properties.DroolsPropertyConstants.PROPERTY_CONTROLLER_TYPE;
30
31 import ch.qos.logback.classic.Level;
32 import ch.qos.logback.classic.Logger;
33 import ch.qos.logback.classic.spi.ILoggingEvent;
34 import ch.qos.logback.core.read.ListAppender;
35 import java.util.HashSet;
36 import java.util.Properties;
37 import java.util.UUID;
38 import org.junit.AfterClass;
39 import org.junit.BeforeClass;
40 import org.junit.Test;
41 import org.onap.policy.controlloop.CanonicalOnset;
42 import org.onap.policy.controlloop.drl.legacy.ControlLoopParams;
43 import org.onap.policy.drools.controller.DroolsControllerConstants;
44 import org.onap.policy.drools.system.PolicyControllerConstants;
45 import org.onap.policy.drools.system.PolicyEngineConstants;
46 import org.onap.policy.drools.utils.PropertyUtil;
47 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
48 import org.powermock.reflect.Whitebox;
49 import org.slf4j.LoggerFactory;
50
51 public class TdjamControllerTest {
52     private static Properties prop;
53     private static Logger logger = (Logger) LoggerFactory.getLogger(TdjamController.class);
54     private static ListAppender<ILoggingEvent> appender = new ListAppender<ILoggingEvent>();
55
56     /**
57      * Setup appender, and initialize properties.
58      */
59     @BeforeClass
60     public static void setupClass() throws Exception {
61         logger.setLevel(Level.DEBUG);
62         logger.addAppender(appender);
63
64         prop = PropertyUtil.getProperties("src/test/resources/config/tdjam-controller.properties");
65         prop.setProperty(PROPERTY_CONTROLLER_TYPE, "tdjam");
66
67         PolicyEngineConstants.getManager().configure(new Properties());
68         PolicyEngineConstants.getManager().start();
69
70     }
71
72     /**
73      * Remove appender.
74      */
75     @AfterClass
76     public static void cleanupClass() {
77
78         PolicyEngineConstants.getManager().stop();
79         PolicyEngineConstants.getManager().getExecutorService().shutdown();
80
81         appender.stop();
82         System.out.println("APPENDER:");
83         for (ILoggingEvent event : appender.list) {
84             System.out.println("    " + event);
85         }
86         logger.detachAppender(appender);
87     }
88
89     @Test
90     public void toscaPolicyTests() {
91         TdjamController tc = (TdjamController) PolicyControllerConstants.getFactory().build("tc", prop);
92         assertTrue(PolicyControllerConstants.getFactory().inventory().contains(tc));
93         assertTrue(DroolsControllerConstants.getFactory().inventory().contains(tc));
94
95         final HashSet<ToscaPolicy> toscaPolicies = new HashSet<>();
96         final HashSet<ControlLoopParams> controlLoopParams = new HashSet<>();
97
98         ToscaPolicy a1 = buildToscaPolicy("a", "1", tc);
99         ToscaPolicy a2 = buildToscaPolicy("a", "2", tc);
100         ToscaPolicy b1 = buildToscaPolicy("b", "1", tc);
101
102         toscaPolicies.add(a1);
103         toscaPolicies.add(a2);
104         toscaPolicies.add(b1);
105
106         assertSame(a1, tc.getToscaPolicy("a", "1"));
107         assertSame(a2, tc.getToscaPolicy("a", "2"));
108         assertSame(b1, tc.getToscaPolicy("b", "1"));
109         assertEquals(toscaPolicies, tc.getAllToscaPolicies());
110
111         // create associated ControlLoopParams
112         final ControlLoopParams clpa1 = buildControlLoopParams("a", "1", "clpa1", tc);
113         final ControlLoopParams clpa2 = buildControlLoopParams("a", "2", "clpa2", tc);
114         final ControlLoopParams clpb1 = buildControlLoopParams("b", "1", "clpb1", tc);
115         final ControlLoopParams clpb3 = buildControlLoopParams("b", "3", "clpb3", null);
116
117         // the add for 'clpb3' should fail, because there is no ToscaPolicy
118         startLog();
119         assertSame(clpb3, tc.addControlLoopParams(clpb3));
120         stopLog();
121         assertLog(".*Missing ToscaPolicy, name=b, version=3.*");
122         assertNull(tc.removeControlLoopParams("clpb3"));
123
124         controlLoopParams.add(clpa1);
125         controlLoopParams.add(clpa2);
126         controlLoopParams.add(clpb1);
127         assertEquals(controlLoopParams, new HashSet<>(tc.getAllControlLoopParams()));
128
129         // manually remove a ControlLoopParams
130         assertSame(clpa1, tc.removeControlLoopParams("clpa1"));
131         assertTrue(controlLoopParams.remove(clpa1));
132         assertEquals(controlLoopParams, new HashSet<>(tc.getAllControlLoopParams()));
133
134         // tests of nonexistent policies
135         assertNull(tc.getToscaPolicy("c", "1")); // non-existent name
136         assertNull(tc.removeToscaPolicy("c", "1"));
137         assertNull(tc.getToscaPolicy("b", "3")); // non-existent version
138         assertNull(tc.removeToscaPolicy("b", "3"));
139
140         assertSame(a1, tc.removeToscaPolicy("a", "1"));
141         assertTrue(toscaPolicies.remove(a1));
142         assertEquals(toscaPolicies, tc.getAllToscaPolicies());
143         assertSame(a2, tc.removeToscaPolicy("a", "2"));
144         assertTrue(toscaPolicies.remove(a2));
145         assertEquals(toscaPolicies, tc.getAllToscaPolicies());
146
147         // ControlLoopParams removal should be automatic
148         assertTrue(controlLoopParams.remove(clpa2));
149         assertEquals(controlLoopParams, new HashSet<>(tc.getAllControlLoopParams()));
150
151         // test reset method
152         tc.reset();
153         assertTrue(tc.getAllToscaPolicies().isEmpty());
154         assertTrue(tc.getAllControlLoopParams().isEmpty());
155         assertTrue(tc.getAllEventManagers().isEmpty());
156         assertTrue(tc.getAllOnsetToEventManager().isEmpty());
157
158         PolicyControllerConstants.getFactory().shutdown(tc);
159         assertFalse(PolicyControllerConstants.getFactory().inventory().contains(tc));
160         assertFalse(DroolsControllerConstants.getFactory().inventory().contains(tc));
161     }
162
163     @Test
164     public void onsetErrors() throws Exception {
165         TdjamController tc = (TdjamController) PolicyControllerConstants.getFactory().build("tc", prop);
166         assertTrue(PolicyControllerConstants.getFactory().inventory().contains(tc));
167         assertTrue(DroolsControllerConstants.getFactory().inventory().contains(tc));
168         tc.start();
169
170         buildToscaPolicy("a", "1", tc);
171         final ControlLoopParams clpa1 = buildControlLoopParams("a", "1", "clpa1", tc);
172         assertTrue(tc.getAllControlLoopParams().contains(clpa1));
173
174         CanonicalOnset canonicalOnset = new CanonicalOnset();
175         startLog();
176         Whitebox.invokeMethod(tc, "processEvent", canonicalOnset);
177         stopLog();
178         assertLog(".*No ControlLoopParams for event: CanonicalOnset.*");
179
180         // set Name with new canonicalOnset
181         CanonicalOnset canonicalOnset2 = new CanonicalOnset();
182         canonicalOnset2.setClosedLoopControlName("clpa1");
183         // try with a non-null requestID, but missing target
184         canonicalOnset2.setRequestId(UUID.randomUUID());
185         startLog();
186         Whitebox.invokeMethod(tc, "processEvent", canonicalOnset2);
187         stopLog();
188         assertLog(".*Exception creating ControlLoopEventManager.*");
189
190         PolicyControllerConstants.getFactory().shutdown(tc);
191         assertFalse(PolicyControllerConstants.getFactory().inventory().contains(tc));
192     }
193
194     private ToscaPolicy buildToscaPolicy(String name, String version, TdjamController tc) {
195         ToscaPolicy tp = new ToscaPolicy();
196         tp.setName(name);
197         tp.setVersion(version);
198
199         if (tc != null) {
200             tc.addToscaPolicy(tp);
201         }
202         return tp;
203     }
204
205     private ControlLoopParams buildControlLoopParams(String name, String version,
206         String closedLoopControlName, TdjamController tc) {
207
208         ControlLoopParams clp = new ControlLoopParams();
209         clp.setPolicyName(name);
210         clp.setPolicyVersion(version);
211         clp.setClosedLoopControlName(closedLoopControlName);
212
213         if (tc != null) {
214             assertTrue(tc.addControlLoopParams(clp) != clp);
215         }
216
217         return clp;
218     }
219
220     private void startLog() {
221         appender.list.clear();
222         appender.start();
223     }
224
225     private void stopLog() {
226         appender.stop();
227     }
228
229     private void assertLog(String regexp) {
230         for (ILoggingEvent event : appender.list) {
231             if (event.toString().matches(regexp)) {
232                 return;
233             }
234         }
235         fail("Missing log entry: " + regexp);
236     }
237 }