Catalog alignment
[sdc.git] / common-app-api / src / test / java / org / openecomp / sdc / common / http / client / api / HttpClientConfigImmutableTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.openecomp.sdc.common.http.client.api;
22
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;
32
33 import java.util.Collections;
34 import java.util.Map;
35
36 import static org.junit.Assert.assertEquals;
37 import static org.junit.Assert.assertTrue;
38
39 @RunWith(MockitoJUnitRunner.class)
40 public class HttpClientConfigImmutableTest {
41
42     @Mock
43     private ComparableHttpRequestRetryHandler testRetryHandler;
44
45     private ClientCertificate testClientCertificate;
46
47     private Map<String, String> testHeaders;
48
49     private BasicAuthorization testBasicAuthorization;
50
51
52     private Timeouts testTimeouts;
53
54
55     private int testNumOfRetries;
56
57     @Test
58     public void validateArgConstructorCreatesValidImmutableConfig() {
59         HttpClientConfigImmutable httpClientConfigImmutable = new HttpClientConfigImmutable(prepareTestClientConfig());
60         validateAllVieldsInImmutableConfig(httpClientConfigImmutable);
61     }
62
63     @Test
64     public void validateStaticMethodCreatesValidImmutableConfig() {
65         HttpClientConfigImmutable httpClientConfigImmutable = HttpClientConfigImmutable.getOrCreate(prepareTestClientConfig());
66         validateAllVieldsInImmutableConfig(httpClientConfigImmutable);
67     }
68
69     @Test
70     public void validateToString() {
71         HttpClientConfigImmutable httpClientConfigImmutable = HttpClientConfigImmutable.getOrCreate(prepareTestClientConfig());
72         final String result = httpClientConfigImmutable.toString();
73
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)));
80     }
81
82     @Test
83     public void validateHashCode() {
84         HttpClientConfigImmutable httpClientConfigImmutable01 = new HttpClientConfigImmutable(prepareTestClientConfig());
85         HttpClientConfigImmutable httpClientConfigImmutable02 = HttpClientConfigImmutable.getOrCreate(prepareTestClientConfig());
86
87         assertEquals(
88                 httpClientConfigImmutable01.hashCode(),
89                 httpClientConfigImmutable02.hashCode()
90         );
91     }
92
93     private void validateAllVieldsInImmutableConfig(HttpClientConfigImmutable httpClientConfigImmutable) {
94         assertEquals(
95                 httpClientConfigImmutable.getNumOfRetries(),
96                 testNumOfRetries);
97         assertEquals(
98                 httpClientConfigImmutable.getReadTimeoutMs(),
99                 testTimeouts.getReadTimeoutMs());
100         assertEquals(
101                 httpClientConfigImmutable.getConnectTimeoutMs(),
102                 testTimeouts.getConnectTimeoutMs());
103         assertEquals(
104                 httpClientConfigImmutable.getConnectPoolTimeoutMs(),
105                 testTimeouts.getConnectPoolTimeoutMs());
106         assertEquals(
107                 httpClientConfigImmutable.getBasicAuthUserName(),
108                 testBasicAuthorization.getUserName());
109         assertEquals(
110                 httpClientConfigImmutable.getBasicAuthPassword(),
111                 testBasicAuthorization.getPassword());
112         assertEquals(
113                 httpClientConfigImmutable.getClientCertificate().getClass(),
114                 testClientCertificate.getClass());
115         assertEquals(
116                 httpClientConfigImmutable.getClientCertKeyStore(),
117                 testClientCertificate.getKeyStore());
118         assertEquals(
119                 httpClientConfigImmutable.getClientCertKeyPassword(),
120                 testClientCertificate.getKeyStorePassword());
121         assertEquals(
122                 httpClientConfigImmutable.getRetryHandler(),
123                 testRetryHandler);
124         assertEquals(
125                 httpClientConfigImmutable.getHeaders(),
126                 testHeaders);
127     }
128
129     private HttpClientConfig prepareTestClientConfig() {
130         final String testUserName = "testUser";
131         final String testUserPassword = SecurityUtil.INSTANCE.encrypt("testPassword").left().value();
132         final int timeouts = 10;
133         final String testKeyStore = "testKeyStore";
134         final String testKeyStorePassword = SecurityUtil.INSTANCE.encrypt("testKeyStorePassword").left().value();
135
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);
146
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);
154
155         return httpClientConfig;
156     }
157 }