X-Git-Url: https://gerrit.onap.org/r/gitweb?p=aai%2Frest-client.git;a=blobdiff_plain;f=src%2Ftest%2Fjava%2Forg%2Fonap%2Faai%2Frestclient%2Frest%2FRestClientBuilderTest.java;h=3878813e881d79e4ea83a72404bfae5c8b67047e;hp=7155f9a3804413253ffdbae6838be2e37038f918;hb=c782aa5250b32e8aa3c7ecd088f47fd87334a8ef;hpb=ef858ed661134e651082675c091db056f8add98d diff --git a/src/test/java/org/onap/aai/restclient/rest/RestClientBuilderTest.java b/src/test/java/org/onap/aai/restclient/rest/RestClientBuilderTest.java index 7155f9a..3878813 100644 --- a/src/test/java/org/onap/aai/restclient/rest/RestClientBuilderTest.java +++ b/src/test/java/org/onap/aai/restclient/rest/RestClientBuilderTest.java @@ -26,243 +26,216 @@ 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 javax.ws.rs.client.Client; 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. + * 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 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 (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 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 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()); - - } - + + @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); + } + + + @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); + } + + @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(); + assertNotNull(client.getHostnameVerifier()); + assertEquals("truststore", System.getProperty("javax.net.ssl.trustStore")); + } + + @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); + + 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); + + 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(); + // TODO + assertNotNull(client.getHostnameVerifier()); + assertEquals("truststore", System.getProperty("javax.net.ssl.trustStore")); + } + + @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(); + // TODO + assertNull(client.getHostnameVerifier()); + assertEquals("truststore", System.getProperty("javax.net.ssl.trustStore")); + } + + @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 + */ + 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()); + + } + }