adc416dca226be29154bf367157f1a8a1cd7cf13
[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.apps.uservice.test.engdep;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertNotNull;
25 import static org.junit.Assert.fail;
26
27 import java.io.IOException;
28 import java.net.URISyntaxException;
29 import java.util.Date;
30 import java.util.HashMap;
31 import java.util.Map;
32
33 import org.junit.After;
34 import org.junit.Before;
35 import org.junit.Test;
36 import org.onap.policy.apex.core.deployment.BatchDeployer;
37 import org.onap.policy.apex.core.infrastructure.threading.ThreadUtilities;
38 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
39 import org.onap.policy.apex.model.basicmodel.service.ModelService;
40 import org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel;
41 import org.onap.policy.apex.plugins.executor.mvel.MVELExecutorParameters;
42 import org.onap.policy.apex.service.engine.event.ApexEvent;
43 import org.onap.policy.apex.service.parameters.engineservice.EngineServiceParameters;
44 import org.onap.policy.apex.test.common.model.SampleDomainModelFactory;
45 import org.slf4j.ext.XLogger;
46 import org.slf4j.ext.XLoggerFactory;
47
48 // CHECKSTYLE:OFF: checkstyle:magicNumber
49
50 /**
51  * The Class EngDepMessagingTest.
52  *
53  * @author Liam Fallon (liam.fallon@ericsson.com)
54  */
55 public class EngDepMessagingTest {
56     // Logger for this class
57     private static final XLogger LOGGER = XLoggerFactory.getXLogger(EngDepMessagingTest.class);
58
59     private static final long MAX_START_WAIT = 10000; // 10 sec
60
61     /**
62      * Sets the up.
63      *
64      * @throws Exception the exception
65      */
66     @Before
67     public void setUp() throws Exception {}
68
69     /**
70      * Test EngDep messaging.
71      *
72      * @throws URISyntaxException the URI syntax exception
73      * @throws IOException Signals that an I/O exception has occurred.
74      * @throws ApexException the apex exception
75      */
76     @Test
77     public void testEngDepMessaging() throws URISyntaxException, IOException, ApexException {
78         LOGGER.debug("engine<-->deployment messaging test starting . .  .");
79
80         ModelService.clear();
81
82         final EngineServiceParameters parameters = new EngineServiceParameters();
83         parameters.setName("EngDepMessagingTest");
84         parameters.setVersion("0.0.1");
85         parameters.setDeploymentPort(58820);
86         parameters.setInstanceCount(3);
87         parameters.setId(100);
88         parameters.getEngineParameters().getExecutorParameterMap().put("MVEL", new MVELExecutorParameters());
89
90         final EngineTestServer server = new EngineTestServer(parameters);
91         assertNotNull(server);
92
93         final Thread serverThread = new Thread(server);
94         serverThread.start();
95         final long starttime = System.currentTimeMillis();
96         while (server.isStarting() && System.currentTimeMillis() - starttime < MAX_START_WAIT) {
97             ThreadUtilities.sleep(100);
98         }
99         if (server.isStarting()) {
100             fail("Test server failed to start after " + MAX_START_WAIT + " ms");
101         }
102
103         final AxPolicyModel apexPolicyModel = new SampleDomainModelFactory().getSamplePolicyModel("MVEL");
104
105         final BatchDeployer deployer1 = new BatchDeployer("localhost", 58820);
106         assertNotNull(deployer1);
107
108         deployer1.init();
109         deployer1.deployModel(apexPolicyModel, false, false);
110         deployer1.stopEngines();
111         deployer1.startEngines();
112         deployer1.close();
113
114         // Send events
115         final Date testStartTime = new Date();
116         final Map<String, Object> eventDataMap = new HashMap<>();
117         eventDataMap.put("TestSlogan", "This is a test slogan");
118         eventDataMap.put("TestMatchCase", (byte) 123);
119         eventDataMap.put("TestTimestamp", testStartTime.getTime());
120         eventDataMap.put("TestTemperature", 34.5445667);
121
122         final ApexEvent event0 =
123                 new ApexEvent("Event0000", "0.0.1", "org.onap.policy.apex.domains.sample.events", "apex", "test");
124         event0.putAll(eventDataMap);
125         server.sendEvent(event0);
126
127         final ApexEvent event1 =
128                 new ApexEvent("Event0100", "0.0.1", "org.onap.policy.apex.domains.sample.events", "apex", "test");
129         event1.putAll(eventDataMap);
130         server.sendEvent(event1);
131
132         // Wait for results
133         while (server.getTotalActionEventsReceived() < 2) {
134             ThreadUtilities.sleep(100);
135         }
136         ThreadUtilities.sleep(500);
137
138         assertEquals(server.getTotalActionEventsReceived(), 2);
139
140         deployer1.init();
141         deployer1.stopEngines();
142         deployer1.close();
143
144         // Test re-initialization of model
145         final BatchDeployer deployer2 = new BatchDeployer("localhost", 58820);
146         assertNotNull(deployer2);
147
148         deployer2.init();
149         deployer2.deployModel(apexPolicyModel, true, true);
150         deployer2.stopEngines();
151         deployer2.startEngines();
152         deployer2.close();
153
154         server.sendEvent(event0);
155         server.sendEvent(event1);
156
157         // Wait for results
158         while (server.getTotalActionEventsReceived() < 4) {
159             ThreadUtilities.sleep(100);
160         }
161         ThreadUtilities.sleep(500);
162
163         assertEquals(server.getTotalActionEventsReceived(), 4);
164
165         deployer2.init();
166         deployer2.stopEngines();
167         deployer2.close();
168
169         server.stopServer();
170         LOGGER.debug("engine<-->deployment messaging test finished");
171     }
172
173     /**
174      * Tear down.
175      *
176      * @throws Exception the exception
177      */
178     @After
179     public void tearDown() throws Exception {}
180 }