ansible adapter changes for multiple ansible servs
[appc.git] / appc-adapters / appc-ansible-adapter / appc-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-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 = "200";
64
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         Whitebox.setInternalState(adapter, "messageProcessor", messageProcessor);
113         spyAdapter = Mockito.spy(adapter);
114     }
115
116     @After
117     public void tearDown() {
118         testMode = false;
119         adapter = null;
120         params = null;
121         svcContext = null;
122     }
123
124     /**
125      * This test case is used to test the request is submitted and the status is
126      * marked to pending
127      *
128      * @throws SvcLogicException
129      *             If the request cannot be process due to Number format or JSON
130      *             Exception
131      * @throws APPCException
132      *             If the request cannot be processed for some reason
133      */
134     @Test
135     public void reqExec_shouldSetPending() throws SvcLogicException, APPCException {
136         result.setStatusCode(Integer.valueOf(PENDING));
137         when(messageProcessor.reqMessage(params)).thenReturn(jsonPayload);
138         when(messageProcessor.parsePostResponse(anyString())).thenReturn(result);
139         spyAdapter.reqExec(params, svcContext);
140         assertEquals(PENDING, svcContext.getAttribute(RESULT_CODE_ATTRIBUTE_NAME));
141     }
142
143     /**
144      * This test case is used to test the request is process and the status is
145      * marked to success
146      *
147      * @throws SvcLogicException
148      *             If the request cannot be process due to Number format or JSON
149      *             Exception
150      * @throws APPCException
151      *             If the request cannot be processed for some reason
152      */
153     @Test
154     public void reqExecResult_shouldSetSuccess() throws SvcLogicException, APPCException {
155         params.put("Id", "100");
156         result.setStatusCode(Integer.valueOf(SUCCESS));
157         when(messageProcessor.reqUriResult(params)).thenReturn(agentUrl);
158         when(messageProcessor.parseGetResponse(anyString())).thenReturn(result);
159         spyAdapter.reqExecResult(params, svcContext);
160         assertEquals("400", svcContext.getAttribute(RESULT_CODE_ATTRIBUTE_NAME));
161     }
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         when(messageProcessor.reqUriResult(params)).thenReturn(agentUrl);
178         when(messageProcessor.parseGetResponse(anyString())).thenReturn(result);
179         adapter.reqExecResult(params, svcContext);
180     }
181
182     /**
183      * This test case is used to test the APPC Exception
184      *
185      * @throws SvcLogicException
186      *             If the request cannot be process due to Number format or JSON
187      *             Exception
188      * @throws APPCException
189      *             If the request cannot be processed for some reason
190      */
191     @Test(expected = SvcLogicException.class)
192     public void reqExecResult_appcException() throws APPCException, SvcLogicException {
193         when(messageProcessor.reqUriResult(params)).thenThrow(new APPCException());
194         adapter.reqExecResult(params, svcContext);
195     }
196
197     /**
198      * This test case is used to test the Number Format Exception
199      *
200      * @throws SvcLogicException
201      *             If the request cannot be process due to Number format or JSON
202      *             Exception
203      * @throws APPCException
204      *             If the request cannot be processed for some reason
205      */
206     @Test(expected = SvcLogicException.class)
207     public void reqExecResult_numberFormatException()
208             throws IllegalStateException, IllegalArgumentException, APPCException, SvcLogicException {
209         when(messageProcessor.reqUriResult(params)).thenThrow(new NumberFormatException());
210         adapter.reqExecResult(params, svcContext);
211     }
212
213     /**
214      * This test case is used to test the logs executed for the specific request
215      *
216      * @throws SvcLogicException
217      *             If the request cannot be process due to Number format or JSON
218      *             Exception
219      * @throws APPCException
220      *             If the request cannot be processed for some reason
221      */
222     @Test
223     public void reqExecLog_shouldSetMessage() throws SvcLogicException, APPCException {
224         params.put("Id", "101");
225         when(messageProcessor.reqUriLog(params)).thenReturn(agentUrl);
226         adapter.reqExecLog(params, svcContext);
227         String message = getResponseMessage();
228         assertEquals(message, svcContext.getAttribute(LOG_ATTRIBUTE_NAME));
229     }
230
231     private String getResponseMessage() {
232         JSONObject response = new JSONObject();
233         response.put(STATUS_CODE, 200);
234         response.put(STATUS_MESSAGE, "FINISHED");
235         JSONObject results = new JSONObject();
236
237         JSONObject vmResults = new JSONObject();
238         vmResults.put(STATUS_CODE, 200);
239         vmResults.put(STATUS_MESSAGE, "SUCCESS");
240         vmResults.put("Id", "");
241         results.put("192.168.1.10", vmResults);
242
243         response.put("Results", results);
244         String message = response.toString();
245         return message;
246     }
247
248     /**
249      * This test case is used to test the APPC Exception
250      *
251      * @throws SvcLogicException
252      *             If the request cannot be process due to Number format or JSON
253      *             Exception
254      * @throws APPCException
255      *             If the request cannot be processed for some reason
256      */
257     @Test(expected = SvcLogicException.class)
258     public void reqExecException()
259             throws IllegalStateException, IllegalArgumentException, APPCException, SvcLogicException {
260         when(messageProcessor.reqUriLog(params)).thenThrow(new APPCException("Appc Exception"));
261         adapter.reqExecLog(params, svcContext);
262     }
263
264     /**
265      * This test case is used to test the APPC Exception
266      *
267      * @throws SvcLogicException
268      *             If the request cannot be process due to Number format or JSON
269      *             Exception
270      * @throws APPCException
271      *             If the request cannot be processed for some reason
272      */
273     @Test(expected = SvcLogicException.class)
274     public void reqExec_AppcException()
275             throws IllegalStateException, IllegalArgumentException, SvcLogicException, APPCException {
276         when(messageProcessor.reqMessage(params)).thenThrow(new APPCException());
277         adapter.reqExec(params, svcContext);
278     }
279
280     /**
281      * This test case is used to test the JSON Exception
282      *
283      * @throws SvcLogicException
284      *             If the request cannot be process due to Number format or JSON
285      *             Exception
286      * @throws APPCException
287      *             If the request cannot be processed for some reason
288      */
289     @Test(expected = SvcLogicException.class)
290     public void reqExec_JsonException()
291             throws IllegalStateException, IllegalArgumentException, SvcLogicException, APPCException {
292         when(messageProcessor.reqMessage(params)).thenThrow(new JSONException("Json Exception"));
293         adapter.reqExec(params, svcContext);
294     }
295
296     /**
297      * This test case is used to test the Number Format Exception
298      *
299      * @throws SvcLogicException
300      *             If the request cannot be process due to Number format or JSON
301      *             Exception
302      * @throws APPCException
303      *             If the request cannot be processed for some reason
304      */
305     @Test(expected = SvcLogicException.class)
306     public void reqExec_NumberFormatException()
307             throws IllegalStateException, IllegalArgumentException, SvcLogicException, APPCException {
308         when(messageProcessor.reqMessage(params)).thenThrow(new NumberFormatException("Numbre Format Exception"));
309         adapter.reqExec(params, svcContext);
310     }
311
312     /**
313      * This test case is used to test the constructor with no client type
314      *
315      */
316     @Test
317     public void testInitializeWithDefault() {
318         configuration.setProperty("org.onap.appc.adapter.ansible.clientType", "");
319         adapter = new AnsibleAdapterImpl();
320         assertNotNull(adapter);
321     }
322
323     /**
324      * This test case is used to test the constructor with client type as TRUST_ALL
325      *
326      */
327     @Test
328     public void testInitializeWithTrustAll() {
329         configuration.setProperty("org.onap.appc.adapter.ansible.clientType", "TRUST_ALL");
330         adapter = new AnsibleAdapterImpl();
331         assertNotNull(adapter);
332     }
333
334     /**
335      * This test case is used to test the constructor with client type as TRUST_CERT
336      *
337      */
338     @Test
339     public void testInitializeWithTrustCert() {
340         configuration.setProperty("org.onap.appc.adapter.ansible.clientType", "TRUST_CERT");
341         configuration.setProperty("org.onap.appc.adapter.ansible.trustStore.trustPasswd", KEYSTORE_PASSWORD);
342         adapter = new AnsibleAdapterImpl();
343         assertNotNull(adapter);
344     }
345
346     /**
347      * This test case is used to test the constructor with exception
348      *
349      */
350     @Test
351     public void testInitializeWithException() {
352         configuration.setProperty("org.onap.appc.adapter.ansible.clientType", "TRUST_CERT");
353         configuration.setProperty("org.onap.appc.adapter.ansible.trustStore.trustPasswd", "appc");
354         adapter = new AnsibleAdapterImpl();
355     }
356
357 }