2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * Modifications Copyright (C) 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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.apex.testsuites.integration.uservice.adapt.jms;
24 import static org.junit.Assert.assertEquals;
26 import java.io.IOException;
27 import java.nio.file.Paths;
28 import java.util.ArrayList;
29 import java.util.Arrays;
30 import java.util.List;
32 import javax.jms.JMSException;
34 import org.apache.activemq.ActiveMQConnectionFactory;
35 import org.apache.activemq.broker.BrokerPlugin;
36 import org.apache.activemq.broker.BrokerService;
37 import org.apache.activemq.security.AuthenticationUser;
38 import org.apache.activemq.security.SimpleAuthenticationPlugin;
39 import org.junit.AfterClass;
40 import org.junit.Before;
41 import org.junit.BeforeClass;
42 import org.junit.Ignore;
43 import org.junit.Test;
44 import org.onap.policy.apex.core.infrastructure.threading.ThreadUtilities;
45 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
46 import org.onap.policy.apex.service.engine.main.ApexMain;
47 import org.slf4j.ext.XLogger;
48 import org.slf4j.ext.XLoggerFactory;
51 * The Class TestJms2Jms.
53 public class TestJms2Jms {
54 public static final String PORT = "5445";
55 public static final String HOST = "localhost";
56 public static final String JMS_TOPIC_APEX_IN = "jms/topic/apexIn";
57 public static final String JMS_TOPIC_APEX_OUT = "jms/topic/apexOut";
59 private static final int SLEEP_TIME = 1500;
60 private static final String GROUP_ROLE = "guests";
61 private static final String PACKAGE_NAME = "org.onap.policy.apex.testsuites.integration.common.testclasses";
62 private static final String USERNAME = "guest";
63 private static final String PASSWORD = "IAmAGuest";
64 private static final String URL = "tcp://" + HOST + ":" + PORT;
66 private static final String DATA_PARENT_DIR = Paths.get("target", "activemq-data").toString();
68 private static final XLogger LOGGER = XLoggerFactory.getXLogger(TestJms2Jms.class);
70 private static final long MAX_TEST_LENGTH = 10000;
71 private static final int EVENT_COUNT = 100;
72 private static final int EVENT_INTERVAL = 20;
74 private static BrokerService broker;
76 public static ActiveMQConnectionFactory connectionFactory;
79 * Setup embedded jms server.
81 * @throws Exception the exception
84 public static void setupEmbeddedJmsServer() throws Exception {
85 final ArrayList<BrokerPlugin> plugins = new ArrayList<BrokerPlugin>();
86 final BrokerPlugin authenticationPlugin = getAuthenticationBrokerPlugin();
87 plugins.add(authenticationPlugin);
89 broker = new BrokerService();
90 broker.setUseJmx(false);
91 broker.setPersistent(false);
92 broker.addConnector(URL);
93 broker.setDeleteAllMessagesOnStartup(true);
94 broker.setPlugins(plugins.toArray(new BrokerPlugin[0]));
95 broker.setDataDirectory(DATA_PARENT_DIR);
97 broker.waitUntilStarted();
98 connectionFactory = new ActiveMQConnectionFactory(URL);
99 connectionFactory.setTrustedPackages(Arrays.asList(PACKAGE_NAME));
103 * Clear relative file root environment variable.
106 public void clearRelativeFileRoot() {
107 System.clearProperty("APEX_RELATIVE_FILE_ROOT");
111 * Gets the authentication broker plugin.
113 * @return the authentication broker plugin
115 private static BrokerPlugin getAuthenticationBrokerPlugin() {
116 final List<AuthenticationUser> users = new ArrayList<AuthenticationUser>();
117 users.add(new AuthenticationUser(USERNAME, PASSWORD, GROUP_ROLE));
118 final SimpleAuthenticationPlugin authenticationPlugin = new SimpleAuthenticationPlugin(users);
119 return authenticationPlugin;
123 * Shutdown embedded jms server.
125 * @throws IOException Signals that an I/O exception has occurred.
128 public static void shutdownEmbeddedJmsServer() throws IOException {
130 if (broker != null) {
133 } catch (final Exception e) {
134 LOGGER.warn("Failed to stop JMS server", e);
140 * Test jms object events.
142 * @throws ApexException the apex exception
143 * @throws JMSException the JMS exception
147 public void testJmsObjectEvents() throws ApexException, JMSException {
148 final String[] args = {"-rfr", "target", "-c", "target/examples/config/JMS/JMS2JMSObjectEvent.json"};
149 testJmsEvents(args, true);
153 * Test jms json events.
155 * @throws ApexException the apex exception
156 * @throws JMSException the JMS exception
159 public void testJmsJsonEvents() throws ApexException, JMSException {
160 final String[] args = {"-rfr", "target", "-c", "target/examples/config/JMS/JMS2JMSJsonEvent.json"};
161 testJmsEvents(args, false);
167 * @param args the args
168 * @param sendObjects the send objects
169 * @throws ApexException the apex exception
170 * @throws JMSException the JMS exception
172 private void testJmsEvents(final String[] args, final Boolean sendObjects) throws ApexException, JMSException {
173 final JmsEventSubscriber subscriber =
174 new JmsEventSubscriber(JMS_TOPIC_APEX_OUT, connectionFactory, USERNAME, PASSWORD);
175 final JmsEventProducer producer = new JmsEventProducer(JMS_TOPIC_APEX_IN, connectionFactory, USERNAME, PASSWORD,
176 EVENT_COUNT, sendObjects, EVENT_INTERVAL);
178 final ApexMain apexMain = new ApexMain(args);
179 ThreadUtilities.sleep(3000);
181 producer.sendEvents();
183 final long testStartTime = System.currentTimeMillis();
185 while (isTimedOut(testStartTime) && subscriber.getEventsReceivedCount() < EVENT_COUNT) {
186 ThreadUtilities.sleep(EVENT_INTERVAL);
189 ThreadUtilities.sleep(SLEEP_TIME);
191 subscriber.shutdown();
193 ThreadUtilities.sleep(SLEEP_TIME);
195 assertEquals(EVENT_COUNT, producer.getEventsSentCount());
196 assertEquals(producer.getEventsSentCount(), subscriber.getEventsReceivedCount());
201 * Checks if is timed out.
203 * @param testStartTime the test start time
204 * @return true, if is timed out
206 private boolean isTimedOut(final long testStartTime) {
207 return System.currentTimeMillis() < testStartTime + MAX_TEST_LENGTH;