Fix the bug of cannot return multiple versions of particular tosca policy and policy...
[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 import static org.junit.Assert.assertTrue;
27
28 import com.google.gson.GsonBuilder;
29
30 import java.util.ArrayList;
31 import java.util.List;
32 import java.util.Properties;
33
34 import org.apache.commons.lang3.ObjectUtils;
35 import org.eclipse.persistence.config.PersistenceUnitProperties;
36 import org.junit.After;
37 import org.junit.Before;
38 import org.junit.BeforeClass;
39 import org.junit.Test;
40 import org.onap.policy.common.utils.coder.StandardCoder;
41 import org.onap.policy.common.utils.resources.ResourceUtils;
42 import org.onap.policy.models.base.PfConceptKey;
43 import org.onap.policy.models.base.PfModelException;
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 String yamlAsJsonString;
61     private PfDao pfDao;
62     private StandardCoder standardCoder;
63
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 =
73                 ResourceUtils.getResourceAsString("policytypes/onap.policies.optimization.AffinityPolicy.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.getCanonicalName());
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() throws Exception {
115         pfDao.close();
116     }
117
118     @Test
119     public void testPolicyTypesGet() throws Exception {
120         assertThatThrownBy(() -> {
121             new AuthorativeToscaProvider().getPolicyTypes(null, null, null);
122         }).hasMessage("dao is marked @NonNull but is null");
123
124         assertThatThrownBy(() -> {
125             new AuthorativeToscaProvider().getPolicyList(null, null, null);
126         }).hasMessage("dao is marked @NonNull but 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("onap.policies.optimization.AffinityPolicy:0.0.0");
135
136         ToscaPolicyType beforePolicyType = toscaServiceTemplate.getPolicyTypes().get(1).get(policyTypeKey.getName());
137         ToscaPolicyType createdPolicyType = createdServiceTemplate.getPolicyTypes().get(1).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(0).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 = new AuthorativeToscaProvider().getPolicyTypeList(pfDao,
149                 "onap.policies.optimization.AffinityPolicy", "0.0.0");
150         assertEquals(1, gotPolicyTypeList.size());
151         assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
152
153         gotPolicyTypeList = new AuthorativeToscaProvider().getPolicyTypeList(pfDao,
154                 "onap.policies.optimization.AffinityPolicy", null);
155         assertEquals(1, gotPolicyTypeList.size());
156         assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
157
158         gotPolicyTypeList = new AuthorativeToscaProvider().getPolicyTypeList(pfDao, null, null);
159         assertEquals(2, gotPolicyTypeList.size());
160         assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
161
162         gotPolicyTypeList = new AuthorativeToscaProvider().getPolicyTypeList(pfDao, null, "0.0.0");
163         assertEquals(2, gotPolicyTypeList.size());
164         assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
165     }
166
167
168     @Test
169     public void testPolicyTypesGetFiltered() throws Exception {
170         assertThatThrownBy(() -> {
171             new AuthorativeToscaProvider().getFilteredPolicyTypes(null, null);
172         }).hasMessage("dao is marked @NonNull but is null");
173
174         assertThatThrownBy(() -> {
175             new AuthorativeToscaProvider().getFilteredPolicyTypes(null, ToscaPolicyTypeFilter.builder().build());
176         }).hasMessage("dao is marked @NonNull but is null");
177
178         assertThatThrownBy(() -> {
179             new AuthorativeToscaProvider().getFilteredPolicyTypes(pfDao, null);
180         }).hasMessage("filter is marked @NonNull but is null");
181
182         assertThatThrownBy(() -> {
183             new AuthorativeToscaProvider().getFilteredPolicyTypeList(null, null);
184         }).hasMessage("dao is marked @NonNull but is null");
185
186         assertThatThrownBy(() -> {
187             new AuthorativeToscaProvider().getFilteredPolicyTypeList(null, ToscaPolicyTypeFilter.builder().build());
188         }).hasMessage("dao is marked @NonNull but is null");
189
190         assertThatThrownBy(() -> {
191             new AuthorativeToscaProvider().getFilteredPolicyTypeList(pfDao, null);
192         }).hasMessage("filter is marked @NonNull but is null");
193
194         ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class);
195
196         assertNotNull(toscaServiceTemplate);
197         ToscaServiceTemplate createdServiceTemplate =
198                 new AuthorativeToscaProvider().createPolicyTypes(pfDao, toscaServiceTemplate);
199
200         PfConceptKey policyTypeKey = new PfConceptKey("onap.policies.optimization.AffinityPolicy:0.0.0");
201
202         ToscaPolicyType beforePolicyType = toscaServiceTemplate.getPolicyTypes().get(1).get(policyTypeKey.getName());
203         ToscaPolicyType createdPolicyType = createdServiceTemplate.getPolicyTypes().get(1).get(policyTypeKey.getName());
204         assertEquals(true, beforePolicyType.getName().equals(createdPolicyType.getName()));
205         assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), createdPolicyType.getDescription()));
206
207         ToscaServiceTemplate gotServiceTemplate =
208                 new AuthorativeToscaProvider().getFilteredPolicyTypes(pfDao, ToscaPolicyTypeFilter.builder().build());
209
210         ToscaPolicyType gotPolicyType = gotServiceTemplate.getPolicyTypes().get(1).get(policyTypeKey.getName());
211         assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
212         assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), gotPolicyType.getDescription()));
213
214         gotServiceTemplate = new AuthorativeToscaProvider().getFilteredPolicyTypes(pfDao,
215                 ToscaPolicyTypeFilter.builder().name(policyTypeKey.getName()).build());
216
217         gotPolicyType = gotServiceTemplate.getPolicyTypes().get(0).get(policyTypeKey.getName());
218         assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
219         assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), gotPolicyType.getDescription()));
220
221         gotServiceTemplate = new AuthorativeToscaProvider().getFilteredPolicyTypes(pfDao,
222                 ToscaPolicyTypeFilter.builder().name(policyTypeKey.getName()).version("0.0.0").build());
223
224         gotPolicyType = gotServiceTemplate.getPolicyTypes().get(0).get(policyTypeKey.getName());
225         assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
226         assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), gotPolicyType.getDescription()));
227
228         List<ToscaPolicyType> gotPolicyTypeList = new AuthorativeToscaProvider().getPolicyTypeList(pfDao,
229                 "onap.policies.optimization.AffinityPolicy", "0.0.0");
230         assertEquals(1, gotPolicyTypeList.size());
231         assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
232
233         gotPolicyTypeList = new AuthorativeToscaProvider().getFilteredPolicyTypeList(pfDao,
234                 ToscaPolicyTypeFilter.builder().build());
235         assertEquals(2, gotPolicyTypeList.size());
236         assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
237
238         gotPolicyTypeList = new AuthorativeToscaProvider().getFilteredPolicyTypeList(pfDao,
239                 ToscaPolicyTypeFilter.builder().name(policyTypeKey.getName()).build());
240         assertEquals(1, gotPolicyTypeList.size());
241         assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
242
243         gotPolicyTypeList = new AuthorativeToscaProvider().getFilteredPolicyTypeList(pfDao,
244                 ToscaPolicyTypeFilter.builder().name(policyTypeKey.getName()).version("0.0.0").build());
245         assertEquals(1, gotPolicyTypeList.size());
246         assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
247
248         gotPolicyTypeList = new AuthorativeToscaProvider().getFilteredPolicyTypeList(pfDao,
249                 ToscaPolicyTypeFilter.builder().version("1.0.0").build());
250         assertEquals(1, gotPolicyTypeList.size());
251         assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
252     }
253
254     @Test
255     public void testPolicyTypesCreate() throws Exception {
256         assertThatThrownBy(() -> {
257             new AuthorativeToscaProvider().createPolicyTypes(null, null);
258         }).hasMessage("dao is marked @NonNull but is null");
259
260         assertThatThrownBy(() -> {
261             new AuthorativeToscaProvider().createPolicyTypes(null, new ToscaServiceTemplate());
262         }).hasMessage("dao is marked @NonNull but is null");
263
264         assertThatThrownBy(() -> {
265             new AuthorativeToscaProvider().createPolicyTypes(pfDao, null);
266         }).hasMessage("serviceTemplate is marked @NonNull but is null");
267
268         ToscaServiceTemplate testToscaServiceTemplate = new ToscaServiceTemplate();
269         assertThatThrownBy(() -> {
270             new AuthorativeToscaProvider().createPolicyTypes(pfDao, testToscaServiceTemplate);
271         }).hasMessage("no policy types specified on service template");
272
273         testToscaServiceTemplate.setPolicyTypes(new ArrayList<>());
274         assertThatThrownBy(() -> {
275             new AuthorativeToscaProvider().createPolicyTypes(pfDao, testToscaServiceTemplate);
276         }).hasMessage("An incoming list of concepts must have at least one entry");
277
278         ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class);
279
280         assertNotNull(toscaServiceTemplate);
281         ToscaServiceTemplate createdServiceTemplate =
282                 new AuthorativeToscaProvider().createPolicyTypes(pfDao, toscaServiceTemplate);
283
284         PfConceptKey policyTypeKey = new PfConceptKey("onap.policies.optimization.AffinityPolicy:0.0.0");
285
286         ToscaPolicyType beforePolicyType = toscaServiceTemplate.getPolicyTypes().get(1).get(policyTypeKey.getName());
287         ToscaPolicyType createdPolicyType = createdServiceTemplate.getPolicyTypes().get(1).get(policyTypeKey.getName());
288         assertEquals(true, beforePolicyType.getName().equals(createdPolicyType.getName()));
289         assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), createdPolicyType.getDescription()));
290     }
291
292     @Test
293     public void testPolicyTypesUpdate() throws Exception {
294         assertThatThrownBy(() -> {
295             new AuthorativeToscaProvider().createPolicyTypes(null, null);
296         }).hasMessage("dao is marked @NonNull but is null");
297
298         assertThatThrownBy(() -> {
299             new AuthorativeToscaProvider().updatePolicyTypes(null, null);
300         }).hasMessage("dao is marked @NonNull but is null");
301
302         assertThatThrownBy(() -> {
303             new AuthorativeToscaProvider().updatePolicyTypes(null, new ToscaServiceTemplate());
304         }).hasMessage("dao is marked @NonNull but is null");
305
306         assertThatThrownBy(() -> {
307             new AuthorativeToscaProvider().updatePolicyTypes(pfDao, null);
308         }).hasMessage("serviceTemplate is marked @NonNull but is null");
309
310         ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class);
311
312         assertNotNull(toscaServiceTemplate);
313         ToscaServiceTemplate createdServiceTemplate =
314                 new AuthorativeToscaProvider().createPolicyTypes(pfDao, toscaServiceTemplate);
315
316         PfConceptKey policyTypeKey = new PfConceptKey("onap.policies.optimization.AffinityPolicy:0.0.0");
317
318         ToscaPolicyType beforePolicyType = toscaServiceTemplate.getPolicyTypes().get(1).get(policyTypeKey.getName());
319         ToscaPolicyType createdPolicyType = createdServiceTemplate.getPolicyTypes().get(1).get(policyTypeKey.getName());
320         assertEquals(true, beforePolicyType.getName().equals(createdPolicyType.getName()));
321         assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), createdPolicyType.getDescription()));
322
323         ToscaServiceTemplate updatedServiceTemplate =
324                 new AuthorativeToscaProvider().updatePolicyTypes(pfDao, toscaServiceTemplate);
325
326         ToscaPolicyType updatedPolicy = updatedServiceTemplate.getPolicyTypes().get(1).get(policyTypeKey.getName());
327         assertEquals(true, beforePolicyType.getName().equals(updatedPolicy.getName()));
328         assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), updatedPolicy.getDescription()));
329     }
330
331     @Test
332     public void testPolicyTypesDelete() throws Exception {
333         assertThatThrownBy(() -> {
334             new AuthorativeToscaProvider().deletePolicyType(null, null, null);
335         }).hasMessage("dao is marked @NonNull but is null");
336
337         assertThatThrownBy(() -> {
338             new AuthorativeToscaProvider().deletePolicyType(null, null, "version");
339         }).hasMessage("dao is marked @NonNull but is null");
340
341         assertThatThrownBy(() -> {
342             new AuthorativeToscaProvider().deletePolicyType(null, "name", null);
343         }).hasMessage("dao is marked @NonNull but is null");
344
345         assertThatThrownBy(() -> {
346             new AuthorativeToscaProvider().deletePolicyType(null, "name", "version");
347         }).hasMessage("dao is marked @NonNull but is null");
348
349         assertThatThrownBy(() -> {
350             new AuthorativeToscaProvider().deletePolicyType(pfDao, null, null);
351         }).hasMessage("name is marked @NonNull but is null");
352
353         assertThatThrownBy(() -> {
354             new AuthorativeToscaProvider().deletePolicyType(pfDao, null, "version");
355         }).hasMessage("name is marked @NonNull but is null");
356
357         assertThatThrownBy(() -> {
358             new AuthorativeToscaProvider().deletePolicyType(pfDao, "name", null);
359         }).hasMessage("version is marked @NonNull but is null");
360
361         ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class);
362
363         assertNotNull(toscaServiceTemplate);
364         ToscaServiceTemplate createdServiceTemplate =
365                 new AuthorativeToscaProvider().createPolicyTypes(pfDao, toscaServiceTemplate);
366
367         PfConceptKey policyTypeKey = new PfConceptKey("onap.policies.optimization.AffinityPolicy:0.0.0");
368
369         ToscaPolicyType beforePolicyType = toscaServiceTemplate.getPolicyTypes().get(1).get(policyTypeKey.getName());
370         ToscaPolicyType createdPolicyType = createdServiceTemplate.getPolicyTypes().get(1).get(policyTypeKey.getName());
371         assertEquals(true, beforePolicyType.getName().equals(createdPolicyType.getName()));
372         assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), createdPolicyType.getDescription()));
373
374         ToscaServiceTemplate deletedServiceTemplate = new AuthorativeToscaProvider().deletePolicyType(pfDao,
375                 policyTypeKey.getName(), policyTypeKey.getVersion());
376
377         ToscaPolicyType deletedPolicy = deletedServiceTemplate.getPolicyTypes().get(0).get(policyTypeKey.getName());
378         assertEquals(true, beforePolicyType.getName().equals(deletedPolicy.getName()));
379         assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), deletedPolicy.getDescription()));
380
381         ToscaServiceTemplate gotServiceTemplate = new AuthorativeToscaProvider().getPolicyTypes(pfDao,
382                 policyTypeKey.getName(), policyTypeKey.getVersion());
383
384         assertTrue(gotServiceTemplate.getPolicyTypes().isEmpty());
385     }
386
387     @Test
388     public void testAssertPoliciesExist() throws PfModelException {
389         ToscaServiceTemplate testServiceTemplate = new ToscaServiceTemplate();
390
391         assertThatThrownBy(() -> {
392             new AuthorativeToscaProvider().deletePolicyType(pfDao, "name", null);
393         }).hasMessage("version is marked @NonNull but is null");
394
395         assertThatThrownBy(() -> {
396             new AuthorativeToscaProvider().createPolicyTypes(pfDao, testServiceTemplate);
397         }).hasMessage("no policy types specified on service template");
398
399         testServiceTemplate.setToscaTopologyTemplate(new ToscaTopologyTemplate());
400         assertThatThrownBy(() -> {
401             new AuthorativeToscaProvider().createPolicyTypes(pfDao, testServiceTemplate);
402         }).hasMessage("no policy types specified on service template");
403
404         testServiceTemplate.setPolicyTypes(new ArrayList<>());
405         assertThatThrownBy(() -> {
406             new AuthorativeToscaProvider().createPolicyTypes(pfDao, testServiceTemplate);
407         }).hasMessage("An incoming list of concepts must have at least one entry");
408     }
409 }