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==========================================================================
18 package org.onap.ccsdk.features.sdnr.wt.websocketmanager2.test;
20 import static org.junit.Assert.assertEquals;
21 import static org.junit.Assert.assertNotNull;
22 import static org.junit.Assert.assertTrue;
23 import static org.junit.Assert.fail;
24 import static org.mockito.Mockito.mock;
25 import static org.mockito.Mockito.when;
26 import com.google.common.util.concurrent.ListenableFuture;
27 import java.util.concurrent.ExecutionException;
28 import org.eclipse.jetty.websocket.api.WebSocketPolicy;
29 import org.eclipse.jetty.websocket.servlet.WebSocketServletFactory;
30 import org.json.JSONObject;
31 import org.junit.Test;
32 import org.onap.ccsdk.features.sdnr.wt.websocketmanager2.WebSocketManager;
33 import org.onap.ccsdk.features.sdnr.wt.websocketmanager2.WebSocketManagerSocket;
34 import org.onap.ccsdk.features.sdnr.wt.websocketmanager2.WebSocketManagerSocket.EventInputCallback;
35 import org.onap.ccsdk.features.sdnr.wt.websocketmanager2.utils.AkkaConfig;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.websocketmanager.rev150105.WebsocketEventInput;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.websocketmanager.rev150105.WebsocketEventOutput;
38 import org.opendaylight.yangtools.yang.common.RpcResult;
40 public class WebsocketServerConnectTest {
42 private static final String XML1 = "<notification></notification>";
43 private static final String NODENAME = "abc";
44 private static final String EVENTTYPE = "test";
45 protected boolean responseReceived;
49 responseReceived = false;
50 AkkaConfig config = null;
52 // config = AkkaConfig.load("akka-singlenode.cfg", true);
53 config = AkkaConfig.loadContent(AkkaConfigTest.loadResourceContentAsString("akka-cluster-local.cfg"));
54 } catch (Exception e) {
56 fail("error loading singlenode config");
58 EventInputCallback callback = message -> {
59 JSONObject o = new JSONObject(message);
60 assertEquals("message which was pushed is not as expected", XML1,
61 o.get(WebSocketManagerSocket.KEY_XMLEVENT));
62 assertEquals("nodename which was pushed is not as expected", NODENAME,
63 o.get(WebSocketManagerSocket.KEY_NODENAME));
64 assertEquals("eventtype which was pushed is not as expected", EVENTTYPE,
65 o.get(WebSocketManagerSocket.KEY_EVENTTYPE));
66 responseReceived = true;
68 WebSocketManager servlet = new WebSocketManager(config, callback);
69 WebsocketEventInput input = mock(WebsocketEventInput.class);
70 when(input.getXmlEvent()).thenReturn(XML1);
71 when(input.getNodeName()).thenReturn(NODENAME);
72 when(input.getEventType()).thenReturn(EVENTTYPE);
73 ListenableFuture<RpcResult<WebsocketEventOutput>> result = servlet.websocketEvent(input);
74 assertNotNull(result);
75 RpcResult<WebsocketEventOutput> rpc = null;
78 } catch (InterruptedException | ExecutionException e) {
82 assertTrue("rpc result was not successful", rpc.isSuccessful());
83 assertTrue(rpc.getResult().getResponse().equals("OK"));
84 while (!responseReceived) {
87 } catch (InterruptedException e) {
93 WebSocketServletFactory factory = mock(WebSocketServletFactory.class);
94 WebSocketPolicy wspolicy = mock(WebSocketPolicy.class);
95 when(factory.getPolicy()).thenReturn(wspolicy);
96 servlet.configure(factory);