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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.apex.apps.uservice.test.engdep;
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertNotNull;
25 import static org.junit.Assert.fail;
27 import java.io.IOException;
28 import java.net.URISyntaxException;
29 import java.util.Date;
30 import java.util.HashMap;
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;
48 // CHECKSTYLE:OFF: checkstyle:magicNumber
51 * The Class EngDepMessagingTest.
53 * @author Liam Fallon (liam.fallon@ericsson.com)
55 public class EngDepMessagingTest {
56 // Logger for this class
57 private static final XLogger LOGGER = XLoggerFactory.getXLogger(EngDepMessagingTest.class);
59 private static final long MAX_START_WAIT = 10000; // 10 sec
64 * @throws Exception the exception
67 public void setUp() throws Exception {}
70 * Test EngDep messaging.
72 * @throws URISyntaxException the URI syntax exception
73 * @throws IOException Signals that an I/O exception has occurred.
74 * @throws ApexException the apex exception
77 public void testEngDepMessaging() throws URISyntaxException, IOException, ApexException {
78 LOGGER.debug("engine<-->deployment messaging test starting . . .");
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());
90 final EngineTestServer server = new EngineTestServer(parameters);
91 assertNotNull(server);
93 final Thread serverThread = new Thread(server);
95 final long starttime = System.currentTimeMillis();
96 while (server.isStarting() && System.currentTimeMillis() - starttime < MAX_START_WAIT) {
97 ThreadUtilities.sleep(100);
99 if (server.isStarting()) {
100 fail("Test server failed to start after " + MAX_START_WAIT + " ms");
103 final AxPolicyModel apexPolicyModel = new SampleDomainModelFactory().getSamplePolicyModel("MVEL");
105 final BatchDeployer deployer1 = new BatchDeployer("localhost", 58820);
106 assertNotNull(deployer1);
109 deployer1.deployModel(apexPolicyModel, false, false);
110 deployer1.stopEngines();
111 deployer1.startEngines();
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);
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);
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);
133 while (server.getTotalActionEventsReceived() < 2) {
134 ThreadUtilities.sleep(100);
136 ThreadUtilities.sleep(500);
138 assertEquals(server.getTotalActionEventsReceived(), 2);
141 deployer1.stopEngines();
144 // Test re-initialization of model
145 final BatchDeployer deployer2 = new BatchDeployer("localhost", 58820);
146 assertNotNull(deployer2);
149 deployer2.deployModel(apexPolicyModel, true, true);
150 deployer2.stopEngines();
151 deployer2.startEngines();
154 server.sendEvent(event0);
155 server.sendEvent(event1);
158 while (server.getTotalActionEventsReceived() < 4) {
159 ThreadUtilities.sleep(100);
161 ThreadUtilities.sleep(500);
163 assertEquals(server.getTotalActionEventsReceived(), 4);
166 deployer2.stopEngines();
170 LOGGER.debug("engine<-->deployment messaging test finished");
176 * @throws Exception the exception
179 public void tearDown() throws Exception {}