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