Replace LOGGER/logger/log by LOG
[yangtools.git] / websocket / websocket-client / src / test / java / org / opendaylight / yangtools / websocket / WebSocketClientTest.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.yangtools.websocket;
9
10 import com.google.common.util.concurrent.SettableFuture;
11 import io.netty.handler.codec.http.websocketx.CloseWebSocketFrame;
12 import java.net.URI;
13 import java.net.URISyntaxException;
14 import java.util.concurrent.ExecutionException;
15 import java.util.concurrent.TimeUnit;
16 import java.util.concurrent.TimeoutException;
17 import org.junit.Assert;
18 import org.junit.Before;
19 import org.junit.Test;
20 import org.opendaylight.yangtools.websocket.client.WebSocketIClient;
21 import org.opendaylight.yangtools.websocket.server.WebSocketServer;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
25 @Deprecated
26 public class WebSocketClientTest {
27     private static final Logger LOG = LoggerFactory.getLogger(WebSocketClientTest.class);
28     private static final String MESSAGE = "Take me to your leader!";
29     private Thread webSocketServerThread;
30
31     /**
32      * Tracks if the message from the server has been received
33      */
34     private final SettableFuture<Boolean> messageReceived = SettableFuture.create();
35
36     /**
37      * Tracks the port on which the server is listening
38      */
39     private int port = 0;
40
41     @Before
42     public void startWebSocketServer(){
43         try {
44             WebSocketServer webSocketServer = new WebSocketServer(0);
45             webSocketServerThread = new Thread(webSocketServer);
46             webSocketServerThread.setDaemon(false);
47             webSocketServerThread.start();
48             port = webSocketServer.getPort().get();
49         } catch (Exception e) {
50             LOG.trace("Error starting websocket server");
51         }
52     }
53
54     @Test
55     public void connectAndSendData(){
56
57         URI uri = null;
58         try {
59             uri = new URI(String.format("ws://localhost:%d/websocket", port));
60             LOG.info("CLIENT: " + uri);
61             ClientMessageCallback messageCallback = new ClientMessageCallback();
62             WebSocketIClient wsClient = new WebSocketIClient(uri,messageCallback);
63             try {
64                 wsClient.connect();
65                 wsClient.writeAndFlush(MESSAGE);
66                 wsClient.writeAndFlush(new CloseWebSocketFrame());
67
68                 /*
69                  * Wait for up to 5 seconds for the message to be received. If
70                  * after that time, the message has not been received then
71                  * consider this a failed test.
72                  */
73                 messageReceived.get(5, TimeUnit.SECONDS);
74
75                 webSocketServerThread.interrupt();
76             } catch (InterruptedException e) {
77                 LOG.info("WebSocket client couldn't connect to : " + uri);
78                 Assert.fail("WebSocker client could not connect to : " + uri);
79             } catch (ExecutionException | TimeoutException toe) {
80                 LOG.info("Message not received");
81                 Assert.fail(toe.toString());
82             }
83         } catch (URISyntaxException e) {
84             LOG.info("There is an error in URL sytnax {}",e);
85             Assert.fail("There is an error in URL sytnax");
86         }
87     }
88
89     private class ClientMessageCallback implements org.opendaylight.yangtools.websocket.client.callback.ClientMessageCallback {
90         @Override
91         public void onMessageReceived(final Object message) {
92             LOG.info("received message {}",message);
93             messageReceived.set(true);
94         }
95     }
96 }