93e55202f8b6fd7e43fde31f1cd632f0bb4e5af5
[aai/rest-client.git] / src / test / java / org / openecomp / restclient / rest / RestClientBuilderTest.java
1 package org.openecomp.restclient.rest;\r
2 \r
3 import java.util.Map;\r
4 \r
5 import org.junit.Before;\r
6 import org.junit.Test;\r
7 import org.openecomp.restclient.rest.RestClientBuilder;\r
8 \r
9 import static org.junit.Assert.*;\r
10 \r
11 import com.sun.jersey.api.client.Client;\r
12 \r
13 import com.sun.jersey.client.urlconnection.HTTPSProperties;\r
14 \r
15 \r
16 /**\r
17  * This suite of tests is intended to exercise the functionality of the generice REST client\r
18  * builder.\r
19  */\r
20 public class RestClientBuilderTest {\r
21 \r
22   /**\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
25    */\r
26   @Test\r
27   public void certificateChainVerificationTest() throws Exception {\r
28 \r
29     final String TRUST_STORE_FILENAME = "myTrustStore";\r
30 \r
31 \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
36 \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
40 \r
41     // Now, enable certificate chain verification, but don't specify\r
42     // a trust store filename.\r
43     builder.setValidateServerCertChain(true);\r
44 \r
45     // Now, get a new client instance. We expect the builder to complain\r
46     // because there is no trust store filename.\r
47     try {\r
48       Client client2 = builder.getClient();\r
49       fail("Expected exception due to no trust store filename.");\r
50 \r
51     } catch (IllegalArgumentException e) {\r
52       assertTrue(e.getMessage().contains("Trust store filename must be set"));\r
53     }\r
54 \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
60 \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
64 \r
65     // Validate that the filename is set to the value we specified.\r
66     assertTrue(\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
69   }\r
70 \r
71 \r
72   /**\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
75    */\r
76   @Test\r
77   public void timeoutValuesTest() throws Exception {\r
78 \r
79     // Instantiate a RestClientBuilder with default parameters.\r
80     RestClientBuilder builder = new RestClientBuilder();\r
81 \r
82     // Now, get a client instance and retrieve the client properties.\r
83     Client client = builder.getClient();\r
84 \r
85     Map<String, Object> props = client.getProperties();\r
86 \r
87     // Validate that the connection and read timeouts are set to the\r
88     // default values.\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
95 \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
99 \r
100     // Retrieve a new client instance and get the client properties.\r
101     Client client2 = builder.getClient();\r
102     props = client2.getProperties();\r
103 \r
104     // Validate that the connection and read timeouts are set to the\r
105     // new values.\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
112   }\r
113 \r
114 \r
115   /**\r
116    * This test validates that we can enable and disable host name verification in the clients\r
117    * produced by our builder.\r
118    */\r
119   @Test\r
120   public void hostNameVerifierTest() throws Exception {\r
121 \r
122     // Instantiate a RestClientBuilder with default parameters.\r
123     RestClientBuilder builder = new RestClientBuilder();\r
124 \r
125     // Now, get a client instance.\r
126     Client client1 = builder.getClient();\r
127 \r
128     // Retrieve the client's HTTPS properties.\r
129     HTTPSProperties httpProps = getHTTPSProperties(client1);\r
130 \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
135 \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
140 \r
141 \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
146 \r
147     // Retrieve the client's HTTPS properties.\r
148     httpProps = getHTTPSProperties(client2);\r
149 \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
153   }\r
154 \r
155 \r
156   /**\r
157    * This is a convenience method which extracts the HTTPS properties from a supplied client.\r
158    *\r
159    * @parameter aClient - The client to retrieve the HTTPS properties from.\r
160    */\r
161   private HTTPSProperties getHTTPSProperties(Client aClient) {\r
162 \r
163     Map<String, Object> props = aClient.getProperties();\r
164     return (HTTPSProperties) props.get(HTTPSProperties.PROPERTY_HTTPS_PROPERTIES);\r
165   }\r
166 }\r