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