2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * Modifications Copyright (C) 2020 Nordix Foundation.
5 * Modifications Copyright (C) 2020 Bell Canada. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 * SPDX-License-Identifier: Apache-2.0
20 * ============LICENSE_END=========================================================
23 package org.onap.policy.apex.testsuites.integration.uservice.adapt.jms;
25 import static org.awaitility.Awaitility.await;
26 import static org.junit.Assert.assertEquals;
28 import java.io.IOException;
29 import java.nio.file.Paths;
30 import java.util.ArrayList;
31 import java.util.Arrays;
32 import java.util.List;
33 import java.util.concurrent.TimeUnit;
34 import javax.jms.JMSException;
35 import org.apache.activemq.ActiveMQConnectionFactory;
36 import org.apache.activemq.broker.BrokerPlugin;
37 import org.apache.activemq.broker.BrokerService;
38 import org.apache.activemq.security.AuthenticationUser;
39 import org.apache.activemq.security.SimpleAuthenticationPlugin;
40 import org.junit.AfterClass;
41 import org.junit.Before;
42 import org.junit.BeforeClass;
43 import org.junit.Test;
44 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
45 import org.onap.policy.apex.service.engine.main.ApexMain;
46 import org.slf4j.ext.XLogger;
47 import org.slf4j.ext.XLoggerFactory;
50 * The Class TestJms2Jms.
52 public class TestJms2Jms {
53 public static final String PORT = "5445";
54 public static final String HOST = "localhost";
55 public static final String JMS_TOPIC_APEX_IN = "jms/topic/apexIn";
56 public static final String JMS_TOPIC_APEX_OUT = "jms/topic/apexOut";
58 private static final String GROUP_ROLE = "guests";
59 private static final String PACKAGE_NAME = "org.onap.policy.apex.testsuites.integration.common.testclasses";
60 private static final String USERNAME = "guest";
61 private static final String PASSWORD = "IAmAGuest";
62 private static final String URL = "tcp://" + HOST + ":" + PORT;
64 private static final String DATA_PARENT_DIR = Paths.get("target", "activemq-data").toString();
66 private static final XLogger LOGGER = XLoggerFactory.getXLogger(TestJms2Jms.class);
68 private static final int EVENT_COUNT = 100;
69 private static final int EVENT_INTERVAL = 20;
71 private static BrokerService broker;
73 public static ActiveMQConnectionFactory connectionFactory;
76 * Setup embedded jms server.
78 * @throws Exception the exception
81 public static void setupEmbeddedJmsServer() throws Exception {
82 final ArrayList<BrokerPlugin> plugins = new ArrayList<BrokerPlugin>();
83 final BrokerPlugin authenticationPlugin = getAuthenticationBrokerPlugin();
84 plugins.add(authenticationPlugin);
86 broker = new BrokerService();
87 broker.setUseJmx(false);
88 broker.setPersistent(false);
89 broker.addConnector(URL);
90 broker.setDeleteAllMessagesOnStartup(true);
91 broker.setPlugins(plugins.toArray(new BrokerPlugin[0]));
92 broker.setDataDirectory(DATA_PARENT_DIR);
94 broker.waitUntilStarted();
95 connectionFactory = new ActiveMQConnectionFactory(URL);
96 connectionFactory.setTrustedPackages(Arrays.asList(PACKAGE_NAME));
100 * Clear relative file root environment variable.
103 public void clearRelativeFileRoot() {
104 System.clearProperty("APEX_RELATIVE_FILE_ROOT");
108 * Gets the authentication broker plugin.
110 * @return the authentication broker plugin
112 private static BrokerPlugin getAuthenticationBrokerPlugin() {
113 final List<AuthenticationUser> users = new ArrayList<AuthenticationUser>();
114 users.add(new AuthenticationUser(USERNAME, PASSWORD, GROUP_ROLE));
115 final SimpleAuthenticationPlugin authenticationPlugin = new SimpleAuthenticationPlugin(users);
116 return authenticationPlugin;
120 * Shutdown embedded jms server.
122 * @throws IOException Signals that an I/O exception has occurred.
125 public static void shutdownEmbeddedJmsServer() throws IOException {
127 if (broker != null) {
130 } catch (final Exception e) {
131 LOGGER.warn("Failed to stop JMS server", e);
137 * Test jms object events.
139 * @throws ApexException the apex exception
140 * @throws JMSException the JMS exception
143 public void testJmsObjectEvents() throws ApexException, JMSException {
144 final String[] args = {"-rfr", "target", "-p", "target/examples/config/JMS/JMS2JMSObjectEvent.json"};
145 testJmsEvents(args, true);
149 * Test jms json events.
151 * @throws ApexException the apex exception
152 * @throws JMSException the JMS exception
155 public void testJmsJsonEvents() throws ApexException, JMSException {
156 final String[] args = {"-rfr", "target", "-p", "target/examples/config/JMS/JMS2JMSJsonEvent.json"};
157 testJmsEvents(args, false);
163 * @param args the args
164 * @param sendObjects the send objects
165 * @throws ApexException the apex exception
166 * @throws JMSException the JMS exception
168 private void testJmsEvents(final String[] args, final Boolean sendObjects) throws ApexException, JMSException {
169 final JmsEventSubscriber subscriber =
170 new JmsEventSubscriber(JMS_TOPIC_APEX_OUT, connectionFactory, USERNAME, PASSWORD);
172 final JmsEventProducer producer = new JmsEventProducer(JMS_TOPIC_APEX_IN, connectionFactory, USERNAME, PASSWORD,
173 EVENT_COUNT, sendObjects, EVENT_INTERVAL);
175 final ApexMain apexMain = new ApexMain(args);
177 await().atMost(3L, TimeUnit.SECONDS).until(() -> apexMain.isAlive());
179 producer.sendEvents();
181 await().atMost(10L, TimeUnit.SECONDS).until(() -> producer.getEventsSentCount() >= EVENT_COUNT - 1);
182 await().atMost(10L, TimeUnit.SECONDS).until(() -> subscriber.getEventsReceivedCount() >= EVENT_COUNT - 1);
185 subscriber.shutdown();
188 assertEquals(EVENT_COUNT, producer.getEventsSentCount());
189 assertEquals(producer.getEventsSentCount(), subscriber.getEventsReceivedCount());