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