b2094ca7304f309f669610b3623a2643d23e6d32
[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         RestconfDiscoveryNode rdn = new RestconfDiscoveryNode(
61                 new RestconfApiCallNode(new RestapiCallNode()));
62         rdn.establishPersistentConnection(p, ctx, "networkId");
63         Thread.sleep(2000);
64         rdn.deleteSubscription(p, ctx);
65     }
66
67     @Test(expected = SvcLogicException.class)
68     public void testSubGraphExecution() throws SvcLogicException{
69         SvcLogicGraphInfo subDg = new SvcLogicGraphInfo();
70         subDg.mode("sync");
71         subDg.module("l3VpnService");
72         subDg.rpc("createVpn");
73         subDg.version("1.0");
74         SvcLogicContext ctx = new SvcLogicContext();
75         subDg.executeGraph(ctx);
76     }
77
78     @Test(expected = SvcLogicException.class)
79     public void testEstablishSubscriptionWithoutSubscriberId()
80             throws SvcLogicException{
81         SvcLogicContext ctx = new SvcLogicContext();
82         Map<String, String> p = new HashMap<>();
83         RestconfDiscoveryNode rdn = new RestconfDiscoveryNode(
84                 new RestconfApiCallNode(new RestapiCallNode()));
85         rdn.establishSubscription(p, ctx);
86     }
87
88     @Test
89     public void testResponseCode() {
90         SvcLogicContext ctx = new SvcLogicContext();
91         ctx.setAttribute("restapi-result.response-code", "200");
92         ctx.setAttribute("response-code", "404");
93         RestconfDiscoveryNode rdn = new RestconfDiscoveryNode(
94                 new RestconfApiCallNode(new RestapiCallNode()));
95         assertThat(rdn.getResponseCode("restapi-result", ctx),
96                    is("200"));
97         assertThat(rdn.getResponseCode(null, ctx),
98                    is("404"));
99     }
100
101     @Test
102     public void testOutputIdentifier() {
103         SvcLogicContext ctx = new SvcLogicContext();
104         ctx.setAttribute("restapi-result.ietf-subscribed-notifications:" +
105                                  "establish-subscription.output.identifier",
106                          "89");
107         ctx.setAttribute("ietf-subscribed-notifications:establish-subscripti" +
108                                  "on.output.identifier", "89");
109         RestconfDiscoveryNode rdn = new RestconfDiscoveryNode(
110                 new RestconfApiCallNode(new RestapiCallNode()));
111         assertThat(rdn.getOutputIdentifier("restapi-result", ctx),
112                    is("89"));
113     }
114 }