aboutsummaryrefslogtreecommitdiffstats
path: root/src/test/java/org/onap/aai/restclient/rest/RestClientBuilderTest.java
blob: 7155f9a3804413253ffdbae6838be2e37038f918 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/**
 * ============LICENSE_START=======================================================
 * org.onap.aai
 * ================================================================================
 * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
 * Copyright © 2017 Amdocs
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 *
 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
 */
package org.onap.aai.restclient.rest;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import org.junit.Before;
import org.junit.Test;
import org.onap.aai.restclient.enums.RestAuthenticationMode;
import org.onap.aai.restclient.rest.RestClientBuilder;

import com.sun.jersey.api.client.Client;
import com.sun.jersey.client.urlconnection.HTTPSProperties;

/**
 * This suite of tests is intended to exercise the functionality of the generice REST client
 * builder.
 */
public class RestClientBuilderTest {

  /**
   * Test case initialization
   * 
   * @throws Exception the exception
   */
  @Before
  public void init() throws Exception {
  }
  
  private String generateAuthorizationHeaderValue(String username, String password) {
    String usernameAndPassword = username + ":" + password;
    return "Basic " + java.util.Base64.getEncoder().encodeToString(usernameAndPassword.getBytes());
  }
  
  @Test
  public void validateAccesors() {
    
    RestClientBuilder restClientBuilder = new RestClientBuilder();
    
    // test defaults
    assertEquals(restClientBuilder.isValidateServerHostname(), RestClientBuilder.DEFAULT_VALIDATE_SERVER_HOST);
    assertEquals(restClientBuilder.isValidateServerCertChain(), RestClientBuilder.DEFAULT_VALIDATE_CERT_CHAIN);
    assertEquals(restClientBuilder.getClientCertFileName(), RestClientBuilder.DEFAULT_CLIENT_CERT_FILENAME);
    assertEquals(restClientBuilder.getClientCertPassword(), RestClientBuilder.DEFAULT_CERT_PASSWORD);
    assertEquals(restClientBuilder.getTruststoreFilename(), RestClientBuilder.DEFAULT_TRUST_STORE_FILENAME);
    assertEquals(restClientBuilder.getConnectTimeoutInMs(), RestClientBuilder.DEFAULT_CONNECT_TIMEOUT_MS);
    assertEquals(restClientBuilder.getReadTimeoutInMs(), RestClientBuilder.DEFAULT_READ_TIMEOUT_MS);
    assertEquals(restClientBuilder.getAuthenticationMode(), RestClientBuilder.DEFAULT_AUTH_MODE);
    assertEquals(restClientBuilder.getBasicAuthUsername(), RestClientBuilder.DEFAULT_BASIC_AUTH_USERNAME);
    assertEquals(restClientBuilder.getBasicAuthPassword(), RestClientBuilder.DEFAULT_BASIC_AUTH_PASSWORD);
    
    restClientBuilder.setAuthenticationMode(RestAuthenticationMode.UNKNOWN_MODE);
    restClientBuilder.setBasicAuthPassword("password");
    restClientBuilder.setBasicAuthUsername("username");
    restClientBuilder.setClientCertFileName("filename");
    restClientBuilder.setClientCertPassword("password");
    restClientBuilder.setConnectTimeoutInMs(12345);
    restClientBuilder.setReadTimeoutInMs(54321);
    restClientBuilder.setTruststoreFilename("truststore");
    restClientBuilder.setValidateServerCertChain(true);
    restClientBuilder.setValidateServerHostname(true);
    
    assertEquals(restClientBuilder.isValidateServerHostname(), true);
    assertEquals(restClientBuilder.isValidateServerCertChain(), true);
    assertEquals(restClientBuilder.getClientCertFileName(), "filename");
    assertEquals(restClientBuilder.getClientCertPassword(), "password");
    assertEquals(restClientBuilder.getTruststoreFilename(), "truststore");
    assertEquals(restClientBuilder.getConnectTimeoutInMs(), 12345);
    assertEquals(restClientBuilder.getReadTimeoutInMs(), 54321);
    assertEquals(restClientBuilder.getAuthenticationMode(), RestAuthenticationMode.UNKNOWN_MODE);
    assertEquals(restClientBuilder.getBasicAuthUsername(), "username");
    assertEquals(restClientBuilder.getBasicAuthPassword(), "password");
    
    assertEquals(restClientBuilder.getBasicAuthenticationCredentials(),
        generateAuthorizationHeaderValue("username", "password"));

    assertTrue(restClientBuilder.toString().contains("RestClientBuilder"));

  }
  
