Java 17 / Spring 6 / Spring Boot 3 Upgrade
[policy/pap.git] / main / src / test / java / org / onap / policy / pap / main / rest / TestPdpGroupHealthCheckProvider.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019, 2023 Nordix Foundation.
4  *  Modifications Copyright (C) 2020-2022 Bell Canada. All rights reserved.
5  *  Modifications Copyright (C) 2021 AT&T Intellectual Property. 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.jupiter.api.Assertions.assertEquals;
26 import static org.junit.jupiter.api.Assertions.assertTrue;
27 import static org.mockito.Mockito.when;
28
29 import java.io.File;
30 import java.util.List;
31 import org.apache.commons.lang3.tuple.Pair;
32 import org.junit.jupiter.api.AfterEach;
33 import org.junit.jupiter.api.BeforeEach;
34 import org.junit.jupiter.api.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.pap.main.service.PdpGroupService;
48 import org.springframework.http.HttpStatus;
49
50 /**
51  * Class to perform unit test of {@link PdpGroupHealthCheckProvider}.
52  *
53  * @author Ram Krishna Verma (ram.krishna.verma@est.tech)
54  */
55 class TestPdpGroupHealthCheckProvider {
56
57     @Mock
58     private PdpGroupService pdpGroupService;
59     private List<PdpGroup> groups;
60     private final Coder coder = new StandardCoder();
61
62     AutoCloseable autoCloseable;
63
64     /**
65      * Configures DAO and mocks.
66      */
67     @BeforeEach
68     public void setUp() throws Exception {
69         autoCloseable = MockitoAnnotations.openMocks(this);
70         Registry.newRegistry();
71         groups = loadFile().getGroups();
72
73         when(pdpGroupService.getPdpGroups()).thenReturn(groups);
74     }
75
76     @AfterEach
77     public void tearDown() throws Exception {
78         autoCloseable.close();
79     }
80
81     @Test
82     void testFetchPdpGroupHealthStatus() throws Exception {
83         final PdpGroupHealthCheckProvider provider = new PdpGroupHealthCheckProvider(pdpGroupService);
84         final Pair<HttpStatus, Pdps> pair = provider.fetchPdpGroupHealthStatus();
85         assertEquals(HttpStatus.OK, pair.getLeft());
86         verifyPdps(pair.getRight().getPdpList(), groups);
87     }
88
89     private void verifyPdps(final List<Pdp> pdpList, final List<PdpGroup> groups) {
90         assertEquals(6, pdpList.size());
91         boolean containsAll = false;
92
93         do {
94             for (final PdpGroup group : groups) {
95                 for (final PdpSubGroup subGroup : group.getPdpSubgroups()) {
96                     containsAll = pdpList.containsAll(subGroup.getPdpInstances());
97                 }
98             }
99         } while (!containsAll);
100
101         assertTrue(containsAll);
102     }
103
104     private PdpGroups loadFile() {
105         final File propFile = new File(ResourceUtils.getFilePath4Resource("rest/" + "pdpGroup.json"));
106         try {
107             return coder.decode(propFile, PdpGroups.class);
108
109         } catch (final CoderException e) {
110             throw new RuntimeException(e);
111         }
112     }
113 }