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