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;
33 import java.io.IOException;
35 import java.util.HashMap;
38 import static org.hamcrest.MatcherAssert.assertThat;
39 import static org.hamcrest.core.Is.is;
40 import static org.mockito.Matchers.any;
41 import static org.mockito.Mockito.doNothing;
42 import static org.mockito.Mockito.doReturn;
43 import static org.mockito.Mockito.mock;
45 public class TestRestconfDiscoveryNode {
47 private static final URI CONTEXT = URI.create("http://localhost:8080/");
50 public void sendRequest() throws SvcLogicException, InterruptedException, IOException {
51 final ResourceConfig resourceConfig = new ResourceConfig(
52 SseServerMock.class, SseFeature.class);
53 HttpServer server = GrizzlyHttpServerFactory.createHttpServer(CONTEXT,
56 RestconfApiCallNode restconf = mock(RestconfApiCallNode.class);
57 doNothing().when(restconf)
58 .sendRequest(any(Map.class), any(SvcLogicContext.class));
59 RestapiCallNode restApi = new RestapiCallNode();
60 doReturn(restApi).when(restconf).getRestapiCallNode();
62 SvcLogicContext ctx = new SvcLogicContext();
63 ctx.setAttribute("prop.encoding-json", "encoding-json");
64 ctx.setAttribute("restapi-result.response-code", "200");
65 ctx.setAttribute("restapi-result.ietf-subscribed-notifications" +
66 ":establish-subscription.output.identifier",
69 Map<String, String> p = new HashMap<>();
70 p.put("sseConnectURL", "http://localhost:8080/events");
71 p.put("subscriberId", "networkId");
72 p.put("responsePrefix", "restapi-result");
73 p.put("restapiUser", "access");
74 p.put("restapiPassword", "abc@123");
75 p.put("customHttpHeaders", "X-ACCESS-TOKEN=x-ik2ps4ikvzupbx0486ft" +
76 "1ebzs7rt85futh9ho6eofy3wjsap7wqktemlqm4bbsmnar3vrtbyrzuk" +
77 "bv5itd6m1cftldpjarnyle3sdcqq9hftc4lebz464b5ffxmlbvg9");
78 p.put("restapiUrl", "https://localhost:8080/restconf/operations/" +
79 "ietf-subscribed-notifications:establish-subscription");
80 p.put("module", "testmodule");
81 p.put("rpc", "testrpc");
82 p.put("version", "1.0");
83 p.put("mode", "sync");
84 RestconfDiscoveryNode rdn = new RestconfDiscoveryNode(restconf);
85 rdn.establishSubscription(p, ctx);
87 rdn.deleteSubscription(p, ctx);
91 @Test(expected = SvcLogicException.class)
92 public void testSubGraphExecution() throws SvcLogicException{
93 SvcLogicGraphInfo subDg = new SvcLogicGraphInfo();
95 subDg.module("l3VpnService");
96 subDg.rpc("createVpn");
98 SvcLogicContext ctx = new SvcLogicContext();
99 subDg.executeGraph(ctx);
102 @Test(expected = SvcLogicException.class)
103 public void testEstablishSubscriptionWithoutSubscriberId()
104 throws SvcLogicException{
105 SvcLogicContext ctx = new SvcLogicContext();
106 Map<String, String> p = new HashMap<>();
107 RestconfDiscoveryNode rdn = new RestconfDiscoveryNode(
108 new RestconfApiCallNode(new RestapiCallNode()));
109 rdn.establishSubscription(p, ctx);
113 public void testResponseCode() {
114 SvcLogicContext ctx = new SvcLogicContext();
115 ctx.setAttribute("restapi-result.response-code", "200");
116 ctx.setAttribute("response-code", "404");
117 RestconfDiscoveryNode rdn = new RestconfDiscoveryNode(
118 new RestconfApiCallNode(new RestapiCallNode()));
119 assertThat(rdn.getResponseCode("restapi-result", ctx),
121 assertThat(rdn.getResponseCode(null, ctx),
126 public void testOutputIdentifier() {
127 SvcLogicContext ctx = new SvcLogicContext();
128 ctx.setAttribute("restapi-result.ietf-subscribed-notifications:" +
129 "establish-subscription.output.identifier",
131 ctx.setAttribute("ietf-subscribed-notifications:establish-subscripti" +
132 "on.output.identifier", "89");
133 RestconfDiscoveryNode rdn = new RestconfDiscoveryNode(
134 new RestconfApiCallNode(new RestapiCallNode()));
135 assertThat(rdn.getOutputIdentifier("restapi-result", ctx),
140 public void testGetTokenId() {
141 String customHttpHeaders = "X-ACCESS-TOKEN=x-ik2ps4ikvzupbx0486ft1ebzs7rt85" +
142 "futh9ho6eofy3wjsap7wqktemlqm4bbsmnar3vrtbyrzukbv5itd6m1cftldpjarny" +
143 "le3sdcqq9hftc4lebz464b5ffxmlbvg9";
144 RestconfDiscoveryNode rdn = new RestconfDiscoveryNode(
145 new RestconfApiCallNode(new RestapiCallNode()));
147 assertThat(rdn.getTokenId(customHttpHeaders),
148 is("x-ik2ps4ikvzupbx0486ft1ebzs7rt85futh9ho6eofy3wjsap7wqkt" +
149 "emlqm4bbsmnar3vrtbyrzukbv5itd6m1cftldpjarnyle3sdcqq9h" +
150 "ftc4lebz464b5ffxmlbvg9"));
154 public void testSubscriptionInfo() throws SvcLogicException {
155 SubscriptionInfo info = new SubscriptionInfo();
156 info.subscriberId("network-id");
157 info.subscriptionId("8");
158 info.filterUrl("/ietf-interfaces:interfaces");
159 info.yangFilePath("/opt/yang");
160 SvcLogicGraphInfo svcLogicGraphInfo = new SvcLogicGraphInfo();
161 svcLogicGraphInfo.mode("sync");
162 svcLogicGraphInfo.module("testModule");
163 svcLogicGraphInfo.rpc("testRpc");
164 svcLogicGraphInfo.version("1.0");
165 info.callBackDG(svcLogicGraphInfo);
166 assertThat(info.subscriberId(), is("network-id"));
167 assertThat(info.subscriptionId(), is("8"));
168 assertThat(info.filterUrl(), is("/ietf-interfaces:interfaces"));
169 assertThat(info.yangFilePath(), is("/opt/yang"));
170 assertThat(info.callBackDG().module(), is("testModule"));
171 assertThat(info.callBackDG().mode(), is("sync"));
172 assertThat(info.callBackDG().rpc(), is("testRpc"));
173 assertThat(info.callBackDG().version(), is("1.0"));