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 / TestConnectionBuilder.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2018 Ericsson. All rights reserved.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.appc.adapter.ansible.impl;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertNotNull;
25 import static org.mockito.Matchers.anyObject;
26 import static org.mockito.Matchers.eq;
27 import static org.mockito.Mockito.times;
28 import static org.mockito.Mockito.verify;
29 import static org.mockito.Mockito.when;
30 import org.junit.Ignore;
31 import java.io.IOException;
32 import java.security.KeyManagementException;
33 import java.security.KeyStoreException;
34 import java.security.NoSuchAlgorithmException;
35 import java.security.cert.CertificateException;
36 import java.util.Properties;
37
38 import org.apache.http.HttpEntity;
39 import org.apache.http.HttpResponse;
40 import org.apache.http.StatusLine;
41 import org.apache.http.client.ClientProtocolException;
42 import org.apache.http.client.methods.CloseableHttpResponse;
43 import org.apache.http.client.protocol.HttpClientContext;
44 import org.apache.http.impl.client.CloseableHttpClient;
45 import org.junit.After;
46 import org.junit.Before;
47 import org.junit.BeforeClass;
48 import org.junit.Test;
49 import org.junit.runner.RunWith;
50 import org.mockito.Mock;
51 import org.mockito.Mockito;
52 import org.mockito.runners.MockitoJUnitRunner;
53 import org.onap.appc.adapter.ansible.model.AnsibleMessageParser;
54 import org.onap.appc.adapter.ansible.model.AnsibleResult;
55 import org.onap.appc.adapter.ansible.model.AnsibleResultCodes;
56 import org.onap.appc.configuration.Configuration;
57 import org.onap.appc.configuration.ConfigurationFactory;
58 import org.onap.appc.exceptions.APPCException;
59 import org.powermock.reflect.Whitebox;
60
61 @RunWith(MockitoJUnitRunner.class)
62 public class TestConnectionBuilder {
63
64     private static String KEYSTORE_FILE;
65     private static String KEYSTORE_PASSWORD;
66     private static String KEYSTORE_CERTIFICATE;
67     private static String USERNAME;
68     private static String PASSWORD;
69     private static String URL;
70
71     private final int SUCCESS_STATUS = 200;
72     private ConnectionBuilder connectionBuilder;
73
74     @Mock
75     private AnsibleMessageParser messageProcessor;
76
77     @Mock
78     private CloseableHttpClient httpClient;
79
80     @Mock
81     private HttpClientContext httpClientContext;
82
83     @Mock
84     private CloseableHttpResponse response;
85
86     @Mock
87     private HttpEntity entity;
88
89     @Mock
90     private StatusLine statusLine;
91
92     /**
93      * Load the configuration properties
94      */
95     @BeforeClass
96     public static void once() {
97         Configuration configuration = ConfigurationFactory.getConfiguration();
98         Properties props = configuration.getProperties();
99         KEYSTORE_FILE = props.getProperty("org.onap.appc.adapter.ansible.trustStore");
100         KEYSTORE_PASSWORD = props.getProperty("org.onap.appc.adapter.ansible.trustStore.trustPasswd");
101         KEYSTORE_CERTIFICATE = props.getProperty("org.onap.appc.adapter.ansible.cert");
102         USERNAME = props.getProperty("org.onap.appc.adapter.ansible.username");
103         PASSWORD = props.getProperty("org.onap.appc.adapter.ansible.password");
104         URL = props.getProperty("org.onap.appc.adapter.ansible.identity");
105     }
106
107     /**
108      * Use reflection to locate fields and methods so that they can be manipulated during the test
109      * to change the internal state accordingly.
110      *
111      * @throws KeyManagementException If unable to manage the key
112      * @throws NoSuchAlgorithmException If an algorithm is found to be used but is unknown
113      * @throws KeyStoreException If any issues accessing the keystore
114      * @throws ClientProtocolException The client protocol exception
115      * @throws IOException Signals that an I/O exception has occurred.
116      */
117     @Before
118     public void setup() throws KeyManagementException, NoSuchAlgorithmException, KeyStoreException,
119             ClientProtocolException, IOException,APPCException {
120         connectionBuilder = new ConnectionBuilder(0,2000);
121         Whitebox.setInternalState(connectionBuilder, "httpClient", httpClient);
122         Whitebox.setInternalState(connectionBuilder, "httpContext", httpClientContext);
123         HttpResponse httpResponse = (HttpResponse) response;
124         when(httpResponse.getEntity()).thenReturn(entity);
125         when(httpResponse.getStatusLine()).thenReturn(statusLine);
126         when(statusLine.getStatusCode()).thenReturn(SUCCESS_STATUS);
127     }
128
129     @After
130     public void tearDown() {
131         connectionBuilder = null;
132     }
133
134     /**
135      * This test case is used to invoke the constructor with keystore file and trust store password.
136      *
137      * @throws KeyManagementException If unable to manage the key
138      * @throws KeyStoreException If any issues accessing the keystore
139      * @throws CertificateException If the certificate is tampared
140      * @throws NoSuchAlgorithmException If an algorithm is found to be used but is unknown
141      * @throws IOException Signals that an I/O exception has occurred.
142      * @throws APPCException If there are any application exception
143      */
144     @Test
145     public void testConnectionBuilder() throws KeyManagementException, KeyStoreException, CertificateException,
146             NoSuchAlgorithmException, IOException, APPCException {
147         char[] trustStorePassword = KEYSTORE_PASSWORD.toCharArray();
148         ConnectionBuilder connectionBuilder = new ConnectionBuilder(KEYSTORE_FILE, trustStorePassword,600000,"");
149         assertNotNull(connectionBuilder);
150     }
151
152     /**
153      * This test case is used to invoke the constructor with keystore certificate
154      *
155      * @throws KeyManagementException If unable to manage the key
156      * @throws KeyStoreException If any issues accessing the keystore
157      * @throws CertificateException If the certificate is tampared
158      * @throws NoSuchAlgorithmException If an algorithm is found to be used but is unknown
159      * @throws IOException Signals that an I/O exception has occurred.
160      * @throws APPCException If there are any application exception
161      */
162     @Test
163     public void testConnectionBuilderWithFilePath() throws KeyManagementException, KeyStoreException,
164             CertificateException, NoSuchAlgorithmException, IOException, APPCException {
165         new ConnectionBuilder(KEYSTORE_CERTIFICATE,600000);
166     }
167
168     /**
169      * This test case is used to set the http context with username and password
170      *
171      * @throws KeyManagementException If unable to manage the key
172      * @throws KeyStoreException If any issues accessing the keystore
173      * @throws CertificateException If the certificate is tampared
174      * @throws NoSuchAlgorithmException If an algorithm is found to be used but is unknown
175      * @throws IOException Signals that an I/O exception has occurred.
176      * @throws APPCException If there are any application exception
177      */
178     @Test
179     public void testSetHttpContext() throws KeyManagementException, KeyStoreException, CertificateException,
180             NoSuchAlgorithmException, IOException, APPCException {
181         ConnectionBuilder spyConnectionBuilder = Mockito.spy(connectionBuilder);
182         spyConnectionBuilder.setHttpContext(USERNAME, PASSWORD);
183         verify(spyConnectionBuilder, times(1)).setHttpContext(USERNAME, PASSWORD);
184     }
185
186     /**
187      * This test case is used to test the post method
188      *
189      * @throws KeyManagementException If unable to manage the key
190      * @throws KeyStoreException If any issues accessing the keystore
191      * @throws CertificateException If the certificate is tampared
192      * @throws NoSuchAlgorithmException If an algorithm is found to be used but is unknown
193      * @throws IOException Signals that an I/O exception has occurred.
194      * @throws APPCException If there are any application exception
195      */
196     @Test
197     public void testPost() throws KeyManagementException, KeyStoreException, CertificateException,
198             NoSuchAlgorithmException, IOException, APPCException {
199         when(httpClient.execute(anyObject(), eq(httpClientContext))).thenReturn(response);
200         AnsibleResult result = connectionBuilder.post(URL, "appc");
201         assertEquals(SUCCESS_STATUS, result.getStatusCode());
202     }
203
204     /**
205      * This test case is used to test the post method with exception
206      *
207      * @throws KeyManagementException If unable to manage the key
208      * @throws KeyStoreException If any issues accessing the keystore
209      * @throws CertificateException If the certificate is tampared
210      * @throws NoSuchAlgorithmException If an algorithm is found to be used but is unknown
211      * @throws IOException Signals that an I/O exception has occurred.
212      * @throws APPCException If there are any application exception
213      */
214     @Test
215     public void testPostWithException() throws KeyManagementException, KeyStoreException, CertificateException,
216             NoSuchAlgorithmException, IOException, APPCException {
217         when(httpClient.execute(anyObject(), eq(httpClientContext))).thenThrow(new IOException());
218         AnsibleResult result = connectionBuilder.post(URL, "appc");
219         assertEquals(AnsibleResultCodes.IO_EXCEPTION.getValue(), result.getStatusCode());
220     }
221
222     /**
223      * This test case is used to test the get method
224      *
225      * @throws KeyManagementException If unable to manage the key
226      * @throws KeyStoreException If any issues accessing the keystore
227      * @throws CertificateException If the certificate is tampared
228      * @throws NoSuchAlgorithmException If an algorithm is found to be used but is unknown
229      * @throws IOException Signals that an I/O exception has occurred.
230      * @throws APPCException If there are any application exception
231      */
232     @Ignore
233     @Test
234     public void testGet() throws KeyManagementException, KeyStoreException, CertificateException,
235             NoSuchAlgorithmException, IOException, APPCException {
236         when(httpClient.execute(anyObject(), eq(httpClientContext))).thenReturn(response);
237         AnsibleResult result = connectionBuilder.get(URL);
238         assertEquals(SUCCESS_STATUS, result.getStatusCode());
239     }
240
241     /**
242      * This test case is used to test the get method with exception
243      *
244      * @throws KeyManagementException If unable to manage the key
245      * @throws KeyStoreException If any issues accessing the keystore
246      * @throws CertificateException If the certificate is tampared
247      * @throws NoSuchAlgorithmException If an algorithm is found to be used but is unknown
248      * @throws IOException Signals that an I/O exception has occurred.
249      * @throws APPCException If there are any application exception
250      */
251     @Test
252     public void testGetWithException() throws KeyManagementException, KeyStoreException, CertificateException,
253             NoSuchAlgorithmException, IOException, APPCException {
254         when(httpClient.execute(anyObject(), eq(httpClientContext))).thenThrow(new IOException());
255         AnsibleResult result = connectionBuilder.get(URL);
256         assertEquals(AnsibleResultCodes.IO_EXCEPTION.getValue(), result.getStatusCode());
257     }
258
259 }