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 / TestWs2WsClient.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 public class TestWs2WsClient {
34     private static final long MAX_TEST_LENGTH = 10000;
35
36     private static final int EVENT_COUNT = 100;
37     private static final int EVENT_INTERVAL = 20;
38
39     /**
40      * Clear relative file root environment variable.
41      */
42     @Before
43     public void clearRelativeFileRoot() {
44         System.clearProperty("APEX_RELATIVE_FILE_ROOT");
45     }
46
47     @Test
48     public void testJsonWsEvents() throws MessagingException, ApexException {
49         final String[] args = {"-rfr", "target", "-p", "target/examples/config/SampleDomain/Ws2WsClientJsonEvent.json"};
50         testWsEvents(args, false);
51     }
52
53     @Test
54     public void testXmlWsEvents() throws MessagingException, ApexException {
55         final String[] args = {"-rfr", "target", "-p", "target/examples/config/SampleDomain/Ws2WsClientXMLEvent.json"};
56         testWsEvents(args, true);
57     }
58
59     private void testWsEvents(final String[] args, final Boolean xmlEvents) throws MessagingException, ApexException {
60         final WebSocketEventSubscriberServer subServer = new WebSocketEventSubscriberServer(42453);
61         final WebSocketEventProducerServer prodServer =
62                 new WebSocketEventProducerServer(42451, EVENT_COUNT, xmlEvents, EVENT_INTERVAL);
63
64         final ApexMain apexMain = new ApexMain(args);
65
66         prodServer.sendEvents();
67
68         final long testStartTime = System.currentTimeMillis();
69
70         while (System.currentTimeMillis() < testStartTime + MAX_TEST_LENGTH
71                 && subServer.getEventsReceivedCount() < EVENT_COUNT) {
72             ThreadUtilities.sleep(EVENT_INTERVAL);
73         }
74
75         assertEquals(prodServer.getEventsSentCount(), subServer.getEventsReceivedCount());
76
77         apexMain.shutdown();
78         prodServer.shutdown();
79         subServer.shutdown();
80     }
81 }