Test coverage imporvement and code refactoring of restconf discovery node.
[ccsdk/sli/plugins.git] / restconf-client / provider / src / test / java / org / onap / ccsdk / sli / plugins / restconfdiscovery / TestRestconfDiscoveryNode.java
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
33 import java.io.IOException;
34 import java.net.URI;
35 import java.util.HashMap;
36 import java.util.Map;
37
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;
44
45 public class TestRestconfDiscoveryNode {
46
47     private static final URI CONTEXT = URI.create("http://localhost:8080/");
48
49     @Test
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,
54                                                            resourceConfig);
55         server.start();
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();
61
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",
67                          "89");
68
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);
86         Thread.sleep(1000);
87         rdn.deleteSubscription(p, ctx);
88         server.shutdown();
89     }
90
91     @Test(expected = SvcLogicException.class)
92     public void testSubGraphExecution() throws SvcLogicException{
93         SvcLogicGraphInfo subDg = new SvcLogicGraphInfo();
94         subDg.mode("sync");
95         subDg.module("l3VpnService");
96         subDg.rpc("createVpn");
97         subDg.version("1.0");
98         SvcLogicContext ctx = new SvcLogicContext();
99         subDg.executeGraph(ctx);
100     }
101
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);
110     }
111
112     @Test
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),
120                    is("200"));
121         assertThat(rdn.getResponseCode(null, ctx),
122                    is("404"));
123     }
124
125     @Test
126     public void testOutputIdentifier() {
127         SvcLogicContext ctx = new SvcLogicContext();
128         ctx.setAttribute("restapi-result.ietf-subscribed-notifications:" +
129                                  "establish-subscription.output.identifier",
130                          "89");
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),
136                    is("89"));
137     }
138
139     @Test
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()));
146
147         assertThat(rdn.getTokenId(customHttpHeaders),
148                    is("x-ik2ps4ikvzupbx0486ft1ebzs7rt85futh9ho6eofy3wjsap7wqkt" +
149                               "emlqm4bbsmnar3vrtbyrzukbv5itd6m1cftldpjarnyle3sdcqq9h" +
150                               "ftc4lebz464b5ffxmlbvg9"));
151     }
152
153     @Test
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"));
174     }
175 }