Changes for Checkstyle 8.32 in policy-pap
[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  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.pap.main.rest;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.mockito.ArgumentMatchers.any;
25 import static org.mockito.Mockito.mock;
26 import static org.mockito.Mockito.when;
27
28 import java.io.File;
29 import java.util.List;
30 import javax.ws.rs.core.Response;
31 import javax.ws.rs.core.Response.Status;
32 import org.apache.commons.lang3.tuple.Pair;
33 import org.junit.Before;
34 import org.junit.Test;
35 import org.mockito.Mock;
36 import org.mockito.MockitoAnnotations;
37 import org.onap.policy.common.utils.coder.Coder;
38 import org.onap.policy.common.utils.coder.CoderException;
39 import org.onap.policy.common.utils.coder.StandardCoder;
40 import org.onap.policy.common.utils.resources.ResourceUtils;
41 import org.onap.policy.common.utils.services.Registry;
42 import org.onap.policy.models.pdp.concepts.Pdp;
43 import org.onap.policy.models.pdp.concepts.PdpGroup;
44 import org.onap.policy.models.pdp.concepts.PdpGroups;
45 import org.onap.policy.models.pdp.concepts.PdpSubGroup;
46 import org.onap.policy.models.pdp.concepts.Pdps;
47 import org.onap.policy.models.provider.PolicyModelsProvider;
48 import org.onap.policy.pap.main.PapConstants;
49 import org.onap.policy.pap.main.PolicyModelsProviderFactoryWrapper;
50
51 /**
52  * Class to perform unit test of {@link PdpGroupHealthCheckProvider}.
53  *
54  * @author Ram Krishna Verma (ram.krishna.verma@est.tech)
55  */
56 public class TestPdpGroupHealthCheckProvider {
57
58     @Mock
59     private PolicyModelsProvider dao;
60     private PolicyModelsProviderFactoryWrapper daofact;
61     private List<PdpGroup> groups;
62     private Coder coder = new StandardCoder();
63
64     /**
65      * Configures DAO and mocks.
66      */
67     @Before
68     public void setUp() throws Exception {
69
70         Registry.newRegistry();
71         MockitoAnnotations.initMocks(this);
72         daofact = mock(PolicyModelsProviderFactoryWrapper.class);
73         when(daofact.create()).thenReturn(dao);
74
75         groups = loadFile("pdpGroup.json").getGroups();
76
77         when(dao.getPdpGroups(any())).thenReturn(groups);
78
79         Registry.register(PapConstants.REG_PAP_DAO_FACTORY, daofact);
80     }
81
82     @Test
83     public void testFetchPdpGroupHealthStatus() throws Exception {
84         final PdpGroupHealthCheckProvider provider = new PdpGroupHealthCheckProvider();
85         final Pair<Status, Pdps> pair = provider.fetchPdpGroupHealthStatus();
86         assertEquals(Response.Status.OK, pair.getLeft());
87         verifyPdps(pair.getRight().getPdpList(), groups);
88     }
89
90     private void verifyPdps(final List<Pdp> pdpList, final List<PdpGroup> groups) {
91         assertEquals(5, pdpList.size());
92         for (final PdpGroup group : groups) {
93             for (final PdpSubGroup subGroup : group.getPdpSubgroups()) {
94                 pdpList.containsAll(subGroup.getPdpInstances());
95             }
96         }
97     }
98
99     private PdpGroups loadFile(final String fileName) {
100         final File propFile = new File(ResourceUtils.getFilePath4Resource("rest/" + fileName));
101         try {
102             return coder.decode(propFile, PdpGroups.class);
103
104         } catch (final CoderException e) {
105             throw new RuntimeException(e);
106         }
107     }
108 }