Add name to the top level pom for rest-client
[aai/rest-client.git] / src / test / java / org / openecomp / 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.openecomp.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.openecomp.restclient.enums.RestAuthenticationMode;\r
34 \r
35 import com.sun.jersey.api.client.Client;\r
36 import com.sun.jersey.client.urlconnection.HTTPSProperties;\r
37 \r
38 /**\r
39  * This suite of tests is intended to exercise the functionality of the generice REST client\r
40  * builder.\r
41  */\r
42 public class RestClientBuilderTest {\r
43 \r
44   /**\r
45    * Test case initialization\r
46    * \r
47    * @throws Exception the exception\r
48    */\r
49   @Before\r
50   public void init() throws Exception {\r
51   }\r
52   \r
53   private String generateAuthorizationHeaderValue(String username, String password) {\r
54     String usernameAndPassword = username + ":" + password;\r
55     return "Basic " + java.util.Base64.getEncoder().encodeToString(usernameAndPassword.getBytes());\r
56   }\r
57   \r
58   @Test\r
59   public void validateAccesors() {\r
60     \r
61     RestClientBuilder restClientBuilder = new RestClientBuilder();\r
62     \r
63     // test defaults\r
64     assertEquals(restClientBuilder.isValidateServerHostname(), RestClientBuilder.DEFAULT_VALIDATE_SERVER_HOST);\r
65     assertEquals(restClientBuilder.isValidateServerCertChain(), RestClientBuilder.DEFAULT_VALIDATE_CERT_CHAIN);\r
66     assertEquals(restClientBuilder.getClientCertFileName(), RestClientBuilder.DEFAULT_CLIENT_CERT_FILENAME);\r
67     assertEquals(restClientBuilder.getClientCertPassword(), RestClientBuilder.DEFAULT_CERT_PASSWORD);\r
68     assertEquals(restClientBuilder.getTruststoreFilename(), RestClientBuilder.DEFAULT_TRUST_STORE_FILENAME);\r
69     assertEquals(restClientBuilder.getConnectTimeoutInMs(), RestClientBuilder.DEFAULT_CONNECT_TIMEOUT_MS);\r
70     assertEquals(restClientBuilder.getReadTimeoutInMs(), RestClientBuilder.DEFAULT_READ_TIMEOUT_MS);\r
71     assertEquals(restClientBuilder.getAuthenticationMode(), RestClientBuilder.DEFAULT_AUTH_MODE);\r
72     assertEquals(restClientBuilder.getBasicAuthUsername(), RestClientBuilder.DEFAULT_BASIC_AUTH_USERNAME);\r
73     assertEquals(restClientBuilder.getBasicAuthPassword(), RestClientBuilder.DEFAULT_BASIC_AUTH_PASSWORD);\r
74     \r
75     restClientBuilder.setAuthenticationMode(RestAuthenticationMode.UNKNOWN_MODE);\r
76     restClientBuilder.setBasicAuthPassword("password");\r
77     restClientBuilder.setBasicAuthUsername("username");\r
78     restClientBuilder.setClientCertFileName("filename");\r
79     restClientBuilder.setClientCertPassword("password");\r
80     restClientBuilder.setConnectTimeoutInMs(12345);\r
81     restClientBuilder.setReadTimeoutInMs(54321);\r
82     restClientBuilder.setTruststoreFilename("truststore");\r
83     restClientBuilder.setValidateServerCertChain(true);\r
84     restClientBuilder.setValidateServerHostname(true);\r
85     \r
86     assertEquals(restClientBuilder.isValidateServerHostname(), true);\r
87     assertEquals(restClientBuilder.isValidateServerCertChain(), true);\r
88     assertEquals(restClientBuilder.getClientCertFileName(), "filename");\r
89     assertEquals(restClientBuilder.getClientCertPassword(), "password");\r
90     assertEquals(restClientBuilder.getTruststoreFilename(), "truststore");\r
91     assertEquals(restClientBuilder.getConnectTimeoutInMs(), 12345);\r
92     assertEquals(restClientBuilder.getReadTimeoutInMs(), 54321);\r
93     assertEquals(restClientBuilder.getAuthenticationMode(), RestAuthenticationMode.UNKNOWN_MODE);\r
94     assertEquals(restClientBuilder.getBasicAuthUsername(), "username");\r
95     assertEquals(restClientBuilder.getBasicAuthPassword(), "password");\r
96     \r
97     assertEquals(restClientBuilder.getBasicAuthenticationCredentials(),\r
98         generateAuthorizationHeaderValue("username", "password"));\r
99 \r
100     assertTrue(restClientBuilder.toString().contains("RestClientBuilder"));\r
101 \r
102   }\r
103   \r
104   @Test\r
105   public void validateNoAuthClientCreation() throws Exception {\r
106     \r
107     RestClientBuilder restClientBuilder = new RestClientBuilder();\r
108     \r
109     restClientBuilder.setAuthenticationMode(RestAuthenticationMode.HTTP_NOAUTH);\r
110     restClientBuilder.setConnectTimeoutInMs(12345);\r
111     restClientBuilder.setReadTimeoutInMs(54321);\r
112     \r
113     Client client = restClientBuilder.getClient();\r
114     assertNotNull(client);\r
115     assertNull(client.getProperties().get(HTTPSProperties.PROPERTY_HTTPS_PROPERTIES));\r
116   }\r
117   \r
118   \r
119   @Test\r
120   public void validateUnknownModeCreateNoAuthClient() throws Exception {\r
121     \r
122     RestClientBuilder restClientBuilder = new RestClientBuilder();\r
123     \r
124     restClientBuilder.setAuthenticationMode(RestAuthenticationMode.UNKNOWN_MODE);\r
125     restClientBuilder.setConnectTimeoutInMs(12345);\r
126     restClientBuilder.setReadTimeoutInMs(54321);\r
127     \r
128     Client client = restClientBuilder.getClient();\r
129     assertNotNull(client);\r
130     assertNull(client.getProperties().get(HTTPSProperties.PROPERTY_HTTPS_PROPERTIES));\r
131   }\r
132 \r
133   @Test\r
134   public void validateBasicAuthSslClient() throws Exception {\r
135     \r
136     RestClientBuilder restClientBuilder = new RestClientBuilder();\r
137     \r
138     restClientBuilder.setAuthenticationMode(RestAuthenticationMode.SSL_BASIC);\r
139     restClientBuilder.setConnectTimeoutInMs(12345);\r
140     restClientBuilder.setReadTimeoutInMs(54321);\r
141     restClientBuilder.setBasicAuthUsername("username");\r
142     restClientBuilder.setBasicAuthPassword("password");\r
143     \r
144     Client client = restClientBuilder.getClient();\r
145    \r
146     Object sslPropertiesObj = client.getProperties().get(HTTPSProperties.PROPERTY_HTTPS_PROPERTIES);\r
147     HTTPSProperties sslProps = null;\r
148     if ( sslPropertiesObj instanceof HTTPSProperties ) {\r
149       sslProps = (HTTPSProperties)sslPropertiesObj;\r
150       assertNotNull(sslProps.getHostnameVerifier());\r
151     } else {\r
152       fail("Unexpected value for https properties object");\r
153     }\r
154     \r
155   }\r
156 \r
157   @Test\r
158   public void validateSslCertClient_noHostOrCertChainValidation() throws Exception {\r
159     \r
160     RestClientBuilder restClientBuilder = new RestClientBuilder();\r
161     \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(false);\r
167     \r
168     Client client = restClientBuilder.getClient();\r
169    \r
170     Object sslPropertiesObj = client.getProperties().get(HTTPSProperties.PROPERTY_HTTPS_PROPERTIES);\r
171     HTTPSProperties sslProps = null;\r
172     if ( sslPropertiesObj instanceof HTTPSProperties ) {\r
173       sslProps = (HTTPSProperties)sslPropertiesObj;\r
174       assertNotNull(sslProps.getHostnameVerifier());\r
175     } else {\r
176       fail("Unexpected value for https properties object");\r
177     }  }\r
178   \r
179   @Test\r
180   public void validateSslCertClient_hostOnlyValidation() throws Exception {\r
181     \r
182     RestClientBuilder restClientBuilder = new RestClientBuilder();\r
183     \r
184     restClientBuilder.setAuthenticationMode(RestAuthenticationMode.SSL_CERT);\r
185     restClientBuilder.setConnectTimeoutInMs(12345);\r
186     restClientBuilder.setReadTimeoutInMs(54321);\r
187     restClientBuilder.setValidateServerCertChain(false);\r
188     restClientBuilder.setValidateServerHostname(true);\r
189     \r
190     Client client = restClientBuilder.getClient();\r
191    \r
192     Object sslPropertiesObj = client.getProperties().get(HTTPSProperties.PROPERTY_HTTPS_PROPERTIES);\r
193     HTTPSProperties sslProps = null;\r
194     if ( sslPropertiesObj instanceof HTTPSProperties ) {\r
195       sslProps = (HTTPSProperties)sslPropertiesObj;\r
196       assertNull(sslProps.getHostnameVerifier());\r
197     } else {\r
198       fail("Unexpected value for https properties object");\r
199     }\r
200    }\r
201   \r
202   @Test\r
203   public void validateSslCertClient_certChainOnlyValidation() throws Exception {\r
204     \r
205     RestClientBuilder restClientBuilder = new RestClientBuilder();\r
206     \r
207     restClientBuilder.setAuthenticationMode(RestAuthenticationMode.SSL_CERT);\r
208     restClientBuilder.setConnectTimeoutInMs(12345);\r
209     restClientBuilder.setReadTimeoutInMs(54321);\r
210     restClientBuilder.setValidateServerCertChain(true);\r
211     restClientBuilder.setValidateServerHostname(false);\r
212     restClientBuilder.setTruststoreFilename("truststore");\r
213     restClientBuilder.setClientCertPassword(null);\r
214     \r
215     Client client = restClientBuilder.getClient();\r
216    \r
217     Object sslPropertiesObj = client.getProperties().get(HTTPSProperties.PROPERTY_HTTPS_PROPERTIES);\r
218     HTTPSProperties sslProps = null;\r
219     if ( sslPropertiesObj instanceof HTTPSProperties ) {\r
220       sslProps = (HTTPSProperties)sslPropertiesObj;\r
221       assertNotNull(sslProps.getHostnameVerifier());\r
222     } else {\r
223       fail("Unexpected value for https properties object");\r
224     }\r
225   }\r
226   \r
227   @Test\r
228   public void validateSslCertClient_withHostAndCertChainValidation() throws Exception {\r
229     \r
230     RestClientBuilder restClientBuilder = new RestClientBuilder();\r
231     \r
232     restClientBuilder.setAuthenticationMode(RestAuthenticationMode.SSL_CERT);\r
233     restClientBuilder.setConnectTimeoutInMs(12345);\r
234     restClientBuilder.setReadTimeoutInMs(54321);\r
235     restClientBuilder.setValidateServerCertChain(true);\r
236     restClientBuilder.setValidateServerHostname(true);\r
237     restClientBuilder.setClientCertPassword("password");\r
238     restClientBuilder.setTruststoreFilename("truststore");\r
239     \r
240     Client client = restClientBuilder.getClient();\r
241    \r
242     Object sslPropertiesObj = client.getProperties().get(HTTPSProperties.PROPERTY_HTTPS_PROPERTIES);\r
243     HTTPSProperties sslProps = null;\r
244     if ( sslPropertiesObj instanceof HTTPSProperties ) {\r
245       sslProps = (HTTPSProperties)sslPropertiesObj;\r
246       assertNull(sslProps.getHostnameVerifier());\r
247     } else {\r
248       fail("Unexpected value for https properties object");\r
249     }  }\r
250   \r
251   @Test (expected=IllegalArgumentException.class)\r
252   public void validateSslCertClient_illegalArgumentExceptionWhenTruststoreIsNull() throws Exception {\r
253     \r
254     RestClientBuilder restClientBuilder = new RestClientBuilder();\r
255     \r
256     restClientBuilder.setAuthenticationMode(RestAuthenticationMode.SSL_CERT);\r
257     restClientBuilder.setConnectTimeoutInMs(12345);\r
258     restClientBuilder.setReadTimeoutInMs(54321);\r
259     restClientBuilder.setValidateServerCertChain(true);\r
260     restClientBuilder.setValidateServerHostname(true);\r
261     restClientBuilder.setTruststoreFilename(null);\r
262     \r
263     /*\r
264      * Creating the client in this scenario will cause an IllegalArgumentException caused by the\r
265      * truststore being null\r
266      */\r
267     Client client = restClientBuilder.getClient();\r
268    \r
269   }\r
270   \r
271     \r
272 }\r