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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.pap.main.service;
23 import static org.assertj.core.api.Assertions.assertThat;
24 import static org.assertj.core.api.Assertions.assertThatThrownBy;
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;
43 public class PdpGroupServiceTest extends CommonPapRestServer {
45 private static final String FIELD_IS_NULL = "%s is marked non-null but is null";
47 private static final String DEFAULT_GROUP = "defaultGroup";
49 private static final String CREATE_GROUPS = "createGroups";
51 private static final String TYPE = "type";
53 private static final String NAME = "name";
55 private static final String LOCALNAME_IS_NULL = "parameter \"localName\" is null";
58 private PdpGroupService pdpGroupService;
60 private PdpGroups groupsToCreate;
62 private StandardCoder coder = new StandardCoder();
67 * @throws Exception the exception
71 public void setUp() throws Exception {
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);
79 public void testPdpGroupsCrudSuccess() {
81 List<PdpGroup> pdpGroups = pdpGroupService.getPdpGroups();
82 assertThat(pdpGroups).hasSize(1);
83 assertThat(pdpGroups.get(0).getName()).isEqualTo(DEFAULT_GROUP);
85 pdpGroupService.createPdpGroups(groupsToCreate.getGroups());
87 assertThat(pdpGroupService.getPdpGroups()).hasSize(2);
89 pdpGroups = pdpGroupService.getPdpGroups(CREATE_GROUPS);
90 assertThat(pdpGroups).hasSize(1);
91 assertThat(pdpGroups.get(0).getName()).isEqualTo(CREATE_GROUPS);
93 assertThat(pdpGroupService.getPdpGroups(PdpState.PASSIVE)).isEqualTo(pdpGroups);
95 List<PdpGroup> activePdpGroups = pdpGroupService.getPdpGroups(PdpState.ACTIVE);
96 assertThat(activePdpGroups).hasSize(1);
97 assertThat(activePdpGroups.get(0).getPdpSubgroups()).hasSize(3);
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);
103 PdpGroupFilter filter = PdpGroupFilter.builder()
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);
111 pdpGroupService.deletePdpGroup(CREATE_GROUPS);
112 pdpGroups = pdpGroupService.getPdpGroups();
113 assertThat(pdpGroups).hasSize(1);
114 assertThat(pdpGroups.get(0).getName()).isEqualTo(DEFAULT_GROUP);
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);
127 assertThatThrownBy(() -> pdpGroupService.createPdpGroups(null))
128 .hasMessage(String.format(FIELD_IS_NULL, "pdpGroups"));
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);
140 public void testUpdatePdp() {
142 assertThatThrownBy(() -> {
143 pdpGroupService.updatePdp(null, null, new Pdp());
144 }).hasMessage(String.format(FIELD_IS_NULL, "pdpGroupName"));
146 assertThatThrownBy(() -> {
147 pdpGroupService.updatePdp(NAME, null, new Pdp());
148 }).hasMessage(String.format(FIELD_IS_NULL, "pdpSubGroup"));
150 assertThatThrownBy(() -> {
151 pdpGroupService.updatePdp(NAME, TYPE, null);
152 }).hasMessage(String.format(FIELD_IS_NULL, "pdp"));
154 assertThatThrownBy(() -> {
155 pdpGroupService.updatePdp(NAME, TYPE, new Pdp());
156 }).hasMessage(LOCALNAME_IS_NULL);
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);
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);
174 public void testUpdateSubGroup() {
175 assertThatThrownBy(() -> {
176 pdpGroupService.updatePdpSubGroup(null, null);
177 }).hasMessage(String.format(FIELD_IS_NULL, "pdpGroupName"));
179 assertThatThrownBy(() -> {
180 pdpGroupService.updatePdpSubGroup(NAME, null);
181 }).hasMessage(String.format(FIELD_IS_NULL, "pdpSubGroup"));
183 assertThatThrownBy(() -> {
184 pdpGroupService.updatePdpSubGroup(NAME, new PdpSubGroup());
185 }).hasMessage(LOCALNAME_IS_NULL);
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);
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);