Fix write failure on PDP statistics
[policy/models.git] / models-tosca / src / test / java / org / onap / policy / models / tosca / authorative / provider / AuthorativeToscaProviderPolicyTypeTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019-2021 Nordix Foundation.
4  *  Modifications Copyright (C) 2019 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.tosca.authorative.provider;
23
24 import static org.assertj.core.api.Assertions.assertThatThrownBy;
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertNotNull;
27 import static org.junit.Assert.assertTrue;
28
29 import com.google.gson.GsonBuilder;
30 import java.util.LinkedHashMap;
31 import java.util.List;
32 import java.util.Properties;
33 import org.apache.commons.lang3.ObjectUtils;
34 import org.eclipse.persistence.config.PersistenceUnitProperties;
35 import org.junit.After;
36 import org.junit.Before;
37 import org.junit.BeforeClass;
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.PfConceptKey;
42 import org.onap.policy.models.dao.DaoParameters;
43 import org.onap.policy.models.dao.PfDao;
44 import org.onap.policy.models.dao.PfDaoFactory;
45 import org.onap.policy.models.dao.impl.DefaultPfDao;
46 import org.onap.policy.models.tosca.authorative.concepts.ToscaEntityFilter;
47 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyType;
48 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
49 import org.onap.policy.models.tosca.authorative.concepts.ToscaTopologyTemplate;
50 import org.yaml.snakeyaml.Yaml;
51
52 /**
53  * Test of the {@link AuthorativeToscaProvider} class.
54  */
55 public class AuthorativeToscaProviderPolicyTypeTest {
56     private static final String VERSION = "version";
57     private static final String POLICY_NO_VERSION_VERSION1 = "onap.policies.NoVersion:0.0.1";
58     private static final String POLICY_NO_VERSION = "onap.policies.NoVersion";
59     private static final String MISSING_POLICY_TYPES = "no policy types specified on service template";
60     private static final String DAO_IS_NULL = "^dao is marked .*on.*ull but is null$";
61     private static final String VERSION_001 = "0.0.1";
62     private static String yamlAsJsonString;
63     private PfDao pfDao;
64     private StandardCoder standardCoder;
65
66     /**
67      * Read the policy type definition.
68      *
69      * @throws Exception on errors
70      */
71     @BeforeClass
72     public static void readPolicyDefinition() {
73         String yamlString = ResourceUtils.getResourceAsString("src/test/resources/onap.policies.NoVersion.yaml");
74
75         Object yamlObject = new Yaml().load(yamlString);
76         yamlAsJsonString = new GsonBuilder().setPrettyPrinting().create().toJson(yamlObject);
77     }
78
79     /**
80      * Set up the DAO towards the database.
81      *
82      * @throws Exception on database errors
83      */
84     @Before
85     public void setupDao() throws Exception {
86         final DaoParameters daoParameters = new DaoParameters();
87         daoParameters.setPluginClass(DefaultPfDao.class.getName());
88
89         daoParameters.setPersistenceUnit("ToscaConceptTest");
90
91         Properties jdbcProperties = new Properties();
92         jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_USER, "policy");
93         jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_PASSWORD, "P01icY");
94
95         if (System.getProperty("USE-MARIADB") != null) {
96             jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_DRIVER, "org.mariadb.jdbc.Driver");
97             jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_URL, "jdbc:mariadb://localhost:3306/policy");
98         } else {
99             jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_DRIVER, "org.h2.Driver");
100             jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_URL, "jdbc:h2:mem:testdb");
101         }
102
103         daoParameters.setJdbcProperties(jdbcProperties);
104
105         pfDao = new PfDaoFactory().createPfDao(daoParameters);
106         pfDao.init(daoParameters);
107     }
108
109     /**
110      * Set up GSON.
111      */
112     @Before
113     public void setupGson() {
114         standardCoder = new StandardCoder();
115     }
116
117     @After
118     public void teardown() {
119         pfDao.close();
120     }
121
122     @Test
123     public void testPolicyTypesGet() throws Exception {
124         assertThatThrownBy(() -> {
125             new AuthorativeToscaProvider().getPolicyTypes(null, null, null);
126         }).hasMessageMatching(DAO_IS_NULL);
127
128         assertThatThrownBy(() -> {
129             new AuthorativeToscaProvider().getPolicyList(null, null, null);
130         }).hasMessageMatching(DAO_IS_NULL);
131
132         ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class);
133
134         assertNotNull(toscaServiceTemplate);
135         ToscaServiceTemplate createdServiceTemplate =
136                 new AuthorativeToscaProvider().createPolicyTypes(pfDao, toscaServiceTemplate);
137
138         PfConceptKey policyTypeKey = new PfConceptKey(POLICY_NO_VERSION_VERSION1);
139
140         ToscaPolicyType beforePolicyType = toscaServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
141         ToscaPolicyType createdPolicyType = createdServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
142         assertEquals(true, beforePolicyType.getName().equals(createdPolicyType.getName()));
143         assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), createdPolicyType.getDescription()));
144
145         ToscaServiceTemplate gotServiceTemplate = new AuthorativeToscaProvider().getPolicyTypes(pfDao,
146                 policyTypeKey.getName(), policyTypeKey.getVersion());
147
148         ToscaPolicyType gotPolicyType = gotServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
149         assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
150         assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), createdPolicyType.getDescription()));
151
152         List<ToscaPolicyType> gotPolicyTypeList =
153                 new AuthorativeToscaProvider().getPolicyTypeList(pfDao, POLICY_NO_VERSION, VERSION_001);
154         assertEquals(2, gotPolicyTypeList.size());
155         assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
156
157         gotPolicyTypeList = new AuthorativeToscaProvider().getPolicyTypeList(pfDao, POLICY_NO_VERSION, null);
158         assertEquals(2, gotPolicyTypeList.size());
159         assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
160
161         gotPolicyTypeList = new AuthorativeToscaProvider().getPolicyTypeList(pfDao, null, null);
162         assertEquals(2, gotPolicyTypeList.size());
163         assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
164
165         gotPolicyTypeList = new AuthorativeToscaProvider().getPolicyTypeList(pfDao, null, VERSION_001);
166         assertEquals(2, gotPolicyTypeList.size());
167         assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
168
169         assertThatThrownBy(() -> new AuthorativeToscaProvider().getPolicyTypeList(new DefaultPfDao(), POLICY_NO_VERSION,
170                 VERSION_001)).hasMessageContaining("Policy Framework DAO has not been initialized");
171
172         assertTrue(new AuthorativeToscaProvider().getPolicyTypeList(pfDao, "i.dont.Exist", VERSION_001).isEmpty());
173     }
174
175     @Test
176     public void testPolicyTypesGetFiltered() throws Exception {
177         assertThatThrownBy(() -> {
178             new AuthorativeToscaProvider().getFilteredPolicyTypes(null, null);
179         }).hasMessageMatching(DAO_IS_NULL);
180
181         assertThatThrownBy(() -> {
182             new AuthorativeToscaProvider().getFilteredPolicyTypes(null,
183                     ToscaEntityFilter.<ToscaPolicyType>builder().build());
184         }).hasMessageMatching(DAO_IS_NULL);
185
186         assertThatThrownBy(() -> {
187             new AuthorativeToscaProvider().getFilteredPolicyTypes(pfDao, null);
188         }).hasMessageMatching("^filter is marked .*on.*ull but is null$");
189
190         assertThatThrownBy(() -> {
191             new AuthorativeToscaProvider().getFilteredPolicyTypeList(null, null);
192         }).hasMessageMatching(DAO_IS_NULL);
193
194         assertThatThrownBy(() -> {
195             new AuthorativeToscaProvider().getFilteredPolicyTypeList(null,
196                     ToscaEntityFilter.<ToscaPolicyType>builder().build());
197         }).hasMessageMatching(DAO_IS_NULL);
198
199         assertThatThrownBy(() -> {
200             new AuthorativeToscaProvider().getFilteredPolicyTypeList(pfDao, null);
201         }).hasMessageMatching("^filter is marked .*on.*ull but is null$");
202
203         assertThatThrownBy(() -> new AuthorativeToscaProvider().getFilteredPolicyTypeList(new DefaultPfDao(),
204                 ToscaEntityFilter.<ToscaPolicyType>builder().name("i.dont.Exist").build()))
205                         .hasMessageContaining("Policy Framework DAO has not been initialized");
206
207         assertTrue(new AuthorativeToscaProvider().getFilteredPolicyTypeList(pfDao,
208                 ToscaEntityFilter.<ToscaPolicyType>builder().name("i.dont.Exist").build()).isEmpty());
209
210         ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class);
211
212         assertNotNull(toscaServiceTemplate);
213         ToscaServiceTemplate createdServiceTemplate =
214                 new AuthorativeToscaProvider().createPolicyTypes(pfDao, toscaServiceTemplate);
215
216         PfConceptKey policyTypeKey = new PfConceptKey(POLICY_NO_VERSION_VERSION1);
217
218         ToscaPolicyType beforePolicyType = toscaServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
219         ToscaPolicyType createdPolicyType = createdServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
220         assertEquals(true, beforePolicyType.getName().equals(createdPolicyType.getName()));
221         assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), createdPolicyType.getDescription()));
222
223         ToscaServiceTemplate gotServiceTemplate = new AuthorativeToscaProvider().getFilteredPolicyTypes(pfDao,
224                 ToscaEntityFilter.<ToscaPolicyType>builder().build());
225
226         ToscaPolicyType gotPolicyType = gotServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
227         checkEqualsNameDescription(beforePolicyType, gotPolicyType);
228
229         gotServiceTemplate = new AuthorativeToscaProvider().getFilteredPolicyTypes(pfDao,
230                 ToscaEntityFilter.<ToscaPolicyType>builder().name(policyTypeKey.getName()).build());
231
232         gotPolicyType = gotServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
233         checkEqualsNameDescription(beforePolicyType, gotPolicyType);
234
235         gotServiceTemplate = new AuthorativeToscaProvider().getFilteredPolicyTypes(pfDao, ToscaEntityFilter
236                 .<ToscaPolicyType>builder().name(policyTypeKey.getName()).version(VERSION_001).build());
237
238         gotPolicyType = gotServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
239         checkEqualsNameDescription(beforePolicyType, gotPolicyType);
240
241         List<ToscaPolicyType> gotPolicyTypeList =
242                 new AuthorativeToscaProvider().getPolicyTypeList(pfDao, POLICY_NO_VERSION, VERSION_001);
243         assertEquals(2, gotPolicyTypeList.size());
244         assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
245
246         gotPolicyTypeList = new AuthorativeToscaProvider().getFilteredPolicyTypeList(pfDao,
247                 ToscaEntityFilter.<ToscaPolicyType>builder().build());
248         assertEquals(2, gotPolicyTypeList.size());
249         assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
250
251         gotPolicyTypeList = new AuthorativeToscaProvider().getFilteredPolicyTypeList(pfDao,
252                 ToscaEntityFilter.<ToscaPolicyType>builder().name(policyTypeKey.getName()).build());
253         assertEquals(1, gotPolicyTypeList.size());
254         assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
255
256         gotPolicyTypeList = new AuthorativeToscaProvider().getFilteredPolicyTypeList(pfDao, ToscaEntityFilter
257                 .<ToscaPolicyType>builder().name(policyTypeKey.getName()).version(VERSION_001).build());
258         assertEquals(1, gotPolicyTypeList.size());
259         assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
260
261         gotPolicyTypeList = new AuthorativeToscaProvider().getFilteredPolicyTypeList(pfDao,
262                 ToscaEntityFilter.<ToscaPolicyType>builder().version("1.0.0").build());
263         assertEquals(1, gotPolicyTypeList.size());
264         assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
265     }
266
267     private void checkEqualsNameDescription(ToscaPolicyType beforePolicyType, ToscaPolicyType gotPolicyType) {
268         assertEquals(beforePolicyType.getName(), gotPolicyType.getName());
269         assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), gotPolicyType.getDescription()));
270     }
271
272     @Test
273     public void testPolicyTypesCreate() throws Exception {
274         assertThatThrownBy(() -> {
275             new AuthorativeToscaProvider().createPolicyTypes(null, null);
276         }).hasMessageMatching(DAO_IS_NULL);
277
278         assertThatThrownBy(() -> {
279             new AuthorativeToscaProvider().createPolicyTypes(null, new ToscaServiceTemplate());
280         }).hasMessageMatching(DAO_IS_NULL);
281
282         assertThatThrownBy(() -> {
283             new AuthorativeToscaProvider().createPolicyTypes(pfDao, null);
284         }).hasMessageMatching("^serviceTemplate is marked .*on.*ull but is null$");
285
286         ToscaServiceTemplate testToscaServiceTemplate = new ToscaServiceTemplate();
287         assertThatThrownBy(() -> {
288             new AuthorativeToscaProvider().createPolicyTypes(pfDao, testToscaServiceTemplate);
289         }).hasMessage(MISSING_POLICY_TYPES);
290
291         testToscaServiceTemplate.setPolicyTypes(new LinkedHashMap<>());
292         assertThatThrownBy(() -> {
293             new AuthorativeToscaProvider().createPolicyTypes(pfDao, testToscaServiceTemplate);
294         }).hasMessage("An incoming list of concepts must have at least one entry");
295
296         ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class);
297
298         assertNotNull(toscaServiceTemplate);
299         ToscaServiceTemplate createdServiceTemplate =
300                 new AuthorativeToscaProvider().createPolicyTypes(pfDao, toscaServiceTemplate);
301
302         PfConceptKey policyTypeKey = new PfConceptKey(POLICY_NO_VERSION_VERSION1);
303
304         ToscaPolicyType beforePolicyType = toscaServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
305         ToscaPolicyType createdPolicyType = createdServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
306         assertEquals(true, beforePolicyType.getName().equals(createdPolicyType.getName()));
307         assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), createdPolicyType.getDescription()));
308     }
309
310     @Test
311     public void testPolicyTypesUpdate() throws Exception {
312         assertThatThrownBy(() -> {
313             new AuthorativeToscaProvider().createPolicyTypes(null, null);
314         }).hasMessageMatching(DAO_IS_NULL);
315
316         assertThatThrownBy(() -> {
317             new AuthorativeToscaProvider().updatePolicyTypes(null, null);
318         }).hasMessageMatching(DAO_IS_NULL);
319
320         assertThatThrownBy(() -> {
321             new AuthorativeToscaProvider().updatePolicyTypes(null, new ToscaServiceTemplate());
322         }).hasMessageMatching(DAO_IS_NULL);
323
324         assertThatThrownBy(() -> {
325             new AuthorativeToscaProvider().updatePolicyTypes(pfDao, null);
326         }).hasMessageMatching("^serviceTemplate is marked .*on.*ull but is null$");
327
328         ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class);
329
330         assertNotNull(toscaServiceTemplate);
331         ToscaServiceTemplate createdServiceTemplate =
332                 new AuthorativeToscaProvider().createPolicyTypes(pfDao, toscaServiceTemplate);
333
334         PfConceptKey policyTypeKey = new PfConceptKey(POLICY_NO_VERSION_VERSION1);
335
336         ToscaPolicyType beforePolicyType = toscaServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
337         ToscaPolicyType createdPolicyType = createdServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
338         assertEquals(true, beforePolicyType.getName().equals(createdPolicyType.getName()));
339         assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), createdPolicyType.getDescription()));
340
341         ToscaServiceTemplate updatedServiceTemplate =
342                 new AuthorativeToscaProvider().updatePolicyTypes(pfDao, toscaServiceTemplate);
343
344         ToscaPolicyType updatedPolicy = updatedServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
345         assertEquals(true, beforePolicyType.getName().equals(updatedPolicy.getName()));
346         assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), updatedPolicy.getDescription()));
347     }
348
349     @Test
350     public void testPolicyTypesDelete() throws Exception {
351         assertThatThrownBy(() -> {
352             new AuthorativeToscaProvider().deletePolicyType(null, null, null);
353         }).hasMessageMatching(DAO_IS_NULL);
354
355         assertThatThrownBy(() -> {
356             new AuthorativeToscaProvider().deletePolicyType(null, null, VERSION);
357         }).hasMessageMatching(DAO_IS_NULL);
358
359         assertThatThrownBy(() -> {
360             new AuthorativeToscaProvider().deletePolicyType(null, "name", null);
361         }).hasMessageMatching(DAO_IS_NULL);
362
363         assertThatThrownBy(() -> {
364             new AuthorativeToscaProvider().deletePolicyType(null, "name", VERSION);
365         }).hasMessageMatching(DAO_IS_NULL);
366
367         assertThatThrownBy(() -> {
368             new AuthorativeToscaProvider().deletePolicyType(pfDao, null, null);
369         }).hasMessageMatching("^name is marked .*on.*ull but is null$");
370
371         assertThatThrownBy(() -> {
372             new AuthorativeToscaProvider().deletePolicyType(pfDao, null, VERSION);
373         }).hasMessageMatching("^name is marked .*on.*ull but is null$");
374
375         assertThatThrownBy(() -> {
376             new AuthorativeToscaProvider().deletePolicyType(pfDao, "name", null);
377         }).hasMessageMatching("^version is marked .*on.*ull but is null$");
378
379         ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class);
380
381         assertNotNull(toscaServiceTemplate);
382         ToscaServiceTemplate createdServiceTemplate =
383                 new AuthorativeToscaProvider().createPolicyTypes(pfDao, toscaServiceTemplate);
384
385         PfConceptKey policyTypeKey = new PfConceptKey(POLICY_NO_VERSION_VERSION1);
386
387         ToscaPolicyType beforePolicyType = toscaServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
388         ToscaPolicyType createdPolicyType = createdServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
389         assertEquals(true, beforePolicyType.getName().equals(createdPolicyType.getName()));
390         assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), createdPolicyType.getDescription()));
391
392         ToscaServiceTemplate deletedServiceTemplate = new AuthorativeToscaProvider().deletePolicyType(pfDao,
393                 policyTypeKey.getName(), policyTypeKey.getVersion());
394
395         ToscaPolicyType deletedPolicy = deletedServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
396         assertEquals(true, beforePolicyType.getName().equals(deletedPolicy.getName()));
397         assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), deletedPolicy.getDescription()));
398
399         assertThatThrownBy(() -> {
400             new AuthorativeToscaProvider().getPolicyTypes(pfDao, policyTypeKey.getName(), policyTypeKey.getVersion());
401         }).hasMessage("policy types for onap.policies.NoVersion:0.0.1 do not exist");
402     }
403
404     @Test
405     public void testAssertPoliciesExist() {
406         ToscaServiceTemplate testServiceTemplate = new ToscaServiceTemplate();
407
408         assertThatThrownBy(() -> {
409             new AuthorativeToscaProvider().deletePolicyType(pfDao, "name", null);
410         }).hasMessageMatching("^version is marked .*on.*ull but is null$");
411
412         assertThatThrownBy(() -> {
413             new AuthorativeToscaProvider().createPolicyTypes(pfDao, testServiceTemplate);
414         }).hasMessage(MISSING_POLICY_TYPES);
415
416         testServiceTemplate.setToscaTopologyTemplate(new ToscaTopologyTemplate());
417         assertThatThrownBy(() -> {
418             new AuthorativeToscaProvider().createPolicyTypes(pfDao, testServiceTemplate);
419         }).hasMessage(MISSING_POLICY_TYPES);
420
421         testServiceTemplate.setPolicyTypes(new LinkedHashMap<>());
422         assertThatThrownBy(() -> {
423             new AuthorativeToscaProvider().createPolicyTypes(pfDao, testServiceTemplate);
424         }).hasMessage("An incoming list of concepts must have at least one entry");
425     }
426
427     @Test
428     public void testNullParameters() throws Exception {
429         assertThatThrownBy(() -> new AuthorativeToscaProvider().getPolicyTypeList(null, null, null))
430                 .hasMessageMatching("^dao is marked .*on.*ull but is null$");
431     }
432 }