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