64173306d570c2a60dfcada7f08f17f210fa706d
[ccsdk/sli.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - CCSDK
4  * ================================================================================
5  * Copyright (C) 2018 Huawei Technologies Co., Ltd. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.ccsdk.sli.plugins.restconfdiscovery;
22
23 import org.glassfish.grizzly.http.server.HttpServer;
24 import org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpServerFactory;
25 import org.glassfish.jersey.media.sse.SseFeature;
26 import org.glassfish.jersey.server.ResourceConfig;
27 import org.junit.Test;
28 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
29 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
30 import org.onap.ccsdk.sli.plugins.restapicall.RestapiCallNode;
31 import org.onap.ccsdk.sli.plugins.restconfapicall.RestconfApiCallNode;
32 import org.opendaylight.yangtools.yang.parser.impl.YangParserFactoryImpl;
33
34 import java.io.IOException;
35 import java.net.URI;
36 import java.util.HashMap;
37 import java.util.Map;
38
39 import static org.hamcrest.MatcherAssert.assertThat;
40 import static org.hamcrest.core.Is.is;
41 import static org.mockito.Matchers.any;
42 import static org.mockito.Mockito.doNothing;
43 import static org.mockito.Mockito.doReturn;
44 import static org.mockito.Mockito.mock;
45
46 public class TestRestconfDiscoveryNode {
47
48     private static final URI CONTEXT = URI.create("http://localhost:8080/");
49
50     @Test
51     public void sendRequest() throws SvcLogicException, InterruptedException, IOException {
52         final ResourceConfig resourceConfig = new ResourceConfig(
53                 SseServerMock.class, SseFeature.class);
54         HttpServer server = GrizzlyHttpServerFactory.createHttpServer(CONTEXT,
55                                                            resourceConfig);
56         server.start();
57         RestconfApiCallNode restconf = mock(RestconfApiCallNode.class);
58         doNothing().when(restconf)
59                 .sendRequest(any(Map.class), any(SvcLogicContext.class));
60         RestapiCallNode restApi = new RestapiCallNode();
61         doReturn(restApi).when(restconf).getRestapiCallNode();
62
63         SvcLogicContext ctx = new SvcLogicContext();
64         ctx.setAttribute("prop.encoding-json", "encoding-json");
65         ctx.setAttribute("restapi-result.response-code", "200");
66         ctx.setAttribute("restapi-result.ietf-subscribed-notifications" +
67                                  ":establish-subscription.output.identifier",
68                          "89");
69
70         Map<String, String> p = new HashMap<>();
71         p.put("sseConnectURL", "http://localhost:8080/events");
72         p.put("subscriberId", "networkId");
73         p.put("responsePrefix", "restapi-result");
74         p.put("restapiUser", "access");
75         p.put("restapiPassword", "abc@123");
76         p.put("customHttpHeaders", "X-ACCESS-TOKEN=x-ik2ps4ikvzupbx0486ft" +
77                 "1ebzs7rt85futh9ho6eofy3wjsap7wqktemlqm4bbsmnar3vrtbyrzuk" +
78                 "bv5itd6m1cftldpjarnyle3sdcqq9hftc4lebz464b5ffxmlbvg9");
79         p.put("restapiUrl", "https://localhost:8080/restconf/operations/" +
80                 "ietf-subscribed-notifications:establish-subscription");
81         p.put("module", "testmodule");
82         p.put("rpc", "testrpc");
83         p.put("version", "1.0");
84         p.put("mode", "sync");
85         RestconfDiscoveryNode rdn = new RestconfDiscoveryNode(restconf);
86         rdn.establishSubscription(p, ctx);
87         Thread.sleep(1000);
88         rdn.deleteSubscription(p, ctx);
89         server.shutdown();
90     }
91
92     @Test(expected = SvcLogicException.class)
93     public void testSubGraphExecution() throws SvcLogicException{
94         SvcLogicGraphInfo subDg = new SvcLogicGraphInfo();
95         subDg.mode("sync");
96         subDg.module("l3VpnService");
97         subDg.rpc("createVpn");
98         subDg.version("1.0");
99         SvcLogicContext ctx = new SvcLogicContext();
100         subDg.executeGraph(ctx);
101     }
102
103     @Test(expected = SvcLogicException.class)
104     public void testEstablishSubscriptionWithoutSubscriberId()
105             throws SvcLogicException{
106         SvcLogicContext ctx = new SvcLogicContext();
107         Map<String, String> p = new HashMap<>();
108
109         RestconfDiscoveryNode rdn = new RestconfDiscoveryNode(
110                 new RestconfApiCallNode(new RestapiCallNode(), new YangParserFactoryImpl()));
111         rdn.establishSubscription(p, ctx);
112     }
113
114     @Test
115     public void testResponseCode() {
116         SvcLogicContext ctx = new SvcLogicContext();
117         ctx.setAttribute("restapi-result.response-code", "200");
118         ctx.setAttribute("response-code", "404");
119         RestconfDiscoveryNode rdn = new RestconfDiscoveryNode(
120                 new RestconfApiCallNode(new RestapiCallNode(), new YangParserFactoryImpl()));
121         assertThat(rdn.getResponseCode("restapi-result", ctx),
122                    is("200"));
123         assertThat(rdn.getResponseCode(null, ctx),
124                    is("404"));
125     }
126
127     @Test
128     public void testOutputIdentifier() {
129         SvcLogicContext ctx = new SvcLogicContext();
130         ctx.setAttribute("restapi-result.ietf-subscribed-notifications:" +
131                                  "establish-subscription.output.identifier",
132                          "89");
133         ctx.setAttribute("ietf-subscribed-notifications:establish-subscripti" +
134                                  "on.output.identifier", "89");
135         RestconfDiscoveryNode rdn = new RestconfDiscoveryNode(
136                 new RestconfApiCallNode(new RestapiCallNode(), new YangParserFactoryImpl()));
137         assertThat(rdn.getOutputIdentifier("restapi-result", ctx),
138                    is("89"));
139     }
140
141     @Test
142     public void testGetTokenId() {
143         String customHttpHeaders = "X-ACCESS-TOKEN=x-ik2ps4ikvzupbx0486ft1ebzs7rt85" +
144                 "futh9ho6eofy3wjsap7wqktemlqm4bbsmnar3vrtbyrzukbv5itd6m1cftldpjarny" +
145                 "le3sdcqq9hftc4lebz464b5ffxmlbvg9";
146         RestconfDiscoveryNode rdn = new RestconfDiscoveryNode(
147                 new RestconfApiCallNode(new RestapiCallNode(), new YangParserFactoryImpl()));
148
149         assertThat(rdn.getTokenId(customHttpHeaders),
150                    is("x-ik2ps4ikvzupbx0486ft1ebzs7rt85futh9ho6eofy3wjsap7wqkt" +
151                               "emlqm4bbsmnar3vrtbyrzukbv5itd6m1cftldpjarnyle3sdcqq9h" +
152                               "ftc4lebz464b5ffxmlbvg9"));
153     }
154
155     @Test
156     public void testSubscriptionInfo() throws SvcLogicException {
157         SubscriptionInfo info = new SubscriptionInfo();
158         info.subscriberId("network-id");
159         info.subscriptionId("8");
160         info.filterUrl("/ietf-interfaces:interfaces");
161         info.yangFilePath("/opt/yang");
162         SvcLogicGraphInfo svcLogicGraphInfo = new SvcLogicGraphInfo();
163         svcLogicGraphInfo.mode("sync");
164         svcLogicGraphInfo.module("testModule");
165         svcLogicGraphInfo.rpc("testRpc");
166         svcLogicGraphInfo.version("1.0");
167         info.callBackDG(svcLogicGraphInfo);
168         assertThat(info.subscriberId(), is("network-id"));
169         assertThat(info.subscriptionId(), is("8"));
170         assertThat(info.filterUrl(), is("/ietf-interfaces:interfaces"));
171         assertThat(info.yangFilePath(), is("/opt/yang"));
172         assertThat(info.callBackDG().module(), is("testModule"));
173         assertThat(info.callBackDG().mode(), is("sync"));
174         assertThat(info.callBackDG().rpc(), is("testRpc"));
175         assertThat(info.callBackDG().version(), is("1.0"));
176     }
177 }