Merge "Fix database properties"
[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.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.base.PfModelException;
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.ToscaPolicyType;
47 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeFilter;
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  * @author Liam Fallon (liam.fallon@est.tech)
56  */
57 public class AuthorativeToscaProviderPolicyTypeTest {
58     private static String yamlAsJsonString;
59     private PfDao pfDao;
60     private StandardCoder standardCoder;
61
62
63     /**
64      * Read the policy type definition.
65      *
66      * @throws Exception on errors
67      */
68     @BeforeClass
69     public static void readPolicyDefinition() {
70         String yamlString =
71                 ResourceUtils.getResourceAsString("policytypes/onap.policies.optimization.AffinityPolicy.yaml");
72
73         Object yamlObject = new Yaml().load(yamlString);
74         yamlAsJsonString = new GsonBuilder().setPrettyPrinting().create().toJson(yamlObject);
75     }
76
77     /**
78      * Set up the DAO towards the database.
79      *
80      * @throws Exception on database errors
81      */
82     @Before
83     public void setupDao() throws Exception {
84         final DaoParameters daoParameters = new DaoParameters();
85         daoParameters.setPluginClass(DefaultPfDao.class.getCanonicalName());
86
87         daoParameters.setPersistenceUnit("ToscaConceptTest");
88
89         Properties jdbcProperties = new Properties();
90         jdbcProperties.setProperty("javax.persistence.jdbc.user", "policy");
91         jdbcProperties.setProperty("javax.persistence.jdbc.password", "P01icY");
92
93         // H2
94         jdbcProperties.setProperty("javax.persistence.jdbc.driver", "org.h2.Driver");
95         jdbcProperties.setProperty("javax.persistence.jdbc.url", "jdbc:h2:mem:testdb");
96
97         // MariaDB
98         //jdbcProperties.setProperty("javax.persistence.jdbc.driver", "org.mariadb.jdbc.Driver");
99         //jdbcProperties.setProperty("javax.persistence.jdbc.url", "jdbc:mariadb://localhost:3306/policy");
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() throws Exception {
117         pfDao.close();
118     }
119
120     @Test
121     public void testPolicyTypesGet() throws Exception {
122         assertThatThrownBy(() -> {
123             new AuthorativeToscaProvider().getPolicyTypes(null, null, null);
124         }).hasMessage("dao is marked @NonNull but is null");
125
126         assertThatThrownBy(() -> {
127             new AuthorativeToscaProvider().getPolicyList(null, null, null);
128         }).hasMessage("dao is marked @NonNull but 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("onap.policies.optimization.AffinityPolicy:0.0.0");
137
138         ToscaPolicyType beforePolicyType = toscaServiceTemplate.getPolicyTypes().get(1).get(policyTypeKey.getName());
139         ToscaPolicyType createdPolicyType = createdServiceTemplate.getPolicyTypes().get(0).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(0).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 = new AuthorativeToscaProvider().getPolicyTypeList(pfDao,
151                 "onap.policies.optimization.AffinityPolicy", "0.0.0");
152         assertEquals(1, gotPolicyTypeList.size());
153         assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
154
155         gotPolicyTypeList = new AuthorativeToscaProvider().getPolicyTypeList(pfDao,
156                 "onap.policies.optimization.AffinityPolicy", null);
157         assertEquals(1, gotPolicyTypeList.size());
158         assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
159
160         gotPolicyTypeList = new AuthorativeToscaProvider().getPolicyTypeList(pfDao, null, null);
161         assertEquals(2, gotPolicyTypeList.size());
162         assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
163
164         gotPolicyTypeList = new AuthorativeToscaProvider().getPolicyTypeList(pfDao, null, "0.0.0");
165         assertEquals(2, gotPolicyTypeList.size());
166         assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
167     }
168
169
170     @Test
171     public void testPolicyTypesGetFiltered() throws Exception {
172         assertThatThrownBy(() -> {
173             new AuthorativeToscaProvider().getFilteredPolicyTypes(null, null);
174         }).hasMessage("dao is marked @NonNull but is null");
175
176         assertThatThrownBy(() -> {
177             new AuthorativeToscaProvider().getFilteredPolicyTypes(null, ToscaPolicyTypeFilter.builder().build());
178         }).hasMessage("dao is marked @NonNull but is null");
179
180         assertThatThrownBy(() -> {
181             new AuthorativeToscaProvider().getFilteredPolicyTypes(pfDao, null);
182         }).hasMessage("filter is marked @NonNull but is null");
183
184         assertThatThrownBy(() -> {
185             new AuthorativeToscaProvider().getFilteredPolicyTypeList(null, null);
186         }).hasMessage("dao is marked @NonNull but is null");
187
188         assertThatThrownBy(() -> {
189             new AuthorativeToscaProvider().getFilteredPolicyTypeList(null, ToscaPolicyTypeFilter.builder().build());
190         }).hasMessage("dao is marked @NonNull but is null");
191
192         assertThatThrownBy(() -> {
193             new AuthorativeToscaProvider().getFilteredPolicyTypeList(pfDao, null);
194         }).hasMessage("filter is marked @NonNull 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("onap.policies.optimization.AffinityPolicy:0.0.0");
203
204         ToscaPolicyType beforePolicyType = toscaServiceTemplate.getPolicyTypes().get(1).get(policyTypeKey.getName());
205         ToscaPolicyType createdPolicyType = createdServiceTemplate.getPolicyTypes().get(0).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(0).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(0).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("0.0.0").build());
225
226         gotPolicyType = gotServiceTemplate.getPolicyTypes().get(0).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 = new AuthorativeToscaProvider().getPolicyTypeList(pfDao,
231                 "onap.policies.optimization.AffinityPolicy", "0.0.0");
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(2, 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("0.0.0").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(1, 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         }).hasMessage("dao is marked @NonNull but is null");
261
262         assertThatThrownBy(() -> {
263             new AuthorativeToscaProvider().createPolicyTypes(null, new ToscaServiceTemplate());
264         }).hasMessage("dao is marked @NonNull but is null");
265
266         assertThatThrownBy(() -> {
267             new AuthorativeToscaProvider().createPolicyTypes(pfDao, null);
268         }).hasMessage("serviceTemplate is marked @NonNull but is null");
269
270         ToscaServiceTemplate testToscaServiceTemplate = new ToscaServiceTemplate();
271         assertThatThrownBy(() -> {
272             new AuthorativeToscaProvider().createPolicyTypes(pfDao, testToscaServiceTemplate);
273         }).hasMessage("no policy types specified on service template");
274
275         testToscaServiceTemplate.setPolicyTypes(new ArrayList<>());
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("onap.policies.optimization.AffinityPolicy:0.0.0");
287
288         ToscaPolicyType beforePolicyType = toscaServiceTemplate.getPolicyTypes().get(1).get(policyTypeKey.getName());
289         ToscaPolicyType createdPolicyType = createdServiceTemplate.getPolicyTypes().get(0).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         }).hasMessage("dao is marked @NonNull but is null");
299
300         assertThatThrownBy(() -> {
301             new AuthorativeToscaProvider().updatePolicyTypes(null, null);
302         }).hasMessage("dao is marked @NonNull but is null");
303
304         assertThatThrownBy(() -> {
305             new AuthorativeToscaProvider().updatePolicyTypes(null, new ToscaServiceTemplate());
306         }).hasMessage("dao is marked @NonNull but is null");
307
308         assertThatThrownBy(() -> {
309             new AuthorativeToscaProvider().updatePolicyTypes(pfDao, null);
310         }).hasMessage("serviceTemplate is marked @NonNull 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("onap.policies.optimization.AffinityPolicy:0.0.0");
319
320         ToscaPolicyType beforePolicyType = toscaServiceTemplate.getPolicyTypes().get(1).get(policyTypeKey.getName());
321         ToscaPolicyType createdPolicyType = createdServiceTemplate.getPolicyTypes().get(0).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(0).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         }).hasMessage("dao is marked @NonNull but is null");
338
339         assertThatThrownBy(() -> {
340             new AuthorativeToscaProvider().deletePolicyType(null, null, "version");
341         }).hasMessage("dao is marked @NonNull but is null");
342
343         assertThatThrownBy(() -> {
344             new AuthorativeToscaProvider().deletePolicyType(null, "name", null);
345         }).hasMessage("dao is marked @NonNull but is null");
346
347         assertThatThrownBy(() -> {
348             new AuthorativeToscaProvider().deletePolicyType(null, "name", "version");
349         }).hasMessage("dao is marked @NonNull but is null");
350
351         assertThatThrownBy(() -> {
352             new AuthorativeToscaProvider().deletePolicyType(pfDao, null, null);
353         }).hasMessage("name is marked @NonNull but is null");
354
355         assertThatThrownBy(() -> {
356             new AuthorativeToscaProvider().deletePolicyType(pfDao, null, "version");
357         }).hasMessage("name is marked @NonNull but is null");
358
359         assertThatThrownBy(() -> {
360             new AuthorativeToscaProvider().deletePolicyType(pfDao, "name", null);
361         }).hasMessage("version is marked @NonNull 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("onap.policies.optimization.AffinityPolicy:0.0.0");
370
371         ToscaPolicyType beforePolicyType = toscaServiceTemplate.getPolicyTypes().get(1).get(policyTypeKey.getName());
372         ToscaPolicyType createdPolicyType = createdServiceTemplate.getPolicyTypes().get(0).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(0).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         assertEquals(0, gotServiceTemplate.getPolicyTypes().get(0).size());
387     }
388
389     @Test
390     public void testAssertPoliciesExist() throws PfModelException {
391         ToscaServiceTemplate testServiceTemplate = new ToscaServiceTemplate();
392
393         assertThatThrownBy(() -> {
394             new AuthorativeToscaProvider().deletePolicyType(pfDao, "name", null);
395         }).hasMessage("version is marked @NonNull but is null");
396
397         assertThatThrownBy(() -> {
398             new AuthorativeToscaProvider().createPolicyTypes(pfDao, testServiceTemplate);
399         }).hasMessage("no policy types specified on service template");
400
401         testServiceTemplate.setToscaTopologyTemplate(new ToscaTopologyTemplate());
402         assertThatThrownBy(() -> {
403             new AuthorativeToscaProvider().createPolicyTypes(pfDao, testServiceTemplate);
404         }).hasMessage("no policy types specified on service template");
405
406         testServiceTemplate.setPolicyTypes(new ArrayList<>());
407         assertThatThrownBy(() -> {
408             new AuthorativeToscaProvider().createPolicyTypes(pfDao, testServiceTemplate);
409         }).hasMessage("An incoming list of concepts must have at least one entry");
410     }
411 }