c9a96f5f34205506cdf9eaa38e0371e8069e4a3b
[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.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;
39
40 public class WebsocketServerConnectTest {
41
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;
46
47     @Test
48     public void test() {
49         responseReceived = false;
50         AkkaConfig config = null;
51         try {
52             // config = AkkaConfig.load("akka-singlenode.cfg", true);
53             config = AkkaConfig.loadContent(AkkaConfigTest.loadResourceContentAsString("akka-cluster-local.cfg"));
54         } catch (Exception e) {
55             e.printStackTrace();
56             fail("error loading singlenode config");
57         }
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;
67         };
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;
76         try {
77             rpc = result.get();
78         } catch (InterruptedException | ExecutionException e) {
79             fail(e.getMessage());
80         }
81         assertNotNull(rpc);
82         assertTrue("rpc result was not successful", rpc.isSuccessful());
83         assertTrue(rpc.getResult().getResponse().equals("OK"));
84         while (!responseReceived) {
85             try {
86                 Thread.sleep(100);
87             } catch (InterruptedException e) {
88
89                 e.printStackTrace();
90             }
91         }
92
93         WebSocketServletFactory factory = mock(WebSocketServletFactory.class);
94         WebSocketPolicy wspolicy = mock(WebSocketPolicy.class);
95         when(factory.getPolicy()).thenReturn(wspolicy);
96         servlet.configure(factory);
97
98     }
99
100 }