Replace Eclipselink with Hibernate
[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,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
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.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;
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 static final ToscaConceptIdentifier MY_POLICY = new ToscaConceptIdentifier("MyPolicy", "1.2.3");
77     private static final ToscaConceptIdentifier MY_POLICY2 = new ToscaConceptIdentifier("MyPolicyB", "2.3.4");
78
79     private PfDao pfDao;
80     private StandardCoder standardCoder;
81     private PdpPolicyStatusBuilder statusBuilder;
82
83     /**
84      * Set up the DAO towards the database.
85      *
86      * @throws Exception on database errors
87      */
88     @Before
89     public void setupDao() throws Exception {
90         final DaoParameters daoParameters = new DaoParameters();
91         daoParameters.setPluginClass(DefaultPfDao.class.getName());
92
93         daoParameters.setPersistenceUnit("ToscaConceptTest");
94
95         Properties jdbcProperties = new Properties();
96         jdbcProperties.setProperty("javax.persistence.jdbc.user", "policy");
97         jdbcProperties.setProperty("javax.persistence.jdbc.password", "P01icY");
98
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");
102         } else {
103             jdbcProperties.setProperty("javax.persistence.jdbc.driver", "org.h2.Driver");
104             jdbcProperties.setProperty("javax.persistence.jdbc.url", "jdbc:h2:mem:PdpProviderTest");
105         }
106
107         daoParameters.setJdbcProperties(jdbcProperties);
108
109         pfDao = new PfDaoFactory().createPfDao(daoParameters);
110         pfDao.init(daoParameters);
111     }
112
113     /**
114      * Set up GSON.
115      */
116     @Before
117     public void setupGson() {
118         standardCoder = new StandardCoder();
119     }
120
121     /**
122      * Set up Policy Status builder.
123      */
124     @Before
125     public void setupBuilder() {
126         ToscaConceptIdentifier policyType = new ToscaConceptIdentifier("MyPolicyType", "1.2.4");
127
128         statusBuilder = PdpPolicyStatus.builder().deploy(true).pdpType("MyPdpType").policy(MY_POLICY)
129             .policyType(policyType).state(State.SUCCESS);
130     }
131
132     @After
133     public void teardown() {
134         pfDao.close();
135     }
136
137     @Test
138     public void testGroupsGet() throws Exception {
139         assertThatThrownBy(() -> {
140             new PdpProvider().getPdpGroups(null, null);
141         }).hasMessageMatching(DAO_IS_NULL);
142
143         assertThatThrownBy(() -> {
144             new PdpProvider().getPdpGroups(null, "name");
145         }).hasMessageMatching(DAO_IS_NULL);
146
147         String originalJson = ResourceUtils.getResourceAsString(PDP_GROUPS0_JSON);
148         PdpGroups pdpGroups0 = standardCoder.decode(originalJson, PdpGroups.class);
149
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+", ""));
154
155         PdpGroups gotPdpGroups0 = new PdpGroups();
156         gotPdpGroups0.setGroups(new PdpProvider().getPdpGroups(pfDao, PDP_GROUP0));
157
158         String gotJson = standardCoder.encode(gotPdpGroups0);
159
160         assertEquals(originalJson.replaceAll("\\s+", ""), gotJson.replaceAll("\\s+", ""));
161     }
162
163     @Test
164     public void testFilteredPdpGroupGet() throws Exception {
165         assertThatThrownBy(() -> {
166             new PdpProvider().getFilteredPdpGroups(null, null);
167         }).hasMessageMatching(DAO_IS_NULL);
168
169         assertThatThrownBy(() -> {
170             new PdpProvider().getFilteredPdpGroups(null, PdpGroupFilter.builder().build());
171         }).hasMessageMatching(DAO_IS_NULL);
172
173         assertThatThrownBy(() -> {
174             new PdpProvider().getFilteredPdpGroups(pfDao, null);
175         }).hasMessageMatching("filter is marked .*ull but is null");
176
177         String originalJson = ResourceUtils.getResourceAsString("testdata/PdpGroupsForFiltering.json");
178         PdpGroups pdpGroups0 = standardCoder.decode(originalJson, PdpGroups.class);
179
180         assertEquals(5, new PdpProvider().createPdpGroups(pfDao, pdpGroups0.getGroups()).size());
181
182         List<ToscaConceptIdentifier> policyTypeList = new ArrayList<>();
183         policyTypeList.add(new ToscaConceptIdentifier("policy.type.0", "1.2.3"));
184
185         List<ToscaConceptIdentifier> policyList = new ArrayList<>();
186         policyList.add(new ToscaConceptIdentifier("Policy0", "4.5.6"));
187
188         // @formatter:off
189         final PdpGroupFilter filter = PdpGroupFilter.builder()
190                 .groupState(PdpState.PASSIVE)
191                 .name(PDP_GROUP0)
192                 .matchPoliciesExactly(false)
193                 .matchPolicyTypesExactly(false)
194                 .pdpState(PdpState.PASSIVE)
195                 .pdpType("APEX")
196                 .policyTypeList(policyTypeList)
197                 .policyList(policyList)
198                 .build();
199         // @formatter:on
200         assertEquals(1, new PdpProvider().getFilteredPdpGroups(pfDao, filter).size());
201     }
202
203     @Test
204     public void testGroupsCreate() throws Exception {
205         assertThatThrownBy(() -> {
206             new PdpProvider().createPdpGroups(null, null);
207         }).hasMessageMatching(DAO_IS_NULL);
208
209         assertThatThrownBy(() -> {
210             new PdpProvider().createPdpGroups(null, new ArrayList<>());
211         }).hasMessageMatching(DAO_IS_NULL);
212
213         assertThatThrownBy(() -> {
214             new PdpProvider().createPdpGroups(pfDao, null);
215         }).hasMessageMatching("pdpGroups is marked .*ull but is null");
216
217         String originalJson = ResourceUtils.getResourceAsString(PDP_GROUPS0_JSON);
218         PdpGroups pdpGroups0 = standardCoder.decode(originalJson, PdpGroups.class);
219
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+", ""));
224
225         PdpGroups gotPdpGroups0 = new PdpGroups();
226         gotPdpGroups0.setGroups(new PdpProvider().getPdpGroups(pfDao, PDP_GROUP0));
227
228         String gotJson = standardCoder.encode(gotPdpGroups0);
229         assertEquals(originalJson.replaceAll("\\s+", ""), gotJson.replaceAll("\\s+", ""));
230
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);
236     }
237
238     @Test
239     public void testGroupsCreateNoPdp() throws Exception {
240         String originalJson = ResourceUtils.getResourceAsString("testdata/PdpGroupsNoPDPs.json");
241
242         PdpGroups pdpGroups0 = standardCoder.decode(originalJson, PdpGroups.class);
243
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+", ""));
251
252         PdpGroups gotPdpGroups0 = new PdpGroups();
253         gotPdpGroups0.setGroups(new PdpProvider().getPdpGroups(pfDao, "TestPdpGroup"));
254
255         String gotJson = standardCoder.encode(gotPdpGroups0);
256         assertEquals(originalTweakedJson.replaceAll("\\s+", ""), gotJson.replaceAll("\\s+", ""));
257     }
258
259     @Test
260     public void testGroupsUpdate() throws Exception {
261         assertThatThrownBy(() -> {
262             new PdpProvider().updatePdpGroups(null, null);
263         }).hasMessageMatching(DAO_IS_NULL);
264
265         assertThatThrownBy(() -> {
266             new PdpProvider().updatePdpGroups(null, new ArrayList<>());
267         }).hasMessageMatching(DAO_IS_NULL);
268
269         assertThatThrownBy(() -> {
270             new PdpProvider().updatePdpGroups(pfDao, null);
271         }).hasMessageMatching("pdpGroups is marked .*ull but is null");
272
273         String originalJson = ResourceUtils.getResourceAsString(PDP_GROUPS0_JSON);
274         PdpGroups pdpGroups0 = standardCoder.decode(originalJson, PdpGroups.class);
275
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+", ""));
280
281         PdpGroups gotPdpGroups0 = new PdpGroups();
282         gotPdpGroups0.setGroups(new PdpProvider().getPdpGroups(pfDao, PDP_GROUP0));
283
284         String gotJson = standardCoder.encode(gotPdpGroups0);
285         assertEquals(originalJson.replaceAll("\\s+", ""), gotJson.replaceAll("\\s+", ""));
286
287         String updateJson = ResourceUtils.getResourceAsString("testdata/PdpGroups0Update.json");
288         PdpGroups updatePdpGroups0 = standardCoder.decode(updateJson, PdpGroups.class);
289
290         PdpGroups updatedPdpGroups0 = new PdpGroups();
291         updatedPdpGroups0.setGroups(new PdpProvider().updatePdpGroups(pfDao, updatePdpGroups0.getGroups()));
292
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));
296
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);
302     }
303
304     @Test
305     public void testPoliciesDelete() throws Exception {
306         assertThatThrownBy(() -> {
307             new PdpProvider().deletePdpGroup(null, null);
308         }).hasMessageMatching(DAO_IS_NULL);
309
310         assertThatThrownBy(() -> {
311             new PdpProvider().deletePdpGroup(null, "name");
312         }).hasMessageMatching(DAO_IS_NULL);
313
314         assertThatThrownBy(() -> {
315             new PdpProvider().deletePdpGroup(pfDao, null);
316         }).hasMessageMatching("name is marked .*ull but is null");
317
318         assertThatThrownBy(() -> {
319             new PdpProvider().deletePdpGroup(pfDao, "name");
320         }).hasMessage("delete of PDP group \"name:0.0.0\" failed, PDP group does not exist");
321
322         String originalJson = ResourceUtils.getResourceAsString(PDP_GROUPS0_JSON);
323         PdpGroups pdpGroups0 = standardCoder.decode(originalJson, PdpGroups.class);
324
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+", ""));
329
330         PdpGroups gotPdpGroups0 = new PdpGroups();
331         gotPdpGroups0.setGroups(new PdpProvider().getPdpGroups(pfDao, PDP_GROUP0));
332
333         String gotJson = standardCoder.encode(gotPdpGroups0);
334         assertEquals(originalJson.replaceAll("\\s+", ""), gotJson.replaceAll("\\s+", ""));
335
336         PdpGroup deletedPdpGroup = new PdpProvider().deletePdpGroup(pfDao, PDP_GROUP0);
337
338         assertEquals(createdPdpGroups0.getGroups().get(0), deletedPdpGroup);
339
340         assertEquals(0, new PdpProvider().getPdpGroups(pfDao, PDP_GROUP0).size());
341
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");
345     }
346
347     @Test
348     public void testPdpSubgroupUpdate() throws Exception {
349         assertThatThrownBy(() -> {
350             new PdpProvider().updatePdpSubGroup(null, null, null);
351         }).hasMessageMatching(DAO_IS_NULL);
352
353         assertThatThrownBy(() -> {
354             new PdpProvider().updatePdpSubGroup(null, null, new PdpSubGroup());
355         }).hasMessageMatching(DAO_IS_NULL);
356
357         assertThatThrownBy(() -> {
358             new PdpProvider().updatePdpSubGroup(null, "name", null);
359         }).hasMessageMatching(DAO_IS_NULL);
360
361         assertThatThrownBy(() -> {
362             new PdpProvider().updatePdpSubGroup(null, "name", new PdpSubGroup());
363         }).hasMessageMatching(DAO_IS_NULL);
364
365         assertThatThrownBy(() -> {
366             new PdpProvider().updatePdpSubGroup(pfDao, null, null);
367         }).hasMessageMatching(GROUP_IS_NULL);
368
369         assertThatThrownBy(() -> {
370             new PdpProvider().updatePdpSubGroup(pfDao, null, new PdpSubGroup());
371         }).hasMessageMatching(GROUP_IS_NULL);
372
373         assertThatThrownBy(() -> {
374             new PdpProvider().updatePdpSubGroup(pfDao, "name", null);
375         }).hasMessageMatching(SUBGROUP_IS_NULL);
376
377         assertThatThrownBy(() -> {
378             new PdpProvider().updatePdpSubGroup(pfDao, "name", new PdpSubGroup());
379         }).hasMessage("parameter \"localName\" is null");
380
381         String originalJson = ResourceUtils.getResourceAsString(PDP_GROUPS0_JSON);
382         PdpGroups pdpGroups0 = standardCoder.decode(originalJson, PdpGroups.class);
383
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+", ""));
388
389         PdpGroups gotPdpGroups0 = new PdpGroups();
390         gotPdpGroups0.setGroups(new PdpProvider().getPdpGroups(pfDao, PDP_GROUP0));
391
392         String gotJson = standardCoder.encode(gotPdpGroups0);
393         assertEquals(originalJson.replaceAll("\\s+", ""), gotJson.replaceAll("\\s+", ""));
394
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);
399
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());
403
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);
410     }
411
412     @Test
413     public void testPdpUpdate() throws Exception {
414         assertThatThrownBy(() -> {
415             new PdpProvider().updatePdp(null, null, null, null);
416         }).hasMessageMatching(DAO_IS_NULL);
417
418         assertThatThrownBy(() -> {
419             new PdpProvider().updatePdp(null, null, null, new Pdp());
420         }).hasMessageMatching(DAO_IS_NULL);
421
422         assertThatThrownBy(() -> {
423             new PdpProvider().updatePdp(null, null, "TYPE", null);
424         }).hasMessageMatching(DAO_IS_NULL);
425
426         assertThatThrownBy(() -> {
427             new PdpProvider().updatePdp(null, null, "TYPE", new Pdp());
428         }).hasMessageMatching(DAO_IS_NULL);
429
430         assertThatThrownBy(() -> {
431             new PdpProvider().updatePdp(null, "name", null, null);
432         }).hasMessageMatching(DAO_IS_NULL);
433
434         assertThatThrownBy(() -> {
435             new PdpProvider().updatePdp(null, "name", null, new Pdp());
436         }).hasMessageMatching(DAO_IS_NULL);
437
438         assertThatThrownBy(() -> {
439             new PdpProvider().updatePdp(null, "name", "TYPE", null);
440         }).hasMessageMatching(DAO_IS_NULL);
441
442         assertThatThrownBy(() -> {
443             new PdpProvider().updatePdp(null, "name", "TYPE", new Pdp());
444         }).hasMessageMatching(DAO_IS_NULL);
445
446         assertThatThrownBy(() -> {
447             new PdpProvider().updatePdp(pfDao, null, null, null);
448         }).hasMessageMatching(GROUP_IS_NULL);
449
450         assertThatThrownBy(() -> {
451             new PdpProvider().updatePdp(pfDao, null, null, new Pdp());
452         }).hasMessageMatching(GROUP_IS_NULL);
453
454         assertThatThrownBy(() -> {
455             new PdpProvider().updatePdp(pfDao, null, "TYPE", null);
456         }).hasMessageMatching(GROUP_IS_NULL);
457
458         assertThatThrownBy(() -> {
459             new PdpProvider().updatePdp(pfDao, null, "TYPE", new Pdp());
460         }).hasMessageMatching(GROUP_IS_NULL);
461
462         assertThatThrownBy(() -> {
463             new PdpProvider().updatePdp(pfDao, "name", null, null);
464         }).hasMessageMatching(SUBGROUP_IS_NULL);
465
466         assertThatThrownBy(() -> {
467             new PdpProvider().updatePdp(pfDao, "name", null, new Pdp());
468         }).hasMessageMatching(SUBGROUP_IS_NULL);
469
470         assertThatThrownBy(() -> {
471             new PdpProvider().updatePdp(pfDao, "name", "TYPE", null);
472         }).hasMessageMatching("pdp is marked .*ull but is null");
473
474         assertThatThrownBy(() -> {
475             new PdpProvider().updatePdp(pfDao, "name", "TYPE", new Pdp());
476         }).hasMessage("parameter \"localName\" is null");
477
478         String originalJson = ResourceUtils.getResourceAsString(PDP_GROUPS0_JSON);
479         PdpGroups pdpGroups0 = standardCoder.decode(originalJson, PdpGroups.class);
480
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+", ""));
485
486         PdpGroups gotPdpGroups0 = new PdpGroups();
487         gotPdpGroups0.setGroups(new PdpProvider().getPdpGroups(pfDao, PDP_GROUP0));
488
489         String gotJson = standardCoder.encode(gotPdpGroups0);
490         assertEquals(originalJson.replaceAll("\\s+", ""), gotJson.replaceAll("\\s+", ""));
491
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);
496
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());
502
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");
508     }
509
510     @Test
511     public void testGetPdpStatistics() throws PfModelException {
512         assertThatThrownBy(() -> {
513             new PdpProvider().getPdpStatistics(null, null);
514         }).hasMessageMatching(DAO_IS_NULL);
515
516         assertThatThrownBy(() -> {
517             new PdpProvider().getPdpStatistics(null, "name");
518         }).hasMessageMatching(DAO_IS_NULL);
519
520         assertEquals(0, new PdpProvider().getPdpStatistics(pfDao, "name").size());
521     }
522
523     @Test
524     public void testUpdatePdpStatisticsDao() throws PfModelException {
525         assertThatThrownBy(() -> {
526             new PdpProvider().updatePdpStatistics(null, null, null, null, null);
527         }).hasMessageMatching(DAO_IS_NULL);
528
529         assertThatThrownBy(() -> {
530             new PdpProvider().updatePdpStatistics(null, null, null, null, new PdpStatistics());
531         }).hasMessageMatching(DAO_IS_NULL);
532
533         assertThatThrownBy(() -> {
534             new PdpProvider().updatePdpStatistics(null, null, null, "inst", null);
535         }).hasMessageMatching(DAO_IS_NULL);
536
537         assertThatThrownBy(() -> {
538             new PdpProvider().updatePdpStatistics(null, null, null, "inst", new PdpStatistics());
539         }).hasMessageMatching(DAO_IS_NULL);
540
541         assertThatThrownBy(() -> {
542             new PdpProvider().updatePdpStatistics(null, null, "TYPE", null, null);
543         }).hasMessageMatching(DAO_IS_NULL);
544
545         assertThatThrownBy(() -> {
546             new PdpProvider().updatePdpStatistics(null, null, "TYPE", null, new PdpStatistics());
547         }).hasMessageMatching(DAO_IS_NULL);
548
549         assertThatThrownBy(() -> {
550             new PdpProvider().updatePdpStatistics(null, null, "TYPE", "inst", null);
551         }).hasMessageMatching(DAO_IS_NULL);
552
553         assertThatThrownBy(() -> {
554             new PdpProvider().updatePdpStatistics(null, null, "TYPE", "inst", new PdpStatistics());
555         }).hasMessageMatching(DAO_IS_NULL);
556
557         assertThatThrownBy(() -> {
558             new PdpProvider().updatePdpStatistics(null, "name", null, null, null);
559         }).hasMessageMatching(DAO_IS_NULL);
560
561         assertThatThrownBy(() -> {
562             new PdpProvider().updatePdpStatistics(null, "name", null, null, new PdpStatistics());
563         }).hasMessageMatching(DAO_IS_NULL);
564
565         assertThatThrownBy(() -> {
566             new PdpProvider().updatePdpStatistics(null, "name", null, "inst", null);
567         }).hasMessageMatching(DAO_IS_NULL);
568
569         assertThatThrownBy(() -> {
570             new PdpProvider().updatePdpStatistics(null, "name", null, "inst", new PdpStatistics());
571         }).hasMessageMatching(DAO_IS_NULL);
572
573         assertThatThrownBy(() -> {
574             new PdpProvider().updatePdpStatistics(null, "name", "TYPE", null, null);
575         }).hasMessageMatching(DAO_IS_NULL);
576
577         assertThatThrownBy(() -> {
578             new PdpProvider().updatePdpStatistics(null, "name", "TYPE", null, new PdpStatistics());
579         }).hasMessageMatching(DAO_IS_NULL);
580
581         assertThatThrownBy(() -> {
582             new PdpProvider().updatePdpStatistics(null, "name", "TYPE", "inst", null);
583         }).hasMessageMatching(DAO_IS_NULL);
584
585         assertThatThrownBy(() -> {
586             new PdpProvider().updatePdpStatistics(null, "name", "TYPE", "inst", new PdpStatistics());
587         }).hasMessageMatching(DAO_IS_NULL);
588     }
589
590     @Test
591     public void testUpdatePdpStatisticsGroup() throws PfModelException {
592         assertThatThrownBy(() -> {
593             new PdpProvider().updatePdpStatistics(pfDao, null, null, null, null);
594         }).hasMessageMatching(GROUP_IS_NULL);
595
596         assertThatThrownBy(() -> {
597             new PdpProvider().updatePdpStatistics(pfDao, null, null, null, new PdpStatistics());
598         }).hasMessageMatching(GROUP_IS_NULL);
599
600         assertThatThrownBy(() -> {
601             new PdpProvider().updatePdpStatistics(pfDao, null, null, "inst", null);
602         }).hasMessageMatching(GROUP_IS_NULL);
603
604         assertThatThrownBy(() -> {
605             new PdpProvider().updatePdpStatistics(pfDao, null, null, "inst", new PdpStatistics());
606         }).hasMessageMatching(GROUP_IS_NULL);
607
608         assertThatThrownBy(() -> {
609             new PdpProvider().updatePdpStatistics(pfDao, null, "TYPE", null, null);
610         }).hasMessageMatching(GROUP_IS_NULL);
611
612         assertThatThrownBy(() -> {
613             new PdpProvider().updatePdpStatistics(pfDao, null, "TYPE", null, new PdpStatistics());
614         }).hasMessageMatching(GROUP_IS_NULL);
615
616         assertThatThrownBy(() -> {
617             new PdpProvider().updatePdpStatistics(pfDao, null, "TYPE", "inst", null);
618         }).hasMessageMatching(GROUP_IS_NULL);
619
620         assertThatThrownBy(() -> {
621             new PdpProvider().updatePdpStatistics(pfDao, null, "TYPE", "inst", new PdpStatistics());
622         }).hasMessageMatching(GROUP_IS_NULL);
623
624         assertThatThrownBy(() -> {
625             new PdpProvider().updatePdpStatistics(pfDao, "name", null, null, null);
626         }).hasMessageMatching(PDP_TYPE_IS_NULL);
627
628         assertThatThrownBy(() -> {
629             new PdpProvider().updatePdpStatistics(pfDao, "name", null, null, new PdpStatistics());
630         }).hasMessageMatching(PDP_TYPE_IS_NULL);
631
632         assertThatThrownBy(() -> {
633             new PdpProvider().updatePdpStatistics(pfDao, "name", null, "inst", null);
634         }).hasMessageMatching(PDP_TYPE_IS_NULL);
635
636         assertThatThrownBy(() -> {
637             new PdpProvider().updatePdpStatistics(pfDao, "name", null, "inst", new PdpStatistics());
638         }).hasMessageMatching(PDP_TYPE_IS_NULL);
639
640         assertThatThrownBy(() -> {
641             new PdpProvider().updatePdpStatistics(pfDao, "name", "TYPE", null, null);
642         }).hasMessageMatching("pdpInstanceId is marked .*ull but is null");
643
644         assertThatThrownBy(() -> {
645             new PdpProvider().updatePdpStatistics(pfDao, "name", "TYPE", null, new PdpStatistics());
646         }).hasMessageMatching("pdpInstanceId is marked .*ull but is null");
647
648         assertThatThrownBy(() -> {
649             new PdpProvider().updatePdpStatistics(pfDao, "name", "TYPE", "inst", null);
650         }).hasMessageMatching("pdpStatistics is marked .*ull but is null");
651
652         new PdpProvider().updatePdpStatistics(pfDao, "name", "TYPE", "inst", new PdpStatistics());
653     }
654
655     @Test
656     public void testGetAllPolicyStatusPfDao() throws PfModelException {
657         assertThatThrownBy(() -> {
658             new PdpProvider().getAllPolicyStatus(null);
659         }).hasMessageMatching(DAO_IS_NULL);
660
661         assertThat(new PdpProvider().getAllPolicyStatus(pfDao)).isEmpty();
662
663         PdpProvider provider = loadDeployments();
664         assertThat(provider.getAllPolicyStatus(pfDao)).hasSize(5);
665     }
666
667     private PdpProvider loadDeployments() {
668         PdpProvider provider = new PdpProvider();
669
670         // same name, different version
671         final ToscaConceptIdentifier policy3 = new ToscaConceptIdentifier(MY_POLICY.getName(), "10.20.30");
672
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);
679
680         return provider;
681     }
682
683     @Test
684     public void testGetAllPolicyStatusPfDaoToscaConceptIdentifierOptVersion() throws PfModelException {
685         assertThatThrownBy(() -> {
686             new PdpProvider().getAllPolicyStatus(null, new ToscaConceptIdentifierOptVersion("somePdp", null));
687         }).hasMessageMatching(DAO_IS_NULL);
688
689         assertThatThrownBy(() -> {
690             new PdpProvider().getAllPolicyStatus(pfDao, null);
691         }).hasMessageContaining("policy").hasMessageContaining("null");
692
693         assertThat(new PdpProvider().getAllPolicyStatus(pfDao, new ToscaConceptIdentifierOptVersion("somePdp", null)))
694             .isEmpty();
695
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)))
699             .hasSize(3);
700     }
701
702     @Test
703     public void testGetGroupPolicyStatus() throws PfModelException {
704         assertThatThrownBy(() -> {
705             new PdpProvider().getGroupPolicyStatus(null, "someGroup");
706         }).hasMessageMatching(DAO_IS_NULL);
707
708         assertThatThrownBy(() -> {
709             new PdpProvider().getGroupPolicyStatus(pfDao, null);
710         }).hasMessageContaining("group").hasMessageContaining("null");
711
712         assertThat(new PdpProvider().getGroupPolicyStatus(pfDao, PDP_GROUP0)).isEmpty();
713
714         PdpProvider provider = loadDeployments();
715         assertThat(provider.getGroupPolicyStatus(pfDao, GROUP_A)).hasSize(3);
716     }
717
718     @Test
719     public void cudPolicyStatus() throws PfModelException {
720         PdpProvider prov = new PdpProvider();
721
722         assertThatThrownBy(() -> prov.cudPolicyStatus(null, List.of(), List.of(), List.of()))
723             .hasMessageMatching(DAO_IS_NULL);
724
725         // null collections should be OK
726         assertThatCode(() -> prov.cudPolicyStatus(pfDao, null, null, null)).doesNotThrowAnyException();
727     }
728
729     @Test
730     public void cudPolicyStatus_Create() throws PfModelException {
731         PdpProvider prov = new PdpProvider();
732
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);
738
739         List<PdpPolicyStatus> records = prov.getGroupPolicyStatus(pfDao, GROUP_A);
740         assertThat(records).hasSize(2);
741
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);
745
746         records = prov.getGroupPolicyStatus(pfDao, GROUP_B);
747         assertThat(records).hasSize(1);
748         assertThat(records.get(0)).isEqualTo(idz);
749     }
750
751     @Test
752     public void cudPolicyStatus_Update() throws PfModelException {
753         PdpProvider prov = new PdpProvider();
754
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);
760
761         assertThat(prov.getGroupPolicyStatus(pfDao, GROUP_A)).hasSize(4);
762
763         /*
764          * Now update some records.
765          */
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);
771
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);
777     }
778
779     @Test
780     public void cudPolicyStatus_Delete() throws PfModelException {
781         PdpProvider prov = new PdpProvider();
782
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);
788
789         assertThat(prov.getGroupPolicyStatus(pfDao, GROUP_A)).hasSize(4);
790
791         /*
792          * Delete some records and then check again.
793          */
794         prov.cudPolicyStatus(pfDao, null, null, List.of(idw, idy));
795
796         List<PdpPolicyStatus> records = prov.getGroupPolicyStatus(pfDao, GROUP_A);
797         assertThat(records).hasSize(2);
798
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);
802     }
803
804     @Test
805     public void testFromAuthorativeStatus() throws PfModelException {
806         PdpProvider prov = new PdpProvider();
807
808         assertThatCode(() -> prov.cudPolicyStatus(pfDao, null, null, null)).doesNotThrowAnyException();
809
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();
814
815         // make a couple invalid records
816         idb.setState(null);
817         idd.setState(null);
818
819         List<PdpPolicyStatus> list = List.of(ida, idb, idc, idd);
820
821         // @formatter:off
822         assertThatCode(() -> prov.cudPolicyStatus(pfDao, list, null, null))
823             .isInstanceOf(PfModelRuntimeException.class)
824             .hasMessageContaining("1").hasMessageContaining("3")
825             .hasMessageNotContaining("0").hasMessageNotContaining("2");
826         // @formatter:on
827
828         assertThat(prov.getGroupPolicyStatus(pfDao, GROUP_A)).isEmpty();
829     }
830 }