2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Copyright (C) 2017 Amdocs
8 * =============================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
21 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 * ============LICENSE_END=========================================================
25 package org.onap.appc.adapter.impl;
27 import org.junit.After;
28 import org.junit.Before;
29 import org.junit.Test;
30 import org.onap.ccsdk.sli.adaptors.saltstack.impl.SaltstackAdapterImpl;
31 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
32 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
34 import java.util.HashMap;
37 import static org.junit.Assert.assertEquals;
38 import static org.junit.Assert.fail;
41 public class TestSaltstackAdapterImpl {
43 private final String PENDING = "100";
44 private final String SUCCESS = "400";
45 private String message = "{\"Results\":{\"192.168.1.10\":{\"Id\":\"101\",\"StatusCode\":200,\"StatusMessage\":\"SUCCESS\"}},\"StatusCode\":200,\"StatusMessage\":\"FINISHED\"}";
47 private SaltstackAdapterImpl adapter;
48 private String TestId;
49 private boolean testMode = true;
50 private Map<String, String> params;
51 private SvcLogicContext svcContext;
55 public void setup() throws IllegalArgumentException {
57 svcContext = new SvcLogicContext();
58 adapter = new SaltstackAdapterImpl(testMode);
60 params = new HashMap<>();
61 params.put("AgentUrl", "https://192.168.1.1");
62 params.put("User", "test");
63 params.put("Password", "test");
67 public void tearDown() {
74 @Test(expected = SvcLogicException.class)
75 public void reqExecCommand_shouldSetFailed() throws SvcLogicException,
76 IllegalStateException, IllegalArgumentException {
78 params.put("HostName", "test");
79 params.put("Port", "10");
80 params.put("User", "test");
81 params.put("Password", "test");
82 params.put("Test", "fail");
83 adapter.reqExecCommand(params, svcContext);
84 String status = svcContext.getAttribute("org.onap.appc.adapter.saltstack.result.code");
85 TestId = svcContext.getAttribute("org.onap.appc.adapter.saltstack.Id");
86 assertEquals("101", status);
89 @Test(expected = SvcLogicException.class)
90 public void reqExecCommand_shouldSetUserFailed() throws SvcLogicException,
91 IllegalStateException, IllegalArgumentException {
93 params.put("HostName", "test");
94 params.put("Port", "10");
95 params.put("Password", "test");
96 params.put("Test", "fail");
97 adapter.reqExecCommand(params, svcContext);
98 String status = svcContext.getAttribute("org.onap.appc.adapter.saltstack.result.code");
99 TestId = svcContext.getAttribute("org.onap.appc.adapter.saltstack.Id");
100 assertEquals("101", status);
103 @Test(expected = SvcLogicException.class)
104 public void reqExecCommand_shouldSetHostFailed() throws SvcLogicException,
105 IllegalStateException, IllegalArgumentException {
107 params.put("Port", "10");
108 params.put("User", "test");
109 params.put("Password", "test");
110 params.put("Test", "fail");
111 adapter.reqExecCommand(params, svcContext);
112 String status = svcContext.getAttribute("org.onap.appc.adapter.saltstack.result.code");
113 TestId = svcContext.getAttribute("org.onap.appc.adapter.saltstack.Id");
114 assertEquals("101", status);
117 @Test(expected = SvcLogicException.class)
118 public void reqExecCommand_shouldSetPortFailed() throws SvcLogicException,
119 IllegalStateException, IllegalArgumentException {
121 params.put("HostName", "test");
122 params.put("User", "test");
123 params.put("Password", "test");
124 params.put("Test", "fail");
125 adapter.reqExecCommand(params, svcContext);
126 String status = svcContext.getAttribute("org.onap.appc.adapter.saltstack.result.code");
127 TestId = svcContext.getAttribute("org.onap.appc.adapter.saltstack.Id");
128 assertEquals("101", status);
131 @Test(expected = SvcLogicException.class)
132 public void reqExecCommand_shouldSetPasswordFailed() throws SvcLogicException,
133 IllegalStateException, IllegalArgumentException {
135 params.put("HostName", "test");
136 params.put("Port", "10");
137 params.put("User", "test");
138 params.put("Test", "fail");
139 adapter.reqExecCommand(params, svcContext);
140 String status = svcContext.getAttribute("org.onap.appc.adapter.saltstack.result.code");
141 TestId = svcContext.getAttribute("org.onap.appc.adapter.saltstack.Id");
142 assertEquals("101", status);
145 @Test(expected = SvcLogicException.class)
146 public void reqExecCommand_shouldSetMandatoryFailed() throws SvcLogicException,
147 IllegalStateException, IllegalArgumentException {
149 params.put("Test", "fail");
150 adapter.reqExecCommand(params, svcContext);
151 String status = svcContext.getAttribute("org.onap.appc.adapter.saltstack.result.code");
152 TestId = svcContext.getAttribute("org.onap.appc.adapter.saltstack.Id");
153 assertEquals("101", status);
156 @Test(expected = SvcLogicException.class)
157 public void reqExecCommand_NoResponseFile() throws SvcLogicException,
158 IllegalStateException, IllegalArgumentException {
160 params.put("HostName", "test");
161 params.put("Port", "10");
162 params.put("User", "test");
163 params.put("Password", "test");
164 params.put("Test", "success");
165 params.put("cmd", "test");
166 params.put("slsExec", "false");
168 adapter.reqExecCommand(params, svcContext);
169 String status = svcContext.getAttribute("org.onap.appc.adapter.saltstack.result.code");
170 TestId = svcContext.getAttribute("org.onap.appc.adapter.saltstack.Id");
171 assertEquals("400", status);
172 } catch (NullPointerException e) {
173 fail(e.getMessage() + " Unknown exception encountered ");
177 @Test(expected = SvcLogicException.class)
178 public void reqExecCommand_NoResponseFileWithRetry() throws SvcLogicException,
179 IllegalStateException, IllegalArgumentException {
181 params.put("HostName", "test");
182 params.put("Port", "10");
183 params.put("User", "test");
184 params.put("Password", "test");
185 params.put("Test", "success");
186 params.put("retryDelay", "10");
187 params.put("retryCount", "10");
188 params.put("cmd", "test");
189 params.put("slsExec", "false");
191 adapter.reqExecCommand(params, svcContext);
192 String status = svcContext.getAttribute("org.onap.appc.adapter.saltstack.result.code");
193 TestId = svcContext.getAttribute("org.onap.appc.adapter.saltstack.Id");
194 assertEquals("400", status);
195 } catch (NullPointerException e) {
196 fail(e.getMessage() + " Unknown exception encountered ");
200 @Test(expected = SvcLogicException.class)
201 public void reqExecCommand_NoResponseFileWithRetryZero() throws SvcLogicException,
202 IllegalStateException, IllegalArgumentException {
204 params.put("HostName", "test");
205 params.put("Port", "10");
206 params.put("User", "test");
207 params.put("Password", "test");
208 params.put("Test", "success");
209 params.put("retryDelay", "0");
210 params.put("retryCount", "0");
211 params.put("cmd", "test");
212 params.put("slsExec", "false");
214 adapter.reqExecCommand(params, svcContext);
215 String status = svcContext.getAttribute("org.onap.appc.adapter.saltstack.result.code");
216 TestId = svcContext.getAttribute("org.onap.appc.adapter.saltstack.Id");
217 assertEquals("400", status);
218 } catch (NullPointerException e) {
219 fail(e.getMessage() + " Unknown exception encountered ");
223 @Test(expected = SvcLogicException.class)
224 public void reqExecCommand_NoResponseFileWithNoRetry() throws SvcLogicException,
225 IllegalStateException, IllegalArgumentException {
227 params.put("HostName", "test");
228 params.put("Port", "10");
229 params.put("User", "test");
230 params.put("Password", "test");
231 params.put("Test", "success");
232 params.put("retryDelay", "-1");
233 params.put("retryCount", "-1");
234 params.put("cmd", "test");
235 params.put("slsExec", "false");
238 adapter.reqExecCommand(params, svcContext);
239 String status = svcContext.getAttribute("org.onap.appc.adapter.saltstack.result.code");
240 TestId = svcContext.getAttribute("org.onap.appc.adapter.saltstack.Id");
241 assertEquals("400", status);
242 } catch (NullPointerException e) {
243 fail(e.getMessage() + " Unknown exception encountered ");
247 @Test(expected = SvcLogicException.class)
248 public void reqExecCommand_shouldSetFailure() throws SvcLogicException,
249 IllegalStateException, IllegalArgumentException {
251 params.put("HostName", "test");
252 params.put("Port", "10");
253 params.put("User", "test");
254 params.put("Password", "test");
255 params.put("cmd", "test");
256 params.put("slsExec", "test");
257 params.put("Test", "fail");
259 adapter.reqExecCommand(params, svcContext);
260 String status = svcContext.getAttribute("org.onap.appc.adapter.saltstack.result.code");
261 TestId = svcContext.getAttribute("org.onap.appc.adapter.saltstack.Id");
262 assertEquals("400", status);
263 } catch (NullPointerException e) {
264 fail(e.getMessage() + " Unknown exception encountered ");
269 public void reqExecCommand_shouldSetSuccessNoSLS() throws SvcLogicException,
270 IllegalStateException, IllegalArgumentException {
272 params.put("HostName", "test");
273 params.put("Port", "10");
274 params.put("User", "test");
275 params.put("Password", "test");
276 params.put("Test", "success");
277 params.put("fileName", "src/test/resources/test.json");
278 params.put("Id", "test1");
279 params.put("cmd", "test");
280 params.put("slsExec", "false");
282 adapter.reqExecCommand(params, svcContext);
283 String status = svcContext.getAttribute("org.onap.appc.adapter.saltstack.result.code");
284 TestId = svcContext.getAttribute("org.onap.appc.adapter.saltstack.Id");
285 assertEquals("250", status);
286 assertEquals(TestId, "test1");
290 public void reqExecCommand_shouldSetSuccessExecSLS() throws SvcLogicException,
291 IllegalStateException, IllegalArgumentException {
293 params.put("HostName", "test");
294 params.put("Port", "10");
295 params.put("User", "test");
296 params.put("Password", "test");
297 params.put("Test", "success");
298 params.put("fileName", "src/test/resources/test-sls.json");
299 params.put("Id", "test1");
300 params.put("cmd", "test");
301 params.put("slsExec", "true");
303 adapter.reqExecCommand(params, svcContext);
304 String status = svcContext.getAttribute("org.onap.appc.adapter.saltstack.result.code");
305 TestId = svcContext.getAttribute("org.onap.appc.adapter.saltstack.Id");
306 assertEquals("200", status);
307 assertEquals(TestId, "test1");
310 @Test(expected = SvcLogicException.class)
311 public void reqExecCommand_shouldSetFailExecSLS() throws SvcLogicException,
312 IllegalStateException, IllegalArgumentException {
314 params.put("HostName", "test");
315 params.put("Port", "10");
316 params.put("User", "test");
317 params.put("Password", "test");
318 params.put("Test", "success");
319 params.put("fileName", "src/test/resources/test.json");
320 params.put("Id", "test1");
321 params.put("cmd", "test");
322 params.put("slsExec", "true");
324 adapter.reqExecCommand(params, svcContext);
325 String status = svcContext.getAttribute("org.onap.appc.adapter.saltstack.result.code");
326 TestId = svcContext.getAttribute("org.onap.appc.adapter.saltstack.Id");
327 assertEquals(TestId, "test1");
331 public void reqExecCommand_shouldSetSuccessFileTxt() throws SvcLogicException,
332 IllegalStateException, IllegalArgumentException {
334 params.put("HostName", "test");
335 params.put("Port", "10");
336 params.put("User", "test");
337 params.put("Password", "test");
338 params.put("Test", "success");
339 params.put("fileName", "src/test/resources/test.txt");
340 params.put("Id", "txt");
341 params.put("cmd", "test");
342 params.put("slsExec", "false");
344 adapter.reqExecCommand(params, svcContext);
345 String status = svcContext.getAttribute("org.onap.appc.adapter.saltstack.result.code");
346 TestId = svcContext.getAttribute("org.onap.appc.adapter.saltstack.Id");
347 assertEquals("250", status);
348 assertEquals(TestId, "txt");
352 public void reqExecCommand_shouldSetSuccessFileNoExtension() throws SvcLogicException,
353 IllegalStateException, IllegalArgumentException {
355 params.put("HostName", "test");
356 params.put("Port", "10");
357 params.put("User", "test");
358 params.put("Password", "test");
359 params.put("Test", "success");
360 params.put("fileName", "src/test/resources/test");
361 params.put("Id", "txt");
362 params.put("cmd", "test");
363 params.put("slsExec", "false");
365 adapter.reqExecCommand(params, svcContext);
366 String status = svcContext.getAttribute("org.onap.appc.adapter.saltstack.result.code");
367 TestId = svcContext.getAttribute("org.onap.appc.adapter.saltstack.Id");
368 assertEquals("250", status);
369 assertEquals(TestId, "txt");
373 public void reqExecCommand_shouldSetSuccessFileInvalidJson() throws SvcLogicException,
374 IllegalStateException, IllegalArgumentException {
376 params.put("HostName", "test");
377 params.put("Port", "10");
378 params.put("User", "test");
379 params.put("Password", "test");
380 params.put("Test", "success");
381 params.put("fileName", "src/test/resources/test-invalid.json");
382 params.put("Id", "test1");
383 params.put("cmd", "test");
384 params.put("slsExec", "false");
386 adapter.reqExecCommand(params, svcContext);
387 String status = svcContext.getAttribute("org.onap.appc.adapter.saltstack.result.code");
388 TestId = svcContext.getAttribute("org.onap.appc.adapter.saltstack.Id");
389 assertEquals("250", status);
392 @Test(expected = SvcLogicException.class)
393 public void reqExecCommand_shouldSetFailFileInvalidFile() throws SvcLogicException,
394 IllegalStateException, IllegalArgumentException {
396 params.put("HostName", "test");
397 params.put("Port", "10");
398 params.put("User", "test");
399 params.put("Password", "test");
400 params.put("Test", "success");
401 params.put("fileName", "src/test/resources/test-none.json");
402 params.put("Id", "test1");
404 adapter.reqExecCommand(params, svcContext);
408 public void reqExecCommand_shouldSetSuccessFileJsonNoReqID() throws SvcLogicException,
409 IllegalStateException, IllegalArgumentException {
411 params.put("HostName", "test");
412 params.put("Port", "10");
413 params.put("User", "test");
414 params.put("Password", "test");
415 params.put("Test", "success");
416 params.put("fileName", "src/test/resources/test.json");
417 params.put("cmd", "test");
418 params.put("slsExec", "false");
420 adapter.reqExecCommand(params, svcContext);
421 String status = svcContext.getAttribute("org.onap.appc.adapter.saltstack.result.code");
422 TestId = svcContext.getAttribute("org.onap.appc.adapter.saltstack.Id");
423 assertEquals("250", status);
427 public void reqExecSLSFile_shouldSetSuccessJson() throws SvcLogicException,
428 IllegalStateException, IllegalArgumentException {
430 params.put("HostName", "test");
431 params.put("Port", "10");
432 params.put("User", "test");
433 params.put("Password", "test");
434 params.put("Test", "success");
435 params.put("slsFile", "src/test/resources/test.json");
436 params.put("fileName", "src/test/resources/test-sls.json");
437 params.put("Id", "test1");
438 params.put("cmd", "test");
440 adapter.reqExecSLSFile(params, svcContext);
441 String status = svcContext.getAttribute("org.onap.appc.adapter.saltstack.result.code");
442 TestId = svcContext.getAttribute("org.onap.appc.adapter.saltstack.Id");
443 assertEquals("200", status);
444 assertEquals(TestId, "test1");
447 @Test(expected = SvcLogicException.class)
448 public void reqExecSLSFile_NoSLSfile() throws SvcLogicException,
449 IllegalStateException, IllegalArgumentException {
451 params.put("HostName", "test");
452 params.put("Port", "10");
453 params.put("User", "test");
454 params.put("Password", "test");
455 params.put("Test", "success");
456 params.put("slsFile", "src/test/resources/test-none.json");
457 params.put("fileName", "src/test/resources/test-sls.json");
458 params.put("Id", "test1");
460 adapter.reqExecSLSFile(params, svcContext);
461 String status = svcContext.getAttribute("org.onap.appc.adapter.saltstack.result.code");
462 TestId = svcContext.getAttribute("org.onap.appc.adapter.saltstack.Id");
463 assertEquals(TestId, "test1");
466 @Test(expected = SvcLogicException.class)
467 public void reqExecSLSFile_NoResponsefile() throws SvcLogicException,
468 IllegalStateException, IllegalArgumentException {
470 params.put("HostName", "test");
471 params.put("Port", "10");
472 params.put("User", "test");
473 params.put("Password", "test");
474 params.put("Test", "success");
475 params.put("slsFile", "src/test/resources/test.json");
476 params.put("fileName", "src/test/resources/test-none.json");
477 params.put("Id", "test1");
479 adapter.reqExecSLSFile(params, svcContext);
480 String status = svcContext.getAttribute("org.onap.appc.adapter.saltstack.result.code");
481 TestId = svcContext.getAttribute("org.onap.appc.adapter.saltstack.Id");
482 assertEquals(TestId, "test1");
486 public void reqExecSLSFile_WithMinionSetSuccessJson() throws SvcLogicException,
487 IllegalStateException, IllegalArgumentException {
489 params.put("HostName", "test");
490 params.put("Port", "10");
491 params.put("User", "test");
492 params.put("Password", "test");
493 params.put("Test", "success");
494 params.put("slsFile", "src/test/resources/test.json");
495 params.put("fileName", "src/test/resources/test-sls.json");
496 params.put("Id", "test1");
497 params.put("cmd", "test");
498 params.put("applyTo", "minion1");
500 adapter.reqExecSLSFile(params, svcContext);
501 String status = svcContext.getAttribute("org.onap.appc.adapter.saltstack.result.code");
502 TestId = svcContext.getAttribute("org.onap.appc.adapter.saltstack.Id");
503 assertEquals("200", status);
504 assertEquals(TestId, "test1");
507 @Test(expected = SvcLogicException.class)
508 public void reqExecSLSFile_WithMinionNoSLSfile() throws SvcLogicException,
509 IllegalStateException, IllegalArgumentException {
511 params.put("HostName", "test");
512 params.put("Port", "10");
513 params.put("User", "test");
514 params.put("Password", "test");
515 params.put("Test", "success");
516 params.put("slsFile", "src/test/resources/test-none.json");
517 params.put("fileName", "src/test/resources/test-sls.json");
518 params.put("Id", "test1");
519 params.put("applyTo", "minion1");
521 adapter.reqExecSLSFile(params, svcContext);
522 String status = svcContext.getAttribute("org.onap.appc.adapter.saltstack.result.code");
523 TestId = svcContext.getAttribute("org.onap.appc.adapter.saltstack.Id");
524 assertEquals(TestId, "test1");
527 @Test(expected = SvcLogicException.class)
528 public void reqExecSLSFile_WithMinionNoResponsefile() throws SvcLogicException,
529 IllegalStateException, IllegalArgumentException {
531 params.put("HostName", "test");
532 params.put("Port", "10");
533 params.put("User", "test");
534 params.put("Password", "test");
535 params.put("Test", "success");
536 params.put("slsFile", "src/test/resources/test.json");
537 params.put("fileName", "src/test/resources/test-none.json");
538 params.put("Id", "test1");
539 params.put("applyTo", "minion1");
541 adapter.reqExecSLSFile(params, svcContext);
542 String status = svcContext.getAttribute("org.onap.appc.adapter.saltstack.result.code");
543 TestId = svcContext.getAttribute("org.onap.appc.adapter.saltstack.Id");
544 assertEquals(TestId, "test1");
548 public void reqExecSLSFile_WithAllMinionSetSuccessJson() throws SvcLogicException,
549 IllegalStateException, IllegalArgumentException {
551 params.put("HostName", "test");
552 params.put("Port", "10");
553 params.put("User", "test");
554 params.put("Password", "test");
555 params.put("Test", "success");
556 params.put("slsFile", "src/test/resources/test.json");
557 params.put("fileName", "src/test/resources/test-sls.json");
558 params.put("Id", "test1");
559 params.put("cmd", "test");
560 params.put("applyTo", "*");
562 adapter.reqExecSLSFile(params, svcContext);
563 String status = svcContext.getAttribute("org.onap.appc.adapter.saltstack.result.code");
564 TestId = svcContext.getAttribute("org.onap.appc.adapter.saltstack.Id");
565 assertEquals("200", status);
566 assertEquals(TestId, "test1");
569 @Test(expected = SvcLogicException.class)
570 public void reqExecSLSFile_WithAllMinionNoSLSfile() throws SvcLogicException,
571 IllegalStateException, IllegalArgumentException {
573 params.put("HostName", "test");
574 params.put("Port", "10");
575 params.put("User", "test");
576 params.put("Password", "test");
577 params.put("Test", "success");
578 params.put("slsFile", "src/test/resources/test-none.json");
579 params.put("fileName", "src/test/resources/test-sls.json");
580 params.put("Id", "test1");
581 params.put("applyTo", "*");
583 adapter.reqExecSLSFile(params, svcContext);
584 String status = svcContext.getAttribute("org.onap.appc.adapter.saltstack.result.code");
585 TestId = svcContext.getAttribute("org.onap.appc.adapter.saltstack.Id");
586 assertEquals(TestId, "test1");
589 @Test(expected = SvcLogicException.class)
590 public void reqExecSLSFile_WithAllMinionNoResponsefile() throws SvcLogicException,
591 IllegalStateException, IllegalArgumentException {
593 params.put("HostName", "test");
594 params.put("Port", "10");
595 params.put("User", "test");
596 params.put("Password", "test");
597 params.put("Test", "success");
598 params.put("slsFile", "src/test/resources/test.json");
599 params.put("fileName", "src/test/resources/test-none.json");
600 params.put("Id", "test1");
601 params.put("applyTo", "*");
603 adapter.reqExecSLSFile(params, svcContext);
604 String status = svcContext.getAttribute("org.onap.appc.adapter.saltstack.result.code");
605 TestId = svcContext.getAttribute("org.onap.appc.adapter.saltstack.Id");
606 assertEquals(TestId, "test1");
610 public void reqExecLog_shouldSetMessage() throws IllegalStateException, IllegalArgumentException {
612 params.put("Id", "101");
615 adapter.reqExecLog(params, svcContext);
616 String status = svcContext.getAttribute("org.onap.appc.adapter.saltstack.log");
617 //assertEquals(message, status);
618 assertEquals(null, status);
619 } catch (SvcLogicException e) {
620 String status = svcContext.getAttribute("org.onap.appc.adapter.saltstack.log");
621 fail(e.getMessage() + " Code = " + status);
622 } catch (Exception e) {
623 fail(e.getMessage() + " Unknown exception encountered ");