Merge "Aai Simulator for CQ"
[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.sql.Connection;
30 import java.sql.DriverManager;
31 import java.util.ArrayList;
32 import java.util.List;
33
34 import org.apache.commons.lang3.ObjectUtils;
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 Connection connection;
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         // Use the JDBC UI "jdbc:h2:mem:testdb" to test towards the h2 database
87         // Use the JDBC UI "jdbc:mariadb://localhost:3306/policy" to test towards a locally installed mariadb instance
88         connection = DriverManager.getConnection("jdbc:h2:mem:testdb", "policy", "P01icY");
89
90         final DaoParameters daoParameters = new DaoParameters();
91         daoParameters.setPluginClass(DefaultPfDao.class.getCanonicalName());
92
93         // Use the persistence unit ToscaConceptTest to test towards the h2 database
94         // Use the persistence unit ToscaConceptMariaDBTest to test towards a locally installed mariadb instance
95         daoParameters.setPersistenceUnit("ToscaConceptTest");
96
97         pfDao = new PfDaoFactory().createPfDao(daoParameters);
98         pfDao.init(daoParameters);
99     }
100
101     /**
102      * Set up GSON.
103      */
104     @Before
105     public void setupGson() {
106         standardCoder = new StandardCoder();
107     }
108
109     @After
110     public void teardown() throws Exception {
111         pfDao.close();
112         connection.close();
113     }
114
115     @Test
116     public void testPolicyTypesGet() throws Exception {
117         assertThatThrownBy(() -> {
118             new AuthorativeToscaProvider().getPolicyTypes(null, null, null);
119         }).hasMessage("dao is marked @NonNull but is null");
120
121         assertThatThrownBy(() -> {
122             new AuthorativeToscaProvider().getPolicyList(null, null, null);
123         }).hasMessage("dao is marked @NonNull but is null");
124
125         ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class);
126
127         assertNotNull(toscaServiceTemplate);
128         ToscaServiceTemplate createdServiceTemplate =
129                 new AuthorativeToscaProvider().createPolicyTypes(pfDao, toscaServiceTemplate);
130
131         PfConceptKey policyTypeKey = new PfConceptKey("onap.policies.optimization.AffinityPolicy:0.0.0");
132
133         ToscaPolicyType beforePolicyType = toscaServiceTemplate.getPolicyTypes().get(1).get(policyTypeKey.getName());
134         ToscaPolicyType createdPolicyType = createdServiceTemplate.getPolicyTypes().get(0).get(policyTypeKey.getName());
135         assertEquals(true, beforePolicyType.getName().equals(createdPolicyType.getName()));
136         assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), createdPolicyType.getDescription()));
137
138         ToscaServiceTemplate gotServiceTemplate = new AuthorativeToscaProvider().getPolicyTypes(pfDao,
139                 policyTypeKey.getName(), policyTypeKey.getVersion());
140
141         ToscaPolicyType gotPolicyType = gotServiceTemplate.getPolicyTypes().get(0).get(policyTypeKey.getName());
142         assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
143         assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), createdPolicyType.getDescription()));
144
145         List<ToscaPolicyType> gotPolicyTypeList = new AuthorativeToscaProvider().getPolicyTypeList(pfDao,
146                 "onap.policies.optimization.AffinityPolicy", "0.0.0");
147         assertEquals(1, gotPolicyTypeList.size());
148         assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
149
150         gotPolicyTypeList = new AuthorativeToscaProvider().getPolicyTypeList(pfDao,
151                 "onap.policies.optimization.AffinityPolicy", null);
152         assertEquals(1, gotPolicyTypeList.size());
153         assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
154
155         gotPolicyTypeList = new AuthorativeToscaProvider().getPolicyTypeList(pfDao, null, null);
156         assertEquals(2, gotPolicyTypeList.size());
157         assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
158
159         gotPolicyTypeList = new AuthorativeToscaProvider().getPolicyTypeList(pfDao, null, "0.0.0");
160         assertEquals(2, gotPolicyTypeList.size());
161         assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
162     }
163
164
165     @Test
166     public void testPolicyTypesGetFiltered() throws Exception {
167         assertThatThrownBy(() -> {
168             new AuthorativeToscaProvider().getFilteredPolicyTypes(null, null);
169         }).hasMessage("dao is marked @NonNull but is null");
170
171         assertThatThrownBy(() -> {
172             new AuthorativeToscaProvider().getFilteredPolicyTypes(null, ToscaPolicyTypeFilter.builder().build());
173         }).hasMessage("dao is marked @NonNull but is null");
174
175         assertThatThrownBy(() -> {
176             new AuthorativeToscaProvider().getFilteredPolicyTypes(pfDao, null);
177         }).hasMessage("filter is marked @NonNull but is null");
178
179         assertThatThrownBy(() -> {
180             new AuthorativeToscaProvider().getFilteredPolicyTypeList(null, null);
181         }).hasMessage("dao is marked @NonNull but is null");
182
183         assertThatThrownBy(() -> {
184             new AuthorativeToscaProvider().getFilteredPolicyTypeList(null, ToscaPolicyTypeFilter.builder().build());
185         }).hasMessage("dao is marked @NonNull but is null");
186
187         assertThatThrownBy(() -> {
188             new AuthorativeToscaProvider().getFilteredPolicyTypeList(pfDao, null);
189         }).hasMessage("filter is marked @NonNull but is null");
190
191         ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class);
192
193         assertNotNull(toscaServiceTemplate);
194         ToscaServiceTemplate createdServiceTemplate =
195                 new AuthorativeToscaProvider().createPolicyTypes(pfDao, toscaServiceTemplate);
196
197         PfConceptKey policyTypeKey = new PfConceptKey("onap.policies.optimization.AffinityPolicy:0.0.0");
198
199         ToscaPolicyType beforePolicyType = toscaServiceTemplate.getPolicyTypes().get(1).get(policyTypeKey.getName());
200         ToscaPolicyType createdPolicyType = createdServiceTemplate.getPolicyTypes().get(0).get(policyTypeKey.getName());
201         assertEquals(true, beforePolicyType.getName().equals(createdPolicyType.getName()));
202         assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), createdPolicyType.getDescription()));
203
204         ToscaServiceTemplate gotServiceTemplate =
205                 new AuthorativeToscaProvider().getFilteredPolicyTypes(pfDao, ToscaPolicyTypeFilter.builder().build());
206
207         ToscaPolicyType gotPolicyType = gotServiceTemplate.getPolicyTypes().get(0).get(policyTypeKey.getName());
208         assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
209         assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), gotPolicyType.getDescription()));
210
211         gotServiceTemplate = new AuthorativeToscaProvider().getFilteredPolicyTypes(pfDao,
212                 ToscaPolicyTypeFilter.builder().name(policyTypeKey.getName()).build());
213
214         gotPolicyType = gotServiceTemplate.getPolicyTypes().get(0).get(policyTypeKey.getName());
215         assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
216         assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), gotPolicyType.getDescription()));
217
218         gotServiceTemplate = new AuthorativeToscaProvider().getFilteredPolicyTypes(pfDao,
219                 ToscaPolicyTypeFilter.builder().name(policyTypeKey.getName()).version("0.0.0").build());
220
221         gotPolicyType = gotServiceTemplate.getPolicyTypes().get(0).get(policyTypeKey.getName());
222         assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
223         assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), gotPolicyType.getDescription()));
224
225         List<ToscaPolicyType> gotPolicyTypeList = new AuthorativeToscaProvider().getPolicyTypeList(pfDao,
226                 "onap.policies.optimization.AffinityPolicy", "0.0.0");
227         assertEquals(1, gotPolicyTypeList.size());
228         assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
229
230         gotPolicyTypeList = new AuthorativeToscaProvider().getFilteredPolicyTypeList(pfDao,
231                 ToscaPolicyTypeFilter.builder().build());
232         assertEquals(2, gotPolicyTypeList.size());
233         assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
234
235         gotPolicyTypeList = new AuthorativeToscaProvider().getFilteredPolicyTypeList(pfDao,
236                 ToscaPolicyTypeFilter.builder().name(policyTypeKey.getName()).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()).version("0.0.0").build());
242         assertEquals(1, gotPolicyTypeList.size());
243         assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
244
245         gotPolicyTypeList = new AuthorativeToscaProvider().getFilteredPolicyTypeList(pfDao,
246                 ToscaPolicyTypeFilter.builder().version("1.0.0").build());
247         assertEquals(1, gotPolicyTypeList.size());
248         assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
249     }
250
251     @Test
252     public void testPolicyTypesCreate() throws Exception {
253         assertThatThrownBy(() -> {
254             new AuthorativeToscaProvider().createPolicyTypes(null, null);
255         }).hasMessage("dao is marked @NonNull but is null");
256
257         assertThatThrownBy(() -> {
258             new AuthorativeToscaProvider().createPolicyTypes(null, new ToscaServiceTemplate());
259         }).hasMessage("dao is marked @NonNull but is null");
260
261         assertThatThrownBy(() -> {
262             new AuthorativeToscaProvider().createPolicyTypes(pfDao, null);
263         }).hasMessage("serviceTemplate is marked @NonNull but is null");
264
265         ToscaServiceTemplate testToscaServiceTemplate = new ToscaServiceTemplate();
266         assertThatThrownBy(() -> {
267             new AuthorativeToscaProvider().createPolicyTypes(pfDao, testToscaServiceTemplate);
268         }).hasMessage("no policy types specified on service template");
269
270         testToscaServiceTemplate.setPolicyTypes(new ArrayList<>());
271         assertThatThrownBy(() -> {
272             new AuthorativeToscaProvider().createPolicyTypes(pfDao, testToscaServiceTemplate);
273         }).hasMessage("An incoming list of concepts must have at least one entry");
274
275         ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class);
276
277         assertNotNull(toscaServiceTemplate);
278         ToscaServiceTemplate createdServiceTemplate =
279                 new AuthorativeToscaProvider().createPolicyTypes(pfDao, toscaServiceTemplate);
280
281         PfConceptKey policyTypeKey = new PfConceptKey("onap.policies.optimization.AffinityPolicy:0.0.0");
282
283         ToscaPolicyType beforePolicyType = toscaServiceTemplate.getPolicyTypes().get(1).get(policyTypeKey.getName());
284         ToscaPolicyType createdPolicyType = createdServiceTemplate.getPolicyTypes().get(0).get(policyTypeKey.getName());
285         assertEquals(true, beforePolicyType.getName().equals(createdPolicyType.getName()));
286         assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), createdPolicyType.getDescription()));
287     }
288
289     @Test
290     public void testPolicyTypesUpdate() throws Exception {
291         assertThatThrownBy(() -> {
292             new AuthorativeToscaProvider().createPolicyTypes(null, null);
293         }).hasMessage("dao is marked @NonNull but is null");
294
295         assertThatThrownBy(() -> {
296             new AuthorativeToscaProvider().updatePolicyTypes(null, null);
297         }).hasMessage("dao is marked @NonNull but is null");
298
299         assertThatThrownBy(() -> {
300             new AuthorativeToscaProvider().updatePolicyTypes(null, new ToscaServiceTemplate());
301         }).hasMessage("dao is marked @NonNull but is null");
302
303         assertThatThrownBy(() -> {
304             new AuthorativeToscaProvider().updatePolicyTypes(pfDao, null);
305         }).hasMessage("serviceTemplate is marked @NonNull but is null");
306
307         ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class);
308
309         assertNotNull(toscaServiceTemplate);
310         ToscaServiceTemplate createdServiceTemplate =
311                 new AuthorativeToscaProvider().createPolicyTypes(pfDao, toscaServiceTemplate);
312
313         PfConceptKey policyTypeKey = new PfConceptKey("onap.policies.optimization.AffinityPolicy:0.0.0");
314
315         ToscaPolicyType beforePolicyType = toscaServiceTemplate.getPolicyTypes().get(1).get(policyTypeKey.getName());
316         ToscaPolicyType createdPolicyType = createdServiceTemplate.getPolicyTypes().get(0).get(policyTypeKey.getName());
317         assertEquals(true, beforePolicyType.getName().equals(createdPolicyType.getName()));
318         assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), createdPolicyType.getDescription()));
319
320         ToscaServiceTemplate updatedServiceTemplate =
321                 new AuthorativeToscaProvider().updatePolicyTypes(pfDao, toscaServiceTemplate);
322
323         ToscaPolicyType updatedPolicy = updatedServiceTemplate.getPolicyTypes().get(0).get(policyTypeKey.getName());
324         assertEquals(true, beforePolicyType.getName().equals(updatedPolicy.getName()));
325         assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), updatedPolicy.getDescription()));
326     }
327
328     @Test
329     public void testPolicyTypesDelete() throws Exception {
330         assertThatThrownBy(() -> {
331             new AuthorativeToscaProvider().deletePolicyType(null, null, null);
332         }).hasMessage("dao is marked @NonNull but is null");
333
334         assertThatThrownBy(() -> {
335             new AuthorativeToscaProvider().deletePolicyType(null, null, "version");
336         }).hasMessage("dao is marked @NonNull but is null");
337
338         assertThatThrownBy(() -> {
339             new AuthorativeToscaProvider().deletePolicyType(null, "name", null);
340         }).hasMessage("dao is marked @NonNull but is null");
341
342         assertThatThrownBy(() -> {
343             new AuthorativeToscaProvider().deletePolicyType(null, "name", "version");
344         }).hasMessage("dao is marked @NonNull but is null");
345
346         assertThatThrownBy(() -> {
347             new AuthorativeToscaProvider().deletePolicyType(pfDao, null, null);
348         }).hasMessage("name is marked @NonNull but is null");
349
350         assertThatThrownBy(() -> {
351             new AuthorativeToscaProvider().deletePolicyType(pfDao, null, "version");
352         }).hasMessage("name is marked @NonNull but is null");
353
354         assertThatThrownBy(() -> {
355             new AuthorativeToscaProvider().deletePolicyType(pfDao, "name", null);
356         }).hasMessage("version is marked @NonNull but is null");
357
358         ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class);
359
360         assertNotNull(toscaServiceTemplate);
361         ToscaServiceTemplate createdServiceTemplate =
362                 new AuthorativeToscaProvider().createPolicyTypes(pfDao, toscaServiceTemplate);
363
364         PfConceptKey policyTypeKey = new PfConceptKey("onap.policies.optimization.AffinityPolicy:0.0.0");
365
366         ToscaPolicyType beforePolicyType = toscaServiceTemplate.getPolicyTypes().get(1).get(policyTypeKey.getName());
367         ToscaPolicyType createdPolicyType = createdServiceTemplate.getPolicyTypes().get(0).get(policyTypeKey.getName());
368         assertEquals(true, beforePolicyType.getName().equals(createdPolicyType.getName()));
369         assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), createdPolicyType.getDescription()));
370
371         ToscaServiceTemplate deletedServiceTemplate = new AuthorativeToscaProvider().deletePolicyType(pfDao,
372                 policyTypeKey.getName(), policyTypeKey.getVersion());
373
374         ToscaPolicyType deletedPolicy = deletedServiceTemplate.getPolicyTypes().get(0).get(policyTypeKey.getName());
375         assertEquals(true, beforePolicyType.getName().equals(deletedPolicy.getName()));
376         assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), deletedPolicy.getDescription()));
377
378         ToscaServiceTemplate gotServiceTemplate = new AuthorativeToscaProvider().getPolicyTypes(pfDao,
379                 policyTypeKey.getName(), policyTypeKey.getVersion());
380
381         assertEquals(0, gotServiceTemplate.getPolicyTypes().get(0).size());
382     }
383
384     @Test
385     public void testAssertPoliciesExist() throws PfModelException {
386         ToscaServiceTemplate testServiceTemplate = new ToscaServiceTemplate();
387
388         assertThatThrownBy(() -> {
389             new AuthorativeToscaProvider().deletePolicyType(pfDao, "name", null);
390         }).hasMessage("version is marked @NonNull but is null");
391
392         assertThatThrownBy(() -> {
393             new AuthorativeToscaProvider().createPolicyTypes(pfDao, testServiceTemplate);
394         }).hasMessage("no policy types specified on service template");
395
396         testServiceTemplate.setToscaTopologyTemplate(new ToscaTopologyTemplate());
397         assertThatThrownBy(() -> {
398             new AuthorativeToscaProvider().createPolicyTypes(pfDao, testServiceTemplate);
399         }).hasMessage("no policy types specified on service template");
400
401         testServiceTemplate.setPolicyTypes(new ArrayList<>());
402         assertThatThrownBy(() -> {
403             new AuthorativeToscaProvider().createPolicyTypes(pfDao, testServiceTemplate);
404         }).hasMessage("An incoming list of concepts must have at least one entry");
405     }
406 }