9ae7842a61696f3114c8b43dfe1d1ceb7d9c0368
[policy/models.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019-2021, 2023-2025 Nordix Foundation.
4  *  Modifications Copyright (C) 2019, 2021 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.jupiter.api.Assertions.assertEquals;
26 import static org.junit.jupiter.api.Assertions.assertNotNull;
27 import static org.junit.jupiter.api.Assertions.assertTrue;
28
29 import com.google.gson.GsonBuilder;
30 import java.util.LinkedHashMap;
31 import java.util.List;
32 import java.util.Properties;
33 import org.apache.commons.lang3.ObjectUtils;
34 import org.junit.jupiter.api.AfterEach;
35 import org.junit.jupiter.api.BeforeAll;
36 import org.junit.jupiter.api.BeforeEach;
37 import org.junit.jupiter.api.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.dao.DaoParameters;
42 import org.onap.policy.models.dao.PfDao;
43 import org.onap.policy.models.dao.PfDaoFactory;
44 import org.onap.policy.models.dao.impl.DefaultPfDao;
45 import org.onap.policy.models.tosca.authorative.concepts.ToscaEntityFilter;
46 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyType;
47 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
48 import org.onap.policy.models.tosca.authorative.concepts.ToscaTopologyTemplate;
49 import org.yaml.snakeyaml.Yaml;
50
51 /**
52  * Test of the {@link AuthorativeToscaProvider} class.
53  */
54 class AuthorativeToscaProviderPolicyTypeTest {
55     private static final String VERSION = "version";
56     private static final String POLICY_NO_VERSION_VERSION1 = "onap.policies.NoVersion:0.0.1";
57     private static final String POLICY_NO_VERSION = "onap.policies.NoVersion";
58     private static final String MISSING_POLICY_TYPES = "no policy types specified on service template";
59     private static final String DAO_IS_NULL = "^dao is marked .*on.*ull but is null$";
60     private static final String VERSION_001 = "0.0.1";
61     private static String yamlAsJsonString;
62     private PfDao pfDao;
63     private StandardCoder standardCoder;
64
65     /**
66      * Read the policy type definition.
67      *
68      */
69     @BeforeAll
70     public static void readPolicyDefinition() {
71         String yamlString = ResourceUtils.getResourceAsString("src/test/resources/onap.policies.NoVersion.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     @BeforeEach
83     void setupDao() throws Exception {
84         final DaoParameters daoParameters = new DaoParameters();
85         daoParameters.setPluginClass(DefaultPfDao.class.getName());
86
87         daoParameters.setPersistenceUnit("ToscaConceptTest");
88
89         Properties jdbcProperties = new Properties();
90         jdbcProperties.setProperty("jakarta.persistence.jdbc.user", "policy");
91         jdbcProperties.setProperty("jakarta.persistence.jdbc.password", "P01icY");
92         jdbcProperties.setProperty("jakarta.persistence.jdbc.driver", "org.h2.Driver");
93         jdbcProperties.setProperty("jakarta.persistence.jdbc.url",
94             "jdbc:h2:mem:AuthorativeToscaProviderPolicyTypeTest");
95         daoParameters.setJdbcProperties(jdbcProperties);
96
97         pfDao = new PfDaoFactory().createPfDao(daoParameters);
98         pfDao.init(daoParameters);
99     }
100
101     /**
102      * Set up GSON.
103      */
104     @BeforeEach
105     void setupGson() {
106         standardCoder = new StandardCoder();
107     }
108
109     @AfterEach
110     void teardown() {
111         pfDao.close();
112     }
113
114     @Test
115     void testPolicyTypesGet() throws Exception {
116         assertThatThrownBy(() -> new AuthorativeToscaProvider().getPolicyTypes(null, null, null))
117             .hasMessageMatching(DAO_IS_NULL);
118
119         assertThatThrownBy(() -> new AuthorativeToscaProvider().getPolicyList(null, null, null))
120             .hasMessageMatching(DAO_IS_NULL);
121
122         ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class);
123
124         assertNotNull(toscaServiceTemplate);
125         ToscaServiceTemplate createdServiceTemplate =
126                 new AuthorativeToscaProvider().createPolicyTypes(pfDao, toscaServiceTemplate);
127
128         PfConceptKey policyTypeKey = new PfConceptKey(POLICY_NO_VERSION_VERSION1);
129
130         ToscaPolicyType beforePolicyType = toscaServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
131         ToscaPolicyType createdPolicyType = createdServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
132         assertEquals(beforePolicyType.getName(), createdPolicyType.getName());
133         assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), createdPolicyType.getDescription()));
134
135         ToscaServiceTemplate gotServiceTemplate = new AuthorativeToscaProvider().getPolicyTypes(pfDao,
136                 policyTypeKey.getName(), policyTypeKey.getVersion());
137
138         ToscaPolicyType gotPolicyType = gotServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
139         assertEquals(beforePolicyType.getName(), gotPolicyType.getName());
140         assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), createdPolicyType.getDescription()));
141
142         List<ToscaPolicyType> gotPolicyTypeList =
143                 new AuthorativeToscaProvider().getPolicyTypeList(pfDao, POLICY_NO_VERSION, VERSION_001);
144         assertEquals(2, gotPolicyTypeList.size());
145         assertEquals(beforePolicyType.getName(), gotPolicyType.getName());
146
147         gotPolicyTypeList = new AuthorativeToscaProvider().getPolicyTypeList(pfDao, POLICY_NO_VERSION, null);
148         assertEquals(2, gotPolicyTypeList.size());
149         assertEquals(beforePolicyType.getName(), gotPolicyType.getName());
150
151         gotPolicyTypeList = new AuthorativeToscaProvider().getPolicyTypeList(pfDao, null, null);
152         assertEquals(2, gotPolicyTypeList.size());
153         assertEquals(beforePolicyType.getName(), gotPolicyType.getName());
154
155         gotPolicyTypeList = new AuthorativeToscaProvider().getPolicyTypeList(pfDao, null, VERSION_001);
156         assertEquals(2, gotPolicyTypeList.size());
157         assertEquals(beforePolicyType.getName(), gotPolicyType.getName());
158
159         assertThatThrownBy(() -> new AuthorativeToscaProvider().getPolicyTypeList(new DefaultPfDao(), POLICY_NO_VERSION,
160                 VERSION_001)).hasMessageContaining("Policy Framework DAO has not been initialized");
161
162         assertTrue(new AuthorativeToscaProvider().getPolicyTypeList(pfDao, "i.dont.Exist", VERSION_001).isEmpty());
163     }
164
165     @Test
166     void testPolicyTypesGetFiltered() throws Exception {
167         assertThatThrownBy(() -> new AuthorativeToscaProvider().getFilteredPolicyTypes(null, null))
168             .hasMessageMatching(DAO_IS_NULL);
169
170         assertThatThrownBy(() -> new AuthorativeToscaProvider().getFilteredPolicyTypes(null,
171                 ToscaEntityFilter.<ToscaPolicyType>builder().build())).hasMessageMatching(DAO_IS_NULL);
172
173         assertThatThrownBy(() -> new AuthorativeToscaProvider().getFilteredPolicyTypes(pfDao, null))
174             .hasMessageMatching("^filter is marked .*on.*ull but is null$");
175
176         assertThatThrownBy(() -> new AuthorativeToscaProvider().getFilteredPolicyTypeList(null, null))
177             .hasMessageMatching(DAO_IS_NULL);
178
179         assertThatThrownBy(() -> new AuthorativeToscaProvider().getFilteredPolicyTypeList(null,
180                 ToscaEntityFilter.<ToscaPolicyType>builder().build()))
181             .hasMessageMatching(DAO_IS_NULL);
182
183         assertThatThrownBy(() -> new AuthorativeToscaProvider().getFilteredPolicyTypeList(pfDao, null))
184             .hasMessageMatching("^filter is marked .*on.*ull but is null$");
185
186         assertThatThrownBy(() -> new AuthorativeToscaProvider().getFilteredPolicyTypeList(new DefaultPfDao(),
187                 ToscaEntityFilter.<ToscaPolicyType>builder().name("i.dont.Exist").build()))
188                         .hasMessageContaining("Policy Framework DAO has not been initialized");
189
190         assertTrue(new AuthorativeToscaProvider().getFilteredPolicyTypeList(pfDao,
191                 ToscaEntityFilter.<ToscaPolicyType>builder().name("i.dont.Exist").build()).isEmpty());
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(POLICY_NO_VERSION_VERSION1);
200
201         ToscaPolicyType beforePolicyType = toscaServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
202         ToscaPolicyType createdPolicyType = createdServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
203         assertEquals(beforePolicyType.getName(), createdPolicyType.getName());
204         assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), createdPolicyType.getDescription()));
205
206         ToscaServiceTemplate gotServiceTemplate = new AuthorativeToscaProvider().getFilteredPolicyTypes(pfDao,
207                 ToscaEntityFilter.<ToscaPolicyType>builder().build());
208
209         ToscaPolicyType gotPolicyType = gotServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
210         checkEqualsNameDescription(beforePolicyType, gotPolicyType);
211
212         gotServiceTemplate = new AuthorativeToscaProvider().getFilteredPolicyTypes(pfDao,
213                 ToscaEntityFilter.<ToscaPolicyType>builder().name(policyTypeKey.getName()).build());
214
215         gotPolicyType = gotServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
216         checkEqualsNameDescription(beforePolicyType, gotPolicyType);
217
218         gotServiceTemplate = new AuthorativeToscaProvider().getFilteredPolicyTypes(pfDao, ToscaEntityFilter
219                 .<ToscaPolicyType>builder().name(policyTypeKey.getName()).version(VERSION_001).build());
220
221         gotPolicyType = gotServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
222         checkEqualsNameDescription(beforePolicyType, gotPolicyType);
223
224         List<ToscaPolicyType> gotPolicyTypeList =
225                 new AuthorativeToscaProvider().getPolicyTypeList(pfDao, POLICY_NO_VERSION, VERSION_001);
226         assertEquals(2, gotPolicyTypeList.size());
227         assertEquals(beforePolicyType.getName(), gotPolicyType.getName());
228
229         gotPolicyTypeList = new AuthorativeToscaProvider().getFilteredPolicyTypeList(pfDao,
230                 ToscaEntityFilter.<ToscaPolicyType>builder().build());
231         assertEquals(2, gotPolicyTypeList.size());
232         assertEquals(beforePolicyType.getName(), gotPolicyType.getName());
233
234         gotPolicyTypeList = new AuthorativeToscaProvider().getFilteredPolicyTypeList(pfDao,
235                 ToscaEntityFilter.<ToscaPolicyType>builder().name(policyTypeKey.getName()).build());
236         assertEquals(1, gotPolicyTypeList.size());
237         assertEquals(beforePolicyType.getName(), gotPolicyType.getName());
238
239         gotPolicyTypeList = new AuthorativeToscaProvider().getFilteredPolicyTypeList(pfDao, ToscaEntityFilter
240                 .<ToscaPolicyType>builder().name(policyTypeKey.getName()).version(VERSION_001).build());
241         assertEquals(1, gotPolicyTypeList.size());
242         assertEquals(beforePolicyType.getName(), gotPolicyType.getName());
243
244         gotPolicyTypeList = new AuthorativeToscaProvider().getFilteredPolicyTypeList(pfDao,
245                 ToscaEntityFilter.<ToscaPolicyType>builder().version("1.0.0").build());
246         assertEquals(1, gotPolicyTypeList.size());
247         assertEquals(beforePolicyType.getName(), gotPolicyType.getName());
248     }
249
250     private void checkEqualsNameDescription(ToscaPolicyType beforePolicyType, ToscaPolicyType gotPolicyType) {
251         assertEquals(beforePolicyType.getName(), gotPolicyType.getName());
252         assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), gotPolicyType.getDescription()));
253     }
254
255     @Test
256     void testPolicyTypesCreate() throws Exception {
257         assertThatThrownBy(() -> new AuthorativeToscaProvider().createPolicyTypes(null, null))
258             .hasMessageMatching(DAO_IS_NULL);
259
260         assertThatThrownBy(() -> new AuthorativeToscaProvider().createPolicyTypes(null, new ToscaServiceTemplate()))
261             .hasMessageMatching(DAO_IS_NULL);
262
263         assertThatThrownBy(() -> new AuthorativeToscaProvider().createPolicyTypes(pfDao, null))
264             .hasMessageMatching("^serviceTemplate is marked .*on.*ull but is null$");
265
266         ToscaServiceTemplate testToscaServiceTemplate = new ToscaServiceTemplate();
267         assertThatThrownBy(() -> new AuthorativeToscaProvider().createPolicyTypes(pfDao, testToscaServiceTemplate))
268             .hasMessage(MISSING_POLICY_TYPES);
269
270         testToscaServiceTemplate.setPolicyTypes(new LinkedHashMap<>());
271         assertThatThrownBy(() -> new AuthorativeToscaProvider().createPolicyTypes(pfDao, testToscaServiceTemplate))
272             .hasMessage("An incoming list of concepts must have at least one entry");
273
274         ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class);
275
276         assertNotNull(toscaServiceTemplate);
277         ToscaServiceTemplate createdServiceTemplate =
278                 new AuthorativeToscaProvider().createPolicyTypes(pfDao, toscaServiceTemplate);
279
280         PfConceptKey policyTypeKey = new PfConceptKey(POLICY_NO_VERSION_VERSION1);
281
282         ToscaPolicyType beforePolicyType = toscaServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
283         ToscaPolicyType createdPolicyType = createdServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
284         assertEquals(beforePolicyType.getName(), createdPolicyType.getName());
285         assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), createdPolicyType.getDescription()));
286     }
287
288     @Test
289     void testPolicyTypesUpdate() throws Exception {
290         assertThatThrownBy(() -> new AuthorativeToscaProvider().createPolicyTypes(null, null))
291             .hasMessageMatching(DAO_IS_NULL);
292
293         assertThatThrownBy(() -> new AuthorativeToscaProvider().updatePolicyTypes(null, null))
294             .hasMessageMatching(DAO_IS_NULL);
295
296         assertThatThrownBy(() -> new AuthorativeToscaProvider().updatePolicyTypes(null, new ToscaServiceTemplate()))
297             .hasMessageMatching(DAO_IS_NULL);
298
299         assertThatThrownBy(() -> new AuthorativeToscaProvider().updatePolicyTypes(pfDao, null))
300             .hasMessageMatching("^serviceTemplate is marked .*on.*ull but is null$");
301
302         ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class);
303
304         assertNotNull(toscaServiceTemplate);
305         ToscaServiceTemplate createdServiceTemplate =
306                 new AuthorativeToscaProvider().createPolicyTypes(pfDao, toscaServiceTemplate);
307
308         PfConceptKey policyTypeKey = new PfConceptKey(POLICY_NO_VERSION_VERSION1);
309
310         ToscaPolicyType beforePolicyType = toscaServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
311         ToscaPolicyType createdPolicyType = createdServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
312         assertEquals(beforePolicyType.getName(), createdPolicyType.getName());
313         assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), createdPolicyType.getDescription()));
314
315         ToscaServiceTemplate updatedServiceTemplate =
316                 new AuthorativeToscaProvider().updatePolicyTypes(pfDao, toscaServiceTemplate);
317
318         ToscaPolicyType updatedPolicy = updatedServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
319         assertEquals(beforePolicyType.getName(), updatedPolicy.getName());
320         assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), updatedPolicy.getDescription()));
321     }
322
323     @Test
324     void testPolicyTypesDelete() throws Exception {
325         assertThatThrownBy(() -> new AuthorativeToscaProvider().deletePolicyType(null, null, null))
326             .hasMessageMatching(DAO_IS_NULL);
327
328         assertThatThrownBy(() -> new AuthorativeToscaProvider().deletePolicyType(null, null, VERSION))
329             .hasMessageMatching(DAO_IS_NULL);
330
331         assertThatThrownBy(() -> new AuthorativeToscaProvider().deletePolicyType(null, "name", null))
332             .hasMessageMatching(DAO_IS_NULL);
333
334         assertThatThrownBy(() -> new AuthorativeToscaProvider().deletePolicyType(null, "name", VERSION))
335             .hasMessageMatching(DAO_IS_NULL);
336
337         assertThatThrownBy(() -> new AuthorativeToscaProvider().deletePolicyType(pfDao, null, null))
338             .hasMessageMatching("^name is marked .*on.*ull but is null$");
339
340         assertThatThrownBy(() -> new AuthorativeToscaProvider().deletePolicyType(pfDao, null, VERSION))
341             .hasMessageMatching("^name is marked .*on.*ull but is null$");
342
343         assertThatThrownBy(() -> new AuthorativeToscaProvider().deletePolicyType(pfDao, "name", null))
344             .hasMessageMatching("^version is marked .*on.*ull but is null$");
345
346         ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class);
347
348         assertNotNull(toscaServiceTemplate);
349         ToscaServiceTemplate createdServiceTemplate =
350                 new AuthorativeToscaProvider().createPolicyTypes(pfDao, toscaServiceTemplate);
351
352         PfConceptKey policyTypeKey = new PfConceptKey(POLICY_NO_VERSION_VERSION1);
353
354         ToscaPolicyType beforePolicyType = toscaServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
355         ToscaPolicyType createdPolicyType = createdServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
356         assertEquals(beforePolicyType.getName(), createdPolicyType.getName());
357         assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), createdPolicyType.getDescription()));
358
359         ToscaServiceTemplate deletedServiceTemplate = new AuthorativeToscaProvider().deletePolicyType(pfDao,
360                 policyTypeKey.getName(), policyTypeKey.getVersion());
361
362         ToscaPolicyType deletedPolicy = deletedServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
363         assertEquals(beforePolicyType.getName(), deletedPolicy.getName());
364         assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), deletedPolicy.getDescription()));
365
366         assertThatThrownBy(() -> new AuthorativeToscaProvider()
367             .getPolicyTypes(pfDao, policyTypeKey.getName(), policyTypeKey.getVersion()))
368             .hasMessage("policy types for onap.policies.NoVersion:0.0.1 do not exist");
369     }
370
371     @Test
372     void testAssertPoliciesExist() {
373         ToscaServiceTemplate testServiceTemplate = new ToscaServiceTemplate();
374
375         assertThatThrownBy(() -> new AuthorativeToscaProvider().deletePolicyType(pfDao, "name", null))
376             .hasMessageMatching("^version is marked .*on.*ull but is null$");
377
378         assertThatThrownBy(() -> new AuthorativeToscaProvider().createPolicyTypes(pfDao, testServiceTemplate))
379             .hasMessage(MISSING_POLICY_TYPES);
380
381         testServiceTemplate.setToscaTopologyTemplate(new ToscaTopologyTemplate());
382         assertThatThrownBy(() -> new AuthorativeToscaProvider().createPolicyTypes(pfDao, testServiceTemplate))
383             .hasMessage(MISSING_POLICY_TYPES);
384
385         testServiceTemplate.setPolicyTypes(new LinkedHashMap<>());
386         assertThatThrownBy(() -> new AuthorativeToscaProvider().createPolicyTypes(pfDao, testServiceTemplate))
387             .hasMessage("An incoming list of concepts must have at least one entry");
388     }
389
390     @Test
391     void testNullParameters() {
392         assertThatThrownBy(() -> new AuthorativeToscaProvider().getPolicyTypeList(null, null, null))
393                 .hasMessageMatching("^dao is marked .*on.*ull but is null$");
394     }
395 }