Invalid package name for restapicallnode test
[ccsdk/sli/plugins.git] / sshapi-call-node / provider / src / test / java / jtest / org / onap / ccsdk / sli / plugins / restapicall / TestSshApiCallNode.java
1 package jtest.org.onap.ccsdk.sli.plugins.restapicall;
2
3 import org.junit.After;
4 import org.junit.Before;
5 import org.junit.Test;
6 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
7 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
8 import org.onap.ccsdk.sli.plugins.sshapicall.SshApiCallNode;
9 import org.slf4j.Logger;
10 import org.slf4j.LoggerFactory;
11
12 import java.util.HashMap;
13 import java.util.Map;
14
15 import static org.junit.Assert.assertEquals;
16
17 public class TestSshApiCallNode {
18
19     private static final Logger log = LoggerFactory.getLogger(TestSshApiCallNode.class);
20
21     private SshApiCallNode adapter;
22     private String TestId;
23     private boolean testMode = true;
24     private Map<String, String> params;
25     private SvcLogicContext svcContext;
26
27
28     @Before
29     public void setup() throws IllegalArgumentException {
30         testMode = true;
31         svcContext = new SvcLogicContext();
32         adapter = new SshApiCallNode();
33
34         params = new HashMap<>();
35         params.put("AgentUrl", "https://192.168.1.1");
36         params.put("User", "test");
37         params.put("Password", "test");
38     }
39
40     @After
41     public void tearDown() {
42         testMode = false;
43         adapter = null;
44         params = null;
45         svcContext = null;
46     }
47
48     @Test(expected = SvcLogicException.class)
49     public void testExecCommand_noUrlFailed() throws SvcLogicException,
50             IllegalStateException, IllegalArgumentException {
51
52         params.put("HostName", "test");
53         params.put("Port", "10");
54         params.put("User", "test");
55         params.put("Password", "test");
56         params.put("Test", "fail");
57         adapter.execCommand(params, svcContext);
58     }
59
60     @Test(expected = SvcLogicException.class)
61     public void testExecCommandPty_noUrlFailed() throws SvcLogicException,
62             IllegalStateException, IllegalArgumentException {
63
64         params.put("HostName", "test");
65         params.put("Port", "10");
66         params.put("User", "test");
67         params.put("Password", "test");
68         params.put("Test", "fail");
69         adapter.execCommandWithPty(params, svcContext);
70     }
71
72     @Test(expected = SvcLogicException.class)
73     public void testExecCommandResponse_noUrlFailed() throws SvcLogicException,
74             IllegalStateException, IllegalArgumentException {
75
76         params.put("HostName", "test");
77         params.put("Port", "10");
78         params.put("User", "test");
79         params.put("Password", "test");
80         params.put("Test", "fail");
81         adapter.execWithStatusCheck(params, svcContext);
82     }
83
84     @Test(expected = SvcLogicException.class)
85     public void testExecCommand_noPortFailed() throws SvcLogicException,
86             IllegalStateException, IllegalArgumentException {
87
88         params.put("Url", "test");
89         params.put("User", "test");
90         params.put("Password", "test");
91         params.put("Test", "fail");
92         adapter.execCommand(params, svcContext);
93     }
94
95     @Test(expected = SvcLogicException.class)
96     public void testExecCommandPty_noPortFailed() throws SvcLogicException,
97             IllegalStateException, IllegalArgumentException {
98
99         params.put("Url", "test");
100         params.put("User", "test");
101         params.put("Password", "test");
102         params.put("Test", "fail");
103         adapter.execCommandWithPty(params, svcContext);
104     }
105
106     @Test(expected = SvcLogicException.class)
107     public void testExecCommandResponse_noPortFailed() throws SvcLogicException,
108             IllegalStateException, IllegalArgumentException {
109
110         params.put("Url", "test");
111         params.put("User", "test");
112         params.put("Password", "test");
113         params.put("Test", "fail");
114         adapter.execWithStatusCheck(params, svcContext);
115     }
116
117     @Test(expected = SvcLogicException.class)
118     public void testExecCommand_noCmdFailed() throws SvcLogicException,
119             IllegalStateException, IllegalArgumentException {
120
121         params.put("Url", "test");
122         params.put("Port", "10");
123         adapter.execCommand(params, svcContext);
124     }
125
126     @Test(expected = SvcLogicException.class)
127     public void testExecCommandPty_noCmdFailed() throws SvcLogicException,
128             IllegalStateException, IllegalArgumentException {
129
130         params.put("Url", "test");
131         params.put("Port", "10");
132         adapter.execCommandWithPty(params, svcContext);
133     }
134
135     @Test(expected = SvcLogicException.class)
136     public void testExecCommandResponse_noCmdFailed() throws SvcLogicException,
137             IllegalStateException, IllegalArgumentException {
138
139         params.put("Url", "test");
140         params.put("Port", "10");
141         adapter.execWithStatusCheck(params, svcContext);
142     }
143
144     @Test(expected = SvcLogicException.class)
145     public void testExecCommandResponse_noSSHBasicFailed() throws SvcLogicException,
146             IllegalStateException, IllegalArgumentException {
147
148         params.put("Url", "test");
149         params.put("Port", "10");
150         params.put("User", "test");
151         params.put("Password", "test");
152         params.put("AuthType", "basic");
153         params.put("Cmd", "test");
154         adapter.execWithStatusCheck(params, svcContext);
155     }
156
157     @Test(expected = SvcLogicException.class)
158     public void testExecCommandResponse_noSSHKeyFailed() throws SvcLogicException,
159             IllegalStateException, IllegalArgumentException {
160
161         params.put("Url", "test");
162         params.put("Port", "10");
163         params.put("User", "test");
164         params.put("Password", "test");
165         params.put("AuthType", "key");
166         params.put("Cmd", "test");
167         adapter.execWithStatusCheck(params, svcContext);
168     }
169
170     @Test(expected = SvcLogicException.class)
171     public void testExecCommandResponse_noSSHNoneFailed() throws SvcLogicException,
172             IllegalStateException, IllegalArgumentException {
173
174         params.put("Url", "test");
175         params.put("Port", "10");
176         params.put("User", "test");
177         params.put("Password", "test");
178         params.put("AuthType", "none");
179         params.put("Cmd", "test");
180         params.put("ResponseType", "xml");
181         adapter.execWithStatusCheck(params, svcContext);
182     }
183
184     @Test(expected = SvcLogicException.class)
185     public void testExecCommandResponse_noSSHFailed() throws SvcLogicException,
186             IllegalStateException, IllegalArgumentException {
187
188         params.put("Url", "test");
189         params.put("Port", "10");
190         params.put("User", "test");
191         params.put("Password", "test");
192         params.put("Cmd", "test");
193         params.put("ResponseType", "json");
194         adapter.execWithStatusCheck(params, svcContext);
195     }
196
197     @Test(expected = IllegalArgumentException.class)
198     public void testExecCommandResponse_noSSHInvalidParam() throws SvcLogicException,
199             IllegalStateException, IllegalArgumentException {
200
201         params.put("Url", "test");
202         params.put("Port", "10");
203         params.put("User", "test");
204         params.put("Password", "test");
205         params.put("Cmd", "test");
206         params.put("ResponseType", "txt");
207         adapter.execWithStatusCheck(params, svcContext);
208     }
209
210     @Test(expected = IllegalArgumentException.class)
211     public void testExecCommandResponse_noSSHInvalidAuthParam() throws SvcLogicException,
212             IllegalStateException, IllegalArgumentException {
213
214         params.put("Url", "test");
215         params.put("Port", "10");
216         params.put("User", "test");
217         params.put("Password", "test");
218         params.put("Cmd", "test");
219         params.put("AuthType", "spring");
220         params.put("ResponseType", "json");
221         adapter.execWithStatusCheck(params, svcContext);
222     }
223
224     @Test
225     public void testExecCommandResponse_validJSON() throws SvcLogicException,
226             IllegalStateException, IllegalArgumentException {
227
228         params.put("Url", "test");
229         params.put("Port", "10");
230         params.put("User", "test");
231         params.put("Password", "test");
232         params.put("Cmd", "test");
233         params.put("AuthType", "basic");
234         params.put("ResponseType", "json");
235         params.put("TestOut", "{\"equipment-data\":\"boo\"}");
236         params.put("TestFail", "false");
237         adapter = new SshApiCallNode(true);
238         adapter.execWithStatusCheck(params, svcContext);
239         assertEquals("boo", svcContext.getAttribute("equipment-data"));
240     }
241
242     @Test
243     public void testExecCommandResponse_validXML() throws SvcLogicException,
244             IllegalStateException, IllegalArgumentException {
245
246         params.put("Url", "test");
247         params.put("Port", "10");
248         params.put("User", "test");
249         params.put("Password", "test");
250         params.put("Cmd", "test");
251         params.put("AuthType", "basic");
252         params.put("ResponseType", "xml");
253         params.put("TestOut", "<modelVersion>4.0.0</modelVersion>");
254         params.put("TestFail", "false");
255         adapter = new SshApiCallNode(true);
256         adapter.execWithStatusCheck(params, svcContext);
257         assertEquals("4.0.0", svcContext.getAttribute("modelVersion"));
258     }
259
260     @Test
261     public void testExecCommandResponse_validJSONPrefix() throws SvcLogicException,
262             IllegalStateException, IllegalArgumentException {
263
264         params.put("Url", "test");
265         params.put("Port", "10");
266         params.put("User", "test");
267         params.put("Password", "test");
268         params.put("Cmd", "test");
269         params.put("AuthType", "basic");
270         params.put("ResponseType", "json");
271         params.put("TestOut", "{\"equipment-data\":\"boo\"}");
272         params.put("ResponsePrefix", "test");
273         params.put("TestFail", "false");
274         adapter = new SshApiCallNode(true);
275         adapter.execWithStatusCheck(params, svcContext);
276         assertEquals("boo", svcContext.getAttribute("test.equipment-data"));
277     }
278
279     @Test
280     public void testExecCommandResponse_validXMLPrefix() throws SvcLogicException,
281             IllegalStateException, IllegalArgumentException {
282
283         params.put("Url", "test");
284         params.put("Port", "10");
285         params.put("User", "test");
286         params.put("Password", "test");
287         params.put("Cmd", "test");
288         params.put("AuthType", "basic");
289         params.put("ResponseType", "xml");
290         params.put("TestOut", "<modelVersion>4.0.0</modelVersion>");
291         params.put("ResponsePrefix", "test");
292         params.put("TestFail", "false");
293         adapter = new SshApiCallNode(true);
294         adapter.execWithStatusCheck(params, svcContext);
295         assertEquals("4.0.0", svcContext.getAttribute("test.modelVersion"));
296     }
297
298     @Test(expected = SvcLogicException.class)
299     public void testExecCommandResponse_validXMLFail() throws SvcLogicException,
300             IllegalStateException, IllegalArgumentException {
301
302         params.put("Url", "test");
303         params.put("Port", "10");
304         params.put("User", "test");
305         params.put("Password", "test");
306         params.put("Cmd", "test");
307         params.put("AuthType", "basic");
308         params.put("ResponseType", "xml");
309         params.put("TestOut", "<modelVersion>4.0.0</modelVersion>");
310         params.put("TestFail", "true");
311         params.put("ResponsePrefix", "test");
312         adapter = new SshApiCallNode(true);
313         adapter.execWithStatusCheck(params, svcContext);
314     }
315
316     @Test(expected = SvcLogicException.class)
317     public void testExecCommandResponse_validXMLPrefixKey() throws SvcLogicException,
318             IllegalStateException, IllegalArgumentException {
319         params = new HashMap<>();
320         params.put("Url", "test");
321         params.put("Port", "10");
322         params.put("SshKey", "test");
323         params.put("Cmd", "test");
324         params.put("ResponseType", "xml");
325         params.put("TestOut", "<modelVersion>4.0.0</modelVersion>");
326         params.put("ResponsePrefix", "test");
327         adapter.execWithStatusCheck(params, svcContext);
328         assertEquals("4.0.0", svcContext.getAttribute("test.modelVersion"));
329     }
330 }