Java 17 / Spring 6 / Spring Boot 3 Upgrade
[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  *  Modifications Copyright (C) 2022-2023 Nordix Foundation.
5  * ================================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.pap.main.service;
23
24 import static org.assertj.core.api.Assertions.assertThat;
25 import static org.assertj.core.api.Assertions.assertThatThrownBy;
26
27 import java.util.Collections;
28 import java.util.List;
29 import org.junit.jupiter.api.BeforeEach;
30 import org.junit.jupiter.api.Test;
31 import org.onap.policy.common.utils.coder.StandardCoder;
32 import org.onap.policy.common.utils.resources.ResourceUtils;
33 import org.onap.policy.models.pdp.concepts.Pdp;
34 import org.onap.policy.models.pdp.concepts.PdpGroup;
35 import org.onap.policy.models.pdp.concepts.PdpGroupFilter;
36 import org.onap.policy.models.pdp.concepts.PdpGroups;
37 import org.onap.policy.models.pdp.concepts.PdpSubGroup;
38 import org.onap.policy.models.pdp.enums.PdpHealthStatus;
39 import org.onap.policy.models.pdp.enums.PdpState;
40 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
41 import org.onap.policy.pap.main.rest.CommonPapRestServer;
42 import org.springframework.beans.factory.annotation.Autowired;
43 import org.springframework.test.annotation.DirtiesContext;
44 import org.springframework.test.context.ActiveProfiles;
45
46 @ActiveProfiles("test")
47 @DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)
48 class PdpGroupServiceTest extends CommonPapRestServer {
49
50     private static final String FIELD_IS_NULL = "%s is marked non-null but is null";
51
52     private static final String DEFAULT_GROUP = "defaultGroup";
53
54     private static final String CREATE_GROUPS = "createGroups";
55
56     private static final String TYPE = "type";
57
58     private static final String NAME = "name";
59
60     private static final String LOCALNAME_IS_NULL = "parameter \"localName\" is null";
61
62     @Autowired
63     private PdpGroupService pdpGroupService;
64
65     private PdpGroups groupsToCreate;
66
67     private final StandardCoder coder = new StandardCoder();
68
69     /**
70      * Setup before tests.
71      *
72      * @throws Exception the exception
73      */
74     @Override
75     @BeforeEach
76     public void setUp() throws Exception {
77         super.setUp();
78         PdpGroups defaultGroup = coder.decode(ResourceUtils.getResourceAsString("e2e/PdpGroups.json"), PdpGroups.class);
79         pdpGroupService.createPdpGroups(defaultGroup.getGroups());
80         groupsToCreate = coder.decode(ResourceUtils.getResourceAsString("e2e/createGroups.json"), PdpGroups.class);
81     }
82
83     @Test
84     void testPdpGroupsCrudSuccess() {
85
86         List<PdpGroup> pdpGroups = pdpGroupService.getPdpGroups();
87         assertThat(pdpGroups).hasSize(1);
88         assertThat(pdpGroups.get(0).getName()).isEqualTo(DEFAULT_GROUP);
89
90         pdpGroupService.createPdpGroups(groupsToCreate.getGroups());
91
92         assertThat(pdpGroupService.getPdpGroups()).hasSize(2);
93
94         pdpGroups = pdpGroupService.getPdpGroups(CREATE_GROUPS);
95         assertThat(pdpGroups).hasSize(1);
96         assertThat(pdpGroups.get(0).getName()).isEqualTo(CREATE_GROUPS);
97
98         assertThat(pdpGroupService.getPdpGroups(PdpState.PASSIVE)).isEqualTo(pdpGroups);
99
100         List<PdpGroup> activePdpGroups = pdpGroupService.getPdpGroups(PdpState.ACTIVE);
101         assertThat(activePdpGroups).hasSize(1);
102         assertThat(activePdpGroups.get(0).getPdpSubgroups()).hasSize(3);
103
104         assertThat(pdpGroupService.getPdpGroups(CREATE_GROUPS, PdpState.PASSIVE)).hasSize(1);
105         assertThat(pdpGroupService.getPdpGroups("invalid-group", PdpState.PASSIVE)).isEmpty();
106         assertThat(pdpGroupService.getPdpGroups(DEFAULT_GROUP, PdpState.ACTIVE)).hasSize(1);
107
108         PdpGroupFilter filter = PdpGroupFilter.builder()
109             .policyTypeList(
110                 Collections.singletonList(new ToscaConceptIdentifier("onap.policies.native.Xacml", "1.0.0")))
111             .groupState(PdpState.ACTIVE).build();
112         List<PdpGroup> filteredGroups = pdpGroupService.getFilteredPdpGroups(filter);
113         assertThat(filteredGroups).hasSize(1);
114         assertThat(filteredGroups.get(0).getName()).isEqualTo(DEFAULT_GROUP);
115
116         pdpGroupService.deletePdpGroup(CREATE_GROUPS);
117         pdpGroups = pdpGroupService.getPdpGroups();
118         assertThat(pdpGroups).hasSize(1);
119         assertThat(pdpGroups.get(0).getName()).isEqualTo(DEFAULT_GROUP);
120     }
121
122     @Test
123     void testPdpGroupsCrudFailure() {
124         PdpState pdpState = null;
125         assertThatThrownBy(() -> pdpGroupService.getPdpGroups(pdpState))
126             .hasMessage(String.format(FIELD_IS_NULL, "pdpState"));
127         pdpGroupService.createPdpGroups(groupsToCreate.getGroups());
128         assertThatThrownBy(() -> pdpGroupService.deletePdpGroup("invalid-group"))
129             .hasMessage("delete of PDP group \"invalid-group\" failed, PDP group does not exist");
130         assertThat(pdpGroupService.getPdpGroups()).hasSize(2);
131
132         assertThatThrownBy(() -> pdpGroupService.createPdpGroups(null))
133             .hasMessage(String.format(FIELD_IS_NULL, "pdpGroups"));
134
135         PdpGroup invalidPdpGroup = new PdpGroup(groupsToCreate.getGroups().get(0));
136         invalidPdpGroup.setName("invalidPdpGroup");
137         invalidPdpGroup.setPdpGroupState(null);
138         assertThatThrownBy(() -> pdpGroupService.createPdpGroups(List.of(invalidPdpGroup)))
139             .hasMessageContaining("Failed saving PdpGroup.")
140             .hasMessageContaining("item \"pdpGroupState\" value \"null\" INVALID, is null");
141         pdpGroupService.deletePdpGroup(CREATE_GROUPS);
142     }
143
144     @Test
145     void testUpdatePdp() {
146         assertThatThrownBy(() -> pdpGroupService.updatePdp(null, null, new Pdp()))
147             .hasMessage(String.format(FIELD_IS_NULL, "pdpGroupName"));
148
149         assertThatThrownBy(() -> pdpGroupService.updatePdp(NAME, null, new Pdp()))
150             .hasMessage(String.format(FIELD_IS_NULL, "pdpSubGroup"));
151
152         assertThatThrownBy(() -> pdpGroupService.updatePdp(NAME, TYPE, null))
153             .hasMessage(String.format(FIELD_IS_NULL, "pdp"));
154
155         assertThatThrownBy(() -> pdpGroupService.updatePdp(NAME, TYPE, new Pdp())).hasMessage(LOCALNAME_IS_NULL);
156
157         pdpGroupService.createPdpGroups(groupsToCreate.getGroups());
158         assertThat(pdpGroupService.getPdpGroups()).hasSize(2);
159         PdpGroup pdpGroup = pdpGroupService.getPdpGroups(CREATE_GROUPS).get(0);
160         Pdp pdp = pdpGroup.getPdpSubgroups().get(0).getPdpInstances().get(0);
161         assertThat(pdp.getHealthy()).isEqualTo(PdpHealthStatus.HEALTHY);
162
163         // now update and test
164         pdp.setHealthy(PdpHealthStatus.NOT_HEALTHY);
165         pdpGroupService.updatePdp(CREATE_GROUPS, "pdpTypeA", pdp);
166         PdpGroup updatGroup = pdpGroupService.getPdpGroups(CREATE_GROUPS).get(0);
167         assertThat(updatGroup.getPdpSubgroups().get(0).getPdpInstances().get(0).getHealthy())
168             .isEqualTo(PdpHealthStatus.NOT_HEALTHY);
169         pdpGroupService.deletePdpGroup(CREATE_GROUPS);
170     }
171
172     @Test
173     void testUpdateSubGroup() {
174         assertThatThrownBy(() -> pdpGroupService.updatePdpSubGroup(null, null))
175             .hasMessage(String.format(FIELD_IS_NULL, "pdpGroupName"));
176
177         assertThatThrownBy(() -> pdpGroupService.updatePdpSubGroup(NAME, null))
178             .hasMessage(String.format(FIELD_IS_NULL, "pdpSubGroup"));
179
180         assertThatThrownBy(() -> pdpGroupService.updatePdpSubGroup(NAME, new PdpSubGroup()))
181             .hasMessage(LOCALNAME_IS_NULL);
182
183         pdpGroupService.createPdpGroups(groupsToCreate.getGroups());
184         assertThat(pdpGroupService.getPdpGroups()).hasSize(2);
185         PdpGroup pdpGroup = pdpGroupService.getPdpGroups(CREATE_GROUPS).get(0);
186         PdpSubGroup pdpSubGroup = pdpGroup.getPdpSubgroups().get(0);
187         assertThat(pdpSubGroup.getDesiredInstanceCount()).isEqualTo(2);
188
189         // now update and test
190         pdpSubGroup.setDesiredInstanceCount(1);
191         pdpGroupService.updatePdpSubGroup(CREATE_GROUPS, pdpSubGroup);
192         PdpGroup updatGroup = pdpGroupService.getPdpGroups(CREATE_GROUPS).get(0);
193         assertThat(updatGroup.getPdpSubgroups().get(0).getDesiredInstanceCount()).isEqualTo(1);
194         pdpGroupService.deletePdpGroup(CREATE_GROUPS);
195     }
196 }