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