Add PDP-Policy deployment table to DB
[policy/models.git] / models-pdp / src / test / java / org / onap / policy / models / pdp / persistence / provider / PdpProviderTest.java
1 /*-
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
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.models.pdp.persistence.provider;
23
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;
30
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.simple.provider.SimpleToscaProvider;
61
62 /**
63  * Test the {@link SimpleToscaProvider} class.
64  *
65  * @author Liam Fallon (liam.fallon@est.tech)
66  */
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 PfDao pfDao;
77     private StandardCoder standardCoder;
78     private PdpPolicyStatusBuilder statusBuilder;
79
80
81     /**
82      * Set up the DAO towards the database.
83      *
84      * @throws Exception on database errors
85      */
86     @Before
87     public void setupDao() throws Exception {
88         final DaoParameters daoParameters = new DaoParameters();
89         daoParameters.setPluginClass(DefaultPfDao.class.getName());
90
91         daoParameters.setPersistenceUnit("ToscaConceptTest");
92
93         Properties jdbcProperties = new Properties();
94         jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_USER, "policy");
95         jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_PASSWORD, "P01icY");
96
97         // H2, use "org.mariadb.jdbc.Driver" and "jdbc:mariadb://localhost:3306/policy" for locally installed MariaDB
98         jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_DRIVER, "org.h2.Driver");
99         jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_URL, "jdbc:h2:mem:testdb");
100
101         daoParameters.setJdbcProperties(jdbcProperties);
102
103         pfDao = new PfDaoFactory().createPfDao(daoParameters);
104         pfDao.init(daoParameters);
105     }
106
107     /**
108      * Set up GSON.
109      */
110     @Before
111     public void setupGson() {
112         standardCoder = new StandardCoder();
113     }
114
115     /**
116      * Set up Policy Status builder.
117      */
118     @Before
119     public void setupBuilder() {
120         ToscaConceptIdentifier policy = new ToscaConceptIdentifier("MyPolicy", "1.2.3");
121         ToscaConceptIdentifier policyType = new ToscaConceptIdentifier("MyPolicyType", "1.2.4");
122
123         statusBuilder = PdpPolicyStatus.builder().deploy(true).pdpType("MyPdpType").policy(policy)
124                         .policyType(policyType).state(State.SUCCESS);
125     }
126
127     @After
128     public void teardown() {
129         pfDao.close();
130     }
131
132     @Test
133     public void testGroupsGet() throws Exception {
134         assertThatThrownBy(() -> {
135             new PdpProvider().getPdpGroups(null, null);
136         }).hasMessageMatching(DAO_IS_NULL);
137
138         assertThatThrownBy(() -> {
139             new PdpProvider().getPdpGroups(null, "name");
140         }).hasMessageMatching(DAO_IS_NULL);
141
142         String originalJson = ResourceUtils.getResourceAsString(PDP_GROUPS0_JSON);
143         PdpGroups pdpGroups0 = standardCoder.decode(originalJson, PdpGroups.class);
144
145         PdpGroups createdPdpGroups0 = new PdpGroups();
146         createdPdpGroups0.setGroups(new PdpProvider().createPdpGroups(pfDao, pdpGroups0.getGroups()));
147         String createdJson = standardCoder.encode(createdPdpGroups0);
148         assertEquals(originalJson.replaceAll("\\s+", ""), createdJson.replaceAll("\\s+", ""));
149
150         PdpGroups gotPdpGroups0 = new PdpGroups();
151         gotPdpGroups0.setGroups(new PdpProvider().getPdpGroups(pfDao, PDP_GROUP0));
152
153         String gotJson = standardCoder.encode(gotPdpGroups0);
154
155         assertEquals(originalJson.replaceAll("\\s+", ""), gotJson.replaceAll("\\s+", ""));
156     }
157
158     @Test
159     public void testFilteredPdpGroupGet() throws Exception {
160         assertThatThrownBy(() -> {
161             new PdpProvider().getFilteredPdpGroups(null, null);
162         }).hasMessageMatching(DAO_IS_NULL);
163
164         assertThatThrownBy(() -> {
165             new PdpProvider().getFilteredPdpGroups(null, PdpGroupFilter.builder().build());
166         }).hasMessageMatching(DAO_IS_NULL);
167
168         assertThatThrownBy(() -> {
169             new PdpProvider().getFilteredPdpGroups(pfDao, null);
170         }).hasMessageMatching("filter is marked .*ull but is null");
171
172         String originalJson = ResourceUtils.getResourceAsString("testdata/PdpGroupsForFiltering.json");
173         PdpGroups pdpGroups0 = standardCoder.decode(originalJson, PdpGroups.class);
174
175         assertEquals(5, new PdpProvider().createPdpGroups(pfDao, pdpGroups0.getGroups()).size());
176
177         List<ToscaConceptIdentifier> policyTypeList = new ArrayList<>();
178         policyTypeList.add(new ToscaConceptIdentifier("policy.type.0", "1.2.3"));
179
180         List<ToscaConceptIdentifier> policyList = new ArrayList<>();
181         policyList.add(new ToscaConceptIdentifier("Policy0", "4.5.6"));
182
183         // @formatter:off
184         final PdpGroupFilter filter = PdpGroupFilter.builder()
185                 .groupState(PdpState.PASSIVE)
186                 .name(PDP_GROUP0)
187                 .matchPoliciesExactly(false)
188                 .matchPolicyTypesExactly(false)
189                 .pdpState(PdpState.PASSIVE)
190                 .pdpType("APEX")
191                 .policyTypeList(policyTypeList)
192                 .policyList(policyList)
193                 .build();
194         // @formatter:on
195         assertEquals(1, new PdpProvider().getFilteredPdpGroups(pfDao, filter).size());
196     }
197
198     @Test
199     public void testGroupsCreate() throws Exception {
200         assertThatThrownBy(() -> {
201             new PdpProvider().createPdpGroups(null, null);
202         }).hasMessageMatching(DAO_IS_NULL);
203
204         assertThatThrownBy(() -> {
205             new PdpProvider().createPdpGroups(null, new ArrayList<>());
206         }).hasMessageMatching(DAO_IS_NULL);
207
208         assertThatThrownBy(() -> {
209             new PdpProvider().createPdpGroups(pfDao, null);
210         }).hasMessageMatching("pdpGroups is marked .*ull but is null");
211
212         String originalJson = ResourceUtils.getResourceAsString(PDP_GROUPS0_JSON);
213         PdpGroups pdpGroups0 = standardCoder.decode(originalJson, PdpGroups.class);
214
215         PdpGroups createdPdpGroups0 = new PdpGroups();
216         createdPdpGroups0.setGroups(new PdpProvider().createPdpGroups(pfDao, pdpGroups0.getGroups()));
217         String createdJson = standardCoder.encode(createdPdpGroups0);
218         assertEquals(originalJson.replaceAll("\\s+", ""), createdJson.replaceAll("\\s+", ""));
219
220         PdpGroups gotPdpGroups0 = new PdpGroups();
221         gotPdpGroups0.setGroups(new PdpProvider().getPdpGroups(pfDao, PDP_GROUP0));
222
223         String gotJson = standardCoder.encode(gotPdpGroups0);
224         assertEquals(originalJson.replaceAll("\\s+", ""), gotJson.replaceAll("\\s+", ""));
225
226         pdpGroups0.getGroups().get(0).setPdpGroupState(null);
227         assertThatThrownBy(() -> {
228             new PdpProvider().createPdpGroups(pfDao, pdpGroups0.getGroups());
229         }).hasMessageContaining("PDP group").hasMessageContaining("pdpGroupState")
230                         .hasMessageContaining(Validated.IS_NULL);
231     }
232
233     @Test
234     public void testGroupsCreateNoPdp() throws Exception {
235         String originalJson = ResourceUtils.getResourceAsString("testdata/PdpGroupsNoPDPs.json");
236
237         PdpGroups pdpGroups0 = standardCoder.decode(originalJson, PdpGroups.class);
238
239         PdpGroups createdPdpGroups0 = new PdpGroups();
240         createdPdpGroups0.setGroups(new PdpProvider().createPdpGroups(pfDao, pdpGroups0.getGroups()));
241         assertNotEquals(pdpGroups0, createdPdpGroups0);
242         pdpGroups0.getGroups().get(0).getPdpSubgroups().get(0).setPdpInstances(new ArrayList<>());
243         String originalTweakedJson = standardCoder.encode(pdpGroups0);
244         String createdJson = standardCoder.encode(createdPdpGroups0);
245         assertEquals(originalTweakedJson.replaceAll("\\s+", ""), createdJson.replaceAll("\\s+", ""));
246
247         PdpGroups gotPdpGroups0 = new PdpGroups();
248         gotPdpGroups0.setGroups(new PdpProvider().getPdpGroups(pfDao, "TestPdpGroup"));
249
250         String gotJson = standardCoder.encode(gotPdpGroups0);
251         assertEquals(originalTweakedJson.replaceAll("\\s+", ""), gotJson.replaceAll("\\s+", ""));
252     }
253
254     @Test
255     public void testGroupsUpdate() throws Exception {
256         assertThatThrownBy(() -> {
257             new PdpProvider().updatePdpGroups(null, null);
258         }).hasMessageMatching(DAO_IS_NULL);
259
260         assertThatThrownBy(() -> {
261             new PdpProvider().updatePdpGroups(null, new ArrayList<>());
262         }).hasMessageMatching(DAO_IS_NULL);
263
264         assertThatThrownBy(() -> {
265             new PdpProvider().updatePdpGroups(pfDao, null);
266         }).hasMessageMatching("pdpGroups is marked .*ull but is null");
267
268         String originalJson = ResourceUtils.getResourceAsString(PDP_GROUPS0_JSON);
269         PdpGroups pdpGroups0 = standardCoder.decode(originalJson, PdpGroups.class);
270
271         PdpGroups createdPdpGroups0 = new PdpGroups();
272         createdPdpGroups0.setGroups(new PdpProvider().createPdpGroups(pfDao, pdpGroups0.getGroups()));
273         String createdJson = standardCoder.encode(createdPdpGroups0);
274         assertEquals(originalJson.replaceAll("\\s+", ""), createdJson.replaceAll("\\s+", ""));
275
276         PdpGroups gotPdpGroups0 = new PdpGroups();
277         gotPdpGroups0.setGroups(new PdpProvider().getPdpGroups(pfDao, PDP_GROUP0));
278
279         String gotJson = standardCoder.encode(gotPdpGroups0);
280         assertEquals(originalJson.replaceAll("\\s+", ""), gotJson.replaceAll("\\s+", ""));
281
282         String updateJson = ResourceUtils.getResourceAsString("testdata/PdpGroups0Update.json");
283         PdpGroups updatePdpGroups0 = standardCoder.decode(updateJson, PdpGroups.class);
284
285         PdpGroups updatedPdpGroups0 = new PdpGroups();
286         updatedPdpGroups0.setGroups(new PdpProvider().updatePdpGroups(pfDao, updatePdpGroups0.getGroups()));
287
288         List<Pdp> beforePdpInstances = updatePdpGroups0.getGroups().get(0).getPdpSubgroups().get(0).getPdpInstances();
289         List<Pdp> afterPdpInstances = updatedPdpGroups0.getGroups().get(0).getPdpSubgroups().get(0).getPdpInstances();
290         assertTrue(beforePdpInstances.containsAll(afterPdpInstances));
291
292         pdpGroups0.getGroups().get(0).setPdpGroupState(null);
293         assertThatThrownBy(() -> {
294             new PdpProvider().updatePdpGroups(pfDao, pdpGroups0.getGroups());
295         }).hasMessageContaining("PDP group").hasMessageContaining("pdpGroupState")
296                     .hasMessageContaining(Validated.IS_NULL);
297     }
298
299     @Test
300     public void testPoliciesDelete() throws Exception {
301         assertThatThrownBy(() -> {
302             new PdpProvider().deletePdpGroup(null, null);
303         }).hasMessageMatching(DAO_IS_NULL);
304
305         assertThatThrownBy(() -> {
306             new PdpProvider().deletePdpGroup(null, "name");
307         }).hasMessageMatching(DAO_IS_NULL);
308
309         assertThatThrownBy(() -> {
310             new PdpProvider().deletePdpGroup(pfDao, null);
311         }).hasMessageMatching("name is marked .*ull but is null");
312
313         assertThatThrownBy(() -> {
314             new PdpProvider().deletePdpGroup(pfDao, "name");
315         }).hasMessage("delete of PDP group \"name:0.0.0\" failed, PDP group does not exist");
316
317         String originalJson = ResourceUtils.getResourceAsString(PDP_GROUPS0_JSON);
318         PdpGroups pdpGroups0 = standardCoder.decode(originalJson, PdpGroups.class);
319
320         PdpGroups createdPdpGroups0 = new PdpGroups();
321         createdPdpGroups0.setGroups(new PdpProvider().createPdpGroups(pfDao, pdpGroups0.getGroups()));
322         String createdJson = standardCoder.encode(createdPdpGroups0);
323         assertEquals(originalJson.replaceAll("\\s+", ""), createdJson.replaceAll("\\s+", ""));
324
325         PdpGroups gotPdpGroups0 = new PdpGroups();
326         gotPdpGroups0.setGroups(new PdpProvider().getPdpGroups(pfDao, PDP_GROUP0));
327
328         String gotJson = standardCoder.encode(gotPdpGroups0);
329         assertEquals(originalJson.replaceAll("\\s+", ""), gotJson.replaceAll("\\s+", ""));
330
331         PdpGroup deletedPdpGroup = new PdpProvider().deletePdpGroup(pfDao, PDP_GROUP0);
332
333         assertEquals(createdPdpGroups0.getGroups().get(0), deletedPdpGroup);
334
335         assertEquals(0, new PdpProvider().getPdpGroups(pfDao, PDP_GROUP0).size());
336
337         assertThatThrownBy(() -> {
338             new PdpProvider().deletePdpGroup(pfDao, PDP_GROUP0);
339         }).hasMessage("delete of PDP group \"PdpGroup0:0.0.0\" failed, PDP group does not exist");
340     }
341
342     @Test
343     public void testPdpSubgroupUpdate() throws Exception {
344         assertThatThrownBy(() -> {
345             new PdpProvider().updatePdpSubGroup(null, null, null);
346         }).hasMessageMatching(DAO_IS_NULL);
347
348         assertThatThrownBy(() -> {
349             new PdpProvider().updatePdpSubGroup(null, null, new PdpSubGroup());
350         }).hasMessageMatching(DAO_IS_NULL);
351
352         assertThatThrownBy(() -> {
353             new PdpProvider().updatePdpSubGroup(null, "name", null);
354         }).hasMessageMatching(DAO_IS_NULL);
355
356         assertThatThrownBy(() -> {
357             new PdpProvider().updatePdpSubGroup(null, "name", new PdpSubGroup());
358         }).hasMessageMatching(DAO_IS_NULL);
359
360         assertThatThrownBy(() -> {
361             new PdpProvider().updatePdpSubGroup(pfDao, null, null);
362         }).hasMessageMatching(GROUP_IS_NULL);
363
364         assertThatThrownBy(() -> {
365             new PdpProvider().updatePdpSubGroup(pfDao, null, new PdpSubGroup());
366         }).hasMessageMatching(GROUP_IS_NULL);
367
368         assertThatThrownBy(() -> {
369             new PdpProvider().updatePdpSubGroup(pfDao, "name", null);
370         }).hasMessageMatching(SUBGROUP_IS_NULL);
371
372         assertThatThrownBy(() -> {
373             new PdpProvider().updatePdpSubGroup(pfDao, "name", new PdpSubGroup());
374         }).hasMessage("parameter \"localName\" is null");
375
376         String originalJson = ResourceUtils.getResourceAsString(PDP_GROUPS0_JSON);
377         PdpGroups pdpGroups0 = standardCoder.decode(originalJson, PdpGroups.class);
378
379         PdpGroups createdPdpGroups0 = new PdpGroups();
380         createdPdpGroups0.setGroups(new PdpProvider().createPdpGroups(pfDao, pdpGroups0.getGroups()));
381         String createdJson = standardCoder.encode(createdPdpGroups0);
382         assertEquals(originalJson.replaceAll("\\s+", ""), createdJson.replaceAll("\\s+", ""));
383
384         PdpGroups gotPdpGroups0 = new PdpGroups();
385         gotPdpGroups0.setGroups(new PdpProvider().getPdpGroups(pfDao, PDP_GROUP0));
386
387         String gotJson = standardCoder.encode(gotPdpGroups0);
388         assertEquals(originalJson.replaceAll("\\s+", ""), gotJson.replaceAll("\\s+", ""));
389
390         PdpSubGroup existingSubGroup = gotPdpGroups0.getGroups().get(0).getPdpSubgroups().get(0);
391         existingSubGroup.setCurrentInstanceCount(10);
392         existingSubGroup.setDesiredInstanceCount(10);
393         new PdpProvider().updatePdpSubGroup(pfDao, PDP_GROUP0, existingSubGroup);
394
395         List<PdpGroup> afterUpdatePdpGroups = new PdpProvider().getPdpGroups(pfDao, PDP_GROUP0);
396         assertEquals(10, afterUpdatePdpGroups.get(0).getPdpSubgroups().get(0).getCurrentInstanceCount());
397         assertEquals(10, afterUpdatePdpGroups.get(0).getPdpSubgroups().get(0).getDesiredInstanceCount());
398
399         existingSubGroup.setDesiredInstanceCount(-1);
400         assertThatThrownBy(() -> {
401             new PdpProvider().updatePdpSubGroup(pfDao, PDP_GROUP0, existingSubGroup);
402         }).hasMessageContaining("PDP sub group").hasMessageContaining("desiredInstanceCount")
403                         .hasMessageContaining("below the minimum value");
404         existingSubGroup.setDesiredInstanceCount(10);
405     }
406
407     @Test
408     public void testPdpUpdate() throws Exception {
409         assertThatThrownBy(() -> {
410             new PdpProvider().updatePdp(null, null, null, null);
411         }).hasMessageMatching(DAO_IS_NULL);
412
413         assertThatThrownBy(() -> {
414             new PdpProvider().updatePdp(null, null, null, new Pdp());
415         }).hasMessageMatching(DAO_IS_NULL);
416
417         assertThatThrownBy(() -> {
418             new PdpProvider().updatePdp(null, null, "TYPE", null);
419         }).hasMessageMatching(DAO_IS_NULL);
420
421         assertThatThrownBy(() -> {
422             new PdpProvider().updatePdp(null, null, "TYPE", new Pdp());
423         }).hasMessageMatching(DAO_IS_NULL);
424
425         assertThatThrownBy(() -> {
426             new PdpProvider().updatePdp(null, "name", null, null);
427         }).hasMessageMatching(DAO_IS_NULL);
428
429         assertThatThrownBy(() -> {
430             new PdpProvider().updatePdp(null, "name", null, new Pdp());
431         }).hasMessageMatching(DAO_IS_NULL);
432
433         assertThatThrownBy(() -> {
434             new PdpProvider().updatePdp(null, "name", "TYPE", null);
435         }).hasMessageMatching(DAO_IS_NULL);
436
437         assertThatThrownBy(() -> {
438             new PdpProvider().updatePdp(null, "name", "TYPE", new Pdp());
439         }).hasMessageMatching(DAO_IS_NULL);
440
441         assertThatThrownBy(() -> {
442             new PdpProvider().updatePdp(pfDao, null, null, null);
443         }).hasMessageMatching(GROUP_IS_NULL);
444
445         assertThatThrownBy(() -> {
446             new PdpProvider().updatePdp(pfDao, null, null, new Pdp());
447         }).hasMessageMatching(GROUP_IS_NULL);
448
449         assertThatThrownBy(() -> {
450             new PdpProvider().updatePdp(pfDao, null, "TYPE", null);
451         }).hasMessageMatching(GROUP_IS_NULL);
452
453         assertThatThrownBy(() -> {
454             new PdpProvider().updatePdp(pfDao, null, "TYPE", new Pdp());
455         }).hasMessageMatching(GROUP_IS_NULL);
456
457         assertThatThrownBy(() -> {
458             new PdpProvider().updatePdp(pfDao, "name", null, null);
459         }).hasMessageMatching(SUBGROUP_IS_NULL);
460
461         assertThatThrownBy(() -> {
462             new PdpProvider().updatePdp(pfDao, "name", null, new Pdp());
463         }).hasMessageMatching(SUBGROUP_IS_NULL);
464
465         assertThatThrownBy(() -> {
466             new PdpProvider().updatePdp(pfDao, "name", "TYPE", null);
467         }).hasMessageMatching("pdp is marked .*ull but is null");
468
469         assertThatThrownBy(() -> {
470             new PdpProvider().updatePdp(pfDao, "name", "TYPE", new Pdp());
471         }).hasMessage("parameter \"localName\" is null");
472
473         String originalJson = ResourceUtils.getResourceAsString(PDP_GROUPS0_JSON);
474         PdpGroups pdpGroups0 = standardCoder.decode(originalJson, PdpGroups.class);
475
476         PdpGroups createdPdpGroups0 = new PdpGroups();
477         createdPdpGroups0.setGroups(new PdpProvider().createPdpGroups(pfDao, pdpGroups0.getGroups()));
478         String createdJson = standardCoder.encode(createdPdpGroups0);
479         assertEquals(originalJson.replaceAll("\\s+", ""), createdJson.replaceAll("\\s+", ""));
480
481         PdpGroups gotPdpGroups0 = new PdpGroups();
482         gotPdpGroups0.setGroups(new PdpProvider().getPdpGroups(pfDao, PDP_GROUP0));
483
484         String gotJson = standardCoder.encode(gotPdpGroups0);
485         assertEquals(originalJson.replaceAll("\\s+", ""), gotJson.replaceAll("\\s+", ""));
486
487         Pdp existingPdp = gotPdpGroups0.getGroups().get(0).getPdpSubgroups().get(0).getPdpInstances().get(0);
488         existingPdp.setPdpState(PdpState.TEST);
489         existingPdp.setHealthy(PdpHealthStatus.TEST_IN_PROGRESS);
490         new PdpProvider().updatePdp(pfDao, PDP_GROUP0, "APEX", existingPdp);
491
492         List<PdpGroup> afterUpdatePdpGroups = new PdpProvider().getPdpGroups(pfDao, PDP_GROUP0);
493         assertEquals(PdpState.TEST,
494                 afterUpdatePdpGroups.get(0).getPdpSubgroups().get(0).getPdpInstances().get(0).getPdpState());
495         assertEquals(PdpHealthStatus.TEST_IN_PROGRESS,
496                 afterUpdatePdpGroups.get(0).getPdpSubgroups().get(0).getPdpInstances().get(0).getHealthy());
497
498         existingPdp.setMessage("");
499         assertThatThrownBy(() -> {
500             new PdpProvider().updatePdp(pfDao, PDP_GROUP0, "APEX", existingPdp);
501         }).hasMessageContaining("PDP").hasMessageContaining("message").hasMessageContaining(Validated.IS_BLANK);
502         existingPdp.setMessage("A Message");
503     }
504
505     @Test
506     public void testGetPdpStatistics() throws PfModelException {
507         assertThatThrownBy(() -> {
508             new PdpProvider().getPdpStatistics(null, null);
509         }).hasMessageMatching(DAO_IS_NULL);
510
511         assertThatThrownBy(() -> {
512             new PdpProvider().getPdpStatistics(null, "name");
513         }).hasMessageMatching(DAO_IS_NULL);
514
515         assertEquals(0, new PdpProvider().getPdpStatistics(pfDao, "name").size());
516     }
517
518     @Test
519     public void testUpdatePdpStatistics() throws PfModelException {
520         assertThatThrownBy(() -> {
521             new PdpProvider().updatePdpStatistics(null, null, null, null, null);
522         }).hasMessageMatching(DAO_IS_NULL);
523
524         assertThatThrownBy(() -> {
525             new PdpProvider().updatePdpStatistics(null, null, null, null, new PdpStatistics());
526         }).hasMessageMatching(DAO_IS_NULL);
527
528         assertThatThrownBy(() -> {
529             new PdpProvider().updatePdpStatistics(null, null, null, "inst", null);
530         }).hasMessageMatching(DAO_IS_NULL);
531
532         assertThatThrownBy(() -> {
533             new PdpProvider().updatePdpStatistics(null, null, null, "inst", new PdpStatistics());
534         }).hasMessageMatching(DAO_IS_NULL);
535
536         assertThatThrownBy(() -> {
537             new PdpProvider().updatePdpStatistics(null, null, "TYPE", null, null);
538         }).hasMessageMatching(DAO_IS_NULL);
539
540         assertThatThrownBy(() -> {
541             new PdpProvider().updatePdpStatistics(null, null, "TYPE", null, new PdpStatistics());
542         }).hasMessageMatching(DAO_IS_NULL);
543
544         assertThatThrownBy(() -> {
545             new PdpProvider().updatePdpStatistics(null, null, "TYPE", "inst", null);
546         }).hasMessageMatching(DAO_IS_NULL);
547
548         assertThatThrownBy(() -> {
549             new PdpProvider().updatePdpStatistics(null, null, "TYPE", "inst", new PdpStatistics());
550         }).hasMessageMatching(DAO_IS_NULL);
551
552         assertThatThrownBy(() -> {
553             new PdpProvider().updatePdpStatistics(null, "name", null, null, null);
554         }).hasMessageMatching(DAO_IS_NULL);
555
556         assertThatThrownBy(() -> {
557             new PdpProvider().updatePdpStatistics(null, "name", null, null, new PdpStatistics());
558         }).hasMessageMatching(DAO_IS_NULL);
559
560         assertThatThrownBy(() -> {
561             new PdpProvider().updatePdpStatistics(null, "name", null, "inst", null);
562         }).hasMessageMatching(DAO_IS_NULL);
563
564         assertThatThrownBy(() -> {
565             new PdpProvider().updatePdpStatistics(null, "name", null, "inst", new PdpStatistics());
566         }).hasMessageMatching(DAO_IS_NULL);
567
568         assertThatThrownBy(() -> {
569             new PdpProvider().updatePdpStatistics(null, "name", "TYPE", null, null);
570         }).hasMessageMatching(DAO_IS_NULL);
571
572         assertThatThrownBy(() -> {
573             new PdpProvider().updatePdpStatistics(null, "name", "TYPE", null, new PdpStatistics());
574         }).hasMessageMatching(DAO_IS_NULL);
575
576         assertThatThrownBy(() -> {
577             new PdpProvider().updatePdpStatistics(null, "name", "TYPE", "inst", null);
578         }).hasMessageMatching(DAO_IS_NULL);
579
580         assertThatThrownBy(() -> {
581             new PdpProvider().updatePdpStatistics(null, "name", "TYPE", "inst", new PdpStatistics());
582         }).hasMessageMatching(DAO_IS_NULL);
583
584         assertThatThrownBy(() -> {
585             new PdpProvider().updatePdpStatistics(pfDao, null, null, null, null);
586         }).hasMessageMatching(GROUP_IS_NULL);
587
588         assertThatThrownBy(() -> {
589             new PdpProvider().updatePdpStatistics(pfDao, null, null, null, new PdpStatistics());
590         }).hasMessageMatching(GROUP_IS_NULL);
591
592         assertThatThrownBy(() -> {
593             new PdpProvider().updatePdpStatistics(pfDao, null, null, "inst", null);
594         }).hasMessageMatching(GROUP_IS_NULL);
595
596         assertThatThrownBy(() -> {
597             new PdpProvider().updatePdpStatistics(pfDao, null, null, "inst", new PdpStatistics());
598         }).hasMessageMatching(GROUP_IS_NULL);
599
600         assertThatThrownBy(() -> {
601             new PdpProvider().updatePdpStatistics(pfDao, null, "TYPE", null, null);
602         }).hasMessageMatching(GROUP_IS_NULL);
603
604         assertThatThrownBy(() -> {
605             new PdpProvider().updatePdpStatistics(pfDao, null, "TYPE", null, new PdpStatistics());
606         }).hasMessageMatching(GROUP_IS_NULL);
607
608         assertThatThrownBy(() -> {
609             new PdpProvider().updatePdpStatistics(pfDao, null, "TYPE", "inst", null);
610         }).hasMessageMatching(GROUP_IS_NULL);
611
612         assertThatThrownBy(() -> {
613             new PdpProvider().updatePdpStatistics(pfDao, null, "TYPE", "inst", new PdpStatistics());
614         }).hasMessageMatching(GROUP_IS_NULL);
615
616         assertThatThrownBy(() -> {
617             new PdpProvider().updatePdpStatistics(pfDao, "name", null, null, null);
618         }).hasMessageMatching(PDP_TYPE_IS_NULL);
619
620         assertThatThrownBy(() -> {
621             new PdpProvider().updatePdpStatistics(pfDao, "name", null, null, new PdpStatistics());
622         }).hasMessageMatching(PDP_TYPE_IS_NULL);
623
624         assertThatThrownBy(() -> {
625             new PdpProvider().updatePdpStatistics(pfDao, "name", null, "inst", null);
626         }).hasMessageMatching(PDP_TYPE_IS_NULL);
627
628         assertThatThrownBy(() -> {
629             new PdpProvider().updatePdpStatistics(pfDao, "name", null, "inst", new PdpStatistics());
630         }).hasMessageMatching(PDP_TYPE_IS_NULL);
631
632         assertThatThrownBy(() -> {
633             new PdpProvider().updatePdpStatistics(pfDao, "name", "TYPE", null, null);
634         }).hasMessageMatching("pdpInstanceId is marked .*ull but is null");
635
636         assertThatThrownBy(() -> {
637             new PdpProvider().updatePdpStatistics(pfDao, "name", "TYPE", null, new PdpStatistics());
638         }).hasMessageMatching("pdpInstanceId is marked .*ull but is null");
639
640         assertThatThrownBy(() -> {
641             new PdpProvider().updatePdpStatistics(pfDao, "name", "TYPE", "inst", null);
642         }).hasMessageMatching("pdpStatistics is marked .*ull but is null");
643
644         new PdpProvider().updatePdpStatistics(pfDao, "name", "TYPE", "inst", new PdpStatistics());
645     }
646
647     @Test
648     public void testGetGroupPolicyStatus() throws PfModelException {
649         assertThatThrownBy(() -> {
650             new PdpProvider().getGroupPolicyStatus(null, "someGroup");
651         }).hasMessageMatching(DAO_IS_NULL);
652
653         assertThatThrownBy(() -> {
654             new PdpProvider().getGroupPolicyStatus(pfDao, null);
655         }).hasMessageContaining("group").hasMessageContaining("null");
656
657         assertThat(new PdpProvider().getGroupPolicyStatus(pfDao, PDP_GROUP0)).isEmpty();
658     }
659
660     @Test
661     public void cudPolicyStatus() throws PfModelException {
662         PdpProvider prov = new PdpProvider();
663
664         assertThatThrownBy(() -> prov.cudPolicyStatus(null, List.of(), List.of(), List.of()))
665                         .hasMessageMatching(DAO_IS_NULL);
666
667         // null collections should be OK
668         assertThatCode(() -> prov.cudPolicyStatus(pfDao, null, null, null)).doesNotThrowAnyException();
669     }
670
671     @Test
672     public void cudPolicyStatus_Create() throws PfModelException {
673         PdpProvider prov = new PdpProvider();
674
675         PdpPolicyStatus idx = statusBuilder.pdpGroup(GROUP_A).pdpId("idX").build();
676         PdpPolicyStatus idy = statusBuilder.pdpGroup(GROUP_A).pdpId("idY").build();
677         PdpPolicyStatus idz = statusBuilder.pdpGroup(GROUP_B).pdpId("idZ").build();
678         prov.cudPolicyStatus(pfDao, List.of(idx, idy), null, null);
679         prov.cudPolicyStatus(pfDao, List.of(idz), null, null);
680
681         List<PdpPolicyStatus> records = prov.getGroupPolicyStatus(pfDao, GROUP_A);
682         assertThat(records).hasSize(2);
683
684         Collections.sort(records, (rec1, rec2) -> rec1.getPdpId().compareTo(rec2.getPdpId()));
685         assertThat(records.get(0)).isEqualTo(idx);
686         assertThat(records.get(1)).isEqualTo(idy);
687
688         records = prov.getGroupPolicyStatus(pfDao, GROUP_B);
689         assertThat(records).hasSize(1);
690         assertThat(records.get(0)).isEqualTo(idz);
691     }
692
693     @Test
694     public void cudPolicyStatus_Update() throws PfModelException {
695         PdpProvider prov = new PdpProvider();
696
697         PdpPolicyStatus idw = statusBuilder.pdpGroup(GROUP_A).pdpId("wId").build();
698         PdpPolicyStatus idx = statusBuilder.pdpGroup(GROUP_A).pdpId("xId").build();
699         PdpPolicyStatus idy = statusBuilder.pdpGroup(GROUP_A).pdpId("yId").build();
700         PdpPolicyStatus idz = statusBuilder.pdpGroup(GROUP_A).pdpId("zId").build();
701         prov.cudPolicyStatus(pfDao, List.of(idw, idx, idy, idz), null, null);
702
703         assertThat(prov.getGroupPolicyStatus(pfDao, GROUP_A)).hasSize(4);
704
705         /*
706          * Now update some records.
707          */
708         idx.setState(State.FAILURE);
709         idz.setState(State.WAITING);
710         prov.cudPolicyStatus(pfDao, null, List.of(idx, idz), null);
711         List<PdpPolicyStatus> records = prov.getGroupPolicyStatus(pfDao, GROUP_A);
712         assertThat(records).hasSize(4);
713
714         Collections.sort(records, (rec1, rec2) -> rec1.getPdpId().compareTo(rec2.getPdpId()));
715         assertThat(records.get(0)).isEqualTo(idw);
716         assertThat(records.get(1)).isEqualTo(idx);
717         assertThat(records.get(2)).isEqualTo(idy);
718         assertThat(records.get(3)).isEqualTo(idz);
719     }
720
721     @Test
722     public void cudPolicyStatus_Delete() throws PfModelException {
723         PdpProvider prov = new PdpProvider();
724
725         PdpPolicyStatus idw = statusBuilder.pdpGroup(GROUP_A).pdpId("idW").build();
726         PdpPolicyStatus idx = statusBuilder.pdpGroup(GROUP_A).pdpId("idX").build();
727         PdpPolicyStatus idy = statusBuilder.pdpGroup(GROUP_A).pdpId("idY").build();
728         PdpPolicyStatus idz = statusBuilder.pdpGroup(GROUP_A).pdpId("idZ").build();
729         prov.cudPolicyStatus(pfDao, List.of(idw, idx, idy, idz), null, null);
730
731         assertThat(prov.getGroupPolicyStatus(pfDao, GROUP_A)).hasSize(4);
732
733         /*
734          * Delete some records and then check again.
735          */
736         prov.cudPolicyStatus(pfDao, null, null, List.of(idw, idy));
737
738         List<PdpPolicyStatus> records = prov.getGroupPolicyStatus(pfDao, GROUP_A);
739         assertThat(records).hasSize(2);
740
741         Collections.sort(records, (rec1, rec2) -> rec1.getPdpId().compareTo(rec2.getPdpId()));
742         assertThat(records.get(0)).isEqualTo(idx);
743         assertThat(records.get(1)).isEqualTo(idz);
744     }
745
746     @Test
747     public void testFromAuthorativeStatus() throws PfModelException {
748         PdpProvider prov = new PdpProvider();
749
750         assertThatCode(() -> prov.cudPolicyStatus(pfDao, null, null, null)).doesNotThrowAnyException();
751
752         PdpPolicyStatus ida = statusBuilder.pdpGroup(GROUP_A).pdpId("idA").build();
753         PdpPolicyStatus idb = statusBuilder.pdpGroup(GROUP_A).pdpId("idB").build();
754         PdpPolicyStatus idc = statusBuilder.pdpGroup(GROUP_A).pdpId("idC").build();
755         PdpPolicyStatus idd = statusBuilder.pdpGroup(GROUP_A).pdpId("idD").build();
756
757         // make a couple invalid records
758         idb.setState(null);
759         idd.setState(null);
760
761         List<PdpPolicyStatus> list = List.of(ida, idb, idc, idd);
762
763         // @formatter:off
764         assertThatCode(() -> prov.cudPolicyStatus(pfDao, list, null, null))
765             .isInstanceOf(PfModelRuntimeException.class)
766             .hasMessageContaining("1").hasMessageContaining("3")
767             .hasMessageNotContaining("0").hasMessageNotContaining("2");
768         // @formatter:on
769
770         assertThat(prov.getGroupPolicyStatus(pfDao, GROUP_A)).isEmpty();
771     }
772 }