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