1 package org.openecomp.restclient.rest;
\r
3 import java.util.Map;
\r
5 import org.junit.Before;
\r
6 import org.junit.Test;
\r
7 import org.openecomp.restclient.rest.RestClientBuilder;
\r
9 import static org.junit.Assert.*;
\r
11 import com.sun.jersey.api.client.Client;
\r
13 import com.sun.jersey.client.urlconnection.HTTPSProperties;
\r
17 * This suite of tests is intended to exercise the functionality of the generice REST client
\r
20 public class RestClientBuilderTest {
\r
23 * This test validates that we can enable and disable certificate chain verification and that the
\r
24 * associated parameters are correctly set.
\r
27 public void certificateChainVerificationTest() throws Exception {
\r
29 final String TRUST_STORE_FILENAME = "myTrustStore";
\r
32 // Instantiate a RestClientBuilder with default parameters and
\r
33 // get a client instance.
\r
34 RestClientBuilder builder = new RestClientBuilder();
\r
35 Client client = builder.getClient();
\r
37 // Validate that, by default, no trust store has been set.
\r
38 assertNull("Trust store filename should not be set for default builder",
\r
39 System.getProperty("javax.net.ssl.trustStore"));
\r
41 // Now, enable certificate chain verification, but don't specify
\r
42 // a trust store filename.
\r
43 builder.setValidateServerCertChain(true);
\r
45 // Now, get a new client instance. We expect the builder to complain
\r
46 // because there is no trust store filename.
\r
48 Client client2 = builder.getClient();
\r
49 fail("Expected exception due to no trust store filename.");
\r
51 } catch (IllegalArgumentException e) {
\r
52 assertTrue(e.getMessage().contains("Trust store filename must be set"));
\r
55 // Now, set a value for the trust store filename and try again to
\r
56 // get a client instance. This time it should succeed and we should
\r
57 // see that our trust name filename was set.
\r
58 builder.setTruststoreFilename(TRUST_STORE_FILENAME);
\r
59 Client client3 = builder.getClient();
\r
61 // Validate that the trust store filename was set.
\r
62 assertNotNull("Expected trust store filename to be set",
\r
63 System.getProperty("javax.net.ssl.trustStore"));
\r
65 // Validate that the filename is set to the value we specified.
\r
67 "Unexpected trust store filename value " + System.getProperty("javax.net.ssl.trustStore"),
\r
68 System.getProperty("javax.net.ssl.trustStore").equals(TRUST_STORE_FILENAME));
\r
73 * This test validates that we can set timeout values in our client builder and that those values
\r
74 * are reflected in the client produced by the builder.
\r
77 public void timeoutValuesTest() throws Exception {
\r
79 // Instantiate a RestClientBuilder with default parameters.
\r
80 RestClientBuilder builder = new RestClientBuilder();
\r
82 // Now, get a client instance and retrieve the client properties.
\r
83 Client client = builder.getClient();
\r
85 Map<String, Object> props = client.getProperties();
\r
87 // Validate that the connection and read timeouts are set to the
\r
89 assertEquals("Unexpected connect timeout parameter",
\r
90 props.get("com.sun.jersey.client.property.connectTimeout"),
\r
91 RestClientBuilder.DEFAULT_CONNECT_TIMEOUT_MS);
\r
92 assertEquals("Unexpected read timeout parameter",
\r
93 props.get("com.sun.jersey.client.property.readTimeout"),
\r
94 RestClientBuilder.DEFAULT_READ_TIMEOUT_MS);
\r
96 // Now, change the timeouts in the builder to non-default values.
\r
97 builder.setConnectTimeoutInMs(RestClientBuilder.DEFAULT_CONNECT_TIMEOUT_MS + 100);
\r
98 builder.setReadTimeoutInMs(RestClientBuilder.DEFAULT_READ_TIMEOUT_MS + 100);
\r
100 // Retrieve a new client instance and get the client properties.
\r
101 Client client2 = builder.getClient();
\r
102 props = client2.getProperties();
\r
104 // Validate that the connection and read timeouts are set to the
\r
106 assertEquals("Unexpected connect timeout parameter",
\r
107 props.get("com.sun.jersey.client.property.connectTimeout"),
\r
108 RestClientBuilder.DEFAULT_CONNECT_TIMEOUT_MS + 100);
\r
109 assertEquals("Unexpected read timeout parameter",
\r
110 props.get("com.sun.jersey.client.property.readTimeout"),
\r
111 RestClientBuilder.DEFAULT_READ_TIMEOUT_MS + 100);
\r
116 * This test validates that we can enable and disable host name verification in the clients
\r
117 * produced by our builder.
\r
120 public void hostNameVerifierTest() throws Exception {
\r
122 // Instantiate a RestClientBuilder with default parameters.
\r
123 RestClientBuilder builder = new RestClientBuilder();
\r
125 // Now, get a client instance.
\r
126 Client client1 = builder.getClient();
\r
128 // Retrieve the client's HTTPS properties.
\r
129 HTTPSProperties httpProps = getHTTPSProperties(client1);
\r
131 // By default, hostname verification should be disabled, which means
\r
132 // that our builder will have injected its own {@link HostnameVerifier}
\r
133 // which just always returns true.
\r
134 assertNotNull(httpProps.getHostnameVerifier());
\r
136 // Verify that the host name verifier returns true regardless of what
\r
137 // hostname we pass in.
\r
138 assertTrue("Default hostname verifier should always return true",
\r
139 httpProps.getHostnameVerifier().verify("not_a_valid_hostname", null));
\r
142 // Now, enable hostname verification for our client builder, and
\r
143 // get a new client.
\r
144 builder.setValidateServerHostname(true);
\r
145 Client client2 = builder.getClient();
\r
147 // Retrieve the client's HTTPS properties.
\r
148 httpProps = getHTTPSProperties(client2);
\r
150 // Verify that with hostname verification enabled, our builder did not
\r
151 // insert its own stubbed verifier.
\r
152 assertNull(httpProps.getHostnameVerifier());
\r
157 * This is a convenience method which extracts the HTTPS properties from a supplied client.
\r
159 * @parameter aClient - The client to retrieve the HTTPS properties from.
\r
161 private HTTPSProperties getHTTPSProperties(Client aClient) {
\r
163 Map<String, Object> props = aClient.getProperties();
\r
164 return (HTTPSProperties) props.get(HTTPSProperties.PROPERTY_HTTPS_PROPERTIES);
\r