Initial OpenEcomp A&AI Rest Client commit.
[aai/rest-client.git] / src / test / java / org / openecomp / restclient / rest / RestClientBuilderTest.java
diff --git a/src/test/java/org/openecomp/restclient/rest/RestClientBuilderTest.java b/src/test/java/org/openecomp/restclient/rest/RestClientBuilderTest.java
new file mode 100644 (file)
index 0000000..93e5520
--- /dev/null
@@ -0,0 +1,166 @@
+package org.openecomp.restclient.rest;\r
+\r
+import java.util.Map;\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
+\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
+ */\r
+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
+   */\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
+\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
+  @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
+  }\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
+  @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
+  }\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
+  }\r
+}\r