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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.models.tosca.authorative.provider;
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;
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;
52 * Test of the {@link AuthorativeToscaProvider} class.
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;
63 private StandardCoder standardCoder;
66 * Read the policy type definition.
70 public static void readPolicyDefinition() {
71 String yamlString = ResourceUtils.getResourceAsString("src/test/resources/onap.policies.NoVersion.yaml");
73 Object yamlObject = new Yaml().load(yamlString);
74 yamlAsJsonString = new GsonBuilder().setPrettyPrinting().create().toJson(yamlObject);
78 * Set up the DAO towards the database.
80 * @throws Exception on database errors
83 void setupDao() throws Exception {
84 final DaoParameters daoParameters = new DaoParameters();
85 daoParameters.setPluginClass(DefaultPfDao.class.getName());
87 daoParameters.setPersistenceUnit("ToscaConceptTest");
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);
97 pfDao = new PfDaoFactory().createPfDao(daoParameters);
98 pfDao.init(daoParameters);
106 standardCoder = new StandardCoder();
115 void testPolicyTypesGet() throws Exception {
116 assertThatThrownBy(() -> new AuthorativeToscaProvider().getPolicyTypes(null, null, null))
117 .hasMessageMatching(DAO_IS_NULL);
119 assertThatThrownBy(() -> new AuthorativeToscaProvider().getPolicyList(null, null, null))
120 .hasMessageMatching(DAO_IS_NULL);
122 ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class);
124 assertNotNull(toscaServiceTemplate);
125 ToscaServiceTemplate createdServiceTemplate =
126 new AuthorativeToscaProvider().createPolicyTypes(pfDao, toscaServiceTemplate);
128 PfConceptKey policyTypeKey = new PfConceptKey(POLICY_NO_VERSION_VERSION1);
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()));
135 ToscaServiceTemplate gotServiceTemplate = new AuthorativeToscaProvider().getPolicyTypes(pfDao,
136 policyTypeKey.getName(), policyTypeKey.getVersion());
138 ToscaPolicyType gotPolicyType = gotServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
139 assertEquals(beforePolicyType.getName(), gotPolicyType.getName());
140 assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), createdPolicyType.getDescription()));
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());
147 gotPolicyTypeList = new AuthorativeToscaProvider().getPolicyTypeList(pfDao, POLICY_NO_VERSION, null);
148 assertEquals(2, gotPolicyTypeList.size());
149 assertEquals(beforePolicyType.getName(), gotPolicyType.getName());
151 gotPolicyTypeList = new AuthorativeToscaProvider().getPolicyTypeList(pfDao, null, null);
152 assertEquals(2, gotPolicyTypeList.size());
153 assertEquals(beforePolicyType.getName(), gotPolicyType.getName());
155 gotPolicyTypeList = new AuthorativeToscaProvider().getPolicyTypeList(pfDao, null, VERSION_001);
156 assertEquals(2, gotPolicyTypeList.size());
157 assertEquals(beforePolicyType.getName(), gotPolicyType.getName());
159 assertThatThrownBy(() -> new AuthorativeToscaProvider().getPolicyTypeList(new DefaultPfDao(), POLICY_NO_VERSION,
160 VERSION_001)).hasMessageContaining("Policy Framework DAO has not been initialized");
162 assertTrue(new AuthorativeToscaProvider().getPolicyTypeList(pfDao, "i.dont.Exist", VERSION_001).isEmpty());
166 void testPolicyTypesGetFiltered() throws Exception {
167 assertThatThrownBy(() -> new AuthorativeToscaProvider().getFilteredPolicyTypes(null, null))
168 .hasMessageMatching(DAO_IS_NULL);
170 assertThatThrownBy(() -> new AuthorativeToscaProvider().getFilteredPolicyTypes(null,
171 ToscaEntityFilter.<ToscaPolicyType>builder().build())).hasMessageMatching(DAO_IS_NULL);
173 assertThatThrownBy(() -> new AuthorativeToscaProvider().getFilteredPolicyTypes(pfDao, null))
174 .hasMessageMatching("^filter is marked .*on.*ull but is null$");
176 assertThatThrownBy(() -> new AuthorativeToscaProvider().getFilteredPolicyTypeList(null, null))
177 .hasMessageMatching(DAO_IS_NULL);
179 assertThatThrownBy(() -> new AuthorativeToscaProvider().getFilteredPolicyTypeList(null,
180 ToscaEntityFilter.<ToscaPolicyType>builder().build()))
181 .hasMessageMatching(DAO_IS_NULL);
183 assertThatThrownBy(() -> new AuthorativeToscaProvider().getFilteredPolicyTypeList(pfDao, null))
184 .hasMessageMatching("^filter is marked .*on.*ull but is null$");
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");
190 assertTrue(new AuthorativeToscaProvider().getFilteredPolicyTypeList(pfDao,
191 ToscaEntityFilter.<ToscaPolicyType>builder().name("i.dont.Exist").build()).isEmpty());
193 ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class);
195 assertNotNull(toscaServiceTemplate);
196 ToscaServiceTemplate createdServiceTemplate =
197 new AuthorativeToscaProvider().createPolicyTypes(pfDao, toscaServiceTemplate);
199 PfConceptKey policyTypeKey = new PfConceptKey(POLICY_NO_VERSION_VERSION1);
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()));
206 ToscaServiceTemplate gotServiceTemplate = new AuthorativeToscaProvider().getFilteredPolicyTypes(pfDao,
207 ToscaEntityFilter.<ToscaPolicyType>builder().build());
209 ToscaPolicyType gotPolicyType = gotServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
210 checkEqualsNameDescription(beforePolicyType, gotPolicyType);
212 gotServiceTemplate = new AuthorativeToscaProvider().getFilteredPolicyTypes(pfDao,
213 ToscaEntityFilter.<ToscaPolicyType>builder().name(policyTypeKey.getName()).build());
215 gotPolicyType = gotServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
216 checkEqualsNameDescription(beforePolicyType, gotPolicyType);
218 gotServiceTemplate = new AuthorativeToscaProvider().getFilteredPolicyTypes(pfDao, ToscaEntityFilter
219 .<ToscaPolicyType>builder().name(policyTypeKey.getName()).version(VERSION_001).build());
221 gotPolicyType = gotServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
222 checkEqualsNameDescription(beforePolicyType, gotPolicyType);
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());
229 gotPolicyTypeList = new AuthorativeToscaProvider().getFilteredPolicyTypeList(pfDao,
230 ToscaEntityFilter.<ToscaPolicyType>builder().build());
231 assertEquals(2, gotPolicyTypeList.size());
232 assertEquals(beforePolicyType.getName(), gotPolicyType.getName());
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());
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());
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());
250 private void checkEqualsNameDescription(ToscaPolicyType beforePolicyType, ToscaPolicyType gotPolicyType) {
251 assertEquals(beforePolicyType.getName(), gotPolicyType.getName());
252 assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), gotPolicyType.getDescription()));
256 void testPolicyTypesCreate() throws Exception {
257 assertThatThrownBy(() -> new AuthorativeToscaProvider().createPolicyTypes(null, null))
258 .hasMessageMatching(DAO_IS_NULL);
260 assertThatThrownBy(() -> new AuthorativeToscaProvider().createPolicyTypes(null, new ToscaServiceTemplate()))
261 .hasMessageMatching(DAO_IS_NULL);
263 assertThatThrownBy(() -> new AuthorativeToscaProvider().createPolicyTypes(pfDao, null))
264 .hasMessageMatching("^serviceTemplate is marked .*on.*ull but is null$");
266 ToscaServiceTemplate testToscaServiceTemplate = new ToscaServiceTemplate();
267 assertThatThrownBy(() -> new AuthorativeToscaProvider().createPolicyTypes(pfDao, testToscaServiceTemplate))
268 .hasMessage(MISSING_POLICY_TYPES);
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");
274 ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class);
276 assertNotNull(toscaServiceTemplate);
277 ToscaServiceTemplate createdServiceTemplate =
278 new AuthorativeToscaProvider().createPolicyTypes(pfDao, toscaServiceTemplate);
280 PfConceptKey policyTypeKey = new PfConceptKey(POLICY_NO_VERSION_VERSION1);
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()));
289 void testPolicyTypesUpdate() throws Exception {
290 assertThatThrownBy(() -> new AuthorativeToscaProvider().createPolicyTypes(null, null))
291 .hasMessageMatching(DAO_IS_NULL);
293 assertThatThrownBy(() -> new AuthorativeToscaProvider().updatePolicyTypes(null, null))
294 .hasMessageMatching(DAO_IS_NULL);
296 assertThatThrownBy(() -> new AuthorativeToscaProvider().updatePolicyTypes(null, new ToscaServiceTemplate()))
297 .hasMessageMatching(DAO_IS_NULL);
299 assertThatThrownBy(() -> new AuthorativeToscaProvider().updatePolicyTypes(pfDao, null))
300 .hasMessageMatching("^serviceTemplate is marked .*on.*ull but is null$");
302 ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class);
304 assertNotNull(toscaServiceTemplate);
305 ToscaServiceTemplate createdServiceTemplate =
306 new AuthorativeToscaProvider().createPolicyTypes(pfDao, toscaServiceTemplate);
308 PfConceptKey policyTypeKey = new PfConceptKey(POLICY_NO_VERSION_VERSION1);
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()));
315 ToscaServiceTemplate updatedServiceTemplate =
316 new AuthorativeToscaProvider().updatePolicyTypes(pfDao, toscaServiceTemplate);
318 ToscaPolicyType updatedPolicy = updatedServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
319 assertEquals(beforePolicyType.getName(), updatedPolicy.getName());
320 assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), updatedPolicy.getDescription()));
324 void testPolicyTypesDelete() throws Exception {
325 assertThatThrownBy(() -> new AuthorativeToscaProvider().deletePolicyType(null, null, null))
326 .hasMessageMatching(DAO_IS_NULL);
328 assertThatThrownBy(() -> new AuthorativeToscaProvider().deletePolicyType(null, null, VERSION))
329 .hasMessageMatching(DAO_IS_NULL);
331 assertThatThrownBy(() -> new AuthorativeToscaProvider().deletePolicyType(null, "name", null))
332 .hasMessageMatching(DAO_IS_NULL);
334 assertThatThrownBy(() -> new AuthorativeToscaProvider().deletePolicyType(null, "name", VERSION))
335 .hasMessageMatching(DAO_IS_NULL);
337 assertThatThrownBy(() -> new AuthorativeToscaProvider().deletePolicyType(pfDao, null, null))
338 .hasMessageMatching("^name is marked .*on.*ull but is null$");
340 assertThatThrownBy(() -> new AuthorativeToscaProvider().deletePolicyType(pfDao, null, VERSION))
341 .hasMessageMatching("^name is marked .*on.*ull but is null$");
343 assertThatThrownBy(() -> new AuthorativeToscaProvider().deletePolicyType(pfDao, "name", null))
344 .hasMessageMatching("^version is marked .*on.*ull but is null$");
346 ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class);
348 assertNotNull(toscaServiceTemplate);
349 ToscaServiceTemplate createdServiceTemplate =
350 new AuthorativeToscaProvider().createPolicyTypes(pfDao, toscaServiceTemplate);
352 PfConceptKey policyTypeKey = new PfConceptKey(POLICY_NO_VERSION_VERSION1);
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()));
359 ToscaServiceTemplate deletedServiceTemplate = new AuthorativeToscaProvider().deletePolicyType(pfDao,
360 policyTypeKey.getName(), policyTypeKey.getVersion());
362 ToscaPolicyType deletedPolicy = deletedServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
363 assertEquals(beforePolicyType.getName(), deletedPolicy.getName());
364 assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), deletedPolicy.getDescription()));
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");
372 void testAssertPoliciesExist() {
373 ToscaServiceTemplate testServiceTemplate = new ToscaServiceTemplate();
375 assertThatThrownBy(() -> new AuthorativeToscaProvider().deletePolicyType(pfDao, "name", null))
376 .hasMessageMatching("^version is marked .*on.*ull but is null$");
378 assertThatThrownBy(() -> new AuthorativeToscaProvider().createPolicyTypes(pfDao, testServiceTemplate))
379 .hasMessage(MISSING_POLICY_TYPES);
381 testServiceTemplate.setToscaTopologyTemplate(new ToscaTopologyTemplate());
382 assertThatThrownBy(() -> new AuthorativeToscaProvider().createPolicyTypes(pfDao, testServiceTemplate))
383 .hasMessage(MISSING_POLICY_TYPES);
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");
391 void testNullParameters() {
392 assertThatThrownBy(() -> new AuthorativeToscaProvider().getPolicyTypeList(null, null, null))
393 .hasMessageMatching("^dao is marked .*on.*ull but is null$");