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 / websocket / TestWs2WsServer.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
4  *  Modifications Copyright (C) 2020 Bell Canada. All rights reserved.
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.websocket;
23
24 import static org.junit.Assert.assertEquals;
25
26 import org.junit.Before;
27 import org.junit.Test;
28 import org.onap.policy.apex.core.infrastructure.messaging.MessagingException;
29 import org.onap.policy.apex.core.infrastructure.threading.ThreadUtilities;
30 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
31 import org.onap.policy.apex.service.engine.main.ApexMain;
32
33 /**
34  * The Class TestWs2WsServer.
35  */
36 public class TestWs2WsServer {
37     private static final long MAX_TEST_LENGTH = 10000;
38
39     private static final int EVENT_COUNT = 100;
40     private static final int EVENT_INTERVAL = 20;
41
42     /**
43      * Clear relative file root environment variable.
44      */
45     @Before
46     public void clearRelativeFileRoot() {
47         System.clearProperty("APEX_RELATIVE_FILE_ROOT");
48     }
49
50     /**
51      * Test json ws events.
52      *
53      * @throws MessagingException the messaging exception
54      * @throws ApexException the apex exception
55      */
56     @Test
57     public void testJsonWsEvents() throws MessagingException, ApexException {
58         final String[] args = {"-rfr", "target", "-p", "target/examples/config/SampleDomain/Ws2WsServerJsonEvent.json"};
59         testWsEvents(args, false);
60     }
61
62     /**
63      * Test xml ws events.
64      *
65      * @throws MessagingException the messaging exception
66      * @throws ApexException the apex exception
67      */
68     @Test
69     public void testXmlWsEvents() throws MessagingException, ApexException {
70         final String[] args = {"-rfr", "target", "-p", "target/examples/config/SampleDomain/Ws2WsServerXMLEvent.json"};
71         testWsEvents(args, true);
72     }
73
74     /**
75      * Test ws events.
76      *
77      * @param args the args
78      * @param xmlEvents the xml events
79      * @throws MessagingException the messaging exception
80      * @throws ApexException the apex exception
81      */
82     public void testWsEvents(final String[] args, final boolean xmlEvents) throws MessagingException, ApexException {
83         final ApexMain apexMain = new ApexMain(args);
84
85         final WebSocketEventSubscriberClient subClient = new WebSocketEventSubscriberClient("localhost", 42452);
86         final WebSocketEventProducerClient prodClient =
87                 new WebSocketEventProducerClient("localhost", 42450, EVENT_COUNT, xmlEvents, EVENT_INTERVAL);
88
89         prodClient.sendEvents();
90
91         final long testStartTime = System.currentTimeMillis();
92
93         while (System.currentTimeMillis() < testStartTime + MAX_TEST_LENGTH
94                 && subClient.getEventsReceivedCount() < EVENT_COUNT) {
95             ThreadUtilities.sleep(EVENT_INTERVAL);
96         }
97
98         assertEquals(subClient.getEventsReceivedCount(), prodClient.getEventsSentCount());
99
100         prodClient.shutdown();
101         subClient.shutdown();
102         apexMain.shutdown();
103     }
104 }