2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2018 Ericsson. All rights reserved.
4 * ================================================================================
5 * Modifications Copyright (C) 2019 IBM.
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 * SPDX-License-Identifier: Apache-2.0
20 * ============LICENSE_END=========================================================
23 package org.onap.appc.adapter.ansible.impl;
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertNotNull;
27 import static org.mockito.Matchers.anyObject;
28 import static org.mockito.Matchers.eq;
29 import static org.mockito.Mockito.times;
30 import static org.mockito.Mockito.verify;
31 import static org.mockito.Mockito.when;
32 import org.junit.Ignore;
33 import java.io.IOException;
34 import java.security.KeyManagementException;
35 import java.security.KeyStoreException;
36 import java.security.NoSuchAlgorithmException;
37 import java.security.cert.CertificateException;
38 import java.util.Properties;
40 import org.apache.http.HttpEntity;
41 import org.apache.http.HttpResponse;
42 import org.apache.http.StatusLine;
43 import org.apache.http.client.ClientProtocolException;
44 import org.apache.http.client.methods.CloseableHttpResponse;
45 import org.apache.http.client.protocol.HttpClientContext;
46 import org.apache.http.impl.client.CloseableHttpClient;
47 import org.junit.After;
48 import org.junit.Before;
49 import org.junit.BeforeClass;
50 import org.junit.Test;
51 import org.junit.runner.RunWith;
52 import org.mockito.Mock;
53 import org.mockito.Mockito;
54 import org.mockito.runners.MockitoJUnitRunner;
55 import org.onap.appc.adapter.ansible.model.AnsibleMessageParser;
56 import org.onap.appc.adapter.ansible.model.AnsibleResult;
57 import org.onap.appc.adapter.ansible.model.AnsibleResultCodes;
58 import org.onap.appc.configuration.Configuration;
59 import org.onap.appc.configuration.ConfigurationFactory;
60 import org.onap.appc.exceptions.APPCException;
61 import org.powermock.reflect.Whitebox;
63 @RunWith(MockitoJUnitRunner.class)
64 public class TestConnectionBuilder {
66 private static String KEYSTORE_FILE;
67 private static String KEYSTORE_PASSWORD;
68 private static String KEYSTORE_CERTIFICATE;
69 private static String USERNAME;
70 private static String PASSWORD;
71 private static String URL;
73 private final int SUCCESS_STATUS = 200;
74 private ConnectionBuilder connectionBuilder;
77 private AnsibleMessageParser messageProcessor;
80 private CloseableHttpClient httpClient;
83 private HttpClientContext httpClientContext;
86 private CloseableHttpResponse response;
89 private HttpEntity entity;
92 private StatusLine statusLine;
95 * Load the configuration properties
98 public static void once() {
99 Configuration configuration = ConfigurationFactory.getConfiguration();
100 Properties props = configuration.getProperties();
101 KEYSTORE_FILE = props.getProperty("org.onap.appc.adapter.ansible.trustStore");
102 KEYSTORE_PASSWORD = props.getProperty("org.onap.appc.adapter.ansible.trustStore.trustPasswd");
103 KEYSTORE_CERTIFICATE = props.getProperty("org.onap.appc.adapter.ansible.cert");
104 USERNAME = props.getProperty("org.onap.appc.adapter.ansible.username");
105 PASSWORD = props.getProperty("org.onap.appc.adapter.ansible.password");
106 URL = props.getProperty("org.onap.appc.adapter.ansible.identity");
110 * Use reflection to locate fields and methods so that they can be manipulated during the test
111 * to change the internal state accordingly.
113 * @throws KeyManagementException If unable to manage the key
114 * @throws NoSuchAlgorithmException If an algorithm is found to be used but is unknown
115 * @throws KeyStoreException If any issues accessing the keystore
116 * @throws ClientProtocolException The client protocol exception
117 * @throws IOException Signals that an I/O exception has occurred.
120 public void setup() throws KeyManagementException, NoSuchAlgorithmException, KeyStoreException,
121 ClientProtocolException, IOException,APPCException {
122 connectionBuilder = new ConnectionBuilder(1,2000);
123 Whitebox.setInternalState(connectionBuilder, "httpClient", httpClient);
124 Whitebox.setInternalState(connectionBuilder, "httpContext", httpClientContext);
125 HttpResponse httpResponse = (HttpResponse) response;
126 when(httpResponse.getEntity()).thenReturn(entity);
127 when(httpResponse.getStatusLine()).thenReturn(statusLine);
128 when(statusLine.getStatusCode()).thenReturn(SUCCESS_STATUS);
132 public void tearDown() {
133 connectionBuilder = null;
137 * This test case is used to invoke the constructor with keystore file and trust store password.
139 * @throws KeyManagementException If unable to manage the key
140 * @throws KeyStoreException If any issues accessing the keystore
141 * @throws CertificateException If the certificate is tampared
142 * @throws NoSuchAlgorithmException If an algorithm is found to be used but is unknown
143 * @throws IOException Signals that an I/O exception has occurred.
144 * @throws APPCException If there are any application exception
147 public void testConnectionBuilder() throws KeyManagementException, KeyStoreException, CertificateException,
148 NoSuchAlgorithmException, IOException, APPCException {
149 char[] trustStorePassword = KEYSTORE_PASSWORD.toCharArray();
150 ConnectionBuilder connectionBuilder = new ConnectionBuilder(KEYSTORE_FILE, trustStorePassword,600000,"");
151 assertNotNull(connectionBuilder);
155 * This test case is used to invoke the constructor with keystore certificate
157 * @throws KeyManagementException If unable to manage the key
158 * @throws KeyStoreException If any issues accessing the keystore
159 * @throws CertificateException If the certificate is tampared
160 * @throws NoSuchAlgorithmException If an algorithm is found to be used but is unknown
161 * @throws IOException Signals that an I/O exception has occurred.
162 * @throws APPCException If there are any application exception
165 public void testConnectionBuilderWithFilePath() throws KeyManagementException, KeyStoreException,
166 CertificateException, NoSuchAlgorithmException, IOException, APPCException {
167 new ConnectionBuilder(KEYSTORE_CERTIFICATE,600000);
171 * This test case is used to set the http context with username and password
173 * @throws KeyManagementException If unable to manage the key
174 * @throws KeyStoreException If any issues accessing the keystore
175 * @throws CertificateException If the certificate is tampared
176 * @throws NoSuchAlgorithmException If an algorithm is found to be used but is unknown
177 * @throws IOException Signals that an I/O exception has occurred.
178 * @throws APPCException If there are any application exception
181 public void testSetHttpContext() throws KeyManagementException, KeyStoreException, CertificateException,
182 NoSuchAlgorithmException, IOException, APPCException {
183 ConnectionBuilder spyConnectionBuilder = Mockito.spy(connectionBuilder);
184 spyConnectionBuilder.setHttpContext(USERNAME, PASSWORD);
185 verify(spyConnectionBuilder, times(1)).setHttpContext(USERNAME, PASSWORD);
189 * This test case is used to test the post method
191 * @throws KeyManagementException If unable to manage the key
192 * @throws KeyStoreException If any issues accessing the keystore
193 * @throws CertificateException If the certificate is tampared
194 * @throws NoSuchAlgorithmException If an algorithm is found to be used but is unknown
195 * @throws IOException Signals that an I/O exception has occurred.
196 * @throws APPCException If there are any application exception
199 public void testPost() throws KeyManagementException, KeyStoreException, CertificateException,
200 NoSuchAlgorithmException, IOException, APPCException {
201 when(httpClient.execute(anyObject(), eq(httpClientContext))).thenReturn(response);
202 AnsibleResult result = connectionBuilder.post(URL, "appc");
203 assertEquals(SUCCESS_STATUS, result.getStatusCode());
207 * This test case is used to test the post method with exception
209 * @throws KeyManagementException If unable to manage the key
210 * @throws KeyStoreException If any issues accessing the keystore
211 * @throws CertificateException If the certificate is tampared
212 * @throws NoSuchAlgorithmException If an algorithm is found to be used but is unknown
213 * @throws IOException Signals that an I/O exception has occurred.
214 * @throws APPCException If there are any application exception
217 public void testPostWithException() throws KeyManagementException, KeyStoreException, CertificateException,
218 NoSuchAlgorithmException, IOException, APPCException {
219 when(httpClient.execute(anyObject(), eq(httpClientContext))).thenThrow(new IOException());
220 AnsibleResult result = connectionBuilder.post(URL, "appc");
221 assertEquals(AnsibleResultCodes.IO_EXCEPTION.getValue(), result.getStatusCode());
225 * This test case is used to test the get method
227 * @throws KeyManagementException If unable to manage the key
228 * @throws KeyStoreException If any issues accessing the keystore
229 * @throws CertificateException If the certificate is tampared
230 * @throws NoSuchAlgorithmException If an algorithm is found to be used but is unknown
231 * @throws IOException Signals that an I/O exception has occurred.
232 * @throws APPCException If there are any application exception
236 public void testGet() throws KeyManagementException, KeyStoreException, CertificateException,
237 NoSuchAlgorithmException, IOException, APPCException {
238 when(httpClient.execute(anyObject(), eq(httpClientContext))).thenReturn(response);
239 AnsibleResult result = connectionBuilder.get(URL);
240 assertEquals(SUCCESS_STATUS, result.getStatusCode());
244 * This test case is used to test the get method with exception
246 * @throws KeyManagementException If unable to manage the key
247 * @throws KeyStoreException If any issues accessing the keystore
248 * @throws CertificateException If the certificate is tampared
249 * @throws NoSuchAlgorithmException If an algorithm is found to be used but is unknown
250 * @throws IOException Signals that an I/O exception has occurred.
251 * @throws APPCException If there are any application exception
254 public void testGetWithException() throws KeyManagementException, KeyStoreException, CertificateException,
255 NoSuchAlgorithmException, IOException, APPCException {
256 when(httpClient.execute(anyObject(), eq(httpClientContext))).thenThrow(new IOException());
257 AnsibleResult result = connectionBuilder.get(URL);
258 assertEquals(AnsibleResultCodes.IO_EXCEPTION.getValue(), result.getStatusCode());