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.eventmanager;
23 import static org.assertj.core.api.Assertions.assertThat;
24 import static org.assertj.core.api.Assertions.assertThatCode;
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertFalse;
27 import static org.junit.Assert.assertNotNull;
28 import static org.junit.Assert.assertSame;
29 import static org.junit.Assert.assertTrue;
30 import static org.mockito.Mockito.verify;
32 import java.io.IOException;
33 import java.util.ArrayList;
34 import java.util.List;
35 import java.util.UUID;
36 import java.util.concurrent.CompletableFuture;
37 import java.util.concurrent.ExecutorService;
38 import org.junit.Before;
39 import org.junit.Test;
40 import org.mockito.ArgumentCaptor;
41 import org.mockito.Mock;
42 import org.mockito.MockitoAnnotations;
43 import org.onap.policy.common.utils.coder.Coder;
44 import org.onap.policy.common.utils.coder.CoderException;
45 import org.onap.policy.common.utils.coder.StandardYamlCoder;
46 import org.onap.policy.common.utils.io.Serializer;
47 import org.onap.policy.common.utils.resources.ResourceUtils;
48 import org.onap.policy.controlloop.ControlLoopException;
49 import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
50 import org.onap.policy.controlloop.actorserviceprovider.OperationResult;
51 import org.onap.policy.controlloop.drl.legacy.ControlLoopParams;
52 import org.onap.policy.controlloop.ophistory.OperationHistoryDataManagerStub;
53 import org.onap.policy.drools.core.lock.LockCallback;
54 import org.onap.policy.drools.core.lock.LockImpl;
55 import org.onap.policy.drools.core.lock.LockState;
56 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
57 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
59 public class ControlLoopEventManagerTest {
60 private static final UUID REQ_ID = UUID.randomUUID();
61 private static final String CL_NAME = "my-closed-loop-name";
62 private static final String POLICY_NAME = "my-policy-name";
63 private static final String POLICY_SCOPE = "my-scope";
64 private static final String POLICY_VERSION = "1.2.3";
65 private static final String LOCK1 = "my-lock-A";
66 private static final String LOCK2 = "my-lock-B";
67 private static final Coder yamlCoder = new StandardYamlCoder();
68 private static final String MY_KEY = "def";
71 private ExecutorService executor;
73 private long preCreateTimeMs;
74 private List<LockImpl> locks;
75 private ToscaPolicy tosca;
76 private ControlLoopParams params;
77 private ControlLoopEventManager mgr;
83 public void setUp() throws ControlLoopException, CoderException {
84 MockitoAnnotations.initMocks(this);
86 params = new ControlLoopParams();
87 params.setClosedLoopControlName(CL_NAME);
88 params.setPolicyName(POLICY_NAME);
89 params.setPolicyScope(POLICY_SCOPE);
90 params.setPolicyVersion(POLICY_VERSION);
92 loadPolicy("eventManager/event-mgr-simple.yaml");
94 locks = new ArrayList<>();
96 preCreateTimeMs = System.currentTimeMillis();
98 MyManager.executor = executor;
99 MyManager.locks = locks;
101 mgr = new MyManager(params, REQ_ID);
105 public void testConstructor() {
106 assertEquals(POLICY_NAME, mgr.getPolicyName());
108 assertTrue(mgr.isActive());
109 assertEquals(CL_NAME, mgr.getClosedLoopControlName());
110 assertSame(REQ_ID, mgr.getRequestId());
111 assertEquals(POLICY_NAME, mgr.getPolicyName());
112 assertEquals(POLICY_VERSION, mgr.getPolicyVersion());
113 assertNotNull(mgr.getProcessor());
114 assertThat(mgr.getEndTimeMs()).isGreaterThanOrEqualTo(preCreateTimeMs);
118 public void testGetCreateCount() throws ControlLoopException {
119 long original = ControlLoopEventManager.getCreateCount();
121 new MyManager(params, REQ_ID);
122 assertEquals(original + 1, ControlLoopEventManager.getCreateCount());
124 new MyManager(params, REQ_ID);
125 assertEquals(original + 2, ControlLoopEventManager.getCreateCount());
129 public void testIsActive() throws Exception {
130 mgr = new ControlLoopEventManager(params, REQ_ID);
131 assertTrue(mgr.isActive());
133 ControlLoopEventManager mgr2 = Serializer.roundTrip(mgr);
134 assertFalse(mgr2.isActive());
138 public void testDestroy() throws IOException {
139 mgr.requestLock(LOCK1);
140 mgr.requestLock(LOCK2);
141 mgr.requestLock(LOCK1);
143 // ensure destroy() doesn't throw an exception if the object is deserialized
144 ControlLoopEventManager mgr2 = Serializer.roundTrip(mgr);
145 assertThatCode(() -> mgr2.destroy()).doesNotThrowAnyException();
147 // locks should not have been freed
148 for (LockImpl lock : locks) {
149 assertFalse(lock.isUnavailable());
156 for (LockImpl lock : locks) {
157 assertTrue(lock.isUnavailable());
162 public void testDetmControlLoopTimeoutMs() throws Exception {
163 long timeMs = 1200 * 1000L;
164 long end = mgr.getEndTimeMs();
165 assertThat(end).isGreaterThanOrEqualTo(preCreateTimeMs + timeMs).isLessThan(preCreateTimeMs + timeMs + 5000);
169 public void testRequestLock() {
170 final CompletableFuture<OperationOutcome> future1 = mgr.requestLock(LOCK1);
171 assertTrue(mgr.getOutcomes().isEmpty());
173 final CompletableFuture<OperationOutcome> future2 = mgr.requestLock(LOCK2);
174 assertTrue(mgr.getOutcomes().isEmpty());
176 assertSame(future1, mgr.requestLock(LOCK1));
177 assertTrue(mgr.getOutcomes().isEmpty());
179 assertEquals(2, locks.size());
181 assertTrue(future1.isDone());
182 assertTrue(future2.isDone());
184 // indicate that the first lock failed
185 locks.get(0).notifyUnavailable();
187 verifyLock(OperationResult.FAILURE);
188 assertTrue(mgr.getOutcomes().isEmpty());
191 private void verifyLock(OperationResult result) {
192 OperationOutcome outcome = mgr.getOutcomes().poll();
193 assertNotNull(outcome);
194 assertEquals(ActorConstants.LOCK_ACTOR, outcome.getActor());
195 assertEquals(ActorConstants.LOCK_OPERATION, outcome.getOperation());
196 assertNotNull(outcome.getEnd());
197 assertTrue(outcome.isFinalOutcome());
198 assertEquals(result, outcome.getResult());
202 public void testOnStart() {
203 OperationOutcome outcome1 = new OperationOutcome();
204 OperationOutcome outcome2 = new OperationOutcome();
206 mgr.onStart(outcome1);
207 mgr.onStart(outcome2);
209 assertSame(outcome1, mgr.getOutcomes().poll());
210 assertSame(outcome2, mgr.getOutcomes().poll());
211 assertTrue(mgr.getOutcomes().isEmpty());
215 public void testOnComplete() {
216 OperationOutcome outcome1 = new OperationOutcome();
217 OperationOutcome outcome2 = new OperationOutcome();
219 mgr.onComplete(outcome1);
220 mgr.onComplete(outcome2);
222 assertSame(outcome1, mgr.getOutcomes().poll());
223 assertSame(outcome2, mgr.getOutcomes().poll());
224 assertTrue(mgr.getOutcomes().isEmpty());
228 public void testContains_testGetProperty_testSetProperty_testRemoveProperty() {
229 mgr.setProperty("abc", "a string");
230 mgr.setProperty(MY_KEY, 100);
232 assertTrue(mgr.contains(MY_KEY));
233 assertFalse(mgr.contains("ghi"));
235 String strValue = mgr.getProperty("abc");
236 assertEquals("a string", strValue);
238 int intValue = mgr.getProperty(MY_KEY);
239 assertEquals(100, intValue);
241 mgr.removeProperty(MY_KEY);
242 assertFalse(mgr.contains(MY_KEY));
246 * Tests getDataManager() when guard.disabled=true.
249 public void testGetDataManagerDisabled() throws ControlLoopException {
250 mgr = new MyManager(params, REQ_ID) {
251 private static final long serialVersionUID = 1L;
253 protected String getEnvironmentProperty(String propName) {
254 return ("guard.disabled".equals(propName) ? "true" : null);
258 assertThat(mgr.getDataManager()).isInstanceOf(OperationHistoryDataManagerStub.class);
262 public void testToString() {
263 assertNotNull(mgr.toString());
266 private void loadPolicy(String fileName) throws CoderException {
267 ToscaServiceTemplate template =
268 yamlCoder.decode(ResourceUtils.getResourceAsString(fileName), ToscaServiceTemplate.class);
269 tosca = template.getToscaTopologyTemplate().getPolicies().get(0).values().iterator().next();
271 params.setToscaPolicy(tosca);
274 private void runExecutor() {
275 ArgumentCaptor<Runnable> runCaptor = ArgumentCaptor.forClass(Runnable.class);
276 verify(executor).execute(runCaptor.capture());
278 runCaptor.getValue().run();
282 private static class MyManager extends ControlLoopEventManager {
283 private static final long serialVersionUID = 1L;
285 private static ExecutorService executor;
286 private static List<LockImpl> locks;
288 public MyManager(ControlLoopParams params, UUID requestId)
289 throws ControlLoopException {
290 super(params, requestId);
294 protected ExecutorService getBlockingExecutor() {
299 protected void makeLock(String targetEntity, String requestId, int holdSec, LockCallback callback) {
300 LockImpl lock = new LockImpl(LockState.ACTIVE, targetEntity, requestId, holdSec, callback);
302 callback.lockAvailable(lock);