Add authorization header in SSE request
[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.jersey.grizzly2.httpserver.GrizzlyHttpServerFactory;
24 import org.glassfish.jersey.media.sse.SseFeature;
25 import org.glassfish.jersey.server.ResourceConfig;
26 import org.junit.Test;
27 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
28 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
29 import org.onap.ccsdk.sli.plugins.restapicall.RestapiCallNode;
30 import org.onap.ccsdk.sli.plugins.restconfapicall.RestconfApiCallNode;
31
32 import java.net.URI;
33 import java.util.HashMap;
34 import java.util.Map;
35
36 import static org.hamcrest.MatcherAssert.assertThat;
37 import static org.hamcrest.core.Is.is;
38
39 public class TestRestconfDiscoveryNode {
40
41     private static final URI CONTEXT = URI.create("http://localhost:8080/");
42
43     @Test
44     public void testEstablishPersistentConnection() throws SvcLogicException,
45             InterruptedException {
46         final ResourceConfig resourceConfig = new ResourceConfig(
47                 SseServerMock.class, SseFeature.class);
48         GrizzlyHttpServerFactory.createHttpServer(CONTEXT, resourceConfig);
49         SvcLogicContext ctx = new SvcLogicContext();
50         ctx.setAttribute("prop.encoding-json", "encoding-json");
51         ctx.setAttribute("restapi-result.response-code", "200");
52         ctx.setAttribute("restapi-result.ietf-subscribed-notifications" +
53                                  ":establish-subscription.output.identifier",
54                          "100");
55
56         Map<String, String> p = new HashMap<>();
57         p.put("sseConnectURL", "http://localhost:8080/events");
58         p.put("subscriberId", "networkId");
59         p.put("responsePrefix", "restapi-result");
60         p.put("restapiUser", "access");
61         p.put("restapiPassword", "abc@123");
62         p.put("customHttpHeaders", "X-ACCESS-TOKEN=x-ik2ps4ikvzupbx0486ft" +
63                 "1ebzs7rt85futh9ho6eofy3wjsap7wqktemlqm4bbsmnar3vrtbyrzuk" +
64                 "bv5itd6m1cftldpjarnyle3sdcqq9hftc4lebz464b5ffxmlbvg9");
65         p.put("restapiUrl", "https://localhost:8080/restconf/operations/" +
66                 "ietf-subscribed-notifications:establish-subscription");
67         RestconfDiscoveryNode rdn = new RestconfDiscoveryNode(
68                 new RestconfApiCallNode(new RestapiCallNode()));
69         rdn.establishPersistentConnection(p, ctx, "networkId");
70         Thread.sleep(2000);
71         rdn.deleteSubscription(p, ctx);
72     }
73
74     @Test(expected = SvcLogicException.class)
75     public void testSubGraphExecution() throws SvcLogicException{
76         SvcLogicGraphInfo subDg = new SvcLogicGraphInfo();
77         subDg.mode("sync");
78         subDg.module("l3VpnService");
79         subDg.rpc("createVpn");
80         subDg.version("1.0");
81         SvcLogicContext ctx = new SvcLogicContext();
82         subDg.executeGraph(ctx);
83     }
84
85     @Test(expected = SvcLogicException.class)
86     public void testEstablishSubscriptionWithoutSubscriberId()
87             throws SvcLogicException{
88         SvcLogicContext ctx = new SvcLogicContext();
89         Map<String, String> p = new HashMap<>();
90         RestconfDiscoveryNode rdn = new RestconfDiscoveryNode(
91                 new RestconfApiCallNode(new RestapiCallNode()));
92         rdn.establishSubscription(p, ctx);
93     }
94
95     @Test
96     public void testResponseCode() {
97         SvcLogicContext ctx = new SvcLogicContext();
98         ctx.setAttribute("restapi-result.response-code", "200");
99         ctx.setAttribute("response-code", "404");
100         RestconfDiscoveryNode rdn = new RestconfDiscoveryNode(
101                 new RestconfApiCallNode(new RestapiCallNode()));
102         assertThat(rdn.getResponseCode("restapi-result", ctx),
103                    is("200"));
104         assertThat(rdn.getResponseCode(null, ctx),
105                    is("404"));
106     }
107
108     @Test
109     public void testOutputIdentifier() {
110         SvcLogicContext ctx = new SvcLogicContext();
111         ctx.setAttribute("restapi-result.ietf-subscribed-notifications:" +
112                                  "establish-subscription.output.identifier",
113                          "89");
114         ctx.setAttribute("ietf-subscribed-notifications:establish-subscripti" +
115                                  "on.output.identifier", "89");
116         RestconfDiscoveryNode rdn = new RestconfDiscoveryNode(
117                 new RestconfApiCallNode(new RestapiCallNode()));
118         assertThat(rdn.getOutputIdentifier("restapi-result", ctx),
119                    is("89"));
120     }
121
122     @Test
123     public void testGetTokenId() {
124         String customHttpHeaders = "X-ACCESS-TOKEN=x-ik2ps4ikvzupbx0486ft1ebzs7rt85" +
125                 "futh9ho6eofy3wjsap7wqktemlqm4bbsmnar3vrtbyrzukbv5itd6m1cftldpjarny" +
126                 "le3sdcqq9hftc4lebz464b5ffxmlbvg9";
127         RestconfDiscoveryNode rdn = new RestconfDiscoveryNode(
128                 new RestconfApiCallNode(new RestapiCallNode()));
129
130         assertThat(rdn.getTokenId(customHttpHeaders),
131                    is("x-ik2ps4ikvzupbx0486ft1ebzs7rt85futh9ho6eofy3wjsap7wqkt" +
132                               "emlqm4bbsmnar3vrtbyrzukbv5itd6m1cftldpjarnyle3sdcqq9h" +
133                               "ftc4lebz464b5ffxmlbvg9"));
134     }
135 }