fff1aca50879bc2046fef7e9d9323392f71f2ab6
[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, 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.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,
101                             "jdbc:h2:mem:AuthorativeToscaProviderPolicyTypeTest");
102         }
103
104         daoParameters.setJdbcProperties(jdbcProperties);
105
106         pfDao = new PfDaoFactory().createPfDao(daoParameters);
107         pfDao.init(daoParameters);
108     }
109
110     /**
111      * Set up GSON.
112      */
113     @Before
114     public void setupGson() {
115         standardCoder = new StandardCoder();
116     }
117
118     @After
119     public void teardown() {
120         pfDao.close();
121     }
122
123     @Test
124     public void testPolicyTypesGet() throws Exception {
125         assertThatThrownBy(() -> {
126             new AuthorativeToscaProvider().getPolicyTypes(null, null, null);
127         }).hasMessageMatching(DAO_IS_NULL);
128
129         assertThatThrownBy(() -> {
130             new AuthorativeToscaProvider().getPolicyList(null, null, null);
131         }).hasMessageMatching(DAO_IS_NULL);
132
133         ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class);
134
135         assertNotNull(toscaServiceTemplate);
136         ToscaServiceTemplate createdServiceTemplate =
137                 new AuthorativeToscaProvider().createPolicyTypes(pfDao, toscaServiceTemplate);
138
139         PfConceptKey policyTypeKey = new PfConceptKey(POLICY_NO_VERSION_VERSION1);
140
141         ToscaPolicyType beforePolicyType = toscaServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
142         ToscaPolicyType createdPolicyType = createdServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
143         assertEquals(true, beforePolicyType.getName().equals(createdPolicyType.getName()));
144         assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), createdPolicyType.getDescription()));
145
146         ToscaServiceTemplate gotServiceTemplate = new AuthorativeToscaProvider().getPolicyTypes(pfDao,
147                 policyTypeKey.getName(), policyTypeKey.getVersion());
148
149         ToscaPolicyType gotPolicyType = gotServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
150         assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
151         assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), createdPolicyType.getDescription()));
152
153         List<ToscaPolicyType> gotPolicyTypeList =
154                 new AuthorativeToscaProvider().getPolicyTypeList(pfDao, POLICY_NO_VERSION, VERSION_001);
155         assertEquals(2, gotPolicyTypeList.size());
156         assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
157
158         gotPolicyTypeList = new AuthorativeToscaProvider().getPolicyTypeList(pfDao, POLICY_NO_VERSION, null);
159         assertEquals(2, gotPolicyTypeList.size());
160         assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
161
162         gotPolicyTypeList = new AuthorativeToscaProvider().getPolicyTypeList(pfDao, null, null);
163         assertEquals(2, gotPolicyTypeList.size());
164         assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
165
166         gotPolicyTypeList = new AuthorativeToscaProvider().getPolicyTypeList(pfDao, null, VERSION_001);
167         assertEquals(2, gotPolicyTypeList.size());
168         assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
169
170         assertThatThrownBy(() -> new AuthorativeToscaProvider().getPolicyTypeList(new DefaultPfDao(), POLICY_NO_VERSION,
171                 VERSION_001)).hasMessageContaining("Policy Framework DAO has not been initialized");
172
173         assertTrue(new AuthorativeToscaProvider().getPolicyTypeList(pfDao, "i.dont.Exist", VERSION_001).isEmpty());
174     }
175
176     @Test
177     public void testPolicyTypesGetFiltered() throws Exception {
178         assertThatThrownBy(() -> {
179             new AuthorativeToscaProvider().getFilteredPolicyTypes(null, null);
180         }).hasMessageMatching(DAO_IS_NULL);
181
182         assertThatThrownBy(() -> {
183             new AuthorativeToscaProvider().getFilteredPolicyTypes(null,
184                     ToscaEntityFilter.<ToscaPolicyType>builder().build());
185         }).hasMessageMatching(DAO_IS_NULL);
186
187         assertThatThrownBy(() -> {
188             new AuthorativeToscaProvider().getFilteredPolicyTypes(pfDao, null);
189         }).hasMessageMatching("^filter is marked .*on.*ull but is null$");
190
191         assertThatThrownBy(() -> {
192             new AuthorativeToscaProvider().getFilteredPolicyTypeList(null, null);
193         }).hasMessageMatching(DAO_IS_NULL);
194
195         assertThatThrownBy(() -> {
196             new AuthorativeToscaProvider().getFilteredPolicyTypeList(null,
197                     ToscaEntityFilter.<ToscaPolicyType>builder().build());
198         }).hasMessageMatching(DAO_IS_NULL);
199
200         assertThatThrownBy(() -> {
201             new AuthorativeToscaProvider().getFilteredPolicyTypeList(pfDao, null);
202         }).hasMessageMatching("^filter is marked .*on.*ull but is null$");
203
204         assertThatThrownBy(() -> new AuthorativeToscaProvider().getFilteredPolicyTypeList(new DefaultPfDao(),
205                 ToscaEntityFilter.<ToscaPolicyType>builder().name("i.dont.Exist").build()))
206                         .hasMessageContaining("Policy Framework DAO has not been initialized");
207
208         assertTrue(new AuthorativeToscaProvider().getFilteredPolicyTypeList(pfDao,
209                 ToscaEntityFilter.<ToscaPolicyType>builder().name("i.dont.Exist").build()).isEmpty());
210
211         ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class);
212
213         assertNotNull(toscaServiceTemplate);
214         ToscaServiceTemplate createdServiceTemplate =
215                 new AuthorativeToscaProvider().createPolicyTypes(pfDao, toscaServiceTemplate);
216
217         PfConceptKey policyTypeKey = new PfConceptKey(POLICY_NO_VERSION_VERSION1);
218
219         ToscaPolicyType beforePolicyType = toscaServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
220         ToscaPolicyType createdPolicyType = createdServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
221         assertEquals(true, beforePolicyType.getName().equals(createdPolicyType.getName()));
222         assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), createdPolicyType.getDescription()));
223
224         ToscaServiceTemplate gotServiceTemplate = new AuthorativeToscaProvider().getFilteredPolicyTypes(pfDao,
225                 ToscaEntityFilter.<ToscaPolicyType>builder().build());
226
227         ToscaPolicyType gotPolicyType = gotServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
228         checkEqualsNameDescription(beforePolicyType, gotPolicyType);
229
230         gotServiceTemplate = new AuthorativeToscaProvider().getFilteredPolicyTypes(pfDao,
231                 ToscaEntityFilter.<ToscaPolicyType>builder().name(policyTypeKey.getName()).build());
232
233         gotPolicyType = gotServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
234         checkEqualsNameDescription(beforePolicyType, gotPolicyType);
235
236         gotServiceTemplate = new AuthorativeToscaProvider().getFilteredPolicyTypes(pfDao, ToscaEntityFilter
237                 .<ToscaPolicyType>builder().name(policyTypeKey.getName()).version(VERSION_001).build());
238
239         gotPolicyType = gotServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
240         checkEqualsNameDescription(beforePolicyType, gotPolicyType);
241
242         List<ToscaPolicyType> gotPolicyTypeList =
243                 new AuthorativeToscaProvider().getPolicyTypeList(pfDao, POLICY_NO_VERSION, VERSION_001);
244         assertEquals(2, gotPolicyTypeList.size());
245         assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
246
247         gotPolicyTypeList = new AuthorativeToscaProvider().getFilteredPolicyTypeList(pfDao,
248                 ToscaEntityFilter.<ToscaPolicyType>builder().build());
249         assertEquals(2, gotPolicyTypeList.size());
250         assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
251
252         gotPolicyTypeList = new AuthorativeToscaProvider().getFilteredPolicyTypeList(pfDao,
253                 ToscaEntityFilter.<ToscaPolicyType>builder().name(policyTypeKey.getName()).build());
254         assertEquals(1, gotPolicyTypeList.size());
255         assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
256
257         gotPolicyTypeList = new AuthorativeToscaProvider().getFilteredPolicyTypeList(pfDao, ToscaEntityFilter
258                 .<ToscaPolicyType>builder().name(policyTypeKey.getName()).version(VERSION_001).build());
259         assertEquals(1, gotPolicyTypeList.size());
260         assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
261
262         gotPolicyTypeList = new AuthorativeToscaProvider().getFilteredPolicyTypeList(pfDao,
263                 ToscaEntityFilter.<ToscaPolicyType>builder().version("1.0.0").build());
264         assertEquals(1, gotPolicyTypeList.size());
265         assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
266     }
267
268     private void checkEqualsNameDescription(ToscaPolicyType beforePolicyType, ToscaPolicyType gotPolicyType) {
269         assertEquals(beforePolicyType.getName(), gotPolicyType.getName());
270         assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), gotPolicyType.getDescription()));
271     }
272
273     @Test
274     public void testPolicyTypesCreate() throws Exception {
275         assertThatThrownBy(() -> {
276             new AuthorativeToscaProvider().createPolicyTypes(null, null);
277         }).hasMessageMatching(DAO_IS_NULL);
278
279         assertThatThrownBy(() -> {
280             new AuthorativeToscaProvider().createPolicyTypes(null, new ToscaServiceTemplate());
281         }).hasMessageMatching(DAO_IS_NULL);
282
283         assertThatThrownBy(() -> {
284             new AuthorativeToscaProvider().createPolicyTypes(pfDao, null);
285         }).hasMessageMatching("^serviceTemplate is marked .*on.*ull but is null$");
286
287         ToscaServiceTemplate testToscaServiceTemplate = new ToscaServiceTemplate();
288         assertThatThrownBy(() -> {
289             new AuthorativeToscaProvider().createPolicyTypes(pfDao, testToscaServiceTemplate);
290         }).hasMessage(MISSING_POLICY_TYPES);
291
292         testToscaServiceTemplate.setPolicyTypes(new LinkedHashMap<>());
293         assertThatThrownBy(() -> {
294             new AuthorativeToscaProvider().createPolicyTypes(pfDao, testToscaServiceTemplate);
295         }).hasMessage("An incoming list of concepts must have at least one entry");
296
297         ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class);
298
299         assertNotNull(toscaServiceTemplate);
300         ToscaServiceTemplate createdServiceTemplate =
301                 new AuthorativeToscaProvider().createPolicyTypes(pfDao, toscaServiceTemplate);
302
303         PfConceptKey policyTypeKey = new PfConceptKey(POLICY_NO_VERSION_VERSION1);
304
305         ToscaPolicyType beforePolicyType = toscaServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
306         ToscaPolicyType createdPolicyType = createdServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
307         assertEquals(true, beforePolicyType.getName().equals(createdPolicyType.getName()));
308         assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), createdPolicyType.getDescription()));
309     }
310
311     @Test
312     public void testPolicyTypesUpdate() throws Exception {
313         assertThatThrownBy(() -> {
314             new AuthorativeToscaProvider().createPolicyTypes(null, null);
315         }).hasMessageMatching(DAO_IS_NULL);
316
317         assertThatThrownBy(() -> {
318             new AuthorativeToscaProvider().updatePolicyTypes(null, null);
319         }).hasMessageMatching(DAO_IS_NULL);
320
321         assertThatThrownBy(() -> {
322             new AuthorativeToscaProvider().updatePolicyTypes(null, new ToscaServiceTemplate());
323         }).hasMessageMatching(DAO_IS_NULL);
324
325         assertThatThrownBy(() -> {
326             new AuthorativeToscaProvider().updatePolicyTypes(pfDao, null);
327         }).hasMessageMatching("^serviceTemplate is marked .*on.*ull but is null$");
328
329         ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class);
330
331         assertNotNull(toscaServiceTemplate);
332         ToscaServiceTemplate createdServiceTemplate =
333                 new AuthorativeToscaProvider().createPolicyTypes(pfDao, toscaServiceTemplate);
334
335         PfConceptKey policyTypeKey = new PfConceptKey(POLICY_NO_VERSION_VERSION1);
336
337         ToscaPolicyType beforePolicyType = toscaServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
338         ToscaPolicyType createdPolicyType = createdServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
339         assertEquals(true, beforePolicyType.getName().equals(createdPolicyType.getName()));
340         assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), createdPolicyType.getDescription()));
341
342         ToscaServiceTemplate updatedServiceTemplate =
343                 new AuthorativeToscaProvider().updatePolicyTypes(pfDao, toscaServiceTemplate);
344
345         ToscaPolicyType updatedPolicy = updatedServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
346         assertEquals(true, beforePolicyType.getName().equals(updatedPolicy.getName()));
347         assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), updatedPolicy.getDescription()));
348     }
349
350     @Test
351     public void testPolicyTypesDelete() throws Exception {
352         assertThatThrownBy(() -> {
353             new AuthorativeToscaProvider().deletePolicyType(null, null, null);
354         }).hasMessageMatching(DAO_IS_NULL);
355
356         assertThatThrownBy(() -> {
357             new AuthorativeToscaProvider().deletePolicyType(null, null, VERSION);
358         }).hasMessageMatching(DAO_IS_NULL);
359
360         assertThatThrownBy(() -> {
361             new AuthorativeToscaProvider().deletePolicyType(null, "name", null);
362         }).hasMessageMatching(DAO_IS_NULL);
363
364         assertThatThrownBy(() -> {
365             new AuthorativeToscaProvider().deletePolicyType(null, "name", VERSION);
366         }).hasMessageMatching(DAO_IS_NULL);
367
368         assertThatThrownBy(() -> {
369             new AuthorativeToscaProvider().deletePolicyType(pfDao, null, null);
370         }).hasMessageMatching("^name is marked .*on.*ull but is null$");
371
372         assertThatThrownBy(() -> {
373             new AuthorativeToscaProvider().deletePolicyType(pfDao, null, VERSION);
374         }).hasMessageMatching("^name is marked .*on.*ull but is null$");
375
376         assertThatThrownBy(() -> {
377             new AuthorativeToscaProvider().deletePolicyType(pfDao, "name", null);
378         }).hasMessageMatching("^version is marked .*on.*ull but is null$");
379
380         ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class);
381
382         assertNotNull(toscaServiceTemplate);
383         ToscaServiceTemplate createdServiceTemplate =
384                 new AuthorativeToscaProvider().createPolicyTypes(pfDao, toscaServiceTemplate);
385
386         PfConceptKey policyTypeKey = new PfConceptKey(POLICY_NO_VERSION_VERSION1);
387
388         ToscaPolicyType beforePolicyType = toscaServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
389         ToscaPolicyType createdPolicyType = createdServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
390         assertEquals(true, beforePolicyType.getName().equals(createdPolicyType.getName()));
391         assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), createdPolicyType.getDescription()));
392
393         ToscaServiceTemplate deletedServiceTemplate = new AuthorativeToscaProvider().deletePolicyType(pfDao,
394                 policyTypeKey.getName(), policyTypeKey.getVersion());
395
396         ToscaPolicyType deletedPolicy = deletedServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
397         assertEquals(true, beforePolicyType.getName().equals(deletedPolicy.getName()));
398         assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), deletedPolicy.getDescription()));
399
400         assertThatThrownBy(() -> {
401             new AuthorativeToscaProvider().getPolicyTypes(pfDao, policyTypeKey.getName(), policyTypeKey.getVersion());
402         }).hasMessage("policy types for onap.policies.NoVersion:0.0.1 do not exist");
403     }
404
405     @Test
406     public void testAssertPoliciesExist() {
407         ToscaServiceTemplate testServiceTemplate = new ToscaServiceTemplate();
408
409         assertThatThrownBy(() -> {
410             new AuthorativeToscaProvider().deletePolicyType(pfDao, "name", null);
411         }).hasMessageMatching("^version is marked .*on.*ull but is null$");
412
413         assertThatThrownBy(() -> {
414             new AuthorativeToscaProvider().createPolicyTypes(pfDao, testServiceTemplate);
415         }).hasMessage(MISSING_POLICY_TYPES);
416
417         testServiceTemplate.setToscaTopologyTemplate(new ToscaTopologyTemplate());
418         assertThatThrownBy(() -> {
419             new AuthorativeToscaProvider().createPolicyTypes(pfDao, testServiceTemplate);
420         }).hasMessage(MISSING_POLICY_TYPES);
421
422         testServiceTemplate.setPolicyTypes(new LinkedHashMap<>());
423         assertThatThrownBy(() -> {
424             new AuthorativeToscaProvider().createPolicyTypes(pfDao, testServiceTemplate);
425         }).hasMessage("An incoming list of concepts must have at least one entry");
426     }
427
428     @Test
429     public void testNullParameters() throws Exception {
430         assertThatThrownBy(() -> new AuthorativeToscaProvider().getPolicyTypeList(null, null, null))
431                 .hasMessageMatching("^dao is marked .*on.*ull but is null$");
432     }
433 }