aboutsummaryrefslogtreecommitdiffstats
path: root/src/test/java/org/openecomp/restclient/rest/RestClientBuilderTest.java
blob: 93e55202f8b6fd7e43fde31f1cd632f0bb4e5af5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package org.openecomp.restclient.rest;

import java.util.Map;

import org.junit.Before;
import org.junit.Test;
import org.openecomp.restclient.rest.RestClientBuilder;

import static org.junit.Assert.*;

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.
 */
public class RestClientBuilderTest {

  /**
   * This test validates that we can enable and disable certificate chain verification and that the
   * associated parameters are correctly set.
   */
  @Test
  public void certificateChainVerificationTest() throws Exception {

    final String TRUST_STORE_FILENAME = "myTrustStore";


    // Instantiate a RestClientBuilder with default parameters and
    // get a client instance.
    RestClientBuilder builder = new RestClientBuilder();
    Client client = builder.getClient();

    // Validate that, by default, no trust store has been set.
    assertNull("Trust store filename should not be set for default builder",
        System.getProperty("javax.net.ssl.trustStore"));

    // Now, enable certificate chain verification, but don't specify
    // a trust store filename.
    builder.setValidateServerCertChain(true);

    // Now, get a new client instance. We expect the builder to complain
    // because there is no trust store filename.
    try {
      Client client2 = builder.getClient();
      fail("Expected exception due to no trust store filename.");

    } catch (IllegalArgumentException e) {
      assertTrue(e.getMessage().contains("Trust store filename must be set"));
    }

    // Now, set a value for the trust store filename and try again to
    // get a client instance. This time it should succeed and we should
    // see that our trust name filename was set.
    builder.setTruststoreFilename(TRUST_STORE_FILENAME);
    Client client3 = builder.getClient();

    // Validate that the trust store filename was set.
    assertNotNull("Expected trust store filename to be set",
        System.getProperty("javax.net.ssl.trustStore"));

    // Validate that the filename is set to the value we specified.
    assertTrue(
        "Unexpected trust store filename value " + System.getProperty("javax.net.ssl.trustStore"),
        System.getProperty("javax.net.ssl.trustStore").equals(TRUST_STORE_FILENAME));
  }


  /**
   * This test validates that we can set timeout values in our client builder and that those values
   * are reflected in the client produced by the builder.
   */
  @Test
  public void timeoutValuesTest() throws Exception {

    // Instantiate a RestClientBuilder with default parameters.
    RestClientBuilder builder = new RestClientBuilder();

    // Now, get a client instance and retrieve the client properties.
    Client client = builder.getClient();

    Map<String, Object> props = client.getProperties();

    // Validate that the connection and read timeouts are set to the
    // default values.
    assertEquals("Unexpected connect timeout parameter",
        props.get("com.sun.jersey.client.property.connectTimeout"),
        RestClientBuilder.DEFAULT_CONNECT_TIMEOUT_MS);
    assertEquals("Unexpected read timeout parameter",
        props.get("com.sun.jersey.client.property.readTimeout"),
        RestClientBuilder.DEFAULT_READ_TIMEOUT_MS);

    // Now, change the timeouts in the builder to non-default values.
    builder.setConnectTimeoutInMs(RestClientBuilder.DEFAULT_CONNECT_TIMEOUT_MS + 100);
    builder.setReadTimeoutInMs(RestClientBuilder.DEFAULT_READ_TIMEOUT_MS + 100);

    // Retrieve a new client instance and get the client properties.
    Client client2 = builder.getClient();
    props = client2.getProperties();

    // Validate that the connection and read timeouts are set to the
    // new values.
    assertEquals("Unexpected connect timeout parameter",
        props.get("com.sun.jersey.client.property.connectTimeout"),
        RestClientBuilder.DEFAULT_CONNECT_TIMEOUT_MS + 100);
    assertEquals("Unexpected read timeout parameter",
        props.get("com.sun.jersey.client.property.readTimeout"),
        RestClientBuilder.DEFAULT_READ_TIMEOUT_MS + 100);
  }


  /**
   * This test validates that we can enable and disable host name verification in the clients
   * produced by our builder.
   */
  @Test
  public void hostNameVerifierTest() throws Exception {

    // Instantiate a RestClientBuilder with default parameters.
    RestClientBuilder builder = new RestClientBuilder();

    // Now, get a client instance.
    Client client1 = builder.getClient();

    // Retrieve the client's HTTPS properties.
    HTTPSProperties httpProps = getHTTPSProperties(client1);

    // By default, hostname verification should be disabled, which means
    // that our builder will have injected its own {@link HostnameVerifier}
    // which just always returns true.
    assertNotNull(httpProps.getHostnameVerifier());

    // Verify that the host name verifier returns true regardless of what
    // hostname we pass in.
    assertTrue("Default hostname verifier should always return true",
        httpProps.getHostnameVerifier().verify("not_a_valid_hostname", null));


    // Now, enable hostname verification for our client builder, and
    // get a new client.
    builder.setValidateServerHostname(true);
    Client client2 = builder.getClient();

    // Retrieve the client's HTTPS properties.
    httpProps = getHTTPSProperties(client2);

    // Verify that with hostname verification enabled, our builder did not
    // insert its own stubbed verifier.
    assertNull(httpProps.getHostnameVerifier());
  }


  /**
   * This is a convenience method which extracts the HTTPS properties from a supplied client.
   *
   * @parameter aClient - The client to retrieve the HTTPS properties from.
   */
  private HTTPSProperties getHTTPSProperties(Client aClient) {

    Map<String, Object> props = aClient.getProperties();
    return (HTTPSProperties) props.get(HTTPSProperties.PROPERTY_HTTPS_PROPERTIES);
  }
}