2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019-2021,2023 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.Assert.assertEquals;
26 import static org.junit.Assert.assertNotNull;
27 import static org.junit.Assert.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.After;
35 import org.junit.Before;
36 import org.junit.BeforeClass;
37 import org.junit.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 public 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.
68 * @throws Exception on errors
71 public static void readPolicyDefinition() {
72 String yamlString = ResourceUtils.getResourceAsString("src/test/resources/onap.policies.NoVersion.yaml");
74 Object yamlObject = new Yaml().load(yamlString);
75 yamlAsJsonString = new GsonBuilder().setPrettyPrinting().create().toJson(yamlObject);
79 * Set up the DAO towards the database.
81 * @throws Exception on database errors
84 public void setupDao() throws Exception {
85 final DaoParameters daoParameters = new DaoParameters();
86 daoParameters.setPluginClass(DefaultPfDao.class.getName());
88 daoParameters.setPersistenceUnit("ToscaConceptTest");
90 Properties jdbcProperties = new Properties();
91 jdbcProperties.setProperty("javax.persistence.jdbc.user", "policy");
92 jdbcProperties.setProperty("javax.persistence.jdbc.password", "P01icY");
93 if (System.getProperty("USE-MARIADB") != null) {
94 jdbcProperties.setProperty("javax.persistence.jdbc.driver", "org.mariadb.jdbc.Driver");
95 jdbcProperties.setProperty("javax.persistence.jdbc.url", "jdbc:mariadb://localhost:3306/policy");
97 jdbcProperties.setProperty("javax.persistence.jdbc.driver", "org.h2.Driver");
98 jdbcProperties.setProperty("javax.persistence.jdbc.url",
99 "jdbc:h2:mem:AuthorativeToscaProviderPolicyTypeTest");
101 daoParameters.setJdbcProperties(jdbcProperties);
103 pfDao = new PfDaoFactory().createPfDao(daoParameters);
104 pfDao.init(daoParameters);
111 public void setupGson() {
112 standardCoder = new StandardCoder();
116 public void teardown() {
121 public void testPolicyTypesGet() throws Exception {
122 assertThatThrownBy(() -> {
123 new AuthorativeToscaProvider().getPolicyTypes(null, null, null);
124 }).hasMessageMatching(DAO_IS_NULL);
126 assertThatThrownBy(() -> {
127 new AuthorativeToscaProvider().getPolicyList(null, null, null);
128 }).hasMessageMatching(DAO_IS_NULL);
130 ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class);
132 assertNotNull(toscaServiceTemplate);
133 ToscaServiceTemplate createdServiceTemplate =
134 new AuthorativeToscaProvider().createPolicyTypes(pfDao, toscaServiceTemplate);
136 PfConceptKey policyTypeKey = new PfConceptKey(POLICY_NO_VERSION_VERSION1);
138 ToscaPolicyType beforePolicyType = toscaServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
139 ToscaPolicyType createdPolicyType = createdServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
140 assertEquals(true, beforePolicyType.getName().equals(createdPolicyType.getName()));
141 assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), createdPolicyType.getDescription()));
143 ToscaServiceTemplate gotServiceTemplate = new AuthorativeToscaProvider().getPolicyTypes(pfDao,
144 policyTypeKey.getName(), policyTypeKey.getVersion());
146 ToscaPolicyType gotPolicyType = gotServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
147 assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
148 assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), createdPolicyType.getDescription()));
150 List<ToscaPolicyType> gotPolicyTypeList =
151 new AuthorativeToscaProvider().getPolicyTypeList(pfDao, POLICY_NO_VERSION, VERSION_001);
152 assertEquals(2, gotPolicyTypeList.size());
153 assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
155 gotPolicyTypeList = new AuthorativeToscaProvider().getPolicyTypeList(pfDao, POLICY_NO_VERSION, null);
156 assertEquals(2, gotPolicyTypeList.size());
157 assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
159 gotPolicyTypeList = new AuthorativeToscaProvider().getPolicyTypeList(pfDao, null, null);
160 assertEquals(2, gotPolicyTypeList.size());
161 assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
163 gotPolicyTypeList = new AuthorativeToscaProvider().getPolicyTypeList(pfDao, null, VERSION_001);
164 assertEquals(2, gotPolicyTypeList.size());
165 assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
167 assertThatThrownBy(() -> new AuthorativeToscaProvider().getPolicyTypeList(new DefaultPfDao(), POLICY_NO_VERSION,
168 VERSION_001)).hasMessageContaining("Policy Framework DAO has not been initialized");
170 assertTrue(new AuthorativeToscaProvider().getPolicyTypeList(pfDao, "i.dont.Exist", VERSION_001).isEmpty());
174 public void testPolicyTypesGetFiltered() throws Exception {
175 assertThatThrownBy(() -> {
176 new AuthorativeToscaProvider().getFilteredPolicyTypes(null, null);
177 }).hasMessageMatching(DAO_IS_NULL);
179 assertThatThrownBy(() -> {
180 new AuthorativeToscaProvider().getFilteredPolicyTypes(null,
181 ToscaEntityFilter.<ToscaPolicyType>builder().build());
182 }).hasMessageMatching(DAO_IS_NULL);
184 assertThatThrownBy(() -> {
185 new AuthorativeToscaProvider().getFilteredPolicyTypes(pfDao, null);
186 }).hasMessageMatching("^filter is marked .*on.*ull but is null$");
188 assertThatThrownBy(() -> {
189 new AuthorativeToscaProvider().getFilteredPolicyTypeList(null, null);
190 }).hasMessageMatching(DAO_IS_NULL);
192 assertThatThrownBy(() -> {
193 new AuthorativeToscaProvider().getFilteredPolicyTypeList(null,
194 ToscaEntityFilter.<ToscaPolicyType>builder().build());
195 }).hasMessageMatching(DAO_IS_NULL);
197 assertThatThrownBy(() -> {
198 new AuthorativeToscaProvider().getFilteredPolicyTypeList(pfDao, null);
199 }).hasMessageMatching("^filter is marked .*on.*ull but is null$");
201 assertThatThrownBy(() -> new AuthorativeToscaProvider().getFilteredPolicyTypeList(new DefaultPfDao(),
202 ToscaEntityFilter.<ToscaPolicyType>builder().name("i.dont.Exist").build()))
203 .hasMessageContaining("Policy Framework DAO has not been initialized");
205 assertTrue(new AuthorativeToscaProvider().getFilteredPolicyTypeList(pfDao,
206 ToscaEntityFilter.<ToscaPolicyType>builder().name("i.dont.Exist").build()).isEmpty());
208 ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class);
210 assertNotNull(toscaServiceTemplate);
211 ToscaServiceTemplate createdServiceTemplate =
212 new AuthorativeToscaProvider().createPolicyTypes(pfDao, toscaServiceTemplate);
214 PfConceptKey policyTypeKey = new PfConceptKey(POLICY_NO_VERSION_VERSION1);
216 ToscaPolicyType beforePolicyType = toscaServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
217 ToscaPolicyType createdPolicyType = createdServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
218 assertEquals(true, beforePolicyType.getName().equals(createdPolicyType.getName()));
219 assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), createdPolicyType.getDescription()));
221 ToscaServiceTemplate gotServiceTemplate = new AuthorativeToscaProvider().getFilteredPolicyTypes(pfDao,
222 ToscaEntityFilter.<ToscaPolicyType>builder().build());
224 ToscaPolicyType gotPolicyType = gotServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
225 checkEqualsNameDescription(beforePolicyType, gotPolicyType);
227 gotServiceTemplate = new AuthorativeToscaProvider().getFilteredPolicyTypes(pfDao,
228 ToscaEntityFilter.<ToscaPolicyType>builder().name(policyTypeKey.getName()).build());
230 gotPolicyType = gotServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
231 checkEqualsNameDescription(beforePolicyType, gotPolicyType);
233 gotServiceTemplate = new AuthorativeToscaProvider().getFilteredPolicyTypes(pfDao, ToscaEntityFilter
234 .<ToscaPolicyType>builder().name(policyTypeKey.getName()).version(VERSION_001).build());
236 gotPolicyType = gotServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
237 checkEqualsNameDescription(beforePolicyType, gotPolicyType);
239 List<ToscaPolicyType> gotPolicyTypeList =
240 new AuthorativeToscaProvider().getPolicyTypeList(pfDao, POLICY_NO_VERSION, VERSION_001);
241 assertEquals(2, gotPolicyTypeList.size());
242 assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
244 gotPolicyTypeList = new AuthorativeToscaProvider().getFilteredPolicyTypeList(pfDao,
245 ToscaEntityFilter.<ToscaPolicyType>builder().build());
246 assertEquals(2, gotPolicyTypeList.size());
247 assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
249 gotPolicyTypeList = new AuthorativeToscaProvider().getFilteredPolicyTypeList(pfDao,
250 ToscaEntityFilter.<ToscaPolicyType>builder().name(policyTypeKey.getName()).build());
251 assertEquals(1, gotPolicyTypeList.size());
252 assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
254 gotPolicyTypeList = new AuthorativeToscaProvider().getFilteredPolicyTypeList(pfDao, ToscaEntityFilter
255 .<ToscaPolicyType>builder().name(policyTypeKey.getName()).version(VERSION_001).build());
256 assertEquals(1, gotPolicyTypeList.size());
257 assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
259 gotPolicyTypeList = new AuthorativeToscaProvider().getFilteredPolicyTypeList(pfDao,
260 ToscaEntityFilter.<ToscaPolicyType>builder().version("1.0.0").build());
261 assertEquals(1, gotPolicyTypeList.size());
262 assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
265 private void checkEqualsNameDescription(ToscaPolicyType beforePolicyType, ToscaPolicyType gotPolicyType) {
266 assertEquals(beforePolicyType.getName(), gotPolicyType.getName());
267 assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), gotPolicyType.getDescription()));
271 public void testPolicyTypesCreate() throws Exception {
272 assertThatThrownBy(() -> {
273 new AuthorativeToscaProvider().createPolicyTypes(null, null);
274 }).hasMessageMatching(DAO_IS_NULL);
276 assertThatThrownBy(() -> {
277 new AuthorativeToscaProvider().createPolicyTypes(null, new ToscaServiceTemplate());
278 }).hasMessageMatching(DAO_IS_NULL);
280 assertThatThrownBy(() -> {
281 new AuthorativeToscaProvider().createPolicyTypes(pfDao, null);
282 }).hasMessageMatching("^serviceTemplate is marked .*on.*ull but is null$");
284 ToscaServiceTemplate testToscaServiceTemplate = new ToscaServiceTemplate();
285 assertThatThrownBy(() -> {
286 new AuthorativeToscaProvider().createPolicyTypes(pfDao, testToscaServiceTemplate);
287 }).hasMessage(MISSING_POLICY_TYPES);
289 testToscaServiceTemplate.setPolicyTypes(new LinkedHashMap<>());
290 assertThatThrownBy(() -> {
291 new AuthorativeToscaProvider().createPolicyTypes(pfDao, testToscaServiceTemplate);
292 }).hasMessage("An incoming list of concepts must have at least one entry");
294 ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class);
296 assertNotNull(toscaServiceTemplate);
297 ToscaServiceTemplate createdServiceTemplate =
298 new AuthorativeToscaProvider().createPolicyTypes(pfDao, toscaServiceTemplate);
300 PfConceptKey policyTypeKey = new PfConceptKey(POLICY_NO_VERSION_VERSION1);
302 ToscaPolicyType beforePolicyType = toscaServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
303 ToscaPolicyType createdPolicyType = createdServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
304 assertEquals(true, beforePolicyType.getName().equals(createdPolicyType.getName()));
305 assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), createdPolicyType.getDescription()));
309 public void testPolicyTypesUpdate() throws Exception {
310 assertThatThrownBy(() -> {
311 new AuthorativeToscaProvider().createPolicyTypes(null, null);
312 }).hasMessageMatching(DAO_IS_NULL);
314 assertThatThrownBy(() -> {
315 new AuthorativeToscaProvider().updatePolicyTypes(null, null);
316 }).hasMessageMatching(DAO_IS_NULL);
318 assertThatThrownBy(() -> {
319 new AuthorativeToscaProvider().updatePolicyTypes(null, new ToscaServiceTemplate());
320 }).hasMessageMatching(DAO_IS_NULL);
322 assertThatThrownBy(() -> {
323 new AuthorativeToscaProvider().updatePolicyTypes(pfDao, null);
324 }).hasMessageMatching("^serviceTemplate is marked .*on.*ull but is null$");
326 ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class);
328 assertNotNull(toscaServiceTemplate);
329 ToscaServiceTemplate createdServiceTemplate =
330 new AuthorativeToscaProvider().createPolicyTypes(pfDao, toscaServiceTemplate);
332 PfConceptKey policyTypeKey = new PfConceptKey(POLICY_NO_VERSION_VERSION1);
334 ToscaPolicyType beforePolicyType = toscaServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
335 ToscaPolicyType createdPolicyType = createdServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
336 assertEquals(true, beforePolicyType.getName().equals(createdPolicyType.getName()));
337 assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), createdPolicyType.getDescription()));
339 ToscaServiceTemplate updatedServiceTemplate =
340 new AuthorativeToscaProvider().updatePolicyTypes(pfDao, toscaServiceTemplate);
342 ToscaPolicyType updatedPolicy = updatedServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
343 assertEquals(true, beforePolicyType.getName().equals(updatedPolicy.getName()));
344 assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), updatedPolicy.getDescription()));
348 public void testPolicyTypesDelete() throws Exception {
349 assertThatThrownBy(() -> {
350 new AuthorativeToscaProvider().deletePolicyType(null, null, null);
351 }).hasMessageMatching(DAO_IS_NULL);
353 assertThatThrownBy(() -> {
354 new AuthorativeToscaProvider().deletePolicyType(null, null, VERSION);
355 }).hasMessageMatching(DAO_IS_NULL);
357 assertThatThrownBy(() -> {
358 new AuthorativeToscaProvider().deletePolicyType(null, "name", null);
359 }).hasMessageMatching(DAO_IS_NULL);
361 assertThatThrownBy(() -> {
362 new AuthorativeToscaProvider().deletePolicyType(null, "name", VERSION);
363 }).hasMessageMatching(DAO_IS_NULL);
365 assertThatThrownBy(() -> {
366 new AuthorativeToscaProvider().deletePolicyType(pfDao, null, null);
367 }).hasMessageMatching("^name is marked .*on.*ull but is null$");
369 assertThatThrownBy(() -> {
370 new AuthorativeToscaProvider().deletePolicyType(pfDao, null, VERSION);
371 }).hasMessageMatching("^name is marked .*on.*ull but is null$");
373 assertThatThrownBy(() -> {
374 new AuthorativeToscaProvider().deletePolicyType(pfDao, "name", null);
375 }).hasMessageMatching("^version is marked .*on.*ull but is null$");
377 ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class);
379 assertNotNull(toscaServiceTemplate);
380 ToscaServiceTemplate createdServiceTemplate =
381 new AuthorativeToscaProvider().createPolicyTypes(pfDao, toscaServiceTemplate);
383 PfConceptKey policyTypeKey = new PfConceptKey(POLICY_NO_VERSION_VERSION1);
385 ToscaPolicyType beforePolicyType = toscaServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
386 ToscaPolicyType createdPolicyType = createdServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
387 assertEquals(true, beforePolicyType.getName().equals(createdPolicyType.getName()));
388 assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), createdPolicyType.getDescription()));
390 ToscaServiceTemplate deletedServiceTemplate = new AuthorativeToscaProvider().deletePolicyType(pfDao,
391 policyTypeKey.getName(), policyTypeKey.getVersion());
393 ToscaPolicyType deletedPolicy = deletedServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
394 assertEquals(true, beforePolicyType.getName().equals(deletedPolicy.getName()));
395 assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), deletedPolicy.getDescription()));
397 assertThatThrownBy(() -> {
398 new AuthorativeToscaProvider().getPolicyTypes(pfDao, policyTypeKey.getName(), policyTypeKey.getVersion());
399 }).hasMessage("policy types for onap.policies.NoVersion:0.0.1 do not exist");
403 public void testAssertPoliciesExist() {
404 ToscaServiceTemplate testServiceTemplate = new ToscaServiceTemplate();
406 assertThatThrownBy(() -> {
407 new AuthorativeToscaProvider().deletePolicyType(pfDao, "name", null);
408 }).hasMessageMatching("^version is marked .*on.*ull but is null$");
410 assertThatThrownBy(() -> {
411 new AuthorativeToscaProvider().createPolicyTypes(pfDao, testServiceTemplate);
412 }).hasMessage(MISSING_POLICY_TYPES);
414 testServiceTemplate.setToscaTopologyTemplate(new ToscaTopologyTemplate());
415 assertThatThrownBy(() -> {
416 new AuthorativeToscaProvider().createPolicyTypes(pfDao, testServiceTemplate);
417 }).hasMessage(MISSING_POLICY_TYPES);
419 testServiceTemplate.setPolicyTypes(new LinkedHashMap<>());
420 assertThatThrownBy(() -> {
421 new AuthorativeToscaProvider().createPolicyTypes(pfDao, testServiceTemplate);
422 }).hasMessage("An incoming list of concepts must have at least one entry");
426 public void testNullParameters() throws Exception {
427 assertThatThrownBy(() -> new AuthorativeToscaProvider().getPolicyTypeList(null, null, null))
428 .hasMessageMatching("^dao is marked .*on.*ull but is null$");