2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2018 Ericsson. All rights reserved.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.apex.service.engine.runtime.impl;
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertFalse;
25 import static org.junit.Assert.assertNotNull;
26 import static org.junit.Assert.assertNull;
27 import static org.junit.Assert.assertTrue;
28 import static org.junit.Assert.fail;
30 import java.io.ByteArrayInputStream;
31 import java.io.IOException;
32 import java.util.concurrent.BlockingQueue;
33 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.model.utilities.TextFileUtils;
57 import org.onap.policy.apex.service.engine.event.ApexEvent;
58 import org.onap.policy.common.parameters.ParameterService;
61 * Test the engine worker class.
63 public class EngineWorkerTest {
64 private final ApplicationThreadFactory atFactory = new ApplicationThreadFactory("apex-engine-service", 512);
66 private static String simpleModelString;
67 private static String mfpModelString;
68 private static AxPolicyModel simpleModel;
71 * Read the models into strings.
73 * @throws IOException on model reading errors
74 * @throws ApexModelException on model reading exceptions
77 public static void readSimpleModel() throws IOException, ApexModelException {
78 simpleModelString = TextFileUtils
79 .getTextFileAsString("src/test/resources/policymodels/SamplePolicyModelJAVASCRIPT.json");
81 mfpModelString = TextFileUtils.getTextFileAsString("src/test/resources/policymodels/MyFirstPolicyModel.json");
83 final ApexModelReader<AxPolicyModel> modelReader = new ApexModelReader<>(AxPolicyModel.class);
84 simpleModel = modelReader.read(new ByteArrayInputStream(simpleModelString.getBytes()));
88 * Initialize default parameters.
91 public static void initializeDefaultParameters() {
92 ParameterService.clear();
93 final SchemaParameters schemaParameters = new SchemaParameters();
94 schemaParameters.setName(ContextParameterConstants.SCHEMA_GROUP_NAME);
95 ParameterService.register(schemaParameters);
97 final ContextParameters contextParameters = new ContextParameters();
98 contextParameters.setName(ContextParameterConstants.MAIN_GROUP_NAME);
99 ParameterService.register(contextParameters);
101 final DistributorParameters distributorParameters = new DistributorParameters();
102 distributorParameters.setName(ContextParameterConstants.DISTRIBUTOR_GROUP_NAME);
103 ParameterService.register(distributorParameters);
105 final LockManagerParameters lockManagerParameters = new LockManagerParameters();
106 lockManagerParameters.setName(ContextParameterConstants.LOCKING_GROUP_NAME);
107 ParameterService.register(lockManagerParameters);
109 final PersistorParameters persistorParameters = new PersistorParameters();
110 persistorParameters.setName(ContextParameterConstants.PERSISTENCE_GROUP_NAME);
111 ParameterService.register(persistorParameters);
113 final EngineParameters engineParameters = new EngineParameters();
114 engineParameters.setName(EngineParameterConstants.MAIN_GROUP_NAME);
115 ExecutorParameters jsExecutorParameters = new ExecutorParameters();
116 jsExecutorParameters.setName("JAVASCRIPT");
117 jsExecutorParameters.setTaskSelectionExecutorPluginClass(
118 "org.onap.policy.apex.service.engine.runtime.impl.DummyTse");
119 jsExecutorParameters.setTaskExecutorPluginClass("org.onap.policy.apex.service.engine.runtime.impl.DummyTe");
120 jsExecutorParameters.setStateFinalizerExecutorPluginClass(
121 "org.onap.policy.apex.service.engine.runtime.impl.DummySfe");
122 engineParameters.getExecutorParameterMap().put("JAVASCRIPT", jsExecutorParameters);
123 ExecutorParameters mvvelExecutorParameters = new ExecutorParameters();
124 mvvelExecutorParameters.setName("MVEL");
125 mvvelExecutorParameters.setTaskSelectionExecutorPluginClass(
126 "org.onap.policy.apex.service.engine.runtime.impl.DummyTse");
127 mvvelExecutorParameters.setTaskExecutorPluginClass("org.onap.policy.apex.service.engine.runtime.impl.DummyTe");
128 mvvelExecutorParameters.setStateFinalizerExecutorPluginClass(
129 "org.onap.policy.apex.service.engine.runtime.impl.DummySfe");
130 engineParameters.getExecutorParameterMap().put("MVEL", jsExecutorParameters);
131 ParameterService.register(engineParameters);
135 * Teardown default parameters.
138 public static void teardownDefaultParameters() {
139 ParameterService.deregister(ContextParameterConstants.SCHEMA_GROUP_NAME);
140 ParameterService.deregister(ContextParameterConstants.DISTRIBUTOR_GROUP_NAME);
141 ParameterService.deregister(ContextParameterConstants.LOCKING_GROUP_NAME);
142 ParameterService.deregister(ContextParameterConstants.PERSISTENCE_GROUP_NAME);
143 ParameterService.deregister(ContextParameterConstants.MAIN_GROUP_NAME);
144 ParameterService.deregister(EngineParameterConstants.MAIN_GROUP_NAME);
148 public void cleardownTest() {
149 ModelService.clear();
153 public void testEngineWorker() {
154 BlockingQueue<ApexEvent> eventQueue = new LinkedBlockingQueue<>();
156 EngineWorker worker = new EngineWorker(new AxArtifactKey("Worker", "0.0.1"), eventQueue, atFactory);
158 worker.registerActionListener(null, null);
159 worker.registerActionListener("DummyListener", null);
160 worker.registerActionListener(null, new DummyApexEventListener());
162 worker.registerActionListener("DummyListener", new DummyApexEventListener());
163 worker.deregisterActionListener(null);
164 worker.deregisterActionListener("DummyListener");
167 worker.getEngineServiceEventInterface();
168 fail("test should throw an exception");
169 } catch (Exception apEx) {
170 assertEquals("getEngineServiceEventInterface() call is not allowed on an Apex Engine Worker",
175 worker.startPeriodicEvents(100000);
176 fail("test should throw an exception");
177 } catch (Exception apEx) {
178 assertEquals("startPeriodicEvents() call is not allowed on an Apex Engine Worker", apEx.getMessage());
182 worker.stopPeriodicEvents();
183 fail("test should throw an exception");
184 } catch (Exception apEx) {
185 assertEquals("stopPeriodicEvents() call is not allowed on an Apex Engine Worker", apEx.getMessage());
188 assertEquals("Worker:0.0.1", worker.getEngineKeys().iterator().next().getId());
190 assertNull(worker.getApexModelKey());
192 String runtimeInfo = worker.getRuntimeInfo(worker.getEngineKeys().iterator().next());
193 assertEquals("{\n \"TimeStamp\":", runtimeInfo.substring(0, 16));
195 assertEquals(AxEngineState.STOPPED, worker.getState());
197 String status = worker.getStatus(worker.getEngineKeys().iterator().next());
198 assertEquals("{\n \"apexEngineModel\" :", status.substring(0, 24));
200 assertFalse(worker.isStarted());
201 assertFalse(worker.isStarted(null));
202 assertFalse(worker.isStarted(new AxArtifactKey("DummyKey", "0.0.1")));
203 assertFalse(worker.isStarted(worker.getEngineKeys().iterator().next()));
204 assertTrue(worker.isStopped());
205 assertTrue(worker.isStopped(null));
206 assertTrue(worker.isStopped(new AxArtifactKey("DummyKey", "0.0.1")));
207 assertTrue(worker.isStopped(worker.getEngineKeys().iterator().next()));
210 worker.start(new AxArtifactKey("DummyKey", "0.0.1"));
211 fail("test should throw an exception");
212 } catch (ApexException apEx) {
213 assertEquals("engine key DummyKey:0.0.1 does not match the keyWorker:0.0.1 of this engine",
218 worker.start(worker.getEngineKeys().iterator().next());
219 fail("test should throw an exception");
220 } catch (ApexException apEx) {
221 assertEquals("start()<-Worker:0.0.1,STOPPED, cannot start engine, engine has not been initialized, "
222 + "its model is not loaded", apEx.getMessage());
227 fail("test should throw an exception");
228 } catch (ApexException apEx) {
229 assertEquals("start()<-Worker:0.0.1,STOPPED, cannot start engine, "
230 + "engine has not been initialized, its model is not loaded", apEx.getMessage());
234 worker.stop(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.stop(worker.getEngineKeys().iterator().next());
243 } catch (ApexException apEx) {
244 fail("test should not throw an exception");
249 } catch (ApexException apEx) {
250 fail("test should not throw an exception");
254 worker.clear(new AxArtifactKey("DummyKey", "0.0.1"));
255 fail("test should throw an exception");
256 } catch (ApexException apEx) {
257 assertEquals("engine key DummyKey:0.0.1 does not match the keyWorker:0.0.1 of this engine",
262 worker.clear(worker.getEngineKeys().iterator().next());
263 } catch (ApexException apEx) {
264 fail("test should not throw an exception");
269 } catch (ApexException apEx) {
270 fail("test should not throw an exception");
274 worker.updateModel(new AxArtifactKey("DummyKey", "0.0.1"), "", true);
275 fail("test should throw an exception");
276 } catch (ApexException apEx) {
277 assertEquals("failed to unmarshal the apex model on engine DummyKey:0.0.1", apEx.getMessage());
281 worker.updateModel(new AxArtifactKey("DummyKey", "0.0.1"), "I am not an Apex model", true);
282 fail("test should throw an exception");
283 } catch (ApexException apEx) {
284 assertEquals("failed to unmarshal the apex model on engine DummyKey:0.0.1", apEx.getMessage());
288 worker.updateModel(new AxArtifactKey("DummyKey", "0.0.1"), simpleModelString, true);
289 fail("test should throw an exception");
290 } catch (ApexException apEx) {
291 assertEquals("engine key DummyKey:0.0.1 does not match the keyWorker:0.0.1 of this engine",
296 worker.updateModel(new AxArtifactKey("DummyKey", "0.0.1"), (AxPolicyModel) null, true);
297 fail("test should throw an exception");
298 } catch (ApexException apEx) {
299 assertEquals("engine key DummyKey:0.0.1 does not match the keyWorker:0.0.1 of this engine",
304 worker.updateModel(new AxArtifactKey("DummyKey", "0.0.1"), simpleModel, true);
305 fail("test should throw an exception");
306 } catch (ApexException apEx) {
307 assertEquals("engine key DummyKey:0.0.1 does not match the keyWorker:0.0.1 of this engine",
314 public void testApexImplModelWIthModel() throws ApexException {
315 BlockingQueue<ApexEvent> eventQueue = new LinkedBlockingQueue<>();
317 EngineWorker worker = new EngineWorker(new AxArtifactKey("Worker", "0.0.1"), eventQueue, atFactory);
318 assertEquals("Worker:0.0.1", worker.getKey().getId());
321 worker.updateModel(worker.getKey(), simpleModelString, false);
322 } catch (ApexException apEx) {
323 fail("test should not throw an exception");
326 eventQueue.add(new ApexEvent("SomeEvent", "0.0.1", "the.event.namespace", "EventSource", "EventTarget"));
329 worker.updateModel(worker.getKey(), mfpModelString, false);
330 fail("test should throw an exception");
331 } catch (ApexException apEx) {
332 assertEquals("apex model update failed, supplied model with key \"MyFirstPolicyModel:0.0.1\" is not a "
333 + "compatible model update "
334 + "from the existing engine model with key \"SamplePolicyModelJAVASCRIPT:0.0.1\"",
339 worker.updateModel(worker.getKey(), mfpModelString, true);
340 } catch (ApexException apEx) {
341 fail("test should not throw an exception");
345 worker.updateModel(worker.getKey(), simpleModelString, true);
346 } catch (ApexException apEx) {
347 fail("test should not throw an exception");
350 String runtimeInfo = worker.getRuntimeInfo(worker.getEngineKeys().iterator().next());
351 assertEquals("{\n \"TimeStamp\":", runtimeInfo.substring(0, 16));
353 assertEquals(AxEngineState.STOPPED, worker.getState());
356 assertEquals(AxEngineState.READY, worker.getState());
358 String status = worker.getStatus(worker.getEngineKeys().iterator().next());
359 assertEquals("{\n \"apexEngineModel\" :", status.substring(0, 24));
361 assertTrue(worker.isStarted());
362 assertTrue(worker.isStarted(worker.getEngineKeys().iterator().next()));
363 assertFalse(worker.isStopped());
364 assertFalse(worker.isStopped(worker.getEngineKeys().iterator().next()));
367 worker.start(worker.getEngineKeys().iterator().next());
368 fail("test should throw an exception");
369 } catch (ApexException apEx) {
370 assertEquals("apex engine for engine key Worker:0.0.1 is already running with state READY",
376 fail("test should throw an exception");
377 } catch (ApexException apEx) {
378 assertEquals("apex engine for engine key Worker:0.0.1 is already running with state READY",
383 worker.stop(worker.getEngineKeys().iterator().next());
384 } catch (ApexException apEx) {
385 fail("test should not throw an exception");
389 worker.start(worker.getEngineKeys().iterator().next());
390 } catch (ApexException apEx) {
391 fail("test should not throw an exception");
396 } catch (ApexException apEx) {
397 fail("test should not throw an exception");
402 } catch (ApexException apEx) {
403 fail("test should not throw an exception");
410 worker.clear(worker.getEngineKeys().iterator().next());
411 fail("test should throw an exception");
412 } catch (ApexException apEx) {
413 assertEquals("clear()<-Worker:0.0.1,READY, cannot clear engine, engine is not stopped", apEx.getMessage());
417 worker.stop(worker.getEngineKeys().iterator().next());
418 worker.clear(worker.getEngineKeys().iterator().next());
419 } catch (ApexException apEx) {
420 fail("test should not throw an exception");
425 } catch (ApexException apEx) {
426 fail("test should not throw an exception");
429 assertNotNull(worker.getApexModelKey());