d9691f17eb8109c80efee8edca69b5e56188618a
[policy/apex-pdp.git] /
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 org.junit.Test;
29 import org.onap.policy.apex.core.infrastructure.messaging.stringmessaging.WsStringMessageClient;
30 import org.onap.policy.apex.core.infrastructure.messaging.stringmessaging.WsStringMessageListener;
31 import org.onap.policy.apex.core.infrastructure.messaging.stringmessaging.WsStringMessageServer;
32 import org.slf4j.ext.XLogger;
33 import org.slf4j.ext.XLoggerFactory;
34
35 import java.util.concurrent.TimeUnit;
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         try {
59             client = new WsStringMessageClient("localhost", 44441);
60             assertNotNull(client);
61             client.start(new WsStringClientMessageListener());
62
63             client.sendString("Hello, client here");
64
65             await().atLeast(50, TimeUnit.MILLISECONDS).until(() -> finished);
66
67         } finally {
68             if (client != null) {
69                 client.stop();
70             }
71             if (server != null) {
72                 server.stop();
73             }
74
75         }
76         logger.debug("end to end messaging test finished");
77     }
78
79     private class WsStringServerMessageListener implements WsStringMessageListener {
80         @Override
81         public void receiveString(final String stringMessage) {
82             logger.debug(stringMessage);
83             assertEquals("Hello, client here", stringMessage);
84             server.sendString("Hello back from server");
85         }
86     }
87
88     private class WsStringClientMessageListener implements WsStringMessageListener {
89         @Override
90         public void receiveString(final String stringMessage) {
91             logger.debug(stringMessage);
92             assertEquals("Hello back from server", stringMessage);
93             finished = true;
94         }
95     }
96 }