Saltstack now aligned with APPC
[ccsdk/sli/adaptors.git] / saltstack-adapter / saltstack-adapter-provider / src / test / java / org / onap / ccsdk / adapter / impl / TestSaltstackAdapterImpl.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : CCSDK
4  * ================================================================================
5  * Copyright (C) 2018 Samsung Electronics. All rights reserved.
6  * ================================================================================
7  *
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
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
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.
20  *
21  *
22  * ============LICENSE_END=========================================================
23  */
24
25 package org.onap.ccsdk.adapter.impl;
26
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;
33
34 import java.util.HashMap;
35 import java.util.Map;
36
37 import static org.junit.Assert.assertEquals;
38 import static org.junit.Assert.fail;
39
40
41 public class TestSaltstackAdapterImpl {
42
43     private SaltstackAdapterImpl adapter;
44     private String TestId;
45     private boolean testMode = true;
46     private Map<String, String> params;
47     private SvcLogicContext svcContext;
48
49
50     @Before
51     public void setup() throws IllegalArgumentException {
52         testMode = true;
53         svcContext = new SvcLogicContext();
54         adapter = new SaltstackAdapterImpl(testMode);
55
56         params = new HashMap<>();
57         params.put("AgentUrl", "https://192.168.1.1");
58         params.put("User", "test");
59         params.put("Password", "test");
60     }
61
62     @After
63     public void tearDown() {
64         testMode = false;
65         adapter = null;
66         params = null;
67         svcContext = null;
68     }
69
70     @Test(expected = SvcLogicException.class)
71     public void reqExecCommand_shouldSetFailed() throws SvcLogicException,
72             IllegalStateException, IllegalArgumentException {
73
74         params.put("HostName", "test");
75         params.put("Port", "10");
76         params.put("User", "test");
77         params.put("Password", "test");
78         params.put("Test", "fail");
79         adapter.reqExecCommand(params, svcContext);
80         String status = svcContext.getAttribute("org.onap.appc.adapter.saltstack.result.code");
81         TestId = svcContext.getAttribute("org.onap.appc.adapter.saltstack.Id");
82         assertEquals("101", status);
83     }
84
85     @Test(expected = SvcLogicException.class)
86     public void reqExecCommand_shouldSetUserFailed() throws SvcLogicException,
87             IllegalStateException, IllegalArgumentException {
88
89         params.put("HostName", "test");
90         params.put("Port", "10");
91         params.put("Password", "test");
92         params.put("Test", "fail");
93         adapter.reqExecCommand(params, svcContext);
94         String status = svcContext.getAttribute("org.onap.appc.adapter.saltstack.result.code");
95         TestId = svcContext.getAttribute("org.onap.appc.adapter.saltstack.Id");
96         assertEquals("101", status);
97     }
98
99     @Test(expected = SvcLogicException.class)
100     public void reqExecCommand_shouldSetHostFailed() throws SvcLogicException,
101             IllegalStateException, IllegalArgumentException {
102
103         params.put("Port", "10");
104         params.put("User", "test");
105         params.put("Password", "test");
106         params.put("Test", "fail");
107         adapter.reqExecCommand(params, svcContext);
108         String status = svcContext.getAttribute("org.onap.appc.adapter.saltstack.result.code");
109         TestId = svcContext.getAttribute("org.onap.appc.adapter.saltstack.Id");
110         assertEquals("101", status);
111     }
112
113     @Test(expected = SvcLogicException.class)
114     public void reqExecCommand_shouldSetPortFailed() throws SvcLogicException,
115             IllegalStateException, IllegalArgumentException {
116
117         params.put("HostName", "test");
118         params.put("User", "test");
119         params.put("Password", "test");
120         params.put("Test", "fail");
121         adapter.reqExecCommand(params, svcContext);
122         String status = svcContext.getAttribute("org.onap.appc.adapter.saltstack.result.code");
123         TestId = svcContext.getAttribute("org.onap.appc.adapter.saltstack.Id");
124         assertEquals("101", status);
125     }
126
127     @Test(expected = SvcLogicException.class)
128     public void reqExecCommand_shouldSetPasswordFailed() throws SvcLogicException,
129             IllegalStateException, IllegalArgumentException {
130
131         params.put("HostName", "test");
132         params.put("Port", "10");
133         params.put("User", "test");
134         params.put("Test", "fail");
135         adapter.reqExecCommand(params, svcContext);
136         String status = svcContext.getAttribute("org.onap.appc.adapter.saltstack.result.code");
137         TestId = svcContext.getAttribute("org.onap.appc.adapter.saltstack.Id");
138         assertEquals("101", status);
139     }
140
141     @Test(expected = SvcLogicException.class)
142     public void reqExecCommand_shouldSetMandatoryFailed() throws SvcLogicException,
143             IllegalStateException, IllegalArgumentException {
144
145         params.put("Test", "fail");
146         adapter.reqExecCommand(params, svcContext);
147         String status = svcContext.getAttribute("org.onap.appc.adapter.saltstack.result.code");
148         TestId = svcContext.getAttribute("org.onap.appc.adapter.saltstack.Id");
149         assertEquals("101", status);
150     }
151
152     @Test(expected = SvcLogicException.class)
153     public void reqExecCommand_NoResponseFile() throws SvcLogicException,
154             IllegalStateException, IllegalArgumentException {
155
156         params.put("HostName", "test");
157         params.put("Port", "10");
158         params.put("User", "test");
159         params.put("Password", "test");
160         params.put("Test", "success");
161         params.put("Cmd", "test");
162         params.put("SlsExec", "false");
163         try {
164             adapter.reqExecCommand(params, svcContext);
165             String status = svcContext.getAttribute("org.onap.appc.adapter.saltstack.result.code");
166             TestId = svcContext.getAttribute("org.onap.appc.adapter.saltstack.Id");
167             assertEquals("400", status);
168         } catch (NullPointerException e) {
169             fail(e.getMessage() + " Unknown exception encountered ");
170         }
171     }
172
173     @Test(expected = SvcLogicException.class)
174     public void reqExecCommand_NoResponseFileWithRetry() throws SvcLogicException,
175             IllegalStateException, IllegalArgumentException {
176
177         params.put("HostName", "test");
178         params.put("Port", "10");
179         params.put("User", "test");
180         params.put("Password", "test");
181         params.put("Test", "success");
182         params.put("withRetry", "true");
183         params.put("Cmd", "test");
184         params.put("SlsExec", "false");
185         try {
186             adapter.reqExecCommand(params, svcContext);
187             String status = svcContext.getAttribute("org.onap.appc.adapter.saltstack.result.code");
188             TestId = svcContext.getAttribute("org.onap.appc.adapter.saltstack.Id");
189             assertEquals("400", status);
190         } catch (NullPointerException e) {
191             fail(e.getMessage() + " Unknown exception encountered ");
192         }
193     }
194
195     @Test(expected = SvcLogicException.class)
196     public void reqExecCommand_NoResponseFileWithRetryZero() throws SvcLogicException,
197             IllegalStateException, IllegalArgumentException {
198
199         params.put("HostName", "test");
200         params.put("Port", "10");
201         params.put("User", "test");
202         params.put("Password", "test");
203         params.put("Test", "success");
204         params.put("withRetry", "0");
205         params.put("Cmd", "test");
206         params.put("SlsExec", "false");
207         try {
208             adapter.reqExecCommand(params, svcContext);
209             String status = svcContext.getAttribute("org.onap.appc.adapter.saltstack.result.code");
210             TestId = svcContext.getAttribute("org.onap.appc.adapter.saltstack.Id");
211             assertEquals("400", status);
212         } catch (NullPointerException e) {
213             fail(e.getMessage() + " Unknown exception encountered ");
214         }
215     }
216
217     @Test(expected = SvcLogicException.class)
218     public void reqExecCommand_NoResponseFileWithNoRetry() throws SvcLogicException,
219             IllegalStateException, IllegalArgumentException {
220
221         params.put("HostName", "test");
222         params.put("Port", "10");
223         params.put("User", "test");
224         params.put("Password", "test");
225         params.put("Test", "success");
226         params.put("withRetry", "false");
227         params.put("Cmd", "test");
228         params.put("SlsExec", "false");
229
230         try {
231             adapter.reqExecCommand(params, svcContext);
232             String status = svcContext.getAttribute("org.onap.appc.adapter.saltstack.result.code");
233             TestId = svcContext.getAttribute("org.onap.appc.adapter.saltstack.Id");
234             assertEquals("400", status);
235         } catch (NullPointerException e) {
236             fail(e.getMessage() + " Unknown exception encountered ");
237         }
238     }
239
240     @Test(expected = SvcLogicException.class)
241     public void reqExecCommand_shouldSetFailure() throws SvcLogicException,
242             IllegalStateException, IllegalArgumentException {
243
244         params.put("HostName", "test");
245         params.put("Port", "10");
246         params.put("User", "test");
247         params.put("Password", "test");
248         params.put("Cmd", "test");
249         params.put("SlsExec", "test");
250         params.put("Test", "fail");
251         try {
252             adapter.reqExecCommand(params, svcContext);
253             String status = svcContext.getAttribute("org.onap.appc.adapter.saltstack.result.code");
254             TestId = svcContext.getAttribute("org.onap.appc.adapter.saltstack.Id");
255             assertEquals("400", status);
256         } catch (NullPointerException e) {
257             fail(e.getMessage() + " Unknown exception encountered ");
258         }
259     }
260
261     @Test
262     public void reqExecCommand_shouldSetSuccessNoSLS() throws SvcLogicException,
263             IllegalStateException, IllegalArgumentException {
264
265         params.put("HostName", "test");
266         params.put("Port", "10");
267         params.put("User", "test");
268         params.put("Password", "test");
269         params.put("Test", "success");
270         params.put("fileName", "src/test/resources/test.json");
271         params.put("Id", "test1");
272         params.put("Cmd", "test");
273         params.put("SlsExec", "false");
274
275         adapter.reqExecCommand(params, svcContext);
276         String status = svcContext.getAttribute("org.onap.appc.adapter.saltstack.result.code");
277         TestId = svcContext.getAttribute("org.onap.appc.adapter.saltstack.Id");
278         assertEquals("200", status);
279         assertEquals(TestId, "test1");
280     }
281
282     @Test
283     public void reqExecCommand_shouldSetSuccessExecSLS() throws SvcLogicException,
284             IllegalStateException, IllegalArgumentException {
285
286         params.put("HostName", "test");
287         params.put("Port", "10");
288         params.put("User", "test");
289         params.put("Password", "test");
290         params.put("Test", "success");
291         params.put("fileName", "src/test/resources/test-sls.json");
292         params.put("Id", "test1");
293         params.put("Cmd", "test");
294         params.put("SlsExec", "true");
295
296         adapter.reqExecCommand(params, svcContext);
297         String status = svcContext.getAttribute("org.onap.appc.adapter.saltstack.result.code");
298         TestId = svcContext.getAttribute("org.onap.appc.adapter.saltstack.Id");
299         assertEquals("200", status);
300         assertEquals(TestId, "test1");
301     }
302
303     @Test(expected = SvcLogicException.class)
304     public void reqExecCommand_shouldSetFailExecSLS() throws SvcLogicException,
305             IllegalStateException, IllegalArgumentException {
306
307         params.put("HostName", "test");
308         params.put("Port", "10");
309         params.put("User", "test");
310         params.put("Password", "test");
311         params.put("Test", "success");
312         params.put("fileName", "src/test/resources/test.json");
313         params.put("Id", "test1");
314         params.put("Cmd", "test");
315         params.put("SlsExec", "true");
316
317         adapter.reqExecCommand(params, svcContext);
318         TestId = svcContext.getAttribute("org.onap.appc.adapter.saltstack.Id");
319         assertEquals(TestId, "test1");
320     }
321
322     @Test
323     public void reqExecCommand_shouldSetSuccessFileTxt() throws SvcLogicException,
324             IllegalStateException, IllegalArgumentException {
325
326         params.put("HostName", "test");
327         params.put("Port", "10");
328         params.put("User", "test");
329         params.put("Password", "test");
330         params.put("Test", "success");
331         params.put("fileName", "src/test/resources/test.txt");
332         params.put("Id", "txt");
333         params.put("Cmd", "test");
334         params.put("SlsExec", "false");
335
336         adapter.reqExecCommand(params, svcContext);
337         String status = svcContext.getAttribute("org.onap.appc.adapter.saltstack.result.code");
338         TestId = svcContext.getAttribute("org.onap.appc.adapter.saltstack.Id");
339         assertEquals("200", status);
340         assertEquals(TestId, "txt");
341     }
342
343     @Test
344     public void reqExecCommand_shouldSetSuccessFileNoExtension() throws SvcLogicException,
345             IllegalStateException, IllegalArgumentException {
346
347         params.put("HostName", "test");
348         params.put("Port", "10");
349         params.put("User", "test");
350         params.put("Password", "test");
351         params.put("Test", "success");
352         params.put("fileName", "src/test/resources/test");
353         params.put("Id", "txt");
354         params.put("Cmd", "test");
355         params.put("SlsExec", "false");
356
357         adapter.reqExecCommand(params, svcContext);
358         String status = svcContext.getAttribute("org.onap.appc.adapter.saltstack.result.code");
359         TestId = svcContext.getAttribute("org.onap.appc.adapter.saltstack.Id");
360         assertEquals("200", status);
361         assertEquals(TestId, "txt");
362     }
363
364     @Test
365     public void reqExecCommand_shouldSetSuccessFileInvalidJson() throws SvcLogicException,
366             IllegalStateException, IllegalArgumentException {
367
368         params.put("HostName", "test");
369         params.put("Port", "10");
370         params.put("User", "test");
371         params.put("Password", "test");
372         params.put("Test", "success");
373         params.put("fileName", "src/test/resources/test-invalid.json");
374         params.put("Id", "test1");
375         params.put("Cmd", "test");
376         params.put("SlsExec", "false");
377
378         adapter.reqExecCommand(params, svcContext);
379         String status = svcContext.getAttribute("org.onap.appc.adapter.saltstack.result.code");
380         TestId = svcContext.getAttribute("org.onap.appc.adapter.saltstack.Id");
381         assertEquals("200", status);
382     }
383
384     @Test(expected = SvcLogicException.class)
385     public void reqExecCommand_shouldSetFailFileInvalidFile() throws SvcLogicException,
386             IllegalStateException, IllegalArgumentException {
387
388         params.put("HostName", "test");
389         params.put("Port", "10");
390         params.put("User", "test");
391         params.put("Password", "test");
392         params.put("Test", "success");
393         params.put("fileName", "src/test/resources/test-none.json");
394         params.put("Id", "test1");
395
396         adapter.reqExecCommand(params, svcContext);
397     }
398
399     @Test
400     public void reqExecCommand_shouldSetSuccessFileJsonNoReqID() throws SvcLogicException,
401             IllegalStateException, IllegalArgumentException {
402
403         params.put("HostName", "test");
404         params.put("Port", "10");
405         params.put("User", "test");
406         params.put("Password", "test");
407         params.put("Test", "success");
408         params.put("fileName", "src/test/resources/test.json");
409         params.put("Cmd", "test");
410         params.put("SlsExec", "false");
411
412         adapter.reqExecCommand(params, svcContext);
413         String status = svcContext.getAttribute("org.onap.appc.adapter.saltstack.result.code");
414         TestId = svcContext.getAttribute("org.onap.appc.adapter.saltstack.Id");
415         assertEquals("200", status);
416     }
417
418     @Test
419     public void reqExecSLSFile_shouldSetSuccessJson() throws SvcLogicException,
420             IllegalStateException, IllegalArgumentException {
421
422         params.put("HostName", "test");
423         params.put("Port", "10");
424         params.put("User", "test");
425         params.put("Password", "test");
426         params.put("Test", "success");
427         params.put("SlsFile", "src/test/resources/test.sls");
428         params.put("fileName", "src/test/resources/test-sls.json");
429         params.put("Id", "test1");
430         params.put("Cmd", "test");
431
432         adapter.reqExecSLSFile(params, svcContext);
433         String status = svcContext.getAttribute("org.onap.appc.adapter.saltstack.result.code");
434         TestId = svcContext.getAttribute("org.onap.appc.adapter.saltstack.Id");
435         assertEquals("200", status);
436         assertEquals(TestId, "test1");
437     }
438
439     @Test(expected = SvcLogicException.class)
440     public void reqExecSLSFile_NoSLSfile() throws SvcLogicException,
441             IllegalStateException, IllegalArgumentException {
442
443         params.put("HostName", "test");
444         params.put("Port", "10");
445         params.put("User", "test");
446         params.put("Password", "test");
447         params.put("Test", "success");
448         params.put("SlsFile", "src/test/resources/test-none.sls");
449         params.put("fileName", "src/test/resources/test-sls.json");
450         params.put("Id", "test1");
451
452         adapter.reqExecSLSFile(params, svcContext);
453         TestId = svcContext.getAttribute("org.onap.appc.adapter.saltstack.Id");
454         assertEquals(TestId, "test1");
455     }
456
457     @Test(expected = SvcLogicException.class)
458     public void reqExecSLSFile_NoExtn() throws SvcLogicException,
459             IllegalStateException, IllegalArgumentException {
460
461         params.put("HostName", "test");
462         params.put("Port", "10");
463         params.put("User", "test");
464         params.put("Password", "test");
465         params.put("Test", "success");
466         params.put("SlsFile", "src/test/resources/test-none");
467         params.put("fileName", "src/test/resources/test-sls.json");
468         params.put("Id", "test1");
469
470         adapter.reqExecSLSFile(params, svcContext);
471         TestId = svcContext.getAttribute("org.onap.appc.adapter.saltstack.Id");
472         assertEquals(TestId, "test1");
473     }
474
475     @Test(expected = SvcLogicException.class)
476     public void reqExecSLSFile_NoResponsefile() throws SvcLogicException,
477             IllegalStateException, IllegalArgumentException {
478
479         params.put("HostName", "test");
480         params.put("Port", "10");
481         params.put("User", "test");
482         params.put("Password", "test");
483         params.put("Test", "success");
484         params.put("SlsFile", "src/test/resources/test.json");
485         params.put("fileName", "src/test/resources/test-none.json");
486         params.put("Id", "test1");
487
488         adapter.reqExecSLSFile(params, svcContext);
489         TestId = svcContext.getAttribute("org.onap.appc.adapter.saltstack.Id");
490         assertEquals(TestId, "test1");
491     }
492
493     @Test(expected = SvcLogicException.class)
494     public void reqExecSLSFile_WithMinionSetNotSLSType() throws SvcLogicException,
495             IllegalStateException, IllegalArgumentException {
496
497         params.put("HostName", "test");
498         params.put("Port", "10");
499         params.put("User", "test");
500         params.put("Password", "test");
501         params.put("Test", "success");
502         params.put("SlsFile", "src/test/resources/test.json");
503         params.put("fileName", "src/test/resources/test-sls.json");
504         params.put("Id", "test1");
505         params.put("Cmd", "test");
506         params.put("NodeList", "minion1");
507
508         adapter.reqExecSLSFile(params, svcContext);
509         String status = svcContext.getAttribute("org.onap.appc.adapter.saltstack.result.code");
510         TestId = svcContext.getAttribute("org.onap.appc.adapter.saltstack.Id");
511         assertEquals("200", status);
512         assertEquals(TestId, "test1");
513     }
514
515     @Test
516     public void reqExecSLSFile_WithMinionSetSuccessSls() throws SvcLogicException,
517             IllegalStateException, IllegalArgumentException {
518
519         params.put("HostName", "test");
520         params.put("Port", "10");
521         params.put("User", "test");
522         params.put("Password", "test");
523         params.put("Test", "success");
524         params.put("SlsFile", "src/test/resources/test.sls");
525         params.put("fileName", "src/test/resources/test-sls.json");
526         params.put("Id", "test1");
527         params.put("Cmd", "test");
528         params.put("NodeList", "minion1");
529
530         adapter.reqExecSLSFile(params, svcContext);
531         String status = svcContext.getAttribute("org.onap.appc.adapter.saltstack.result.code");
532         TestId = svcContext.getAttribute("org.onap.appc.adapter.saltstack.Id");
533         assertEquals("200", status);
534         assertEquals(TestId, "test1");
535     }
536
537     @Test(expected = SvcLogicException.class)
538     public void reqExecSLSFile_WithMinionNoSLSfile() throws SvcLogicException,
539             IllegalStateException, IllegalArgumentException {
540
541         params.put("HostName", "test");
542         params.put("Port", "10");
543         params.put("User", "test");
544         params.put("Password", "test");
545         params.put("Test", "success");
546         params.put("SlsFile", "src/test/resources/test-none.json");
547         params.put("fileName", "src/test/resources/test-sls.json");
548         params.put("Id", "test1");
549         params.put("NodeList", "minion1");
550
551         adapter.reqExecSLSFile(params, svcContext);
552         TestId = svcContext.getAttribute("org.onap.appc.adapter.saltstack.Id");
553         assertEquals(TestId, "test1");
554     }
555
556     @Test(expected = SvcLogicException.class)
557     public void reqExecSLSFile_WithMinionNoResponsefile() throws SvcLogicException,
558             IllegalStateException, IllegalArgumentException {
559
560         params.put("HostName", "test");
561         params.put("Port", "10");
562         params.put("User", "test");
563         params.put("Password", "test");
564         params.put("Test", "success");
565         params.put("SlsFile", "src/test/resources/test.json");
566         params.put("fileName", "src/test/resources/test-none.json");
567         params.put("Id", "test1");
568         params.put("NodeList", "minion1");
569
570         adapter.reqExecSLSFile(params, svcContext);
571         TestId = svcContext.getAttribute("org.onap.appc.adapter.saltstack.Id");
572         assertEquals(TestId, "test1");
573     }
574
575     @Test
576     public void reqExecSLSFile_WithAllMinionSetSuccessJson() throws SvcLogicException,
577             IllegalStateException, IllegalArgumentException {
578
579         params.put("HostName", "test");
580         params.put("Port", "10");
581         params.put("User", "test");
582         params.put("Password", "test");
583         params.put("Test", "success");
584         params.put("SlsFile", "src/test/resources/test.sls");
585         params.put("fileName", "src/test/resources/test-sls.json");
586         params.put("Id", "test1");
587         params.put("Cmd", "test");
588         params.put("NodeList", "*");
589
590         adapter.reqExecSLSFile(params, svcContext);
591         String status = svcContext.getAttribute("org.onap.appc.adapter.saltstack.result.code");
592         TestId = svcContext.getAttribute("org.onap.appc.adapter.saltstack.Id");
593         assertEquals("200", status);
594         assertEquals(TestId, "test1");
595     }
596
597     @Test(expected = SvcLogicException.class)
598     public void reqExecSLSFile_WithAllMinionNoSLSfile() throws SvcLogicException,
599             IllegalStateException, IllegalArgumentException {
600
601         params.put("HostName", "test");
602         params.put("Port", "10");
603         params.put("User", "test");
604         params.put("Password", "test");
605         params.put("Test", "success");
606         params.put("SlsFile", "src/test/resources/test-none.json");
607         params.put("fileName", "src/test/resources/test-sls.json");
608         params.put("Id", "test1");
609         params.put("NodeList", "*");
610
611         adapter.reqExecSLSFile(params, svcContext);
612         TestId = svcContext.getAttribute("org.onap.appc.adapter.saltstack.Id");
613         assertEquals(TestId, "test1");
614     }
615
616     @Test(expected = SvcLogicException.class)
617     public void reqExecSLSFile_WithAllMinionNoResponsefile() throws SvcLogicException,
618             IllegalStateException, IllegalArgumentException {
619
620         params.put("HostName", "test");
621         params.put("Port", "10");
622         params.put("User", "test");
623         params.put("Password", "test");
624         params.put("Test", "success");
625         params.put("SlsFile", "src/test/resources/test.json");
626         params.put("fileName", "src/test/resources/test-none.json");
627         params.put("Id", "test1");
628         params.put("NodeList", "*");
629
630         adapter.reqExecSLSFile(params, svcContext);
631         TestId = svcContext.getAttribute("org.onap.appc.adapter.saltstack.Id");
632         assertEquals(TestId, "test1");
633     }
634
635
636     @Test
637     public void reqExecSLS_shouldSetSuccessJson() throws SvcLogicException,
638             IllegalStateException, IllegalArgumentException {
639
640         params.put("HostName", "test");
641         params.put("Port", "10");
642         params.put("User", "test");
643         params.put("Password", "test");
644         params.put("Test", "success");
645         params.put("SlsName", "src/test.sls");
646         params.put("fileName", "src/test/resources/test-sls.json");
647         params.put("Id", "test1");
648         params.put("Cmd", "test");
649
650         adapter.reqExecSLS(params, svcContext);
651         String status = svcContext.getAttribute("org.onap.appc.adapter.saltstack.result.code");
652         TestId = svcContext.getAttribute("org.onap.appc.adapter.saltstack.Id");
653         assertEquals("200", status);
654         assertEquals(TestId, "test1");
655     }
656
657     @Test
658     public void reqExecSLS_shouldSetNoExtn() throws SvcLogicException,
659             IllegalStateException, IllegalArgumentException {
660
661         params.put("HostName", "test");
662         params.put("Port", "10");
663         params.put("User", "test");
664         params.put("Password", "test");
665         params.put("Test", "success");
666         params.put("SlsName", "src/test");
667         params.put("fileName", "src/test/resources/test-sls.json");
668         params.put("Id", "test1");
669         params.put("Cmd", "test");
670
671         adapter.reqExecSLS(params, svcContext);
672         String status = svcContext.getAttribute("org.onap.appc.adapter.saltstack.result.code");
673         TestId = svcContext.getAttribute("org.onap.appc.adapter.saltstack.Id");
674         assertEquals("200", status);
675         assertEquals(TestId, "test1");
676     }
677
678     @Test(expected = SvcLogicException.class)
679     public void reqExecSLS_NoResponsefile() throws SvcLogicException,
680             IllegalStateException, IllegalArgumentException {
681
682         params.put("HostName", "test");
683         params.put("Port", "10");
684         params.put("User", "test");
685         params.put("Password", "test");
686         params.put("Test", "success");
687         params.put("SlsName", "src/test/resources/test.json");
688         params.put("fileName", "src/test/resources/test-none.json");
689         params.put("Id", "test1");
690
691         adapter.reqExecSLS(params, svcContext);
692         TestId = svcContext.getAttribute("org.onap.appc.adapter.saltstack.Id");
693         assertEquals(TestId, "test1");
694     }
695
696
697     @Test
698     public void reqExecSLS_WithMinionSetSuccessSls() throws SvcLogicException,
699             IllegalStateException, IllegalArgumentException {
700
701         params.put("HostName", "test");
702         params.put("Port", "10");
703         params.put("User", "test");
704         params.put("Password", "test");
705         params.put("Test", "success");
706         params.put("SlsName", "src/test/resources/test.sls");
707         params.put("fileName", "src/test/resources/test-sls.json");
708         params.put("Id", "test1");
709         params.put("Cmd", "test");
710         params.put("NodeList", "minion1");
711
712         adapter.reqExecSLS(params, svcContext);
713         String status = svcContext.getAttribute("org.onap.appc.adapter.saltstack.result.code");
714         TestId = svcContext.getAttribute("org.onap.appc.adapter.saltstack.Id");
715         assertEquals("200", status);
716         assertEquals(TestId, "test1");
717     }
718
719
720     @Test(expected = SvcLogicException.class)
721     public void reqExecSLS_WithMinionNoResponsefile() throws SvcLogicException,
722             IllegalStateException, IllegalArgumentException {
723
724         params.put("HostName", "test");
725         params.put("Port", "10");
726         params.put("User", "test");
727         params.put("Password", "test");
728         params.put("Test", "success");
729         params.put("SlsName", "src/test/resources/test.json");
730         params.put("fileName", "src/test/resources/test-none.json");
731         params.put("Id", "test1");
732         params.put("NodeList", "minion1");
733
734         adapter.reqExecSLS(params, svcContext);
735         TestId = svcContext.getAttribute("org.onap.appc.adapter.saltstack.Id");
736         assertEquals(TestId, "test1");
737     }
738
739     @Test
740     public void reqExecSLS_WithAllMinionSetSuccessJson() throws SvcLogicException,
741             IllegalStateException, IllegalArgumentException {
742
743         params.put("HostName", "test");
744         params.put("Port", "10");
745         params.put("User", "test");
746         params.put("Password", "test");
747         params.put("Test", "success");
748         params.put("SlsName", "src/test/resources/test.sls");
749         params.put("fileName", "src/test/resources/test-sls.json");
750         params.put("Id", "test1");
751         params.put("Cmd", "test");
752         params.put("NodeList", "*");
753
754         adapter.reqExecSLS(params, svcContext);
755         String status = svcContext.getAttribute("org.onap.appc.adapter.saltstack.result.code");
756         TestId = svcContext.getAttribute("org.onap.appc.adapter.saltstack.Id");
757         assertEquals("200", status);
758         assertEquals(TestId, "test1");
759     }
760
761
762     @Test(expected = SvcLogicException.class)
763     public void reqExecSLS_WithAllMinionNoResponsefile() throws SvcLogicException,
764             IllegalStateException, IllegalArgumentException {
765
766         params.put("HostName", "test");
767         params.put("Port", "10");
768         params.put("User", "test");
769         params.put("Password", "test");
770         params.put("Test", "success");
771         params.put("SlsName", "src/test/resources/test.json");
772         params.put("fileName", "src/test/resources/test-none.json");
773         params.put("Id", "test1");
774         params.put("NodeList", "*");
775
776         adapter.reqExecSLS(params, svcContext);
777         TestId = svcContext.getAttribute("org.onap.appc.adapter.saltstack.Id");
778         assertEquals(TestId, "test1");
779     }
780
781
782     @Test
783     public void reqExecCommand_shouldSetSuccessReal() throws SvcLogicException,
784             IllegalStateException, IllegalArgumentException {
785
786         params.put("HostName", "127.0.0.1");
787         params.put("Port", "22");
788         params.put("User", "sdn");
789         params.put("Password", "foo");
790         params.put("Id", "test1");
791         params.put("Cmd", "ls -l");
792         params.put("SlsExec", "false");
793         params.put("Timeout", "120");
794         adapter = new SaltstackAdapterImpl();
795         try {
796             adapter.reqExecCommand(params, svcContext);
797             String status = svcContext.getAttribute("org.onap.appc.adapter.saltstack.result.code");
798             TestId = svcContext.getAttribute("org.onap.appc.adapter.saltstack.Id");
799             assertEquals("200", status);
800             assertEquals(TestId, "test1");
801         } catch (Exception e) {
802             //if local ssh is not enabled
803             System.out.print(e.getMessage());
804         }
805     }
806
807     @Test
808     public void reqExecCommand_shouldSetSuccessRealSLSCommand() throws SvcLogicException,
809             IllegalStateException, IllegalArgumentException {
810
811         params.put("HostName", "<IP>");
812         params.put("Port", "2222");
813         params.put("User", "root");
814         params.put("Password", "vagrant");
815         params.put("Id", "test1");
816         params.put("Cmd", "salt '*' test.ping --out=json --static");
817         params.put("SlsExec", "false");
818         params.put("Timeout", "120");
819
820         adapter = new SaltstackAdapterImpl();
821         try {
822             adapter.reqExecCommand(params, svcContext);
823             String status = svcContext.getAttribute("org.onap.appc.adapter.saltstack.result.code");
824             TestId = svcContext.getAttribute("org.onap.appc.adapter.saltstack.Id");
825             assertEquals("200", status);
826             assertEquals(TestId, "test1");
827             TestId = svcContext.getAttribute("test1.minion1");
828             assertEquals(TestId, "true");
829         } catch (Exception e) {
830             //if saltstack ssh IP is not enabled
831             System.out.print(e.getMessage());
832         }
833     }
834
835     @Test
836     public void reqExecCommand_shouldSetSuccessRealCommand() throws SvcLogicException,
837             IllegalStateException, IllegalArgumentException {
838
839         params.put("HostName", "<IP>");
840         params.put("Port", "2222");
841         params.put("User", "root");
842         params.put("Password", "vagrant");
843         params.put("Id", "test1");
844         params.put("Cmd", "cd /srv/salt/; salt '*' state.apply vim --out=json --static");
845         params.put("SlsExec", "true");
846         params.put("Timeout", "120");
847
848         adapter = new SaltstackAdapterImpl();
849         try {
850             adapter.reqExecCommand(params, svcContext);
851             String status = svcContext.getAttribute("org.onap.appc.adapter.saltstack.result.code");
852             TestId = svcContext.getAttribute("org.onap.appc.adapter.saltstack.Id");
853             assertEquals("200", status);
854             assertEquals(TestId, "test1");
855         } catch (Exception e) {
856             //if saltstack ssh IP is not enabled
857             System.out.print(e.getMessage());
858         }
859     }
860
861     @Test
862     public void reqExecCommand_shouldSetSuccessRealSSL() throws SvcLogicException,
863             IllegalStateException, IllegalArgumentException {
864
865         params.put("HostName", "<IP>");
866         params.put("Port", "2222");
867         params.put("User", "root");
868         params.put("Password", "vagrant");
869         params.put("Id", "test1");
870         params.put("SlsName", "vim");
871         params.put("Timeout", "120");
872         params.put("NodeList", "minion1");
873
874         adapter = new SaltstackAdapterImpl();
875         try {
876             adapter.reqExecSLS(params, svcContext);
877             String status = svcContext.getAttribute("org.onap.appc.adapter.saltstack.result.code");
878             TestId = svcContext.getAttribute("org.onap.appc.adapter.saltstack.Id");
879             assertEquals("200", status);
880             assertEquals(TestId, "test1");
881         } catch (Exception e) {
882             //if saltstack ssh IP is not enabled
883             System.out.print(e.getMessage());
884         }
885     }
886
887     @Test
888     public void reqExecCommand_shouldSetSuccessEnvParam() throws SvcLogicException,
889             IllegalStateException, IllegalArgumentException {
890
891         params.put("HostName", "<IP>");
892         params.put("Port", "2222");
893         params.put("User", "root");
894         params.put("Password", "vagrant");
895         params.put("Id", "test1");
896         params.put("SlsName", "vim");
897         params.put("Timeout", "120");
898         params.put("NodeList", "minion1");
899         params.put("EnvParameters", "{\"exclude\": bar*}");
900
901         adapter = new SaltstackAdapterImpl();
902         try {
903             adapter.reqExecSLS(params, svcContext);
904             String status = svcContext.getAttribute("org.onap.appc.adapter.saltstack.result.code");
905             TestId = svcContext.getAttribute("org.onap.appc.adapter.saltstack.Id");
906             assertEquals("200", status);
907             assertEquals(TestId, "test1");
908         } catch (Exception e) {
909             //if saltstack ssh IP is not enabled
910             System.out.print(e.getMessage());
911         }
912     }
913
914     @Test
915     public void reqExecCommand_shouldSetSuccessFileParam() throws SvcLogicException,
916             IllegalStateException, IllegalArgumentException {
917
918         params.put("HostName", "<IP>");
919         params.put("Port", "2222");
920         params.put("User", "root");
921         params.put("Password", "vagrant");
922         params.put("Id", "test1");
923         params.put("SlsName", "vim");
924         params.put("Timeout", "120");
925         params.put("NodeList", "minion1");
926         params.put("EnvParameters", "{\"exclude\": \"bar,baz\"}");
927         params.put("FileParameters", "{\"config.txt\":\"db_ip=10.1.1.1, sip_timer=10000\"}");
928
929         adapter = new SaltstackAdapterImpl();
930         try {
931             adapter.reqExecSLS(params, svcContext);
932             String status = svcContext.getAttribute("org.onap.appc.adapter.saltstack.result.code");
933             TestId = svcContext.getAttribute("org.onap.appc.adapter.saltstack.Id");
934             assertEquals("200", status);
935             assertEquals(TestId, "test1");
936         } catch (Exception e) {
937             //if saltstack ssh IP is not enabled
938             System.out.print(e.getMessage());
939         }
940     }
941
942     @Test
943     public void reqExecCommand_shouldSetSuccessPillarParam() throws SvcLogicException,
944             IllegalStateException, IllegalArgumentException {
945
946         params.put("HostName", "<IP>");
947         params.put("Port", "2222");
948         params.put("User", "root");
949         params.put("Password", "vagrant");
950         params.put("Id", "test1");
951         params.put("SlsName", "vim");
952         params.put("Timeout", "120");
953         params.put("NodeList", "minion1");
954         params.put("EnvParameters", "{\"exclude\": \"bar,baz\", \"pillar\":\"'{\\\"foo\\\": \\\"bar\\\"}'\"}");
955         params.put("FileParameters", "{\"config.txt\":\"db_ip=10.1.1.1, sip_timer=10000\"}");
956
957         adapter = new SaltstackAdapterImpl();
958         try {
959             adapter.reqExecSLS(params, svcContext);
960             String status = svcContext.getAttribute("org.onap.appc.adapter.saltstack.result.code");
961             TestId = svcContext.getAttribute("org.onap.appc.adapter.saltstack.Id");
962             assertEquals("200", status);
963             assertEquals(TestId, "test1");
964         } catch (Exception e) {
965             //if saltstack ssh IP is not enabled
966             System.out.print(e.getMessage());
967         }
968     }
969
970     @Test
971     public void reqExecCommand_shouldSetSuccessMultiFileParam() throws SvcLogicException,
972             IllegalStateException, IllegalArgumentException {
973
974         params.put("HostName", "<IP>");
975         params.put("Port", "2222");
976         params.put("User", "root");
977         params.put("Password", "vagrant");
978         params.put("Id", "test1");
979         params.put("SlsName", "vim");
980         params.put("Timeout", "120");
981         params.put("NodeList", "minion1");
982         params.put("EnvParameters", "{\"exclude\": bar*}");
983         params.put("FileParameters", "{\"config.txt\":\"db_ip=10.1.1.1, sip_timer=10000\" , \"config-tep.txt\":\"db_ip=10.1.1.1, sip_timer=10000\"}");
984
985         adapter = new SaltstackAdapterImpl();
986         try {
987             adapter.reqExecSLS(params, svcContext);
988             String status = svcContext.getAttribute("org.onap.appc.adapter.saltstack.result.code");
989             TestId = svcContext.getAttribute("org.onap.appc.adapter.saltstack.Id");
990             assertEquals("200", status);
991             assertEquals(TestId, "test1");
992         } catch (Exception e) {
993             //if saltstack ssh IP is not enabled
994             System.out.print(e.getMessage());
995         }
996     }
997
998     @Test
999     public void reqExecCommand_shouldSetSuccessSSLFile() throws SvcLogicException,
1000             IllegalStateException, IllegalArgumentException {
1001
1002         params.put("HostName", "<IP>");
1003         params.put("Port", "2222");
1004         params.put("User", "root");
1005         params.put("Password", "vagrant");
1006         params.put("Id", "test1");
1007         params.put("Timeout", "120");
1008         params.put("NodeList", "minion1");
1009         params.put("SlsFile", "src/test/resources/config.sls");
1010
1011         adapter = new SaltstackAdapterImpl();
1012         try {
1013             adapter.reqExecSLSFile(params, svcContext);
1014             String status = svcContext.getAttribute("org.onap.appc.adapter.saltstack.result.code");
1015             TestId = svcContext.getAttribute("org.onap.appc.adapter.saltstack.Id");
1016             assertEquals("200", status);
1017             assertEquals(TestId, "test1");
1018         } catch (Exception e) {
1019             //if saltstack ssh IP is not enabled
1020             System.out.print(e.getMessage());
1021         }
1022     }
1023
1024     @Test
1025     public void reqExecCommand_shouldSetSuccessSSLFileMultiFileParam() throws SvcLogicException,
1026             IllegalStateException, IllegalArgumentException {
1027
1028         params.put("HostName", "<IP>");
1029         params.put("Port", "2222");
1030         params.put("User", "root");
1031         params.put("Password", "vagrant");
1032         params.put("Id", "test1");
1033         params.put("Timeout", "120");
1034         params.put("NodeList", "minion1");
1035         params.put("SlsFile", "src/test/resources/config.sls");
1036         params.put("EnvParameters", "{\"exclude\": bar, \"pillar\":\"'{\\\"foo\\\": \\\"bar\\\"}'\"}");
1037         params.put("FileParameters", "{\"config.txt\":\"db_ip=10.1.1.1, sip_timer=10000\" , \"config-tep.txt\":\"db_ip=10.1.1.1, sip_timer=10000\"}");
1038
1039         adapter = new SaltstackAdapterImpl();
1040         try {
1041             adapter.reqExecSLSFile(params, svcContext);
1042             String status = svcContext.getAttribute("org.onap.appc.adapter.saltstack.result.code");
1043             TestId = svcContext.getAttribute("org.onap.appc.adapter.saltstack.Id");
1044             assertEquals("200", status);
1045             assertEquals(TestId, "test1");
1046         } catch (Exception e) {
1047             //if saltstack ssh IP is not enabled
1048             System.out.print(e.getMessage());
1049         }
1050     }
1051 }