2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.policy.controlloop.tdjam;
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;
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;
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>();
57 * Setup appender, and initialize properties.
60 public static void setupClass() throws Exception {
61 logger.setLevel(Level.DEBUG);
62 logger.addAppender(appender);
64 prop = PropertyUtil.getProperties("src/test/resources/config/tdjam-controller.properties");
65 prop.setProperty(PROPERTY_CONTROLLER_TYPE, "tdjam");
67 PolicyEngineConstants.getManager().configure(new Properties());
68 PolicyEngineConstants.getManager().start();
76 public static void cleanupClass() {
78 PolicyEngineConstants.getManager().stop();
79 PolicyEngineConstants.getManager().getExecutorService().shutdown();
82 System.out.println("APPENDER:");
83 for (ILoggingEvent event : appender.list) {
84 System.out.println(" " + event);
86 logger.detachAppender(appender);
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));
95 final HashSet<ToscaPolicy> toscaPolicies = new HashSet<>();
96 final HashSet<ControlLoopParams> controlLoopParams = new HashSet<>();
98 ToscaPolicy a1 = buildToscaPolicy("a", "1", tc);
99 ToscaPolicy a2 = buildToscaPolicy("a", "2", tc);
100 ToscaPolicy b1 = buildToscaPolicy("b", "1", tc);
102 toscaPolicies.add(a1);
103 toscaPolicies.add(a2);
104 toscaPolicies.add(b1);
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());
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);
117 // the add for 'clpb3' should fail, because there is no ToscaPolicy
119 assertSame(clpb3, tc.addControlLoopParams(clpb3));
121 assertLog(".*Missing ToscaPolicy, name=b, version=3.*");
122 assertNull(tc.removeControlLoopParams("clpb3"));
124 controlLoopParams.add(clpa1);
125 controlLoopParams.add(clpa2);
126 controlLoopParams.add(clpb1);
127 assertEquals(controlLoopParams, new HashSet<>(tc.getAllControlLoopParams()));
129 // manually remove a ControlLoopParams
130 assertSame(clpa1, tc.removeControlLoopParams("clpa1"));
131 assertTrue(controlLoopParams.remove(clpa1));
132 assertEquals(controlLoopParams, new HashSet<>(tc.getAllControlLoopParams()));
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"));
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());
147 // ControlLoopParams removal should be automatic
148 assertTrue(controlLoopParams.remove(clpa2));
149 assertEquals(controlLoopParams, new HashSet<>(tc.getAllControlLoopParams()));
153 assertTrue(tc.getAllToscaPolicies().isEmpty());
154 assertTrue(tc.getAllControlLoopParams().isEmpty());
155 assertTrue(tc.getAllEventManagers().isEmpty());
156 assertTrue(tc.getAllOnsetToEventManager().isEmpty());
158 PolicyControllerConstants.getFactory().shutdown(tc);
159 assertFalse(PolicyControllerConstants.getFactory().inventory().contains(tc));
160 assertFalse(DroolsControllerConstants.getFactory().inventory().contains(tc));
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));
170 buildToscaPolicy("a", "1", tc);
171 final ControlLoopParams clpa1 = buildControlLoopParams("a", "1", "clpa1", tc);
172 assertTrue(tc.getAllControlLoopParams().contains(clpa1));
174 CanonicalOnset canonicalOnset = new CanonicalOnset();
176 Whitebox.invokeMethod(tc, "processEvent", canonicalOnset);
178 assertLog(".*No ControlLoopParams for event: CanonicalOnset.*");
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());
186 Whitebox.invokeMethod(tc, "processEvent", canonicalOnset2);
188 assertLog(".*Exception creating ControlLoopEventManager.*");
190 PolicyControllerConstants.getFactory().shutdown(tc);
191 assertFalse(PolicyControllerConstants.getFactory().inventory().contains(tc));
194 private ToscaPolicy buildToscaPolicy(String name, String version, TdjamController tc) {
195 ToscaPolicy tp = new ToscaPolicy();
197 tp.setVersion(version);
200 tc.addToscaPolicy(tp);
205 private ControlLoopParams buildControlLoopParams(String name, String version,
206 String closedLoopControlName, TdjamController tc) {
208 ControlLoopParams clp = new ControlLoopParams();
209 clp.setPolicyName(name);
210 clp.setPolicyVersion(version);
211 clp.setClosedLoopControlName(closedLoopControlName);
214 assertTrue(tc.addControlLoopParams(clp) != clp);
220 private void startLog() {
221 appender.list.clear();
225 private void stopLog() {
229 private void assertLog(String regexp) {
230 for (ILoggingEvent event : appender.list) {
231 if (event.toString().matches(regexp)) {
235 fail("Missing log entry: " + regexp);