2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * Modifications Copyright (C) 2020, 2023 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 jakarta.jms.JMSException;
29 import java.io.IOException;
30 import java.util.concurrent.TimeUnit;
31 import java.util.concurrent.atomic.AtomicBoolean;
32 import org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory;
33 import org.junit.AfterClass;
34 import org.junit.Before;
35 import org.junit.BeforeClass;
36 import org.junit.Test;
37 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
38 import org.onap.policy.apex.service.engine.main.ApexMain;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
43 * The Class TestJms2Jms.
45 public class TestJms2Jms {
46 private static final Logger LOGGER = LoggerFactory.getLogger(TestJms2Jms.class);
48 protected static final String SERVER_NAME = "JmsTestServer";
49 protected static final String PORT = "5445";
50 protected static final String HOST = "localhost";
51 protected static final String JMS_TOPIC_APEX_IN = "jms/topic/apexIn";
52 protected static final String JMS_TOPIC_APEX_OUT = "jms/topic/apexOut";
53 protected static final String SERVER_URI = "tcp://" + HOST + ":" + PORT;
55 private static final int EVENT_COUNT = 100;
56 private static final int EVENT_INTERVAL = 20;
58 // Embedded JMS server for testing
59 private static JmsServerRunner jmsServerRunner;
62 * Setup embedded JMS server.
64 * @throws Exception the exception
67 public static void setupEmbeddedJmsServer() throws Exception {
68 jmsServerRunner = new JmsServerRunner(SERVER_NAME, SERVER_URI);
70 await().pollDelay(3L, TimeUnit.SECONDS).until(() -> new AtomicBoolean(true).get() == true);
74 * Clear relative file root environment variable.
77 public void clearRelativeFileRoot() {
78 System.clearProperty("APEX_RELATIVE_FILE_ROOT");
82 * Shutdown embedded jms server.
84 * @throws IOException Signals that an I/O exception has occurred.
87 public static void shutdownEmbeddedJmsServer() throws IOException {
89 if (jmsServerRunner != null) {
90 jmsServerRunner.stop();
92 } catch (final Exception e) {
93 LOGGER.warn("Failed to stop JMS server", e);
99 * Test jms object events.
101 * @throws ApexException the apex exception
102 * @throws JMSException the JMS exception
105 public void testJmsObjectEvents() throws ApexException, JMSException {
106 final String[] args = {
107 "-rfr", "target", "-p", "target/examples/config/JMS/JMS2JMSObjectEvent.json"
109 testJmsEvents(args, true);
113 * Test jms json events.
115 * @throws ApexException the apex exception
116 * @throws JMSException the JMS exception
119 public void testJmsJsonEvents() throws ApexException, JMSException {
120 final String[] args = {
121 "-rfr", "target", "-p", "target/examples/config/JMS/JMS2JMSJsonEvent.json"
123 testJmsEvents(args, false);
129 * @param args the args
130 * @param sendObjects the send objects
131 * @throws ApexException the apex exception
132 * @throws JMSException the JMS exception
134 private void testJmsEvents(final String[] args, final Boolean sendObjects) throws ApexException, JMSException {
135 final JmsEventSubscriber subscriber =
136 new JmsEventSubscriber(JMS_TOPIC_APEX_OUT, new ActiveMQConnectionFactory(SERVER_URI), null, null);
138 final JmsEventProducer producer =
139 new JmsEventProducer(JMS_TOPIC_APEX_IN, new ActiveMQConnectionFactory(SERVER_URI), null, null,
140 EVENT_COUNT, sendObjects, EVENT_INTERVAL);
142 final ApexMain apexMain = new ApexMain(args);
144 await().atMost(3L, TimeUnit.SECONDS).until(() -> apexMain.isAlive());
146 producer.sendEvents();
148 await().atMost(10L, TimeUnit.SECONDS).until(() -> producer.getEventsSentCount() >= EVENT_COUNT - 1);
149 await().atMost(10L, TimeUnit.SECONDS).until(() -> subscriber.getEventsReceivedCount() >= EVENT_COUNT - 1);
152 subscriber.shutdown();
155 assertEquals(EVENT_COUNT, producer.getEventsSentCount());
156 assertEquals(producer.getEventsSentCount(), subscriber.getEventsReceivedCount());