Merge "Using specific exceptions in RestApiCallNode"
[ccsdk/sli/plugins.git] / restapi-call-node / provider / src / test / java / jtest / org / onap / ccsdk / sli / plugins / restapicall / TestRestapiCallNode.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * openECOMP : SDN-C
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights
6  *                      reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  * 
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  * 
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package jtest.org.onap.ccsdk.sli.plugins.restapicall;
23
24 import java.util.HashMap;
25 import java.util.Map;
26
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.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34 public class TestRestapiCallNode {
35
36     private static final Logger log = LoggerFactory.getLogger(TestRestapiCallNode.class);
37
38
39     @Test
40     public void testDelete() throws SvcLogicException {
41         SvcLogicContext ctx = new SvcLogicContext();
42
43         Map<String, String> p = new HashMap<String, String>();
44         p.put("restapiUrl", "https://echo.getpostman.com/delete");
45         p.put("restapiUser", "user1");
46         p.put("restapiPassword", "pwd1");
47         p.put("httpMethod", "delete");
48         p.put("skipSending", "true");
49
50         RestapiCallNode rcn = new RestapiCallNode();
51         rcn.sendRequest(p, ctx);
52     }
53
54     @Test
55     public void testJsonTemplate() throws SvcLogicException {
56         SvcLogicContext ctx = new SvcLogicContext();
57         ctx.setAttribute("tmp.sdn-circuit-req-row_length", "3");
58         ctx.setAttribute("tmp.sdn-circuit-req-row[0].source-uid", "APIDOC-123");
59         ctx.setAttribute("tmp.sdn-circuit-req-row[0].action", "delete");
60         ctx.setAttribute("tmp.sdn-circuit-req-row[0].request-timestamp", "2016-09-09 16:30:35.0");
61         ctx.setAttribute("tmp.sdn-circuit-req-row[0].request-status", "New");
62         ctx.setAttribute("tmp.sdn-circuit-req-row[0].processing-status", "New");
63         ctx.setAttribute("tmp.sdn-circuit-req-row[0].service-clfi", "testClfi1");
64         ctx.setAttribute("tmp.sdn-circuit-req-row[0].clci", "clci");
65         ctx.setAttribute("tmp.sdn-circuit-req-row[1].source-uid", "APIDOC-123");
66         ctx.setAttribute("tmp.sdn-circuit-req-row[1].action", "delete");
67         ctx.setAttribute("tmp.sdn-circuit-req-row[1].request-timestamp", "2016-09-09 16:30:35.0");
68         ctx.setAttribute("tmp.sdn-circuit-req-row[1].request-status", "New");
69         ctx.setAttribute("tmp.sdn-circuit-req-row[1].processing-status", "New");
70         ctx.setAttribute("tmp.sdn-circuit-req-row[1].service-clfi", "testClfi1");
71         ctx.setAttribute("tmp.sdn-circuit-req-row[1].clci", "clci");
72         ctx.setAttribute("tmp.sdn-circuit-req-row[2].source-uid", "APIDOC-123");
73         ctx.setAttribute("tmp.sdn-circuit-req-row[2].action", "delete");
74         ctx.setAttribute("tmp.sdn-circuit-req-row[2].request-timestamp", "2016-09-09 16:30:35.0");
75         ctx.setAttribute("tmp.sdn-circuit-req-row[2].request-status", "New");
76         ctx.setAttribute("tmp.sdn-circuit-req-row[2].processing-status", "New");
77         ctx.setAttribute("tmp.sdn-circuit-req-row[2].service-clfi", "testClfi1");
78         ctx.setAttribute("tmp.sdn-circuit-req-row[2].clci", "clci");
79
80         Map<String, String> p = new HashMap<String, String>();
81         p.put("templateFileName", "src/test/resources/test-template.json");
82         p.put("restapiUrl", "http://echo.getpostman.com");
83         p.put("restapiUser", "user1");
84         p.put("restapiPassword", "abc123");
85         p.put("format", "json");
86         p.put("httpMethod", "post");
87         p.put("responsePrefix", "response");
88         p.put("skipSending", "true");
89
90         RestapiCallNode rcn = new RestapiCallNode();
91         rcn.sendRequest(p, ctx);
92     }
93
94     @Test(expected = SvcLogicException.class)
95     public void testInvalidRepeatTimes() throws SvcLogicException {
96         SvcLogicContext ctx = new SvcLogicContext();
97         ctx.setAttribute("tmp.sdn-circuit-req-row_length", "a");
98         ctx.setAttribute("tmp.sdn-circuit-req-row[0].source-uid", "APIDOC-123");
99         ctx.setAttribute("tmp.sdn-circuit-req-row[0].action", "delete");
100         ctx.setAttribute("tmp.sdn-circuit-req-row[0].request-timestamp", "2016-09-09 16:30:35.0");
101         ctx.setAttribute("tmp.sdn-circuit-req-row[0].request-status", "New");
102         ctx.setAttribute("tmp.sdn-circuit-req-row[0].processing-status", "New");
103         ctx.setAttribute("tmp.sdn-circuit-req-row[0].service-clfi", "testClfi1");
104         ctx.setAttribute("tmp.sdn-circuit-req-row[0].clci", "clci");
105
106         Map<String, String> p = new HashMap<String, String>();
107         p.put("templateFileName", "src/test/resources/test-template.json");
108         p.put("restapiUrl", "http://echo.getpostman.com");
109         p.put("restapiUser", "user1");
110         p.put("restapiPassword", "abc123");
111         p.put("format", "json");
112         p.put("httpMethod", "post");
113         p.put("responsePrefix", "response");
114         p.put("skipSending", "true");
115
116         RestapiCallNode rcn = new RestapiCallNode();
117         rcn.sendRequest(p, ctx);
118     }
119
120     @Test(expected = SvcLogicException.class)
121     public void testInvalidTemplatePath() throws SvcLogicException {
122         SvcLogicContext ctx = new SvcLogicContext();
123         ctx.setAttribute("tmp.sdn-circuit-req-row_length", "1");
124         ctx.setAttribute("tmp.sdn-circuit-req-row[0].source-uid", "APIDOC-123");
125         ctx.setAttribute("tmp.sdn-circuit-req-row[0].action", "delete");
126         ctx.setAttribute("tmp.sdn-circuit-req-row[0].request-timestamp", "2016-09-09 16:30:35.0");
127         ctx.setAttribute("tmp.sdn-circuit-req-row[0].request-status", "New");
128         ctx.setAttribute("tmp.sdn-circuit-req-row[0].processing-status", "New");
129         ctx.setAttribute("tmp.sdn-circuit-req-row[0].service-clfi", "testClfi1");
130         ctx.setAttribute("tmp.sdn-circuit-req-row[0].clci", "clci");
131
132         Map<String, String> p = new HashMap<String, String>();
133         p.put("templateFileName", "src/test/resourcess/test-template.json");
134         p.put("restapiUrl", "http://echo.getpostman.com");
135         p.put("restapiUser", "user1");
136         p.put("restapiPassword", "abc123");
137         p.put("format", "json");
138         p.put("httpMethod", "post");
139         p.put("responsePrefix", "response");
140         p.put("skipSending", "true");
141
142         RestapiCallNode rcn = new RestapiCallNode();
143         rcn.sendRequest(p, ctx);
144     }
145
146     @Test(expected = SvcLogicException.class)
147     public void testWithoutSkipSending() throws SvcLogicException {
148         SvcLogicContext ctx = new SvcLogicContext();
149         ctx.setAttribute("tmp.sdn-circuit-req-row_length", "1");
150         ctx.setAttribute("tmp.sdn-circuit-req-row[0].source-uid", "APIDOC-123");
151         ctx.setAttribute("tmp.sdn-circuit-req-row[0].action", "delete");
152         ctx.setAttribute("tmp.sdn-circuit-req-row[0].request-timestamp", "2016-09-09 16:30:35.0");
153         ctx.setAttribute("tmp.sdn-circuit-req-row[0].request-status", "New");
154         ctx.setAttribute("tmp.sdn-circuit-req-row[0].processing-status", "New");
155         ctx.setAttribute("tmp.sdn-circuit-req-row[0].service-clfi", "testClfi1");
156         ctx.setAttribute("tmp.sdn-circuit-req-row[0].clci", "clci");
157
158         Map<String, String> p = new HashMap<String, String>();
159         p.put("templateFileName", "src/test/resources/test-template.json");
160         p.put("restapiUrl", "http://echo.getpostman.com");
161         p.put("restapiUser", "user1");
162         p.put("restapiPassword", "abc123");
163         p.put("format", "json");
164         p.put("httpMethod", "post");
165         p.put("responsePrefix", "response");
166         p.put("skipSending", "false");
167
168         RestapiCallNode rcn = new RestapiCallNode();
169         rcn.sendRequest(p, ctx);
170     }
171 }