ca2d3ecc06a46f71c3a3e02d383632a7a1f50cf9
[policy/apex-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.apex.testsuites.integration.uservice.adapt.jms;
22
23 import static org.junit.Assert.assertEquals;
24
25 import java.io.IOException;
26 import java.nio.file.Paths;
27 import java.util.ArrayList;
28 import java.util.Arrays;
29 import java.util.List;
30
31 import javax.jms.JMSException;
32
33 import org.apache.activemq.ActiveMQConnectionFactory;
34 import org.apache.activemq.broker.BrokerPlugin;
35 import org.apache.activemq.broker.BrokerService;
36 import org.apache.activemq.security.AuthenticationUser;
37 import org.apache.activemq.security.SimpleAuthenticationPlugin;
38 import org.junit.AfterClass;
39 import org.junit.Before;
40 import org.junit.BeforeClass;
41 import org.junit.Test;
42 import org.onap.policy.apex.core.infrastructure.threading.ThreadUtilities;
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;
47
48 /**
49  * The Class TestJms2Jms.
50  */
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";
56
57     private static final int SLEEP_TIME = 1500;
58     private static final String GROUP_ROLE = "guests";
59     private static final String PACKAGE_NAME = "org.onap.policy.apex.testsuites.integration.common.testclasses";
60     private static final String USERNAME = "guest";
61     private static final String PASSWORD = "IAmAGuest";
62     private static final String URL = "tcp://" + HOST + ":" + PORT;
63
64     private static final String DATA_PARENT_DIR = Paths.get("target", "activemq-data").toString();
65
66     private static final XLogger LOGGER = XLoggerFactory.getXLogger(TestJms2Jms.class);
67
68     private static final long MAX_TEST_LENGTH = 10000;
69     private static final int EVENT_COUNT = 100;
70     private static final int EVENT_INTERVAL = 20;
71
72     private static BrokerService broker;
73
74     public static ActiveMQConnectionFactory connectionFactory;
75
76     /**
77      * Setup embedded jms server.
78      *
79      * @throws Exception the exception
80      */
81     @BeforeClass
82     public static void setupEmbeddedJmsServer() throws Exception {
83         final ArrayList<BrokerPlugin> plugins = new ArrayList<BrokerPlugin>();
84         final BrokerPlugin authenticationPlugin = getAuthenticationBrokerPlugin();
85         plugins.add(authenticationPlugin);
86
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);
94         broker.start();
95         broker.waitUntilStarted();
96         connectionFactory = new ActiveMQConnectionFactory(URL);
97         connectionFactory.setTrustedPackages(Arrays.asList(PACKAGE_NAME));
98     }
99
100     /**
101      * Clear relative file root environment variable.
102      */
103     @Before
104     public void clearRelativeFileRoot() {
105         System.clearProperty("APEX_RELATIVE_FILE_ROOT");
106     }
107
108     /**
109      * Gets the authentication broker plugin.
110      *
111      * @return the authentication broker plugin
112      */
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;
118     }
119
120     /**
121      * Shutdown embedded jms server.
122      *
123      * @throws IOException Signals that an I/O exception has occurred.
124      */
125     @AfterClass
126     public static void shutdownEmbeddedJmsServer() throws IOException {
127         try {
128             if (broker != null) {
129                 broker.stop();
130             }
131         } catch (final Exception e) {
132             LOGGER.warn("Failed to stop JMS server", e);
133         }
134
135     }
136
137     /**
138      * Test jms object events.
139      *
140      * @throws ApexException the apex exception
141      * @throws JMSException the JMS exception
142      */
143     @Test
144     public void testJmsObjectEvents() throws ApexException, JMSException {
145         final String[] args = { "-rfr", "target", "-c", "target/examples/config/JMS/JMS2JMSObjectEvent.json" };
146         testJmsEvents(args, true);
147     }
148
149     /**
150      * Test jms json events.
151      *
152      * @throws ApexException the apex exception
153      * @throws JMSException the JMS exception
154      */
155     @Test
156     public void testJmsJsonEvents() throws ApexException, JMSException {
157         final String[] args = { "-rfr", "target", "-c", "target/examples/config/JMS/JMS2JMSJsonEvent.json" };
158         testJmsEvents(args, false);
159     }
160
161     /**
162      * Test jms events.
163      *
164      * @param args the args
165      * @param sendObjects the send objects
166      * @throws ApexException the apex exception
167      * @throws JMSException the JMS exception
168      */
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);
172         final JmsEventProducer producer = new JmsEventProducer(JMS_TOPIC_APEX_IN, connectionFactory, USERNAME, PASSWORD,
173                 EVENT_COUNT, sendObjects, EVENT_INTERVAL);
174
175         final ApexMain apexMain = new ApexMain(args);
176         ThreadUtilities.sleep(3000);
177
178         producer.sendEvents();
179
180         final long testStartTime = System.currentTimeMillis();
181
182         while (isTimedOut(testStartTime) && subscriber.getEventsReceivedCount() < EVENT_COUNT) {
183             ThreadUtilities.sleep(EVENT_INTERVAL);
184         }
185
186         ThreadUtilities.sleep(SLEEP_TIME);
187         apexMain.shutdown();
188         subscriber.shutdown();
189         producer.shutdown();
190         ThreadUtilities.sleep(SLEEP_TIME);
191
192         assertEquals(EVENT_COUNT, producer.getEventsSentCount());
193         assertEquals(producer.getEventsSentCount(), subscriber.getEventsReceivedCount());
194
195     }
196
197     /**
198      * Checks if is timed out.
199      *
200      * @param testStartTime the test start time
201      * @return true, if is timed out
202      */
203     private boolean isTimedOut(final long testStartTime) {
204         return System.currentTimeMillis() < testStartTime + MAX_TEST_LENGTH;
205     }
206 }