Changes for checkstyle 8.32
[policy/apex-pdp.git] / testsuites / integration / integration-uservice-test / src / test / java / org / onap / policy / apex / testsuites / integration / uservice / adapt / jms / TestJms2Jms.java
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.awaitility.Awaitility.await;
25 import static org.junit.Assert.assertEquals;
26
27 import java.io.IOException;
28 import java.nio.file.Paths;
29 import java.util.ArrayList;
30 import java.util.Arrays;
31 import java.util.List;
32 import java.util.concurrent.TimeUnit;
33 import javax.jms.JMSException;
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.Test;
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 String GROUP_ROLE = "guests";
58     private static final String PACKAGE_NAME = "org.onap.policy.apex.testsuites.integration.common.testclasses";
59     private static final String USERNAME = "guest";
60     private static final String PASSWORD = "IAmAGuest";
61     private static final String URL = "tcp://" + HOST + ":" + PORT;
62
63     private static final String DATA_PARENT_DIR = Paths.get("target", "activemq-data").toString();
64
65     private static final XLogger LOGGER = XLoggerFactory.getXLogger(TestJms2Jms.class);
66
67     private static final int EVENT_COUNT = 100;
68     private static final int EVENT_INTERVAL = 20;
69
70     private static BrokerService broker;
71
72     public static ActiveMQConnectionFactory connectionFactory;
73
74     /**
75      * Setup embedded jms server.
76      *
77      * @throws Exception the exception
78      */
79     @BeforeClass
80     public static void setupEmbeddedJmsServer() throws Exception {
81         final ArrayList<BrokerPlugin> plugins = new ArrayList<BrokerPlugin>();
82         final BrokerPlugin authenticationPlugin = getAuthenticationBrokerPlugin();
83         plugins.add(authenticationPlugin);
84
85         broker = new BrokerService();
86         broker.setUseJmx(false);
87         broker.setPersistent(false);
88         broker.addConnector(URL);
89         broker.setDeleteAllMessagesOnStartup(true);
90         broker.setPlugins(plugins.toArray(new BrokerPlugin[0]));
91         broker.setDataDirectory(DATA_PARENT_DIR);
92         broker.start();
93         broker.waitUntilStarted();
94         connectionFactory = new ActiveMQConnectionFactory(URL);
95         connectionFactory.setTrustedPackages(Arrays.asList(PACKAGE_NAME));
96     }
97
98     /**
99      * Clear relative file root environment variable.
100      */
101     @Before
102     public void clearRelativeFileRoot() {
103         System.clearProperty("APEX_RELATIVE_FILE_ROOT");
104     }
105
106     /**
107      * Gets the authentication broker plugin.
108      *
109      * @return the authentication broker plugin
110      */
111     private static BrokerPlugin getAuthenticationBrokerPlugin() {
112         final List<AuthenticationUser> users = new ArrayList<AuthenticationUser>();
113         users.add(new AuthenticationUser(USERNAME, PASSWORD, GROUP_ROLE));
114         final SimpleAuthenticationPlugin authenticationPlugin = new SimpleAuthenticationPlugin(users);
115         return authenticationPlugin;
116     }
117
118     /**
119      * Shutdown embedded jms server.
120      *
121      * @throws IOException Signals that an I/O exception has occurred.
122      */
123     @AfterClass
124     public static void shutdownEmbeddedJmsServer() throws IOException {
125         try {
126             if (broker != null) {
127                 broker.stop();
128             }
129         } catch (final Exception e) {
130             LOGGER.warn("Failed to stop JMS server", e);
131         }
132
133     }
134
135     /**
136      * Test jms object events.
137      *
138      * @throws ApexException the apex exception
139      * @throws JMSException the JMS exception
140      */
141     @Test
142     public void testJmsObjectEvents() throws ApexException, JMSException {
143         final String[] args = {"-rfr", "target", "-c", "target/examples/config/JMS/JMS2JMSObjectEvent.json"};
144         testJmsEvents(args, true);
145     }
146
147     /**
148      * Test jms json events.
149      *
150      * @throws ApexException the apex exception
151      * @throws JMSException the JMS exception
152      */
153     @Test
154     public void testJmsJsonEvents() throws ApexException, JMSException {
155         final String[] args = {"-rfr", "target", "-c", "target/examples/config/JMS/JMS2JMSJsonEvent.json"};
156         testJmsEvents(args, false);
157     }
158
159     /**
160      * Test jms events.
161      *
162      * @param args the args
163      * @param sendObjects the send objects
164      * @throws ApexException the apex exception
165      * @throws JMSException the JMS exception
166      */
167     private void testJmsEvents(final String[] args, final Boolean sendObjects) throws ApexException, JMSException {
168         final JmsEventSubscriber subscriber =
169                 new JmsEventSubscriber(JMS_TOPIC_APEX_OUT, connectionFactory, USERNAME, PASSWORD);
170
171         final JmsEventProducer producer = new JmsEventProducer(JMS_TOPIC_APEX_IN, connectionFactory, USERNAME, PASSWORD,
172                 EVENT_COUNT, sendObjects, EVENT_INTERVAL);
173
174         final ApexMain apexMain = new ApexMain(args);
175
176         await().atMost(3L, TimeUnit.SECONDS).until(() -> apexMain.isAlive());
177
178         producer.sendEvents();
179
180         await().atMost(10L, TimeUnit.SECONDS).until(() -> producer.getEventsSentCount() >= EVENT_COUNT - 1);
181         await().atMost(10L, TimeUnit.SECONDS).until(() -> subscriber.getEventsReceivedCount() >= EVENT_COUNT - 1);
182
183         apexMain.shutdown();
184         subscriber.shutdown();
185         producer.shutdown();
186
187         assertEquals(EVENT_COUNT, producer.getEventsSentCount());
188         assertEquals(producer.getEventsSentCount(), subscriber.getEventsReceivedCount());
189     }
190 }