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
 
  10  * http://www.apache.org/licenses/LICENSE-2.0
 
  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
 
  16  * ============LICENSE_END==========================================================================
 
  17  ******************************************************************************/
 
  18 package org.onap.ccsdk.features.sdnr.wt.websocketmanager2.test;
 
  20 import static org.junit.Assert.assertTrue;
 
  21 import static org.mockito.Mockito.*;
 
  23 import java.net.InetSocketAddress;
 
  25 import org.eclipse.jetty.websocket.api.Session;
 
  26 import org.junit.Test;
 
  27 import org.onap.ccsdk.features.sdnr.wt.websocketmanager2.WebSocketManagerSocket;
 
  29 public class WebsocketMessageTest {
 
  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}";
 
  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");
 
  57     private static class MyWebSocketManagerSocket extends WebSocketManagerSocket {
 
  59         private String expected;
 
  61         public MyWebSocketManagerSocket() {
 
  63         void setExpected(String expected) {
 
  64             this.expected = expected;
 
  68         public void send(String msg) {
 
  69             System.out.println(msg);
 
  70             assertTrue("Expected '"+expected+"' answer '"+msg+"'", msg.contains(expected));