2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
20 * ============LICENSE_END=========================================================
23 package org.onap.ccsdk.ali.adaptors.ansible.impl;
25 import java.util.HashMap;
27 import java.util.Properties;
28 import org.json.JSONException;
29 import org.json.JSONObject;
30 import org.junit.After;
31 import org.junit.Before;
32 import org.junit.BeforeClass;
33 import org.junit.Test;
34 import org.junit.runner.RunWith;
35 import org.mockito.Mock;
36 import org.mockito.Mockito;
37 import org.mockito.runners.MockitoJUnitRunner;
38 import org.onap.ccsdk.sli.adaptors.ansible.impl.AnsibleAdaptorImpl;
39 import org.onap.ccsdk.sli.adaptors.ansible.impl.AnsibleAdaptorPropertiesProviderImpl;
40 import org.onap.ccsdk.sli.adaptors.ansible.model.AnsibleMessageParser;
41 import org.onap.ccsdk.sli.adaptors.ansible.model.AnsibleResult;
42 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
43 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
44 import org.powermock.reflect.Whitebox;
46 import static org.junit.Assert.assertEquals;
47 import static org.junit.Assert.assertNotNull;
48 import static org.mockito.Matchers.anyString;
49 import static org.mockito.Mockito.when;
50 import static org.onap.ccsdk.sli.adaptors.ansible.AnsibleAdaptorConstants.*;
51 @RunWith(MockitoJUnitRunner.class)
52 public class TestAnsibleAdaptorImpl {
54 private static final String PENDING = "100";
55 private static final String AGENT_URL = "https://192.168.1.1";
57 private static String KEYSTORE_PSWD;
58 private static Properties properties;
59 private boolean testMode = true;
61 private AnsibleAdaptorImpl adaptor;
62 private AnsibleResult result;
63 private AnsibleAdaptorImpl spyAdaptor;
64 private Map<String, String> params;
65 private SvcLogicContext svcContext;
66 private JSONObject jsonPayload;
69 private AnsibleMessageParser messageProcessor;
72 public static void once() {
73 properties = new AnsibleAdaptorPropertiesProviderImpl().getProperties();
74 KEYSTORE_PSWD = properties.getProperty("org.onap.appc.adaptor.ansible.trustStore.trustPasswd");
78 * Use reflection to locate fields and methods so that they can be manipulated
79 * during the test to change the internal state accordingly.
84 svcContext = new SvcLogicContext();
85 adaptor = new AnsibleAdaptorImpl(testMode);
86 params = new HashMap<>();
87 params.put("AgentUrl", AGENT_URL);
88 jsonPayload = new JSONObject();
89 jsonPayload.put("Id", "100");
90 jsonPayload.put("User", "test");
91 jsonPayload.put("Password", "test");
92 jsonPayload.put("PlaybookName", "test_playbook.yaml");
93 jsonPayload.put("Timeout", "60000");
94 jsonPayload.put("AgentUrl", AGENT_URL);
95 result = new AnsibleResult();
96 result.setStatusMessage("Success");
97 result.setResults("Success");
98 result.setOutput("{}");
99 Whitebox.setInternalState(adaptor, "messageProcessor", messageProcessor);
100 spyAdaptor = Mockito.spy(adaptor);
104 public void tearDown() {
112 public void reqExec_shouldSetPending() throws SvcLogicException {
113 result.setStatusCode(Integer.parseInt(PENDING));
114 when(messageProcessor.reqMessage(params)).thenReturn(jsonPayload);
115 when(messageProcessor.parsePostResponse(anyString())).thenReturn(result);
116 spyAdaptor.reqExec(params, svcContext);
117 assertEquals(PENDING, svcContext.getAttribute(RESULT_CODE_ATTRIBUTE_NAME));
120 @Test(expected = SvcLogicException.class)
121 public void reqExecResult_shouldSetSuccess() throws SvcLogicException {
122 params.put("Id", "100");
123 result.setStatusMessage(SUCCESS);
124 when(messageProcessor.reqUriResult(params)).thenReturn(AGENT_URL);
125 when(messageProcessor.parseGetResponse(anyString())).thenReturn(result);
126 spyAdaptor.reqExecResult(params, svcContext);
127 assertEquals(SUCCESS, svcContext.getAttribute(SUCCESS));
129 @Test(expected = SvcLogicException.class)
130 public void reqExecResult_Failure() throws SvcLogicException {
131 params.put("Id", "100");
132 result.setStatusCode(100);
133 result.setStatusMessage("Failed");
134 JSONObject cData = new JSONObject();
135 cData.put("GatewayInfo", "Radius");
136 result.setConfigData(cData.toString());
137 result.setOutput(cData.toString());
138 when(messageProcessor.reqUriResult(params)).thenReturn(AGENT_URL);
139 when(messageProcessor.parseGetResponse(anyString())).thenReturn(result);
140 adaptor.reqExecResult(params, svcContext);
143 @Test(expected = SvcLogicException.class)
144 public void reqExecResult_SvcLogicException() throws SvcLogicException {
145 when(messageProcessor.reqUriResult(params)).thenThrow(new SvcLogicException());
146 adaptor.reqExecResult(params, svcContext);
149 @Test(expected = SvcLogicException.class)
150 public void reqExecResult_numberFormatException()
151 throws IllegalStateException, IllegalArgumentException, SvcLogicException {
152 when(messageProcessor.reqUriResult(params)).thenThrow(new NumberFormatException());
153 adaptor.reqExecResult(params, svcContext);
157 public void reqExecLog_shouldSetMessage() throws SvcLogicException {
158 params.put("Id", "101");
159 when(messageProcessor.reqUriLog(params)).thenReturn(AGENT_URL);
160 adaptor.reqExecLog(params, svcContext);
161 String message = getResponseMessage();
162 assertEquals(message, svcContext.getAttribute(LOG_ATTRIBUTE_NAME));
165 private String getResponseMessage() {
166 JSONObject response = new JSONObject();
167 response.put(STATUS_CODE, 200);
168 response.put(STATUS_MESSAGE, "FINISHED");
169 JSONObject results = new JSONObject();
171 JSONObject vmResults = new JSONObject();
172 vmResults.put(STATUS_CODE, 200);
173 vmResults.put(STATUS_MESSAGE, "SUCCESS");
174 vmResults.put("Id", "");
175 results.put("192.168.1.10", vmResults);
177 response.put("Results", results);
178 return response.toString();
181 @Test(expected = SvcLogicException.class)
182 public void reqExecException()
183 throws IllegalStateException, IllegalArgumentException, SvcLogicException {
184 when(messageProcessor.reqUriLog(params)).thenThrow(new SvcLogicException("Appc Exception"));
185 adaptor.reqExecLog(params, svcContext);
188 @Test(expected = SvcLogicException.class)
189 public void reqExec_SvcLogicException()
190 throws IllegalStateException, IllegalArgumentException, SvcLogicException {
191 when(messageProcessor.reqMessage(params)).thenThrow(new SvcLogicException());
192 adaptor.reqExec(params, svcContext);
195 @Test(expected = SvcLogicException.class)
196 public void reqExec_JsonException()
197 throws IllegalStateException, IllegalArgumentException, SvcLogicException {
198 when(messageProcessor.reqMessage(params)).thenThrow(new JSONException("Json Exception"));
199 adaptor.reqExec(params, svcContext);
202 @Test(expected = SvcLogicException.class)
203 public void reqExec_NumberFormatException()
204 throws IllegalStateException, IllegalArgumentException, SvcLogicException {
205 when(messageProcessor.reqMessage(params)).thenThrow(new NumberFormatException("Numbre Format Exception"));
206 adaptor.reqExec(params, svcContext);
210 public void testInitializeWithDefault() {
211 properties.setProperty("org.onap.appc.adaptor.ansible.clientType", "");
212 adaptor = new AnsibleAdaptorImpl();
213 assertNotNull(adaptor);
217 public void testInitializeWithTrustAll() {
218 properties.setProperty("org.onap.appc.adaptor.ansible.clientType", "TRUST_ALL");
219 adaptor = new AnsibleAdaptorImpl();
220 assertNotNull(adaptor);
224 public void testInitializeWithTrustCert() {
225 properties.setProperty("org.onap.appc.adaptor.ansible.clientType", "TRUST_CERT");
226 properties.setProperty("org.onap.appc.adaptor.ansible.trustStore.trustPasswd", KEYSTORE_PSWD);
227 adaptor = new AnsibleAdaptorImpl();
228 assertNotNull(adaptor);
232 public void testInitializeWithException() {
233 properties.setProperty("org.onap.appc.adaptor.ansible.clientType", "TRUST_CERT");
234 properties.setProperty("org.onap.appc.adaptor.ansible.trustStore.trustPasswd", "appc");
235 adaptor = new AnsibleAdaptorImpl();
236 assertNotNull(adaptor);