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.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;
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>();
60 * Setup appender, and initialize properties.
63 public static void setupClass() throws Exception {
64 logger.setLevel(Level.DEBUG);
65 logger.addAppender(appender);
67 prop = PropertyUtil.getProperties("src/test/resources/config/tdjam-controller.properties");
68 prop.setProperty(PROPERTY_CONTROLLER_TYPE, "tdjam");
70 PolicyEngineConstants.getManager().configure(new Properties());
71 PolicyEngineConstants.getManager().start();
79 public static void cleanupClass() {
81 PolicyEngineConstants.getManager().stop();
82 PolicyEngineConstants.getManager().getExecutorService().shutdown();
85 System.out.println("APPENDER:");
86 for (ILoggingEvent event : appender.list) {
87 System.out.println(" " + event);
89 logger.detachAppender(appender);
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));
98 final HashSet<ToscaPolicy> toscaPolicies = new HashSet<>();
99 final HashSet<ControlLoopParams> controlLoopParams = new HashSet<>();
101 ToscaPolicy a1 = buildToscaPolicy("a", "1", tc);
102 ToscaPolicy a2 = buildToscaPolicy("a", "2", tc);
103 ToscaPolicy b1 = buildToscaPolicy("b", "1", tc);
105 toscaPolicies.add(a1);
106 toscaPolicies.add(a2);
107 toscaPolicies.add(b1);
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());
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);
120 // the add for 'clpb3' should fail, because there is no ToscaPolicy
122 assertSame(clpb3, tc.addControlLoopParams(clpb3));
124 assertLog(".*Missing ToscaPolicy, name=b, version=3.*");
125 assertNull(tc.removeControlLoopParams("clpb3"));
127 controlLoopParams.add(clpa1);
128 controlLoopParams.add(clpa2);
129 controlLoopParams.add(clpb1);
130 assertEquals(controlLoopParams, new HashSet<>(tc.getAllControlLoopParams()));
132 // manually remove a ControlLoopParams
133 assertSame(clpa1, tc.removeControlLoopParams("clpa1"));
134 assertTrue(controlLoopParams.remove(clpa1));
135 assertEquals(controlLoopParams, new HashSet<>(tc.getAllControlLoopParams()));
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"));
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());
150 // ControlLoopParams removal should be automatic
151 assertTrue(controlLoopParams.remove(clpa2));
152 assertEquals(controlLoopParams, new HashSet<>(tc.getAllControlLoopParams()));
156 assertTrue(tc.getAllToscaPolicies().isEmpty());
157 assertTrue(tc.getAllControlLoopParams().isEmpty());
158 assertTrue(tc.getAllEventManagers().isEmpty());
159 assertTrue(tc.getAllOnsetToEventManager().isEmpty());
161 PolicyControllerConstants.getFactory().shutdown(tc);
162 assertFalse(PolicyControllerConstants.getFactory().inventory().contains(tc));
163 assertFalse(DroolsControllerConstants.getFactory().inventory().contains(tc));
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));
173 buildToscaPolicy("a", "1", tc);
174 final ControlLoopParams clpa1 = buildControlLoopParams("a", "1", "clpa1", tc);
175 assertTrue(tc.getAllControlLoopParams().contains(clpa1));
177 CanonicalOnset canonicalOnset = new CanonicalOnset();
179 Whitebox.invokeMethod(tc, "processEvent", canonicalOnset);
181 assertLog(".*No ControlLoopParams for event: CanonicalOnset.*");
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());
189 Whitebox.invokeMethod(tc, "processEvent", canonicalOnset2);
191 assertLog(".*Exception creating ControlLoopEventManager.*");
193 PolicyControllerConstants.getFactory().shutdown(tc);
194 assertFalse(PolicyControllerConstants.getFactory().inventory().contains(tc));
197 private ToscaPolicy buildToscaPolicy(String name, String version, TdjamController tc) {
198 ToscaPolicy tp = new ToscaPolicy();
200 tp.setVersion(version);
203 tc.addToscaPolicy(tp);
208 private ControlLoopParams buildControlLoopParams(String name, String version,
209 String closedLoopControlName, TdjamController tc) {
211 ControlLoopParams clp = new ControlLoopParams();
212 clp.setPolicyName(name);
213 clp.setPolicyVersion(version);
214 clp.setClosedLoopControlName(closedLoopControlName);
217 assertTrue(tc.addControlLoopParams(clp) != clp);
223 private void startLog() {
224 appender.list.clear();
228 private void stopLog() {
232 private void assertLog(String regexp) {
233 for (ILoggingEvent event : appender.list) {
234 if (event.toString().matches(regexp)) {
238 fail("Missing log entry: " + regexp);