b3e68b2be839e283949835c0c51b1100440393cf
[policy/apex-pdp.git] / testsuites / integration / integration-executor-test / src / test / java / org / onap / policy / apex / testsuites / integration / executor / engine / TestApexEngine.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
4  *  Modifications Copyright (C) 2019-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
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.apex.testsuites.integration.executor.engine;
23
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;
28
29 import java.io.IOException;
30 import java.util.Date;
31 import java.util.HashMap;
32 import java.util.Map;
33 import java.util.concurrent.TimeUnit;
34
35 import org.onap.policy.apex.core.engine.EngineParameters;
36 import org.onap.policy.apex.core.engine.engine.ApexEngine;
37 import org.onap.policy.apex.core.engine.engine.impl.ApexEngineFactory;
38 import org.onap.policy.apex.core.engine.event.EnEvent;
39 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
40 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
41 import org.onap.policy.apex.model.enginemodel.concepts.AxEngineState;
42 import org.onap.policy.apex.model.eventmodel.concepts.AxEvent;
43 import org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel;
44 import org.onap.policy.apex.testsuites.integration.common.model.SampleDomainModelFactory;
45 import org.slf4j.ext.XLogger;
46 import org.slf4j.ext.XLoggerFactory;
47
48 public class TestApexEngine {
49     // Logger for this class
50     private static final XLogger logger = XLoggerFactory.getXLogger(TestApexEngine.class);
51
52     /**
53      * Instantiates a new test apex engine.
54      *
55      * @param axLogicExecutorType the type of logic executor to use to construct the sample policy model for this test
56      * @throws ApexException the apex exception
57      * @throws InterruptedException the interrupted exception
58      * @throws IOException Signals that an I/O exception has occurred.
59      */
60     public TestApexEngine(final String axLogicExecutorType,
61             final EngineParameters parameters) throws ApexException, InterruptedException, IOException {
62         logger.debug("Running TestApexEngine test for + " + axLogicExecutorType + "logic . . .");
63
64         final AxPolicyModel apexPolicyModel = new SampleDomainModelFactory().getSamplePolicyModel(axLogicExecutorType);
65         assertNotNull(apexPolicyModel);
66         final AxArtifactKey key = new AxArtifactKey("TestApexEngine", "0.0.1");
67
68         final ApexEngine apexEngine = new ApexEngineFactory().createApexEngine(key);
69         final TestApexActionListener listener = new TestApexActionListener("Test");
70         apexEngine.addEventListener("listener", listener);
71         apexEngine.updateModel(apexPolicyModel, false);
72         apexEngine.start();
73
74         for (final AxEvent axEvent : apexPolicyModel.getEvents().getEventMap().values()) {
75             final EnEvent event = apexEngine.createEvent(axEvent.getKey());
76
77             final Date aDate = new Date(1433453067123L);
78             final Map<String, Object> eventDataMap = new HashMap<String, Object>();
79             eventDataMap.put("TestSlogan", "This is a test slogan for event " + event.getName());
80             eventDataMap.put("TestMatchCase", (byte) 123);
81             eventDataMap.put("TestTimestamp", aDate.getTime());
82             eventDataMap.put("TestTemperature", 34.5445667);
83
84             event.putAll(eventDataMap);
85
86             apexEngine.handleEvent(event);
87         }
88
89         EnEvent result = listener.getResult(false);
90         logger.debug("result 1 is:" + result);
91         checkResult(result);
92         result = listener.getResult(false);
93         logger.debug("result 2 is:" + result);
94         checkResult(result);
95
96         final Map<AxArtifactKey, Map<String, Object>> apexContext = apexEngine.getEngineContext();
97         assertNotNull(apexContext);
98         apexEngine.stop();
99
100         await().atMost(3L, TimeUnit.SECONDS).until(() -> AxEngineState.STOPPED.equals(apexEngine.getState()));
101     }
102
103     /**
104      * Check result.
105      *
106      * @param result the result
107      */
108     private void checkResult(final EnEvent result) {
109         if (result.getExceptionMessage() == null) {
110             assertTrue(result.getName().equals("Event0004") || result.getName().equals("Event0104"));
111
112             assertTrue(((String) result.get("TestSlogan")).startsWith("This is a test slogan for event "));
113             assertTrue(((String) result.get("TestSlogan")).contains(result.getName().substring(0, 8)));
114
115             assertEquals((byte) 123, result.get("TestMatchCase"));
116             assertEquals(34.5445667, result.get("TestTemperature"));
117             assertTrue(
118                     (Byte) result.get("TestMatchCaseSelected") >= 0 && (Byte) result.get("TestMatchCaseSelected") <= 4);
119             assertTrue((Byte) result.get("TestEstablishCaseSelected") >= 0
120                     && (Byte) result.get("TestEstablishCaseSelected") <= 4);
121             assertTrue((Byte) result.get("TestDecideCaseSelected") >= 0
122                     && (Byte) result.get("TestDecideCaseSelected") <= 4);
123             assertTrue((Byte) result.get("TestActCaseSelected") >= 0 && (Byte) result.get("TestActCaseSelected") <= 4);
124         } else {
125             assertTrue(result.getName().equals("Event0001") || result.getName().equals("Event0104"));
126
127             assertTrue(((String) result.get("TestSlogan")).startsWith("This is a test slogan for event "));
128             assertTrue(((String) result.get("TestSlogan")).contains(result.getName().substring(0, 8)));
129
130             assertEquals((byte) 123, result.get("TestMatchCase"));
131             assertEquals(34.5445667, result.get("TestTemperature"));
132         }
133     }
134 }