  @Test
  public void validateNoAuthClientCreation() throws Exception {
    
    RestClientBuilder restClientBuilder = new RestClientBuilder();
    
    restClientBuilder.setAuthenticationMode(RestAuthenticationMode.HTTP_NOAUTH);
    restClientBuilder.setConnectTimeoutInMs(12345);
    restClientBuilder.setReadTimeoutInMs(54321);
    
    Client client = restClientBuilder.getClient();
    assertNotNull(client);
    assertNull(client.getProperties().get(HTTPSProperties.PROPERTY_HTTPS_PROPERTIES));
  }
  
  
  @Test
  public void validateUnknownModeCreateNoAuthClient() throws Exception {
    
    RestClientBuilder restClientBuilder = new RestClientBuilder();
    
    restClientBuilder.setAuthenticationMode(RestAuthenticationMode.UNKNOWN_MODE);
    restClientBuilder.setConnectTimeoutInMs(12345);
    restClientBuilder.setReadTimeoutInMs(54321);
    
    Client client = restClientBuilder.getClient();
    assertNotNull(client);
    assertNull(client.getProperties().get(HTTPSProperties.PROPERTY_HTTPS_PROPERTIES));
  }

  @Test
  public void validateBasicAuthSslClient() throws Exception {
    
    RestClientBuilder restClientBuilder = new RestClientBuilder();
    
    restClientBuilder.setAuthenticationMode(RestAuthenticationMode.SSL_BASIC);
    restClientBuilder.setConnectTimeoutInMs(12345);
    restClientBuilder.setReadTimeoutInMs(54321);
    restClientBuilder.setBasicAuthUsername("username");
    restClientBuilder.setBasicAuthPassword("password");
    restClientBuilder.setTruststoreFilename("truststore");
    
    Client client = restClientBuilder.getClient();
   
    Object sslPropertiesObj = client.getProperties().get(HTTPSProperties.PROPERTY_HTTPS_PROPERTIES);
    HTTPSProperties sslProps = null;
    if ( sslPropertiesObj instanceof HTTPSProperties ) {
      sslProps = (HTTPSProperties)sslPropertiesObj;
      assertNotNull(sslProps.getHostnameVerifier());
    } else {
      fail("Unexpected value for https properties object");
    }
    
  }

  @Test (expected=IllegalArgumentException.class)
  public void validateSslCertClient_noHostOrCertChainValidation() throws Exception {
    
    RestClientBuilder restClientBuilder = new RestClientBuilder();
    
    restClientBuilder.setAuthenticationMode(RestAuthenticationMode.SSL_CERT);
    restClientBuilder.setConnectTimeoutInMs(12345);
    restClientBuilder.setReadTimeoutInMs(54321);
    restClientBuilder.setValidateServerCertChain(false);
    restClientBuilder.setValidateServerHostname(false);
    
    Client client = restClientBuilder.getClient(); 
  }
  
