2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019-2020 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
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 java.util.ArrayList;
30 import java.util.List;
32 import java.util.Properties;
34 import org.eclipse.persistence.config.PersistenceUnitProperties;
35 import org.junit.After;
36 import org.junit.Before;
37 import org.junit.Test;
38 import org.onap.policy.common.utils.coder.CoderException;
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.ToscaPolicy;
48 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyFilter;
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;
54 * Test of the {@link AuthorativeToscaProvider} class.
56 * @author Liam Fallon (liam.fallon@est.tech)
58 public class AuthorativeToscaProviderPolicyTest {
59 private static final String VERSION = "version";
60 private static final String VCPE_JSON = "policies/vCPE.policy.monitoring.input.tosca.json";
61 private static final String POLICY_AND_VERSION = "onap.restart.tca:1.0.0";
62 private static final String POLICY1 = "onap.restart.tca";
63 private static final String DAO_IS_NULL = "^dao is marked .*on.*ull but is null$";
64 private static final String VERSION_100 = "1.0.0";
66 private StandardCoder standardCoder;
69 * Set up the DAO towards the database.
71 * @throws Exception on database errors
74 public void setupDao() throws Exception {
75 final DaoParameters daoParameters = new DaoParameters();
76 daoParameters.setPluginClass(DefaultPfDao.class.getName());
78 daoParameters.setPersistenceUnit("ToscaConceptTest");
80 Properties jdbcProperties = new Properties();
81 jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_USER, "policy");
82 jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_PASSWORD, "P01icY");
84 // H2, use "org.mariadb.jdbc.Driver" and "jdbc:mariadb://localhost:3306/policy" for locally installed MariaDB
85 jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_DRIVER, "org.h2.Driver");
86 jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_URL, "jdbc:h2:mem:testdb");
88 daoParameters.setJdbcProperties(jdbcProperties);
90 pfDao = new PfDaoFactory().createPfDao(daoParameters);
91 pfDao.init(daoParameters);
98 public void setupGson() {
99 standardCoder = new StandardCoder();
103 public void teardown() {
108 public void testPoliciesGet() throws Exception {
109 assertThatThrownBy(() -> {
110 new AuthorativeToscaProvider().getPolicies(null, null, null);
111 }).hasMessageMatching(DAO_IS_NULL);
113 assertThatThrownBy(() -> {
114 new AuthorativeToscaProvider().getPolicyList(null, null, null);
115 }).hasMessageMatching(DAO_IS_NULL);
119 ToscaServiceTemplate toscaServiceTemplate =
120 standardCoder.decode(ResourceUtils.getResourceAsString(VCPE_JSON), ToscaServiceTemplate.class);
122 assertNotNull(toscaServiceTemplate);
123 ToscaServiceTemplate createdServiceTemplate =
124 new AuthorativeToscaProvider().createPolicies(pfDao, toscaServiceTemplate);
126 PfConceptKey policyKey = new PfConceptKey(POLICY_AND_VERSION);
128 ToscaPolicy beforePolicy =
129 toscaServiceTemplate.getToscaTopologyTemplate().getPolicies().get(0).get(policyKey.getName());
130 ToscaPolicy createdPolicy =
131 createdServiceTemplate.getToscaTopologyTemplate().getPolicies().get(0).get(policyKey.getName());
132 assertEquals(0, beforePolicy.compareNameVersion(beforePolicy, createdPolicy));
133 assertTrue(beforePolicy.getType().equals(createdPolicy.getType()));
135 ToscaServiceTemplate gotServiceTemplate =
136 new AuthorativeToscaProvider().getPolicies(pfDao, policyKey.getName(), policyKey.getVersion());
138 ToscaPolicy gotPolicy =
139 gotServiceTemplate.getToscaTopologyTemplate().getPolicies().get(0).get(policyKey.getName());
140 assertEquals(0, beforePolicy.compareNameVersion(beforePolicy, gotPolicy));
141 assertTrue(beforePolicy.getType().equals(gotPolicy.getType()));
143 List<ToscaPolicy> gotPolicyList = new AuthorativeToscaProvider().getPolicyList(pfDao, POLICY1, VERSION_100);
144 assertEquals(1, gotPolicyList.size());
145 assertEquals(0, beforePolicy.compareNameVersion(beforePolicy, gotPolicyList.get(0)));
147 gotPolicyList = new AuthorativeToscaProvider().getPolicyList(pfDao, POLICY1, null);
148 assertEquals(1, gotPolicyList.size());
149 assertEquals(0, beforePolicy.compareNameVersion(beforePolicy, gotPolicyList.get(0)));
151 gotPolicyList = new AuthorativeToscaProvider().getPolicyList(pfDao, null, null);
152 assertEquals(1, gotPolicyList.size());
153 assertEquals(0, beforePolicy.compareNameVersion(beforePolicy, gotPolicyList.get(0)));
155 gotPolicyList = new AuthorativeToscaProvider().getPolicyList(pfDao, null, VERSION_100);
156 assertEquals(1, gotPolicyList.size());
157 assertEquals(0, beforePolicy.compareNameVersion(beforePolicy, gotPolicyList.get(0)));
159 assertTrue(new AuthorativeToscaProvider().getPolicyList(pfDao, "Nonexistant", VERSION_100).isEmpty());
163 public void testPoliciesGetFiltered() throws Exception {
164 assertThatThrownBy(() -> {
165 new AuthorativeToscaProvider().getFilteredPolicies(null, null);
166 }).hasMessageMatching(DAO_IS_NULL);
168 assertThatThrownBy(() -> {
169 new AuthorativeToscaProvider().getFilteredPolicies(null, ToscaPolicyFilter.builder().build());
170 }).hasMessageMatching(DAO_IS_NULL);
172 assertThatThrownBy(() -> {
173 new AuthorativeToscaProvider().getFilteredPolicies(pfDao, null);
174 }).hasMessageMatching("^filter is marked .*on.*ull but is null$");
176 assertThatThrownBy(() -> {
177 new AuthorativeToscaProvider().getFilteredPolicyList(null, null);
178 }).hasMessageMatching(DAO_IS_NULL);
180 assertThatThrownBy(() -> {
181 new AuthorativeToscaProvider().getFilteredPolicyList(null, ToscaPolicyFilter.builder().build());
182 }).hasMessageMatching(DAO_IS_NULL);
184 assertThatThrownBy(() -> {
185 new AuthorativeToscaProvider().getFilteredPolicyList(pfDao, null);
186 }).hasMessageMatching("^filter is marked .*on.*ull but is null$");
190 ToscaServiceTemplate toscaServiceTemplate =
191 standardCoder.decode(ResourceUtils.getResourceAsString(VCPE_JSON), ToscaServiceTemplate.class);
193 assertNotNull(toscaServiceTemplate);
194 ToscaServiceTemplate createdServiceTemplate =
195 new AuthorativeToscaProvider().createPolicies(pfDao, toscaServiceTemplate);
197 PfConceptKey policyKey = new PfConceptKey(POLICY_AND_VERSION);
199 ToscaPolicy beforePolicy =
200 toscaServiceTemplate.getToscaTopologyTemplate().getPolicies().get(0).get(policyKey.getName());
201 ToscaPolicy createdPolicy =
202 createdServiceTemplate.getToscaTopologyTemplate().getPolicies().get(0).get(policyKey.getName());
203 assertEquals(0, beforePolicy.compareNameVersion(beforePolicy, createdPolicy));
204 assertTrue(beforePolicy.getType().equals(createdPolicy.getType()));
206 ToscaServiceTemplate gotServiceTemplate =
207 new AuthorativeToscaProvider().getFilteredPolicies(pfDao, ToscaPolicyFilter.builder().build());
209 ToscaPolicy gotPolicy =
210 gotServiceTemplate.getToscaTopologyTemplate().getPolicies().get(0).get(policyKey.getName());
211 assertEquals(0, beforePolicy.compareNameVersion(beforePolicy, gotPolicy));
212 assertTrue(beforePolicy.getType().equals(gotPolicy.getType()));
214 gotServiceTemplate = new AuthorativeToscaProvider().getFilteredPolicies(pfDao,
215 ToscaPolicyFilter.builder().name(policyKey.getName()).build());
217 gotPolicy = gotServiceTemplate.getToscaTopologyTemplate().getPolicies().get(0).get(policyKey.getName());
218 assertEquals(0, beforePolicy.compareNameVersion(beforePolicy, gotPolicy));
219 assertTrue(beforePolicy.getType().equals(gotPolicy.getType()));
221 gotServiceTemplate = new AuthorativeToscaProvider().getFilteredPolicies(pfDao,
222 ToscaPolicyFilter.builder().name(policyKey.getName()).version(VERSION_100).build());
224 gotPolicy = gotServiceTemplate.getToscaTopologyTemplate().getPolicies().get(0).get(policyKey.getName());
225 assertEquals(0, beforePolicy.compareNameVersion(beforePolicy, gotPolicy));
226 assertTrue(beforePolicy.getType().equals(gotPolicy.getType()));
228 List<ToscaPolicy> gotPolicyList = new AuthorativeToscaProvider().getPolicyList(pfDao, POLICY1, VERSION_100);
229 assertEquals(1, gotPolicyList.size());
230 assertEquals(0, beforePolicy.compareNameVersion(beforePolicy, gotPolicyList.get(0)));
233 new AuthorativeToscaProvider().getFilteredPolicyList(pfDao, ToscaPolicyFilter.builder().build());
234 assertEquals(1, gotPolicyList.size());
235 assertEquals(0, beforePolicy.compareNameVersion(beforePolicy, gotPolicyList.get(0)));
237 gotPolicyList = new AuthorativeToscaProvider().getFilteredPolicyList(pfDao,
238 ToscaPolicyFilter.builder().name(policyKey.getName()).build());
239 assertEquals(1, gotPolicyList.size());
240 assertEquals(0, beforePolicy.compareNameVersion(beforePolicy, gotPolicyList.get(0)));
242 gotPolicyList = new AuthorativeToscaProvider().getFilteredPolicyList(pfDao,
243 ToscaPolicyFilter.builder().name(policyKey.getName()).version(VERSION_100).build());
244 assertEquals(1, gotPolicyList.size());
245 assertEquals(0, beforePolicy.compareNameVersion(beforePolicy, gotPolicyList.get(0)));
249 public void testPolicyCreate() throws Exception {
250 assertThatThrownBy(() -> {
251 new AuthorativeToscaProvider().createPolicies(null, null);
252 }).hasMessageMatching(DAO_IS_NULL);
254 assertThatThrownBy(() -> {
255 new AuthorativeToscaProvider().createPolicies(null, new ToscaServiceTemplate());
256 }).hasMessageMatching(DAO_IS_NULL);
258 assertThatThrownBy(() -> {
259 new AuthorativeToscaProvider().createPolicies(pfDao, null);
260 }).hasMessageMatching("^serviceTemplate is marked .*on.*ull but is null$");
264 ToscaServiceTemplate toscaServiceTemplate =
265 standardCoder.decode(ResourceUtils.getResourceAsString(VCPE_JSON), ToscaServiceTemplate.class);
267 assertNotNull(toscaServiceTemplate);
268 ToscaServiceTemplate createdServiceTemplate =
269 new AuthorativeToscaProvider().createPolicies(pfDao, toscaServiceTemplate);
271 PfConceptKey policyKey = new PfConceptKey(POLICY_AND_VERSION);
273 ToscaPolicy beforePolicy =
274 toscaServiceTemplate.getToscaTopologyTemplate().getPolicies().get(0).get(policyKey.getName());
275 ToscaPolicy createdPolicy =
276 createdServiceTemplate.getToscaTopologyTemplate().getPolicies().get(0).get(policyKey.getName());
277 assertEquals(0, beforePolicy.compareNameVersion(beforePolicy, createdPolicy));
278 assertTrue(beforePolicy.getType().equals(createdPolicy.getType()));
282 public void testPolicyUpdate() throws Exception {
283 assertThatThrownBy(() -> {
284 new AuthorativeToscaProvider().createPolicies(null, null);
285 }).hasMessageMatching(DAO_IS_NULL);
287 assertThatThrownBy(() -> {
288 new AuthorativeToscaProvider().updatePolicies(null, null);
289 }).hasMessageMatching(DAO_IS_NULL);
291 assertThatThrownBy(() -> {
292 new AuthorativeToscaProvider().updatePolicies(null, new ToscaServiceTemplate());
293 }).hasMessageMatching(DAO_IS_NULL);
295 assertThatThrownBy(() -> {
296 new AuthorativeToscaProvider().updatePolicies(pfDao, null);
297 }).hasMessageMatching("^serviceTemplate is marked .*on.*ull but is null$");
301 ToscaServiceTemplate toscaServiceTemplate =
302 standardCoder.decode(ResourceUtils.getResourceAsString(VCPE_JSON), ToscaServiceTemplate.class);
304 assertNotNull(toscaServiceTemplate);
305 ToscaServiceTemplate createdServiceTemplate =
306 new AuthorativeToscaProvider().createPolicies(pfDao, toscaServiceTemplate);
308 PfConceptKey policyKey = new PfConceptKey(POLICY_AND_VERSION);
310 ToscaPolicy beforePolicy =
311 toscaServiceTemplate.getToscaTopologyTemplate().getPolicies().get(0).get(policyKey.getName());
312 ToscaPolicy createdPolicy =
313 createdServiceTemplate.getToscaTopologyTemplate().getPolicies().get(0).get(policyKey.getName());
314 assertEquals(0, beforePolicy.compareNameVersion(beforePolicy, createdPolicy));
315 assertTrue(beforePolicy.getType().equals(createdPolicy.getType()));
317 ToscaServiceTemplate updatedServiceTemplate =
318 new AuthorativeToscaProvider().updatePolicies(pfDao, toscaServiceTemplate);
320 ToscaPolicy updatedPolicy =
321 updatedServiceTemplate.getToscaTopologyTemplate().getPolicies().get(0).get(policyKey.getName());
322 assertEquals(0, beforePolicy.compareNameVersion(beforePolicy, updatedPolicy));
323 assertTrue(beforePolicy.getType().equals(updatedPolicy.getType()));
327 public void testPoliciesDelete() throws Exception {
328 assertThatThrownBy(() -> {
329 new AuthorativeToscaProvider().deletePolicy(null, null, null);
330 }).hasMessageMatching(DAO_IS_NULL);
332 assertThatThrownBy(() -> {
333 new AuthorativeToscaProvider().deletePolicy(null, null, VERSION);
334 }).hasMessageMatching(DAO_IS_NULL);
336 assertThatThrownBy(() -> {
337 new AuthorativeToscaProvider().deletePolicy(null, "name", null);
338 }).hasMessageMatching(DAO_IS_NULL);
340 assertThatThrownBy(() -> {
341 new AuthorativeToscaProvider().deletePolicy(null, "name", VERSION);
342 }).hasMessageMatching(DAO_IS_NULL);
344 assertThatThrownBy(() -> {
345 new AuthorativeToscaProvider().deletePolicy(pfDao, null, null);
346 }).hasMessageMatching("^name is marked .*on.*ull but is null$");
348 assertThatThrownBy(() -> {
349 new AuthorativeToscaProvider().deletePolicy(pfDao, null, VERSION);
350 }).hasMessageMatching("^name is marked .*on.*ull but is null$");
352 assertThatThrownBy(() -> {
353 new AuthorativeToscaProvider().deletePolicy(pfDao, "name", null);
354 }).hasMessageMatching("^version is marked .*on.*ull but is null$");
358 ToscaServiceTemplate toscaServiceTemplate =
359 standardCoder.decode(ResourceUtils.getResourceAsString(VCPE_JSON), ToscaServiceTemplate.class);
361 assertNotNull(toscaServiceTemplate);
362 ToscaServiceTemplate createdServiceTemplate =
363 new AuthorativeToscaProvider().createPolicies(pfDao, toscaServiceTemplate);
365 PfConceptKey policyKey = new PfConceptKey(POLICY_AND_VERSION);
367 ToscaPolicy beforePolicy =
368 toscaServiceTemplate.getToscaTopologyTemplate().getPolicies().get(0).get(policyKey.getName());
369 ToscaPolicy createdPolicy =
370 createdServiceTemplate.getToscaTopologyTemplate().getPolicies().get(0).get(policyKey.getName());
371 assertEquals(0, beforePolicy.compareNameVersion(beforePolicy, createdPolicy));
372 assertTrue(beforePolicy.getType().equals(createdPolicy.getType()));
374 ToscaServiceTemplate deletedServiceTemplate =
375 new AuthorativeToscaProvider().deletePolicy(pfDao, policyKey.getName(), policyKey.getVersion());
377 ToscaPolicy deletedPolicy =
378 deletedServiceTemplate.getToscaTopologyTemplate().getPolicies().get(0).get(policyKey.getName());
379 assertEquals(0, beforePolicy.compareNameVersion(beforePolicy, createdPolicy));
380 assertTrue(beforePolicy.getType().equals(deletedPolicy.getType()));
384 () -> new AuthorativeToscaProvider().getPolicies(pfDao, policyKey.getName(), policyKey.getVersion()))
385 .hasMessageMatching("policies for onap.restart.tca:1.0.0 do not exist");
390 public void testAssertPoliciesExist() {
391 ToscaServiceTemplate testServiceTemplate = new ToscaServiceTemplate();
393 assertThatThrownBy(() -> {
394 new AuthorativeToscaProvider().deletePolicy(pfDao, "name", null);
395 }).hasMessageMatching("^version is marked .*on.*ull but is null$");
397 assertThatThrownBy(() -> {
398 new AuthorativeToscaProvider().createPolicies(pfDao, testServiceTemplate);
399 }).hasMessage("topology template not specified on service template");
401 testServiceTemplate.setToscaTopologyTemplate(new ToscaTopologyTemplate());
402 assertThatThrownBy(() -> {
403 new AuthorativeToscaProvider().createPolicies(pfDao, testServiceTemplate);
404 }).hasMessage("no policies specified on topology template of service template");
406 testServiceTemplate.getToscaTopologyTemplate().setPolicies(new ArrayList<>());
407 assertThatThrownBy(() -> {
408 new AuthorativeToscaProvider().createPolicies(pfDao, testServiceTemplate);
409 }).hasMessage("An incoming list of concepts must have at least one entry");
413 public void testEntityMaps() throws CoderException, PfModelException {
414 Object yamlObject = new Yaml().load(
415 ResourceUtils.getResourceAsString("policytypes/onap.policies.monitoring.cdap.tca.hi.lo.app.yaml"));
416 String yamlAsJsonString = new StandardCoder().encode(yamlObject);
418 ToscaServiceTemplate toscaServiceTemplatePolicyType =
419 standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class);
421 assertNotNull(toscaServiceTemplatePolicyType);
422 new AuthorativeToscaProvider().createPolicyTypes(pfDao, toscaServiceTemplatePolicyType);
424 assertEquals(3, toscaServiceTemplatePolicyType.getDataTypesAsMap().size());
425 assertEquals(2, toscaServiceTemplatePolicyType.getPolicyTypesAsMap().size());
427 ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode(
428 ResourceUtils.getResourceAsString("policies/vCPE.policy.monitoring.input.tosca.json"),
429 ToscaServiceTemplate.class);
431 assertNotNull(toscaServiceTemplate);
432 ToscaServiceTemplate createdServiceTemplate =
433 new AuthorativeToscaProvider().createPolicies(pfDao, toscaServiceTemplate);
435 PfConceptKey policyKey = new PfConceptKey("onap.restart.tca:1.0.0");
437 ToscaPolicy beforePolicy =
438 toscaServiceTemplate.getToscaTopologyTemplate().getPolicies().get(0).get(policyKey.getName());
439 ToscaPolicy createdPolicy =
440 createdServiceTemplate.getToscaTopologyTemplate().getPolicies().get(0).get(policyKey.getName());
441 assertEquals(0, beforePolicy.compareNameVersion(beforePolicy, createdPolicy));
442 assertTrue(beforePolicy.getType().equals(createdPolicy.getType()));
444 assertEquals(1, toscaServiceTemplate.getToscaTopologyTemplate().getPoliciesAsMap().size());
445 assertEquals(1, createdServiceTemplate.getToscaTopologyTemplate().getPoliciesAsMap().size());
447 Map<String, ToscaPolicy> policyMapItem = createdServiceTemplate.getToscaTopologyTemplate().getPolicies().get(0);
448 createdServiceTemplate.getToscaTopologyTemplate().getPolicies().add(policyMapItem);
450 assertThatThrownBy(() -> {
451 createdServiceTemplate.getToscaTopologyTemplate().getPoliciesAsMap();
452 }).hasMessageContaining("list of map of entities contains more than one entity with key");
455 private void createPolicyTypes() throws CoderException, PfModelException {
456 Object yamlObject = new Yaml().load(
457 ResourceUtils.getResourceAsString("policytypes/onap.policies.monitoring.cdap.tca.hi.lo.app.yaml"));
458 String yamlAsJsonString = new StandardCoder().encode(yamlObject);
460 ToscaServiceTemplate toscaServiceTemplatePolicyType =
461 standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class);
463 assertNotNull(toscaServiceTemplatePolicyType);
464 new AuthorativeToscaProvider().createPolicyTypes(pfDao, toscaServiceTemplatePolicyType);