f021b26749693c85814ccacdda426fd7b12ed507
[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.apps.uservice.test.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.BeforeClass;
40 import org.junit.Test;
41 import org.onap.policy.apex.core.infrastructure.threading.ThreadUtilities;
42 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
43 import org.onap.policy.apex.service.engine.main.ApexMain;
44 import org.slf4j.ext.XLogger;
45 import org.slf4j.ext.XLoggerFactory;
46
47 public class TestJMS2JMS {
48     public static final String PORT = "5445";
49     public static final String HOST = "localhost";
50     public static final String JMS_TOPIC_APEX_IN = "jms/topic/apexIn";
51     public static final String JMS_TOPIC_APEX_OUT = "jms/topic/apexOut";
52
53     private static final int SLEEP_TIME = 1500;
54     private static final String GROUP_ROLE = "guests";
55     private static final String PACKAGE_NAME = "org.onap.policy.apex.apps.uservice.test.adapt.jms";
56     private static final String USERNAME = "guest";
57     private static final String PASSWORD = "IAmAGuest";
58     private static final String URL = "tcp://" + HOST + ":" + PORT;
59
60     private static final String DATA_PARENT_DIR = Paths.get("target", "activemq-data").toString();
61
62     private static final XLogger LOGGER = XLoggerFactory.getXLogger(TestJMS2JMS.class);
63
64     private static final long MAX_TEST_LENGTH = 10000;
65     private static final int EVENT_COUNT = 100;
66     private static final int EVENT_INTERVAL = 20;
67
68     private static BrokerService broker;
69
70     public static ActiveMQConnectionFactory connectionFactory;
71
72
73     @BeforeClass
74     public static void setupEmbeddedJMSServer() throws Exception {
75         final ArrayList<BrokerPlugin> plugins = new ArrayList<BrokerPlugin>();
76         final BrokerPlugin authenticationPlugin = getAuthenticationBrokerPlugin();
77         plugins.add(authenticationPlugin);
78
79         broker = new BrokerService();
80         broker.setUseJmx(false);
81         broker.setPersistent(false);
82         broker.addConnector(URL);
83         broker.setDeleteAllMessagesOnStartup(true);
84         broker.setPlugins(plugins.toArray(new BrokerPlugin[0]));
85         broker.setDataDirectory(DATA_PARENT_DIR);
86         broker.start();
87         broker.waitUntilStarted();
88         connectionFactory = new ActiveMQConnectionFactory(URL);
89         connectionFactory.setTrustedPackages(Arrays.asList(PACKAGE_NAME));
90     }
91
92     private static BrokerPlugin getAuthenticationBrokerPlugin() {
93         final List<AuthenticationUser> users = new ArrayList<AuthenticationUser>();
94         users.add(new AuthenticationUser(USERNAME, PASSWORD, GROUP_ROLE));
95         final SimpleAuthenticationPlugin authenticationPlugin = new SimpleAuthenticationPlugin(users);
96         return authenticationPlugin;
97     }
98
99     @AfterClass
100     public static void shutdownEmbeddedJMSServer() throws IOException {
101         try {
102             if (broker != null) {
103                 broker.stop();
104             }
105         } catch (final Exception e) {
106             LOGGER.warn("Failed to stop JMS server", e);
107         }
108
109     }
110
111     @Test
112     public void testJMSObjectEvents() throws ApexException, JMSException {
113         final String[] args = { "src/test/resources/prodcons/JMS2JMSObjectEvent.json" };
114         testJMSEvents(args, true);
115     }
116
117     @Test
118     public void testJMSJsonEvents() throws ApexException, JMSException {
119         final String[] args = { "src/test/resources/prodcons/JMS2JMSJsonEvent.json" };
120         testJMSEvents(args, false);
121     }
122
123     private void testJMSEvents(final String[] args, final Boolean sendObjects) throws ApexException, JMSException {
124         final JMSEventSubscriber subscriber =
125                 new JMSEventSubscriber(JMS_TOPIC_APEX_OUT, connectionFactory, USERNAME, PASSWORD);
126         final JMSEventProducer producer = new JMSEventProducer(JMS_TOPIC_APEX_IN, connectionFactory, USERNAME, PASSWORD,
127                 EVENT_COUNT, sendObjects, EVENT_INTERVAL);
128
129         final ApexMain apexMain = new ApexMain(args);
130         ThreadUtilities.sleep(3000);
131
132         producer.sendEvents();
133
134         final long testStartTime = System.currentTimeMillis();
135
136         while (isTimedOut(testStartTime) && subscriber.getEventsReceivedCount() < EVENT_COUNT) {
137             ThreadUtilities.sleep(EVENT_INTERVAL);
138         }
139
140         ThreadUtilities.sleep(SLEEP_TIME);
141         apexMain.shutdown();
142         subscriber.shutdown();
143         producer.shutdown();
144         ThreadUtilities.sleep(SLEEP_TIME);
145
146         assertEquals(EVENT_COUNT, producer.getEventsSentCount());
147         assertEquals(producer.getEventsSentCount(), subscriber.getEventsReceivedCount());
148
149     }
150
151     private boolean isTimedOut(final long testStartTime) {
152         return System.currentTimeMillis() < testStartTime + MAX_TEST_LENGTH;
153     }
154 }