Topic names in PAP should be configurable from application.yaml
[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 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.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;
44
45 @ActiveProfiles("test")
46 public class PdpGroupServiceTest extends CommonPapRestServer {
47
48     private static final String FIELD_IS_NULL = "%s is marked non-null but is null";
49
50     private static final String DEFAULT_GROUP = "defaultGroup";
51
52     private static final String CREATE_GROUPS = "createGroups";
53
54     private static final String TYPE = "type";
55
56     private static final String NAME = "name";
57
58     private static final String LOCALNAME_IS_NULL = "parameter \"localName\" is null";
59
60     @Autowired
61     private PdpGroupService pdpGroupService;
62
63     private PdpGroups groupsToCreate;
64
65     private StandardCoder coder = new StandardCoder();
66
67     /**
68      * Setup before tests.
69      *
70      * @throws Exception the exception
71      */
72     @Override
73     @Before
74     public void setUp() throws Exception {
75         super.setUp();
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);
79     }
80
81     @Test
82     public void testPdpGroupsCrudSuccess() {
83
84         List<PdpGroup> pdpGroups = pdpGroupService.getPdpGroups();
85         assertThat(pdpGroups).hasSize(1);
86         assertThat(pdpGroups.get(0).getName()).isEqualTo(DEFAULT_GROUP);
87
88         pdpGroupService.createPdpGroups(groupsToCreate.getGroups());
89
90         assertThat(pdpGroupService.getPdpGroups()).hasSize(2);
91
92         pdpGroups = pdpGroupService.getPdpGroups(CREATE_GROUPS);
93         assertThat(pdpGroups).hasSize(1);
94         assertThat(pdpGroups.get(0).getName()).isEqualTo(CREATE_GROUPS);
95
96         assertThat(pdpGroupService.getPdpGroups(PdpState.PASSIVE)).isEqualTo(pdpGroups);
97
98         List<PdpGroup> activePdpGroups = pdpGroupService.getPdpGroups(PdpState.ACTIVE);
99         assertThat(activePdpGroups).hasSize(1);
100         assertThat(activePdpGroups.get(0).getPdpSubgroups()).hasSize(3);
101
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);
105
106         PdpGroupFilter filter = PdpGroupFilter.builder()
107             .policyTypeList(
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);
113
114         pdpGroupService.deletePdpGroup(CREATE_GROUPS);
115         pdpGroups = pdpGroupService.getPdpGroups();
116         assertThat(pdpGroups).hasSize(1);
117         assertThat(pdpGroups.get(0).getName()).isEqualTo(DEFAULT_GROUP);
118     }
119
120     @Test
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);
129
130         assertThatThrownBy(() -> pdpGroupService.createPdpGroups(null))
131             .hasMessage(String.format(FIELD_IS_NULL, "pdpGroups"));
132
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);
140     }
141
142     @Test
143     public void testUpdatePdp() {
144
145         assertThatThrownBy(() -> {
146             pdpGroupService.updatePdp(null, null, new Pdp());
147         }).hasMessage(String.format(FIELD_IS_NULL, "pdpGroupName"));
148
149         assertThatThrownBy(() -> {
150             pdpGroupService.updatePdp(NAME, null, new Pdp());
151         }).hasMessage(String.format(FIELD_IS_NULL, "pdpSubGroup"));
152
153         assertThatThrownBy(() -> {
154             pdpGroupService.updatePdp(NAME, TYPE, null);
155         }).hasMessage(String.format(FIELD_IS_NULL, "pdp"));
156
157         assertThatThrownBy(() -> {
158             pdpGroupService.updatePdp(NAME, TYPE, new Pdp());
159         }).hasMessage(LOCALNAME_IS_NULL);
160
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);
166
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);
174     }
175
176     @Test
177     public void testUpdateSubGroup() {
178         assertThatThrownBy(() -> {
179             pdpGroupService.updatePdpSubGroup(null, null);
180         }).hasMessage(String.format(FIELD_IS_NULL, "pdpGroupName"));
181
182         assertThatThrownBy(() -> {
183             pdpGroupService.updatePdpSubGroup(NAME, null);
184         }).hasMessage(String.format(FIELD_IS_NULL, "pdpSubGroup"));
185
186         assertThatThrownBy(() -> {
187             pdpGroupService.updatePdpSubGroup(NAME, new PdpSubGroup());
188         }).hasMessage(LOCALNAME_IS_NULL);
189
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);
195
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);
202     }
203 }