91417a57ae7d2c0a0951b2468db6fe66da056066
[policy/apex-pdp.git] /
1 /*-
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  *
19  * SPDX-License-Identifier: Apache-2.0
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.policy.apex.testsuites.integration.uservice.adapt.jms;
24
25 import static org.awaitility.Awaitility.await;
26 import static org.junit.Assert.assertEquals;
27
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;
41
42 /**
43  * The Class TestJms2Jms.
44  */
45 public class TestJms2Jms {
46     private static final Logger LOGGER = LoggerFactory.getLogger(TestJms2Jms.class);
47
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;
54
55     private static final int EVENT_COUNT = 100;
56     private static final int EVENT_INTERVAL = 20;
57
58     // Embedded JMS server for testing
59     private static JmsServerRunner jmsServerRunner;
60
61     /**
62      * Setup embedded JMS server.
63      *
64      * @throws Exception the exception
65      */
66     @BeforeClass
67     public static void setupEmbeddedJmsServer() throws Exception {
68         jmsServerRunner = new JmsServerRunner(SERVER_NAME, SERVER_URI);
69
70         await().pollDelay(3L, TimeUnit.SECONDS).until(() -> new AtomicBoolean(true).get() == true);
71     }
72
73     /**
74      * Clear relative file root environment variable.
75      */
76     @Before
77     public void clearRelativeFileRoot() {
78         System.clearProperty("APEX_RELATIVE_FILE_ROOT");
79     }
80
81     /**
82      * Shutdown embedded jms server.
83      *
84      * @throws IOException Signals that an I/O exception has occurred.
85      */
86     @AfterClass
87     public static void shutdownEmbeddedJmsServer() throws IOException {
88         try {
89             if (jmsServerRunner != null) {
90                 jmsServerRunner.stop();
91             }
92         } catch (final Exception e) {
93             LOGGER.warn("Failed to stop JMS server", e);
94         }
95
96     }
97
98     /**
99      * Test jms object events.
100      *
101      * @throws ApexException the apex exception
102      * @throws JMSException the JMS exception
103      */
104     @Test
105     public void testJmsObjectEvents() throws ApexException, JMSException {
106         final String[] args = {
107             "-rfr", "target", "-p", "target/examples/config/JMS/JMS2JMSObjectEvent.json"
108         };
109         testJmsEvents(args, true);
110     }
111
112     /**
113      * Test jms json events.
114      *
115      * @throws ApexException the apex exception
116      * @throws JMSException the JMS exception
117      */
118     @Test
119     public void testJmsJsonEvents() throws ApexException, JMSException {
120         final String[] args = {
121             "-rfr", "target", "-p", "target/examples/config/JMS/JMS2JMSJsonEvent.json"
122         };
123         testJmsEvents(args, false);
124     }
125
126     /**
127      * Test jms events.
128      *
129      * @param args the args
130      * @param sendObjects the send objects
131      * @throws ApexException the apex exception
132      * @throws JMSException the JMS exception
133      */
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);
137
138         final JmsEventProducer producer =
139                 new JmsEventProducer(JMS_TOPIC_APEX_IN, new ActiveMQConnectionFactory(SERVER_URI), null, null,
140                         EVENT_COUNT, sendObjects, EVENT_INTERVAL);
141
142         final ApexMain apexMain = new ApexMain(args);
143
144         await().atMost(3L, TimeUnit.SECONDS).until(() -> apexMain.isAlive());
145
146         producer.sendEvents();
147
148         await().atMost(10L, TimeUnit.SECONDS).until(() -> producer.getEventsSentCount() >= EVENT_COUNT - 1);
149         await().atMost(10L, TimeUnit.SECONDS).until(() -> subscriber.getEventsReceivedCount() >= EVENT_COUNT - 1);
150
151         apexMain.shutdown();
152         subscriber.shutdown();
153         producer.shutdown();
154
155         assertEquals(EVENT_COUNT, producer.getEventsSentCount());
156         assertEquals(producer.getEventsSentCount(), subscriber.getEventsReceivedCount());
157     }
158 }