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