Catalog alignment
[sdc.git] / common-app-api / src / test / java / org / openecomp / sdc / common / http / client / api / HttpClientFactoryTest.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.Mockito;
27 import org.mockito.junit.MockitoJUnitRunner;
28 import org.onap.sdc.security.SecurityUtil;
29 import org.openecomp.sdc.common.http.config.BasicAuthorization;
30 import org.openecomp.sdc.common.http.config.ClientCertificate;
31 import org.openecomp.sdc.common.http.config.HttpClientConfig;
32 import org.openecomp.sdc.common.http.config.Timeouts;
33
34 import java.util.Collections;
35 import java.util.Map;
36
37 import static junit.framework.TestCase.assertNotNull;
38
39 @RunWith(MockitoJUnitRunner.class)
40 public class HttpClientFactoryTest {
41
42     @Mock
43     HttpConnectionMngFactory httpConnectionMngFactory;
44
45     @Test
46     public void validateNewClientCreationReturnsValidClient() throws HttpExecuteException {
47         HttpClient httpClient = new HttpClientFactory(httpConnectionMngFactory).createClient("Http",prepareTestClientConfigImmutable());
48         assertNotNull(httpClient);
49         httpClient.close();
50     }
51
52     private HttpClientConfigImmutable prepareTestClientConfigImmutable() {
53         final String testUserName = "testUser";
54         final String testUserPassword = SecurityUtil.INSTANCE.encrypt("testPassword").left().value();
55         final int timeouts = 10;
56         final String testKeyStore = "testKeyStore";
57         final String testKeyStorePassword = SecurityUtil.INSTANCE.encrypt("testKeyStorePassword").left().value();
58
59         int testNumOfRetries = 10;
60         ComparableHttpRequestRetryHandler testRetryHandler = Mockito.mock(ComparableHttpRequestRetryHandler.class);
61         Map<String, String> testHeaders = Collections.emptyMap();
62         Timeouts testTimeouts = new Timeouts(timeouts, timeouts);
63         testTimeouts.setConnectPoolTimeoutMs(timeouts);
64         BasicAuthorization testBasicAuthorization = new BasicAuthorization();
65         testBasicAuthorization.setUserName(testUserName);
66         testBasicAuthorization.setPassword(testUserPassword);
67         ClientCertificate testClientCertificate = new ClientCertificate();
68         testClientCertificate.setKeyStore(testKeyStore);
69         testClientCertificate.setKeyStorePassword(testKeyStorePassword);
70
71         HttpClientConfig httpClientConfig = new HttpClientConfig();
72         httpClientConfig.setNumOfRetries(testNumOfRetries);
73         httpClientConfig.setTimeouts(testTimeouts);
74         httpClientConfig.setBasicAuthorization(testBasicAuthorization);
75         httpClientConfig.setClientCertificate(testClientCertificate);
76         httpClientConfig.setRetryHandler(testRetryHandler);
77         httpClientConfig.setHeaders(testHeaders);
78
79         return new HttpClientConfigImmutable(httpClientConfig);
80     }
81 }