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