35d348568f360518e09bf2a090f0ca8891adc6a8
[policy/apex-pdp.git] / core / core-infrastructure / src / test / java / org / onap / policy / apex / core / infrastructure / messaging / EndToEndStringMessagingTest.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.core.infrastructure.messaging;
23
24 import static org.awaitility.Awaitility.await;
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertNotNull;
27
28 import java.util.concurrent.TimeUnit;
29
30 import org.junit.Test;
31 import org.onap.policy.apex.core.infrastructure.messaging.stringmessaging.WsStringMessageClient;
32 import org.onap.policy.apex.core.infrastructure.messaging.stringmessaging.WsStringMessageListener;
33 import org.onap.policy.apex.core.infrastructure.messaging.stringmessaging.WsStringMessageServer;
34 import org.slf4j.ext.XLogger;
35 import org.slf4j.ext.XLoggerFactory;
36
37 /**
38  * The Class EndToEndMessagingTest.
39  *
40  * @author Liam Fallon (liam.fallon@ericsson.com)
41  */
42 public class EndToEndStringMessagingTest {
43     // Logger for this class
44     private static final XLogger logger = XLoggerFactory.getXLogger(EndToEndStringMessagingTest.class);
45
46     private WsStringMessageServer server;
47     private WsStringMessageClient client;
48
49     private boolean finished = false;
50
51     @Test
52     public void testEndToEndMessaging() throws MessagingException {
53         logger.debug("end to end messaging test starting . . .");
54         server = new WsStringMessageServer(44441);
55         assertNotNull(server);
56         server.start(new WsStringServerMessageListener());
57
58         await().atMost(2, TimeUnit.SECONDS).until(() -> server.isStarted());
59
60         try {
61             client = new WsStringMessageClient("localhost", 44441);
62             assertNotNull(client);
63             client.start(new WsStringClientMessageListener());
64
65             client.sendString("Hello, client here");
66
67             await().atLeast(50, TimeUnit.MILLISECONDS).until(() -> finished);
68
69         } finally {
70             if (client != null) {
71                 client.stop();
72             }
73             if (server != null) {
74                 server.stop();
75             }
76
77         }
78         logger.debug("end to end messaging test finished");
79     }
80
81     private class WsStringServerMessageListener implements WsStringMessageListener {
82         @Override
83         public void receiveString(final String stringMessage) {
84             logger.debug(stringMessage);
85             assertEquals("Hello, client here", stringMessage);
86             server.sendString("Hello back from server");
87         }
88     }
89
90     private class WsStringClientMessageListener implements WsStringMessageListener {
91         @Override
92         public void receiveString(final String stringMessage) {
93             logger.debug(stringMessage);
94             assertEquals("Hello back from server", stringMessage);
95             finished = true;
96         }
97     }
98 }