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
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.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;
46 @ActiveProfiles("test")
47 @DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)
48 class PdpGroupServiceTest extends CommonPapRestServer {
50 private static final String FIELD_IS_NULL = "%s is marked non-null but is null";
52 private static final String DEFAULT_GROUP = "defaultGroup";
54 private static final String CREATE_GROUPS = "createGroups";
56 private static final String TYPE = "type";
58 private static final String NAME = "name";
60 private static final String LOCALNAME_IS_NULL = "parameter \"localName\" is null";
63 private PdpGroupService pdpGroupService;
65 private PdpGroups groupsToCreate;
67 private final StandardCoder coder = new StandardCoder();
72 * @throws Exception the exception
76 public void setUp() throws Exception {
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);
84 void testPdpGroupsCrudSuccess() {
86 List<PdpGroup> pdpGroups = pdpGroupService.getPdpGroups();
87 assertThat(pdpGroups).hasSize(1);
88 assertThat(pdpGroups.get(0).getName()).isEqualTo(DEFAULT_GROUP);
90 pdpGroupService.createPdpGroups(groupsToCreate.getGroups());
92 assertThat(pdpGroupService.getPdpGroups()).hasSize(2);
94 pdpGroups = pdpGroupService.getPdpGroups(CREATE_GROUPS);
95 assertThat(pdpGroups).hasSize(1);
96 assertThat(pdpGroups.get(0).getName()).isEqualTo(CREATE_GROUPS);
98 assertThat(pdpGroupService.getPdpGroups(PdpState.PASSIVE)).isEqualTo(pdpGroups);
100 List<PdpGroup> activePdpGroups = pdpGroupService.getPdpGroups(PdpState.ACTIVE);
101 assertThat(activePdpGroups).hasSize(1);
102 assertThat(activePdpGroups.get(0).getPdpSubgroups()).hasSize(3);
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);
108 PdpGroupFilter filter = PdpGroupFilter.builder()
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);
116 pdpGroupService.deletePdpGroup(CREATE_GROUPS);
117 pdpGroups = pdpGroupService.getPdpGroups();
118 assertThat(pdpGroups).hasSize(1);
119 assertThat(pdpGroups.get(0).getName()).isEqualTo(DEFAULT_GROUP);
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);
132 assertThatThrownBy(() -> pdpGroupService.createPdpGroups(null))
133 .hasMessage(String.format(FIELD_IS_NULL, "pdpGroups"));
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);
145 void testUpdatePdp() {
146 assertThatThrownBy(() -> pdpGroupService.updatePdp(null, null, new Pdp()))
147 .hasMessage(String.format(FIELD_IS_NULL, "pdpGroupName"));
149 assertThatThrownBy(() -> pdpGroupService.updatePdp(NAME, null, new Pdp()))
150 .hasMessage(String.format(FIELD_IS_NULL, "pdpSubGroup"));
152 assertThatThrownBy(() -> pdpGroupService.updatePdp(NAME, TYPE, null))
153 .hasMessage(String.format(FIELD_IS_NULL, "pdp"));
155 assertThatThrownBy(() -> pdpGroupService.updatePdp(NAME, TYPE, new Pdp())).hasMessage(LOCALNAME_IS_NULL);
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);
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);
173 void testUpdateSubGroup() {
174 assertThatThrownBy(() -> pdpGroupService.updatePdpSubGroup(null, null))
175 .hasMessage(String.format(FIELD_IS_NULL, "pdpGroupName"));
177 assertThatThrownBy(() -> pdpGroupService.updatePdpSubGroup(NAME, null))
178 .hasMessage(String.format(FIELD_IS_NULL, "pdpSubGroup"));
180 assertThatThrownBy(() -> pdpGroupService.updatePdpSubGroup(NAME, new PdpSubGroup()))
181 .hasMessage(LOCALNAME_IS_NULL);
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);
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);