0e4db163ab96555e77dc05de498c41bc9aeb36e0
[ccsdk/features.git] /
1 /*
2  * ============LICENSE_START========================================================================
3  * ONAP : ccsdk feature sdnr wt
4  * =================================================================================================
5  * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property. All rights reserved.
6  * =================================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
8  * in compliance with the License. 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 distributed under the License
13  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
14  * or implied. See the License for the specific language governing permissions and limitations under
15  * the License.
16  * ============LICENSE_END==========================================================================
17  */
18 package org.onap.ccsdk.features.sdnr.wt.websocketmanager2.test;
19
20 import static org.junit.Assert.assertTrue;
21 import static org.mockito.Mockito.*;
22
23 import java.net.InetSocketAddress;
24
25 import org.eclipse.jetty.websocket.api.Session;
26 import org.junit.Test;
27 import org.onap.ccsdk.features.sdnr.wt.websocketmanager2.WebSocketManagerSocket;
28
29 public class WebsocketMessageTest {
30
31     private static final String MSG1 = "{\"data\":\"scopes\",\"scopes\":[\"scope1\"]}";
32     private static final String MSG2 = "{}";
33     private static final String MSG3 = "{\"" + WebSocketManagerSocket.KEY_NODENAME + ":\"xy\","
34             + WebSocketManagerSocket.KEY_EVENTTYPE + ":\"zu\"}";
35     private static final String MSG4 = "{ Not correct messga}";
36
37     @Test
38     public void test() {
39         MyWebSocketManagerSocket socketToTest = new MyWebSocketManagerSocket();
40         Session sess = mock(Session.class);
41         InetSocketAddress remoteAdr = new InetSocketAddress("127.0.0.1", 4444);
42         when(sess.getRemoteAddress()).thenReturn(remoteAdr);
43         socketToTest.onWebSocketConnect(sess);
44         // message from client
45         socketToTest.setExpected(MSG1);
46         socketToTest.onWebSocketText(MSG1);
47         socketToTest.setExpected(MSG2);
48         socketToTest.onWebSocketText(MSG2);
49         socketToTest.setExpected(MSG3);
50         socketToTest.onWebSocketText(MSG3);
51         socketToTest.setExpected(MSG4);
52         socketToTest.onWebSocketText(MSG4);
53         socketToTest.onWebSocketClose(0, "by default");
54         sess.close();
55
56     }
57
58     private static class MyWebSocketManagerSocket extends WebSocketManagerSocket {
59
60         private String expected;
61
62         public MyWebSocketManagerSocket() {}
63
64         void setExpected(String expected) {
65             this.expected = expected;
66         }
67
68         @Override
69         public void send(String msg) {
70             System.out.println(msg);
71             assertTrue("Expected '" + expected + "' answer '" + msg + "'", msg.contains(expected));
72         }
73
74     }
75 }