2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2018 Ericsson. All rights reserved.
4 * Modifications Copyright (C) 2020,2022 Nordix Foundation.
5 * Modifications Copyright (C) 2021-2022 Bell Canada 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.
19 * SPDX-License-Identifier: Apache-2.0
20 * ============LICENSE_END=========================================================
23 package org.onap.policy.apex.service.engine.runtime.impl;
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.assertNull;
29 import static org.junit.Assert.assertTrue;
30 import static org.junit.Assert.fail;
32 import java.io.ByteArrayInputStream;
33 import java.io.IOException;
34 import java.util.concurrent.BlockingQueue;
35 import java.util.concurrent.LinkedBlockingQueue;
36 import org.junit.After;
37 import org.junit.AfterClass;
38 import org.junit.BeforeClass;
39 import org.junit.Test;
40 import org.onap.policy.apex.context.parameters.ContextParameterConstants;
41 import org.onap.policy.apex.context.parameters.ContextParameters;
42 import org.onap.policy.apex.context.parameters.DistributorParameters;
43 import org.onap.policy.apex.context.parameters.LockManagerParameters;
44 import org.onap.policy.apex.context.parameters.PersistorParameters;
45 import org.onap.policy.apex.context.parameters.SchemaParameters;
46 import org.onap.policy.apex.core.engine.EngineParameterConstants;
47 import org.onap.policy.apex.core.engine.EngineParameters;
48 import org.onap.policy.apex.core.engine.ExecutorParameters;
49 import org.onap.policy.apex.core.infrastructure.threading.ApplicationThreadFactory;
50 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
51 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
52 import org.onap.policy.apex.model.basicmodel.handling.ApexModelException;
53 import org.onap.policy.apex.model.basicmodel.handling.ApexModelReader;
54 import org.onap.policy.apex.model.basicmodel.service.ModelService;
55 import org.onap.policy.apex.model.enginemodel.concepts.AxEngineState;
56 import org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel;
57 import org.onap.policy.apex.service.engine.event.ApexEvent;
58 import org.onap.policy.apex.service.engine.main.ApexPolicyStatisticsManager;
59 import org.onap.policy.common.parameters.ParameterService;
60 import org.onap.policy.common.utils.resources.TextFileUtils;
61 import org.onap.policy.common.utils.services.Registry;
64 * Test the engine worker class.
66 public class EngineWorkerTest {
67 private final ApplicationThreadFactory atFactory = new ApplicationThreadFactory("apex-engine-service", 512);
69 private static String simpleModelString;
70 private static String differentModelString;
71 private static AxPolicyModel simpleModel;
74 * Read the models into strings.
76 * @throws IOException on model reading errors
77 * @throws ApexModelException on model reading exceptions
80 public static void readSimpleModel() throws IOException, ApexModelException {
81 simpleModelString = TextFileUtils.getTextFileAsString("src/test/resources/policymodels/SmallModel.json");
83 differentModelString =
84 TextFileUtils.getTextFileAsString("src/test/resources/policymodels/SmallModelDifferent.json");
86 final ApexModelReader<AxPolicyModel> modelReader = new ApexModelReader<>(AxPolicyModel.class);
87 simpleModel = modelReader.read(new ByteArrayInputStream(simpleModelString.getBytes()));
91 * Initialize default parameters.
94 public static void initializeDefaultParameters() {
95 ParameterService.clear();
96 final SchemaParameters schemaParameters = new SchemaParameters();
97 schemaParameters.setName(ContextParameterConstants.SCHEMA_GROUP_NAME);
98 ParameterService.register(schemaParameters);
100 final ContextParameters contextParameters = new ContextParameters();
101 contextParameters.setName(ContextParameterConstants.MAIN_GROUP_NAME);
102 ParameterService.register(contextParameters);
104 final DistributorParameters distributorParameters = new DistributorParameters();
105 distributorParameters.setName(ContextParameterConstants.DISTRIBUTOR_GROUP_NAME);
106 ParameterService.register(distributorParameters);
108 final LockManagerParameters lockManagerParameters = new LockManagerParameters();
109 lockManagerParameters.setName(ContextParameterConstants.LOCKING_GROUP_NAME);
110 ParameterService.register(lockManagerParameters);
112 final PersistorParameters persistorParameters = new PersistorParameters();
113 persistorParameters.setName(ContextParameterConstants.PERSISTENCE_GROUP_NAME);
114 ParameterService.register(persistorParameters);
116 final EngineParameters engineParameters = new EngineParameters();
117 engineParameters.setName(EngineParameterConstants.MAIN_GROUP_NAME);
118 ExecutorParameters jsExecutorParameters = new ExecutorParameters();
119 jsExecutorParameters.setName("JAVASCRIPT");
121 .setTaskSelectionExecutorPluginClass("org.onap.policy.apex.service.engine.runtime.impl.DummyTse");
122 jsExecutorParameters.setTaskExecutorPluginClass("org.onap.policy.apex.service.engine.runtime.impl.DummyTe");
124 .setStateFinalizerExecutorPluginClass("org.onap.policy.apex.service.engine.runtime.impl.DummySfe");
125 engineParameters.getExecutorParameterMap().put("JAVASCRIPT", jsExecutorParameters);
126 ExecutorParameters mvvelExecutorParameters = new ExecutorParameters();
127 mvvelExecutorParameters.setName("MVEL");
128 mvvelExecutorParameters
129 .setTaskSelectionExecutorPluginClass("org.onap.policy.apex.service.engine.runtime.impl.DummyTse");
130 mvvelExecutorParameters.setTaskExecutorPluginClass("org.onap.policy.apex.service.engine.runtime.impl.DummyTe");
131 mvvelExecutorParameters
132 .setStateFinalizerExecutorPluginClass("org.onap.policy.apex.service.engine.runtime.impl.DummySfe");
133 engineParameters.getExecutorParameterMap().put("MVEL", jsExecutorParameters);
134 ParameterService.register(engineParameters);
139 * Teardown default parameters.
142 public static void teardownDefaultParameters() {
143 ParameterService.deregister(ContextParameterConstants.SCHEMA_GROUP_NAME);
144 ParameterService.deregister(ContextParameterConstants.DISTRIBUTOR_GROUP_NAME);
145 ParameterService.deregister(ContextParameterConstants.LOCKING_GROUP_NAME);
146 ParameterService.deregister(ContextParameterConstants.PERSISTENCE_GROUP_NAME);
147 ParameterService.deregister(ContextParameterConstants.MAIN_GROUP_NAME);
148 ParameterService.deregister(EngineParameterConstants.MAIN_GROUP_NAME);
152 public void cleardownTest() {
153 ModelService.clear();
157 public void testEngineWorker() {
159 BlockingQueue<ApexEvent> eventQueue = new LinkedBlockingQueue<>();
161 EngineWorker worker = new EngineWorker(new AxArtifactKey("Worker", "0.0.1"), eventQueue, atFactory);
164 worker.registerActionListener(null, null);
165 fail("test should throw an exception");
166 } catch (Exception apEx) {
167 assertEquals("addEventListener()<-Worker:0.0.1,STOPPED, listenerName is null", apEx.getMessage());
170 worker.registerActionListener("DummyListener", null);
173 worker.registerActionListener(null, new DummyApexEventListener());
174 fail("test should throw an exception");
175 } catch (Exception apEx) {
176 assertEquals("addEventListener()<-Worker:0.0.1,STOPPED, listenerName is null", apEx.getMessage());
179 worker.registerActionListener("DummyListener", new DummyApexEventListener());
182 worker.deregisterActionListener(null);
183 fail("test should throw an exception");
184 } catch (Exception apEx) {
185 assertEquals("removeEventListener()<-Worker:0.0.1,STOPPED, listenerName is null", apEx.getMessage());
188 worker.deregisterActionListener("DummyListener");
191 worker.getEngineServiceEventInterface();
192 fail("test should throw an exception");
193 } catch (Exception apEx) {
194 assertEquals("getEngineServiceEventInterface() call is not allowed on an Apex Engine Worker",
199 worker.startPeriodicEvents(100000);
200 fail("test should throw an exception");
201 } catch (Exception apEx) {
202 assertEquals("startPeriodicEvents() call is not allowed on an Apex Engine Worker", apEx.getMessage());
206 worker.stopPeriodicEvents();
207 fail("test should throw an exception");
208 } catch (Exception apEx) {
209 assertEquals("stopPeriodicEvents() call is not allowed on an Apex Engine Worker", apEx.getMessage());
212 assertEquals("Worker:0.0.1", worker.getEngineKeys().iterator().next().getId());
214 assertNull(worker.getApexModelKey());
216 String runtimeInfo = worker.getRuntimeInfo(worker.getEngineKeys().iterator().next());
217 assertEquals("{\"TimeStamp\":", runtimeInfo.replaceAll("\\s+", "").substring(0, 13));
219 assertEquals(AxEngineState.STOPPED, worker.getState());
221 String status = worker.getStatus(worker.getEngineKeys().iterator().next());
222 assertEquals("{\"TimeStamp\":", runtimeInfo.replaceAll("\\s+", "").substring(0, 13));
224 assertFalse(worker.isStarted());
225 assertFalse(worker.isStarted(null));
226 assertFalse(worker.isStarted(new AxArtifactKey("DummyKey", "0.0.1")));
227 assertFalse(worker.isStarted(worker.getEngineKeys().iterator().next()));
228 assertTrue(worker.isStopped());
229 assertTrue(worker.isStopped(null));
230 assertTrue(worker.isStopped(new AxArtifactKey("DummyKey", "0.0.1")));
231 assertTrue(worker.isStopped(worker.getEngineKeys().iterator().next()));
234 worker.start(new AxArtifactKey("DummyKey", "0.0.1"));
235 fail("test should throw an exception");
236 } catch (ApexException apEx) {
237 assertEquals("engine key DummyKey:0.0.1 does not match the keyWorker:0.0.1 of this engine",
242 worker.start(worker.getEngineKeys().iterator().next());
243 fail("test should throw an exception");
244 } catch (ApexException apEx) {
245 assertEquals("start()<-Worker:0.0.1,STOPPED, cannot start engine, engine has not been initialized, "
246 + "its model is not loaded", apEx.getMessage());
251 fail("test should throw an exception");
252 } catch (ApexException apEx) {
253 assertEquals("start()<-Worker:0.0.1,STOPPED, cannot start engine, "
254 + "engine has not been initialized, its model is not loaded", apEx.getMessage());
258 worker.stop(new AxArtifactKey("DummyKey", "0.0.1"));
259 fail("test should throw an exception");
260 } catch (ApexException apEx) {
261 assertEquals("engine key DummyKey:0.0.1 does not match the keyWorker:0.0.1 of this engine",
266 worker.stop(worker.getEngineKeys().iterator().next());
267 } catch (ApexException apEx) {
268 fail("test should not throw an exception");
273 } catch (ApexException apEx) {
274 fail("test should not throw an exception");
278 worker.clear(new AxArtifactKey("DummyKey", "0.0.1"));
279 fail("test should throw an exception");
280 } catch (ApexException apEx) {
281 assertEquals("engine key DummyKey:0.0.1 does not match the keyWorker:0.0.1 of this engine",
286 worker.clear(worker.getEngineKeys().iterator().next());
287 } catch (ApexException apEx) {
288 fail("test should not throw an exception");
293 } catch (ApexException apEx) {
294 fail("test should not throw an exception");
298 worker.updateModel(new AxArtifactKey("DummyKey", "0.0.1"), "", true);
299 fail("test should throw an exception");
300 } catch (ApexException apEx) {
301 assertEquals("failed to unmarshal the apex model on engine DummyKey:0.0.1", apEx.getMessage());
305 worker.updateModel(new AxArtifactKey("DummyKey", "0.0.1"), "I am not an Apex model", true);
306 fail("test should throw an exception");
307 } catch (ApexException apEx) {
308 assertEquals("failed to unmarshal the apex model on engine DummyKey:0.0.1", apEx.getMessage());
312 worker.updateModel(new AxArtifactKey("DummyKey", "0.0.1"), simpleModelString, true);
313 fail("test should throw an exception");
314 } catch (ApexException apEx) {
315 assertEquals("engine key DummyKey:0.0.1 does not match the keyWorker:0.0.1 of this engine",
320 worker.updateModel(new AxArtifactKey("DummyKey", "0.0.1"), (AxPolicyModel) null, true);
321 fail("test should throw an exception");
322 } catch (ApexException apEx) {
323 assertEquals("engine key DummyKey:0.0.1 does not match the keyWorker:0.0.1 of this engine",
328 worker.updateModel(new AxArtifactKey("DummyKey", "0.0.1"), simpleModel, true);
329 fail("test should throw an exception");
330 } catch (ApexException apEx) {
331 assertEquals("engine key DummyKey:0.0.1 does not match the keyWorker:0.0.1 of this engine",
337 public void testApexImplModelWIthModel() throws ApexException {
338 Registry.newRegistry();
339 Registry.register(ApexPolicyStatisticsManager.REG_APEX_PDP_POLICY_COUNTER, new ApexPolicyStatisticsManager());
340 BlockingQueue<ApexEvent> eventQueue = new LinkedBlockingQueue<>();
342 EngineWorker worker = new EngineWorker(new AxArtifactKey("Worker", "0.0.1"), eventQueue, atFactory);
343 assertEquals("Worker:0.0.1", worker.getKey().getId());
346 worker.updateModel(worker.getKey(), simpleModelString, false);
347 } catch (ApexException apEx) {
348 fail("test should not throw an exception");
351 eventQueue.add(new ApexEvent("SomeEvent", "0.0.1", "the.event.namespace", "EventSource", "EventTarget", ""));
354 worker.updateModel(worker.getKey(), differentModelString, false);
355 fail("test should throw an exception");
356 } catch (ApexException apEx) {
357 assertEquals("apex model update failed, supplied model with key \"SmallModelDifferent:0.0.1\" is not a "
358 + "compatible model update " + "from the existing engine model with key \"SmallModel:0.0.1\"",
363 worker.updateModel(worker.getKey(), differentModelString, true);
364 } catch (ApexException apEx) {
365 fail("test should not throw an exception");
369 worker.updateModel(worker.getKey(), simpleModelString, true);
370 } catch (ApexException apEx) {
371 fail("test should not throw an exception");
374 String runtimeInfo = worker.getRuntimeInfo(worker.getEngineKeys().iterator().next());
375 assertEquals("{\"TimeStamp\":", runtimeInfo.replaceAll("\\s+", "").substring(0, 13));
377 assertEquals(AxEngineState.STOPPED, worker.getState());
380 assertEquals(AxEngineState.READY, worker.getState());
382 String status = worker.getStatus(worker.getEngineKeys().iterator().next());
383 assertEquals("{\"timestamp\":", status.replaceAll("\\s+", "").substring(0, 13));
385 assertTrue(worker.isStarted());
386 assertTrue(worker.isStarted(worker.getEngineKeys().iterator().next()));
387 assertFalse(worker.isStopped());
388 assertFalse(worker.isStopped(worker.getEngineKeys().iterator().next()));
391 worker.start(worker.getEngineKeys().iterator().next());
392 fail("test should throw an exception");
393 } catch (ApexException apEx) {
394 assertEquals("apex engine for engine key Worker:0.0.1 is already running with state READY",
400 fail("test should throw an exception");
401 } catch (ApexException apEx) {
402 assertEquals("apex engine for engine key Worker:0.0.1 is already running with state READY",
407 worker.stop(worker.getEngineKeys().iterator().next());
408 } catch (ApexException apEx) {
409 fail("test should not throw an exception");
413 worker.start(worker.getEngineKeys().iterator().next());
414 } catch (ApexException apEx) {
415 fail("test should not throw an exception");
420 } catch (ApexException apEx) {
421 fail("test should not throw an exception");
426 } catch (ApexException apEx) {
427 fail("test should not throw an exception");
434 worker.clear(worker.getEngineKeys().iterator().next());
435 fail("test should throw an exception");
436 } catch (ApexException apEx) {
437 assertEquals("clear()<-Worker:0.0.1,READY, cannot clear engine, engine is not stopped", apEx.getMessage());
441 worker.stop(worker.getEngineKeys().iterator().next());
442 worker.clear(worker.getEngineKeys().iterator().next());
443 } catch (ApexException apEx) {
444 fail("test should not throw an exception");
449 } catch (ApexException apEx) {
450 fail("test should not throw an exception");
453 assertNotNull(worker.getApexModelKey());
455 final ApexPolicyStatisticsManager policyCounter = ApexPolicyStatisticsManager.getInstanceFromRegistry();
456 assertNotNull(policyCounter);
457 assertEquals(policyCounter.getPolicyExecutedCount(),
458 policyCounter.getPolicyExecutedFailCount() + policyCounter.getPolicyExecutedSuccessCount());