2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2022 Bell Canada. All rights reserved.
4 * Modifications Copyright (C) 2022 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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.pap.main.service;
24 import static org.assertj.core.api.Assertions.assertThat;
25 import static org.assertj.core.api.Assertions.assertThatThrownBy;
27 import java.util.Collections;
28 import java.util.List;
29 import org.junit.Before;
30 import org.junit.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.context.ActiveProfiles;
45 @ActiveProfiles("test")
46 public class PdpGroupServiceTest extends CommonPapRestServer {
48 private static final String FIELD_IS_NULL = "%s is marked non-null but is null";
50 private static final String DEFAULT_GROUP = "defaultGroup";
52 private static final String CREATE_GROUPS = "createGroups";
54 private static final String TYPE = "type";
56 private static final String NAME = "name";
58 private static final String LOCALNAME_IS_NULL = "parameter \"localName\" is null";
61 private PdpGroupService pdpGroupService;
63 private PdpGroups groupsToCreate;
65 private StandardCoder coder = new StandardCoder();
70 * @throws Exception the exception
74 public void setUp() throws Exception {
76 PdpGroups defaultGroup = coder.decode(ResourceUtils.getResourceAsString("e2e/PdpGroups.json"), PdpGroups.class);
77 pdpGroupService.createPdpGroups(defaultGroup.getGroups());
78 groupsToCreate = coder.decode(ResourceUtils.getResourceAsString("e2e/createGroups.json"), PdpGroups.class);
82 public void testPdpGroupsCrudSuccess() {
84 List<PdpGroup> pdpGroups = pdpGroupService.getPdpGroups();
85 assertThat(pdpGroups).hasSize(1);
86 assertThat(pdpGroups.get(0).getName()).isEqualTo(DEFAULT_GROUP);
88 pdpGroupService.createPdpGroups(groupsToCreate.getGroups());
90 assertThat(pdpGroupService.getPdpGroups()).hasSize(2);
92 pdpGroups = pdpGroupService.getPdpGroups(CREATE_GROUPS);
93 assertThat(pdpGroups).hasSize(1);
94 assertThat(pdpGroups.get(0).getName()).isEqualTo(CREATE_GROUPS);
96 assertThat(pdpGroupService.getPdpGroups(PdpState.PASSIVE)).isEqualTo(pdpGroups);
98 List<PdpGroup> activePdpGroups = pdpGroupService.getPdpGroups(PdpState.ACTIVE);
99 assertThat(activePdpGroups).hasSize(1);
100 assertThat(activePdpGroups.get(0).getPdpSubgroups()).hasSize(3);
102 assertThat(pdpGroupService.getPdpGroups(CREATE_GROUPS, PdpState.PASSIVE)).hasSize(1);
103 assertThat(pdpGroupService.getPdpGroups("invalid-group", PdpState.PASSIVE)).isEmpty();
104 assertThat(pdpGroupService.getPdpGroups(DEFAULT_GROUP, PdpState.ACTIVE)).hasSize(1);
106 PdpGroupFilter filter = PdpGroupFilter.builder()
108 Collections.singletonList(new ToscaConceptIdentifier("onap.policies.native.Xacml", "1.0.0")))
109 .groupState(PdpState.ACTIVE).build();
110 List<PdpGroup> filteredGroups = pdpGroupService.getFilteredPdpGroups(filter);
111 assertThat(filteredGroups).hasSize(1);
112 assertThat(filteredGroups.get(0).getName()).isEqualTo(DEFAULT_GROUP);
114 pdpGroupService.deletePdpGroup(CREATE_GROUPS);
115 pdpGroups = pdpGroupService.getPdpGroups();
116 assertThat(pdpGroups).hasSize(1);
117 assertThat(pdpGroups.get(0).getName()).isEqualTo(DEFAULT_GROUP);
121 public void testPdpGroupsCrudFailure() {
122 PdpState pdpState = null;
123 assertThatThrownBy(() -> pdpGroupService.getPdpGroups(pdpState))
124 .hasMessage(String.format(FIELD_IS_NULL, "pdpState"));
125 pdpGroupService.createPdpGroups(groupsToCreate.getGroups());
126 assertThatThrownBy(() -> pdpGroupService.deletePdpGroup("invalid-group"))
127 .hasMessage("delete of PDP group \"invalid-group\" failed, PDP group does not exist");
128 assertThat(pdpGroupService.getPdpGroups()).hasSize(2);
130 assertThatThrownBy(() -> pdpGroupService.createPdpGroups(null))
131 .hasMessage(String.format(FIELD_IS_NULL, "pdpGroups"));
133 PdpGroup invalidPdpGroup = new PdpGroup(groupsToCreate.getGroups().get(0));
134 invalidPdpGroup.setName("invalidPdpGroup");
135 invalidPdpGroup.setPdpGroupState(null);
136 assertThatThrownBy(() -> pdpGroupService.createPdpGroups(List.of(invalidPdpGroup)))
137 .hasMessageContaining("Failed saving PdpGroup.")
138 .hasMessageContaining("item \"pdpGroupState\" value \"null\" INVALID, is null");
139 pdpGroupService.deletePdpGroup(CREATE_GROUPS);
143 public void testUpdatePdp() {
145 assertThatThrownBy(() -> {
146 pdpGroupService.updatePdp(null, null, new Pdp());
147 }).hasMessage(String.format(FIELD_IS_NULL, "pdpGroupName"));
149 assertThatThrownBy(() -> {
150 pdpGroupService.updatePdp(NAME, null, new Pdp());
151 }).hasMessage(String.format(FIELD_IS_NULL, "pdpSubGroup"));
153 assertThatThrownBy(() -> {
154 pdpGroupService.updatePdp(NAME, TYPE, null);
155 }).hasMessage(String.format(FIELD_IS_NULL, "pdp"));
157 assertThatThrownBy(() -> {
158 pdpGroupService.updatePdp(NAME, TYPE, new Pdp());
159 }).hasMessage(LOCALNAME_IS_NULL);
161 pdpGroupService.createPdpGroups(groupsToCreate.getGroups());
162 assertThat(pdpGroupService.getPdpGroups()).hasSize(2);
163 PdpGroup pdpGroup = pdpGroupService.getPdpGroups(CREATE_GROUPS).get(0);
164 Pdp pdp = pdpGroup.getPdpSubgroups().get(0).getPdpInstances().get(0);
165 assertThat(pdp.getHealthy()).isEqualTo(PdpHealthStatus.HEALTHY);
167 // now update and test
168 pdp.setHealthy(PdpHealthStatus.NOT_HEALTHY);
169 pdpGroupService.updatePdp(CREATE_GROUPS, "pdpTypeA", pdp);
170 PdpGroup updatGroup = pdpGroupService.getPdpGroups(CREATE_GROUPS).get(0);
171 assertThat(updatGroup.getPdpSubgroups().get(0).getPdpInstances().get(0).getHealthy())
172 .isEqualTo(PdpHealthStatus.NOT_HEALTHY);
173 pdpGroupService.deletePdpGroup(CREATE_GROUPS);
177 public void testUpdateSubGroup() {
178 assertThatThrownBy(() -> {
179 pdpGroupService.updatePdpSubGroup(null, null);
180 }).hasMessage(String.format(FIELD_IS_NULL, "pdpGroupName"));
182 assertThatThrownBy(() -> {
183 pdpGroupService.updatePdpSubGroup(NAME, null);
184 }).hasMessage(String.format(FIELD_IS_NULL, "pdpSubGroup"));
186 assertThatThrownBy(() -> {
187 pdpGroupService.updatePdpSubGroup(NAME, new PdpSubGroup());
188 }).hasMessage(LOCALNAME_IS_NULL);
190 pdpGroupService.createPdpGroups(groupsToCreate.getGroups());
191 assertThat(pdpGroupService.getPdpGroups()).hasSize(2);
192 PdpGroup pdpGroup = pdpGroupService.getPdpGroups(CREATE_GROUPS).get(0);
193 PdpSubGroup pdpSubGroup = pdpGroup.getPdpSubgroups().get(0);
194 assertThat(pdpSubGroup.getDesiredInstanceCount()).isEqualTo(2);
196 // now update and test
197 pdpSubGroup.setDesiredInstanceCount(1);
198 pdpGroupService.updatePdpSubGroup(CREATE_GROUPS, pdpSubGroup);
199 PdpGroup updatGroup = pdpGroupService.getPdpGroups(CREATE_GROUPS).get(0);
200 assertThat(updatGroup.getPdpSubgroups().get(0).getDesiredInstanceCount()).isEqualTo(1);
201 pdpGroupService.deletePdpGroup(CREATE_GROUPS);