f769dd40dac04754ec65ea98e0d9c6f6988e8f00
[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\","+WebSocketManagerSocket.KEY_EVENTTYPE+":\"zu\"}";
34     private static final String MSG4 = "{ Not correct messga}";
35
36     @Test
37     public void test() {
38         MyWebSocketManagerSocket socketToTest = new MyWebSocketManagerSocket();
39         Session sess = mock(Session.class);
40         InetSocketAddress remoteAdr = new InetSocketAddress("127.0.0.1", 4444);
41         when(sess.getRemoteAddress()).thenReturn(remoteAdr);
42         socketToTest.onWebSocketConnect(sess);
43         // message from client
44         socketToTest.setExpected(MSG1);
45         socketToTest.onWebSocketText(MSG1);
46         socketToTest.setExpected(MSG2);
47         socketToTest.onWebSocketText(MSG2);
48         socketToTest.setExpected(MSG3);
49         socketToTest.onWebSocketText(MSG3);
50         socketToTest.setExpected(MSG4);
51         socketToTest.onWebSocketText(MSG4);
52         socketToTest.onWebSocketClose(0, "by default");
53         sess.close();
54
55     }
56
57     private static class MyWebSocketManagerSocket extends WebSocketManagerSocket {
58
59         private String expected;
60
61         public MyWebSocketManagerSocket() {
62         }
63         void setExpected(String expected) {
64             this.expected = expected;
65         }
66
67         @Override
68         public void send(String msg) {
69             System.out.println(msg);
70             assertTrue("Expected '"+expected+"' answer '"+msg+"'", msg.contains(expected));
71         }
72
73     }
74 }