2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2018 Ericsson. All rights reserved.
4 * Modifications Copyright (C) 2020 Nordix Foundation.
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.apex.service.engine.runtime.impl;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertFalse;
26 import static org.junit.Assert.assertNotNull;
27 import static org.junit.Assert.assertNull;
28 import static org.junit.Assert.assertTrue;
29 import static org.junit.Assert.fail;
31 import java.io.ByteArrayInputStream;
32 import java.io.IOException;
33 import java.util.concurrent.BlockingQueue;
34 import java.util.concurrent.LinkedBlockingQueue;
35 import org.junit.After;
36 import org.junit.AfterClass;
37 import org.junit.BeforeClass;
38 import org.junit.Test;
39 import org.onap.policy.apex.context.parameters.ContextParameterConstants;
40 import org.onap.policy.apex.context.parameters.ContextParameters;
41 import org.onap.policy.apex.context.parameters.DistributorParameters;
42 import org.onap.policy.apex.context.parameters.LockManagerParameters;
43 import org.onap.policy.apex.context.parameters.PersistorParameters;
44 import org.onap.policy.apex.context.parameters.SchemaParameters;
45 import org.onap.policy.apex.core.engine.EngineParameterConstants;
46 import org.onap.policy.apex.core.engine.EngineParameters;
47 import org.onap.policy.apex.core.engine.ExecutorParameters;
48 import org.onap.policy.apex.core.infrastructure.threading.ApplicationThreadFactory;
49 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
50 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
51 import org.onap.policy.apex.model.basicmodel.handling.ApexModelException;
52 import org.onap.policy.apex.model.basicmodel.handling.ApexModelReader;
53 import org.onap.policy.apex.model.basicmodel.service.ModelService;
54 import org.onap.policy.apex.model.enginemodel.concepts.AxEngineState;
55 import org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel;
56 import org.onap.policy.apex.service.engine.event.ApexEvent;
57 import org.onap.policy.apex.service.engine.main.ApexPolicyStatisticsManager;
58 import org.onap.policy.common.parameters.ParameterService;
59 import org.onap.policy.common.utils.resources.TextFileUtils;
60 import org.onap.policy.common.utils.services.Registry;
63 * Test the engine worker class.
65 public class EngineWorkerTest {
66 private final ApplicationThreadFactory atFactory = new ApplicationThreadFactory("apex-engine-service", 512);
68 private static String simpleModelString;
69 private static String differentModelString;
70 private static AxPolicyModel simpleModel;
73 * Read the models into strings.
75 * @throws IOException on model reading errors
76 * @throws ApexModelException on model reading exceptions
79 public static void readSimpleModel() throws IOException, ApexModelException {
80 simpleModelString = TextFileUtils.getTextFileAsString("src/test/resources/policymodels/SmallModel.json");
82 differentModelString =
83 TextFileUtils.getTextFileAsString("src/test/resources/policymodels/SmallModelDifferent.json");
85 final ApexModelReader<AxPolicyModel> modelReader = new ApexModelReader<>(AxPolicyModel.class);
86 simpleModel = modelReader.read(new ByteArrayInputStream(simpleModelString.getBytes()));
90 * Initialize default parameters.
93 public static void initializeDefaultParameters() {
94 ParameterService.clear();
95 final SchemaParameters schemaParameters = new SchemaParameters();
96 schemaParameters.setName(ContextParameterConstants.SCHEMA_GROUP_NAME);
97 ParameterService.register(schemaParameters);
99 final ContextParameters contextParameters = new ContextParameters();
100 contextParameters.setName(ContextParameterConstants.MAIN_GROUP_NAME);
101 ParameterService.register(contextParameters);
103 final DistributorParameters distributorParameters = new DistributorParameters();
104 distributorParameters.setName(ContextParameterConstants.DISTRIBUTOR_GROUP_NAME);
105 ParameterService.register(distributorParameters);
107 final LockManagerParameters lockManagerParameters = new LockManagerParameters();
108 lockManagerParameters.setName(ContextParameterConstants.LOCKING_GROUP_NAME);
109 ParameterService.register(lockManagerParameters);
111 final PersistorParameters persistorParameters = new PersistorParameters();
112 persistorParameters.setName(ContextParameterConstants.PERSISTENCE_GROUP_NAME);
113 ParameterService.register(persistorParameters);
115 final EngineParameters engineParameters = new EngineParameters();
116 engineParameters.setName(EngineParameterConstants.MAIN_GROUP_NAME);
117 ExecutorParameters jsExecutorParameters = new ExecutorParameters();
118 jsExecutorParameters.setName("JAVASCRIPT");
120 .setTaskSelectionExecutorPluginClass("org.onap.policy.apex.service.engine.runtime.impl.DummyTse");
121 jsExecutorParameters.setTaskExecutorPluginClass("org.onap.policy.apex.service.engine.runtime.impl.DummyTe");
123 .setStateFinalizerExecutorPluginClass("org.onap.policy.apex.service.engine.runtime.impl.DummySfe");
124 engineParameters.getExecutorParameterMap().put("JAVASCRIPT", jsExecutorParameters);
125 ExecutorParameters mvvelExecutorParameters = new ExecutorParameters();
126 mvvelExecutorParameters.setName("MVEL");
127 mvvelExecutorParameters
128 .setTaskSelectionExecutorPluginClass("org.onap.policy.apex.service.engine.runtime.impl.DummyTse");
129 mvvelExecutorParameters.setTaskExecutorPluginClass("org.onap.policy.apex.service.engine.runtime.impl.DummyTe");
130 mvvelExecutorParameters
131 .setStateFinalizerExecutorPluginClass("org.onap.policy.apex.service.engine.runtime.impl.DummySfe");
132 engineParameters.getExecutorParameterMap().put("MVEL", jsExecutorParameters);
133 ParameterService.register(engineParameters);
138 * Teardown default parameters.
141 public static void teardownDefaultParameters() {
142 ParameterService.deregister(ContextParameterConstants.SCHEMA_GROUP_NAME);
143 ParameterService.deregister(ContextParameterConstants.DISTRIBUTOR_GROUP_NAME);
144 ParameterService.deregister(ContextParameterConstants.LOCKING_GROUP_NAME);
145 ParameterService.deregister(ContextParameterConstants.PERSISTENCE_GROUP_NAME);
146 ParameterService.deregister(ContextParameterConstants.MAIN_GROUP_NAME);
147 ParameterService.deregister(EngineParameterConstants.MAIN_GROUP_NAME);
151 public void cleardownTest() {
152 ModelService.clear();
156 public void testEngineWorker() {
158 BlockingQueue<ApexEvent> eventQueue = new LinkedBlockingQueue<>();
160 EngineWorker worker = new EngineWorker(new AxArtifactKey("Worker", "0.0.1"), eventQueue, atFactory);
163 worker.registerActionListener(null, null);
164 fail("test should throw an exception");
165 } catch (Exception apEx) {
166 assertEquals("addEventListener()<-Worker:0.0.1,STOPPED, listenerName is null", apEx.getMessage());
169 worker.registerActionListener("DummyListener", null);
172 worker.registerActionListener(null, new DummyApexEventListener());
173 fail("test should throw an exception");
174 } catch (Exception apEx) {
175 assertEquals("addEventListener()<-Worker:0.0.1,STOPPED, listenerName is null", apEx.getMessage());
178 worker.registerActionListener("DummyListener", new DummyApexEventListener());
181 worker.deregisterActionListener(null);
182 fail("test should throw an exception");
183 } catch (Exception apEx) {
184 assertEquals("removeEventListener()<-Worker:0.0.1,STOPPED, listenerName is null", apEx.getMessage());
187 worker.deregisterActionListener("DummyListener");
190 worker.getEngineServiceEventInterface();
191 fail("test should throw an exception");
192 } catch (Exception apEx) {
193 assertEquals("getEngineServiceEventInterface() call is not allowed on an Apex Engine Worker",
198 worker.startPeriodicEvents(100000);
199 fail("test should throw an exception");
200 } catch (Exception apEx) {
201 assertEquals("startPeriodicEvents() call is not allowed on an Apex Engine Worker", apEx.getMessage());
205 worker.stopPeriodicEvents();
206 fail("test should throw an exception");
207 } catch (Exception apEx) {
208 assertEquals("stopPeriodicEvents() call is not allowed on an Apex Engine Worker", apEx.getMessage());
211 assertEquals("Worker:0.0.1", worker.getEngineKeys().iterator().next().getId());
213 assertNull(worker.getApexModelKey());
215 String runtimeInfo = worker.getRuntimeInfo(worker.getEngineKeys().iterator().next());
216 assertEquals("{\"TimeStamp\":", runtimeInfo.replaceAll("\\s+", "").substring(0, 13));
218 assertEquals(AxEngineState.STOPPED, worker.getState());
220 String status = worker.getStatus(worker.getEngineKeys().iterator().next());
221 assertEquals("{\"apexEngineModel\":{\"key\":", status.replaceAll("\\s+", "").substring(0, 26));
223 assertFalse(worker.isStarted());
224 assertFalse(worker.isStarted(null));
225 assertFalse(worker.isStarted(new AxArtifactKey("DummyKey", "0.0.1")));
226 assertFalse(worker.isStarted(worker.getEngineKeys().iterator().next()));
227 assertTrue(worker.isStopped());
228 assertTrue(worker.isStopped(null));
229 assertTrue(worker.isStopped(new AxArtifactKey("DummyKey", "0.0.1")));
230 assertTrue(worker.isStopped(worker.getEngineKeys().iterator().next()));
233 worker.start(new AxArtifactKey("DummyKey", "0.0.1"));
234 fail("test should throw an exception");
235 } catch (ApexException apEx) {
236 assertEquals("engine key DummyKey:0.0.1 does not match the keyWorker:0.0.1 of this engine",
241 worker.start(worker.getEngineKeys().iterator().next());
242 fail("test should throw an exception");
243 } catch (ApexException apEx) {
244 assertEquals("start()<-Worker:0.0.1,STOPPED, cannot start engine, engine has not been initialized, "
245 + "its model is not loaded", apEx.getMessage());
250 fail("test should throw an exception");
251 } catch (ApexException apEx) {
252 assertEquals("start()<-Worker:0.0.1,STOPPED, cannot start engine, "
253 + "engine has not been initialized, its model is not loaded", apEx.getMessage());
257 worker.stop(new AxArtifactKey("DummyKey", "0.0.1"));
258 fail("test should throw an exception");
259 } catch (ApexException apEx) {
260 assertEquals("engine key DummyKey:0.0.1 does not match the keyWorker:0.0.1 of this engine",
265 worker.stop(worker.getEngineKeys().iterator().next());
266 } catch (ApexException apEx) {
267 fail("test should not throw an exception");
272 } catch (ApexException apEx) {
273 fail("test should not throw an exception");
277 worker.clear(new AxArtifactKey("DummyKey", "0.0.1"));
278 fail("test should throw an exception");
279 } catch (ApexException apEx) {
280 assertEquals("engine key DummyKey:0.0.1 does not match the keyWorker:0.0.1 of this engine",
285 worker.clear(worker.getEngineKeys().iterator().next());
286 } catch (ApexException apEx) {
287 fail("test should not throw an exception");
292 } catch (ApexException apEx) {
293 fail("test should not throw an exception");
297 worker.updateModel(new AxArtifactKey("DummyKey", "0.0.1"), "", true);
298 fail("test should throw an exception");
299 } catch (ApexException apEx) {
300 assertEquals("failed to unmarshal the apex model on engine DummyKey:0.0.1", apEx.getMessage());
304 worker.updateModel(new AxArtifactKey("DummyKey", "0.0.1"), "I am not an Apex model", true);
305 fail("test should throw an exception");
306 } catch (ApexException apEx) {
307 assertEquals("failed to unmarshal the apex model on engine DummyKey:0.0.1", apEx.getMessage());
311 worker.updateModel(new AxArtifactKey("DummyKey", "0.0.1"), simpleModelString, true);
312 fail("test should throw an exception");
313 } catch (ApexException apEx) {
314 assertEquals("engine key DummyKey:0.0.1 does not match the keyWorker:0.0.1 of this engine",
319 worker.updateModel(new AxArtifactKey("DummyKey", "0.0.1"), (AxPolicyModel) null, true);
320 fail("test should throw an exception");
321 } catch (ApexException apEx) {
322 assertEquals("engine key DummyKey:0.0.1 does not match the keyWorker:0.0.1 of this engine",
327 worker.updateModel(new AxArtifactKey("DummyKey", "0.0.1"), simpleModel, true);
328 fail("test should throw an exception");
329 } catch (ApexException apEx) {
330 assertEquals("engine key DummyKey:0.0.1 does not match the keyWorker:0.0.1 of this engine",
336 public void testApexImplModelWIthModel() throws ApexException {
337 Registry.newRegistry();
338 Registry.register(ApexPolicyStatisticsManager.REG_APEX_PDP_POLICY_COUNTER, new ApexPolicyStatisticsManager());
339 BlockingQueue<ApexEvent> eventQueue = new LinkedBlockingQueue<>();
341 EngineWorker worker = new EngineWorker(new AxArtifactKey("Worker", "0.0.1"), eventQueue, atFactory);
342 assertEquals("Worker:0.0.1", worker.getKey().getId());
345 worker.updateModel(worker.getKey(), simpleModelString, false);
346 } catch (ApexException apEx) {
347 fail("test should not throw an exception");
350 eventQueue.add(new ApexEvent("SomeEvent", "0.0.1", "the.event.namespace", "EventSource", "EventTarget"));
353 worker.updateModel(worker.getKey(), differentModelString, false);
354 fail("test should throw an exception");
355 } catch (ApexException apEx) {
356 assertEquals("apex model update failed, supplied model with key \"SmallModelDifferent:0.0.1\" is not a "
357 + "compatible model update " + "from the existing engine model with key \"SmallModel:0.0.1\"",
362 worker.updateModel(worker.getKey(), differentModelString, true);
363 } catch (ApexException apEx) {
364 fail("test should not throw an exception");
368 worker.updateModel(worker.getKey(), simpleModelString, true);
369 } catch (ApexException apEx) {
370 fail("test should not throw an exception");
373 String runtimeInfo = worker.getRuntimeInfo(worker.getEngineKeys().iterator().next());
374 assertEquals("{\"TimeStamp\":", runtimeInfo.replaceAll("\\s+", "").substring(0, 13));
376 assertEquals(AxEngineState.STOPPED, worker.getState());
379 assertEquals(AxEngineState.READY, worker.getState());
381 String status = worker.getStatus(worker.getEngineKeys().iterator().next());
382 assertEquals("{\"apexEngineModel\":{\"key\":", status.replaceAll("\\s+", "").substring(0, 26));
384 assertTrue(worker.isStarted());
385 assertTrue(worker.isStarted(worker.getEngineKeys().iterator().next()));
386 assertFalse(worker.isStopped());
387 assertFalse(worker.isStopped(worker.getEngineKeys().iterator().next()));
390 worker.start(worker.getEngineKeys().iterator().next());
391 fail("test should throw an exception");
392 } catch (ApexException apEx) {
393 assertEquals("apex engine for engine key Worker:0.0.1 is already running with state READY",
399 fail("test should throw an exception");
400 } catch (ApexException apEx) {
401 assertEquals("apex engine for engine key Worker:0.0.1 is already running with state READY",
406 worker.stop(worker.getEngineKeys().iterator().next());
407 } catch (ApexException apEx) {
408 fail("test should not throw an exception");
412 worker.start(worker.getEngineKeys().iterator().next());
413 } catch (ApexException apEx) {
414 fail("test should not throw an exception");
419 } catch (ApexException apEx) {
420 fail("test should not throw an exception");
425 } catch (ApexException apEx) {
426 fail("test should not throw an exception");
433 worker.clear(worker.getEngineKeys().iterator().next());
434 fail("test should throw an exception");
435 } catch (ApexException apEx) {
436 assertEquals("clear()<-Worker:0.0.1,READY, cannot clear engine, engine is not stopped", apEx.getMessage());
440 worker.stop(worker.getEngineKeys().iterator().next());
441 worker.clear(worker.getEngineKeys().iterator().next());
442 } catch (ApexException apEx) {
443 fail("test should not throw an exception");
448 } catch (ApexException apEx) {
449 fail("test should not throw an exception");
452 assertNotNull(worker.getApexModelKey());
454 final ApexPolicyStatisticsManager policyCounter = ApexPolicyStatisticsManager.getInstanceFromRegistry();
455 assertNotNull(policyCounter);
456 assertEquals(policyCounter.getPolicyExecutedCount(),
457 policyCounter.getPolicyExecutedFailCount() + policyCounter.getPolicyExecutedSuccessCount());
458 policyCounter.resetAllStatistics();
459 assertEquals(0, policyCounter.getPolicyExecutedCount());
460 assertEquals(0, policyCounter.getPolicyExecutedFailCount());
461 assertEquals(0, policyCounter.getPolicyExecutedSuccessCount());
462 assertEquals(0, policyCounter.getPolicyDeployCount());
463 assertEquals(0, policyCounter.getPolicyDeployFailCount());
464 assertEquals(0, policyCounter.getPolicyDeploySuccessCount());