d96a709ce969e348f5904d10614ce5e4e42134aa
[ccsdk/sli/adaptors.git] / ansible-adapter / ansible-adapter-bundle / src / test / java / org / onap / appc / adapter / ansible / impl / TestAnsibleAdapterImpl.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
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
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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  * ============LICENSE_END=========================================================
23  */
24
25 package org.onap.appc.adapter.ansible.impl;
26
27 import static org.junit.Assert.assertEquals;
28 import static org.junit.Assert.fail;
29
30 import java.util.HashMap;
31 import java.util.Map;
32
33 import org.junit.After;
34 import org.junit.Before;
35 import org.junit.Test;
36 import org.onap.ccsdk.sli.adaptors.ansible.impl.AnsibleAdapterImpl;
37 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
38 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
39
40
41 public class TestAnsibleAdapterImpl {
42
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\"}";
46
47     private AnsibleAdapterImpl adapter;
48     private String TestId;
49     private boolean testMode = true;
50     private Map<String, String> params;
51     private SvcLogicContext svcContext;
52
53
54     @Before
55     public void setup() throws IllegalArgumentException {
56         testMode = true;
57         svcContext = new SvcLogicContext();
58         adapter = new AnsibleAdapterImpl(testMode);
59
60         params = new HashMap<>();
61         params.put("AgentUrl", "https://192.168.1.1");
62         params.put("User", "test");
63         params.put("Password", "test");
64     }
65
66     @After
67     public void tearDown() {
68         testMode = false;
69         adapter = null;
70         params = null;
71         svcContext = null;
72     }
73
74     @Test
75     public void reqExec_shouldSetPending() throws IllegalStateException, IllegalArgumentException {
76
77         params.put("PlaybookName", "test_playbook.yaml");
78
79         try {
80             adapter.reqExec(params, svcContext);
81             String status = svcContext.getAttribute("org.onap.appc.adapter.ansible.result.code");
82             TestId = svcContext.getAttribute("org.onap.appc.adapter.ansible.result.Id");
83             System.out.println("Comparing " + PENDING + " and " + status);
84             assertEquals(PENDING, status);
85         } catch (SvcLogicException e) {
86             String status = svcContext.getAttribute("org.onap.appc.adapter.ansible.result.code");
87             fail(e.getMessage() + " Code = " + status);
88         } catch (Exception e) {
89             fail(e.getMessage() + " Unknown exception encountered ");
90         }
91     }
92
93     @Test
94     public void reqExecResult_shouldSetSuccess() throws IllegalStateException, IllegalArgumentException {
95
96         params.put("Id", "100");
97
98         for (String ukey : params.keySet()) {
99             System.out.println(String.format("Ansible Parameter %s = %s", ukey, params.get(ukey)));
100         }
101
102         try {
103             adapter.reqExecResult(params, svcContext);
104             String status = svcContext.getAttribute("org.onap.appc.adapter.ansible.result.code");
105             assertEquals(SUCCESS, status);
106         } catch (SvcLogicException e) {
107             String status = svcContext.getAttribute("org.onap.appc.adapter.ansible.result.code");
108             fail(e.getMessage() + " Code = " + status);
109         } catch (Exception e) {
110             fail(e.getMessage() + " Unknown exception encountered ");
111         }
112     }
113
114     @Test
115     public void reqExecLog_shouldSetMessage() throws IllegalStateException, IllegalArgumentException {
116
117         params.put("Id", "101");
118
119         try {
120             adapter.reqExecLog(params, svcContext);
121             String status = svcContext.getAttribute("org.onap.appc.adapter.ansible.log");
122             assertEquals(message, status);
123         } catch (SvcLogicException e) {
124             String status = svcContext.getAttribute("org.onap.appc.adapter.ansible.log");
125             fail(e.getMessage() + " Code = " + status);
126         } catch (Exception e) {
127             fail(e.getMessage() + " Unknown exception encountered ");
128         }
129     }
130 }