2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
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 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
20 * ============LICENSE_END=========================================================
23 package org.onap.ccsdk.adapter.ansible.impl;
26 import java.io.FileNotFoundException;
27 import java.io.IOException;
28 import java.security.KeyManagementException;
29 import java.security.KeyStoreException;
30 import java.security.NoSuchAlgorithmException;
31 import java.security.cert.CertificateException;
32 import java.util.Properties;
33 import org.apache.http.HttpEntity;
34 import org.apache.http.HttpResponse;
35 import org.apache.http.StatusLine;
36 import org.apache.http.client.methods.CloseableHttpResponse;
37 import org.apache.http.client.protocol.HttpClientContext;
38 import org.apache.http.impl.client.CloseableHttpClient;
39 import org.junit.After;
40 import org.junit.Before;
41 import org.junit.BeforeClass;
42 import org.junit.Ignore;
43 import org.junit.Test;
44 import org.junit.runner.RunWith;
45 import org.mockito.Mock;
46 import org.mockito.Mockito;
47 import org.mockito.runners.MockitoJUnitRunner;
48 import org.onap.ccsdk.sli.adaptors.ansible.impl.AnsibleAdapterPropertiesProviderImpl;
49 import org.onap.ccsdk.sli.adaptors.ansible.impl.ConnectionBuilder;
50 import org.onap.ccsdk.sli.adaptors.ansible.model.AnsibleResult;
51 import org.onap.ccsdk.sli.adaptors.ansible.model.AnsibleResultCodes;
52 import org.powermock.reflect.Whitebox;
54 import static org.junit.Assert.assertEquals;
55 import static org.junit.Assert.assertNotNull;
56 import static org.junit.Assert.assertNull;
57 import static org.mockito.Matchers.anyObject;
58 import static org.mockito.Matchers.eq;
59 import static org.mockito.Mockito.times;
60 import static org.mockito.Mockito.verify;
61 import static org.mockito.Mockito.when;
62 import static org.onap.ccsdk.sli.adaptors.ansible.AnsibleAdapterConstants.*;
64 @RunWith(MockitoJUnitRunner.class)
65 public class TestConnectionBuilder {
67 private static String KEYSTORE_FILE;
68 private static String KEYSTORE_PSWD;
69 private static String KEYSTORE_CERTIFICATE;
70 private static String USER;
71 private static String PSWD;
72 private static String URL;
74 private final int SUCCESS_STATUS = 200;
75 private ConnectionBuilder connectionBuilder;
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 final String configFilePath = "src/test/resources/properties/ansible-adapter-test.properties".replace("/", File.separator);
98 Properties properties = new AnsibleAdapterPropertiesProviderImpl(configFilePath).getProperties();
100 KEYSTORE_FILE = properties.getProperty(TRUSTSTORE_PROPERTY_NAME);
101 KEYSTORE_PSWD = properties.getProperty(TRUSTSTORE_PASS_PROPERTY_NAME);
102 KEYSTORE_CERTIFICATE = properties.getProperty("org.onap.appc.adapter.ansible.cert");
103 USER = properties.getProperty("org.onap.appc.adapter.ansible.username");
104 PSWD = properties.getProperty("org.onap.appc.adapter.ansible.password");
105 URL = properties.getProperty("org.onap.appc.adapter.ansible.identity");
109 public void setup() throws KeyManagementException, NoSuchAlgorithmException, KeyStoreException {
110 connectionBuilder = new ConnectionBuilder(1, 2000);
111 Whitebox.setInternalState(connectionBuilder, "httpClient", httpClient);
112 Whitebox.setInternalState(connectionBuilder, "httpContext", httpClientContext);
113 HttpResponse httpResponse = response;
114 when(httpResponse.getEntity()).thenReturn(entity);
115 when(httpResponse.getStatusLine()).thenReturn(statusLine);
116 when(statusLine.getStatusCode()).thenReturn(SUCCESS_STATUS);
120 public void tearDown() {
121 connectionBuilder = null;
125 public void testConnectionBuilder() throws KeyManagementException, KeyStoreException, CertificateException,
126 NoSuchAlgorithmException, IOException {
127 char[] trustStorePassword = KEYSTORE_PSWD.toCharArray();
128 ConnectionBuilder connectionBuilder = new ConnectionBuilder(KEYSTORE_FILE, trustStorePassword, 600000, "");
129 assertNotNull(connectionBuilder);
133 public void testConnectionBuilderWithFilePath() throws KeyManagementException, KeyStoreException,
134 CertificateException, NoSuchAlgorithmException, IOException {
135 new ConnectionBuilder(KEYSTORE_CERTIFICATE, 600000);
139 public void testSetHttpContext() {
140 ConnectionBuilder spyConnectionBuilder = Mockito.spy(connectionBuilder);
141 spyConnectionBuilder.setHttpContext(USER, PSWD);
142 verify(spyConnectionBuilder, times(1)).setHttpContext(USER, PSWD);
146 public void testPost() throws IOException {
147 when(httpClient.execute(anyObject(), eq(httpClientContext))).thenReturn(response);
148 AnsibleResult result = connectionBuilder.post(URL, "appc");
149 assertNull(result.getStatusMessage());
150 assertEquals(SUCCESS_STATUS, result.getStatusCode());
151 assertEquals("UNKNOWN", result.getResults());
155 public void testPostWithException() throws IOException {
156 when(httpClient.execute(anyObject(), eq(httpClientContext))).thenThrow(new IOException());
157 AnsibleResult result = connectionBuilder.post(URL, "appc");
158 assertEquals(AnsibleResultCodes.IO_EXCEPTION.getValue(), result.getStatusCode());
163 public void testGet() throws IOException {
164 when(httpClient.execute(anyObject(), eq(httpClientContext))).thenReturn(response);
165 AnsibleResult result = connectionBuilder.get(URL);
166 assertNull(result.getStatusMessage());
167 assertEquals(SUCCESS_STATUS, result.getStatusCode());
168 assertEquals("UNKNOWN", result.getResults());
172 public void testGetWithException() throws IOException {
173 when(httpClient.execute(anyObject(), eq(httpClientContext))).thenThrow(new IOException());
174 AnsibleResult result = connectionBuilder.get(URL);
175 assertEquals(AnsibleResultCodes.IO_EXCEPTION.getValue(), result.getStatusCode());
179 public void testClose() {
180 connectionBuilder.close();
184 public void testGetMode() throws NoSuchAlgorithmException, KeyStoreException, KeyManagementException {
185 connectionBuilder = new ConnectionBuilder(2, 2000);
186 connectionBuilder.setHttpContext(USER, PSWD);
187 AnsibleResult result = connectionBuilder.get("test.server.com");
189 assertEquals(611, result.getStatusCode());
190 assertNull(result.getStatusMessage());
191 assertEquals("UNKNOWN", result.getResults());
194 @Test (expected = FileNotFoundException.class)
195 public void testGetModeNoCert() throws KeyStoreException, CertificateException, IOException,
196 KeyManagementException, NoSuchAlgorithmException {
197 String certFile = "testCert";
199 connectionBuilder = new ConnectionBuilder(certFile, 2000);
200 connectionBuilder.setHttpContext(USER, PSWD);
201 AnsibleResult result = connectionBuilder.get(URL);
203 assertEquals(611, result.getStatusCode());
204 assertNull(result.getStatusMessage());
205 assertEquals("UNKNOWN", result.getResults());
209 public void testGetModeCert() throws KeyStoreException, CertificateException, IOException,
210 KeyManagementException, NoSuchAlgorithmException {
211 String certFile = "src/test/resources/cert";
213 connectionBuilder = new ConnectionBuilder(certFile, 2000);
214 connectionBuilder.setHttpContext(USER, PSWD);
215 AnsibleResult result = connectionBuilder.get("test.server.com");
217 assertEquals(611, result.getStatusCode());
218 assertNull(result.getStatusMessage());
219 assertEquals("UNKNOWN", result.getResults());
222 @Test (expected = IOException.class)
223 public void testGetModeStore() throws KeyStoreException, CertificateException, IOException,
224 KeyManagementException, NoSuchAlgorithmException {
225 String store = "src/test/resources/cert";
227 connectionBuilder = new ConnectionBuilder(store, new char['t'], 2000, "1.1.1.1" );
228 connectionBuilder.setHttpContext(USER, PSWD);
229 AnsibleResult result = connectionBuilder.get(URL);
231 assertEquals(611, result.getStatusCode());
232 assertNull(result.getStatusMessage());
233 assertEquals("UNKNOWN", result.getResults());