Point to latest snapshots
[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 AT&T Corp.
5  * ================================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.pap.main.rest;
23
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertFalse;
26 import static org.junit.Assert.assertTrue;
27 import static org.mockito.ArgumentMatchers.any;
28 import static org.mockito.Mockito.when;
29
30 import java.io.File;
31 import java.net.HttpURLConnection;
32 import java.util.List;
33 import java.util.Map;
34 import javax.ws.rs.core.Response;
35 import javax.ws.rs.core.Response.Status;
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.mockito.Mock;
41 import org.mockito.MockitoAnnotations;
42 import org.onap.policy.common.endpoints.event.comm.bus.internal.BusTopicParams;
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.models.provider.PolicyModelsProvider;
57 import org.onap.policy.pap.main.PapConstants;
58 import org.onap.policy.pap.main.PolicyModelsProviderFactoryWrapper;
59 import org.onap.policy.pap.main.parameters.CommonTestData;
60 import org.onap.policy.pap.main.parameters.PapParameterGroup;
61 import org.onap.policy.pap.main.startstop.PapActivator;
62
63 public 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
69     @Mock
70     private PolicyModelsProvider dao;
71
72     @Mock
73     private PolicyModelsProviderFactoryWrapper daofact;
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 Response response1;
89
90     @Mock
91     private Response response2;
92
93     private List<PdpGroup> groups;
94
95     private PapParameterGroup savedPapParameterGroup;
96
97     /**
98      * Configures mocks and objects.
99      *
100      * @throws Exception if an error occurs
101      */
102     @Before
103     public void setUp() throws Exception {
104         MockitoAnnotations.initMocks(this);
105
106         groups = loadPdpGroupsFromFile().getGroups();
107         when(dao.getPdpGroups(any())).thenReturn(groups);
108
109         when(daofact.create()).thenReturn(dao);
110         Registry.newRegistry();
111         Registry.register(PapConstants.REG_PAP_DAO_FACTORY, daofact);
112
113         when(papActivator.isAlive()).thenReturn(true);
114         Registry.register(PapConstants.REG_PAP_ACTIVATOR, papActivator);
115
116         if (ParameterService.contains(PAP_GROUP_PARAMS_NAME)) {
117             savedPapParameterGroup = ParameterService.get(PAP_GROUP_PARAMS_NAME);
118         }
119         CommonTestData testData = new CommonTestData();
120         ParameterService.register(testData.getPapParameterGroup(0), true);
121
122         when(client1.getName()).thenReturn(CLIENT_1);
123         when(client1.getBaseUrl()).thenReturn("url1");
124         when(response1.getStatus()).thenReturn(HttpURLConnection.HTTP_OK);
125         when(response1.readEntity(HealthCheckReport.class)).thenReturn(createReport(HttpURLConnection.HTTP_OK, true));
126         when(client1.get()).thenReturn(response1);
127
128         when(client2.getName()).thenReturn("client2");
129         when(client2.getBaseUrl()).thenReturn("url2");
130         when(response2.getStatus()).thenReturn(HttpURLConnection.HTTP_OK);
131         when(response2.readEntity(HealthCheckReport.class)).thenReturn(createReport(HttpURLConnection.HTTP_OK, true));
132         when(client2.get()).thenReturn(response2);
133
134         PapParameterGroup papParameterGroup = ParameterService.get(PAP_GROUP_PARAMS_NAME);
135         List<BusTopicParams> params = papParameterGroup.getHealthCheckRestClientParameters();
136         when(clientFactory.build(params.get(0))).thenReturn(client1);
137         when(clientFactory.build(params.get(1))).thenReturn(client2);
138     }
139
140     /**
141      * Tear down.
142      */
143     @After
144     public void tearDown() {
145         if (savedPapParameterGroup != null) {
146             ParameterService.register(savedPapParameterGroup, true);
147         } else {
148             ParameterService.deregister(PAP_GROUP_PARAMS_NAME);
149         }
150     }
151
152     @Test
153     public void testFetchPolicyComponentsHealthStatus_allHealthy() throws Exception {
154         PolicyComponentsHealthCheckProvider provider = new PolicyComponentsHealthCheckProvider(clientFactory);
155         Pair<Status, Map<String, Object>> ret = provider.fetchPolicyComponentsHealthStatus();
156         assertEquals(Response.Status.OK, ret.getLeft());
157         assertTrue((Boolean) ret.getRight().get("healthy"));
158     }
159
160     @Test
161     public void testFetchPolicyComponentsHealthStatus_unhealthyClient() throws Exception {
162         when(response1.getStatus()).thenReturn(HttpURLConnection.HTTP_INTERNAL_ERROR);
163         when(response1.readEntity(HealthCheckReport.class))
164                 .thenReturn(createReport(HttpURLConnection.HTTP_INTERNAL_ERROR, false));
165         Map<String, Object> result = callFetchPolicyComponentsHealthStatus();
166         assertFalse((Boolean) result.get("healthy"));
167         HealthCheckReport report = (HealthCheckReport) result.get(CLIENT_1);
168         assertFalse(report.isHealthy());
169
170         when(response1.getStatus()).thenReturn(HttpURLConnection.HTTP_OK);
171         when(response1.readEntity(HealthCheckReport.class)).thenReturn(createReport(HttpURLConnection.HTTP_OK, false));
172         Map<String, Object> result2 = callFetchPolicyComponentsHealthStatus();
173         assertFalse((Boolean) result2.get("healthy"));
174         HealthCheckReport report2 = (HealthCheckReport) result.get(CLIENT_1);
175         assertFalse(report2.isHealthy());
176     }
177
178     @SuppressWarnings("unchecked")
179     @Test
180     public void testFetchPolicyComponentsHealthStatus_unhealthyPdps() throws Exception {
181         // Get a PDP and set it unhealthy
182         groups.get(0).getPdpSubgroups().get(0).getPdpInstances().get(0).setHealthy(PdpHealthStatus.NOT_HEALTHY);
183         Map<String, Object> result = callFetchPolicyComponentsHealthStatus();
184         Map<String, List<Pdp>> pdpListWithType = (Map<String, List<Pdp>>) result.get(PapConstants.POLICY_PDPS);
185         assertEquals(2, pdpListWithType.size());
186         assertFalse((Boolean) result.get("healthy"));
187     }
188
189     @Test
190     public void testFetchPolicyComponentsHealthStatus_unhealthyPap() throws Exception {
191         when(papActivator.isAlive()).thenReturn(false);
192         Map<String, Object> result = callFetchPolicyComponentsHealthStatus();
193         assertFalse((Boolean) result.get("healthy"));
194         HealthCheckReport report = (HealthCheckReport) result.get(PapConstants.POLICY_PAP);
195         assertFalse(report.isHealthy());
196     }
197
198     private Map<String, Object> callFetchPolicyComponentsHealthStatus() throws Exception {
199         PolicyComponentsHealthCheckProvider provider = new PolicyComponentsHealthCheckProvider(clientFactory);
200         return provider.fetchPolicyComponentsHealthStatus().getRight();
201     }
202
203     private HealthCheckReport createReport(int code, boolean healthy) {
204         HealthCheckReport report = new HealthCheckReport();
205         report.setName("name");
206         report.setUrl("url");
207         report.setCode(code);
208         report.setHealthy(healthy);
209         report.setMessage("message");
210         return report;
211     }
212
213     private static PdpGroups loadPdpGroupsFromFile() {
214         final File propFile = new File(ResourceUtils.getFilePath4Resource(PDP_GROUP_DATA_FILE));
215         try {
216             Coder coder = new StandardCoder();
217             return coder.decode(propFile, PdpGroups.class);
218         } catch (final CoderException e) {
219             throw new RuntimeException(e);
220         }
221     }
222 }