  @Test (expected=IllegalArgumentException.class)
  public void validateSslCertClient_hostOnlyValidation() throws Exception {
    
    RestClientBuilder restClientBuilder = new RestClientBuilder();
    
    restClientBuilder.setAuthenticationMode(RestAuthenticationMode.SSL_CERT);
    restClientBuilder.setConnectTimeoutInMs(12345);
    restClientBuilder.setReadTimeoutInMs(54321);
    restClientBuilder.setValidateServerCertChain(false);
    restClientBuilder.setValidateServerHostname(true);
    
    Client client = restClientBuilder.getClient();
   
  }
  
  @Test
  public void validateSslCertClient_certChainOnlyValidation() throws Exception {
    
    RestClientBuilder restClientBuilder = new RestClientBuilder();
    
    restClientBuilder.setAuthenticationMode(RestAuthenticationMode.SSL_CERT);
    restClientBuilder.setConnectTimeoutInMs(12345);
    restClientBuilder.setReadTimeoutInMs(54321);
    restClientBuilder.setValidateServerCertChain(true);
    restClientBuilder.setValidateServerHostname(false);
    restClientBuilder.setTruststoreFilename("truststore");
    restClientBuilder.setClientCertPassword(null);
    
    Client client = restClientBuilder.getClient();
   
    Object sslPropertiesObj = client.getProperties().get(HTTPSProperties.PROPERTY_HTTPS_PROPERTIES);
    HTTPSProperties sslProps = null;
    if ( sslPropertiesObj instanceof HTTPSProperties ) {
      sslProps = (HTTPSProperties)sslPropertiesObj;
      assertNotNull(sslProps.getHostnameVerifier());
    } else {
      fail("Unexpected value for https properties object");
    }
  }
  
  @Test
  public void validateSslCertClient_withHostAndCertChainValidation() throws Exception {
    
    RestClientBuilder restClientBuilder = new RestClientBuilder();
    
    restClientBuilder.setAuthenticationMode(RestAuthenticationMode.SSL_CERT);
    restClientBuilder.setConnectTimeoutInMs(12345);
    restClientBuilder.setReadTimeoutInMs(54321);
    restClientBuilder.setValidateServerCertChain(true);
    restClientBuilder.setValidateServerHostname(true);
    restClientBuilder.setClientCertPassword("password");
    restClientBuilder.setTruststoreFilename("truststore");
    
    Client client = restClientBuilder.getClient();
   
    Object sslPropertiesObj = client.getProperties().get(HTTPSProperties.PROPERTY_HTTPS_PROPERTIES);
    HTTPSProperties sslProps = null;
    if ( sslPropertiesObj instanceof HTTPSProperties ) {
      sslProps = (HTTPSProperties)sslPropertiesObj;
      assertNull(sslProps.getHostnameVerifier());
    } else {
      fail("Unexpected value for https properties object");
    }  }
  
  @Test (expected=IllegalArgumentException.class)
  public void validateSslCertClient_illegalArgumentExceptionWhenTruststoreIsNull() throws Exception {
    
    RestClientBuilder restClientBuilder = new RestClientBuilder();
    
    restClientBuilder.setAuthenticationMode(RestAuthenticationMode.SSL_CERT);
    restClientBuilder.setConnectTimeoutInMs(12345);
    restClientBuilder.setReadTimeoutInMs(54321);
    restClientBuilder.setValidateServerCertChain(true);
    restClientBuilder.setValidateServerHostname(true);
    restClientBuilder.setTruststoreFilename(null);
    
    /*
     * Creating the client in this scenario will cause an IllegalArgumentException caused by the
     * truststore being null
     */
    Client client = restClientBuilder.getClient();
   
  }
  
  @Test
  public void validateSslProtocolConfiguration() throws Exception {
    
    RestClientBuilder restClientBuilder = new RestClientBuilder();
    assertEquals(RestClientBuilder.DEFAULT_SSL_PROTOCOL, restClientBuilder.getSslProtocol());
    
    restClientBuilder.setSslProtocol("TLSv1.2");
    assertEquals("TLSv1.2", restClientBuilder.getSslProtocol());
    
  }
    
}