Use new RestClientParameters class in PAP
[policy/pap.git] / main / src / test / java / org / onap / policy / pap / main / rest / TestPolicyComponentsHealthCheckProvider.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2020 Nordix Foundation.
4  *  Modifications Copyright (C) 2020-2021 AT&T Corp.
5  *  Modifications Copyright (C) 2020 Bell Canada. 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  *
19  * SPDX-License-Identifier: Apache-2.0
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.policy.pap.main.rest;
24
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertFalse;
27 import static org.junit.Assert.assertTrue;
28 import static org.mockito.ArgumentMatchers.any;
29 import static org.mockito.Mockito.when;
30
31 import java.io.File;
32 import java.net.HttpURLConnection;
33 import java.util.List;
34 import java.util.Map;
35 import javax.ws.rs.core.Response;
36 import javax.ws.rs.core.Response.Status;
37 import org.apache.commons.lang3.tuple.Pair;
38 import org.junit.After;
39 import org.junit.Before;
40 import org.junit.Test;
41 import org.junit.runner.RunWith;
42 import org.mockito.Mock;
43 import org.mockito.junit.MockitoJUnitRunner;
44 import org.onap.policy.common.endpoints.http.client.HttpClient;
45 import org.onap.policy.common.endpoints.http.client.HttpClientFactory;
46 import org.onap.policy.common.endpoints.parameters.RestClientParameters;
47 import org.onap.policy.common.endpoints.report.HealthCheckReport;
48 import org.onap.policy.common.parameters.ParameterService;
49 import org.onap.policy.common.utils.coder.Coder;
50 import org.onap.policy.common.utils.coder.CoderException;
51 import org.onap.policy.common.utils.coder.StandardCoder;
52 import org.onap.policy.common.utils.resources.ResourceUtils;
53 import org.onap.policy.common.utils.services.Registry;
54 import org.onap.policy.models.pdp.concepts.Pdp;
55 import org.onap.policy.models.pdp.concepts.PdpGroup;
56 import org.onap.policy.models.pdp.concepts.PdpGroups;
57 import org.onap.policy.models.pdp.enums.PdpHealthStatus;
58 import org.onap.policy.models.provider.PolicyModelsProvider;
59 import org.onap.policy.pap.main.PapConstants;
60 import org.onap.policy.pap.main.PolicyModelsProviderFactoryWrapper;
61 import org.onap.policy.pap.main.parameters.CommonTestData;
62 import org.onap.policy.pap.main.parameters.PapParameterGroup;
63 import org.onap.policy.pap.main.startstop.PapActivator;
64
65 @RunWith(MockitoJUnitRunner.class)
66 public class TestPolicyComponentsHealthCheckProvider {
67
68     private static final String CLIENT_1 = "client1";
69     private static final String PDP_GROUP_DATA_FILE = "rest/pdpGroup.json";
70     private static final String PAP_GROUP_PARAMS_NAME = "PapGroup";
71     private static final String HEALTHY = "healthy";
72
73     @Mock
74     private PolicyModelsProvider dao;
75
76     @Mock
77     private PolicyModelsProviderFactoryWrapper daofact;
78
79     @Mock
80     private HttpClientFactory clientFactory;
81
82     @Mock
83     PapActivator papActivator;
84
85     @Mock
86     private HttpClient client1;
87
88     @Mock
89     private HttpClient client2;
90
91     @Mock
92     private Response response1;
93
94     @Mock
95     private Response response2;
96
97     private List<PdpGroup> groups;
98
99     private PapParameterGroup savedPapParameterGroup;
100
101     /**
102      * Configures mocks and objects.
103      *
104      * @throws Exception if an error occurs
105      */
106     @Before
107     public void setUp() throws Exception {
108         groups = loadPdpGroupsFromFile().getGroups();
109         when(dao.getPdpGroups(any())).thenReturn(groups);
110
111         when(daofact.create()).thenReturn(dao);
112         Registry.newRegistry();
113         Registry.register(PapConstants.REG_PAP_DAO_FACTORY, daofact);
114
115         when(papActivator.isAlive()).thenReturn(true);
116         Registry.register(PapConstants.REG_PAP_ACTIVATOR, papActivator);
117
118         if (ParameterService.contains(PAP_GROUP_PARAMS_NAME)) {
119             savedPapParameterGroup = ParameterService.get(PAP_GROUP_PARAMS_NAME);
120         }
121         CommonTestData testData = new CommonTestData();
122         ParameterService.register(testData.getPapParameterGroup(0), true);
123
124         when(client1.getName()).thenReturn(CLIENT_1);
125         when(client1.getBaseUrl()).thenReturn("url1");
126         when(response1.getStatus()).thenReturn(HttpURLConnection.HTTP_OK);
127         when(response1.readEntity(HealthCheckReport.class)).thenReturn(createReport(HttpURLConnection.HTTP_OK, true));
128         when(client1.get()).thenReturn(response1);
129
130         when(client2.getName()).thenReturn("client2");
131         when(client2.getBaseUrl()).thenReturn("url2");
132         when(response2.getStatus()).thenReturn(HttpURLConnection.HTTP_OK);
133         when(response2.readEntity(HealthCheckReport.class)).thenReturn(createReport(HttpURLConnection.HTTP_OK, true));
134         when(client2.get()).thenReturn(response2);
135
136         PapParameterGroup papParameterGroup = ParameterService.get(PAP_GROUP_PARAMS_NAME);
137         List<RestClientParameters> params = papParameterGroup.getHealthCheckRestClientParameters();
138         when(clientFactory.build(params.get(0))).thenReturn(client1);
139         when(clientFactory.build(params.get(1))).thenReturn(client2);
140
141         PolicyComponentsHealthCheckProvider.initializeClientHealthCheckExecutorService(papParameterGroup,
142             clientFactory);
143     }
144
145     /**
146      * Tear down.
147      */
148     @After
149     public void tearDown() {
150         if (savedPapParameterGroup != null) {
151             ParameterService.register(savedPapParameterGroup, true);
152         } else {
153             ParameterService.deregister(PAP_GROUP_PARAMS_NAME);
154         }
155         PolicyComponentsHealthCheckProvider.cleanup();
156     }
157
158
159     @Test
160     public void testFetchPolicyComponentsHealthStatus_allHealthy() {
161         PolicyComponentsHealthCheckProvider provider = new PolicyComponentsHealthCheckProvider();
162         Pair<Status, Map<String, Object>> ret = provider.fetchPolicyComponentsHealthStatus();
163         assertEquals(Response.Status.OK, ret.getLeft());
164         assertTrue((Boolean) ret.getRight().get(HEALTHY));
165     }
166
167     @Test
168     public void testFetchPolicyComponentsHealthStatus_unhealthyClient() {
169         when(response1.getStatus()).thenReturn(HttpURLConnection.HTTP_INTERNAL_ERROR);
170         when(response1.readEntity(HealthCheckReport.class))
171             .thenReturn(createReport(HttpURLConnection.HTTP_INTERNAL_ERROR, false));
172         Map<String, Object> result = callFetchPolicyComponentsHealthStatus();
173         assertFalse((Boolean) result.get(HEALTHY));
174         HealthCheckReport report = (HealthCheckReport) result.get(CLIENT_1);
175         assertFalse(report.isHealthy());
176
177         when(response1.getStatus()).thenReturn(HttpURLConnection.HTTP_OK);
178         when(response1.readEntity(HealthCheckReport.class)).thenReturn(createReport(HttpURLConnection.HTTP_OK, false));
179         Map<String, Object> result2 = callFetchPolicyComponentsHealthStatus();
180         assertFalse((Boolean) result2.get(HEALTHY));
181         HealthCheckReport report2 = (HealthCheckReport) result.get(CLIENT_1);
182         assertFalse(report2.isHealthy());
183     }
184
185     @SuppressWarnings("unchecked")
186     @Test
187     public void testFetchPolicyComponentsHealthStatus_unhealthyPdps() {
188         // Get a PDP and set it unhealthy
189         groups.get(0).getPdpSubgroups().get(0).getPdpInstances().get(0).setHealthy(PdpHealthStatus.NOT_HEALTHY);
190         Map<String, Object> result = callFetchPolicyComponentsHealthStatus();
191         Map<String, List<Pdp>> pdpListWithType = (Map<String, List<Pdp>>) result.get(PapConstants.POLICY_PDPS);
192         assertEquals(2, pdpListWithType.size());
193         assertFalse((Boolean) result.get(HEALTHY));
194     }
195
196     @Test
197     public void testFetchPolicyComponentsHealthStatus_PdpDown() {
198         // Set currentInstanceCount as 0 to simulate PDP down
199         groups.get(0).getPdpSubgroups().get(0).setCurrentInstanceCount(0);
200         Map<String, Object> result = callFetchPolicyComponentsHealthStatus();
201         assertFalse((Boolean) result.get(HEALTHY));
202     }
203
204     @Test
205     public void testFetchPolicyComponentsHealthStatus_unhealthyPap() {
206         when(papActivator.isAlive()).thenReturn(false);
207         Map<String, Object> result = callFetchPolicyComponentsHealthStatus();
208         assertFalse((Boolean) result.get(HEALTHY));
209         HealthCheckReport report = (HealthCheckReport) result.get(PapConstants.POLICY_PAP);
210         assertFalse(report.isHealthy());
211     }
212
213     private Map<String, Object> callFetchPolicyComponentsHealthStatus() {
214         PolicyComponentsHealthCheckProvider provider = new PolicyComponentsHealthCheckProvider();
215         return provider.fetchPolicyComponentsHealthStatus().getRight();
216     }
217
218     private HealthCheckReport createReport(int code, boolean healthy) {
219         HealthCheckReport report = new HealthCheckReport();
220         report.setName("name");
221         report.setUrl("url");
222         report.setCode(code);
223         report.setHealthy(healthy);
224         report.setMessage("message");
225         return report;
226     }
227
228     private static PdpGroups loadPdpGroupsFromFile() {
229         final File propFile = new File(ResourceUtils.getFilePath4Resource(PDP_GROUP_DATA_FILE));
230         try {
231             Coder coder = new StandardCoder();
232             return coder.decode(propFile, PdpGroups.class);
233         } catch (final CoderException e) {
234             throw new RuntimeException(e);
235         }
236     }
237 }