Implement validation and hierarchical get
[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-2020 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
31 import java.util.LinkedHashMap;
32 import java.util.List;
33 import java.util.Properties;
34
35 import org.apache.commons.lang3.ObjectUtils;
36 import org.eclipse.persistence.config.PersistenceUnitProperties;
37 import org.junit.After;
38 import org.junit.Before;
39 import org.junit.BeforeClass;
40 import org.junit.Test;
41 import org.onap.policy.common.utils.coder.StandardCoder;
42 import org.onap.policy.common.utils.resources.ResourceUtils;
43 import org.onap.policy.models.base.PfConceptKey;
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.tosca.authorative.concepts.ToscaPolicyType;
49 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeFilter;
50 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
51 import org.onap.policy.models.tosca.authorative.concepts.ToscaTopologyTemplate;
52 import org.yaml.snakeyaml.Yaml;
53
54 /**
55  * Test of the {@link AuthorativeToscaProvider} class.
56  *
57  * @author Liam Fallon (liam.fallon@est.tech)
58  */
59 public class AuthorativeToscaProviderPolicyTypeTest {
60     private static final String VERSION = "version";
61     private static final String POLICY_NO_VERSION_VERSION0 = "onap.policies.NoVersion:0.0.0";
62     private static final String POLICY_NO_VERSION = "onap.policies.NoVersion";
63     private static final String MISSING_POLICY_TYPES = "no policy types specified on service template";
64     private static final String DAO_IS_NULL = "^dao is marked .*on.*ull but is null$";
65     private static final String VERSION_000 = "0.0.0";
66     private static String yamlAsJsonString;
67     private PfDao pfDao;
68     private StandardCoder standardCoder;
69
70     /**
71      * Read the policy type definition.
72      *
73      * @throws Exception on errors
74      */
75     @BeforeClass
76     public static void readPolicyDefinition() {
77         String yamlString = ResourceUtils.getResourceAsString("src/test/resources/onap.policies.NoVersion.yaml");
78
79         Object yamlObject = new Yaml().load(yamlString);
80         yamlAsJsonString = new GsonBuilder().setPrettyPrinting().create().toJson(yamlObject);
81     }
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(PersistenceUnitProperties.JDBC_USER, "policy");
97         jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_PASSWORD, "P01icY");
98
99         // H2, use "org.mariadb.jdbc.Driver" and "jdbc:mariadb://localhost:3306/policy" for locally installed MariaDB
100         jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_DRIVER, "org.h2.Driver");
101         jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_URL, "jdbc:h2:mem:testdb");
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_VERSION0);
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_000);
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_000);
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_000)).hasMessageContaining("Policy Framework DAO has not been initialized");
171
172         assertTrue(new AuthorativeToscaProvider().getPolicyTypeList(pfDao, "i.dont.Exist", VERSION_000).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, ToscaPolicyTypeFilter.builder().build());
183         }).hasMessageMatching(DAO_IS_NULL);
184
185         assertThatThrownBy(() -> {
186             new AuthorativeToscaProvider().getFilteredPolicyTypes(pfDao, null);
187         }).hasMessageMatching("^filter is marked .*on.*ull but is null$");
188
189         assertThatThrownBy(() -> {
190             new AuthorativeToscaProvider().getFilteredPolicyTypeList(null, null);
191         }).hasMessageMatching(DAO_IS_NULL);
192
193         assertThatThrownBy(() -> {
194             new AuthorativeToscaProvider().getFilteredPolicyTypeList(null, ToscaPolicyTypeFilter.builder().build());
195         }).hasMessageMatching(DAO_IS_NULL);
196
197         assertThatThrownBy(() -> {
198             new AuthorativeToscaProvider().getFilteredPolicyTypeList(pfDao, null);
199         }).hasMessageMatching("^filter is marked .*on.*ull but is null$");
200
201         assertThatThrownBy(() -> new AuthorativeToscaProvider().getFilteredPolicyTypeList(new DefaultPfDao(),
202                 ToscaPolicyTypeFilter.builder().name("i.dont.Exist").build()))
203                         .hasMessageContaining("Policy Framework DAO has not been initialized");
204
205         assertTrue(new AuthorativeToscaProvider()
206                 .getFilteredPolicyTypeList(pfDao, ToscaPolicyTypeFilter.builder().name("i.dont.Exist").build())
207                 .isEmpty());
208
209         ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class);
210
211         assertNotNull(toscaServiceTemplate);
212         ToscaServiceTemplate createdServiceTemplate =
213                 new AuthorativeToscaProvider().createPolicyTypes(pfDao, toscaServiceTemplate);
214
215         PfConceptKey policyTypeKey = new PfConceptKey(POLICY_NO_VERSION_VERSION0);
216
217         ToscaPolicyType beforePolicyType = toscaServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
218         ToscaPolicyType createdPolicyType = createdServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
219         assertEquals(true, beforePolicyType.getName().equals(createdPolicyType.getName()));
220         assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), createdPolicyType.getDescription()));
221
222         ToscaServiceTemplate gotServiceTemplate =
223                 new AuthorativeToscaProvider().getFilteredPolicyTypes(pfDao, ToscaPolicyTypeFilter.builder().build());
224
225         ToscaPolicyType gotPolicyType = gotServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
226         assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
227         assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), gotPolicyType.getDescription()));
228
229         gotServiceTemplate = new AuthorativeToscaProvider().getFilteredPolicyTypes(pfDao,
230                 ToscaPolicyTypeFilter.builder().name(policyTypeKey.getName()).build());
231
232         gotPolicyType = gotServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
233         assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
234         assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), gotPolicyType.getDescription()));
235
236         gotServiceTemplate = new AuthorativeToscaProvider().getFilteredPolicyTypes(pfDao,
237                 ToscaPolicyTypeFilter.builder().name(policyTypeKey.getName()).version(VERSION_000).build());
238
239         gotPolicyType = gotServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
240         assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
241         assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), gotPolicyType.getDescription()));
242
243         List<ToscaPolicyType> gotPolicyTypeList =
244                 new AuthorativeToscaProvider().getPolicyTypeList(pfDao, POLICY_NO_VERSION, VERSION_000);
245         assertEquals(2, gotPolicyTypeList.size());
246         assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
247
248         gotPolicyTypeList = new AuthorativeToscaProvider().getFilteredPolicyTypeList(pfDao,
249                 ToscaPolicyTypeFilter.builder().build());
250         assertEquals(2, gotPolicyTypeList.size());
251         assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
252
253         gotPolicyTypeList = new AuthorativeToscaProvider().getFilteredPolicyTypeList(pfDao,
254                 ToscaPolicyTypeFilter.builder().name(policyTypeKey.getName()).build());
255         assertEquals(1, gotPolicyTypeList.size());
256         assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
257
258         gotPolicyTypeList = new AuthorativeToscaProvider().getFilteredPolicyTypeList(pfDao,
259                 ToscaPolicyTypeFilter.builder().name(policyTypeKey.getName()).version(VERSION_000).build());
260         assertEquals(1, gotPolicyTypeList.size());
261         assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
262
263         gotPolicyTypeList = new AuthorativeToscaProvider().getFilteredPolicyTypeList(pfDao,
264                 ToscaPolicyTypeFilter.builder().version("1.0.0").build());
265         assertEquals(1, gotPolicyTypeList.size());
266         assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
267     }
268
269     @Test
270     public void testPolicyTypesCreate() throws Exception {
271         assertThatThrownBy(() -> {
272             new AuthorativeToscaProvider().createPolicyTypes(null, null);
273         }).hasMessageMatching(DAO_IS_NULL);
274
275         assertThatThrownBy(() -> {
276             new AuthorativeToscaProvider().createPolicyTypes(null, new ToscaServiceTemplate());
277         }).hasMessageMatching(DAO_IS_NULL);
278
279         assertThatThrownBy(() -> {
280             new AuthorativeToscaProvider().createPolicyTypes(pfDao, null);
281         }).hasMessageMatching("^serviceTemplate is marked .*on.*ull but is null$");
282
283         ToscaServiceTemplate testToscaServiceTemplate = new ToscaServiceTemplate();
284         assertThatThrownBy(() -> {
285             new AuthorativeToscaProvider().createPolicyTypes(pfDao, testToscaServiceTemplate);
286         }).hasMessage(MISSING_POLICY_TYPES);
287
288         testToscaServiceTemplate.setPolicyTypes(new LinkedHashMap<>());
289         assertThatThrownBy(() -> {
290             new AuthorativeToscaProvider().createPolicyTypes(pfDao, testToscaServiceTemplate);
291         }).hasMessage("An incoming list of concepts must have at least one entry");
292
293         ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class);
294
295         assertNotNull(toscaServiceTemplate);
296         ToscaServiceTemplate createdServiceTemplate =
297                 new AuthorativeToscaProvider().createPolicyTypes(pfDao, toscaServiceTemplate);
298
299         PfConceptKey policyTypeKey = new PfConceptKey(POLICY_NO_VERSION_VERSION0);
300
301         ToscaPolicyType beforePolicyType = toscaServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
302         ToscaPolicyType createdPolicyType = createdServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
303         assertEquals(true, beforePolicyType.getName().equals(createdPolicyType.getName()));
304         assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), createdPolicyType.getDescription()));
305     }
306
307     @Test
308     public void testPolicyTypesUpdate() throws Exception {
309         assertThatThrownBy(() -> {
310             new AuthorativeToscaProvider().createPolicyTypes(null, null);
311         }).hasMessageMatching(DAO_IS_NULL);
312
313         assertThatThrownBy(() -> {
314             new AuthorativeToscaProvider().updatePolicyTypes(null, null);
315         }).hasMessageMatching(DAO_IS_NULL);
316
317         assertThatThrownBy(() -> {
318             new AuthorativeToscaProvider().updatePolicyTypes(null, new ToscaServiceTemplate());
319         }).hasMessageMatching(DAO_IS_NULL);
320
321         assertThatThrownBy(() -> {
322             new AuthorativeToscaProvider().updatePolicyTypes(pfDao, null);
323         }).hasMessageMatching("^serviceTemplate is marked .*on.*ull but is null$");
324
325         ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class);
326
327         assertNotNull(toscaServiceTemplate);
328         ToscaServiceTemplate createdServiceTemplate =
329                 new AuthorativeToscaProvider().createPolicyTypes(pfDao, toscaServiceTemplate);
330
331         PfConceptKey policyTypeKey = new PfConceptKey(POLICY_NO_VERSION_VERSION0);
332
333         ToscaPolicyType beforePolicyType = toscaServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
334         ToscaPolicyType createdPolicyType = createdServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
335         assertEquals(true, beforePolicyType.getName().equals(createdPolicyType.getName()));
336         assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), createdPolicyType.getDescription()));
337
338         ToscaServiceTemplate updatedServiceTemplate =
339                 new AuthorativeToscaProvider().updatePolicyTypes(pfDao, toscaServiceTemplate);
340
341         ToscaPolicyType updatedPolicy = updatedServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
342         assertEquals(true, beforePolicyType.getName().equals(updatedPolicy.getName()));
343         assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), updatedPolicy.getDescription()));
344     }
345
346     @Test
347     public void testPolicyTypesDelete() throws Exception {
348         assertThatThrownBy(() -> {
349             new AuthorativeToscaProvider().deletePolicyType(null, null, null);
350         }).hasMessageMatching(DAO_IS_NULL);
351
352         assertThatThrownBy(() -> {
353             new AuthorativeToscaProvider().deletePolicyType(null, null, VERSION);
354         }).hasMessageMatching(DAO_IS_NULL);
355
356         assertThatThrownBy(() -> {
357             new AuthorativeToscaProvider().deletePolicyType(null, "name", null);
358         }).hasMessageMatching(DAO_IS_NULL);
359
360         assertThatThrownBy(() -> {
361             new AuthorativeToscaProvider().deletePolicyType(null, "name", VERSION);
362         }).hasMessageMatching(DAO_IS_NULL);
363
364         assertThatThrownBy(() -> {
365             new AuthorativeToscaProvider().deletePolicyType(pfDao, null, null);
366         }).hasMessageMatching("^name is marked .*on.*ull but is null$");
367
368         assertThatThrownBy(() -> {
369             new AuthorativeToscaProvider().deletePolicyType(pfDao, null, VERSION);
370         }).hasMessageMatching("^name is marked .*on.*ull but is null$");
371
372         assertThatThrownBy(() -> {
373             new AuthorativeToscaProvider().deletePolicyType(pfDao, "name", null);
374         }).hasMessageMatching("^version is marked .*on.*ull but is null$");
375
376         ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class);
377
378         assertNotNull(toscaServiceTemplate);
379         ToscaServiceTemplate createdServiceTemplate =
380                 new AuthorativeToscaProvider().createPolicyTypes(pfDao, toscaServiceTemplate);
381
382         PfConceptKey policyTypeKey = new PfConceptKey(POLICY_NO_VERSION_VERSION0);
383
384         ToscaPolicyType beforePolicyType = toscaServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
385         ToscaPolicyType createdPolicyType = createdServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
386         assertEquals(true, beforePolicyType.getName().equals(createdPolicyType.getName()));
387         assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), createdPolicyType.getDescription()));
388
389         ToscaServiceTemplate deletedServiceTemplate = new AuthorativeToscaProvider().deletePolicyType(pfDao,
390                 policyTypeKey.getName(), policyTypeKey.getVersion());
391
392         ToscaPolicyType deletedPolicy = deletedServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
393         assertEquals(true, beforePolicyType.getName().equals(deletedPolicy.getName()));
394         assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), deletedPolicy.getDescription()));
395
396         assertThatThrownBy(() -> {
397             new AuthorativeToscaProvider().getPolicyTypes(pfDao, policyTypeKey.getName(), policyTypeKey.getVersion());
398         }).hasMessage("policy types for onap.policies.NoVersion:0.0.0 do not exist");
399     }
400
401     @Test
402     public void testAssertPoliciesExist() {
403         ToscaServiceTemplate testServiceTemplate = new ToscaServiceTemplate();
404
405         assertThatThrownBy(() -> {
406             new AuthorativeToscaProvider().deletePolicyType(pfDao, "name", null);
407         }).hasMessageMatching("^version is marked .*on.*ull but is null$");
408
409         assertThatThrownBy(() -> {
410             new AuthorativeToscaProvider().createPolicyTypes(pfDao, testServiceTemplate);
411         }).hasMessage(MISSING_POLICY_TYPES);
412
413         testServiceTemplate.setToscaTopologyTemplate(new ToscaTopologyTemplate());
414         assertThatThrownBy(() -> {
415             new AuthorativeToscaProvider().createPolicyTypes(pfDao, testServiceTemplate);
416         }).hasMessage(MISSING_POLICY_TYPES);
417
418         testServiceTemplate.setPolicyTypes(new LinkedHashMap<>());
419         assertThatThrownBy(() -> {
420             new AuthorativeToscaProvider().createPolicyTypes(pfDao, testServiceTemplate);
421         }).hasMessage("An incoming list of concepts must have at least one entry");
422     }
423 }