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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.appc.adapter.ansible.impl;
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;
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;
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;
61 @RunWith(MockitoJUnitRunner.class)
62 public class TestConnectionBuilder {
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;
71 private final int SUCCESS_STATUS = 200;
72 private ConnectionBuilder connectionBuilder;
75 private AnsibleMessageParser messageProcessor;
78 private CloseableHttpClient httpClient;
81 private HttpClientContext httpClientContext;
84 private CloseableHttpResponse response;
87 private HttpEntity entity;
90 private StatusLine statusLine;
93 * Load the configuration properties
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");
108 * Use reflection to locate fields and methods so that they can be manipulated during the test
109 * to change the internal state accordingly.
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.
118 public void setup() throws KeyManagementException, NoSuchAlgorithmException, KeyStoreException,
119 ClientProtocolException, IOException {
120 connectionBuilder = new ConnectionBuilder(0);
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);
130 public void tearDown() {
131 connectionBuilder = null;
135 * This test case is used to invoke the constructor with keystore file and trust store password.
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
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);
149 assertNotNull(connectionBuilder);
153 * This test case is used to invoke the constructor with keystore certificate
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
163 public void testConnectionBuilderWithFilePath() throws KeyManagementException, KeyStoreException,
164 CertificateException, NoSuchAlgorithmException, IOException, APPCException {
165 new ConnectionBuilder(KEYSTORE_CERTIFICATE);
169 * This test case is used to set the http context with username and password
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
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);
187 * This test case is used to test the post method
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
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());
205 * This test case is used to test the post method with exception
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
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());
223 * This test case is used to test the get method
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
233 public void testGet() throws KeyManagementException, KeyStoreException, CertificateException,
234 NoSuchAlgorithmException, IOException, APPCException {
235 when(httpClient.execute(anyObject(), eq(httpClientContext))).thenReturn(response);
236 AnsibleResult result = connectionBuilder.get(URL);
237 assertEquals(SUCCESS_STATUS, result.getStatusCode());
241 * This test case is used to test the get method with exception
243 * @throws KeyManagementException If unable to manage the key
244 * @throws KeyStoreException If any issues accessing the keystore
245 * @throws CertificateException If the certificate is tampared
246 * @throws NoSuchAlgorithmException If an algorithm is found to be used but is unknown
247 * @throws IOException Signals that an I/O exception has occurred.
248 * @throws APPCException If there are any application exception
251 public void testGetWithException() throws KeyManagementException, KeyStoreException, CertificateException,
252 NoSuchAlgorithmException, IOException, APPCException {
253 when(httpClient.execute(anyObject(), eq(httpClientContext))).thenThrow(new IOException());
254 AnsibleResult result = connectionBuilder.get(URL);
255 assertEquals(AnsibleResultCodes.IO_EXCEPTION.getValue(), result.getStatusCode());