Move PAP database provider to spring boot default
[policy/pap.git] / main / src / test / java / org / onap / policy / pap / main / service / PdpGroupServiceTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2022 Bell Canada. All rights reserved.
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.service;
22
23 import static org.assertj.core.api.Assertions.assertThat;
24 import static org.assertj.core.api.Assertions.assertThatThrownBy;
25
26 import java.util.Collections;
27 import java.util.List;
28 import org.junit.Before;
29 import org.junit.Test;
30 import org.onap.policy.common.utils.coder.StandardCoder;
31 import org.onap.policy.common.utils.resources.ResourceUtils;
32 import org.onap.policy.models.pdp.concepts.Pdp;
33 import org.onap.policy.models.pdp.concepts.PdpGroup;
34 import org.onap.policy.models.pdp.concepts.PdpGroupFilter;
35 import org.onap.policy.models.pdp.concepts.PdpGroups;
36 import org.onap.policy.models.pdp.concepts.PdpSubGroup;
37 import org.onap.policy.models.pdp.enums.PdpHealthStatus;
38 import org.onap.policy.models.pdp.enums.PdpState;
39 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
40 import org.onap.policy.pap.main.rest.CommonPapRestServer;
41 import org.springframework.beans.factory.annotation.Autowired;
42
43 public class PdpGroupServiceTest extends CommonPapRestServer {
44
45     private static final String FIELD_IS_NULL = "%s is marked non-null but is null";
46
47     private static final String DEFAULT_GROUP = "defaultGroup";
48
49     private static final String CREATE_GROUPS = "createGroups";
50
51     private static final String TYPE = "type";
52
53     private static final String NAME = "name";
54
55     private static final String LOCALNAME_IS_NULL = "parameter \"localName\" is null";
56
57     @Autowired
58     private PdpGroupService pdpGroupService;
59
60     private PdpGroups groupsToCreate;
61
62     private StandardCoder coder = new StandardCoder();
63
64     /**
65      * Setup before tests.
66      *
67      * @throws Exception the exception
68      */
69     @Override
70     @Before
71     public void setUp() throws Exception {
72         super.setUp();
73         PdpGroups defaultGroup = coder.decode(ResourceUtils.getResourceAsString("e2e/PdpGroups.json"), PdpGroups.class);
74         pdpGroupService.createPdpGroups(defaultGroup.getGroups());
75         groupsToCreate = coder.decode(ResourceUtils.getResourceAsString("e2e/createGroups.json"), PdpGroups.class);
76     }
77
78     @Test
79     public void testPdpGroupsCrudSuccess() {
80
81         List<PdpGroup> pdpGroups = pdpGroupService.getPdpGroups();
82         assertThat(pdpGroups).hasSize(1);
83         assertThat(pdpGroups.get(0).getName()).isEqualTo(DEFAULT_GROUP);
84
85         pdpGroupService.createPdpGroups(groupsToCreate.getGroups());
86
87         assertThat(pdpGroupService.getPdpGroups()).hasSize(2);
88
89         pdpGroups = pdpGroupService.getPdpGroups(CREATE_GROUPS);
90         assertThat(pdpGroups).hasSize(1);
91         assertThat(pdpGroups.get(0).getName()).isEqualTo(CREATE_GROUPS);
92
93         assertThat(pdpGroupService.getPdpGroups(PdpState.PASSIVE)).isEqualTo(pdpGroups);
94
95         List<PdpGroup> activePdpGroups = pdpGroupService.getPdpGroups(PdpState.ACTIVE);
96         assertThat(activePdpGroups).hasSize(1);
97         assertThat(activePdpGroups.get(0).getPdpSubgroups()).hasSize(3);
98
99         assertThat(pdpGroupService.getPdpGroups(CREATE_GROUPS, PdpState.PASSIVE)).hasSize(1);
100         assertThat(pdpGroupService.getPdpGroups("invalid-group", PdpState.PASSIVE)).hasSize(0);
101         assertThat(pdpGroupService.getPdpGroups(DEFAULT_GROUP, PdpState.ACTIVE)).hasSize(1);
102
103         PdpGroupFilter filter = PdpGroupFilter.builder()
104             .policyTypeList(
105                 Collections.singletonList(new ToscaConceptIdentifier("onap.policies.native.Xacml", "1.0.0")))
106             .groupState(PdpState.ACTIVE).build();
107         List<PdpGroup> filteredGroups = pdpGroupService.getFilteredPdpGroups(filter);
108         assertThat(filteredGroups).hasSize(1);
109         assertThat(filteredGroups.get(0).getName()).isEqualTo(DEFAULT_GROUP);
110
111         pdpGroupService.deletePdpGroup(CREATE_GROUPS);
112         pdpGroups = pdpGroupService.getPdpGroups();
113         assertThat(pdpGroups).hasSize(1);
114         assertThat(pdpGroups.get(0).getName()).isEqualTo(DEFAULT_GROUP);
115     }
116
117     @Test
118     public void testPdpGroupsCrudFailure() {
119         PdpState pdpState = null;
120         assertThatThrownBy(() -> pdpGroupService.getPdpGroups(pdpState))
121             .hasMessage(String.format(FIELD_IS_NULL, "pdpState"));
122         pdpGroupService.createPdpGroups(groupsToCreate.getGroups());
123         assertThatThrownBy(() -> pdpGroupService.deletePdpGroup("invalid-group"))
124             .hasMessage("delete of PDP group \"invalid-group\" failed, PDP group does not exist");
125         assertThat(pdpGroupService.getPdpGroups()).hasSize(2);
126
127         assertThatThrownBy(() -> pdpGroupService.createPdpGroups(null))
128             .hasMessage(String.format(FIELD_IS_NULL, "pdpGroups"));
129
130         PdpGroup invalidPdpGroup = new PdpGroup(groupsToCreate.getGroups().get(0));
131         invalidPdpGroup.setName("invalidPdpGroup");
132         invalidPdpGroup.setPdpGroupState(null);
133         assertThatThrownBy(() -> pdpGroupService.createPdpGroups(List.of(invalidPdpGroup)))
134             .hasMessageContaining("Failed saving PdpGroup.")
135             .hasMessageContaining("item \"pdpGroupState\" value \"null\" INVALID, is null");
136         pdpGroupService.deletePdpGroup(CREATE_GROUPS);
137     }
138
139     @Test
140     public void testUpdatePdp() {
141
142         assertThatThrownBy(() -> {
143             pdpGroupService.updatePdp(null, null, new Pdp());
144         }).hasMessage(String.format(FIELD_IS_NULL, "pdpGroupName"));
145
146         assertThatThrownBy(() -> {
147             pdpGroupService.updatePdp(NAME, null, new Pdp());
148         }).hasMessage(String.format(FIELD_IS_NULL, "pdpSubGroup"));
149
150         assertThatThrownBy(() -> {
151             pdpGroupService.updatePdp(NAME, TYPE, null);
152         }).hasMessage(String.format(FIELD_IS_NULL, "pdp"));
153
154         assertThatThrownBy(() -> {
155             pdpGroupService.updatePdp(NAME, TYPE, new Pdp());
156         }).hasMessage(LOCALNAME_IS_NULL);
157
158         pdpGroupService.createPdpGroups(groupsToCreate.getGroups());
159         assertThat(pdpGroupService.getPdpGroups()).hasSize(2);
160         PdpGroup pdpGroup = pdpGroupService.getPdpGroups(CREATE_GROUPS).get(0);
161         Pdp pdp = pdpGroup.getPdpSubgroups().get(0).getPdpInstances().get(0);
162         assertThat(pdp.getHealthy()).isEqualTo(PdpHealthStatus.HEALTHY);
163
164         // now update and test
165         pdp.setHealthy(PdpHealthStatus.NOT_HEALTHY);
166         pdpGroupService.updatePdp(CREATE_GROUPS, "pdpTypeA", pdp);
167         PdpGroup updatGroup = pdpGroupService.getPdpGroups(CREATE_GROUPS).get(0);
168         assertThat(updatGroup.getPdpSubgroups().get(0).getPdpInstances().get(0).getHealthy())
169             .isEqualTo(PdpHealthStatus.NOT_HEALTHY);
170         pdpGroupService.deletePdpGroup(CREATE_GROUPS);
171     }
172
173     @Test
174     public void testUpdateSubGroup() {
175         assertThatThrownBy(() -> {
176             pdpGroupService.updatePdpSubGroup(null, null);
177         }).hasMessage(String.format(FIELD_IS_NULL, "pdpGroupName"));
178
179         assertThatThrownBy(() -> {
180             pdpGroupService.updatePdpSubGroup(NAME, null);
181         }).hasMessage(String.format(FIELD_IS_NULL, "pdpSubGroup"));
182
183         assertThatThrownBy(() -> {
184             pdpGroupService.updatePdpSubGroup(NAME, new PdpSubGroup());
185         }).hasMessage(LOCALNAME_IS_NULL);
186
187         pdpGroupService.createPdpGroups(groupsToCreate.getGroups());
188         assertThat(pdpGroupService.getPdpGroups()).hasSize(2);
189         PdpGroup pdpGroup = pdpGroupService.getPdpGroups(CREATE_GROUPS).get(0);
190         PdpSubGroup pdpSubGroup = pdpGroup.getPdpSubgroups().get(0);
191         assertThat(pdpSubGroup.getDesiredInstanceCount()).isEqualTo(2);
192
193         // now update and test
194         pdpSubGroup.setDesiredInstanceCount(1);
195         pdpGroupService.updatePdpSubGroup(CREATE_GROUPS, pdpSubGroup);
196         PdpGroup updatGroup = pdpGroupService.getPdpGroups(CREATE_GROUPS).get(0);
197         assertThat(updatGroup.getPdpSubgroups().get(0).getDesiredInstanceCount()).isEqualTo(1);
198         pdpGroupService.deletePdpGroup(CREATE_GROUPS);
199     }
200 }