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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.apex.testsuites.integration.uservice.adapt.websocket;
24 import static org.junit.Assert.assertEquals;
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;
34 * The Class TestWs2WsServer.
36 public class TestWs2WsServer {
37 private static final long MAX_TEST_LENGTH = 10000;
39 private static final int EVENT_COUNT = 100;
40 private static final int EVENT_INTERVAL = 20;
43 * Clear relative file root environment variable.
46 public void clearRelativeFileRoot() {
47 System.clearProperty("APEX_RELATIVE_FILE_ROOT");
51 * Test json ws events.
53 * @throws MessagingException the messaging exception
54 * @throws ApexException the apex exception
57 public void testJsonWsEvents() throws MessagingException, ApexException {
58 final String[] args = {"-rfr", "target", "-p", "target/examples/config/SampleDomain/Ws2WsServerJsonEvent.json"};
59 testWsEvents(args, false);
65 * @throws MessagingException the messaging exception
66 * @throws ApexException the apex exception
69 public void testXmlWsEvents() throws MessagingException, ApexException {
70 final String[] args = {"-rfr", "target", "-p", "target/examples/config/SampleDomain/Ws2WsServerXMLEvent.json"};
71 testWsEvents(args, true);
77 * @param args the args
78 * @param xmlEvents the xml events
79 * @throws MessagingException the messaging exception
80 * @throws ApexException the apex exception
82 public void testWsEvents(final String[] args, final boolean xmlEvents) throws MessagingException, ApexException {
83 final ApexMain apexMain = new ApexMain(args);
85 final WebSocketEventSubscriberClient subClient = new WebSocketEventSubscriberClient("localhost", 42452);
86 final WebSocketEventProducerClient prodClient =
87 new WebSocketEventProducerClient("localhost", 42450, EVENT_COUNT, xmlEvents, EVENT_INTERVAL);
89 prodClient.sendEvents();
91 final long testStartTime = System.currentTimeMillis();
93 while (System.currentTimeMillis() < testStartTime + MAX_TEST_LENGTH
94 && subClient.getEventsReceivedCount() < EVENT_COUNT) {
95 ThreadUtilities.sleep(EVENT_INTERVAL);
98 assertEquals(subClient.getEventsReceivedCount(), prodClient.getEventsSentCount());
100 prodClient.shutdown();
101 subClient.shutdown();