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.awaitility.Awaitility.await;
25 import static org.junit.Assert.assertEquals;
27 import java.io.IOException;
28 import java.nio.file.Paths;
29 import java.util.ArrayList;
30 import java.util.Arrays;
31 import java.util.List;
32 import java.util.concurrent.TimeUnit;
33 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.Test;
43 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
44 import org.onap.policy.apex.service.engine.main.ApexMain;
45 import org.slf4j.ext.XLogger;
46 import org.slf4j.ext.XLoggerFactory;
49 * The Class TestJms2Jms.
51 public class TestJms2Jms {
52 public static final String PORT = "5445";
53 public static final String HOST = "localhost";
54 public static final String JMS_TOPIC_APEX_IN = "jms/topic/apexIn";
55 public static final String JMS_TOPIC_APEX_OUT = "jms/topic/apexOut";
57 private static final String GROUP_ROLE = "guests";
58 private static final String PACKAGE_NAME = "org.onap.policy.apex.testsuites.integration.common.testclasses";
59 private static final String USERNAME = "guest";
60 private static final String PASSWORD = "IAmAGuest";
61 private static final String URL = "tcp://" + HOST + ":" + PORT;
63 private static final String DATA_PARENT_DIR = Paths.get("target", "activemq-data").toString();
65 private static final XLogger LOGGER = XLoggerFactory.getXLogger(TestJms2Jms.class);
67 private static final int EVENT_COUNT = 100;
68 private static final int EVENT_INTERVAL = 20;
70 private static BrokerService broker;
72 public static ActiveMQConnectionFactory connectionFactory;
75 * Setup embedded jms server.
77 * @throws Exception the exception
80 public static void setupEmbeddedJmsServer() throws Exception {
81 final ArrayList<BrokerPlugin> plugins = new ArrayList<BrokerPlugin>();
82 final BrokerPlugin authenticationPlugin = getAuthenticationBrokerPlugin();
83 plugins.add(authenticationPlugin);
85 broker = new BrokerService();
86 broker.setUseJmx(false);
87 broker.setPersistent(false);
88 broker.addConnector(URL);
89 broker.setDeleteAllMessagesOnStartup(true);
90 broker.setPlugins(plugins.toArray(new BrokerPlugin[0]));
91 broker.setDataDirectory(DATA_PARENT_DIR);
93 broker.waitUntilStarted();
94 connectionFactory = new ActiveMQConnectionFactory(URL);
95 connectionFactory.setTrustedPackages(Arrays.asList(PACKAGE_NAME));
99 * Clear relative file root environment variable.
102 public void clearRelativeFileRoot() {
103 System.clearProperty("APEX_RELATIVE_FILE_ROOT");
107 * Gets the authentication broker plugin.
109 * @return the authentication broker plugin
111 private static BrokerPlugin getAuthenticationBrokerPlugin() {
112 final List<AuthenticationUser> users = new ArrayList<AuthenticationUser>();
113 users.add(new AuthenticationUser(USERNAME, PASSWORD, GROUP_ROLE));
114 final SimpleAuthenticationPlugin authenticationPlugin = new SimpleAuthenticationPlugin(users);
115 return authenticationPlugin;
119 * Shutdown embedded jms server.
121 * @throws IOException Signals that an I/O exception has occurred.
124 public static void shutdownEmbeddedJmsServer() throws IOException {
126 if (broker != null) {
129 } catch (final Exception e) {
130 LOGGER.warn("Failed to stop JMS server", e);
136 * Test jms object events.
138 * @throws ApexException the apex exception
139 * @throws JMSException the JMS exception
142 public void testJmsObjectEvents() throws ApexException, JMSException {
143 final String[] args = {"-rfr", "target", "-c", "target/examples/config/JMS/JMS2JMSObjectEvent.json"};
144 testJmsEvents(args, true);
148 * Test jms json events.
150 * @throws ApexException the apex exception
151 * @throws JMSException the JMS exception
154 public void testJmsJsonEvents() throws ApexException, JMSException {
155 final String[] args = {"-rfr", "target", "-c", "target/examples/config/JMS/JMS2JMSJsonEvent.json"};
156 testJmsEvents(args, false);
162 * @param args the args
163 * @param sendObjects the send objects
164 * @throws ApexException the apex exception
165 * @throws JMSException the JMS exception
167 private void testJmsEvents(final String[] args, final Boolean sendObjects) throws ApexException, JMSException {
168 final JmsEventSubscriber subscriber =
169 new JmsEventSubscriber(JMS_TOPIC_APEX_OUT, connectionFactory, USERNAME, PASSWORD);
171 final JmsEventProducer producer = new JmsEventProducer(JMS_TOPIC_APEX_IN, connectionFactory, USERNAME, PASSWORD,
172 EVENT_COUNT, sendObjects, EVENT_INTERVAL);
174 final ApexMain apexMain = new ApexMain(args);
176 await().atMost(3L, TimeUnit.SECONDS).until(() -> apexMain.isAlive());
178 producer.sendEvents();
180 await().atMost(10L, TimeUnit.SECONDS).until(() -> producer.getEventsSentCount() >= EVENT_COUNT - 1);
181 await().atMost(10L, TimeUnit.SECONDS).until(() -> subscriber.getEventsReceivedCount() >= EVENT_COUNT - 1);
184 subscriber.shutdown();
187 assertEquals(EVENT_COUNT, producer.getEventsSentCount());
188 assertEquals(producer.getEventsSentCount(), subscriber.getEventsReceivedCount());