Merge "Fail component health check if PDP is down"
[policy/pap.git] / main / src / test / java / org / onap / policy / pap / main / rest / TestPdpGroupHealthCheckProvider.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019 Nordix Foundation.
4  *  Modifications Copyright (C) 2020 Bell Canada. All rights reserved.
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.mockito.ArgumentMatchers.any;
26 import static org.mockito.Mockito.mock;
27 import static org.mockito.Mockito.when;
28
29 import java.io.File;
30 import java.util.List;
31 import javax.ws.rs.core.Response;
32 import javax.ws.rs.core.Response.Status;
33 import org.apache.commons.lang3.tuple.Pair;
34 import org.junit.Before;
35 import org.junit.Test;
36 import org.mockito.Mock;
37 import org.mockito.MockitoAnnotations;
38 import org.onap.policy.common.utils.coder.Coder;
39 import org.onap.policy.common.utils.coder.CoderException;
40 import org.onap.policy.common.utils.coder.StandardCoder;
41 import org.onap.policy.common.utils.resources.ResourceUtils;
42 import org.onap.policy.common.utils.services.Registry;
43 import org.onap.policy.models.pdp.concepts.Pdp;
44 import org.onap.policy.models.pdp.concepts.PdpGroup;
45 import org.onap.policy.models.pdp.concepts.PdpGroups;
46 import org.onap.policy.models.pdp.concepts.PdpSubGroup;
47 import org.onap.policy.models.pdp.concepts.Pdps;
48 import org.onap.policy.models.provider.PolicyModelsProvider;
49 import org.onap.policy.pap.main.PapConstants;
50 import org.onap.policy.pap.main.PolicyModelsProviderFactoryWrapper;
51
52 /**
53  * Class to perform unit test of {@link PdpGroupHealthCheckProvider}.
54  *
55  * @author Ram Krishna Verma (ram.krishna.verma@est.tech)
56  */
57 public class TestPdpGroupHealthCheckProvider {
58
59     @Mock
60     private PolicyModelsProvider dao;
61     private PolicyModelsProviderFactoryWrapper daofact;
62     private List<PdpGroup> groups;
63     private Coder coder = new StandardCoder();
64
65     /**
66      * Configures DAO and mocks.
67      */
68     @Before
69     public void setUp() throws Exception {
70
71         Registry.newRegistry();
72         MockitoAnnotations.initMocks(this);
73         daofact = mock(PolicyModelsProviderFactoryWrapper.class);
74         when(daofact.create()).thenReturn(dao);
75
76         groups = loadFile("pdpGroup.json").getGroups();
77
78         when(dao.getPdpGroups(any())).thenReturn(groups);
79
80         Registry.register(PapConstants.REG_PAP_DAO_FACTORY, daofact);
81     }
82
83     @Test
84     public void testFetchPdpGroupHealthStatus() throws Exception {
85         final PdpGroupHealthCheckProvider provider = new PdpGroupHealthCheckProvider();
86         final Pair<Status, Pdps> pair = provider.fetchPdpGroupHealthStatus();
87         assertEquals(Response.Status.OK, pair.getLeft());
88         verifyPdps(pair.getRight().getPdpList(), groups);
89     }
90
91     private void verifyPdps(final List<Pdp> pdpList, final List<PdpGroup> groups) {
92         assertEquals(6, pdpList.size());
93         for (final PdpGroup group : groups) {
94             for (final PdpSubGroup subGroup : group.getPdpSubgroups()) {
95                 pdpList.containsAll(subGroup.getPdpInstances());
96             }
97         }
98     }
99
100     private PdpGroups loadFile(final String fileName) {
101         final File propFile = new File(ResourceUtils.getFilePath4Resource("rest/" + fileName));
102         try {
103             return coder.decode(propFile, PdpGroups.class);
104
105         } catch (final CoderException e) {
106             throw new RuntimeException(e);
107         }
108     }
109 }