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