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