RestconfDiscoveryNode Plugin implementation
[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 java.net.URI;
24 import java.util.HashMap;
25 import java.util.Map;
26
27 import org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpServerFactory;
28 import org.glassfish.jersey.media.sse.SseFeature;
29 import org.glassfish.jersey.server.ResourceConfig;
30 import org.junit.Test;
31 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
32 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
33 import static org.hamcrest.MatcherAssert.assertThat;
34 import static org.hamcrest.core.Is.is;
35
36 public class TestRestconfDiscoveryNode {
37
38     private static final URI CONTEXT = URI.create("http://localhost:8080/");
39
40     @Test
41     public void testEstablishPersistentConnection() throws SvcLogicException,
42             InterruptedException {
43         final ResourceConfig resourceConfig = new ResourceConfig(
44                 SseServerMock.class, SseFeature.class);
45         GrizzlyHttpServerFactory.createHttpServer(CONTEXT, resourceConfig);
46         SvcLogicContext ctx = new SvcLogicContext();
47         ctx.setAttribute("prop.encoding-json", "encoding-json");
48         ctx.setAttribute("restapi-result.response-code", "200");
49         ctx.setAttribute("restapi-result.ietf-subscribed-notifications" +
50                                  ":output.identifier", "100");
51
52         Map<String, String> p = new HashMap<>();
53         p.put("sseConnectURL", "http://localhost:8080/events");
54         p.put("subscriberId", "networkId");
55         p.put("responsePrefix", "restapi-result");
56         RestconfDiscoveryNode rdn = new RestconfDiscoveryNode();
57         rdn.establishPersistentConnection(p, ctx, "networkId");
58         Thread.sleep(2000);
59         rdn.deleteSubscription(p, ctx);
60     }
61
62     @Test(expected = SvcLogicException.class)
63     public void testSubGraphExecution() throws SvcLogicException{
64         SvcLogicGraphInfo subDg = new SvcLogicGraphInfo();
65         subDg.mode("sync");
66         subDg.module("l3VpnService");
67         subDg.rpc("createVpn");
68         subDg.version("1.0");
69         SvcLogicContext ctx = new SvcLogicContext();
70         subDg.executeGraph(ctx);
71     }
72
73     @Test(expected = SvcLogicException.class)
74     public void testEstablishSubscriptionWithoutSubscriberId()
75             throws SvcLogicException{
76         SvcLogicContext ctx = new SvcLogicContext();
77         Map<String, String> p = new HashMap<>();
78         RestconfDiscoveryNode rdn = new RestconfDiscoveryNode();
79         rdn.establishSubscription(p, ctx);
80     }
81
82     @Test
83     public void testResponseCode() {
84         SvcLogicContext ctx = new SvcLogicContext();
85         ctx.setAttribute("restapi-result.response-code", "200");
86         ctx.setAttribute("response-code", "404");
87         RestconfDiscoveryNode rdn = new RestconfDiscoveryNode();
88         assertThat(rdn.getResponseCode("restapi-result", ctx),
89                    is("200"));
90         assertThat(rdn.getResponseCode(null, ctx),
91                    is("404"));
92     }
93
94     @Test
95     public void testOutputIdentifier() {
96         SvcLogicContext ctx = new SvcLogicContext();
97         ctx.setAttribute("restapi-result.ietf-subscribed-notifications:" +
98                                  "output.identifier", "89");
99         ctx.setAttribute("ietf-subscribed-notifications:output.identifier",
100                          "89");
101         RestconfDiscoveryNode rdn = new RestconfDiscoveryNode();
102         assertThat(rdn.getOutputIdentifier("restapi-result", ctx),
103                    is("89"));
104     }
105 }