1 package jtest.org.onap.ccsdk.sli.plugins.restapicall;
3 import org.junit.After;
4 import org.junit.Before;
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;
12 import java.util.HashMap;
15 import static org.junit.Assert.assertEquals;
17 public class TestSshApiCallNode {
19 private static final Logger log = LoggerFactory.getLogger(TestSshApiCallNode.class);
21 private SshApiCallNode adapter;
22 private String TestId;
23 private boolean testMode = true;
24 private Map<String, String> params;
25 private SvcLogicContext svcContext;
29 public void setup() throws IllegalArgumentException {
31 svcContext = new SvcLogicContext();
32 adapter = new SshApiCallNode();
34 params = new HashMap<>();
35 params.put("AgentUrl", "https://192.168.1.1");
36 params.put("User", "test");
37 params.put("Password", "test");
41 public void tearDown() {
48 @Test(expected = SvcLogicException.class)
49 public void testExecCommand_noUrlFailed() throws SvcLogicException,
50 IllegalStateException, IllegalArgumentException {
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);
60 @Test(expected = SvcLogicException.class)
61 public void testExecCommandPty_noUrlFailed() throws SvcLogicException,
62 IllegalStateException, IllegalArgumentException {
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);
72 @Test(expected = SvcLogicException.class)
73 public void testExecCommandResponse_noUrlFailed() throws SvcLogicException,
74 IllegalStateException, IllegalArgumentException {
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);
84 @Test(expected = SvcLogicException.class)
85 public void testExecCommand_noPortFailed() throws SvcLogicException,
86 IllegalStateException, IllegalArgumentException {
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);
95 @Test(expected = SvcLogicException.class)
96 public void testExecCommandPty_noPortFailed() throws SvcLogicException,
97 IllegalStateException, IllegalArgumentException {
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);
106 @Test(expected = SvcLogicException.class)
107 public void testExecCommandResponse_noPortFailed() throws SvcLogicException,
108 IllegalStateException, IllegalArgumentException {
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);
117 @Test(expected = SvcLogicException.class)
118 public void testExecCommand_noCmdFailed() throws SvcLogicException,
119 IllegalStateException, IllegalArgumentException {
121 params.put("Url", "test");
122 params.put("Port", "10");
123 adapter.execCommand(params, svcContext);
126 @Test(expected = SvcLogicException.class)
127 public void testExecCommandPty_noCmdFailed() throws SvcLogicException,
128 IllegalStateException, IllegalArgumentException {
130 params.put("Url", "test");
131 params.put("Port", "10");
132 adapter.execCommandWithPty(params, svcContext);
135 @Test(expected = SvcLogicException.class)
136 public void testExecCommandResponse_noCmdFailed() throws SvcLogicException,
137 IllegalStateException, IllegalArgumentException {
139 params.put("Url", "test");
140 params.put("Port", "10");
141 adapter.execWithStatusCheck(params, svcContext);
144 @Test(expected = SvcLogicException.class)
145 public void testExecCommandResponse_noSSHBasicFailed() throws SvcLogicException,
146 IllegalStateException, IllegalArgumentException {
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);
157 @Test(expected = SvcLogicException.class)
158 public void testExecCommandResponse_noSSHKeyFailed() throws SvcLogicException,
159 IllegalStateException, IllegalArgumentException {
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);
170 @Test(expected = SvcLogicException.class)
171 public void testExecCommandResponse_noSSHNoneFailed() throws SvcLogicException,
172 IllegalStateException, IllegalArgumentException {
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);
184 @Test(expected = SvcLogicException.class)
185 public void testExecCommandResponse_noSSHFailed() throws SvcLogicException,
186 IllegalStateException, IllegalArgumentException {
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);
197 @Test(expected = IllegalArgumentException.class)
198 public void testExecCommandResponse_noSSHInvalidParam() throws SvcLogicException,
199 IllegalStateException, IllegalArgumentException {
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);
210 @Test(expected = IllegalArgumentException.class)
211 public void testExecCommandResponse_noSSHInvalidAuthParam() throws SvcLogicException,
212 IllegalStateException, IllegalArgumentException {
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);
225 public void testExecCommandResponse_validJSON() throws SvcLogicException,
226 IllegalStateException, IllegalArgumentException {
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"));
243 public void testExecCommandResponse_validXML() throws SvcLogicException,
244 IllegalStateException, IllegalArgumentException {
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"));
261 public void testExecCommandResponse_validJSONPrefix() throws SvcLogicException,
262 IllegalStateException, IllegalArgumentException {
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"));
280 public void testExecCommandResponse_validXMLPrefix() throws SvcLogicException,
281 IllegalStateException, IllegalArgumentException {
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"));
298 @Test(expected = SvcLogicException.class)
299 public void testExecCommandResponse_validXMLFail() throws SvcLogicException,
300 IllegalStateException, IllegalArgumentException {
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);
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"));