Update rest-client with additional operations
[aai/rest-client.git] / src / test / java / org / openecomp / restclient / rest / RestClientBuilderTest.java
index 93e5520..e299e36 100644 (file)
@@ -1,18 +1,18 @@
 package org.openecomp.restclient.rest;\r
 \r
-import java.util.Map;\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.openecomp.restclient.rest.RestClientBuilder;\r
-\r
-import static org.junit.Assert.*;\r
+import org.openecomp.restclient.enums.RestAuthenticationMode;\r
 \r
 import com.sun.jersey.api.client.Client;\r
-\r
 import com.sun.jersey.client.urlconnection.HTTPSProperties;\r
 \r
-\r
 /**\r
  * This suite of tests is intended to exercise the functionality of the generice REST client\r
  * builder.\r
@@ -20,147 +20,231 @@ import com.sun.jersey.client.urlconnection.HTTPSProperties;
 public class RestClientBuilderTest {\r
 \r
   /**\r
-   * This test validates that we can enable and disable certificate chain verification and that the\r
-   * associated parameters are correctly set.\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 certificateChainVerificationTest() throws Exception {\r
-\r
-    final String TRUST_STORE_FILENAME = "myTrustStore";\r
-\r
-\r
-    // Instantiate a RestClientBuilder with default parameters and\r
-    // get a client instance.\r
-    RestClientBuilder builder = new RestClientBuilder();\r
-    Client client = builder.getClient();\r
-\r
-    // Validate that, by default, no trust store has been set.\r
-    assertNull("Trust store filename should not be set for default builder",\r
-        System.getProperty("javax.net.ssl.trustStore"));\r
-\r
-    // Now, enable certificate chain verification, but don't specify\r
-    // a trust store filename.\r
-    builder.setValidateServerCertChain(true);\r
-\r
-    // Now, get a new client instance. We expect the builder to complain\r
-    // because there is no trust store filename.\r
-    try {\r
-      Client client2 = builder.getClient();\r
-      fail("Expected exception due to no trust store filename.");\r
-\r
-    } catch (IllegalArgumentException e) {\r
-      assertTrue(e.getMessage().contains("Trust store filename must be set"));\r
-    }\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
-    // Now, set a value for the trust store filename and try again to\r
-    // get a client instance. This time it should succeed and we should\r
-    // see that our trust name filename was set.\r
-    builder.setTruststoreFilename(TRUST_STORE_FILENAME);\r
-    Client client3 = builder.getClient();\r
-\r
-    // Validate that the trust store filename was set.\r
-    assertNotNull("Expected trust store filename to be set",\r
-        System.getProperty("javax.net.ssl.trustStore"));\r
-\r
-    // Validate that the filename is set to the value we specified.\r
-    assertTrue(\r
-        "Unexpected trust store filename value " + System.getProperty("javax.net.ssl.trustStore"),\r
-        System.getProperty("javax.net.ssl.trustStore").equals(TRUST_STORE_FILENAME));\r
   }\r
-\r
-\r
-  /**\r
-   * This test validates that we can set timeout values in our client builder and that those values\r
-   * are reflected in the client produced by the builder.\r
-   */\r
+  \r
   @Test\r
-  public void timeoutValuesTest() throws Exception {\r
-\r
-    // Instantiate a RestClientBuilder with default parameters.\r
-    RestClientBuilder builder = new RestClientBuilder();\r
-\r
-    // Now, get a client instance and retrieve the client properties.\r
-    Client client = builder.getClient();\r
-\r
-    Map<String, Object> props = client.getProperties();\r
-\r
-    // Validate that the connection and read timeouts are set to the\r
-    // default values.\r
-    assertEquals("Unexpected connect timeout parameter",\r
-        props.get("com.sun.jersey.client.property.connectTimeout"),\r
-        RestClientBuilder.DEFAULT_CONNECT_TIMEOUT_MS);\r
-    assertEquals("Unexpected read timeout parameter",\r
-        props.get("com.sun.jersey.client.property.readTimeout"),\r
-        RestClientBuilder.DEFAULT_READ_TIMEOUT_MS);\r
-\r
-    // Now, change the timeouts in the builder to non-default values.\r
-    builder.setConnectTimeoutInMs(RestClientBuilder.DEFAULT_CONNECT_TIMEOUT_MS + 100);\r
-    builder.setReadTimeoutInMs(RestClientBuilder.DEFAULT_READ_TIMEOUT_MS + 100);\r
-\r
-    // Retrieve a new client instance and get the client properties.\r
-    Client client2 = builder.getClient();\r
-    props = client2.getProperties();\r
-\r
-    // Validate that the connection and read timeouts are set to the\r
-    // new values.\r
-    assertEquals("Unexpected connect timeout parameter",\r
-        props.get("com.sun.jersey.client.property.connectTimeout"),\r
-        RestClientBuilder.DEFAULT_CONNECT_TIMEOUT_MS + 100);\r
-    assertEquals("Unexpected read timeout parameter",\r
-        props.get("com.sun.jersey.client.property.readTimeout"),\r
-        RestClientBuilder.DEFAULT_READ_TIMEOUT_MS + 100);\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
-  /**\r
-   * This test validates that we can enable and disable host name verification in the clients\r
-   * produced by our builder.\r
-   */\r
+  \r
+  \r
   @Test\r
-  public void hostNameVerifierTest() throws Exception {\r
-\r
-    // Instantiate a RestClientBuilder with default parameters.\r
-    RestClientBuilder builder = new RestClientBuilder();\r
-\r
-    // Now, get a client instance.\r
-    Client client1 = builder.getClient();\r
-\r
-    // Retrieve the client's HTTPS properties.\r
-    HTTPSProperties httpProps = getHTTPSProperties(client1);\r
-\r
-    // By default, hostname verification should be disabled, which means\r
-    // that our builder will have injected its own {@link HostnameVerifier}\r
-    // which just always returns true.\r
-    assertNotNull(httpProps.getHostnameVerifier());\r
-\r
-    // Verify that the host name verifier returns true regardless of what\r
-    // hostname we pass in.\r
-    assertTrue("Default hostname verifier should always return true",\r
-        httpProps.getHostnameVerifier().verify("not_a_valid_hostname", null));\r
-\r
-\r
-    // Now, enable hostname verification for our client builder, and\r
-    // get a new client.\r
-    builder.setValidateServerHostname(true);\r
-    Client client2 = builder.getClient();\r
-\r
-    // Retrieve the client's HTTPS properties.\r
-    httpProps = getHTTPSProperties(client2);\r
-\r
-    // Verify that with hostname verification enabled, our builder did not\r
-    // insert its own stubbed verifier.\r
-    assertNull(httpProps.getHostnameVerifier());\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
-  /**\r
-   * This is a convenience method which extracts the HTTPS properties from a supplied client.\r
-   *\r
-   * @parameter aClient - The client to retrieve the HTTPS properties from.\r
-   */\r
-  private HTTPSProperties getHTTPSProperties(Client aClient) {\r
-\r
-    Map<String, Object> props = aClient.getProperties();\r
-    return (HTTPSProperties) props.get(HTTPSProperties.PROPERTY_HTTPS_PROPERTIES);\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