2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.ccsdk.sli.plugins.restconfdiscovery;
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;
34 import java.io.IOException;
36 import java.util.HashMap;
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;
46 public class TestRestconfDiscoveryNode {
48 private static final URI CONTEXT = URI.create("http://localhost:8080/");
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,
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();
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",
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);
88 rdn.deleteSubscription(p, ctx);
92 @Test(expected = SvcLogicException.class)
93 public void testSubGraphExecution() throws SvcLogicException{
94 SvcLogicGraphInfo subDg = new SvcLogicGraphInfo();
96 subDg.module("l3VpnService");
97 subDg.rpc("createVpn");
99 SvcLogicContext ctx = new SvcLogicContext();
100 subDg.executeGraph(ctx);
103 @Test(expected = SvcLogicException.class)
104 public void testEstablishSubscriptionWithoutSubscriberId()
105 throws SvcLogicException{
106 SvcLogicContext ctx = new SvcLogicContext();
107 Map<String, String> p = new HashMap<>();
109 RestconfDiscoveryNode rdn = new RestconfDiscoveryNode(
110 new RestconfApiCallNode(new RestapiCallNode(), new YangParserFactoryImpl()));
111 rdn.establishSubscription(p, ctx);
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),
123 assertThat(rdn.getResponseCode(null, ctx),
128 public void testOutputIdentifier() {
129 SvcLogicContext ctx = new SvcLogicContext();
130 ctx.setAttribute("restapi-result.ietf-subscribed-notifications:" +
131 "establish-subscription.output.identifier",
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),
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()));
149 assertThat(rdn.getTokenId(customHttpHeaders),
150 is("x-ik2ps4ikvzupbx0486ft1ebzs7rt85futh9ho6eofy3wjsap7wqkt" +
151 "emlqm4bbsmnar3vrtbyrzukbv5itd6m1cftldpjarnyle3sdcqq9h" +
152 "ftc4lebz464b5ffxmlbvg9"));
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"));