25f89863d9eafa91e285720e3cb25ce5186fa40f
[ccsdk/sli.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : SLI
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  *
19  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.ccsdk.adapter.ansible.impl;
24
25 import java.io.File;
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;
53
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.*;
63
64 @RunWith(MockitoJUnitRunner.class)
65 public class TestConnectionBuilder {
66
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;
73
74     private final int SUCCESS_STATUS = 200;
75     private ConnectionBuilder connectionBuilder;
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         final String configFilePath = "src/test/resources/properties/ansible-adapter-test.properties".replace("/", File.separator);
98         Properties properties = new AnsibleAdapterPropertiesProviderImpl(configFilePath).getProperties();
99
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");
106     }
107
108     @Before
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);
117     }
118
119     @After
120     public void tearDown() {
121         connectionBuilder = null;
122     }
123
124     @Test
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);
130     }
131
132     @Test
133     public void testConnectionBuilderWithFilePath() throws KeyManagementException, KeyStoreException,
134             CertificateException, NoSuchAlgorithmException, IOException {
135         new ConnectionBuilder(KEYSTORE_CERTIFICATE, 600000);
136     }
137
138     @Test
139     public void testSetHttpContext() {
140         ConnectionBuilder spyConnectionBuilder = Mockito.spy(connectionBuilder);
141         spyConnectionBuilder.setHttpContext(USER, PSWD);
142         verify(spyConnectionBuilder, times(1)).setHttpContext(USER, PSWD);
143     }
144
145     @Test
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());
152     }
153
154     @Test
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());
159     }
160
161     @Ignore
162     @Test
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());
169     }
170
171     @Test
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());
176     }
177
178     @Test
179     public void testClose() {
180         connectionBuilder.close();
181     }
182
183     @Test
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");
188
189         assertEquals(611, result.getStatusCode());
190         assertNull(result.getStatusMessage());
191         assertEquals("UNKNOWN", result.getResults());
192     }
193
194     @Test (expected = FileNotFoundException.class)
195     public void testGetModeNoCert() throws KeyStoreException, CertificateException, IOException,
196             KeyManagementException, NoSuchAlgorithmException {
197         String certFile = "testCert";
198
199         connectionBuilder = new ConnectionBuilder(certFile, 2000);
200         connectionBuilder.setHttpContext(USER, PSWD);
201         AnsibleResult result = connectionBuilder.get(URL);
202
203         assertEquals(611, result.getStatusCode());
204         assertNull(result.getStatusMessage());
205         assertEquals("UNKNOWN", result.getResults());
206     }
207
208     @Test
209     public void testGetModeCert() throws KeyStoreException, CertificateException, IOException,
210             KeyManagementException, NoSuchAlgorithmException {
211         String certFile = "src/test/resources/cert";
212
213         connectionBuilder = new ConnectionBuilder(certFile, 2000);
214         connectionBuilder.setHttpContext(USER, PSWD);
215         AnsibleResult result = connectionBuilder.get("test.server.com");
216
217         assertEquals(611, result.getStatusCode());
218         assertNull(result.getStatusMessage());
219         assertEquals("UNKNOWN", result.getResults());
220     }
221
222     @Test (expected = IOException.class)
223     public void testGetModeStore() throws KeyStoreException, CertificateException, IOException,
224             KeyManagementException, NoSuchAlgorithmException {
225         String store = "src/test/resources/cert";
226
227         connectionBuilder = new ConnectionBuilder(store, new char['t'], 2000, "1.1.1.1" );
228         connectionBuilder.setHttpContext(USER, PSWD);
229         AnsibleResult result = connectionBuilder.get(URL);
230
231         assertEquals(611, result.getStatusCode());
232         assertNull(result.getStatusMessage());
233         assertEquals("UNKNOWN", result.getResults());
234     }
235
236 }