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;
34 import javax.jms.JMSException;
36 import org.apache.activemq.ActiveMQConnectionFactory;
37 import org.apache.activemq.broker.BrokerPlugin;
38 import org.apache.activemq.broker.BrokerService;
39 import org.apache.activemq.security.AuthenticationUser;
40 import org.apache.activemq.security.SimpleAuthenticationPlugin;
41 import org.junit.AfterClass;
42 import org.junit.Before;
43 import org.junit.BeforeClass;
44 import org.junit.Test;
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 String GROUP_ROLE = "guests";
60 private static final String PACKAGE_NAME = "org.onap.policy.apex.testsuites.integration.common.testclasses";
61 private static final String USERNAME = "guest";
62 private static final String PASSWORD = "IAmAGuest";
63 private static final String URL = "tcp://" + HOST + ":" + PORT;
65 private static final String DATA_PARENT_DIR = Paths.get("target", "activemq-data").toString();
67 private static final XLogger LOGGER = XLoggerFactory.getXLogger(TestJms2Jms.class);
69 private static final int EVENT_COUNT = 100;
70 private static final int EVENT_INTERVAL = 20;
72 private static BrokerService broker;
74 public static ActiveMQConnectionFactory connectionFactory;
77 * Setup embedded jms server.
79 * @throws Exception the exception
82 public static void setupEmbeddedJmsServer() throws Exception {
83 final ArrayList<BrokerPlugin> plugins = new ArrayList<BrokerPlugin>();
84 final BrokerPlugin authenticationPlugin = getAuthenticationBrokerPlugin();
85 plugins.add(authenticationPlugin);
87 broker = new BrokerService();
88 broker.setUseJmx(false);
89 broker.setPersistent(false);
90 broker.addConnector(URL);
91 broker.setDeleteAllMessagesOnStartup(true);
92 broker.setPlugins(plugins.toArray(new BrokerPlugin[0]));
93 broker.setDataDirectory(DATA_PARENT_DIR);
95 broker.waitUntilStarted();
96 connectionFactory = new ActiveMQConnectionFactory(URL);
97 connectionFactory.setTrustedPackages(Arrays.asList(PACKAGE_NAME));
101 * Clear relative file root environment variable.
104 public void clearRelativeFileRoot() {
105 System.clearProperty("APEX_RELATIVE_FILE_ROOT");
109 * Gets the authentication broker plugin.
111 * @return the authentication broker plugin
113 private static BrokerPlugin getAuthenticationBrokerPlugin() {
114 final List<AuthenticationUser> users = new ArrayList<AuthenticationUser>();
115 users.add(new AuthenticationUser(USERNAME, PASSWORD, GROUP_ROLE));
116 final SimpleAuthenticationPlugin authenticationPlugin = new SimpleAuthenticationPlugin(users);
117 return authenticationPlugin;
121 * Shutdown embedded jms server.
123 * @throws IOException Signals that an I/O exception has occurred.
126 public static void shutdownEmbeddedJmsServer() throws IOException {
128 if (broker != null) {
131 } catch (final Exception e) {
132 LOGGER.warn("Failed to stop JMS server", e);
138 * Test jms object events.
140 * @throws ApexException the apex exception
141 * @throws JMSException the JMS exception
144 public void testJmsObjectEvents() throws ApexException, JMSException {
145 final String[] args = {"-rfr", "target", "-c", "target/examples/config/JMS/JMS2JMSObjectEvent.json"};
146 testJmsEvents(args, true);
150 * Test jms json events.
152 * @throws ApexException the apex exception
153 * @throws JMSException the JMS exception
156 public void testJmsJsonEvents() throws ApexException, JMSException {
157 final String[] args = {"-rfr", "target", "-c", "target/examples/config/JMS/JMS2JMSJsonEvent.json"};
158 testJmsEvents(args, false);
164 * @param args the args
165 * @param sendObjects the send objects
166 * @throws ApexException the apex exception
167 * @throws JMSException the JMS exception
169 private void testJmsEvents(final String[] args, final Boolean sendObjects) throws ApexException, JMSException {
170 final JmsEventSubscriber subscriber =
171 new JmsEventSubscriber(JMS_TOPIC_APEX_OUT, connectionFactory, USERNAME, PASSWORD);
173 final JmsEventProducer producer = new JmsEventProducer(JMS_TOPIC_APEX_IN, connectionFactory, USERNAME, PASSWORD,
174 EVENT_COUNT, sendObjects, EVENT_INTERVAL);
176 final ApexMain apexMain = new ApexMain(args);
178 await().atMost(3L, TimeUnit.SECONDS).until(() -> apexMain.isAlive());
180 producer.sendEvents();
182 await().atMost(10L, TimeUnit.SECONDS).until(() -> producer.getEventsSentCount() >= EVENT_COUNT - 1);
183 await().atMost(10L, TimeUnit.SECONDS).until(() -> subscriber.getEventsReceivedCount() >= EVENT_COUNT - 1);
186 subscriber.shutdown();
189 assertEquals(EVENT_COUNT, producer.getEventsSentCount());
190 assertEquals(producer.getEventsSentCount(), subscriber.getEventsReceivedCount());