5db8353439268c9fcfc057a4b5d711dc82f65ce6
[policy/drools-applications.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP
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
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.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;
40
41 public class HttpClientsTest {
42     private static final String CLIENT_NAME = "MY-CLIENT";
43
44     @Mock
45     private HttpClientFactory factory;
46
47
48     @BeforeClass
49     public static void setUpBeforeClass() {
50         SystemPersistenceConstants.getManager().setConfigurationDir("src/test/resources");
51     }
52
53     @After
54     public void tearDown() {
55         HttpClientFactoryInstance.getClientFactory().destroy();
56     }
57
58     @Test
59     public void test() throws HttpClientConfigException {
60         MockitoAnnotations.initMocks(this);
61
62         HttpClientFactoryInstance.getClientFactory().destroy();
63
64         HttpClients clients = new HttpClients();
65
66         clients.addClients("my");
67
68         // should find the client now
69         HttpClient client = HttpClientFactoryInstance.getClientFactory().get(CLIENT_NAME);
70         assertNotNull(client);
71
72         clients.destroy();
73
74         // destroyed - should NOT find the client
75         assertThatIllegalArgumentException()
76                         .isThrownBy(() -> HttpClientFactoryInstance.getClientFactory().get(CLIENT_NAME));
77
78         // unknown property file
79         assertThatIllegalArgumentException().isThrownBy(() -> clients.addClients("unknown"));
80
81         // force exception from builder
82         HttpClients clients2 = new HttpClients() {
83             @Override
84             protected HttpClientFactory getClientFactory() {
85                 return factory;
86             }
87         };
88
89         when(factory.build(any(Properties.class))).thenThrow(new HttpClientConfigException("expected exception"));
90
91         assertThatIllegalArgumentException().isThrownBy(() -> clients2.addClients("my"))
92                         .withMessage("cannot initialize HTTP clients");
93     }
94 }