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