Replace Eclipselink with Hibernate
[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,2023 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.junit.After;
35 import org.junit.Before;
36 import org.junit.BeforeClass;
37 import org.junit.Test;
38 import org.onap.policy.common.utils.coder.StandardCoder;
39 import org.onap.policy.common.utils.resources.ResourceUtils;
40 import org.onap.policy.models.base.PfConceptKey;
41 import org.onap.policy.models.dao.DaoParameters;
42 import org.onap.policy.models.dao.PfDao;
43 import org.onap.policy.models.dao.PfDaoFactory;
44 import org.onap.policy.models.dao.impl.DefaultPfDao;
45 import org.onap.policy.models.tosca.authorative.concepts.ToscaEntityFilter;
46 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyType;
47 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
48 import org.onap.policy.models.tosca.authorative.concepts.ToscaTopologyTemplate;
49 import org.yaml.snakeyaml.Yaml;
50
51 /**
52  * Test of the {@link AuthorativeToscaProvider} class.
53  */
54 public class AuthorativeToscaProviderPolicyTypeTest {
55     private static final String VERSION = "version";
56     private static final String POLICY_NO_VERSION_VERSION1 = "onap.policies.NoVersion:0.0.1";
57     private static final String POLICY_NO_VERSION = "onap.policies.NoVersion";
58     private static final String MISSING_POLICY_TYPES = "no policy types specified on service template";
59     private static final String DAO_IS_NULL = "^dao is marked .*on.*ull but is null$";
60     private static final String VERSION_001 = "0.0.1";
61     private static String yamlAsJsonString;
62     private PfDao pfDao;
63     private StandardCoder standardCoder;
64
65     /**
66      * Read the policy type definition.
67      *
68      * @throws Exception on errors
69      */
70     @BeforeClass
71     public static void readPolicyDefinition() {
72         String yamlString = ResourceUtils.getResourceAsString("src/test/resources/onap.policies.NoVersion.yaml");
73
74         Object yamlObject = new Yaml().load(yamlString);
75         yamlAsJsonString = new GsonBuilder().setPrettyPrinting().create().toJson(yamlObject);
76     }
77
78     /**
79      * Set up the DAO towards the database.
80      *
81      * @throws Exception on database errors
82      */
83     @Before
84     public void setupDao() throws Exception {
85         final DaoParameters daoParameters = new DaoParameters();
86         daoParameters.setPluginClass(DefaultPfDao.class.getName());
87
88         daoParameters.setPersistenceUnit("ToscaConceptTest");
89
90         Properties jdbcProperties = new Properties();
91         jdbcProperties.setProperty("javax.persistence.jdbc.user", "policy");
92         jdbcProperties.setProperty("javax.persistence.jdbc.password", "P01icY");
93         if (System.getProperty("USE-MARIADB") != null) {
94             jdbcProperties.setProperty("javax.persistence.jdbc.driver", "org.mariadb.jdbc.Driver");
95             jdbcProperties.setProperty("javax.persistence.jdbc.url", "jdbc:mariadb://localhost:3306/policy");
96         } else {
97             jdbcProperties.setProperty("javax.persistence.jdbc.driver", "org.h2.Driver");
98             jdbcProperties.setProperty("javax.persistence.jdbc.url",
99                             "jdbc:h2:mem:AuthorativeToscaProviderPolicyTypeTest");
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 = new AuthorativeToscaProvider().getPolicyTypes(pfDao,
144                 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(() -> new AuthorativeToscaProvider().getPolicyTypeList(new DefaultPfDao(), POLICY_NO_VERSION,
168                 VERSION_001)).hasMessageContaining("Policy Framework DAO has not been initialized");
169
170         assertTrue(new AuthorativeToscaProvider().getPolicyTypeList(pfDao, "i.dont.Exist", VERSION_001).isEmpty());
171     }
172
173     @Test
174     public void testPolicyTypesGetFiltered() throws Exception {
175         assertThatThrownBy(() -> {
176             new AuthorativeToscaProvider().getFilteredPolicyTypes(null, null);
177         }).hasMessageMatching(DAO_IS_NULL);
178
179         assertThatThrownBy(() -> {
180             new AuthorativeToscaProvider().getFilteredPolicyTypes(null,
181                     ToscaEntityFilter.<ToscaPolicyType>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,
194                     ToscaEntityFilter.<ToscaPolicyType>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                 ToscaEntityFilter.<ToscaPolicyType>builder().name("i.dont.Exist").build()))
203                         .hasMessageContaining("Policy Framework DAO has not been initialized");
204
205         assertTrue(new AuthorativeToscaProvider().getFilteredPolicyTypeList(pfDao,
206                 ToscaEntityFilter.<ToscaPolicyType>builder().name("i.dont.Exist").build()).isEmpty());
207
208         ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class);
209
210         assertNotNull(toscaServiceTemplate);
211         ToscaServiceTemplate createdServiceTemplate =
212                 new AuthorativeToscaProvider().createPolicyTypes(pfDao, toscaServiceTemplate);
213
214         PfConceptKey policyTypeKey = new PfConceptKey(POLICY_NO_VERSION_VERSION1);
215
216         ToscaPolicyType beforePolicyType = toscaServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
217         ToscaPolicyType createdPolicyType = createdServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
218         assertEquals(true, beforePolicyType.getName().equals(createdPolicyType.getName()));
219         assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), createdPolicyType.getDescription()));
220
221         ToscaServiceTemplate gotServiceTemplate = new AuthorativeToscaProvider().getFilteredPolicyTypes(pfDao,
222                 ToscaEntityFilter.<ToscaPolicyType>builder().build());
223
224         ToscaPolicyType gotPolicyType = gotServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
225         checkEqualsNameDescription(beforePolicyType, gotPolicyType);
226
227         gotServiceTemplate = new AuthorativeToscaProvider().getFilteredPolicyTypes(pfDao,
228                 ToscaEntityFilter.<ToscaPolicyType>builder().name(policyTypeKey.getName()).build());
229
230         gotPolicyType = gotServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
231         checkEqualsNameDescription(beforePolicyType, gotPolicyType);
232
233         gotServiceTemplate = new AuthorativeToscaProvider().getFilteredPolicyTypes(pfDao, ToscaEntityFilter
234                 .<ToscaPolicyType>builder().name(policyTypeKey.getName()).version(VERSION_001).build());
235
236         gotPolicyType = gotServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
237         checkEqualsNameDescription(beforePolicyType, gotPolicyType);
238
239         List<ToscaPolicyType> gotPolicyTypeList =
240                 new AuthorativeToscaProvider().getPolicyTypeList(pfDao, POLICY_NO_VERSION, VERSION_001);
241         assertEquals(2, gotPolicyTypeList.size());
242         assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
243
244         gotPolicyTypeList = new AuthorativeToscaProvider().getFilteredPolicyTypeList(pfDao,
245                 ToscaEntityFilter.<ToscaPolicyType>builder().build());
246         assertEquals(2, gotPolicyTypeList.size());
247         assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
248
249         gotPolicyTypeList = new AuthorativeToscaProvider().getFilteredPolicyTypeList(pfDao,
250                 ToscaEntityFilter.<ToscaPolicyType>builder().name(policyTypeKey.getName()).build());
251         assertEquals(1, gotPolicyTypeList.size());
252         assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
253
254         gotPolicyTypeList = new AuthorativeToscaProvider().getFilteredPolicyTypeList(pfDao, ToscaEntityFilter
255                 .<ToscaPolicyType>builder().name(policyTypeKey.getName()).version(VERSION_001).build());
256         assertEquals(1, gotPolicyTypeList.size());
257         assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
258
259         gotPolicyTypeList = new AuthorativeToscaProvider().getFilteredPolicyTypeList(pfDao,
260                 ToscaEntityFilter.<ToscaPolicyType>builder().version("1.0.0").build());
261         assertEquals(1, gotPolicyTypeList.size());
262         assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
263     }
264
265     private void checkEqualsNameDescription(ToscaPolicyType beforePolicyType, ToscaPolicyType gotPolicyType) {
266         assertEquals(beforePolicyType.getName(), gotPolicyType.getName());
267         assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), gotPolicyType.getDescription()));
268     }
269
270     @Test
271     public void testPolicyTypesCreate() throws Exception {
272         assertThatThrownBy(() -> {
273             new AuthorativeToscaProvider().createPolicyTypes(null, null);
274         }).hasMessageMatching(DAO_IS_NULL);
275
276         assertThatThrownBy(() -> {
277             new AuthorativeToscaProvider().createPolicyTypes(null, new ToscaServiceTemplate());
278         }).hasMessageMatching(DAO_IS_NULL);
279
280         assertThatThrownBy(() -> {
281             new AuthorativeToscaProvider().createPolicyTypes(pfDao, null);
282         }).hasMessageMatching("^serviceTemplate is marked .*on.*ull but is null$");
283
284         ToscaServiceTemplate testToscaServiceTemplate = new ToscaServiceTemplate();
285         assertThatThrownBy(() -> {
286             new AuthorativeToscaProvider().createPolicyTypes(pfDao, testToscaServiceTemplate);
287         }).hasMessage(MISSING_POLICY_TYPES);
288
289         testToscaServiceTemplate.setPolicyTypes(new LinkedHashMap<>());
290         assertThatThrownBy(() -> {
291             new AuthorativeToscaProvider().createPolicyTypes(pfDao, testToscaServiceTemplate);
292         }).hasMessage("An incoming list of concepts must have at least one entry");
293
294         ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class);
295
296         assertNotNull(toscaServiceTemplate);
297         ToscaServiceTemplate createdServiceTemplate =
298                 new AuthorativeToscaProvider().createPolicyTypes(pfDao, toscaServiceTemplate);
299
300         PfConceptKey policyTypeKey = new PfConceptKey(POLICY_NO_VERSION_VERSION1);
301
302         ToscaPolicyType beforePolicyType = toscaServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
303         ToscaPolicyType createdPolicyType = createdServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
304         assertEquals(true, beforePolicyType.getName().equals(createdPolicyType.getName()));
305         assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), createdPolicyType.getDescription()));
306     }
307
308     @Test
309     public void testPolicyTypesUpdate() throws Exception {
310         assertThatThrownBy(() -> {
311             new AuthorativeToscaProvider().createPolicyTypes(null, null);
312         }).hasMessageMatching(DAO_IS_NULL);
313
314         assertThatThrownBy(() -> {
315             new AuthorativeToscaProvider().updatePolicyTypes(null, null);
316         }).hasMessageMatching(DAO_IS_NULL);
317
318         assertThatThrownBy(() -> {
319             new AuthorativeToscaProvider().updatePolicyTypes(null, new ToscaServiceTemplate());
320         }).hasMessageMatching(DAO_IS_NULL);
321
322         assertThatThrownBy(() -> {
323             new AuthorativeToscaProvider().updatePolicyTypes(pfDao, null);
324         }).hasMessageMatching("^serviceTemplate is marked .*on.*ull but is null$");
325
326         ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class);
327
328         assertNotNull(toscaServiceTemplate);
329         ToscaServiceTemplate createdServiceTemplate =
330                 new AuthorativeToscaProvider().createPolicyTypes(pfDao, toscaServiceTemplate);
331
332         PfConceptKey policyTypeKey = new PfConceptKey(POLICY_NO_VERSION_VERSION1);
333
334         ToscaPolicyType beforePolicyType = toscaServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
335         ToscaPolicyType createdPolicyType = createdServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
336         assertEquals(true, beforePolicyType.getName().equals(createdPolicyType.getName()));
337         assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), createdPolicyType.getDescription()));
338
339         ToscaServiceTemplate updatedServiceTemplate =
340                 new AuthorativeToscaProvider().updatePolicyTypes(pfDao, toscaServiceTemplate);
341
342         ToscaPolicyType updatedPolicy = updatedServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
343         assertEquals(true, beforePolicyType.getName().equals(updatedPolicy.getName()));
344         assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), updatedPolicy.getDescription()));
345     }
346
347     @Test
348     public void testPolicyTypesDelete() throws Exception {
349         assertThatThrownBy(() -> {
350             new AuthorativeToscaProvider().deletePolicyType(null, null, null);
351         }).hasMessageMatching(DAO_IS_NULL);
352
353         assertThatThrownBy(() -> {
354             new AuthorativeToscaProvider().deletePolicyType(null, null, VERSION);
355         }).hasMessageMatching(DAO_IS_NULL);
356
357         assertThatThrownBy(() -> {
358             new AuthorativeToscaProvider().deletePolicyType(null, "name", null);
359         }).hasMessageMatching(DAO_IS_NULL);
360
361         assertThatThrownBy(() -> {
362             new AuthorativeToscaProvider().deletePolicyType(null, "name", VERSION);
363         }).hasMessageMatching(DAO_IS_NULL);
364
365         assertThatThrownBy(() -> {
366             new AuthorativeToscaProvider().deletePolicyType(pfDao, null, null);
367         }).hasMessageMatching("^name is marked .*on.*ull but is null$");
368
369         assertThatThrownBy(() -> {
370             new AuthorativeToscaProvider().deletePolicyType(pfDao, null, VERSION);
371         }).hasMessageMatching("^name is marked .*on.*ull but is null$");
372
373         assertThatThrownBy(() -> {
374             new AuthorativeToscaProvider().deletePolicyType(pfDao, "name", null);
375         }).hasMessageMatching("^version is marked .*on.*ull but is null$");
376
377         ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class);
378
379         assertNotNull(toscaServiceTemplate);
380         ToscaServiceTemplate createdServiceTemplate =
381                 new AuthorativeToscaProvider().createPolicyTypes(pfDao, toscaServiceTemplate);
382
383         PfConceptKey policyTypeKey = new PfConceptKey(POLICY_NO_VERSION_VERSION1);
384
385         ToscaPolicyType beforePolicyType = toscaServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
386         ToscaPolicyType createdPolicyType = createdServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
387         assertEquals(true, beforePolicyType.getName().equals(createdPolicyType.getName()));
388         assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), createdPolicyType.getDescription()));
389
390         ToscaServiceTemplate deletedServiceTemplate = new AuthorativeToscaProvider().deletePolicyType(pfDao,
391                 policyTypeKey.getName(), policyTypeKey.getVersion());
392
393         ToscaPolicyType deletedPolicy = deletedServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
394         assertEquals(true, beforePolicyType.getName().equals(deletedPolicy.getName()));
395         assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), deletedPolicy.getDescription()));
396
397         assertThatThrownBy(() -> {
398             new AuthorativeToscaProvider().getPolicyTypes(pfDao, policyTypeKey.getName(), policyTypeKey.getVersion());
399         }).hasMessage("policy types for onap.policies.NoVersion:0.0.1 do not exist");
400     }
401
402     @Test
403     public void testAssertPoliciesExist() {
404         ToscaServiceTemplate testServiceTemplate = new ToscaServiceTemplate();
405
406         assertThatThrownBy(() -> {
407             new AuthorativeToscaProvider().deletePolicyType(pfDao, "name", null);
408         }).hasMessageMatching("^version is marked .*on.*ull but is null$");
409
410         assertThatThrownBy(() -> {
411             new AuthorativeToscaProvider().createPolicyTypes(pfDao, testServiceTemplate);
412         }).hasMessage(MISSING_POLICY_TYPES);
413
414         testServiceTemplate.setToscaTopologyTemplate(new ToscaTopologyTemplate());
415         assertThatThrownBy(() -> {
416             new AuthorativeToscaProvider().createPolicyTypes(pfDao, testServiceTemplate);
417         }).hasMessage(MISSING_POLICY_TYPES);
418
419         testServiceTemplate.setPolicyTypes(new LinkedHashMap<>());
420         assertThatThrownBy(() -> {
421             new AuthorativeToscaProvider().createPolicyTypes(pfDao, testServiceTemplate);
422         }).hasMessage("An incoming list of concepts must have at least one entry");
423     }
424
425     @Test
426     public void testNullParameters() throws Exception {
427         assertThatThrownBy(() -> new AuthorativeToscaProvider().getPolicyTypeList(null, null, null))
428                 .hasMessageMatching("^dao is marked .*on.*ull but is null$");
429     }
430 }