2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 Nokia. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.openecomp.sdc.common.http.client.api;
23 import org.junit.Test;
24 import org.junit.runner.RunWith;
25 import org.mockito.Mock;
26 import org.mockito.junit.MockitoJUnitRunner;
27 import org.onap.sdc.security.SecurityUtil;
28 import org.openecomp.sdc.common.http.config.BasicAuthorization;
29 import org.openecomp.sdc.common.http.config.ClientCertificate;
30 import org.openecomp.sdc.common.http.config.HttpClientConfig;
31 import org.openecomp.sdc.common.http.config.Timeouts;
33 import java.util.Collections;
36 import static org.junit.Assert.assertEquals;
37 import static org.junit.Assert.assertTrue;
39 @RunWith(MockitoJUnitRunner.class)
40 public class HttpClientConfigImmutableTest {
43 private ComparableHttpRequestRetryHandler testRetryHandler;
45 private ClientCertificate testClientCertificate;
47 private Map<String, String> testHeaders;
49 private BasicAuthorization testBasicAuthorization;
52 private Timeouts testTimeouts;
55 private int testNumOfRetries;
58 public void validateArgConstructorCreatesValidImmutableConfig() {
59 HttpClientConfigImmutable httpClientConfigImmutable = new HttpClientConfigImmutable(prepareTestClientConfig());
60 validateAllVieldsInImmutableConfig(httpClientConfigImmutable);
64 public void validateStaticMethodCreatesValidImmutableConfig() {
65 HttpClientConfigImmutable httpClientConfigImmutable = HttpClientConfigImmutable.getOrCreate(prepareTestClientConfig());
66 validateAllVieldsInImmutableConfig(httpClientConfigImmutable);
70 public void validateToString() {
71 HttpClientConfigImmutable httpClientConfigImmutable = HttpClientConfigImmutable.getOrCreate(prepareTestClientConfig());
72 final String result = httpClientConfigImmutable.toString();
74 assertTrue(result.contains(testBasicAuthorization.toString()));
75 assertTrue(result.contains(testClientCertificate.toString()));
76 assertTrue(result.contains(testRetryHandler.toString()));
77 assertTrue(result.contains(testTimeouts.toString()));
78 assertTrue(result.contains(testHeaders.toString()));
79 assertTrue(result.contains(Integer.toString(testNumOfRetries)));
83 public void validateHashCode() {
84 HttpClientConfigImmutable httpClientConfigImmutable01 = new HttpClientConfigImmutable(prepareTestClientConfig());
85 HttpClientConfigImmutable httpClientConfigImmutable02 = HttpClientConfigImmutable.getOrCreate(prepareTestClientConfig());
88 httpClientConfigImmutable01.hashCode(),
89 httpClientConfigImmutable02.hashCode()
93 private void validateAllVieldsInImmutableConfig(HttpClientConfigImmutable httpClientConfigImmutable) {
95 httpClientConfigImmutable.getNumOfRetries(),
98 httpClientConfigImmutable.getReadTimeoutMs(),
99 testTimeouts.getReadTimeoutMs());
101 httpClientConfigImmutable.getConnectTimeoutMs(),
102 testTimeouts.getConnectTimeoutMs());
104 httpClientConfigImmutable.getConnectPoolTimeoutMs(),
105 testTimeouts.getConnectPoolTimeoutMs());
107 httpClientConfigImmutable.getBasicAuthUserName(),
108 testBasicAuthorization.getUserName());
110 httpClientConfigImmutable.getBasicAuthPassword(),
111 testBasicAuthorization.getPassword());
113 httpClientConfigImmutable.getClientCertificate().getClass(),
114 testClientCertificate.getClass());
116 httpClientConfigImmutable.getClientCertKeyStore(),
117 testClientCertificate.getKeyStore());
119 httpClientConfigImmutable.getClientCertKeyPassword(),
120 testClientCertificate.getKeyStorePassword());
122 httpClientConfigImmutable.getRetryHandler(),
125 httpClientConfigImmutable.getHeaders(),
129 private HttpClientConfig prepareTestClientConfig() {
130 final String testUserName = "testUser";
131 final String testUserPassword = SecurityUtil.encrypt("testPassword").left().value();
132 final int timeouts = 10;
133 final String testKeyStore = "testKeyStore";
134 final String testKeyStorePassword = SecurityUtil.encrypt("testKeyStorePassword").left().value();
136 testNumOfRetries = 10;
137 testHeaders = Collections.emptyMap();
138 testTimeouts = new Timeouts(timeouts,timeouts);
139 testTimeouts.setConnectPoolTimeoutMs(timeouts);
140 testBasicAuthorization = new BasicAuthorization();
141 testBasicAuthorization.setUserName(testUserName);
142 testBasicAuthorization.setPassword(testUserPassword);
143 testClientCertificate = new ClientCertificate();
144 testClientCertificate.setKeyStore(testKeyStore);
145 testClientCertificate.setKeyStorePassword(testKeyStorePassword);
147 HttpClientConfig httpClientConfig = new HttpClientConfig();
148 httpClientConfig.setNumOfRetries(testNumOfRetries);
149 httpClientConfig.setTimeouts(testTimeouts);
150 httpClientConfig.setBasicAuthorization(testBasicAuthorization);
151 httpClientConfig.setClientCertificate(testClientCertificate);
152 httpClientConfig.setRetryHandler(testRetryHandler);
153 httpClientConfig.setHeaders(testHeaders);
155 return httpClientConfig;