Java 17 / Spring 6 / Spring Boot 3 Upgrade
[policy/pap.git] / main / src / test / java / org / onap / policy / pap / main / rest / TestPolicyComponentsHealthCheckProvider.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2020, 2022-2023 Nordix Foundation.
4  *  Modifications Copyright (C) 2020-2021 AT&T Corp.
5  *  Modifications Copyright (C) 2020-2022 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.jupiter.api.Assertions.assertEquals;
26 import static org.junit.jupiter.api.Assertions.assertFalse;
27 import static org.junit.jupiter.api.Assertions.assertTrue;
28 import static org.mockito.Mockito.when;
29
30 import jakarta.ws.rs.core.Response;
31 import java.io.File;
32 import java.net.HttpURLConnection;
33 import java.util.ArrayList;
34 import java.util.List;
35 import java.util.Map;
36 import org.apache.commons.lang3.tuple.Pair;
37 import org.junit.jupiter.api.AfterEach;
38 import org.junit.jupiter.api.BeforeEach;
39 import org.junit.jupiter.api.Test;
40 import org.mockito.Mock;
41 import org.mockito.MockitoAnnotations;
42 import org.onap.policy.common.endpoints.http.client.HttpClient;
43 import org.onap.policy.common.endpoints.http.client.HttpClientFactory;
44 import org.onap.policy.common.endpoints.report.HealthCheckReport;
45 import org.onap.policy.common.parameters.ParameterService;
46 import org.onap.policy.common.utils.coder.Coder;
47 import org.onap.policy.common.utils.coder.CoderException;
48 import org.onap.policy.common.utils.coder.StandardCoder;
49 import org.onap.policy.common.utils.resources.ResourceUtils;
50 import org.onap.policy.common.utils.services.Registry;
51 import org.onap.policy.models.pdp.concepts.Pdp;
52 import org.onap.policy.models.pdp.concepts.PdpGroup;
53 import org.onap.policy.models.pdp.concepts.PdpGroups;
54 import org.onap.policy.models.pdp.enums.PdpHealthStatus;
55 import org.onap.policy.pap.main.PapConstants;
56 import org.onap.policy.pap.main.parameters.CommonTestData;
57 import org.onap.policy.pap.main.parameters.PapParameterGroup;
58 import org.onap.policy.pap.main.service.PdpGroupService;
59 import org.onap.policy.pap.main.startstop.PapActivator;
60 import org.springframework.http.HttpStatus;
61 import org.springframework.test.util.ReflectionTestUtils;
62
63 class TestPolicyComponentsHealthCheckProvider {
64
65     private static final String CLIENT_1 = "client1";
66     private static final String PDP_GROUP_DATA_FILE = "rest/pdpGroup.json";
67     private static final String PAP_GROUP_PARAMS_NAME = "PapGroup";
68     private static final String HEALTHY = "healthy";
69
70     @Mock
71     private PdpGroupService pdpGroupService;
72
73     @Mock
74     private HttpClientFactory clientFactory;
75
76     @Mock
77     PapActivator papActivator;
78
79     @Mock
80     private HttpClient client1;
81
82     @Mock
83     private HttpClient client2;
84
85     @Mock
86     private HttpClient client3;
87
88     @Mock
89     private Response response1;
90
91     @Mock
92     private Response response2;
93
94     @Mock
95     private Response response3;
96
97     private List<PdpGroup> groups;
98
99     private PapParameterGroup savedPapParameterGroup;
100
101     private PolicyComponentsHealthCheckProvider provider;
102
103     AutoCloseable autoCloseable;
104
105     /**
106      * Configures mocks and objects.
107      *
108      * @throws Exception if an error occurs
109      */
110     @BeforeEach
111     public void setUp() throws Exception {
112         autoCloseable = MockitoAnnotations.openMocks(this);
113         groups = loadPdpGroupsFromFile().getGroups();
114         when(pdpGroupService.getPdpGroups()).thenReturn(groups);
115
116         Registry.newRegistry();
117
118         when(papActivator.isAlive()).thenReturn(true);
119         Registry.register(PapConstants.REG_PAP_ACTIVATOR, papActivator);
120
121         if (ParameterService.contains(PAP_GROUP_PARAMS_NAME)) {
122             savedPapParameterGroup = ParameterService.get(PAP_GROUP_PARAMS_NAME);
123         }
124         CommonTestData testData = new CommonTestData();
125         ParameterService.register(testData.getPapParameterGroup(0), true);
126
127         when(client1.getName()).thenReturn(CLIENT_1);
128         when(client1.getBaseUrl()).thenReturn("url1");
129         when(response1.getStatus()).thenReturn(HttpURLConnection.HTTP_OK);
130         when(response1.readEntity(HealthCheckReport.class)).thenReturn(createReport(HttpURLConnection.HTTP_OK, true));
131         when(client1.get()).thenReturn(response1);
132
133         when(client2.getName()).thenReturn("client2");
134         when(client2.getBaseUrl()).thenReturn("url2");
135         when(response2.getStatus()).thenReturn(HttpURLConnection.HTTP_OK);
136         when(response2.readEntity(HealthCheckReport.class)).thenReturn(createReport(HttpURLConnection.HTTP_OK, true));
137         when(client2.get()).thenReturn(response2);
138
139         when(client3.getName()).thenReturn("dmaap");
140         when(client3.getBaseUrl()).thenReturn("message-router");
141         when(response3.getStatus()).thenReturn(HttpURLConnection.HTTP_OK);
142         when(response3.readEntity(DmaapGetTopicResponse.class)).thenReturn(createDmaapResponse());
143         when(client3.get()).thenReturn(response3);
144         List<HttpClient> clients = new ArrayList<>();
145         clients.add(client1);
146         clients.add(client2);
147         clients.add(client3);
148         PapParameterGroup papParameterGroup = ParameterService.get(PAP_GROUP_PARAMS_NAME);
149         provider = new PolicyComponentsHealthCheckProvider(papParameterGroup, pdpGroupService);
150         ReflectionTestUtils.setField(provider, "papParameterGroup", papParameterGroup);
151         provider.initializeClientHealthCheckExecutorService();
152         ReflectionTestUtils.setField(provider, "clients", clients);
153         ReflectionTestUtils.setField(provider, "topicPolicyPdpPap", "POLICY-PDP-PAP");
154     }
155
156     /**
157      * Tear down.
158      */
159     @AfterEach
160     public void tearDown() throws Exception {
161         if (savedPapParameterGroup != null) {
162             ParameterService.register(savedPapParameterGroup, true);
163         } else {
164             ParameterService.deregister(PAP_GROUP_PARAMS_NAME);
165         }
166         provider.cleanup();
167         autoCloseable.close();
168     }
169
170
171     @Test
172     void testFetchPolicyComponentsHealthStatus_allHealthy() {
173         Pair<HttpStatus, Map<String, Object>> ret = provider.fetchPolicyComponentsHealthStatus();
174         assertEquals(HttpStatus.OK, ret.getLeft());
175         assertTrue((Boolean) ret.getRight().get(HEALTHY));
176     }
177
178     @Test
179     void testFetchPolicyComponentsHealthStatus_unhealthyClient() {
180         when(response1.getStatus()).thenReturn(HttpURLConnection.HTTP_INTERNAL_ERROR);
181         when(response1.readEntity(HealthCheckReport.class))
182             .thenReturn(createReport(HttpURLConnection.HTTP_INTERNAL_ERROR, false));
183         Map<String, Object> result = callFetchPolicyComponentsHealthStatus();
184         assertFalse((Boolean) result.get(HEALTHY));
185         HealthCheckReport report = (HealthCheckReport) result.get(CLIENT_1);
186         assertFalse(report.isHealthy());
187
188         when(response1.getStatus()).thenReturn(HttpURLConnection.HTTP_OK);
189         when(response1.readEntity(HealthCheckReport.class)).thenReturn(createReport(HttpURLConnection.HTTP_OK, false));
190         Map<String, Object> result2 = callFetchPolicyComponentsHealthStatus();
191         assertFalse((Boolean) result2.get(HEALTHY));
192         HealthCheckReport report2 = (HealthCheckReport) result.get(CLIENT_1);
193         assertFalse(report2.isHealthy());
194
195         when(response3.getStatus()).thenReturn(HttpURLConnection.HTTP_INTERNAL_ERROR);
196         when(response3.readEntity(DmaapGetTopicResponse.class)).thenReturn(null);
197         Map<String, Object> result3 = callFetchPolicyComponentsHealthStatus();
198         assertFalse((Boolean) result3.get(HEALTHY));
199         HealthCheckReport report3 = (HealthCheckReport) result3.get("dmaap");
200         assertFalse(report3.isHealthy());
201
202         when(response3.getStatus()).thenReturn(HttpURLConnection.HTTP_OK);
203         when(response3.readEntity(DmaapGetTopicResponse.class)).thenReturn(new DmaapGetTopicResponse());
204         Map<String, Object> result4 = callFetchPolicyComponentsHealthStatus();
205         assertFalse((Boolean) result4.get(HEALTHY));
206         HealthCheckReport report4 = (HealthCheckReport) result4.get("dmaap");
207         assertFalse(report4.isHealthy());
208     }
209
210     @SuppressWarnings("unchecked")
211     @Test
212     void testFetchPolicyComponentsHealthStatus_unhealthyPdps() {
213         // Get a PDP and set it unhealthy
214         groups.get(0).getPdpSubgroups().get(0).getPdpInstances().get(0).setHealthy(PdpHealthStatus.NOT_HEALTHY);
215         Map<String, Object> result = callFetchPolicyComponentsHealthStatus();
216         Map<String, List<Pdp>> pdpListWithType = (Map<String, List<Pdp>>) result.get(PapConstants.POLICY_PDPS);
217         assertEquals(2, pdpListWithType.size());
218         assertFalse((Boolean) result.get(HEALTHY));
219     }
220
221     @Test
222     void testFetchPolicyComponentsHealthStatus_PdpDown() {
223         // Set currentInstanceCount as 0 to simulate PDP down
224         groups.get(0).getPdpSubgroups().get(0).setCurrentInstanceCount(0);
225         Map<String, Object> result = callFetchPolicyComponentsHealthStatus();
226         assertFalse((Boolean) result.get(HEALTHY));
227     }
228
229     @Test
230     void testFetchPolicyComponentsHealthStatus_unhealthyPap() {
231         when(papActivator.isAlive()).thenReturn(false);
232         Map<String, Object> result = callFetchPolicyComponentsHealthStatus();
233         assertFalse((Boolean) result.get(HEALTHY));
234         HealthCheckReport report = (HealthCheckReport) result.get(PapConstants.POLICY_PAP);
235         assertFalse(report.isHealthy());
236     }
237
238     private Map<String, Object> callFetchPolicyComponentsHealthStatus() {
239
240         return provider.fetchPolicyComponentsHealthStatus().getRight();
241     }
242
243     private HealthCheckReport createReport(int code, boolean healthy) {
244         HealthCheckReport report = new HealthCheckReport();
245         report.setName("name");
246         report.setUrl("url");
247         report.setCode(code);
248         report.setHealthy(healthy);
249         report.setMessage("message");
250         return report;
251     }
252
253     private static PdpGroups loadPdpGroupsFromFile() {
254         final File propFile = new File(ResourceUtils.getFilePath4Resource(PDP_GROUP_DATA_FILE));
255         try {
256             Coder coder = new StandardCoder();
257             return coder.decode(propFile, PdpGroups.class);
258         } catch (final CoderException e) {
259             throw new RuntimeException(e);
260         }
261     }
262
263     private DmaapGetTopicResponse createDmaapResponse() {
264         DmaapGetTopicResponse response = new DmaapGetTopicResponse();
265         response.setTopics(List.of("POLICY-PDP-PAP"));
266         return response;
267     }
268 }