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