2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2020 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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.policy.controlloop.common.rules.test;
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;
28 import java.util.Properties;
29 import org.junit.After;
30 import org.junit.BeforeClass;
31 import org.junit.Test;
32 import org.mockito.Mock;
33 import org.mockito.MockitoAnnotations;
34 import org.onap.policy.common.endpoints.http.client.HttpClient;
35 import org.onap.policy.common.endpoints.http.client.HttpClientConfigException;
36 import org.onap.policy.common.endpoints.http.client.HttpClientFactory;
37 import org.onap.policy.common.endpoints.http.client.HttpClientFactoryInstance;
38 import org.onap.policy.controlloop.common.rules.test.HttpClients;
39 import org.onap.policy.drools.persistence.SystemPersistenceConstants;
41 public class HttpClientsTest {
42 private static final String CLIENT_NAME = "MY-CLIENT";
45 private HttpClientFactory factory;
49 public static void setUpBeforeClass() {
50 SystemPersistenceConstants.getManager().setConfigurationDir("src/test/resources");
54 public void tearDown() {
55 HttpClientFactoryInstance.getClientFactory().destroy();
59 public void test() throws HttpClientConfigException {
60 MockitoAnnotations.initMocks(this);
62 HttpClientFactoryInstance.getClientFactory().destroy();
64 HttpClients clients = new HttpClients();
66 clients.addClients("my");
68 // should find the client now
69 HttpClient client = HttpClientFactoryInstance.getClientFactory().get(CLIENT_NAME);
70 assertNotNull(client);
74 // destroyed - should NOT find the client
75 assertThatIllegalArgumentException()
76 .isThrownBy(() -> HttpClientFactoryInstance.getClientFactory().get(CLIENT_NAME));
78 // unknown property file
79 assertThatIllegalArgumentException().isThrownBy(() -> clients.addClients("unknown"));
81 // force exception from builder
82 HttpClients clients2 = new HttpClients() {
84 protected HttpClientFactory getClientFactory() {
89 when(factory.build(any(Properties.class))).thenThrow(new HttpClientConfigException("expected exception"));
91 assertThatIllegalArgumentException().isThrownBy(() -> clients2.addClients("my"))
92 .withMessage("cannot initialize HTTP clients");