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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 * SPDX-License-Identifier: Apache-2.0
20 * ============LICENSE_END=========================================================
23 package org.onap.policy.pap.main.rest;
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;
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;
51 * Class to perform unit test of {@link PdpGroupHealthCheckProvider}.
53 * @author Ram Krishna Verma (ram.krishna.verma@est.tech)
55 class TestPdpGroupHealthCheckProvider {
58 private PdpGroupService pdpGroupService;
59 private List<PdpGroup> groups;
60 private final Coder coder = new StandardCoder();
62 AutoCloseable autoCloseable;
65 * Configures DAO and mocks.
68 public void setUp() throws Exception {
69 autoCloseable = MockitoAnnotations.openMocks(this);
70 Registry.newRegistry();
71 groups = loadFile().getGroups();
73 when(pdpGroupService.getPdpGroups()).thenReturn(groups);
77 public void tearDown() throws Exception {
78 autoCloseable.close();
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);
89 private void verifyPdps(final List<Pdp> pdpList, final List<PdpGroup> groups) {
90 assertEquals(6, pdpList.size());
91 boolean containsAll = false;
94 for (final PdpGroup group : groups) {
95 for (final PdpSubGroup subGroup : group.getPdpSubgroups()) {
96 containsAll = pdpList.containsAll(subGroup.getPdpInstances());
99 } while (!containsAll);
101 assertTrue(containsAll);
104 private PdpGroups loadFile() {
105 final File propFile = new File(ResourceUtils.getFilePath4Resource("rest/" + "pdpGroup.json"));
107 return coder.decode(propFile, PdpGroups.class);
109 } catch (final CoderException e) {
110 throw new RuntimeException(e);