2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019-2021,2023 Nordix Foundation.
4 * Modifications Copyright (C) 2019-2021 AT&T Intellectual Property. All rights reserved.
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.models.pdp.persistence.provider;
24 import static org.assertj.core.api.Assertions.assertThat;
25 import static org.assertj.core.api.Assertions.assertThatCode;
26 import static org.assertj.core.api.Assertions.assertThatThrownBy;
27 import static org.junit.Assert.assertEquals;
28 import static org.junit.Assert.assertNotEquals;
29 import static org.junit.Assert.assertTrue;
31 import java.util.ArrayList;
32 import java.util.Collections;
33 import java.util.List;
34 import java.util.Properties;
35 import org.junit.After;
36 import org.junit.Before;
37 import org.junit.Test;
38 import org.onap.policy.common.utils.coder.StandardCoder;
39 import org.onap.policy.common.utils.resources.ResourceUtils;
40 import org.onap.policy.models.base.PfModelException;
41 import org.onap.policy.models.base.PfModelRuntimeException;
42 import org.onap.policy.models.base.Validated;
43 import org.onap.policy.models.dao.DaoParameters;
44 import org.onap.policy.models.dao.PfDao;
45 import org.onap.policy.models.dao.PfDaoFactory;
46 import org.onap.policy.models.dao.impl.DefaultPfDao;
47 import org.onap.policy.models.pdp.concepts.Pdp;
48 import org.onap.policy.models.pdp.concepts.PdpGroup;
49 import org.onap.policy.models.pdp.concepts.PdpGroupFilter;
50 import org.onap.policy.models.pdp.concepts.PdpGroups;
51 import org.onap.policy.models.pdp.concepts.PdpPolicyStatus;
52 import org.onap.policy.models.pdp.concepts.PdpPolicyStatus.PdpPolicyStatusBuilder;
53 import org.onap.policy.models.pdp.concepts.PdpPolicyStatus.State;
54 import org.onap.policy.models.pdp.concepts.PdpStatistics;
55 import org.onap.policy.models.pdp.concepts.PdpSubGroup;
56 import org.onap.policy.models.pdp.enums.PdpHealthStatus;
57 import org.onap.policy.models.pdp.enums.PdpState;
58 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
59 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifierOptVersion;
60 import org.onap.policy.models.tosca.simple.provider.SimpleToscaProvider;
63 * Test the {@link SimpleToscaProvider} class.
65 * @author Liam Fallon (liam.fallon@est.tech)
67 public class PdpProviderTest {
68 private static final String PDP_GROUPS0_JSON = "testdata/PdpGroups0.json";
69 private static final String PDP_TYPE_IS_NULL = "pdpType is marked .*ull but is null";
70 private static final String SUBGROUP_IS_NULL = "pdpSubGroup is marked .*ull but is null";
71 private static final String GROUP_IS_NULL = "pdpGroupName is marked .*ull but is null";
72 private static final String DAO_IS_NULL = "dao is marked .*ull but is null";
73 private static final String PDP_GROUP0 = "PdpGroup0";
74 private static final String GROUP_A = "groupA";
75 private static final String GROUP_B = "groupB";
76 private static final ToscaConceptIdentifier MY_POLICY = new ToscaConceptIdentifier("MyPolicy", "1.2.3");
77 private static final ToscaConceptIdentifier MY_POLICY2 = new ToscaConceptIdentifier("MyPolicyB", "2.3.4");
80 private StandardCoder standardCoder;
81 private PdpPolicyStatusBuilder statusBuilder;
84 * Set up the DAO towards the database.
86 * @throws Exception on database errors
89 public void setupDao() throws Exception {
90 final DaoParameters daoParameters = new DaoParameters();
91 daoParameters.setPluginClass(DefaultPfDao.class.getName());
93 daoParameters.setPersistenceUnit("ToscaConceptTest");
95 Properties jdbcProperties = new Properties();
96 jdbcProperties.setProperty("javax.persistence.jdbc.user", "policy");
97 jdbcProperties.setProperty("javax.persistence.jdbc.password", "P01icY");
99 if (System.getProperty("USE-MARIADB") != null) {
100 jdbcProperties.setProperty("javax.persistence.jdbc.driver", "org.mariadb.jdbc.Driver");
101 jdbcProperties.setProperty("javax.persistence.jdbc.url", "jdbc:mariadb://localhost:3306/policy");
103 jdbcProperties.setProperty("javax.persistence.jdbc.driver", "org.h2.Driver");
104 jdbcProperties.setProperty("javax.persistence.jdbc.url", "jdbc:h2:mem:PdpProviderTest");
107 daoParameters.setJdbcProperties(jdbcProperties);
109 pfDao = new PfDaoFactory().createPfDao(daoParameters);
110 pfDao.init(daoParameters);
117 public void setupGson() {
118 standardCoder = new StandardCoder();
122 * Set up Policy Status builder.
125 public void setupBuilder() {
126 ToscaConceptIdentifier policyType = new ToscaConceptIdentifier("MyPolicyType", "1.2.4");
128 statusBuilder = PdpPolicyStatus.builder().deploy(true).pdpType("MyPdpType").policy(MY_POLICY)
129 .policyType(policyType).state(State.SUCCESS);
133 public void teardown() {
138 public void testGroupsGet() throws Exception {
139 assertThatThrownBy(() -> {
140 new PdpProvider().getPdpGroups(null, null);
141 }).hasMessageMatching(DAO_IS_NULL);
143 assertThatThrownBy(() -> {
144 new PdpProvider().getPdpGroups(null, "name");
145 }).hasMessageMatching(DAO_IS_NULL);
147 String originalJson = ResourceUtils.getResourceAsString(PDP_GROUPS0_JSON);
148 PdpGroups pdpGroups0 = standardCoder.decode(originalJson, PdpGroups.class);
150 PdpGroups createdPdpGroups0 = new PdpGroups();
151 createdPdpGroups0.setGroups(new PdpProvider().createPdpGroups(pfDao, pdpGroups0.getGroups()));
152 String createdJson = standardCoder.encode(createdPdpGroups0);
153 assertEquals(originalJson.replaceAll("\\s+", ""), createdJson.replaceAll("\\s+", ""));
155 PdpGroups gotPdpGroups0 = new PdpGroups();
156 gotPdpGroups0.setGroups(new PdpProvider().getPdpGroups(pfDao, PDP_GROUP0));
158 String gotJson = standardCoder.encode(gotPdpGroups0);
160 assertEquals(originalJson.replaceAll("\\s+", ""), gotJson.replaceAll("\\s+", ""));
164 public void testFilteredPdpGroupGet() throws Exception {
165 assertThatThrownBy(() -> {
166 new PdpProvider().getFilteredPdpGroups(null, null);
167 }).hasMessageMatching(DAO_IS_NULL);
169 assertThatThrownBy(() -> {
170 new PdpProvider().getFilteredPdpGroups(null, PdpGroupFilter.builder().build());
171 }).hasMessageMatching(DAO_IS_NULL);
173 assertThatThrownBy(() -> {
174 new PdpProvider().getFilteredPdpGroups(pfDao, null);
175 }).hasMessageMatching("filter is marked .*ull but is null");
177 String originalJson = ResourceUtils.getResourceAsString("testdata/PdpGroupsForFiltering.json");
178 PdpGroups pdpGroups0 = standardCoder.decode(originalJson, PdpGroups.class);
180 assertEquals(5, new PdpProvider().createPdpGroups(pfDao, pdpGroups0.getGroups()).size());
182 List<ToscaConceptIdentifier> policyTypeList = new ArrayList<>();
183 policyTypeList.add(new ToscaConceptIdentifier("policy.type.0", "1.2.3"));
185 List<ToscaConceptIdentifier> policyList = new ArrayList<>();
186 policyList.add(new ToscaConceptIdentifier("Policy0", "4.5.6"));
189 final PdpGroupFilter filter = PdpGroupFilter.builder()
190 .groupState(PdpState.PASSIVE)
192 .matchPoliciesExactly(false)
193 .matchPolicyTypesExactly(false)
194 .pdpState(PdpState.PASSIVE)
196 .policyTypeList(policyTypeList)
197 .policyList(policyList)
200 assertEquals(1, new PdpProvider().getFilteredPdpGroups(pfDao, filter).size());
204 public void testGroupsCreate() throws Exception {
205 assertThatThrownBy(() -> {
206 new PdpProvider().createPdpGroups(null, null);
207 }).hasMessageMatching(DAO_IS_NULL);
209 assertThatThrownBy(() -> {
210 new PdpProvider().createPdpGroups(null, new ArrayList<>());
211 }).hasMessageMatching(DAO_IS_NULL);
213 assertThatThrownBy(() -> {
214 new PdpProvider().createPdpGroups(pfDao, null);
215 }).hasMessageMatching("pdpGroups is marked .*ull but is null");
217 String originalJson = ResourceUtils.getResourceAsString(PDP_GROUPS0_JSON);
218 PdpGroups pdpGroups0 = standardCoder.decode(originalJson, PdpGroups.class);
220 PdpGroups createdPdpGroups0 = new PdpGroups();
221 createdPdpGroups0.setGroups(new PdpProvider().createPdpGroups(pfDao, pdpGroups0.getGroups()));
222 String createdJson = standardCoder.encode(createdPdpGroups0);
223 assertEquals(originalJson.replaceAll("\\s+", ""), createdJson.replaceAll("\\s+", ""));
225 PdpGroups gotPdpGroups0 = new PdpGroups();
226 gotPdpGroups0.setGroups(new PdpProvider().getPdpGroups(pfDao, PDP_GROUP0));
228 String gotJson = standardCoder.encode(gotPdpGroups0);
229 assertEquals(originalJson.replaceAll("\\s+", ""), gotJson.replaceAll("\\s+", ""));
231 pdpGroups0.getGroups().get(0).setPdpGroupState(null);
232 assertThatThrownBy(() -> {
233 new PdpProvider().createPdpGroups(pfDao, pdpGroups0.getGroups());
234 }).hasMessageContaining("PDP group").hasMessageContaining("pdpGroupState")
235 .hasMessageContaining(Validated.IS_NULL);
239 public void testGroupsCreateNoPdp() throws Exception {
240 String originalJson = ResourceUtils.getResourceAsString("testdata/PdpGroupsNoPDPs.json");
242 PdpGroups pdpGroups0 = standardCoder.decode(originalJson, PdpGroups.class);
244 PdpGroups createdPdpGroups0 = new PdpGroups();
245 createdPdpGroups0.setGroups(new PdpProvider().createPdpGroups(pfDao, pdpGroups0.getGroups()));
246 assertNotEquals(pdpGroups0, createdPdpGroups0);
247 pdpGroups0.getGroups().get(0).getPdpSubgroups().get(0).setPdpInstances(new ArrayList<>());
248 String originalTweakedJson = standardCoder.encode(pdpGroups0);
249 String createdJson = standardCoder.encode(createdPdpGroups0);
250 assertEquals(originalTweakedJson.replaceAll("\\s+", ""), createdJson.replaceAll("\\s+", ""));
252 PdpGroups gotPdpGroups0 = new PdpGroups();
253 gotPdpGroups0.setGroups(new PdpProvider().getPdpGroups(pfDao, "TestPdpGroup"));
255 String gotJson = standardCoder.encode(gotPdpGroups0);
256 assertEquals(originalTweakedJson.replaceAll("\\s+", ""), gotJson.replaceAll("\\s+", ""));
260 public void testGroupsUpdate() throws Exception {
261 assertThatThrownBy(() -> {
262 new PdpProvider().updatePdpGroups(null, null);
263 }).hasMessageMatching(DAO_IS_NULL);
265 assertThatThrownBy(() -> {
266 new PdpProvider().updatePdpGroups(null, new ArrayList<>());
267 }).hasMessageMatching(DAO_IS_NULL);
269 assertThatThrownBy(() -> {
270 new PdpProvider().updatePdpGroups(pfDao, null);
271 }).hasMessageMatching("pdpGroups is marked .*ull but is null");
273 String originalJson = ResourceUtils.getResourceAsString(PDP_GROUPS0_JSON);
274 PdpGroups pdpGroups0 = standardCoder.decode(originalJson, PdpGroups.class);
276 PdpGroups createdPdpGroups0 = new PdpGroups();
277 createdPdpGroups0.setGroups(new PdpProvider().createPdpGroups(pfDao, pdpGroups0.getGroups()));
278 String createdJson = standardCoder.encode(createdPdpGroups0);
279 assertEquals(originalJson.replaceAll("\\s+", ""), createdJson.replaceAll("\\s+", ""));
281 PdpGroups gotPdpGroups0 = new PdpGroups();
282 gotPdpGroups0.setGroups(new PdpProvider().getPdpGroups(pfDao, PDP_GROUP0));
284 String gotJson = standardCoder.encode(gotPdpGroups0);
285 assertEquals(originalJson.replaceAll("\\s+", ""), gotJson.replaceAll("\\s+", ""));
287 String updateJson = ResourceUtils.getResourceAsString("testdata/PdpGroups0Update.json");
288 PdpGroups updatePdpGroups0 = standardCoder.decode(updateJson, PdpGroups.class);
290 PdpGroups updatedPdpGroups0 = new PdpGroups();
291 updatedPdpGroups0.setGroups(new PdpProvider().updatePdpGroups(pfDao, updatePdpGroups0.getGroups()));
293 List<Pdp> beforePdpInstances = updatePdpGroups0.getGroups().get(0).getPdpSubgroups().get(0).getPdpInstances();
294 List<Pdp> afterPdpInstances = updatedPdpGroups0.getGroups().get(0).getPdpSubgroups().get(0).getPdpInstances();
295 assertTrue(beforePdpInstances.containsAll(afterPdpInstances));
297 pdpGroups0.getGroups().get(0).setPdpGroupState(null);
298 assertThatThrownBy(() -> {
299 new PdpProvider().updatePdpGroups(pfDao, pdpGroups0.getGroups());
300 }).hasMessageContaining("PDP group").hasMessageContaining("pdpGroupState")
301 .hasMessageContaining(Validated.IS_NULL);
305 public void testPoliciesDelete() throws Exception {
306 assertThatThrownBy(() -> {
307 new PdpProvider().deletePdpGroup(null, null);
308 }).hasMessageMatching(DAO_IS_NULL);
310 assertThatThrownBy(() -> {
311 new PdpProvider().deletePdpGroup(null, "name");
312 }).hasMessageMatching(DAO_IS_NULL);
314 assertThatThrownBy(() -> {
315 new PdpProvider().deletePdpGroup(pfDao, null);
316 }).hasMessageMatching("name is marked .*ull but is null");
318 assertThatThrownBy(() -> {
319 new PdpProvider().deletePdpGroup(pfDao, "name");
320 }).hasMessage("delete of PDP group \"name:0.0.0\" failed, PDP group does not exist");
322 String originalJson = ResourceUtils.getResourceAsString(PDP_GROUPS0_JSON);
323 PdpGroups pdpGroups0 = standardCoder.decode(originalJson, PdpGroups.class);
325 PdpGroups createdPdpGroups0 = new PdpGroups();
326 createdPdpGroups0.setGroups(new PdpProvider().createPdpGroups(pfDao, pdpGroups0.getGroups()));
327 String createdJson = standardCoder.encode(createdPdpGroups0);
328 assertEquals(originalJson.replaceAll("\\s+", ""), createdJson.replaceAll("\\s+", ""));
330 PdpGroups gotPdpGroups0 = new PdpGroups();
331 gotPdpGroups0.setGroups(new PdpProvider().getPdpGroups(pfDao, PDP_GROUP0));
333 String gotJson = standardCoder.encode(gotPdpGroups0);
334 assertEquals(originalJson.replaceAll("\\s+", ""), gotJson.replaceAll("\\s+", ""));
336 PdpGroup deletedPdpGroup = new PdpProvider().deletePdpGroup(pfDao, PDP_GROUP0);
338 assertEquals(createdPdpGroups0.getGroups().get(0), deletedPdpGroup);
340 assertEquals(0, new PdpProvider().getPdpGroups(pfDao, PDP_GROUP0).size());
342 assertThatThrownBy(() -> {
343 new PdpProvider().deletePdpGroup(pfDao, PDP_GROUP0);
344 }).hasMessage("delete of PDP group \"PdpGroup0:0.0.0\" failed, PDP group does not exist");
348 public void testPdpSubgroupUpdate() throws Exception {
349 assertThatThrownBy(() -> {
350 new PdpProvider().updatePdpSubGroup(null, null, null);
351 }).hasMessageMatching(DAO_IS_NULL);
353 assertThatThrownBy(() -> {
354 new PdpProvider().updatePdpSubGroup(null, null, new PdpSubGroup());
355 }).hasMessageMatching(DAO_IS_NULL);
357 assertThatThrownBy(() -> {
358 new PdpProvider().updatePdpSubGroup(null, "name", null);
359 }).hasMessageMatching(DAO_IS_NULL);
361 assertThatThrownBy(() -> {
362 new PdpProvider().updatePdpSubGroup(null, "name", new PdpSubGroup());
363 }).hasMessageMatching(DAO_IS_NULL);
365 assertThatThrownBy(() -> {
366 new PdpProvider().updatePdpSubGroup(pfDao, null, null);
367 }).hasMessageMatching(GROUP_IS_NULL);
369 assertThatThrownBy(() -> {
370 new PdpProvider().updatePdpSubGroup(pfDao, null, new PdpSubGroup());
371 }).hasMessageMatching(GROUP_IS_NULL);
373 assertThatThrownBy(() -> {
374 new PdpProvider().updatePdpSubGroup(pfDao, "name", null);
375 }).hasMessageMatching(SUBGROUP_IS_NULL);
377 assertThatThrownBy(() -> {
378 new PdpProvider().updatePdpSubGroup(pfDao, "name", new PdpSubGroup());
379 }).hasMessage("parameter \"localName\" is null");
381 String originalJson = ResourceUtils.getResourceAsString(PDP_GROUPS0_JSON);
382 PdpGroups pdpGroups0 = standardCoder.decode(originalJson, PdpGroups.class);
384 PdpGroups createdPdpGroups0 = new PdpGroups();
385 createdPdpGroups0.setGroups(new PdpProvider().createPdpGroups(pfDao, pdpGroups0.getGroups()));
386 String createdJson = standardCoder.encode(createdPdpGroups0);
387 assertEquals(originalJson.replaceAll("\\s+", ""), createdJson.replaceAll("\\s+", ""));
389 PdpGroups gotPdpGroups0 = new PdpGroups();
390 gotPdpGroups0.setGroups(new PdpProvider().getPdpGroups(pfDao, PDP_GROUP0));
392 String gotJson = standardCoder.encode(gotPdpGroups0);
393 assertEquals(originalJson.replaceAll("\\s+", ""), gotJson.replaceAll("\\s+", ""));
395 PdpSubGroup existingSubGroup = gotPdpGroups0.getGroups().get(0).getPdpSubgroups().get(0);
396 existingSubGroup.setCurrentInstanceCount(10);
397 existingSubGroup.setDesiredInstanceCount(10);
398 new PdpProvider().updatePdpSubGroup(pfDao, PDP_GROUP0, existingSubGroup);
400 List<PdpGroup> afterUpdatePdpGroups = new PdpProvider().getPdpGroups(pfDao, PDP_GROUP0);
401 assertEquals(10, afterUpdatePdpGroups.get(0).getPdpSubgroups().get(0).getCurrentInstanceCount());
402 assertEquals(10, afterUpdatePdpGroups.get(0).getPdpSubgroups().get(0).getDesiredInstanceCount());
404 existingSubGroup.setDesiredInstanceCount(-1);
405 assertThatThrownBy(() -> {
406 new PdpProvider().updatePdpSubGroup(pfDao, PDP_GROUP0, existingSubGroup);
407 }).hasMessageContaining("PDP sub group").hasMessageContaining("desiredInstanceCount")
408 .hasMessageContaining("below the minimum value");
409 existingSubGroup.setDesiredInstanceCount(10);
413 public void testPdpUpdate() throws Exception {
414 assertThatThrownBy(() -> {
415 new PdpProvider().updatePdp(null, null, null, null);
416 }).hasMessageMatching(DAO_IS_NULL);
418 assertThatThrownBy(() -> {
419 new PdpProvider().updatePdp(null, null, null, new Pdp());
420 }).hasMessageMatching(DAO_IS_NULL);
422 assertThatThrownBy(() -> {
423 new PdpProvider().updatePdp(null, null, "TYPE", null);
424 }).hasMessageMatching(DAO_IS_NULL);
426 assertThatThrownBy(() -> {
427 new PdpProvider().updatePdp(null, null, "TYPE", new Pdp());
428 }).hasMessageMatching(DAO_IS_NULL);
430 assertThatThrownBy(() -> {
431 new PdpProvider().updatePdp(null, "name", null, null);
432 }).hasMessageMatching(DAO_IS_NULL);
434 assertThatThrownBy(() -> {
435 new PdpProvider().updatePdp(null, "name", null, new Pdp());
436 }).hasMessageMatching(DAO_IS_NULL);
438 assertThatThrownBy(() -> {
439 new PdpProvider().updatePdp(null, "name", "TYPE", null);
440 }).hasMessageMatching(DAO_IS_NULL);
442 assertThatThrownBy(() -> {
443 new PdpProvider().updatePdp(null, "name", "TYPE", new Pdp());
444 }).hasMessageMatching(DAO_IS_NULL);
446 assertThatThrownBy(() -> {
447 new PdpProvider().updatePdp(pfDao, null, null, null);
448 }).hasMessageMatching(GROUP_IS_NULL);
450 assertThatThrownBy(() -> {
451 new PdpProvider().updatePdp(pfDao, null, null, new Pdp());
452 }).hasMessageMatching(GROUP_IS_NULL);
454 assertThatThrownBy(() -> {
455 new PdpProvider().updatePdp(pfDao, null, "TYPE", null);
456 }).hasMessageMatching(GROUP_IS_NULL);
458 assertThatThrownBy(() -> {
459 new PdpProvider().updatePdp(pfDao, null, "TYPE", new Pdp());
460 }).hasMessageMatching(GROUP_IS_NULL);
462 assertThatThrownBy(() -> {
463 new PdpProvider().updatePdp(pfDao, "name", null, null);
464 }).hasMessageMatching(SUBGROUP_IS_NULL);
466 assertThatThrownBy(() -> {
467 new PdpProvider().updatePdp(pfDao, "name", null, new Pdp());
468 }).hasMessageMatching(SUBGROUP_IS_NULL);
470 assertThatThrownBy(() -> {
471 new PdpProvider().updatePdp(pfDao, "name", "TYPE", null);
472 }).hasMessageMatching("pdp is marked .*ull but is null");
474 assertThatThrownBy(() -> {
475 new PdpProvider().updatePdp(pfDao, "name", "TYPE", new Pdp());
476 }).hasMessage("parameter \"localName\" is null");
478 String originalJson = ResourceUtils.getResourceAsString(PDP_GROUPS0_JSON);
479 PdpGroups pdpGroups0 = standardCoder.decode(originalJson, PdpGroups.class);
481 PdpGroups createdPdpGroups0 = new PdpGroups();
482 createdPdpGroups0.setGroups(new PdpProvider().createPdpGroups(pfDao, pdpGroups0.getGroups()));
483 String createdJson = standardCoder.encode(createdPdpGroups0);
484 assertEquals(originalJson.replaceAll("\\s+", ""), createdJson.replaceAll("\\s+", ""));
486 PdpGroups gotPdpGroups0 = new PdpGroups();
487 gotPdpGroups0.setGroups(new PdpProvider().getPdpGroups(pfDao, PDP_GROUP0));
489 String gotJson = standardCoder.encode(gotPdpGroups0);
490 assertEquals(originalJson.replaceAll("\\s+", ""), gotJson.replaceAll("\\s+", ""));
492 Pdp existingPdp = gotPdpGroups0.getGroups().get(0).getPdpSubgroups().get(0).getPdpInstances().get(0);
493 existingPdp.setPdpState(PdpState.TEST);
494 existingPdp.setHealthy(PdpHealthStatus.TEST_IN_PROGRESS);
495 new PdpProvider().updatePdp(pfDao, PDP_GROUP0, "APEX", existingPdp);
497 List<PdpGroup> afterUpdatePdpGroups = new PdpProvider().getPdpGroups(pfDao, PDP_GROUP0);
498 assertEquals(PdpState.TEST,
499 afterUpdatePdpGroups.get(0).getPdpSubgroups().get(0).getPdpInstances().get(0).getPdpState());
500 assertEquals(PdpHealthStatus.TEST_IN_PROGRESS,
501 afterUpdatePdpGroups.get(0).getPdpSubgroups().get(0).getPdpInstances().get(0).getHealthy());
503 existingPdp.setMessage("");
504 assertThatThrownBy(() -> {
505 new PdpProvider().updatePdp(pfDao, PDP_GROUP0, "APEX", existingPdp);
506 }).hasMessageContaining("PDP").hasMessageContaining("message").hasMessageContaining(Validated.IS_BLANK);
507 existingPdp.setMessage("A Message");
511 public void testGetPdpStatistics() throws PfModelException {
512 assertThatThrownBy(() -> {
513 new PdpProvider().getPdpStatistics(null, null);
514 }).hasMessageMatching(DAO_IS_NULL);
516 assertThatThrownBy(() -> {
517 new PdpProvider().getPdpStatistics(null, "name");
518 }).hasMessageMatching(DAO_IS_NULL);
520 assertEquals(0, new PdpProvider().getPdpStatistics(pfDao, "name").size());
524 public void testUpdatePdpStatisticsDao() throws PfModelException {
525 assertThatThrownBy(() -> {
526 new PdpProvider().updatePdpStatistics(null, null, null, null, null);
527 }).hasMessageMatching(DAO_IS_NULL);
529 assertThatThrownBy(() -> {
530 new PdpProvider().updatePdpStatistics(null, null, null, null, new PdpStatistics());
531 }).hasMessageMatching(DAO_IS_NULL);
533 assertThatThrownBy(() -> {
534 new PdpProvider().updatePdpStatistics(null, null, null, "inst", null);
535 }).hasMessageMatching(DAO_IS_NULL);
537 assertThatThrownBy(() -> {
538 new PdpProvider().updatePdpStatistics(null, null, null, "inst", new PdpStatistics());
539 }).hasMessageMatching(DAO_IS_NULL);
541 assertThatThrownBy(() -> {
542 new PdpProvider().updatePdpStatistics(null, null, "TYPE", null, null);
543 }).hasMessageMatching(DAO_IS_NULL);
545 assertThatThrownBy(() -> {
546 new PdpProvider().updatePdpStatistics(null, null, "TYPE", null, new PdpStatistics());
547 }).hasMessageMatching(DAO_IS_NULL);
549 assertThatThrownBy(() -> {
550 new PdpProvider().updatePdpStatistics(null, null, "TYPE", "inst", null);
551 }).hasMessageMatching(DAO_IS_NULL);
553 assertThatThrownBy(() -> {
554 new PdpProvider().updatePdpStatistics(null, null, "TYPE", "inst", new PdpStatistics());
555 }).hasMessageMatching(DAO_IS_NULL);
557 assertThatThrownBy(() -> {
558 new PdpProvider().updatePdpStatistics(null, "name", null, null, null);
559 }).hasMessageMatching(DAO_IS_NULL);
561 assertThatThrownBy(() -> {
562 new PdpProvider().updatePdpStatistics(null, "name", null, null, new PdpStatistics());
563 }).hasMessageMatching(DAO_IS_NULL);
565 assertThatThrownBy(() -> {
566 new PdpProvider().updatePdpStatistics(null, "name", null, "inst", null);
567 }).hasMessageMatching(DAO_IS_NULL);
569 assertThatThrownBy(() -> {
570 new PdpProvider().updatePdpStatistics(null, "name", null, "inst", new PdpStatistics());
571 }).hasMessageMatching(DAO_IS_NULL);
573 assertThatThrownBy(() -> {
574 new PdpProvider().updatePdpStatistics(null, "name", "TYPE", null, null);
575 }).hasMessageMatching(DAO_IS_NULL);
577 assertThatThrownBy(() -> {
578 new PdpProvider().updatePdpStatistics(null, "name", "TYPE", null, new PdpStatistics());
579 }).hasMessageMatching(DAO_IS_NULL);
581 assertThatThrownBy(() -> {
582 new PdpProvider().updatePdpStatistics(null, "name", "TYPE", "inst", null);
583 }).hasMessageMatching(DAO_IS_NULL);
585 assertThatThrownBy(() -> {
586 new PdpProvider().updatePdpStatistics(null, "name", "TYPE", "inst", new PdpStatistics());
587 }).hasMessageMatching(DAO_IS_NULL);
591 public void testUpdatePdpStatisticsGroup() throws PfModelException {
592 assertThatThrownBy(() -> {
593 new PdpProvider().updatePdpStatistics(pfDao, null, null, null, null);
594 }).hasMessageMatching(GROUP_IS_NULL);
596 assertThatThrownBy(() -> {
597 new PdpProvider().updatePdpStatistics(pfDao, null, null, null, new PdpStatistics());
598 }).hasMessageMatching(GROUP_IS_NULL);
600 assertThatThrownBy(() -> {
601 new PdpProvider().updatePdpStatistics(pfDao, null, null, "inst", null);
602 }).hasMessageMatching(GROUP_IS_NULL);
604 assertThatThrownBy(() -> {
605 new PdpProvider().updatePdpStatistics(pfDao, null, null, "inst", new PdpStatistics());
606 }).hasMessageMatching(GROUP_IS_NULL);
608 assertThatThrownBy(() -> {
609 new PdpProvider().updatePdpStatistics(pfDao, null, "TYPE", null, null);
610 }).hasMessageMatching(GROUP_IS_NULL);
612 assertThatThrownBy(() -> {
613 new PdpProvider().updatePdpStatistics(pfDao, null, "TYPE", null, new PdpStatistics());
614 }).hasMessageMatching(GROUP_IS_NULL);
616 assertThatThrownBy(() -> {
617 new PdpProvider().updatePdpStatistics(pfDao, null, "TYPE", "inst", null);
618 }).hasMessageMatching(GROUP_IS_NULL);
620 assertThatThrownBy(() -> {
621 new PdpProvider().updatePdpStatistics(pfDao, null, "TYPE", "inst", new PdpStatistics());
622 }).hasMessageMatching(GROUP_IS_NULL);
624 assertThatThrownBy(() -> {
625 new PdpProvider().updatePdpStatistics(pfDao, "name", null, null, null);
626 }).hasMessageMatching(PDP_TYPE_IS_NULL);
628 assertThatThrownBy(() -> {
629 new PdpProvider().updatePdpStatistics(pfDao, "name", null, null, new PdpStatistics());
630 }).hasMessageMatching(PDP_TYPE_IS_NULL);
632 assertThatThrownBy(() -> {
633 new PdpProvider().updatePdpStatistics(pfDao, "name", null, "inst", null);
634 }).hasMessageMatching(PDP_TYPE_IS_NULL);
636 assertThatThrownBy(() -> {
637 new PdpProvider().updatePdpStatistics(pfDao, "name", null, "inst", new PdpStatistics());
638 }).hasMessageMatching(PDP_TYPE_IS_NULL);
640 assertThatThrownBy(() -> {
641 new PdpProvider().updatePdpStatistics(pfDao, "name", "TYPE", null, null);
642 }).hasMessageMatching("pdpInstanceId is marked .*ull but is null");
644 assertThatThrownBy(() -> {
645 new PdpProvider().updatePdpStatistics(pfDao, "name", "TYPE", null, new PdpStatistics());
646 }).hasMessageMatching("pdpInstanceId is marked .*ull but is null");
648 assertThatThrownBy(() -> {
649 new PdpProvider().updatePdpStatistics(pfDao, "name", "TYPE", "inst", null);
650 }).hasMessageMatching("pdpStatistics is marked .*ull but is null");
652 new PdpProvider().updatePdpStatistics(pfDao, "name", "TYPE", "inst", new PdpStatistics());
656 public void testGetAllPolicyStatusPfDao() throws PfModelException {
657 assertThatThrownBy(() -> {
658 new PdpProvider().getAllPolicyStatus(null);
659 }).hasMessageMatching(DAO_IS_NULL);
661 assertThat(new PdpProvider().getAllPolicyStatus(pfDao)).isEmpty();
663 PdpProvider provider = loadDeployments();
664 assertThat(provider.getAllPolicyStatus(pfDao)).hasSize(5);
667 private PdpProvider loadDeployments() {
668 PdpProvider provider = new PdpProvider();
670 // same name, different version
671 final ToscaConceptIdentifier policy3 = new ToscaConceptIdentifier(MY_POLICY.getName(), "10.20.30");
673 PdpPolicyStatus id1 = statusBuilder.pdpGroup(GROUP_A).pdpId("pdp1").policy(MY_POLICY).build();
674 PdpPolicyStatus id2 = statusBuilder.pdpGroup(GROUP_A).pdpId("pdp2").policy(MY_POLICY2).build();
675 PdpPolicyStatus id3 = statusBuilder.pdpGroup(GROUP_A).pdpId("pdp3").policy(policy3).build();
676 PdpPolicyStatus id4 = statusBuilder.pdpGroup(GROUP_B).pdpId("pdp4").policy(MY_POLICY).build();
677 PdpPolicyStatus id5 = statusBuilder.pdpGroup(GROUP_B).pdpId("pdp5").policy(MY_POLICY2).build();
678 provider.cudPolicyStatus(pfDao, List.of(id1, id2, id3, id4, id5), null, null);
684 public void testGetAllPolicyStatusPfDaoToscaConceptIdentifierOptVersion() throws PfModelException {
685 assertThatThrownBy(() -> {
686 new PdpProvider().getAllPolicyStatus(null, new ToscaConceptIdentifierOptVersion("somePdp", null));
687 }).hasMessageMatching(DAO_IS_NULL);
689 assertThatThrownBy(() -> {
690 new PdpProvider().getAllPolicyStatus(pfDao, null);
691 }).hasMessageContaining("policy").hasMessageContaining("null");
693 assertThat(new PdpProvider().getAllPolicyStatus(pfDao, new ToscaConceptIdentifierOptVersion("somePdp", null)))
696 PdpProvider provider = loadDeployments();
697 assertThat(provider.getAllPolicyStatus(pfDao, new ToscaConceptIdentifierOptVersion(MY_POLICY))).hasSize(2);
698 assertThat(provider.getAllPolicyStatus(pfDao, new ToscaConceptIdentifierOptVersion(MY_POLICY.getName(), null)))
703 public void testGetGroupPolicyStatus() throws PfModelException {
704 assertThatThrownBy(() -> {
705 new PdpProvider().getGroupPolicyStatus(null, "someGroup");
706 }).hasMessageMatching(DAO_IS_NULL);
708 assertThatThrownBy(() -> {
709 new PdpProvider().getGroupPolicyStatus(pfDao, null);
710 }).hasMessageContaining("group").hasMessageContaining("null");
712 assertThat(new PdpProvider().getGroupPolicyStatus(pfDao, PDP_GROUP0)).isEmpty();
714 PdpProvider provider = loadDeployments();
715 assertThat(provider.getGroupPolicyStatus(pfDao, GROUP_A)).hasSize(3);
719 public void cudPolicyStatus() throws PfModelException {
720 PdpProvider prov = new PdpProvider();
722 assertThatThrownBy(() -> prov.cudPolicyStatus(null, List.of(), List.of(), List.of()))
723 .hasMessageMatching(DAO_IS_NULL);
725 // null collections should be OK
726 assertThatCode(() -> prov.cudPolicyStatus(pfDao, null, null, null)).doesNotThrowAnyException();
730 public void cudPolicyStatus_Create() throws PfModelException {
731 PdpProvider prov = new PdpProvider();
733 PdpPolicyStatus idx = statusBuilder.pdpGroup(GROUP_A).pdpId("idX").build();
734 PdpPolicyStatus idy = statusBuilder.pdpGroup(GROUP_A).pdpId("idY").build();
735 PdpPolicyStatus idz = statusBuilder.pdpGroup(GROUP_B).pdpId("idZ").build();
736 prov.cudPolicyStatus(pfDao, List.of(idx, idy), null, null);
737 prov.cudPolicyStatus(pfDao, List.of(idz), null, null);
739 List<PdpPolicyStatus> records = prov.getGroupPolicyStatus(pfDao, GROUP_A);
740 assertThat(records).hasSize(2);
742 Collections.sort(records, (rec1, rec2) -> rec1.getPdpId().compareTo(rec2.getPdpId()));
743 assertThat(records.get(0)).isEqualTo(idx);
744 assertThat(records.get(1)).isEqualTo(idy);
746 records = prov.getGroupPolicyStatus(pfDao, GROUP_B);
747 assertThat(records).hasSize(1);
748 assertThat(records.get(0)).isEqualTo(idz);
752 public void cudPolicyStatus_Update() throws PfModelException {
753 PdpProvider prov = new PdpProvider();
755 PdpPolicyStatus idw = statusBuilder.pdpGroup(GROUP_A).pdpId("wId").build();
756 PdpPolicyStatus idx = statusBuilder.pdpGroup(GROUP_A).pdpId("xId").build();
757 PdpPolicyStatus idy = statusBuilder.pdpGroup(GROUP_A).pdpId("yId").build();
758 PdpPolicyStatus idz = statusBuilder.pdpGroup(GROUP_A).pdpId("zId").build();
759 prov.cudPolicyStatus(pfDao, List.of(idw, idx, idy, idz), null, null);
761 assertThat(prov.getGroupPolicyStatus(pfDao, GROUP_A)).hasSize(4);
764 * Now update some records.
766 idx.setState(State.FAILURE);
767 idz.setState(State.WAITING);
768 prov.cudPolicyStatus(pfDao, null, List.of(idx, idz), null);
769 List<PdpPolicyStatus> records = prov.getGroupPolicyStatus(pfDao, GROUP_A);
770 assertThat(records).hasSize(4);
772 Collections.sort(records, (rec1, rec2) -> rec1.getPdpId().compareTo(rec2.getPdpId()));
773 assertThat(records.get(0)).isEqualTo(idw);
774 assertThat(records.get(1)).isEqualTo(idx);
775 assertThat(records.get(2)).isEqualTo(idy);
776 assertThat(records.get(3)).isEqualTo(idz);
780 public void cudPolicyStatus_Delete() throws PfModelException {
781 PdpProvider prov = new PdpProvider();
783 PdpPolicyStatus idw = statusBuilder.pdpGroup(GROUP_A).pdpId("idW").build();
784 PdpPolicyStatus idx = statusBuilder.pdpGroup(GROUP_A).pdpId("idX").build();
785 PdpPolicyStatus idy = statusBuilder.pdpGroup(GROUP_A).pdpId("idY").build();
786 PdpPolicyStatus idz = statusBuilder.pdpGroup(GROUP_A).pdpId("idZ").build();
787 prov.cudPolicyStatus(pfDao, List.of(idw, idx, idy, idz), null, null);
789 assertThat(prov.getGroupPolicyStatus(pfDao, GROUP_A)).hasSize(4);
792 * Delete some records and then check again.
794 prov.cudPolicyStatus(pfDao, null, null, List.of(idw, idy));
796 List<PdpPolicyStatus> records = prov.getGroupPolicyStatus(pfDao, GROUP_A);
797 assertThat(records).hasSize(2);
799 Collections.sort(records, (rec1, rec2) -> rec1.getPdpId().compareTo(rec2.getPdpId()));
800 assertThat(records.get(0)).isEqualTo(idx);
801 assertThat(records.get(1)).isEqualTo(idz);
805 public void testFromAuthorativeStatus() throws PfModelException {
806 PdpProvider prov = new PdpProvider();
808 assertThatCode(() -> prov.cudPolicyStatus(pfDao, null, null, null)).doesNotThrowAnyException();
810 PdpPolicyStatus ida = statusBuilder.pdpGroup(GROUP_A).pdpId("idA").build();
811 PdpPolicyStatus idb = statusBuilder.pdpGroup(GROUP_A).pdpId("idB").build();
812 PdpPolicyStatus idc = statusBuilder.pdpGroup(GROUP_A).pdpId("idC").build();
813 PdpPolicyStatus idd = statusBuilder.pdpGroup(GROUP_A).pdpId("idD").build();
815 // make a couple invalid records
819 List<PdpPolicyStatus> list = List.of(ida, idb, idc, idd);
822 assertThatCode(() -> prov.cudPolicyStatus(pfDao, list, null, null))
823 .isInstanceOf(PfModelRuntimeException.class)
824 .hasMessageContaining("1").hasMessageContaining("3")
825 .hasMessageNotContaining("0").hasMessageNotContaining("2");
828 assertThat(prov.getGroupPolicyStatus(pfDao, GROUP_A)).isEmpty();