dac10df346b52e3ac1ec32bbc646404cb60acbd2
[policy/drools-applications.git] / controlloop / common / rules-test / src / test / java / org / onap / policy / controlloop / common / rules / test / HttpClientsTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP
4  * ================================================================================
5  * Copyright (C) 2020-2021 AT&T Intellectual Property. 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.onap.policy.controlloop.common.rules.test;
22
23 import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
24 import static org.junit.Assert.assertNotNull;
25 import static org.mockito.ArgumentMatchers.any;
26 import static org.mockito.Mockito.when;
27
28 import java.util.Properties;
29 import org.junit.After;
30 import org.junit.BeforeClass;
31 import org.junit.Test;
32 import org.junit.runner.RunWith;
33 import org.mockito.Mock;
34 import org.mockito.junit.MockitoJUnitRunner;
35 import org.onap.policy.common.endpoints.http.client.HttpClient;
36 import org.onap.policy.common.endpoints.http.client.HttpClientConfigException;
37 import org.onap.policy.common.endpoints.http.client.HttpClientFactory;
38 import org.onap.policy.common.endpoints.http.client.HttpClientFactoryInstance;
39 import org.onap.policy.drools.persistence.SystemPersistenceConstants;
40
41 @RunWith(MockitoJUnitRunner.class)
42 public class HttpClientsTest {
43     private static final String CLIENT_NAME = "MY-CLIENT";
44
45     @Mock
46     private HttpClientFactory factory;
47
48
49     @BeforeClass
50     public static void setUpBeforeClass() {
51         SystemPersistenceConstants.getManager().setConfigurationDir("src/test/resources");
52     }
53
54     @After
55     public void tearDown() {
56         HttpClientFactoryInstance.getClientFactory().destroy();
57     }
58
59     @Test
60     public void test() throws HttpClientConfigException {
61         HttpClientFactoryInstance.getClientFactory().destroy();
62
63         HttpClients clients = new HttpClients();
64
65         clients.addClients("my");
66
67         // should find the client now
68         HttpClient client = HttpClientFactoryInstance.getClientFactory().get(CLIENT_NAME);
69         assertNotNull(client);
70
71         clients.destroy();
72
73         // destroyed - should NOT find the client
74         assertThatIllegalArgumentException()
75                         .isThrownBy(() -> HttpClientFactoryInstance.getClientFactory().get(CLIENT_NAME));
76
77         // unknown property file
78         assertThatIllegalArgumentException().isThrownBy(() -> clients.addClients("unknown"));
79
80         // force exception from builder
81         HttpClients clients2 = new HttpClients() {
82             @Override
83             protected HttpClientFactory getClientFactory() {
84                 return factory;
85             }
86         };
87
88         when(factory.build(any(Properties.class))).thenThrow(new HttpClientConfigException("expected exception"));
89
90         assertThatIllegalArgumentException().isThrownBy(() -> clients2.addClients("my"))
91                         .withMessage("cannot initialize HTTP clients");
92     }
93 }