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.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;
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;
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>();
58 * Setup appender, and initialize properties.
61 public static void setupClass() throws Exception {
62 logger.setLevel(Level.DEBUG);
63 logger.addAppender(appender);
65 prop = PropertyUtil.getProperties("src/test/resources/config/tdjam-controller.properties");
66 prop.setProperty(PROPERTY_CONTROLLER_TYPE, "tdjam");
68 PolicyEngineConstants.getManager().configure(new Properties());
69 PolicyEngineConstants.getManager().start();
77 public static void cleanupClass() {
79 PolicyEngineConstants.getManager().stop();
80 PolicyEngineConstants.getManager().getExecutorService().shutdown();
83 System.out.println("APPENDER:");
84 for (ILoggingEvent event : appender.list) {
85 System.out.println(" " + event);
87 logger.detachAppender(appender);
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));
96 final HashSet<ToscaPolicy> toscaPolicies = new HashSet<>();
97 final HashSet<ControlLoopParams> controlLoopParams = new HashSet<>();
99 ToscaPolicy a1 = buildToscaPolicy("a", "1", tc);
100 ToscaPolicy a2 = buildToscaPolicy("a", "2", tc);
101 ToscaPolicy b1 = buildToscaPolicy("b", "1", tc);
103 toscaPolicies.add(a1);
104 toscaPolicies.add(a2);
105 toscaPolicies.add(b1);
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());
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);
118 // the add for 'clpb3' should fail, because there is no ToscaPolicy
120 assertSame(clpb3, tc.addControlLoopParams(clpb3));
122 assertLog(".*Missing ToscaPolicy, name=b, version=3.*");
123 assertNull(tc.removeControlLoopParams("clpb3"));
125 controlLoopParams.add(clpa1);
126 controlLoopParams.add(clpa2);
127 controlLoopParams.add(clpb1);
128 assertEquals(controlLoopParams, new HashSet<>(tc.getAllControlLoopParams()));
130 // manually remove a ControlLoopParams
131 assertSame(clpa1, tc.removeControlLoopParams("clpa1"));
132 assertTrue(controlLoopParams.remove(clpa1));
133 assertEquals(controlLoopParams, new HashSet<>(tc.getAllControlLoopParams()));
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"));
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());
148 // ControlLoopParams removal should be automatic
149 assertTrue(controlLoopParams.remove(clpa2));
150 assertEquals(controlLoopParams, new HashSet<>(tc.getAllControlLoopParams()));
154 assertTrue(tc.getAllToscaPolicies().isEmpty());
155 assertTrue(tc.getAllControlLoopParams().isEmpty());
156 assertTrue(tc.getAllEventManagers().isEmpty());
157 assertTrue(tc.getAllOnsetToEventManager().isEmpty());
159 PolicyControllerConstants.getFactory().shutdown(tc);
160 assertFalse(PolicyControllerConstants.getFactory().inventory().contains(tc));
161 assertFalse(DroolsControllerConstants.getFactory().inventory().contains(tc));
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));
171 buildToscaPolicy("a", "1", tc);
172 final ControlLoopParams clpa1 = buildControlLoopParams("a", "1", "clpa1", tc);
173 assertTrue(tc.getAllControlLoopParams().contains(clpa1));
175 CanonicalOnset canonicalOnset = new CanonicalOnset();
177 Whitebox.invokeMethod(tc, "processEvent", canonicalOnset);
179 assertLog(".*No ControlLoopParams for event: CanonicalOnset.*");
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());
187 Whitebox.invokeMethod(tc, "processEvent", canonicalOnset2);
189 assertLog(".*Exception creating ControlLoopEventManager.*");
191 PolicyControllerConstants.getFactory().shutdown(tc);
192 assertFalse(PolicyControllerConstants.getFactory().inventory().contains(tc));
195 private ToscaPolicy buildToscaPolicy(String name, String version, TdjamController tc) {
196 ToscaPolicy tp = new ToscaPolicy();
198 tp.setVersion(version);
201 tc.addToscaPolicy(tp);
206 private ControlLoopParams buildControlLoopParams(String name, String version,
207 String closedLoopControlName, TdjamController tc) {
209 ControlLoopParams clp = new ControlLoopParams();
210 clp.setPolicyName(name);
211 clp.setPolicyVersion(version);
212 clp.setClosedLoopControlName(closedLoopControlName);
215 assertNotSame(clp, tc.addControlLoopParams(clp));
221 private void startLog() {
222 appender.list.clear();
226 private void stopLog() {
230 private void assertLog(String regexp) {
231 for (ILoggingEvent event : appender.list) {
232 if (event.toString().matches(regexp)) {
236 fail("Missing log entry: " + regexp);