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