8e13a66ea23b47451a0ef6deef9ad675b18da415
[appc.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2017-2018 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  * ============LICENSE_END=========================================================
22  */
23
24 package org.onap.appc.adapter.ansible.impl;
25
26 import static org.junit.Assert.assertEquals;
27 import static org.junit.Assert.assertNotNull;
28 import static org.mockito.Matchers.anyString;
29 import static org.mockito.Mockito.when;
30
31 import java.util.HashMap;
32 import java.util.Map;
33
34 import org.json.JSONException;
35 import org.json.JSONObject;
36 import org.junit.After;
37 import org.junit.Before;
38 import org.junit.BeforeClass;
39 import org.junit.Test;
40 import org.junit.runner.RunWith;
41 import org.mockito.Mock;
42 import org.mockito.Mockito;
43 import org.mockito.runners.MockitoJUnitRunner;
44 import org.onap.appc.adapter.ansible.model.AnsibleMessageParser;
45 import org.onap.appc.adapter.ansible.model.AnsibleResult;
46 import org.onap.appc.configuration.Configuration;
47 import org.onap.appc.configuration.ConfigurationFactory;
48 import org.onap.appc.exceptions.APPCException;
49 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
50 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
51 import org.powermock.reflect.Whitebox;
52
53 @RunWith(MockitoJUnitRunner.class)
54 public class TestAnsibleAdapterImpl {
55
56     private static String KEYSTORE_PASSWORD;
57     private static Configuration configuration;
58     private static final String RESULT_CODE_ATTRIBUTE_NAME = "org.onap.appc.adapter.ansible.result.code";
59     private static final String LOG_ATTRIBUTE_NAME = "org.onap.appc.adapter.ansible.log";
60     private static final String STATUS_CODE = "StatusCode";
61     private static final String STATUS_MESSAGE = "StatusMessage";
62     private static final String PENDING = "100";
63     private static final String SUCCESS = "SUCCESS";
64     private static final String MESSAGE_ATTRIBUTE_NAME = "SUCCESS";
65     private AnsibleAdapterImpl adapter;
66     private boolean testMode = true;
67     private Map<String, String> params;
68     private SvcLogicContext svcContext;
69     private JSONObject jsonPayload;
70     private AnsibleResult result;
71     private String agentUrl = "https://192.168.1.1";
72     private AnsibleAdapterImpl spyAdapter;
73
74     @Mock
75     private AnsibleMessageParser messageProcessor;
76
77     @Mock
78     private ConnectionBuilder httpClient;
79
80     /**
81      * Load the configuration properties
82      */
83     @BeforeClass
84     public static void once() {
85         configuration = ConfigurationFactory.getConfiguration();
86         KEYSTORE_PASSWORD = configuration.getProperty("org.onap.appc.adapter.ansible.trustStore.trustPasswd");
87
88     }
89
90     /**
91      * Use reflection to locate fields and methods so that they can be manipulated
92      * during the test to change the internal state accordingly.
93      *
94      */
95     @Before
96     public void setup() {
97         testMode = true;
98         svcContext = new SvcLogicContext();
99         adapter = new AnsibleAdapterImpl(testMode);
100         params = new HashMap<>();
101         params.put("AgentUrl", agentUrl);
102         jsonPayload = new JSONObject();
103         jsonPayload.put("Id", "100");
104         jsonPayload.put("User", "test");
105         jsonPayload.put("Password", "test");
106         jsonPayload.put("PlaybookName", "test_playbook.yaml");
107         jsonPayload.put("Timeout", "60000");
108         jsonPayload.put("AgentUrl", agentUrl);
109         result = new AnsibleResult();
110         result.setStatusMessage("Success");
111         result.setResults("Success");
112         result.setOutput("{}");
113         Whitebox.setInternalState(adapter, "messageProcessor", messageProcessor);
114         spyAdapter = Mockito.spy(adapter);
115     }
116
117     @After
118     public void tearDown() {
119         testMode = false;
120         adapter = null;
121         params = null;
122         svcContext = null;
123     }
124
125     /**
126      * This test case is used to test the request is submitted and the status is
127      * marked to pending
128      *
129      * @throws SvcLogicException
130      *             If the request cannot be process due to Number format or JSON
131      *             Exception
132      * @throws APPCException
133      *             If the request cannot be processed for some reason
134      */
135     @Test
136     public void reqExec_shouldSetPending() throws SvcLogicException, APPCException {
137         result.setStatusCode(Integer.valueOf(PENDING));
138         when(messageProcessor.reqMessage(params)).thenReturn(jsonPayload);
139         when(messageProcessor.parsePostResponse(anyString())).thenReturn(result);
140         spyAdapter.reqExec(params, svcContext);
141         assertEquals(PENDING, svcContext.getAttribute(RESULT_CODE_ATTRIBUTE_NAME));
142     }
143
144     /**
145      * This test case is used to test the request is process and the status is
146      * marked to success
147      *
148      * @throws SvcLogicException
149      *             If the request cannot be process due to Number format or JSON
150      *             Exception
151      * @throws APPCException
152      *             If the request cannot be processed for some reason
153      */
154    @Test(expected = SvcLogicException.class)
155     public void reqExecResult_shouldSetSuccess() throws SvcLogicException, APPCException {
156         params.put("Id", "100");
157         result.setStatusMessage(SUCCESS);
158         when(messageProcessor.reqUriResult(params)).thenReturn(agentUrl);
159         when(messageProcessor.parseGetResponse(anyString())).thenReturn(result);
160         spyAdapter.reqExecResult(params, svcContext);
161         assertEquals(SUCCESS, svcContext.getAttribute(MESSAGE_ATTRIBUTE_NAME));
162     }
163     /**
164      * This test case is used to test the Failure of the request
165      *
166      * @throws SvcLogicException
167      *             If the request cannot be process due to Number format or JSON
168      *             Exception
169      * @throws APPCException
170      *             If the request cannot be processed for some reason
171      */
172     @Test(expected = SvcLogicException.class)
173     public void reqExecResult_Failure() throws SvcLogicException, APPCException {
174         params.put("Id", "100");
175         result.setStatusCode(100);
176         result.setStatusMessage("Failed");
177         JSONObject cData = new JSONObject();
178         cData.put("GatewayInfo", "Radius");
179         result.setconfigData(cData.toString());
180         result.setOutput(cData.toString());
181         when(messageProcessor.reqUriResult(params)).thenReturn(agentUrl);
182         when(messageProcessor.parseGetResponse(anyString())).thenReturn(result);
183         adapter.reqExecResult(params, svcContext);
184     }
185
186     /**
187      * This test case is used to test the APPC Exception
188      *
189      * @throws SvcLogicException
190      *             If the request cannot be process due to Number format or JSON
191      *             Exception
192      * @throws APPCException
193      *             If the request cannot be processed for some reason
194      */
195     @Test(expected = SvcLogicException.class)
196     public void reqExecResult_appcException() throws APPCException, SvcLogicException {
197         when(messageProcessor.reqUriResult(params)).thenThrow(new APPCException());
198         adapter.reqExecResult(params, svcContext);
199     }
200
201     /**
202      * This test case is used to test the Number Format Exception
203      *
204      * @throws SvcLogicException
205      *             If the request cannot be process due to Number format or JSON
206      *             Exception
207      * @throws APPCException
208      *             If the request cannot be processed for some reason
209      */
210     @Test(expected = SvcLogicException.class)
211     public void reqExecResult_numberFormatException()
212             throws IllegalStateException, IllegalArgumentException, APPCException, SvcLogicException {
213         when(messageProcessor.reqUriResult(params)).thenThrow(new NumberFormatException());
214         adapter.reqExecResult(params, svcContext);
215     }
216
217     /**
218      * This test case is used to test the logs executed for the specific request
219      *
220      * @throws SvcLogicException
221      *             If the request cannot be process due to Number format or JSON
222      *             Exception
223      * @throws APPCException
224      *             If the request cannot be processed for some reason
225      */
226     @Test
227     public void reqExecLog_shouldSetMessage() throws SvcLogicException, APPCException {
228         params.put("Id", "101");
229         when(messageProcessor.reqUriLog(params)).thenReturn(agentUrl);
230         adapter.reqExecLog(params, svcContext);
231         String message = getResponseMessage();
232         assertEquals(message, svcContext.getAttribute(LOG_ATTRIBUTE_NAME));
233     }
234
235     private String getResponseMessage() {
236         JSONObject response = new JSONObject();
237         response.put(STATUS_CODE, 200);
238         response.put(STATUS_MESSAGE, "FINISHED");
239         JSONObject results = new JSONObject();
240
241         JSONObject vmResults = new JSONObject();
242         vmResults.put(STATUS_CODE, 200);
243         vmResults.put(STATUS_MESSAGE, "SUCCESS");
244         vmResults.put("Id", "");
245         results.put("192.168.1.10", vmResults);
246
247         response.put("Results", results);
248         String message = response.toString();
249         return message;
250     }
251
252     /**
253      * This test case is used to test the APPC Exception
254      *
255      * @throws SvcLogicException
256      *             If the request cannot be process due to Number format or JSON
257      *             Exception
258      * @throws APPCException
259      *             If the request cannot be processed for some reason
260      */
261     @Test(expected = SvcLogicException.class)
262     public void reqExecException()
263             throws IllegalStateException, IllegalArgumentException, APPCException, SvcLogicException {
264         when(messageProcessor.reqUriLog(params)).thenThrow(new APPCException("Appc Exception"));
265         adapter.reqExecLog(params, svcContext);
266     }
267
268     /**
269      * This test case is used to test the APPC Exception
270      *
271      * @throws SvcLogicException
272      *             If the request cannot be process due to Number format or JSON
273      *             Exception
274      * @throws APPCException
275      *             If the request cannot be processed for some reason
276      */
277     @Test(expected = SvcLogicException.class)
278     public void reqExec_AppcException()
279             throws IllegalStateException, IllegalArgumentException, SvcLogicException, APPCException {
280         when(messageProcessor.reqMessage(params)).thenThrow(new APPCException());
281         adapter.reqExec(params, svcContext);
282     }
283
284     /**
285      * This test case is used to test the JSON Exception
286      *
287      * @throws SvcLogicException
288      *             If the request cannot be process due to Number format or JSON
289      *             Exception
290      * @throws APPCException
291      *             If the request cannot be processed for some reason
292      */
293     @Test(expected = SvcLogicException.class)
294     public void reqExec_JsonException()
295             throws IllegalStateException, IllegalArgumentException, SvcLogicException, APPCException {
296         when(messageProcessor.reqMessage(params)).thenThrow(new JSONException("Json Exception"));
297         adapter.reqExec(params, svcContext);
298     }
299
300     /**
301      * This test case is used to test the Number Format Exception
302      *
303      * @throws SvcLogicException
304      *             If the request cannot be process due to Number format or JSON
305      *             Exception
306      * @throws APPCException
307      *             If the request cannot be processed for some reason
308      */
309     @Test(expected = SvcLogicException.class)
310     public void reqExec_NumberFormatException()
311             throws IllegalStateException, IllegalArgumentException, SvcLogicException, APPCException {
312         when(messageProcessor.reqMessage(params)).thenThrow(new NumberFormatException("Numbre Format Exception"));
313         adapter.reqExec(params, svcContext);
314     }
315
316     /**
317      * This test case is used to test the constructor with no client type
318      *
319      */
320     @Test
321     public void testInitializeWithDefault() {
322         configuration.setProperty("org.onap.appc.adapter.ansible.clientType", "");
323         adapter = new AnsibleAdapterImpl();
324         assertNotNull(adapter);
325     }
326
327     /**
328      * This test case is used to test the constructor with client type as TRUST_ALL
329      *
330      */
331     @Test
332     public void testInitializeWithTrustAll() {
333         configuration.setProperty("org.onap.appc.adapter.ansible.clientType", "TRUST_ALL");
334         adapter = new AnsibleAdapterImpl();
335         assertNotNull(adapter);
336     }
337
338     /**
339      * This test case is used to test the constructor with client type as TRUST_CERT
340      *
341      */
342     @Test
343     public void testInitializeWithTrustCert() {
344         configuration.setProperty("org.onap.appc.adapter.ansible.clientType", "TRUST_CERT");
345         configuration.setProperty("org.onap.appc.adapter.ansible.trustStore.trustPasswd", KEYSTORE_PASSWORD);
346         adapter = new AnsibleAdapterImpl();
347         assertNotNull(adapter);
348     }
349
350     /**
351      * This test case is used to test the constructor with exception
352      *
353      */
354     @Test
355     public void testInitializeWithException() {
356         configuration.setProperty("org.onap.appc.adapter.ansible.clientType", "TRUST_CERT");
357         configuration.setProperty("org.onap.appc.adapter.ansible.trustStore.trustPasswd", "appc");
358         adapter = new AnsibleAdapterImpl();
359     }
360
361 }