Fix simple sonar issues in models-tosca
[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  *  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 import java.util.ArrayList;
31 import java.util.List;
32 import java.util.Properties;
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.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 final String VERSION = "version";
59     private static final String POLICY_AFFINITY_VERSION0 = "onap.policies.optimization.AffinityPolicy:0.0.0";
60     private static final String POLICY_AFFINITY = "onap.policies.optimization.AffinityPolicy";
61     private static final String MISSING_POLICY_TYPES = "no policy types specified on service template";
62     private static final String DAO_IS_NULL = "dao is marked @NonNull but is null";
63     private static final String VERSION_000 = "0.0.0";
64     private static String yamlAsJsonString;
65     private PfDao pfDao;
66     private StandardCoder standardCoder;
67
68
69     /**
70      * Read the policy type definition.
71      *
72      * @throws Exception on errors
73      */
74     @BeforeClass
75     public static void readPolicyDefinition() {
76         String yamlString =
77                 ResourceUtils.getResourceAsString("policytypes/onap.policies.optimization.AffinityPolicy.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.getCanonicalName());
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         }).hasMessage(DAO_IS_NULL);
127
128         assertThatThrownBy(() -> {
129             new AuthorativeToscaProvider().getPolicyList(null, null, null);
130         }).hasMessage(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(1).get(policyTypeKey.getName());
141         ToscaPolicyType createdPolicyType = createdServiceTemplate.getPolicyTypes().get(1).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(0).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 = new AuthorativeToscaProvider().getPolicyTypeList(pfDao,
153                 POLICY_AFFINITY, VERSION_000);
154         assertEquals(1, gotPolicyTypeList.size());
155         assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
156
157         gotPolicyTypeList = new AuthorativeToscaProvider().getPolicyTypeList(pfDao,
158                 POLICY_AFFINITY, null);
159         assertEquals(1, gotPolicyTypeList.size());
160         assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
161
162         gotPolicyTypeList = new AuthorativeToscaProvider().getPolicyTypeList(pfDao, null, null);
163         assertEquals(2, gotPolicyTypeList.size());
164         assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
165
166         gotPolicyTypeList = new AuthorativeToscaProvider().getPolicyTypeList(pfDao, null, VERSION_000);
167         assertEquals(2, gotPolicyTypeList.size());
168         assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
169     }
170
171
172     @Test
173     public void testPolicyTypesGetFiltered() throws Exception {
174         assertThatThrownBy(() -> {
175             new AuthorativeToscaProvider().getFilteredPolicyTypes(null, null);
176         }).hasMessage(DAO_IS_NULL);
177
178         assertThatThrownBy(() -> {
179             new AuthorativeToscaProvider().getFilteredPolicyTypes(null, ToscaPolicyTypeFilter.builder().build());
180         }).hasMessage(DAO_IS_NULL);
181
182         assertThatThrownBy(() -> {
183             new AuthorativeToscaProvider().getFilteredPolicyTypes(pfDao, null);
184         }).hasMessage("filter is marked @NonNull but is null");
185
186         assertThatThrownBy(() -> {
187             new AuthorativeToscaProvider().getFilteredPolicyTypeList(null, null);
188         }).hasMessage(DAO_IS_NULL);
189
190         assertThatThrownBy(() -> {
191             new AuthorativeToscaProvider().getFilteredPolicyTypeList(null, ToscaPolicyTypeFilter.builder().build());
192         }).hasMessage(DAO_IS_NULL);
193
194         assertThatThrownBy(() -> {
195             new AuthorativeToscaProvider().getFilteredPolicyTypeList(pfDao, null);
196         }).hasMessage("filter is marked @NonNull but is null");
197
198         ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class);
199
200         assertNotNull(toscaServiceTemplate);
201         ToscaServiceTemplate createdServiceTemplate =
202                 new AuthorativeToscaProvider().createPolicyTypes(pfDao, toscaServiceTemplate);
203
204         PfConceptKey policyTypeKey = new PfConceptKey(POLICY_AFFINITY_VERSION0);
205
206         ToscaPolicyType beforePolicyType = toscaServiceTemplate.getPolicyTypes().get(1).get(policyTypeKey.getName());
207         ToscaPolicyType createdPolicyType = createdServiceTemplate.getPolicyTypes().get(1).get(policyTypeKey.getName());
208         assertEquals(true, beforePolicyType.getName().equals(createdPolicyType.getName()));
209         assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), createdPolicyType.getDescription()));
210
211         ToscaServiceTemplate gotServiceTemplate =
212                 new AuthorativeToscaProvider().getFilteredPolicyTypes(pfDao, ToscaPolicyTypeFilter.builder().build());
213
214         ToscaPolicyType 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()).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         gotServiceTemplate = new AuthorativeToscaProvider().getFilteredPolicyTypes(pfDao,
226                 ToscaPolicyTypeFilter.builder().name(policyTypeKey.getName()).version(VERSION_000).build());
227
228         gotPolicyType = gotServiceTemplate.getPolicyTypes().get(0).get(policyTypeKey.getName());
229         assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
230         assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), gotPolicyType.getDescription()));
231
232         List<ToscaPolicyType> gotPolicyTypeList = new AuthorativeToscaProvider().getPolicyTypeList(pfDao,
233                 POLICY_AFFINITY, VERSION_000);
234         assertEquals(1, gotPolicyTypeList.size());
235         assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
236
237         gotPolicyTypeList = new AuthorativeToscaProvider().getFilteredPolicyTypeList(pfDao,
238                 ToscaPolicyTypeFilter.builder().build());
239         assertEquals(2, gotPolicyTypeList.size());
240         assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
241
242         gotPolicyTypeList = new AuthorativeToscaProvider().getFilteredPolicyTypeList(pfDao,
243                 ToscaPolicyTypeFilter.builder().name(policyTypeKey.getName()).build());
244         assertEquals(1, gotPolicyTypeList.size());
245         assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
246
247         gotPolicyTypeList = new AuthorativeToscaProvider().getFilteredPolicyTypeList(pfDao,
248                 ToscaPolicyTypeFilter.builder().name(policyTypeKey.getName()).version(VERSION_000).build());
249         assertEquals(1, gotPolicyTypeList.size());
250         assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
251
252         gotPolicyTypeList = new AuthorativeToscaProvider().getFilteredPolicyTypeList(pfDao,
253                 ToscaPolicyTypeFilter.builder().version("1.0.0").build());
254         assertEquals(1, gotPolicyTypeList.size());
255         assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
256     }
257
258     @Test
259     public void testPolicyTypesCreate() throws Exception {
260         assertThatThrownBy(() -> {
261             new AuthorativeToscaProvider().createPolicyTypes(null, null);
262         }).hasMessage(DAO_IS_NULL);
263
264         assertThatThrownBy(() -> {
265             new AuthorativeToscaProvider().createPolicyTypes(null, new ToscaServiceTemplate());
266         }).hasMessage(DAO_IS_NULL);
267
268         assertThatThrownBy(() -> {
269             new AuthorativeToscaProvider().createPolicyTypes(pfDao, null);
270         }).hasMessage("serviceTemplate is marked @NonNull but is null");
271
272         ToscaServiceTemplate testToscaServiceTemplate = new ToscaServiceTemplate();
273         assertThatThrownBy(() -> {
274             new AuthorativeToscaProvider().createPolicyTypes(pfDao, testToscaServiceTemplate);
275         }).hasMessage(MISSING_POLICY_TYPES);
276
277         testToscaServiceTemplate.setPolicyTypes(new ArrayList<>());
278         assertThatThrownBy(() -> {
279             new AuthorativeToscaProvider().createPolicyTypes(pfDao, testToscaServiceTemplate);
280         }).hasMessage("An incoming list of concepts must have at least one entry");
281
282         ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class);
283
284         assertNotNull(toscaServiceTemplate);
285         ToscaServiceTemplate createdServiceTemplate =
286                 new AuthorativeToscaProvider().createPolicyTypes(pfDao, toscaServiceTemplate);
287
288         PfConceptKey policyTypeKey = new PfConceptKey(POLICY_AFFINITY_VERSION0);
289
290         ToscaPolicyType beforePolicyType = toscaServiceTemplate.getPolicyTypes().get(1).get(policyTypeKey.getName());
291         ToscaPolicyType createdPolicyType = createdServiceTemplate.getPolicyTypes().get(1).get(policyTypeKey.getName());
292         assertEquals(true, beforePolicyType.getName().equals(createdPolicyType.getName()));
293         assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), createdPolicyType.getDescription()));
294     }
295
296     @Test
297     public void testPolicyTypesUpdate() throws Exception {
298         assertThatThrownBy(() -> {
299             new AuthorativeToscaProvider().createPolicyTypes(null, null);
300         }).hasMessage(DAO_IS_NULL);
301
302         assertThatThrownBy(() -> {
303             new AuthorativeToscaProvider().updatePolicyTypes(null, null);
304         }).hasMessage(DAO_IS_NULL);
305
306         assertThatThrownBy(() -> {
307             new AuthorativeToscaProvider().updatePolicyTypes(null, new ToscaServiceTemplate());
308         }).hasMessage(DAO_IS_NULL);
309
310         assertThatThrownBy(() -> {
311             new AuthorativeToscaProvider().updatePolicyTypes(pfDao, null);
312         }).hasMessage("serviceTemplate is marked @NonNull but is null");
313
314         ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class);
315
316         assertNotNull(toscaServiceTemplate);
317         ToscaServiceTemplate createdServiceTemplate =
318                 new AuthorativeToscaProvider().createPolicyTypes(pfDao, toscaServiceTemplate);
319
320         PfConceptKey policyTypeKey = new PfConceptKey(POLICY_AFFINITY_VERSION0);
321
322         ToscaPolicyType beforePolicyType = toscaServiceTemplate.getPolicyTypes().get(1).get(policyTypeKey.getName());
323         ToscaPolicyType createdPolicyType = createdServiceTemplate.getPolicyTypes().get(1).get(policyTypeKey.getName());
324         assertEquals(true, beforePolicyType.getName().equals(createdPolicyType.getName()));
325         assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), createdPolicyType.getDescription()));
326
327         ToscaServiceTemplate updatedServiceTemplate =
328                 new AuthorativeToscaProvider().updatePolicyTypes(pfDao, toscaServiceTemplate);
329
330         ToscaPolicyType updatedPolicy = updatedServiceTemplate.getPolicyTypes().get(1).get(policyTypeKey.getName());
331         assertEquals(true, beforePolicyType.getName().equals(updatedPolicy.getName()));
332         assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), updatedPolicy.getDescription()));
333     }
334
335     @Test
336     public void testPolicyTypesDelete() throws Exception {
337         assertThatThrownBy(() -> {
338             new AuthorativeToscaProvider().deletePolicyType(null, null, null);
339         }).hasMessage(DAO_IS_NULL);
340
341         assertThatThrownBy(() -> {
342             new AuthorativeToscaProvider().deletePolicyType(null, null, VERSION);
343         }).hasMessage(DAO_IS_NULL);
344
345         assertThatThrownBy(() -> {
346             new AuthorativeToscaProvider().deletePolicyType(null, "name", null);
347         }).hasMessage(DAO_IS_NULL);
348
349         assertThatThrownBy(() -> {
350             new AuthorativeToscaProvider().deletePolicyType(null, "name", VERSION);
351         }).hasMessage(DAO_IS_NULL);
352
353         assertThatThrownBy(() -> {
354             new AuthorativeToscaProvider().deletePolicyType(pfDao, null, null);
355         }).hasMessage("name is marked @NonNull but is null");
356
357         assertThatThrownBy(() -> {
358             new AuthorativeToscaProvider().deletePolicyType(pfDao, null, VERSION);
359         }).hasMessage("name is marked @NonNull but is null");
360
361         assertThatThrownBy(() -> {
362             new AuthorativeToscaProvider().deletePolicyType(pfDao, "name", null);
363         }).hasMessage("version is marked @NonNull but is null");
364
365         ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class);
366
367         assertNotNull(toscaServiceTemplate);
368         ToscaServiceTemplate createdServiceTemplate =
369                 new AuthorativeToscaProvider().createPolicyTypes(pfDao, toscaServiceTemplate);
370
371         PfConceptKey policyTypeKey = new PfConceptKey(POLICY_AFFINITY_VERSION0);
372
373         ToscaPolicyType beforePolicyType = toscaServiceTemplate.getPolicyTypes().get(1).get(policyTypeKey.getName());
374         ToscaPolicyType createdPolicyType = createdServiceTemplate.getPolicyTypes().get(1).get(policyTypeKey.getName());
375         assertEquals(true, beforePolicyType.getName().equals(createdPolicyType.getName()));
376         assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), createdPolicyType.getDescription()));
377
378         ToscaServiceTemplate deletedServiceTemplate = new AuthorativeToscaProvider().deletePolicyType(pfDao,
379                 policyTypeKey.getName(), policyTypeKey.getVersion());
380
381         ToscaPolicyType deletedPolicy = deletedServiceTemplate.getPolicyTypes().get(0).get(policyTypeKey.getName());
382         assertEquals(true, beforePolicyType.getName().equals(deletedPolicy.getName()));
383         assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), deletedPolicy.getDescription()));
384
385         ToscaServiceTemplate gotServiceTemplate = new AuthorativeToscaProvider().getPolicyTypes(pfDao,
386                 policyTypeKey.getName(), policyTypeKey.getVersion());
387
388         assertTrue(gotServiceTemplate.getPolicyTypes().isEmpty());
389     }
390
391     @Test
392     public void testAssertPoliciesExist() {
393         ToscaServiceTemplate testServiceTemplate = new ToscaServiceTemplate();
394
395         assertThatThrownBy(() -> {
396             new AuthorativeToscaProvider().deletePolicyType(pfDao, "name", null);
397         }).hasMessage("version is marked @NonNull but is null");
398
399         assertThatThrownBy(() -> {
400             new AuthorativeToscaProvider().createPolicyTypes(pfDao, testServiceTemplate);
401         }).hasMessage(MISSING_POLICY_TYPES);
402
403         testServiceTemplate.setToscaTopologyTemplate(new ToscaTopologyTemplate());
404         assertThatThrownBy(() -> {
405             new AuthorativeToscaProvider().createPolicyTypes(pfDao, testServiceTemplate);
406         }).hasMessage(MISSING_POLICY_TYPES);
407
408         testServiceTemplate.setPolicyTypes(new ArrayList<>());
409         assertThatThrownBy(() -> {
410             new AuthorativeToscaProvider().createPolicyTypes(pfDao, testServiceTemplate);
411         }).hasMessage("An incoming list of concepts must have at least one entry");
412     }
413 }