2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019-2021 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.eclipse.persistence.config.PersistenceUnitProperties;
36 import org.junit.After;
37 import org.junit.Before;
38 import org.junit.Test;
39 import org.onap.policy.common.utils.coder.StandardCoder;
40 import org.onap.policy.common.utils.resources.ResourceUtils;
41 import org.onap.policy.models.base.PfModelException;
42 import org.onap.policy.models.base.PfModelRuntimeException;
43 import org.onap.policy.models.base.Validated;
44 import org.onap.policy.models.dao.DaoParameters;
45 import org.onap.policy.models.dao.PfDao;
46 import org.onap.policy.models.dao.PfDaoFactory;
47 import org.onap.policy.models.dao.impl.DefaultPfDao;
48 import org.onap.policy.models.pdp.concepts.Pdp;
49 import org.onap.policy.models.pdp.concepts.PdpGroup;
50 import org.onap.policy.models.pdp.concepts.PdpGroupFilter;
51 import org.onap.policy.models.pdp.concepts.PdpGroups;
52 import org.onap.policy.models.pdp.concepts.PdpPolicyStatus;
53 import org.onap.policy.models.pdp.concepts.PdpPolicyStatus.PdpPolicyStatusBuilder;
54 import org.onap.policy.models.pdp.concepts.PdpPolicyStatus.State;
55 import org.onap.policy.models.pdp.concepts.PdpStatistics;
56 import org.onap.policy.models.pdp.concepts.PdpSubGroup;
57 import org.onap.policy.models.pdp.enums.PdpHealthStatus;
58 import org.onap.policy.models.pdp.enums.PdpState;
59 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
60 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifierOptVersion;
61 import org.onap.policy.models.tosca.simple.provider.SimpleToscaProvider;
64 * Test the {@link SimpleToscaProvider} class.
66 * @author Liam Fallon (liam.fallon@est.tech)
68 public class PdpProviderTest {
69 private static final String PDP_GROUPS0_JSON = "testdata/PdpGroups0.json";
70 private static final String PDP_TYPE_IS_NULL = "pdpType is marked .*ull but is null";
71 private static final String SUBGROUP_IS_NULL = "pdpSubGroup is marked .*ull but is null";
72 private static final String GROUP_IS_NULL = "pdpGroupName is marked .*ull but is null";
73 private static final String DAO_IS_NULL = "dao is marked .*ull but is null";
74 private static final String PDP_GROUP0 = "PdpGroup0";
75 private static final String GROUP_A = "groupA";
76 private static final String GROUP_B = "groupB";
77 private static final ToscaConceptIdentifier MY_POLICY = new ToscaConceptIdentifier("MyPolicy", "1.2.3");
78 private static final ToscaConceptIdentifier MY_POLICY2 = new ToscaConceptIdentifier("MyPolicyB", "2.3.4");
81 private StandardCoder standardCoder;
82 private PdpPolicyStatusBuilder statusBuilder;
86 * Set up the DAO towards the database.
88 * @throws Exception on database errors
91 public void setupDao() throws Exception {
92 final DaoParameters daoParameters = new DaoParameters();
93 daoParameters.setPluginClass(DefaultPfDao.class.getName());
95 daoParameters.setPersistenceUnit("ToscaConceptTest");
97 Properties jdbcProperties = new Properties();
98 jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_USER, "policy");
99 jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_PASSWORD, "P01icY");
101 if (System.getProperty("USE-MARIADB") != null) {
102 jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_DRIVER, "org.mariadb.jdbc.Driver");
103 jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_URL, "jdbc:mariadb://localhost:3306/policy");
105 jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_DRIVER, "org.h2.Driver");
106 jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_URL, "jdbc:h2:mem:PdpProviderTest");
109 daoParameters.setJdbcProperties(jdbcProperties);
111 pfDao = new PfDaoFactory().createPfDao(daoParameters);
112 pfDao.init(daoParameters);
119 public void setupGson() {
120 standardCoder = new StandardCoder();
124 * Set up Policy Status builder.
127 public void setupBuilder() {
128 ToscaConceptIdentifier policyType = new ToscaConceptIdentifier("MyPolicyType", "1.2.4");
130 statusBuilder = PdpPolicyStatus.builder().deploy(true).pdpType("MyPdpType").policy(MY_POLICY)
131 .policyType(policyType).state(State.SUCCESS);
135 public void teardown() {
140 public void testGroupsGet() throws Exception {
141 assertThatThrownBy(() -> {
142 new PdpProvider().getPdpGroups(null, null);
143 }).hasMessageMatching(DAO_IS_NULL);
145 assertThatThrownBy(() -> {
146 new PdpProvider().getPdpGroups(null, "name");
147 }).hasMessageMatching(DAO_IS_NULL);
149 String originalJson = ResourceUtils.getResourceAsString(PDP_GROUPS0_JSON);
150 PdpGroups pdpGroups0 = standardCoder.decode(originalJson, PdpGroups.class);
152 PdpGroups createdPdpGroups0 = new PdpGroups();
153 createdPdpGroups0.setGroups(new PdpProvider().createPdpGroups(pfDao, pdpGroups0.getGroups()));
154 String createdJson = standardCoder.encode(createdPdpGroups0);
155 assertEquals(originalJson.replaceAll("\\s+", ""), createdJson.replaceAll("\\s+", ""));
157 PdpGroups gotPdpGroups0 = new PdpGroups();
158 gotPdpGroups0.setGroups(new PdpProvider().getPdpGroups(pfDao, PDP_GROUP0));
160 String gotJson = standardCoder.encode(gotPdpGroups0);
162 assertEquals(originalJson.replaceAll("\\s+", ""), gotJson.replaceAll("\\s+", ""));
166 public void testFilteredPdpGroupGet() throws Exception {
167 assertThatThrownBy(() -> {
168 new PdpProvider().getFilteredPdpGroups(null, null);
169 }).hasMessageMatching(DAO_IS_NULL);
171 assertThatThrownBy(() -> {
172 new PdpProvider().getFilteredPdpGroups(null, PdpGroupFilter.builder().build());
173 }).hasMessageMatching(DAO_IS_NULL);
175 assertThatThrownBy(() -> {
176 new PdpProvider().getFilteredPdpGroups(pfDao, null);
177 }).hasMessageMatching("filter is marked .*ull but is null");
179 String originalJson = ResourceUtils.getResourceAsString("testdata/PdpGroupsForFiltering.json");
180 PdpGroups pdpGroups0 = standardCoder.decode(originalJson, PdpGroups.class);
182 assertEquals(5, new PdpProvider().createPdpGroups(pfDao, pdpGroups0.getGroups()).size());
184 List<ToscaConceptIdentifier> policyTypeList = new ArrayList<>();
185 policyTypeList.add(new ToscaConceptIdentifier("policy.type.0", "1.2.3"));
187 List<ToscaConceptIdentifier> policyList = new ArrayList<>();
188 policyList.add(new ToscaConceptIdentifier("Policy0", "4.5.6"));
191 final PdpGroupFilter filter = PdpGroupFilter.builder()
192 .groupState(PdpState.PASSIVE)
194 .matchPoliciesExactly(false)
195 .matchPolicyTypesExactly(false)
196 .pdpState(PdpState.PASSIVE)
198 .policyTypeList(policyTypeList)
199 .policyList(policyList)
202 assertEquals(1, new PdpProvider().getFilteredPdpGroups(pfDao, filter).size());
206 public void testGroupsCreate() throws Exception {
207 assertThatThrownBy(() -> {
208 new PdpProvider().createPdpGroups(null, null);
209 }).hasMessageMatching(DAO_IS_NULL);
211 assertThatThrownBy(() -> {
212 new PdpProvider().createPdpGroups(null, new ArrayList<>());
213 }).hasMessageMatching(DAO_IS_NULL);
215 assertThatThrownBy(() -> {
216 new PdpProvider().createPdpGroups(pfDao, null);
217 }).hasMessageMatching("pdpGroups is marked .*ull but is null");
219 String originalJson = ResourceUtils.getResourceAsString(PDP_GROUPS0_JSON);
220 PdpGroups pdpGroups0 = standardCoder.decode(originalJson, PdpGroups.class);
222 PdpGroups createdPdpGroups0 = new PdpGroups();
223 createdPdpGroups0.setGroups(new PdpProvider().createPdpGroups(pfDao, pdpGroups0.getGroups()));
224 String createdJson = standardCoder.encode(createdPdpGroups0);
225 assertEquals(originalJson.replaceAll("\\s+", ""), createdJson.replaceAll("\\s+", ""));
227 PdpGroups gotPdpGroups0 = new PdpGroups();
228 gotPdpGroups0.setGroups(new PdpProvider().getPdpGroups(pfDao, PDP_GROUP0));
230 String gotJson = standardCoder.encode(gotPdpGroups0);
231 assertEquals(originalJson.replaceAll("\\s+", ""), gotJson.replaceAll("\\s+", ""));
233 pdpGroups0.getGroups().get(0).setPdpGroupState(null);
234 assertThatThrownBy(() -> {
235 new PdpProvider().createPdpGroups(pfDao, pdpGroups0.getGroups());
236 }).hasMessageContaining("PDP group").hasMessageContaining("pdpGroupState")
237 .hasMessageContaining(Validated.IS_NULL);
241 public void testGroupsCreateNoPdp() throws Exception {
242 String originalJson = ResourceUtils.getResourceAsString("testdata/PdpGroupsNoPDPs.json");
244 PdpGroups pdpGroups0 = standardCoder.decode(originalJson, PdpGroups.class);
246 PdpGroups createdPdpGroups0 = new PdpGroups();
247 createdPdpGroups0.setGroups(new PdpProvider().createPdpGroups(pfDao, pdpGroups0.getGroups()));
248 assertNotEquals(pdpGroups0, createdPdpGroups0);
249 pdpGroups0.getGroups().get(0).getPdpSubgroups().get(0).setPdpInstances(new ArrayList<>());
250 String originalTweakedJson = standardCoder.encode(pdpGroups0);
251 String createdJson = standardCoder.encode(createdPdpGroups0);
252 assertEquals(originalTweakedJson.replaceAll("\\s+", ""), createdJson.replaceAll("\\s+", ""));
254 PdpGroups gotPdpGroups0 = new PdpGroups();
255 gotPdpGroups0.setGroups(new PdpProvider().getPdpGroups(pfDao, "TestPdpGroup"));
257 String gotJson = standardCoder.encode(gotPdpGroups0);
258 assertEquals(originalTweakedJson.replaceAll("\\s+", ""), gotJson.replaceAll("\\s+", ""));
262 public void testGroupsUpdate() throws Exception {
263 assertThatThrownBy(() -> {
264 new PdpProvider().updatePdpGroups(null, null);
265 }).hasMessageMatching(DAO_IS_NULL);
267 assertThatThrownBy(() -> {
268 new PdpProvider().updatePdpGroups(null, new ArrayList<>());
269 }).hasMessageMatching(DAO_IS_NULL);
271 assertThatThrownBy(() -> {
272 new PdpProvider().updatePdpGroups(pfDao, null);
273 }).hasMessageMatching("pdpGroups is marked .*ull but is null");
275 String originalJson = ResourceUtils.getResourceAsString(PDP_GROUPS0_JSON);
276 PdpGroups pdpGroups0 = standardCoder.decode(originalJson, PdpGroups.class);
278 PdpGroups createdPdpGroups0 = new PdpGroups();
279 createdPdpGroups0.setGroups(new PdpProvider().createPdpGroups(pfDao, pdpGroups0.getGroups()));
280 String createdJson = standardCoder.encode(createdPdpGroups0);
281 assertEquals(originalJson.replaceAll("\\s+", ""), createdJson.replaceAll("\\s+", ""));
283 PdpGroups gotPdpGroups0 = new PdpGroups();
284 gotPdpGroups0.setGroups(new PdpProvider().getPdpGroups(pfDao, PDP_GROUP0));
286 String gotJson = standardCoder.encode(gotPdpGroups0);
287 assertEquals(originalJson.replaceAll("\\s+", ""), gotJson.replaceAll("\\s+", ""));
289 String updateJson = ResourceUtils.getResourceAsString("testdata/PdpGroups0Update.json");
290 PdpGroups updatePdpGroups0 = standardCoder.decode(updateJson, PdpGroups.class);
292 PdpGroups updatedPdpGroups0 = new PdpGroups();
293 updatedPdpGroups0.setGroups(new PdpProvider().updatePdpGroups(pfDao, updatePdpGroups0.getGroups()));
295 List<Pdp> beforePdpInstances = updatePdpGroups0.getGroups().get(0).getPdpSubgroups().get(0).getPdpInstances();
296 List<Pdp> afterPdpInstances = updatedPdpGroups0.getGroups().get(0).getPdpSubgroups().get(0).getPdpInstances();
297 assertTrue(beforePdpInstances.containsAll(afterPdpInstances));
299 pdpGroups0.getGroups().get(0).setPdpGroupState(null);
300 assertThatThrownBy(() -> {
301 new PdpProvider().updatePdpGroups(pfDao, pdpGroups0.getGroups());
302 }).hasMessageContaining("PDP group").hasMessageContaining("pdpGroupState")
303 .hasMessageContaining(Validated.IS_NULL);
307 public void testPoliciesDelete() throws Exception {
308 assertThatThrownBy(() -> {
309 new PdpProvider().deletePdpGroup(null, null);
310 }).hasMessageMatching(DAO_IS_NULL);
312 assertThatThrownBy(() -> {
313 new PdpProvider().deletePdpGroup(null, "name");
314 }).hasMessageMatching(DAO_IS_NULL);
316 assertThatThrownBy(() -> {
317 new PdpProvider().deletePdpGroup(pfDao, null);
318 }).hasMessageMatching("name is marked .*ull but is null");
320 assertThatThrownBy(() -> {
321 new PdpProvider().deletePdpGroup(pfDao, "name");
322 }).hasMessage("delete of PDP group \"name:0.0.0\" failed, PDP group does not exist");
324 String originalJson = ResourceUtils.getResourceAsString(PDP_GROUPS0_JSON);
325 PdpGroups pdpGroups0 = standardCoder.decode(originalJson, PdpGroups.class);
327 PdpGroups createdPdpGroups0 = new PdpGroups();
328 createdPdpGroups0.setGroups(new PdpProvider().createPdpGroups(pfDao, pdpGroups0.getGroups()));
329 String createdJson = standardCoder.encode(createdPdpGroups0);
330 assertEquals(originalJson.replaceAll("\\s+", ""), createdJson.replaceAll("\\s+", ""));
332 PdpGroups gotPdpGroups0 = new PdpGroups();
333 gotPdpGroups0.setGroups(new PdpProvider().getPdpGroups(pfDao, PDP_GROUP0));
335 String gotJson = standardCoder.encode(gotPdpGroups0);
336 assertEquals(originalJson.replaceAll("\\s+", ""), gotJson.replaceAll("\\s+", ""));
338 PdpGroup deletedPdpGroup = new PdpProvider().deletePdpGroup(pfDao, PDP_GROUP0);
340 assertEquals(createdPdpGroups0.getGroups().get(0), deletedPdpGroup);
342 assertEquals(0, new PdpProvider().getPdpGroups(pfDao, PDP_GROUP0).size());
344 assertThatThrownBy(() -> {
345 new PdpProvider().deletePdpGroup(pfDao, PDP_GROUP0);
346 }).hasMessage("delete of PDP group \"PdpGroup0:0.0.0\" failed, PDP group does not exist");
350 public void testPdpSubgroupUpdate() throws Exception {
351 assertThatThrownBy(() -> {
352 new PdpProvider().updatePdpSubGroup(null, null, null);
353 }).hasMessageMatching(DAO_IS_NULL);
355 assertThatThrownBy(() -> {
356 new PdpProvider().updatePdpSubGroup(null, null, new PdpSubGroup());
357 }).hasMessageMatching(DAO_IS_NULL);
359 assertThatThrownBy(() -> {
360 new PdpProvider().updatePdpSubGroup(null, "name", null);
361 }).hasMessageMatching(DAO_IS_NULL);
363 assertThatThrownBy(() -> {
364 new PdpProvider().updatePdpSubGroup(null, "name", new PdpSubGroup());
365 }).hasMessageMatching(DAO_IS_NULL);
367 assertThatThrownBy(() -> {
368 new PdpProvider().updatePdpSubGroup(pfDao, null, null);
369 }).hasMessageMatching(GROUP_IS_NULL);
371 assertThatThrownBy(() -> {
372 new PdpProvider().updatePdpSubGroup(pfDao, null, new PdpSubGroup());
373 }).hasMessageMatching(GROUP_IS_NULL);
375 assertThatThrownBy(() -> {
376 new PdpProvider().updatePdpSubGroup(pfDao, "name", null);
377 }).hasMessageMatching(SUBGROUP_IS_NULL);
379 assertThatThrownBy(() -> {
380 new PdpProvider().updatePdpSubGroup(pfDao, "name", new PdpSubGroup());
381 }).hasMessage("parameter \"localName\" is null");
383 String originalJson = ResourceUtils.getResourceAsString(PDP_GROUPS0_JSON);
384 PdpGroups pdpGroups0 = standardCoder.decode(originalJson, PdpGroups.class);
386 PdpGroups createdPdpGroups0 = new PdpGroups();
387 createdPdpGroups0.setGroups(new PdpProvider().createPdpGroups(pfDao, pdpGroups0.getGroups()));
388 String createdJson = standardCoder.encode(createdPdpGroups0);
389 assertEquals(originalJson.replaceAll("\\s+", ""), createdJson.replaceAll("\\s+", ""));
391 PdpGroups gotPdpGroups0 = new PdpGroups();
392 gotPdpGroups0.setGroups(new PdpProvider().getPdpGroups(pfDao, PDP_GROUP0));
394 String gotJson = standardCoder.encode(gotPdpGroups0);
395 assertEquals(originalJson.replaceAll("\\s+", ""), gotJson.replaceAll("\\s+", ""));
397 PdpSubGroup existingSubGroup = gotPdpGroups0.getGroups().get(0).getPdpSubgroups().get(0);
398 existingSubGroup.setCurrentInstanceCount(10);
399 existingSubGroup.setDesiredInstanceCount(10);
400 new PdpProvider().updatePdpSubGroup(pfDao, PDP_GROUP0, existingSubGroup);
402 List<PdpGroup> afterUpdatePdpGroups = new PdpProvider().getPdpGroups(pfDao, PDP_GROUP0);
403 assertEquals(10, afterUpdatePdpGroups.get(0).getPdpSubgroups().get(0).getCurrentInstanceCount());
404 assertEquals(10, afterUpdatePdpGroups.get(0).getPdpSubgroups().get(0).getDesiredInstanceCount());
406 existingSubGroup.setDesiredInstanceCount(-1);
407 assertThatThrownBy(() -> {
408 new PdpProvider().updatePdpSubGroup(pfDao, PDP_GROUP0, existingSubGroup);
409 }).hasMessageContaining("PDP sub group").hasMessageContaining("desiredInstanceCount")
410 .hasMessageContaining("below the minimum value");
411 existingSubGroup.setDesiredInstanceCount(10);
415 public void testPdpUpdate() throws Exception {
416 assertThatThrownBy(() -> {
417 new PdpProvider().updatePdp(null, null, null, null);
418 }).hasMessageMatching(DAO_IS_NULL);
420 assertThatThrownBy(() -> {
421 new PdpProvider().updatePdp(null, null, null, new Pdp());
422 }).hasMessageMatching(DAO_IS_NULL);
424 assertThatThrownBy(() -> {
425 new PdpProvider().updatePdp(null, null, "TYPE", null);
426 }).hasMessageMatching(DAO_IS_NULL);
428 assertThatThrownBy(() -> {
429 new PdpProvider().updatePdp(null, null, "TYPE", new Pdp());
430 }).hasMessageMatching(DAO_IS_NULL);
432 assertThatThrownBy(() -> {
433 new PdpProvider().updatePdp(null, "name", null, null);
434 }).hasMessageMatching(DAO_IS_NULL);
436 assertThatThrownBy(() -> {
437 new PdpProvider().updatePdp(null, "name", null, new Pdp());
438 }).hasMessageMatching(DAO_IS_NULL);
440 assertThatThrownBy(() -> {
441 new PdpProvider().updatePdp(null, "name", "TYPE", null);
442 }).hasMessageMatching(DAO_IS_NULL);
444 assertThatThrownBy(() -> {
445 new PdpProvider().updatePdp(null, "name", "TYPE", new Pdp());
446 }).hasMessageMatching(DAO_IS_NULL);
448 assertThatThrownBy(() -> {
449 new PdpProvider().updatePdp(pfDao, null, null, null);
450 }).hasMessageMatching(GROUP_IS_NULL);
452 assertThatThrownBy(() -> {
453 new PdpProvider().updatePdp(pfDao, null, null, new Pdp());
454 }).hasMessageMatching(GROUP_IS_NULL);
456 assertThatThrownBy(() -> {
457 new PdpProvider().updatePdp(pfDao, null, "TYPE", null);
458 }).hasMessageMatching(GROUP_IS_NULL);
460 assertThatThrownBy(() -> {
461 new PdpProvider().updatePdp(pfDao, null, "TYPE", new Pdp());
462 }).hasMessageMatching(GROUP_IS_NULL);
464 assertThatThrownBy(() -> {
465 new PdpProvider().updatePdp(pfDao, "name", null, null);
466 }).hasMessageMatching(SUBGROUP_IS_NULL);
468 assertThatThrownBy(() -> {
469 new PdpProvider().updatePdp(pfDao, "name", null, new Pdp());
470 }).hasMessageMatching(SUBGROUP_IS_NULL);
472 assertThatThrownBy(() -> {
473 new PdpProvider().updatePdp(pfDao, "name", "TYPE", null);
474 }).hasMessageMatching("pdp is marked .*ull but is null");
476 assertThatThrownBy(() -> {
477 new PdpProvider().updatePdp(pfDao, "name", "TYPE", new Pdp());
478 }).hasMessage("parameter \"localName\" is null");
480 String originalJson = ResourceUtils.getResourceAsString(PDP_GROUPS0_JSON);
481 PdpGroups pdpGroups0 = standardCoder.decode(originalJson, PdpGroups.class);
483 PdpGroups createdPdpGroups0 = new PdpGroups();
484 createdPdpGroups0.setGroups(new PdpProvider().createPdpGroups(pfDao, pdpGroups0.getGroups()));
485 String createdJson = standardCoder.encode(createdPdpGroups0);
486 assertEquals(originalJson.replaceAll("\\s+", ""), createdJson.replaceAll("\\s+", ""));
488 PdpGroups gotPdpGroups0 = new PdpGroups();
489 gotPdpGroups0.setGroups(new PdpProvider().getPdpGroups(pfDao, PDP_GROUP0));
491 String gotJson = standardCoder.encode(gotPdpGroups0);
492 assertEquals(originalJson.replaceAll("\\s+", ""), gotJson.replaceAll("\\s+", ""));
494 Pdp existingPdp = gotPdpGroups0.getGroups().get(0).getPdpSubgroups().get(0).getPdpInstances().get(0);
495 existingPdp.setPdpState(PdpState.TEST);
496 existingPdp.setHealthy(PdpHealthStatus.TEST_IN_PROGRESS);
497 new PdpProvider().updatePdp(pfDao, PDP_GROUP0, "APEX", existingPdp);
499 List<PdpGroup> afterUpdatePdpGroups = new PdpProvider().getPdpGroups(pfDao, PDP_GROUP0);
500 assertEquals(PdpState.TEST,
501 afterUpdatePdpGroups.get(0).getPdpSubgroups().get(0).getPdpInstances().get(0).getPdpState());
502 assertEquals(PdpHealthStatus.TEST_IN_PROGRESS,
503 afterUpdatePdpGroups.get(0).getPdpSubgroups().get(0).getPdpInstances().get(0).getHealthy());
505 existingPdp.setMessage("");
506 assertThatThrownBy(() -> {
507 new PdpProvider().updatePdp(pfDao, PDP_GROUP0, "APEX", existingPdp);
508 }).hasMessageContaining("PDP").hasMessageContaining("message").hasMessageContaining(Validated.IS_BLANK);
509 existingPdp.setMessage("A Message");
513 public void testGetPdpStatistics() throws PfModelException {
514 assertThatThrownBy(() -> {
515 new PdpProvider().getPdpStatistics(null, null);
516 }).hasMessageMatching(DAO_IS_NULL);
518 assertThatThrownBy(() -> {
519 new PdpProvider().getPdpStatistics(null, "name");
520 }).hasMessageMatching(DAO_IS_NULL);
522 assertEquals(0, new PdpProvider().getPdpStatistics(pfDao, "name").size());
526 public void testUpdatePdpStatisticsDao() throws PfModelException {
527 assertThatThrownBy(() -> {
528 new PdpProvider().updatePdpStatistics(null, null, null, null, null);
529 }).hasMessageMatching(DAO_IS_NULL);
531 assertThatThrownBy(() -> {
532 new PdpProvider().updatePdpStatistics(null, null, null, null, new PdpStatistics());
533 }).hasMessageMatching(DAO_IS_NULL);
535 assertThatThrownBy(() -> {
536 new PdpProvider().updatePdpStatistics(null, null, null, "inst", null);
537 }).hasMessageMatching(DAO_IS_NULL);
539 assertThatThrownBy(() -> {
540 new PdpProvider().updatePdpStatistics(null, null, null, "inst", new PdpStatistics());
541 }).hasMessageMatching(DAO_IS_NULL);
543 assertThatThrownBy(() -> {
544 new PdpProvider().updatePdpStatistics(null, null, "TYPE", null, null);
545 }).hasMessageMatching(DAO_IS_NULL);
547 assertThatThrownBy(() -> {
548 new PdpProvider().updatePdpStatistics(null, null, "TYPE", null, new PdpStatistics());
549 }).hasMessageMatching(DAO_IS_NULL);
551 assertThatThrownBy(() -> {
552 new PdpProvider().updatePdpStatistics(null, null, "TYPE", "inst", null);
553 }).hasMessageMatching(DAO_IS_NULL);
555 assertThatThrownBy(() -> {
556 new PdpProvider().updatePdpStatistics(null, null, "TYPE", "inst", new PdpStatistics());
557 }).hasMessageMatching(DAO_IS_NULL);
559 assertThatThrownBy(() -> {
560 new PdpProvider().updatePdpStatistics(null, "name", null, null, null);
561 }).hasMessageMatching(DAO_IS_NULL);
563 assertThatThrownBy(() -> {
564 new PdpProvider().updatePdpStatistics(null, "name", null, null, new PdpStatistics());
565 }).hasMessageMatching(DAO_IS_NULL);
567 assertThatThrownBy(() -> {
568 new PdpProvider().updatePdpStatistics(null, "name", null, "inst", null);
569 }).hasMessageMatching(DAO_IS_NULL);
571 assertThatThrownBy(() -> {
572 new PdpProvider().updatePdpStatistics(null, "name", null, "inst", new PdpStatistics());
573 }).hasMessageMatching(DAO_IS_NULL);
575 assertThatThrownBy(() -> {
576 new PdpProvider().updatePdpStatistics(null, "name", "TYPE", null, null);
577 }).hasMessageMatching(DAO_IS_NULL);
579 assertThatThrownBy(() -> {
580 new PdpProvider().updatePdpStatistics(null, "name", "TYPE", null, new PdpStatistics());
581 }).hasMessageMatching(DAO_IS_NULL);
583 assertThatThrownBy(() -> {
584 new PdpProvider().updatePdpStatistics(null, "name", "TYPE", "inst", null);
585 }).hasMessageMatching(DAO_IS_NULL);
587 assertThatThrownBy(() -> {
588 new PdpProvider().updatePdpStatistics(null, "name", "TYPE", "inst", new PdpStatistics());
589 }).hasMessageMatching(DAO_IS_NULL);
593 public void testUpdatePdpStatisticsGroup() throws PfModelException {
594 assertThatThrownBy(() -> {
595 new PdpProvider().updatePdpStatistics(pfDao, null, null, null, null);
596 }).hasMessageMatching(GROUP_IS_NULL);
598 assertThatThrownBy(() -> {
599 new PdpProvider().updatePdpStatistics(pfDao, null, null, null, new PdpStatistics());
600 }).hasMessageMatching(GROUP_IS_NULL);
602 assertThatThrownBy(() -> {
603 new PdpProvider().updatePdpStatistics(pfDao, null, null, "inst", null);
604 }).hasMessageMatching(GROUP_IS_NULL);
606 assertThatThrownBy(() -> {
607 new PdpProvider().updatePdpStatistics(pfDao, null, null, "inst", new PdpStatistics());
608 }).hasMessageMatching(GROUP_IS_NULL);
610 assertThatThrownBy(() -> {
611 new PdpProvider().updatePdpStatistics(pfDao, null, "TYPE", null, null);
612 }).hasMessageMatching(GROUP_IS_NULL);
614 assertThatThrownBy(() -> {
615 new PdpProvider().updatePdpStatistics(pfDao, null, "TYPE", null, new PdpStatistics());
616 }).hasMessageMatching(GROUP_IS_NULL);
618 assertThatThrownBy(() -> {
619 new PdpProvider().updatePdpStatistics(pfDao, null, "TYPE", "inst", null);
620 }).hasMessageMatching(GROUP_IS_NULL);
622 assertThatThrownBy(() -> {
623 new PdpProvider().updatePdpStatistics(pfDao, null, "TYPE", "inst", new PdpStatistics());
624 }).hasMessageMatching(GROUP_IS_NULL);
626 assertThatThrownBy(() -> {
627 new PdpProvider().updatePdpStatistics(pfDao, "name", null, null, null);
628 }).hasMessageMatching(PDP_TYPE_IS_NULL);
630 assertThatThrownBy(() -> {
631 new PdpProvider().updatePdpStatistics(pfDao, "name", null, null, new PdpStatistics());
632 }).hasMessageMatching(PDP_TYPE_IS_NULL);
634 assertThatThrownBy(() -> {
635 new PdpProvider().updatePdpStatistics(pfDao, "name", null, "inst", null);
636 }).hasMessageMatching(PDP_TYPE_IS_NULL);
638 assertThatThrownBy(() -> {
639 new PdpProvider().updatePdpStatistics(pfDao, "name", null, "inst", new PdpStatistics());
640 }).hasMessageMatching(PDP_TYPE_IS_NULL);
642 assertThatThrownBy(() -> {
643 new PdpProvider().updatePdpStatistics(pfDao, "name", "TYPE", null, null);
644 }).hasMessageMatching("pdpInstanceId is marked .*ull but is null");
646 assertThatThrownBy(() -> {
647 new PdpProvider().updatePdpStatistics(pfDao, "name", "TYPE", null, new PdpStatistics());
648 }).hasMessageMatching("pdpInstanceId is marked .*ull but is null");
650 assertThatThrownBy(() -> {
651 new PdpProvider().updatePdpStatistics(pfDao, "name", "TYPE", "inst", null);
652 }).hasMessageMatching("pdpStatistics is marked .*ull but is null");
654 new PdpProvider().updatePdpStatistics(pfDao, "name", "TYPE", "inst", new PdpStatistics());
658 public void testGetAllPolicyStatusPfDao() throws PfModelException {
659 assertThatThrownBy(() -> {
660 new PdpProvider().getAllPolicyStatus(null);
661 }).hasMessageMatching(DAO_IS_NULL);
663 assertThat(new PdpProvider().getAllPolicyStatus(pfDao)).isEmpty();
665 PdpProvider provider = loadDeployments();
666 assertThat(provider.getAllPolicyStatus(pfDao)).hasSize(5);
669 private PdpProvider loadDeployments() {
670 PdpProvider provider = new PdpProvider();
672 // same name, different version
673 final ToscaConceptIdentifier policy3 = new ToscaConceptIdentifier(MY_POLICY.getName(), "10.20.30");
675 PdpPolicyStatus id1 = statusBuilder.pdpGroup(GROUP_A).pdpId("pdp1").policy(MY_POLICY).build();
676 PdpPolicyStatus id2 = statusBuilder.pdpGroup(GROUP_A).pdpId("pdp2").policy(MY_POLICY2).build();
677 PdpPolicyStatus id3 = statusBuilder.pdpGroup(GROUP_A).pdpId("pdp3").policy(policy3).build();
678 PdpPolicyStatus id4 = statusBuilder.pdpGroup(GROUP_B).pdpId("pdp4").policy(MY_POLICY).build();
679 PdpPolicyStatus id5 = statusBuilder.pdpGroup(GROUP_B).pdpId("pdp5").policy(MY_POLICY2).build();
680 provider.cudPolicyStatus(pfDao, List.of(id1, id2, id3, id4, id5), null, null);
686 public void testGetAllPolicyStatusPfDaoToscaConceptIdentifierOptVersion() throws PfModelException {
687 assertThatThrownBy(() -> {
688 new PdpProvider().getAllPolicyStatus(null, new ToscaConceptIdentifierOptVersion("somePdp", null));
689 }).hasMessageMatching(DAO_IS_NULL);
691 assertThatThrownBy(() -> {
692 new PdpProvider().getAllPolicyStatus(pfDao, null);
693 }).hasMessageContaining("policy").hasMessageContaining("null");
695 assertThat(new PdpProvider().getAllPolicyStatus(pfDao, new ToscaConceptIdentifierOptVersion("somePdp", null)))
698 PdpProvider provider = loadDeployments();
699 assertThat(provider.getAllPolicyStatus(pfDao, new ToscaConceptIdentifierOptVersion(MY_POLICY))).hasSize(2);
700 assertThat(provider.getAllPolicyStatus(pfDao, new ToscaConceptIdentifierOptVersion(MY_POLICY.getName(), null)))
705 public void testGetGroupPolicyStatus() throws PfModelException {
706 assertThatThrownBy(() -> {
707 new PdpProvider().getGroupPolicyStatus(null, "someGroup");
708 }).hasMessageMatching(DAO_IS_NULL);
710 assertThatThrownBy(() -> {
711 new PdpProvider().getGroupPolicyStatus(pfDao, null);
712 }).hasMessageContaining("group").hasMessageContaining("null");
714 assertThat(new PdpProvider().getGroupPolicyStatus(pfDao, PDP_GROUP0)).isEmpty();
716 PdpProvider provider = loadDeployments();
717 assertThat(provider.getGroupPolicyStatus(pfDao, GROUP_A)).hasSize(3);
721 public void cudPolicyStatus() throws PfModelException {
722 PdpProvider prov = new PdpProvider();
724 assertThatThrownBy(() -> prov.cudPolicyStatus(null, List.of(), List.of(), List.of()))
725 .hasMessageMatching(DAO_IS_NULL);
727 // null collections should be OK
728 assertThatCode(() -> prov.cudPolicyStatus(pfDao, null, null, null)).doesNotThrowAnyException();
732 public void cudPolicyStatus_Create() throws PfModelException {
733 PdpProvider prov = new PdpProvider();
735 PdpPolicyStatus idx = statusBuilder.pdpGroup(GROUP_A).pdpId("idX").build();
736 PdpPolicyStatus idy = statusBuilder.pdpGroup(GROUP_A).pdpId("idY").build();
737 PdpPolicyStatus idz = statusBuilder.pdpGroup(GROUP_B).pdpId("idZ").build();
738 prov.cudPolicyStatus(pfDao, List.of(idx, idy), null, null);
739 prov.cudPolicyStatus(pfDao, List.of(idz), null, null);
741 List<PdpPolicyStatus> records = prov.getGroupPolicyStatus(pfDao, GROUP_A);
742 assertThat(records).hasSize(2);
744 Collections.sort(records, (rec1, rec2) -> rec1.getPdpId().compareTo(rec2.getPdpId()));
745 assertThat(records.get(0)).isEqualTo(idx);
746 assertThat(records.get(1)).isEqualTo(idy);
748 records = prov.getGroupPolicyStatus(pfDao, GROUP_B);
749 assertThat(records).hasSize(1);
750 assertThat(records.get(0)).isEqualTo(idz);
754 public void cudPolicyStatus_Update() throws PfModelException {
755 PdpProvider prov = new PdpProvider();
757 PdpPolicyStatus idw = statusBuilder.pdpGroup(GROUP_A).pdpId("wId").build();
758 PdpPolicyStatus idx = statusBuilder.pdpGroup(GROUP_A).pdpId("xId").build();
759 PdpPolicyStatus idy = statusBuilder.pdpGroup(GROUP_A).pdpId("yId").build();
760 PdpPolicyStatus idz = statusBuilder.pdpGroup(GROUP_A).pdpId("zId").build();
761 prov.cudPolicyStatus(pfDao, List.of(idw, idx, idy, idz), null, null);
763 assertThat(prov.getGroupPolicyStatus(pfDao, GROUP_A)).hasSize(4);
766 * Now update some records.
768 idx.setState(State.FAILURE);
769 idz.setState(State.WAITING);
770 prov.cudPolicyStatus(pfDao, null, List.of(idx, idz), null);
771 List<PdpPolicyStatus> records = prov.getGroupPolicyStatus(pfDao, GROUP_A);
772 assertThat(records).hasSize(4);
774 Collections.sort(records, (rec1, rec2) -> rec1.getPdpId().compareTo(rec2.getPdpId()));
775 assertThat(records.get(0)).isEqualTo(idw);
776 assertThat(records.get(1)).isEqualTo(idx);
777 assertThat(records.get(2)).isEqualTo(idy);
778 assertThat(records.get(3)).isEqualTo(idz);
782 public void cudPolicyStatus_Delete() throws PfModelException {
783 PdpProvider prov = new PdpProvider();
785 PdpPolicyStatus idw = statusBuilder.pdpGroup(GROUP_A).pdpId("idW").build();
786 PdpPolicyStatus idx = statusBuilder.pdpGroup(GROUP_A).pdpId("idX").build();
787 PdpPolicyStatus idy = statusBuilder.pdpGroup(GROUP_A).pdpId("idY").build();
788 PdpPolicyStatus idz = statusBuilder.pdpGroup(GROUP_A).pdpId("idZ").build();
789 prov.cudPolicyStatus(pfDao, List.of(idw, idx, idy, idz), null, null);
791 assertThat(prov.getGroupPolicyStatus(pfDao, GROUP_A)).hasSize(4);
794 * Delete some records and then check again.
796 prov.cudPolicyStatus(pfDao, null, null, List.of(idw, idy));
798 List<PdpPolicyStatus> records = prov.getGroupPolicyStatus(pfDao, GROUP_A);
799 assertThat(records).hasSize(2);
801 Collections.sort(records, (rec1, rec2) -> rec1.getPdpId().compareTo(rec2.getPdpId()));
802 assertThat(records.get(0)).isEqualTo(idx);
803 assertThat(records.get(1)).isEqualTo(idz);
807 public void testFromAuthorativeStatus() throws PfModelException {
808 PdpProvider prov = new PdpProvider();
810 assertThatCode(() -> prov.cudPolicyStatus(pfDao, null, null, null)).doesNotThrowAnyException();
812 PdpPolicyStatus ida = statusBuilder.pdpGroup(GROUP_A).pdpId("idA").build();
813 PdpPolicyStatus idb = statusBuilder.pdpGroup(GROUP_A).pdpId("idB").build();
814 PdpPolicyStatus idc = statusBuilder.pdpGroup(GROUP_A).pdpId("idC").build();
815 PdpPolicyStatus idd = statusBuilder.pdpGroup(GROUP_A).pdpId("idD").build();
817 // make a couple invalid records
821 List<PdpPolicyStatus> list = List.of(ida, idb, idc, idd);
824 assertThatCode(() -> prov.cudPolicyStatus(pfDao, list, null, null))
825 .isInstanceOf(PfModelRuntimeException.class)
826 .hasMessageContaining("1").hasMessageContaining("3")
827 .hasMessageNotContaining("0").hasMessageNotContaining("2");
830 assertThat(prov.getGroupPolicyStatus(pfDao, GROUP_A)).isEmpty();