2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * Modifications Copyright (C) 2019-2020, 2024 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.testsuites.integration.executor.engine;
24 import static org.awaitility.Awaitility.await;
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertNotNull;
27 import static org.junit.Assert.assertTrue;
29 import java.util.Date;
30 import java.util.HashMap;
32 import java.util.concurrent.TimeUnit;
33 import org.onap.policy.apex.core.engine.engine.ApexEngine;
34 import org.onap.policy.apex.core.engine.engine.impl.ApexEngineFactory;
35 import org.onap.policy.apex.core.engine.event.EnEvent;
36 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
37 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
38 import org.onap.policy.apex.model.enginemodel.concepts.AxEngineState;
39 import org.onap.policy.apex.model.eventmodel.concepts.AxEvent;
40 import org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel;
41 import org.onap.policy.apex.testsuites.integration.common.model.SampleDomainModelFactory;
42 import org.slf4j.ext.XLogger;
43 import org.slf4j.ext.XLoggerFactory;
45 public class TestApexEngine {
46 // Logger for this class
47 private static final XLogger logger = XLoggerFactory.getXLogger(TestApexEngine.class);
50 * Instantiates a new test apex engine.
52 * @param axLogicExecutorType the type of logic executor to use to construct the sample policy model for this test
53 * @throws ApexException the apex exception
55 public TestApexEngine(final String axLogicExecutorType) throws ApexException {
56 logger.debug("Running TestApexEngine test for + {}logic . . .", axLogicExecutorType);
58 final AxPolicyModel apexPolicyModel = new SampleDomainModelFactory().getSamplePolicyModel(axLogicExecutorType);
59 assertNotNull(apexPolicyModel);
60 final AxArtifactKey key = new AxArtifactKey("TestApexEngine", "0.0.1");
62 final ApexEngine apexEngine = new ApexEngineFactory().createApexEngine(key);
63 final TestApexActionListener listener = new TestApexActionListener("Test");
64 apexEngine.addEventListener("listener", listener);
65 apexEngine.updateModel(apexPolicyModel, false);
68 for (final AxEvent axEvent : apexPolicyModel.getEvents().getEventMap().values()) {
69 final EnEvent event = apexEngine.createEvent(axEvent.getKey());
71 final Date aDate = new Date(1433453067123L);
72 final Map<String, Object> eventDataMap = new HashMap<String, Object>();
73 eventDataMap.put("TestSlogan", "This is a test slogan for event " + event.getName());
74 eventDataMap.put("TestMatchCase", (byte) 123);
75 eventDataMap.put("TestTimestamp", aDate.getTime());
76 eventDataMap.put("TestTemperature", 34.5445667);
78 event.putAll(eventDataMap);
80 apexEngine.handleEvent(event);
83 EnEvent result = listener.getResult(false);
84 logger.debug("result 1 is:" + result);
86 result = listener.getResult(false);
87 logger.debug("result 2 is:" + result);
90 final Map<AxArtifactKey, Map<String, Object>> apexContext = apexEngine.getEngineContext();
91 assertNotNull(apexContext);
94 await().atMost(3L, TimeUnit.SECONDS).until(() -> AxEngineState.STOPPED.equals(apexEngine.getState()));
100 * @param result the result
102 private void checkResult(final EnEvent result) {
103 if (result.getExceptionMessage() == null) {
104 assertTrue(result.getName().equals("Event0004") || result.getName().equals("Event0104"));
106 assertTrue(((String) result.get("TestSlogan")).startsWith("This is a test slogan for event "));
107 assertTrue(((String) result.get("TestSlogan")).contains(result.getName().substring(0, 8)));
109 assertEquals((byte) 123, result.get("TestMatchCase"));
110 assertEquals(34.5445667, result.get("TestTemperature"));
112 (Byte) result.get("TestMatchCaseSelected") >= 0 && (Byte) result.get("TestMatchCaseSelected") <= 4);
113 assertTrue((Byte) result.get("TestEstablishCaseSelected") >= 0
114 && (Byte) result.get("TestEstablishCaseSelected") <= 4);
115 assertTrue((Byte) result.get("TestDecideCaseSelected") >= 0
116 && (Byte) result.get("TestDecideCaseSelected") <= 4);
117 assertTrue((Byte) result.get("TestActCaseSelected") >= 0 && (Byte) result.get("TestActCaseSelected") <= 4);
119 assertTrue(result.getName().equals("Event0001") || result.getName().equals("Event0104"));
121 assertTrue(((String) result.get("TestSlogan")).startsWith("This is a test slogan for event "));
122 assertTrue(((String) result.get("TestSlogan")).contains(result.getName().substring(0, 8)));
124 assertEquals((byte) 123, result.get("TestMatchCase"));
125 assertEquals(34.5445667, result.get("TestTemperature"));