APEX standalone support for ToscaPolicy format
[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  *  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 java.io.IOException;
29 import java.nio.file.Paths;
30 import java.util.ArrayList;
31 import java.util.Arrays;
32 import java.util.List;
33 import java.util.concurrent.TimeUnit;
34 import javax.jms.JMSException;
35 import org.apache.activemq.ActiveMQConnectionFactory;
36 import org.apache.activemq.broker.BrokerPlugin;
37 import org.apache.activemq.broker.BrokerService;
38 import org.apache.activemq.security.AuthenticationUser;
39 import org.apache.activemq.security.SimpleAuthenticationPlugin;
40 import org.junit.AfterClass;
41 import org.junit.Before;
42 import org.junit.BeforeClass;
43 import org.junit.Test;
44 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
45 import org.onap.policy.apex.service.engine.main.ApexMain;
46 import org.slf4j.ext.XLogger;
47 import org.slf4j.ext.XLoggerFactory;
48
49 /**
50  * The Class TestJms2Jms.
51  */
52 public class TestJms2Jms {
53     public static final String PORT = "5445";
54     public static final String HOST = "localhost";
55     public static final String JMS_TOPIC_APEX_IN = "jms/topic/apexIn";
56     public static final String JMS_TOPIC_APEX_OUT = "jms/topic/apexOut";
57
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 int EVENT_COUNT = 100;
69     private static final int EVENT_INTERVAL = 20;
70
71     private static BrokerService broker;
72
73     public static ActiveMQConnectionFactory connectionFactory;
74
75     /**
76      * Setup embedded jms server.
77      *
78      * @throws Exception the exception
79      */
80     @BeforeClass
81     public static void setupEmbeddedJmsServer() throws Exception {
82         final ArrayList<BrokerPlugin> plugins = new ArrayList<BrokerPlugin>();
83         final BrokerPlugin authenticationPlugin = getAuthenticationBrokerPlugin();
84         plugins.add(authenticationPlugin);
85
86         broker = new BrokerService();
87         broker.setUseJmx(false);
88         broker.setPersistent(false);
89         broker.addConnector(URL);
90         broker.setDeleteAllMessagesOnStartup(true);
91         broker.setPlugins(plugins.toArray(new BrokerPlugin[0]));
92         broker.setDataDirectory(DATA_PARENT_DIR);
93         broker.start();
94         broker.waitUntilStarted();
95         connectionFactory = new ActiveMQConnectionFactory(URL);
96         connectionFactory.setTrustedPackages(Arrays.asList(PACKAGE_NAME));
97     }
98
99     /**
100      * Clear relative file root environment variable.
101      */
102     @Before
103     public void clearRelativeFileRoot() {
104         System.clearProperty("APEX_RELATIVE_FILE_ROOT");
105     }
106
107     /**
108      * Gets the authentication broker plugin.
109      *
110      * @return the authentication broker plugin
111      */
112     private static BrokerPlugin getAuthenticationBrokerPlugin() {
113         final List<AuthenticationUser> users = new ArrayList<AuthenticationUser>();
114         users.add(new AuthenticationUser(USERNAME, PASSWORD, GROUP_ROLE));
115         final SimpleAuthenticationPlugin authenticationPlugin = new SimpleAuthenticationPlugin(users);
116         return authenticationPlugin;
117     }
118
119     /**
120      * Shutdown embedded jms server.
121      *
122      * @throws IOException Signals that an I/O exception has occurred.
123      */
124     @AfterClass
125     public static void shutdownEmbeddedJmsServer() throws IOException {
126         try {
127             if (broker != null) {
128                 broker.stop();
129             }
130         } catch (final Exception e) {
131             LOGGER.warn("Failed to stop JMS server", e);
132         }
133
134     }
135
136     /**
137      * Test jms object events.
138      *
139      * @throws ApexException the apex exception
140      * @throws JMSException the JMS exception
141      */
142     @Test
143     public void testJmsObjectEvents() throws ApexException, JMSException {
144         final String[] args = {"-rfr", "target", "-p", "target/examples/config/JMS/JMS2JMSObjectEvent.json"};
145         testJmsEvents(args, true);
146     }
147
148     /**
149      * Test jms json events.
150      *
151      * @throws ApexException the apex exception
152      * @throws JMSException the JMS exception
153      */
154     @Test
155     public void testJmsJsonEvents() throws ApexException, JMSException {
156         final String[] args = {"-rfr", "target", "-p", "target/examples/config/JMS/JMS2JMSJsonEvent.json"};
157         testJmsEvents(args, false);
158     }
159
160     /**
161      * Test jms events.
162      *
163      * @param args the args
164      * @param sendObjects the send objects
165      * @throws ApexException the apex exception
166      * @throws JMSException the JMS exception
167      */
168     private void testJmsEvents(final String[] args, final Boolean sendObjects) throws ApexException, JMSException {
169         final JmsEventSubscriber subscriber =
170                 new JmsEventSubscriber(JMS_TOPIC_APEX_OUT, connectionFactory, USERNAME, PASSWORD);
171
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
177         await().atMost(3L, TimeUnit.SECONDS).until(() -> apexMain.isAlive());
178
179         producer.sendEvents();
180
181         await().atMost(10L, TimeUnit.SECONDS).until(() -> producer.getEventsSentCount() >= EVENT_COUNT - 1);
182         await().atMost(10L, TimeUnit.SECONDS).until(() -> subscriber.getEventsReceivedCount() >= EVENT_COUNT - 1);
183
184         apexMain.shutdown();
185         subscriber.shutdown();
186         producer.shutdown();
187
188         assertEquals(EVENT_COUNT, producer.getEventsSentCount());
189         assertEquals(producer.getEventsSentCount(), subscriber.getEventsReceivedCount());
190     }
191 }