2 * ============LICENSE_START=======================================================
\r
4 * ================================================================================
\r
5 * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
\r
6 * Copyright © 2017 Amdocs
\r
7 * ================================================================================
\r
8 * Licensed under the Apache License, Version 2.0 (the "License");
\r
9 * you may not use this file except in compliance with the License.
\r
10 * You may obtain a copy of the License at
\r
12 * http://www.apache.org/licenses/LICENSE-2.0
\r
14 * Unless required by applicable law or agreed to in writing, software
\r
15 * distributed under the License is distributed on an "AS IS" BASIS,
\r
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
17 * See the License for the specific language governing permissions and
\r
18 * limitations under the License.
\r
19 * ============LICENSE_END=========================================================
\r
21 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
\r
23 package org.onap.aai.restclient.rest;
\r
25 import static org.junit.Assert.assertEquals;
\r
26 import static org.junit.Assert.assertNotNull;
\r
27 import static org.junit.Assert.assertNull;
\r
28 import static org.junit.Assert.assertTrue;
\r
29 import static org.junit.Assert.fail;
\r
31 import org.junit.Before;
\r
32 import org.junit.Test;
\r
33 import org.onap.aai.restclient.enums.RestAuthenticationMode;
\r
34 import org.onap.aai.restclient.rest.RestClientBuilder;
\r
36 import com.sun.jersey.api.client.Client;
\r
37 import com.sun.jersey.client.urlconnection.HTTPSProperties;
\r
40 * This suite of tests is intended to exercise the functionality of the generice REST client
\r
43 public class RestClientBuilderTest {
\r
46 * Test case initialization
\r
48 * @throws Exception the exception
\r
51 public void init() throws Exception {
\r
54 private String generateAuthorizationHeaderValue(String username, String password) {
\r
55 String usernameAndPassword = username + ":" + password;
\r
56 return "Basic " + java.util.Base64.getEncoder().encodeToString(usernameAndPassword.getBytes());
\r
60 public void validateAccesors() {
\r
62 RestClientBuilder restClientBuilder = new RestClientBuilder();
\r
65 assertEquals(restClientBuilder.isValidateServerHostname(), RestClientBuilder.DEFAULT_VALIDATE_SERVER_HOST);
\r
66 assertEquals(restClientBuilder.isValidateServerCertChain(), RestClientBuilder.DEFAULT_VALIDATE_CERT_CHAIN);
\r
67 assertEquals(restClientBuilder.getClientCertFileName(), RestClientBuilder.DEFAULT_CLIENT_CERT_FILENAME);
\r
68 assertEquals(restClientBuilder.getClientCertPassword(), RestClientBuilder.DEFAULT_CERT_PASSWORD);
\r
69 assertEquals(restClientBuilder.getTruststoreFilename(), RestClientBuilder.DEFAULT_TRUST_STORE_FILENAME);
\r
70 assertEquals(restClientBuilder.getConnectTimeoutInMs(), RestClientBuilder.DEFAULT_CONNECT_TIMEOUT_MS);
\r
71 assertEquals(restClientBuilder.getReadTimeoutInMs(), RestClientBuilder.DEFAULT_READ_TIMEOUT_MS);
\r
72 assertEquals(restClientBuilder.getAuthenticationMode(), RestClientBuilder.DEFAULT_AUTH_MODE);
\r
73 assertEquals(restClientBuilder.getBasicAuthUsername(), RestClientBuilder.DEFAULT_BASIC_AUTH_USERNAME);
\r
74 assertEquals(restClientBuilder.getBasicAuthPassword(), RestClientBuilder.DEFAULT_BASIC_AUTH_PASSWORD);
\r
76 restClientBuilder.setAuthenticationMode(RestAuthenticationMode.UNKNOWN_MODE);
\r
77 restClientBuilder.setBasicAuthPassword("password");
\r
78 restClientBuilder.setBasicAuthUsername("username");
\r
79 restClientBuilder.setClientCertFileName("filename");
\r
80 restClientBuilder.setClientCertPassword("password");
\r
81 restClientBuilder.setConnectTimeoutInMs(12345);
\r
82 restClientBuilder.setReadTimeoutInMs(54321);
\r
83 restClientBuilder.setTruststoreFilename("truststore");
\r
84 restClientBuilder.setValidateServerCertChain(true);
\r
85 restClientBuilder.setValidateServerHostname(true);
\r
87 assertEquals(restClientBuilder.isValidateServerHostname(), true);
\r
88 assertEquals(restClientBuilder.isValidateServerCertChain(), true);
\r
89 assertEquals(restClientBuilder.getClientCertFileName(), "filename");
\r
90 assertEquals(restClientBuilder.getClientCertPassword(), "password");
\r
91 assertEquals(restClientBuilder.getTruststoreFilename(), "truststore");
\r
92 assertEquals(restClientBuilder.getConnectTimeoutInMs(), 12345);
\r
93 assertEquals(restClientBuilder.getReadTimeoutInMs(), 54321);
\r
94 assertEquals(restClientBuilder.getAuthenticationMode(), RestAuthenticationMode.UNKNOWN_MODE);
\r
95 assertEquals(restClientBuilder.getBasicAuthUsername(), "username");
\r
96 assertEquals(restClientBuilder.getBasicAuthPassword(), "password");
\r
98 assertEquals(restClientBuilder.getBasicAuthenticationCredentials(),
\r
99 generateAuthorizationHeaderValue("username", "password"));
\r
101 assertTrue(restClientBuilder.toString().contains("RestClientBuilder"));
\r
106 public void validateNoAuthClientCreation() throws Exception {
\r
108 RestClientBuilder restClientBuilder = new RestClientBuilder();
\r
110 restClientBuilder.setAuthenticationMode(RestAuthenticationMode.HTTP_NOAUTH);
\r
111 restClientBuilder.setConnectTimeoutInMs(12345);
\r
112 restClientBuilder.setReadTimeoutInMs(54321);
\r
114 Client client = restClientBuilder.getClient();
\r
115 assertNotNull(client);
\r
116 assertNull(client.getProperties().get(HTTPSProperties.PROPERTY_HTTPS_PROPERTIES));
\r
121 public void validateUnknownModeCreateNoAuthClient() throws Exception {
\r
123 RestClientBuilder restClientBuilder = new RestClientBuilder();
\r
125 restClientBuilder.setAuthenticationMode(RestAuthenticationMode.UNKNOWN_MODE);
\r
126 restClientBuilder.setConnectTimeoutInMs(12345);
\r
127 restClientBuilder.setReadTimeoutInMs(54321);
\r
129 Client client = restClientBuilder.getClient();
\r
130 assertNotNull(client);
\r
131 assertNull(client.getProperties().get(HTTPSProperties.PROPERTY_HTTPS_PROPERTIES));
\r
135 public void validateBasicAuthSslClient() throws Exception {
\r
137 RestClientBuilder restClientBuilder = new RestClientBuilder();
\r
139 restClientBuilder.setAuthenticationMode(RestAuthenticationMode.SSL_BASIC);
\r
140 restClientBuilder.setConnectTimeoutInMs(12345);
\r
141 restClientBuilder.setReadTimeoutInMs(54321);
\r
142 restClientBuilder.setBasicAuthUsername("username");
\r
143 restClientBuilder.setBasicAuthPassword("password");
\r
145 Client client = restClientBuilder.getClient();
\r
147 Object sslPropertiesObj = client.getProperties().get(HTTPSProperties.PROPERTY_HTTPS_PROPERTIES);
\r
148 HTTPSProperties sslProps = null;
\r
149 if ( sslPropertiesObj instanceof HTTPSProperties ) {
\r
150 sslProps = (HTTPSProperties)sslPropertiesObj;
\r
151 assertNotNull(sslProps.getHostnameVerifier());
\r
153 fail("Unexpected value for https properties object");
\r
159 public void validateSslCertClient_noHostOrCertChainValidation() throws Exception {
\r
161 RestClientBuilder restClientBuilder = new RestClientBuilder();
\r
163 restClientBuilder.setAuthenticationMode(RestAuthenticationMode.SSL_CERT);
\r
164 restClientBuilder.setConnectTimeoutInMs(12345);
\r
165 restClientBuilder.setReadTimeoutInMs(54321);
\r
166 restClientBuilder.setValidateServerCertChain(false);
\r
167 restClientBuilder.setValidateServerHostname(false);
\r
169 Client client = restClientBuilder.getClient();
\r
171 Object sslPropertiesObj = client.getProperties().get(HTTPSProperties.PROPERTY_HTTPS_PROPERTIES);
\r
172 HTTPSProperties sslProps = null;
\r
173 if ( sslPropertiesObj instanceof HTTPSProperties ) {
\r
174 sslProps = (HTTPSProperties)sslPropertiesObj;
\r
175 assertNotNull(sslProps.getHostnameVerifier());
\r
177 fail("Unexpected value for https properties object");
\r
181 public void validateSslCertClient_hostOnlyValidation() throws Exception {
\r
183 RestClientBuilder restClientBuilder = new RestClientBuilder();
\r
185 restClientBuilder.setAuthenticationMode(RestAuthenticationMode.SSL_CERT);
\r
186 restClientBuilder.setConnectTimeoutInMs(12345);
\r
187 restClientBuilder.setReadTimeoutInMs(54321);
\r
188 restClientBuilder.setValidateServerCertChain(false);
\r
189 restClientBuilder.setValidateServerHostname(true);
\r
191 Client client = restClientBuilder.getClient();
\r
193 Object sslPropertiesObj = client.getProperties().get(HTTPSProperties.PROPERTY_HTTPS_PROPERTIES);
\r
194 HTTPSProperties sslProps = null;
\r
195 if ( sslPropertiesObj instanceof HTTPSProperties ) {
\r
196 sslProps = (HTTPSProperties)sslPropertiesObj;
\r
197 assertNull(sslProps.getHostnameVerifier());
\r
199 fail("Unexpected value for https properties object");
\r
204 public void validateSslCertClient_certChainOnlyValidation() throws Exception {
\r
206 RestClientBuilder restClientBuilder = new RestClientBuilder();
\r
208 restClientBuilder.setAuthenticationMode(RestAuthenticationMode.SSL_CERT);
\r
209 restClientBuilder.setConnectTimeoutInMs(12345);
\r
210 restClientBuilder.setReadTimeoutInMs(54321);
\r
211 restClientBuilder.setValidateServerCertChain(true);
\r
212 restClientBuilder.setValidateServerHostname(false);
\r
213 restClientBuilder.setTruststoreFilename("truststore");
\r
214 restClientBuilder.setClientCertPassword(null);
\r
216 Client client = restClientBuilder.getClient();
\r
218 Object sslPropertiesObj = client.getProperties().get(HTTPSProperties.PROPERTY_HTTPS_PROPERTIES);
\r
219 HTTPSProperties sslProps = null;
\r
220 if ( sslPropertiesObj instanceof HTTPSProperties ) {
\r
221 sslProps = (HTTPSProperties)sslPropertiesObj;
\r
222 assertNotNull(sslProps.getHostnameVerifier());
\r
224 fail("Unexpected value for https properties object");
\r
229 public void validateSslCertClient_withHostAndCertChainValidation() throws Exception {
\r
231 RestClientBuilder restClientBuilder = new RestClientBuilder();
\r
233 restClientBuilder.setAuthenticationMode(RestAuthenticationMode.SSL_CERT);
\r
234 restClientBuilder.setConnectTimeoutInMs(12345);
\r
235 restClientBuilder.setReadTimeoutInMs(54321);
\r
236 restClientBuilder.setValidateServerCertChain(true);
\r
237 restClientBuilder.setValidateServerHostname(true);
\r
238 restClientBuilder.setClientCertPassword("password");
\r
239 restClientBuilder.setTruststoreFilename("truststore");
\r
241 Client client = restClientBuilder.getClient();
\r
243 Object sslPropertiesObj = client.getProperties().get(HTTPSProperties.PROPERTY_HTTPS_PROPERTIES);
\r
244 HTTPSProperties sslProps = null;
\r
245 if ( sslPropertiesObj instanceof HTTPSProperties ) {
\r
246 sslProps = (HTTPSProperties)sslPropertiesObj;
\r
247 assertNull(sslProps.getHostnameVerifier());
\r
249 fail("Unexpected value for https properties object");
\r
252 @Test (expected=IllegalArgumentException.class)
\r
253 public void validateSslCertClient_illegalArgumentExceptionWhenTruststoreIsNull() throws Exception {
\r
255 RestClientBuilder restClientBuilder = new RestClientBuilder();
\r
257 restClientBuilder.setAuthenticationMode(RestAuthenticationMode.SSL_CERT);
\r
258 restClientBuilder.setConnectTimeoutInMs(12345);
\r
259 restClientBuilder.setReadTimeoutInMs(54321);
\r
260 restClientBuilder.setValidateServerCertChain(true);
\r
261 restClientBuilder.setValidateServerHostname(true);
\r
262 restClientBuilder.setTruststoreFilename(null);
\r
265 * Creating the client in this scenario will cause an IllegalArgumentException caused by the
\r
266 * truststore being null
\r
268 Client client = restClientBuilder.getClient();
\r