60b9711c92977fa25b5fff4a53da3c6d62aa496c
[policy/apex-pdp.git] /
1 /*-
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
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.apex.testsuites.integration.uservice.adapt.jms;
23
24 import static org.junit.Assert.assertEquals;
25
26 import java.io.IOException;
27 import java.nio.file.Paths;
28 import java.util.ArrayList;
29 import java.util.Arrays;
30 import java.util.List;
31
32 import javax.jms.JMSException;
33
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.Ignore;
43 import org.junit.Test;
44 import org.onap.policy.apex.core.infrastructure.threading.ThreadUtilities;
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;
49
50 /**
51  * The Class TestJms2Jms.
52  */
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";
58
59     private static final int SLEEP_TIME = 1500;
60     private static final String GROUP_ROLE = "guests";
61     private static final String PACKAGE_NAME = "org.onap.policy.apex.testsuites.integration.common.testclasses";
62     private static final String USERNAME = "guest";
63     private static final String PASSWORD = "IAmAGuest";
64     private static final String URL = "tcp://" + HOST + ":" + PORT;
65
66     private static final String DATA_PARENT_DIR = Paths.get("target", "activemq-data").toString();
67
68     private static final XLogger LOGGER = XLoggerFactory.getXLogger(TestJms2Jms.class);
69
70     private static final long MAX_TEST_LENGTH = 10000;
71     private static final int EVENT_COUNT = 100;
72     private static final int EVENT_INTERVAL = 20;
73
74     private static BrokerService broker;
75
76     public static ActiveMQConnectionFactory connectionFactory;
77
78     /**
79      * Setup embedded jms server.
80      *
81      * @throws Exception the exception
82      */
83     @BeforeClass
84     public static void setupEmbeddedJmsServer() throws Exception {
85         final ArrayList<BrokerPlugin> plugins = new ArrayList<BrokerPlugin>();
86         final BrokerPlugin authenticationPlugin = getAuthenticationBrokerPlugin();
87         plugins.add(authenticationPlugin);
88
89         broker = new BrokerService();
90         broker.setUseJmx(false);
91         broker.setPersistent(false);
92         broker.addConnector(URL);
93         broker.setDeleteAllMessagesOnStartup(true);
94         broker.setPlugins(plugins.toArray(new BrokerPlugin[0]));
95         broker.setDataDirectory(DATA_PARENT_DIR);
96         broker.start();
97         broker.waitUntilStarted();
98         connectionFactory = new ActiveMQConnectionFactory(URL);
99         connectionFactory.setTrustedPackages(Arrays.asList(PACKAGE_NAME));
100     }
101
102     /**
103      * Clear relative file root environment variable.
104      */
105     @Before
106     public void clearRelativeFileRoot() {
107         System.clearProperty("APEX_RELATIVE_FILE_ROOT");
108     }
109
110     /**
111      * Gets the authentication broker plugin.
112      *
113      * @return the authentication broker plugin
114      */
115     private static BrokerPlugin getAuthenticationBrokerPlugin() {
116         final List<AuthenticationUser> users = new ArrayList<AuthenticationUser>();
117         users.add(new AuthenticationUser(USERNAME, PASSWORD, GROUP_ROLE));
118         final SimpleAuthenticationPlugin authenticationPlugin = new SimpleAuthenticationPlugin(users);
119         return authenticationPlugin;
120     }
121
122     /**
123      * Shutdown embedded jms server.
124      *
125      * @throws IOException Signals that an I/O exception has occurred.
126      */
127     @AfterClass
128     public static void shutdownEmbeddedJmsServer() throws IOException {
129         try {
130             if (broker != null) {
131                 broker.stop();
132             }
133         } catch (final Exception e) {
134             LOGGER.warn("Failed to stop JMS server", e);
135         }
136
137     }
138
139     /**
140      * Test jms object events.
141      *
142      * @throws ApexException the apex exception
143      * @throws JMSException the JMS exception
144      */
145     @Test
146     @Ignore
147     public void testJmsObjectEvents() throws ApexException, JMSException {
148         final String[] args = {"-rfr", "target", "-c", "target/examples/config/JMS/JMS2JMSObjectEvent.json"};
149         testJmsEvents(args, true);
150     }
151
152     /**
153      * Test jms json events.
154      *
155      * @throws ApexException the apex exception
156      * @throws JMSException the JMS exception
157      */
158     @Test
159     public void testJmsJsonEvents() throws ApexException, JMSException {
160         final String[] args = {"-rfr", "target", "-c", "target/examples/config/JMS/JMS2JMSJsonEvent.json"};
161         testJmsEvents(args, false);
162     }
163
164     /**
165      * Test jms events.
166      *
167      * @param args the args
168      * @param sendObjects the send objects
169      * @throws ApexException the apex exception
170      * @throws JMSException the JMS exception
171      */
172     private void testJmsEvents(final String[] args, final Boolean sendObjects) throws ApexException, JMSException {
173         final JmsEventSubscriber subscriber =
174                 new JmsEventSubscriber(JMS_TOPIC_APEX_OUT, connectionFactory, USERNAME, PASSWORD);
175         final JmsEventProducer producer = new JmsEventProducer(JMS_TOPIC_APEX_IN, connectionFactory, USERNAME, PASSWORD,
176                 EVENT_COUNT, sendObjects, EVENT_INTERVAL);
177
178         final ApexMain apexMain = new ApexMain(args);
179         ThreadUtilities.sleep(3000);
180
181         producer.sendEvents();
182
183         final long testStartTime = System.currentTimeMillis();
184
185         while (isTimedOut(testStartTime) && subscriber.getEventsReceivedCount() < EVENT_COUNT) {
186             ThreadUtilities.sleep(EVENT_INTERVAL);
187         }
188
189         ThreadUtilities.sleep(SLEEP_TIME);
190         apexMain.shutdown();
191         subscriber.shutdown();
192         producer.shutdown();
193         ThreadUtilities.sleep(SLEEP_TIME);
194
195         assertEquals(EVENT_COUNT, producer.getEventsSentCount());
196         assertEquals(producer.getEventsSentCount(), subscriber.getEventsReceivedCount());
197
198     }
199
200     /**
201      * Checks if is timed out.
202      *
203      * @param testStartTime the test start time
204      * @return true, if is timed out
205      */
206     private boolean isTimedOut(final long testStartTime) {
207         return System.currentTimeMillis() < testStartTime + MAX_TEST_LENGTH;
208     }
209 }