--- /dev/null
+/**\r
+ * ============LICENSE_START=======================================================\r
+ * org.onap.aai\r
+ * ================================================================================\r
+ * Copyright © 2017 AT&T Intellectual Property. All rights reserved.\r
+ * Copyright © 2017 Amdocs\r
+ * ================================================================================\r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ *\r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ * ============LICENSE_END=========================================================\r
+ *\r
+ * ECOMP is a trademark and service mark of AT&T Intellectual Property.\r
+ */\r
+package org.onap.aai.restclient.rest;\r
+\r
+import static org.junit.Assert.assertEquals;\r
+import static org.junit.Assert.assertNotNull;\r
+import static org.junit.Assert.assertNull;\r
+import static org.junit.Assert.assertTrue;\r
+import static org.junit.Assert.fail;\r
+\r
+import org.junit.Before;\r
+import org.junit.Test;\r
+import org.onap.aai.restclient.enums.RestAuthenticationMode;\r
+import org.onap.aai.restclient.rest.RestClientBuilder;\r
+\r
+import com.sun.jersey.api.client.Client;\r
+import com.sun.jersey.client.urlconnection.HTTPSProperties;\r
+\r
+/**\r
+ * This suite of tests is intended to exercise the functionality of the generice REST client\r
+ * builder.\r
+ */\r
+public class RestClientBuilderTest {\r
+\r
+ /**\r
+ * Test case initialization\r
+ * \r
+ * @throws Exception the exception\r
+ */\r
+ @Before\r
+ public void init() throws Exception {\r
+ }\r
+ \r
+ private String generateAuthorizationHeaderValue(String username, String password) {\r
+ String usernameAndPassword = username + ":" + password;\r
+ return "Basic " + java.util.Base64.getEncoder().encodeToString(usernameAndPassword.getBytes());\r
+ }\r
+ \r
+ @Test\r
+ public void validateAccesors() {\r
+ \r
+ RestClientBuilder restClientBuilder = new RestClientBuilder();\r
+ \r
+ // test defaults\r
+ assertEquals(restClientBuilder.isValidateServerHostname(), RestClientBuilder.DEFAULT_VALIDATE_SERVER_HOST);\r
+ assertEquals(restClientBuilder.isValidateServerCertChain(), RestClientBuilder.DEFAULT_VALIDATE_CERT_CHAIN);\r
+ assertEquals(restClientBuilder.getClientCertFileName(), RestClientBuilder.DEFAULT_CLIENT_CERT_FILENAME);\r
+ assertEquals(restClientBuilder.getClientCertPassword(), RestClientBuilder.DEFAULT_CERT_PASSWORD);\r
+ assertEquals(restClientBuilder.getTruststoreFilename(), RestClientBuilder.DEFAULT_TRUST_STORE_FILENAME);\r
+ assertEquals(restClientBuilder.getConnectTimeoutInMs(), RestClientBuilder.DEFAULT_CONNECT_TIMEOUT_MS);\r
+ assertEquals(restClientBuilder.getReadTimeoutInMs(), RestClientBuilder.DEFAULT_READ_TIMEOUT_MS);\r
+ assertEquals(restClientBuilder.getAuthenticationMode(), RestClientBuilder.DEFAULT_AUTH_MODE);\r
+ assertEquals(restClientBuilder.getBasicAuthUsername(), RestClientBuilder.DEFAULT_BASIC_AUTH_USERNAME);\r
+ assertEquals(restClientBuilder.getBasicAuthPassword(), RestClientBuilder.DEFAULT_BASIC_AUTH_PASSWORD);\r
+ \r
+ restClientBuilder.setAuthenticationMode(RestAuthenticationMode.UNKNOWN_MODE);\r
+ restClientBuilder.setBasicAuthPassword("password");\r
+ restClientBuilder.setBasicAuthUsername("username");\r
+ restClientBuilder.setClientCertFileName("filename");\r
+ restClientBuilder.setClientCertPassword("password");\r
+ restClientBuilder.setConnectTimeoutInMs(12345);\r
+ restClientBuilder.setReadTimeoutInMs(54321);\r
+ restClientBuilder.setTruststoreFilename("truststore");\r
+ restClientBuilder.setValidateServerCertChain(true);\r
+ restClientBuilder.setValidateServerHostname(true);\r
+ \r
+ assertEquals(restClientBuilder.isValidateServerHostname(), true);\r
+ assertEquals(restClientBuilder.isValidateServerCertChain(), true);\r
+ assertEquals(restClientBuilder.getClientCertFileName(), "filename");\r
+ assertEquals(restClientBuilder.getClientCertPassword(), "password");\r
+ assertEquals(restClientBuilder.getTruststoreFilename(), "truststore");\r
+ assertEquals(restClientBuilder.getConnectTimeoutInMs(), 12345);\r
+ assertEquals(restClientBuilder.getReadTimeoutInMs(), 54321);\r
+ assertEquals(restClientBuilder.getAuthenticationMode(), RestAuthenticationMode.UNKNOWN_MODE);\r
+ assertEquals(restClientBuilder.getBasicAuthUsername(), "username");\r
+ assertEquals(restClientBuilder.getBasicAuthPassword(), "password");\r
+ \r
+ assertEquals(restClientBuilder.getBasicAuthenticationCredentials(),\r
+ generateAuthorizationHeaderValue("username", "password"));\r
+\r
+ assertTrue(restClientBuilder.toString().contains("RestClientBuilder"));\r
+\r
+ }\r
+ \r
+ @Test\r
+ public void validateNoAuthClientCreation() throws Exception {\r
+ \r
+ RestClientBuilder restClientBuilder = new RestClientBuilder();\r
+ \r
+ restClientBuilder.setAuthenticationMode(RestAuthenticationMode.HTTP_NOAUTH);\r
+ restClientBuilder.setConnectTimeoutInMs(12345);\r
+ restClientBuilder.setReadTimeoutInMs(54321);\r
+ \r
+ Client client = restClientBuilder.getClient();\r
+ assertNotNull(client);\r
+ assertNull(client.getProperties().get(HTTPSProperties.PROPERTY_HTTPS_PROPERTIES));\r
+ }\r
+ \r
+ \r
+ @Test\r
+ public void validateUnknownModeCreateNoAuthClient() throws Exception {\r
+ \r
+ RestClientBuilder restClientBuilder = new RestClientBuilder();\r
+ \r
+ restClientBuilder.setAuthenticationMode(RestAuthenticationMode.UNKNOWN_MODE);\r
+ restClientBuilder.setConnectTimeoutInMs(12345);\r
+ restClientBuilder.setReadTimeoutInMs(54321);\r
+ \r
+ Client client = restClientBuilder.getClient();\r
+ assertNotNull(client);\r
+ assertNull(client.getProperties().get(HTTPSProperties.PROPERTY_HTTPS_PROPERTIES));\r
+ }\r
+\r
+ @Test\r
+ public void validateBasicAuthSslClient() throws Exception {\r
+ \r
+ RestClientBuilder restClientBuilder = new RestClientBuilder();\r
+ \r
+ restClientBuilder.setAuthenticationMode(RestAuthenticationMode.SSL_BASIC);\r
+ restClientBuilder.setConnectTimeoutInMs(12345);\r
+ restClientBuilder.setReadTimeoutInMs(54321);\r
+ restClientBuilder.setBasicAuthUsername("username");\r
+ restClientBuilder.setBasicAuthPassword("password");\r
+ \r
+ Client client = restClientBuilder.getClient();\r
+ \r
+ Object sslPropertiesObj = client.getProperties().get(HTTPSProperties.PROPERTY_HTTPS_PROPERTIES);\r
+ HTTPSProperties sslProps = null;\r
+ if ( sslPropertiesObj instanceof HTTPSProperties ) {\r
+ sslProps = (HTTPSProperties)sslPropertiesObj;\r
+ assertNotNull(sslProps.getHostnameVerifier());\r
+ } else {\r
+ fail("Unexpected value for https properties object");\r
+ }\r
+ \r
+ }\r
+\r
+ @Test\r
+ public void validateSslCertClient_noHostOrCertChainValidation() throws Exception {\r
+ \r
+ RestClientBuilder restClientBuilder = new RestClientBuilder();\r
+ \r
+ restClientBuilder.setAuthenticationMode(RestAuthenticationMode.SSL_CERT);\r
+ restClientBuilder.setConnectTimeoutInMs(12345);\r
+ restClientBuilder.setReadTimeoutInMs(54321);\r
+ restClientBuilder.setValidateServerCertChain(false);\r
+ restClientBuilder.setValidateServerHostname(false);\r
+ \r
+ Client client = restClientBuilder.getClient();\r
+ \r
+ Object sslPropertiesObj = client.getProperties().get(HTTPSProperties.PROPERTY_HTTPS_PROPERTIES);\r
+ HTTPSProperties sslProps = null;\r
+ if ( sslPropertiesObj instanceof HTTPSProperties ) {\r
+ sslProps = (HTTPSProperties)sslPropertiesObj;\r
+ assertNotNull(sslProps.getHostnameVerifier());\r
+ } else {\r
+ fail("Unexpected value for https properties object");\r
+ } }\r
+ \r
+ @Test\r
+ public void validateSslCertClient_hostOnlyValidation() throws Exception {\r
+ \r
+ RestClientBuilder restClientBuilder = new RestClientBuilder();\r
+ \r
+ restClientBuilder.setAuthenticationMode(RestAuthenticationMode.SSL_CERT);\r
+ restClientBuilder.setConnectTimeoutInMs(12345);\r
+ restClientBuilder.setReadTimeoutInMs(54321);\r
+ restClientBuilder.setValidateServerCertChain(false);\r
+ restClientBuilder.setValidateServerHostname(true);\r
+ \r
+ Client client = restClientBuilder.getClient();\r
+ \r
+ Object sslPropertiesObj = client.getProperties().get(HTTPSProperties.PROPERTY_HTTPS_PROPERTIES);\r
+ HTTPSProperties sslProps = null;\r
+ if ( sslPropertiesObj instanceof HTTPSProperties ) {\r
+ sslProps = (HTTPSProperties)sslPropertiesObj;\r
+ assertNull(sslProps.getHostnameVerifier());\r
+ } else {\r
+ fail("Unexpected value for https properties object");\r
+ }\r
+ }\r
+ \r
+ @Test\r
+ public void validateSslCertClient_certChainOnlyValidation() throws Exception {\r
+ \r
+ RestClientBuilder restClientBuilder = new RestClientBuilder();\r
+ \r
+ restClientBuilder.setAuthenticationMode(RestAuthenticationMode.SSL_CERT);\r
+ restClientBuilder.setConnectTimeoutInMs(12345);\r
+ restClientBuilder.setReadTimeoutInMs(54321);\r
+ restClientBuilder.setValidateServerCertChain(true);\r
+ restClientBuilder.setValidateServerHostname(false);\r
+ restClientBuilder.setTruststoreFilename("truststore");\r
+ restClientBuilder.setClientCertPassword(null);\r
+ \r
+ Client client = restClientBuilder.getClient();\r
+ \r
+ Object sslPropertiesObj = client.getProperties().get(HTTPSProperties.PROPERTY_HTTPS_PROPERTIES);\r
+ HTTPSProperties sslProps = null;\r
+ if ( sslPropertiesObj instanceof HTTPSProperties ) {\r
+ sslProps = (HTTPSProperties)sslPropertiesObj;\r
+ assertNotNull(sslProps.getHostnameVerifier());\r
+ } else {\r
+ fail("Unexpected value for https properties object");\r
+ }\r
+ }\r
+ \r
+ @Test\r
+ public void validateSslCertClient_withHostAndCertChainValidation() throws Exception {\r
+ \r
+ RestClientBuilder restClientBuilder = new RestClientBuilder();\r
+ \r
+ restClientBuilder.setAuthenticationMode(RestAuthenticationMode.SSL_CERT);\r
+ restClientBuilder.setConnectTimeoutInMs(12345);\r
+ restClientBuilder.setReadTimeoutInMs(54321);\r
+ restClientBuilder.setValidateServerCertChain(true);\r
+ restClientBuilder.setValidateServerHostname(true);\r
+ restClientBuilder.setClientCertPassword("password");\r
+ restClientBuilder.setTruststoreFilename("truststore");\r
+ \r
+ Client client = restClientBuilder.getClient();\r
+ \r
+ Object sslPropertiesObj = client.getProperties().get(HTTPSProperties.PROPERTY_HTTPS_PROPERTIES);\r
+ HTTPSProperties sslProps = null;\r
+ if ( sslPropertiesObj instanceof HTTPSProperties ) {\r
+ sslProps = (HTTPSProperties)sslPropertiesObj;\r
+ assertNull(sslProps.getHostnameVerifier());\r
+ } else {\r
+ fail("Unexpected value for https properties object");\r
+ } }\r
+ \r
+ @Test (expected=IllegalArgumentException.class)\r
+ public void validateSslCertClient_illegalArgumentExceptionWhenTruststoreIsNull() throws Exception {\r
+ \r
+ RestClientBuilder restClientBuilder = new RestClientBuilder();\r
+ \r
+ restClientBuilder.setAuthenticationMode(RestAuthenticationMode.SSL_CERT);\r
+ restClientBuilder.setConnectTimeoutInMs(12345);\r
+ restClientBuilder.setReadTimeoutInMs(54321);\r
+ restClientBuilder.setValidateServerCertChain(true);\r
+ restClientBuilder.setValidateServerHostname(true);\r
+ restClientBuilder.setTruststoreFilename(null);\r
+ \r
+ /*\r
+ * Creating the client in this scenario will cause an IllegalArgumentException caused by the\r
+ * truststore being null\r
+ */\r
+ Client client = restClientBuilder.getClient();\r
+ \r
+ }\r
+ \r
+ \r
+}\r