e029150fab85f68de46664807bdf3ccd22e3d8e9
[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.mock;
22 import static org.mockito.Mockito.when;
23 import java.net.InetSocketAddress;
24 import org.eclipse.jetty.websocket.api.Session;
25 import org.junit.Test;
26 import org.onap.ccsdk.features.sdnr.wt.websocketmanager.WebSocketManagerSocket;
27
28 public class WebsocketMessageTest {
29
30     private static final String MSG1 = "{\"data\":\"scopes\",\"scopes\":[{\"node-id\":\"scope1\"}]}";
31     private static final String MSG1_RESPONSE = "{\"status\":\"success\",\"scopes\":[{\"node-id\":\"scope1\"}]}";
32     private static final String MSG2 = "{}";
33     private static final String MSG3 = "{\n"
34             + "  \"event-time\": \"2021-03-12T05:08:55.3Z\",\n"
35             + "  \"type\": \"urn:opendaylight:params:xml:ns:yang:devicemanager@2019-01-09:object-creation-notification\",\n"
36             + "  \"node-id\": \"SDN-Controller-0\",\n"
37             + "  \"data\": {\n"
38             + "    \"object-id-ref\": \"sim1\",\n"
39             + "    \"counter\": 7,\n"
40             + "    \"time-stamp\": \"2021-03-12T05:08:55.2Z\"\n"
41             + "  }\n"
42             + "}";
43     private static final String MSG4 = "{ Not correct messga}";
44
45     @Test
46     public void test() {
47         MyWebSocketManagerSocket socketToTest = new MyWebSocketManagerSocket();
48         Session sess = mock(Session.class);
49         InetSocketAddress remoteAdr = new InetSocketAddress("127.0.0.1", 4444);
50         when(sess.getRemoteAddress()).thenReturn(remoteAdr);
51         socketToTest.onWebSocketConnect(sess);
52         // message from client
53         socketToTest.setExpected(MSG1_RESPONSE);
54         socketToTest.onWebSocketText(MSG1);
55         socketToTest.setExpected(MSG2);
56         socketToTest.onWebSocketText(MSG2);
57         socketToTest.setExpected(MSG3);
58         socketToTest.onWebSocketText(MSG3);
59         socketToTest.setExpected(MSG4);
60         socketToTest.onWebSocketText(MSG4);
61         socketToTest.onWebSocketClose(0, "by default");
62         sess.close();
63
64     }
65
66     private static class MyWebSocketManagerSocket extends WebSocketManagerSocket {
67
68         private String expected;
69
70         public MyWebSocketManagerSocket() {}
71
72         void setExpected(String expected) {
73             this.expected = expected;
74         }
75
76         @Override
77         public void send(String msg) {
78             System.out.println(msg);
79             assertTrue("Expected '" + expected + "' answer '" + msg + "'", msg.contains(expected));
80         }
81
82     }
83 }