5e7d8c178e26987428f6c2ef8d0a12e753ac77d2
[aai/rest-client.git] / src / test / java / org / onap / aai / restclient / rest / RestClientBuilderTest.java
1 /**\r
2  * ============LICENSE_START=======================================================\r
3  * org.onap.aai\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
11  *\r
12  *       http://www.apache.org/licenses/LICENSE-2.0\r
13  *\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
20  *\r
21  * ECOMP is a trademark and service mark of AT&T Intellectual Property.\r
22  */\r
23 package org.onap.aai.restclient.rest;\r
24 \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
30 \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
35 \r
36 import com.sun.jersey.api.client.Client;\r
37 import com.sun.jersey.client.urlconnection.HTTPSProperties;\r
38 \r
39 /**\r
40  * This suite of tests is intended to exercise the functionality of the generice REST client\r
41  * builder.\r
42  */\r
43 public class RestClientBuilderTest {\r
44 \r
45   /**\r
46    * Test case initialization\r
47    * \r
48    * @throws Exception the exception\r
49    */\r
50   @Before\r
51   public void init() throws Exception {\r
52   }\r
53   \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
57   }\r
58   \r
59   @Test\r
60   public void validateAccesors() {\r
61     \r
62     RestClientBuilder restClientBuilder = new RestClientBuilder();\r
63     \r
64     // test defaults\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
75     \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
86     \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
97     \r
98     assertEquals(restClientBuilder.getBasicAuthenticationCredentials(),\r
99         generateAuthorizationHeaderValue("username", "password"));\r
100 \r
101     assertTrue(restClientBuilder.toString().contains("RestClientBuilder"));\r
102 \r
103   }\r
104   \r
105   @Test\r
106   public void validateNoAuthClientCreation() throws Exception {\r
107     \r
108     RestClientBuilder restClientBuilder = new RestClientBuilder();\r
109     \r
110     restClientBuilder.setAuthenticationMode(RestAuthenticationMode.HTTP_NOAUTH);\r
111     restClientBuilder.setConnectTimeoutInMs(12345);\r
112     restClientBuilder.setReadTimeoutInMs(54321);\r
113     \r
114     Client client = restClientBuilder.getClient();\r
115     assertNotNull(client);\r
116     assertNull(client.getProperties().get(HTTPSProperties.PROPERTY_HTTPS_PROPERTIES));\r
117   }\r
118   \r
119   \r
120   @Test\r
121   public void validateUnknownModeCreateNoAuthClient() throws Exception {\r
122     \r
123     RestClientBuilder restClientBuilder = new RestClientBuilder();\r
124     \r
125     restClientBuilder.setAuthenticationMode(RestAuthenticationMode.UNKNOWN_MODE);\r
126     restClientBuilder.setConnectTimeoutInMs(12345);\r
127     restClientBuilder.setReadTimeoutInMs(54321);\r
128     \r
129     Client client = restClientBuilder.getClient();\r
130     assertNotNull(client);\r
131     assertNull(client.getProperties().get(HTTPSProperties.PROPERTY_HTTPS_PROPERTIES));\r
132   }\r
133 \r
134   @Test\r
135   public void validateBasicAuthSslClient() throws Exception {\r
136     \r
137     RestClientBuilder restClientBuilder = new RestClientBuilder();\r
138     \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
144     \r
145     Client client = restClientBuilder.getClient();\r
146    \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
152     } else {\r
153       fail("Unexpected value for https properties object");\r
154     }\r
155     \r
156   }\r
157 \r
158   @Test\r
159   public void validateSslCertClient_noHostOrCertChainValidation() throws Exception {\r
160     \r
161     RestClientBuilder restClientBuilder = new RestClientBuilder();\r
162     \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
168     \r
169     Client client = restClientBuilder.getClient();\r
170    \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
176     } else {\r
177       fail("Unexpected value for https properties object");\r
178     }  }\r
179   \r
180   @Test\r
181   public void validateSslCertClient_hostOnlyValidation() throws Exception {\r
182     \r
183     RestClientBuilder restClientBuilder = new RestClientBuilder();\r
184     \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
190     \r
191     Client client = restClientBuilder.getClient();\r
192    \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
198     } else {\r
199       fail("Unexpected value for https properties object");\r
200     }\r
201    }\r
202   \r
203   @Test\r
204   public void validateSslCertClient_certChainOnlyValidation() throws Exception {\r
205     \r
206     RestClientBuilder restClientBuilder = new RestClientBuilder();\r
207     \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
215     \r
216     Client client = restClientBuilder.getClient();\r
217    \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
223     } else {\r
224       fail("Unexpected value for https properties object");\r
225     }\r
226   }\r
227   \r
228   @Test\r
229   public void validateSslCertClient_withHostAndCertChainValidation() throws Exception {\r
230     \r
231     RestClientBuilder restClientBuilder = new RestClientBuilder();\r
232     \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
240     \r
241     Client client = restClientBuilder.getClient();\r
242    \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
248     } else {\r
249       fail("Unexpected value for https properties object");\r
250     }  }\r
251   \r
252   @Test (expected=IllegalArgumentException.class)\r
253   public void validateSslCertClient_illegalArgumentExceptionWhenTruststoreIsNull() throws Exception {\r
254     \r
255     RestClientBuilder restClientBuilder = new RestClientBuilder();\r
256     \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
263     \r
264     /*\r
265      * Creating the client in this scenario will cause an IllegalArgumentException caused by the\r
266      * truststore being null\r
267      */\r
268     Client client = restClientBuilder.getClient();\r
269    \r
270   }\r
271   \r
272   @Test\r
273   public void validateSslProtocolConfiguration() throws Exception {\r
274     \r
275     RestClientBuilder restClientBuilder = new RestClientBuilder();\r
276     assertEquals(RestClientBuilder.DEFAULT_SSL_PROTOCOL, restClientBuilder.getSslProtocol());\r
277     \r
278     restClientBuilder.setSslProtocol("TLSv1.2");\r
279     assertEquals("TLSv1.2", restClientBuilder.getSslProtocol());\r
280     \r
281   }\r
282     \r
283 }\r