2 * ============LICENSE_START=======================================================
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 package org.onap.policy.controlloop.common.rules.test;
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;
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;
39 class HttpClientsTest {
40 private static final String CLIENT_NAME = "MY-CLIENT";
42 private final HttpClientFactory factory = mock(HttpClientFactory.class);
46 public static void setUpBeforeClass() {
47 SystemPersistenceConstants.getManager().setConfigurationDir("src/test/resources");
51 public void tearDown() {
52 HttpClientFactoryInstance.getClientFactory().destroy();
56 void test() throws HttpClientConfigException {
57 HttpClientFactoryInstance.getClientFactory().destroy();
59 var clients = new HttpClients();
61 clients.addClients("my");
63 // should find the client now
64 var client = HttpClientFactoryInstance.getClientFactory().get(CLIENT_NAME);
65 assertNotNull(client);
69 // destroyed - should NOT find the client
70 assertThatIllegalArgumentException()
71 .isThrownBy(() -> HttpClientFactoryInstance.getClientFactory().get(CLIENT_NAME));
73 // unknown property file
74 assertThatIllegalArgumentException().isThrownBy(() -> clients.addClients("unknown"));
76 // force exception from builder
77 var clients2 = new HttpClients() {
79 protected HttpClientFactory getClientFactory() {
84 when(factory.build(any(Properties.class))).thenThrow(new HttpClientConfigException("expected exception"));
86 assertThatIllegalArgumentException().isThrownBy(() -> clients2.addClients("my"))
87 .withMessage("cannot initialize HTTP clients");