Upgrade Java 17 in policy-drools-apps
[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  * Modifications Copyright (C) 2023 Nordix Foundation.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.controlloop.common.rules.test;
23
24 import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
25 import static org.junit.jupiter.api.Assertions.assertNotNull;
26 import static org.mockito.ArgumentMatchers.any;
27 import static org.mockito.Mockito.mock;
28 import static org.mockito.Mockito.when;
29
30 import java.util.Properties;
31 import org.junit.jupiter.api.AfterEach;
32 import org.junit.jupiter.api.BeforeAll;
33 import org.junit.jupiter.api.Test;
34 import org.onap.policy.common.endpoints.http.client.HttpClientConfigException;
35 import org.onap.policy.common.endpoints.http.client.HttpClientFactory;
36 import org.onap.policy.common.endpoints.http.client.HttpClientFactoryInstance;
37 import org.onap.policy.drools.persistence.SystemPersistenceConstants;
38
39 class HttpClientsTest {
40     private static final String CLIENT_NAME = "MY-CLIENT";
41
42     private final HttpClientFactory factory = mock(HttpClientFactory.class);
43
44
45     @BeforeAll
46     public static void setUpBeforeClass() {
47         SystemPersistenceConstants.getManager().setConfigurationDir("src/test/resources");
48     }
49
50     @AfterEach
51     public void tearDown() {
52         HttpClientFactoryInstance.getClientFactory().destroy();
53     }
54
55     @Test
56     void test() throws HttpClientConfigException {
57         HttpClientFactoryInstance.getClientFactory().destroy();
58
59         var clients = new HttpClients();
60
61         clients.addClients("my");
62
63         // should find the client now
64         var client = HttpClientFactoryInstance.getClientFactory().get(CLIENT_NAME);
65         assertNotNull(client);
66
67         clients.destroy();
68
69         // destroyed - should NOT find the client
70         assertThatIllegalArgumentException()
71                         .isThrownBy(() -> HttpClientFactoryInstance.getClientFactory().get(CLIENT_NAME));
72
73         // unknown property file
74         assertThatIllegalArgumentException().isThrownBy(() -> clients.addClients("unknown"));
75
76         // force exception from builder
77         var clients2 = new HttpClients() {
78             @Override
79             protected HttpClientFactory getClientFactory() {
80                 return factory;
81             }
82         };
83
84         when(factory.build(any(Properties.class))).thenThrow(new HttpClientConfigException("expected exception"));
85
86         assertThatIllegalArgumentException().isThrownBy(() -> clients2.addClients("my"))
87                         .withMessage("cannot initialize HTTP clients");
88     }
89 }