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
30 import javax.ws.rs.client.Client;
\r
31 import org.junit.Before;
\r
32 import org.junit.Test;
\r
33 import org.onap.aai.restclient.enums.RestAuthenticationMode;
\r
36 * This suite of tests is intended to exercise the functionality of the generice REST client builder.
\r
38 public class RestClientBuilderTest {
\r
41 * Test case initialization
\r
43 * @throws Exception the exception
\r
46 public void init() throws Exception {}
\r
48 private String generateAuthorizationHeaderValue(String username, String password) {
\r
49 String usernameAndPassword = username + ":" + password;
\r
50 return "Basic " + java.util.Base64.getEncoder().encodeToString(usernameAndPassword.getBytes());
\r
54 public void validateAccesors() {
\r
56 RestClientBuilder restClientBuilder = new RestClientBuilder();
\r
59 assertEquals(restClientBuilder.isValidateServerHostname(), RestClientBuilder.DEFAULT_VALIDATE_SERVER_HOST);
\r
60 assertEquals(restClientBuilder.isValidateServerCertChain(), RestClientBuilder.DEFAULT_VALIDATE_CERT_CHAIN);
\r
61 assertEquals(restClientBuilder.getClientCertFileName(), RestClientBuilder.DEFAULT_CLIENT_CERT_FILENAME);
\r
62 assertEquals(restClientBuilder.getClientCertPassword(), RestClientBuilder.DEFAULT_CERT_PASSWORD);
\r
63 assertEquals(restClientBuilder.getTruststoreFilename(), RestClientBuilder.DEFAULT_TRUST_STORE_FILENAME);
\r
64 assertEquals(restClientBuilder.getConnectTimeoutInMs(), RestClientBuilder.DEFAULT_CONNECT_TIMEOUT_MS);
\r
65 assertEquals(restClientBuilder.getReadTimeoutInMs(), RestClientBuilder.DEFAULT_READ_TIMEOUT_MS);
\r
66 assertEquals(restClientBuilder.getAuthenticationMode(), RestClientBuilder.DEFAULT_AUTH_MODE);
\r
67 assertEquals(restClientBuilder.getBasicAuthUsername(), RestClientBuilder.DEFAULT_BASIC_AUTH_USERNAME);
\r
68 assertEquals(restClientBuilder.getBasicAuthPassword(), RestClientBuilder.DEFAULT_BASIC_AUTH_PASSWORD);
\r
70 restClientBuilder.setAuthenticationMode(RestAuthenticationMode.UNKNOWN_MODE);
\r
71 restClientBuilder.setBasicAuthPassword("password");
\r
72 restClientBuilder.setBasicAuthUsername("username");
\r
73 restClientBuilder.setClientCertFileName("filename");
\r
74 restClientBuilder.setClientCertPassword("password");
\r
75 restClientBuilder.setConnectTimeoutInMs(12345);
\r
76 restClientBuilder.setReadTimeoutInMs(54321);
\r
77 restClientBuilder.setTruststoreFilename("truststore");
\r
78 restClientBuilder.setValidateServerCertChain(true);
\r
79 restClientBuilder.setValidateServerHostname(true);
\r
81 assertEquals(restClientBuilder.isValidateServerHostname(), true);
\r
82 assertEquals(restClientBuilder.isValidateServerCertChain(), true);
\r
83 assertEquals(restClientBuilder.getClientCertFileName(), "filename");
\r
84 assertEquals(restClientBuilder.getClientCertPassword(), "password");
\r
85 assertEquals(restClientBuilder.getTruststoreFilename(), "truststore");
\r
86 assertEquals(restClientBuilder.getConnectTimeoutInMs(), 12345);
\r
87 assertEquals(restClientBuilder.getReadTimeoutInMs(), 54321);
\r
88 assertEquals(restClientBuilder.getAuthenticationMode(), RestAuthenticationMode.UNKNOWN_MODE);
\r
89 assertEquals(restClientBuilder.getBasicAuthUsername(), "username");
\r
90 assertEquals(restClientBuilder.getBasicAuthPassword(), "password");
\r
92 assertEquals(restClientBuilder.getBasicAuthenticationCredentials(),
\r
93 generateAuthorizationHeaderValue("username", "password"));
\r
95 assertTrue(restClientBuilder.toString().contains("RestClientBuilder"));
\r
100 public void validateNoAuthClientCreation() throws Exception {
\r
102 RestClientBuilder restClientBuilder = new RestClientBuilder();
\r
104 restClientBuilder.setAuthenticationMode(RestAuthenticationMode.HTTP_NOAUTH);
\r
105 restClientBuilder.setConnectTimeoutInMs(12345);
\r
106 restClientBuilder.setReadTimeoutInMs(54321);
\r
108 Client client = restClientBuilder.getClient();
\r
109 assertNotNull(client);
\r
114 public void validateUnknownModeCreateNoAuthClient() throws Exception {
\r
116 RestClientBuilder restClientBuilder = new RestClientBuilder();
\r
118 restClientBuilder.setAuthenticationMode(RestAuthenticationMode.UNKNOWN_MODE);
\r
119 restClientBuilder.setConnectTimeoutInMs(12345);
\r
120 restClientBuilder.setReadTimeoutInMs(54321);
\r
122 Client client = restClientBuilder.getClient();
\r
123 assertNotNull(client);
\r
127 public void validateBasicAuthSslClient() throws Exception {
\r
129 RestClientBuilder restClientBuilder = new RestClientBuilder();
\r
131 restClientBuilder.setAuthenticationMode(RestAuthenticationMode.SSL_BASIC);
\r
132 restClientBuilder.setConnectTimeoutInMs(12345);
\r
133 restClientBuilder.setReadTimeoutInMs(54321);
\r
134 restClientBuilder.setBasicAuthUsername("username");
\r
135 restClientBuilder.setBasicAuthPassword("password");
\r
136 restClientBuilder.setTruststoreFilename("truststore");
\r
138 Client client = restClientBuilder.getClient();
\r
139 assertNotNull(client.getHostnameVerifier());
\r
140 assertEquals("truststore", System.getProperty("javax.net.ssl.trustStore"));
\r
143 @Test(expected = IllegalArgumentException.class)
\r
144 public void validateSslCertClient_noHostOrCertChainValidation() throws Exception {
\r
146 RestClientBuilder restClientBuilder = new RestClientBuilder();
\r
148 restClientBuilder.setAuthenticationMode(RestAuthenticationMode.SSL_CERT);
\r
149 restClientBuilder.setConnectTimeoutInMs(12345);
\r
150 restClientBuilder.setReadTimeoutInMs(54321);
\r
151 restClientBuilder.setValidateServerCertChain(false);
\r
152 restClientBuilder.setValidateServerHostname(false);
\r
154 restClientBuilder.getClient();
\r
157 @Test(expected = IllegalArgumentException.class)
\r
158 public void validateSslCertClient_hostOnlyValidation() throws Exception {
\r
160 RestClientBuilder restClientBuilder = new RestClientBuilder();
\r
162 restClientBuilder.setAuthenticationMode(RestAuthenticationMode.SSL_CERT);
\r
163 restClientBuilder.setConnectTimeoutInMs(12345);
\r
164 restClientBuilder.setReadTimeoutInMs(54321);
\r
165 restClientBuilder.setValidateServerCertChain(false);
\r
166 restClientBuilder.setValidateServerHostname(true);
\r
168 restClientBuilder.getClient();
\r
173 public void validateSslCertClient_certChainOnlyValidation() throws Exception {
\r
175 RestClientBuilder restClientBuilder = new RestClientBuilder();
\r
177 restClientBuilder.setAuthenticationMode(RestAuthenticationMode.SSL_CERT);
\r
178 restClientBuilder.setConnectTimeoutInMs(12345);
\r
179 restClientBuilder.setReadTimeoutInMs(54321);
\r
180 restClientBuilder.setValidateServerCertChain(true);
\r
181 restClientBuilder.setValidateServerHostname(false);
\r
182 restClientBuilder.setTruststoreFilename("truststore");
\r
183 restClientBuilder.setClientCertPassword(null);
\r
185 Client client = restClientBuilder.getClient();
\r
187 assertNotNull(client.getHostnameVerifier());
\r
188 assertEquals("truststore", System.getProperty("javax.net.ssl.trustStore"));
\r
192 public void validateSslCertClient_withHostAndCertChainValidation() throws Exception {
\r
194 RestClientBuilder restClientBuilder = new RestClientBuilder();
\r
196 restClientBuilder.setAuthenticationMode(RestAuthenticationMode.SSL_CERT);
\r
197 restClientBuilder.setConnectTimeoutInMs(12345);
\r
198 restClientBuilder.setReadTimeoutInMs(54321);
\r
199 restClientBuilder.setValidateServerCertChain(true);
\r
200 restClientBuilder.setValidateServerHostname(true);
\r
201 restClientBuilder.setClientCertPassword("password");
\r
202 restClientBuilder.setTruststoreFilename("truststore");
\r
204 Client client = restClientBuilder.getClient();
\r
206 assertNull(client.getHostnameVerifier());
\r
207 assertEquals("truststore", System.getProperty("javax.net.ssl.trustStore"));
\r
210 @Test(expected = IllegalArgumentException.class)
\r
211 public void validateSslCertClient_illegalArgumentExceptionWhenTruststoreIsNull() throws Exception {
\r
213 RestClientBuilder restClientBuilder = new RestClientBuilder();
\r
215 restClientBuilder.setAuthenticationMode(RestAuthenticationMode.SSL_CERT);
\r
216 restClientBuilder.setConnectTimeoutInMs(12345);
\r
217 restClientBuilder.setReadTimeoutInMs(54321);
\r
218 restClientBuilder.setValidateServerCertChain(true);
\r
219 restClientBuilder.setValidateServerHostname(true);
\r
220 restClientBuilder.setTruststoreFilename(null);
\r
223 * Creating the client in this scenario will cause an IllegalArgumentException caused by the truststore being
\r
226 restClientBuilder.getClient();
\r
231 public void validateSslProtocolConfiguration() throws Exception {
\r
233 RestClientBuilder restClientBuilder = new RestClientBuilder();
\r
234 assertEquals(RestClientBuilder.DEFAULT_SSL_PROTOCOL, restClientBuilder.getSslProtocol());
\r
236 restClientBuilder.setSslProtocol("TLSv1.2");
\r
237 assertEquals("TLSv1.2", restClientBuilder.getSslProtocol());
\r