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