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