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.result.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.result.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.result.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.result.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.result.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.result.Id");
153 assertEquals("101", status);
156 @Test(expected = SvcLogicException.class)
157 public void reqExecCommand_shouldSetSuccess() throws SvcLogicException,
158 IllegalStateException, IllegalArgumentException {
160 params.put("PlaybookName", "test_playbook.yaml");
161 params.put("HostName", "test");
162 params.put("Port", "10");
163 params.put("User", "test");
164 params.put("Password", "test");
165 params.put("Test", "success");
167 adapter.reqExecCommand(params, svcContext);
168 String status = svcContext.getAttribute("org.onap.appc.adapter.saltstack.result.code");
169 TestId = svcContext.getAttribute("org.onap.appc.adapter.saltstack.result.Id");
170 assertEquals("400", status);
171 } catch (NullPointerException e) {
172 fail(e.getMessage() + " Unknown exception encountered ");
176 @Test(expected = SvcLogicException.class)
177 public void reqExecCommand_shouldSetSuccessWithRetry() throws SvcLogicException,
178 IllegalStateException, IllegalArgumentException {
180 params.put("PlaybookName", "test_playbook.yaml");
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");
189 adapter.reqExecCommand(params, svcContext);
190 String status = svcContext.getAttribute("org.onap.appc.adapter.saltstack.result.code");
191 TestId = svcContext.getAttribute("org.onap.appc.adapter.saltstack.result.Id");
192 assertEquals("400", status);
193 } catch (NullPointerException e) {
194 fail(e.getMessage() + " Unknown exception encountered ");
198 @Test(expected = SvcLogicException.class)
199 public void reqExecCommand_shouldSetSuccessWithRetryZero() throws SvcLogicException,
200 IllegalStateException, IllegalArgumentException {
202 params.put("PlaybookName", "test_playbook.yaml");
203 params.put("HostName", "test");
204 params.put("Port", "10");
205 params.put("User", "test");
206 params.put("Password", "test");
207 params.put("Test", "success");
208 params.put("retryDelay", "0");
209 params.put("retryCount", "0");
211 adapter.reqExecCommand(params, svcContext);
212 String status = svcContext.getAttribute("org.onap.appc.adapter.saltstack.result.code");
213 TestId = svcContext.getAttribute("org.onap.appc.adapter.saltstack.result.Id");
214 assertEquals("400", status);
215 } catch (NullPointerException e) {
216 fail(e.getMessage() + " Unknown exception encountered ");
220 @Test(expected = SvcLogicException.class)
221 public void reqExecCommand_shouldSetSuccessWithNoRetry() throws SvcLogicException,
222 IllegalStateException, IllegalArgumentException {
224 params.put("PlaybookName", "test_playbook.yaml");
225 params.put("HostName", "test");
226 params.put("Port", "10");
227 params.put("User", "test");
228 params.put("Password", "test");
229 params.put("Test", "success");
230 params.put("retryDelay", "-1");
231 params.put("retryCount", "-1");
233 adapter.reqExecCommand(params, svcContext);
234 String status = svcContext.getAttribute("org.onap.appc.adapter.saltstack.result.code");
235 TestId = svcContext.getAttribute("org.onap.appc.adapter.saltstack.result.Id");
236 assertEquals("400", status);
237 } catch (NullPointerException e) {
238 fail(e.getMessage() + " Unknown exception encountered ");
242 public void reqExecSLS_shouldSetSuccess() throws IllegalStateException, IllegalArgumentException {
244 params.put("Id", "100");
246 for (String ukey : params.keySet()) {
247 System.out.println(String.format("Saltstack Parameter %s = %s", ukey, params.get(ukey)));
251 adapter.reqExecSLS(params, svcContext);
252 String status = svcContext.getAttribute("org.onap.appc.adapter.saltstack.result.code");
253 //assertEquals(SUCCESS, status);
254 assertEquals(null, status);
255 } catch (SvcLogicException e) {
256 String status = svcContext.getAttribute("org.onap.appc.adapter.saltstack.result.code");
257 fail(e.getMessage() + " Code = " + status);
258 } catch (Exception e) {
259 fail(e.getMessage() + " Unknown exception encountered ");
264 public void reqExecLog_shouldSetMessage() throws IllegalStateException, IllegalArgumentException {
266 params.put("Id", "101");
269 adapter.reqExecLog(params, svcContext);
270 String status = svcContext.getAttribute("org.onap.appc.adapter.saltstack.log");
271 //assertEquals(message, status);
272 assertEquals(null, status);
273 } catch (SvcLogicException e) {
274 String status = svcContext.getAttribute("org.onap.appc.adapter.saltstack.log");
275 fail(e.getMessage() + " Code = " + status);
276 } catch (Exception e) {
277 fail(e.getMessage() + " Unknown exception encountered ");