2  * ============LICENSE_START=======================================================
 
   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
 
  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.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;
 
  41 @RunWith(MockitoJUnitRunner.class)
 
  42 public class HttpClientsTest {
 
  43     private static final String CLIENT_NAME = "MY-CLIENT";
 
  46     private HttpClientFactory factory;
 
  50     public static void setUpBeforeClass() {
 
  51         SystemPersistenceConstants.getManager().setConfigurationDir("src/test/resources");
 
  55     public void tearDown() {
 
  56         HttpClientFactoryInstance.getClientFactory().destroy();
 
  60     public void test() throws HttpClientConfigException {
 
  61         HttpClientFactoryInstance.getClientFactory().destroy();
 
  63         HttpClients clients = new HttpClients();
 
  65         clients.addClients("my");
 
  67         // should find the client now
 
  68         HttpClient client = HttpClientFactoryInstance.getClientFactory().get(CLIENT_NAME);
 
  69         assertNotNull(client);
 
  73         // destroyed - should NOT find the client
 
  74         assertThatIllegalArgumentException()
 
  75                         .isThrownBy(() -> HttpClientFactoryInstance.getClientFactory().get(CLIENT_NAME));
 
  77         // unknown property file
 
  78         assertThatIllegalArgumentException().isThrownBy(() -> clients.addClients("unknown"));
 
  80         // force exception from builder
 
  81         HttpClients clients2 = new HttpClients() {
 
  83             protected HttpClientFactory getClientFactory() {
 
  88         when(factory.build(any(Properties.class))).thenThrow(new HttpClientConfigException("expected exception"));
 
  90         assertThatIllegalArgumentException().isThrownBy(() -> clients2.addClients("my"))
 
  91                         .withMessage("cannot initialize HTTP clients");