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.provider.impl;
24 import static org.junit.Assert.assertEquals;
26 import java.util.LinkedHashMap;
28 import java.util.Map.Entry;
29 import java.util.Properties;
31 import org.eclipse.persistence.config.PersistenceUnitProperties;
32 import org.junit.AfterClass;
33 import org.junit.BeforeClass;
34 import org.junit.Test;
35 import org.onap.policy.common.utils.coder.CoderException;
36 import org.onap.policy.common.utils.coder.StandardCoder;
37 import org.onap.policy.common.utils.resources.ResourceUtils;
38 import org.onap.policy.models.base.PfModelException;
39 import org.onap.policy.models.dao.DaoParameters;
40 import org.onap.policy.models.dao.PfDao;
41 import org.onap.policy.models.dao.PfDaoFactory;
42 import org.onap.policy.models.dao.impl.DefaultPfDao;
43 import org.onap.policy.models.tosca.authorative.concepts.ToscaEntityKey;
44 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
45 import org.onap.policy.models.tosca.authorative.provider.AuthorativeToscaProvider;
46 import org.yaml.snakeyaml.Yaml;
49 * Test of the {@link AuthorativeToscaProvider} class.
51 * @author Liam Fallon (liam.fallon@est.tech)
53 public class AuthorativeToscaProviderReferenceTest {
54 private static PfDao pfDao;
55 private static final StandardCoder coder = new StandardCoder();
58 private static final String[] EXAMPLE_POLICY_TYPES = {
59 "onap.policies.controlloop.guard.Blacklist#1.0.0"
60 + "#policytypes/onap.policies.controlloop.guard.Blacklist.yaml",
61 "onap.policies.controlloop.guard.coordination.FirstBlocksSecond#1.0.0"
62 + "#policytypes/onap.policies.controlloop.guard.coordination.FirstBlocksSecond.yaml",
63 "onap.policies.controlloop.guard.FrequencyLimiter#1.0.0"
64 + "#policytypes/onap.policies.controlloop.guard.FrequencyLimiter.yaml",
65 "onap.policies.controlloop.guard.MinMax#1.0.0"
66 + "#policytypes/onap.policies.controlloop.guard.MinMax.yaml",
67 "onap.policies.controlloop.operational.Common#1.0.0"
68 + "#policytypes/onap.policies.controlloop.operational.Common.yaml",
69 "onap.policies.controlloop.Operational#1.0.0"
70 + "#policytypes/onap.policies.controlloop.Operational.yaml",
71 "onap.policies.drools.Controller#1.0.0"
72 + "#policytypes/onap.policies.drools.Controller.yaml",
73 "onap.policies.monitoring.cdap.tca.hi.lo.app#1.0.0"
74 + "#policytypes/onap.policies.monitoring.cdap.tca.hi.lo.app.yaml",
75 "onap.policies.monitoring.dcaegen2.collectors.datafile.datafile-app-server#1.0.0"
76 + "#policytypes/onap.policies.monitoring.dcaegen2.collectors.datafile.datafile-app-server.yaml",
77 "onap.policies.Naming#1.0.0"
78 + "#policytypes/onap.policies.Naming.yaml",
79 "onap.policies.native.Apex#1.0.0"
80 + "#policytypes/onap.policies.native.Apex.yaml",
81 "onap.policies.native.Drools#1.0.0"
82 + "#policytypes/onap.policies.native.Drools.yaml",
83 "onap.policies.native.Xacml#1.0.0"
84 + "#policytypes/onap.policies.native.Xacml.yaml",
85 "onap.policies.optimization.resource.AffinityPolicy#1.0.0"
86 + "#policytypes/onap.policies.optimization.resource.AffinityPolicy.yaml",
87 "onap.policies.optimization.resource.DistancePolicy#1.0.0"
88 + "#policytypes/onap.policies.optimization.resource.DistancePolicy.yaml",
89 "onap.policies.optimization.resource.HpaPolicy#1.0.0"
90 + "#policytypes/onap.policies.optimization.resource.HpaPolicy.yaml",
91 "onap.policies.optimization.resource.OptimizationPolicy#1.0.0"
92 + "#policytypes/onap.policies.optimization.resource.OptimizationPolicy.yaml",
93 "onap.policies.optimization.resource.PciPolicy#1.0.0"
94 + "#policytypes/onap.policies.optimization.resource.PciPolicy.yaml",
95 "onap.policies.optimization.resource.Vim_fit#1.0.0"
96 + "#policytypes/onap.policies.optimization.resource.Vim_fit.yaml",
97 "onap.policies.optimization.resource.VnfPolicy#1.0.0"
98 + "#policytypes/onap.policies.optimization.resource.VnfPolicy.yaml",
99 "onap.policies.optimization.Resource#1.0.0"
100 + "#policytypes/onap.policies.optimization.Resource.yaml",
101 "onap.policies.optimization.service.QueryPolicy#1.0.0"
102 + "#policytypes/onap.policies.optimization.service.QueryPolicy.yaml",
103 "onap.policies.optimization.service.SubscriberPolicy#1.0.0"
104 + "#policytypes/onap.policies.optimization.service.SubscriberPolicy.yaml",
105 "onap.policies.optimization.Service#1.0.0"
106 + "#policytypes/onap.policies.optimization.Service.yaml",
107 "onap.policies.Optimization#1.0.0"
108 + "#policytypes/onap.policies.Optimization.yaml"};
111 private static final Map<ToscaEntityKey, ToscaServiceTemplate> policyTypeMap = new LinkedHashMap<>();
114 * Set up the DAO towards the database.
116 * @throws Exception on database errors
119 public static void beforeSetupDao() throws Exception {
120 final DaoParameters daoParameters = new DaoParameters();
121 daoParameters.setPluginClass(DefaultPfDao.class.getName());
123 daoParameters.setPersistenceUnit("ToscaConceptTest");
125 Properties jdbcProperties = new Properties();
126 jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_USER, "policy");
127 jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_PASSWORD, "P01icY");
129 // H2, use "org.mariadb.jdbc.Driver" and "jdbc:mariadb://localhost:3306/policy" for locally installed MariaDB
130 jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_DRIVER, "org.h2.Driver");
131 jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_URL, "jdbc:h2:mem:testdb");
133 daoParameters.setJdbcProperties(jdbcProperties);
135 pfDao = new PfDaoFactory().createPfDao(daoParameters);
136 pfDao.init(daoParameters);
140 * Populate the database.
142 * @throws PfModelException on database exceptions
143 * @throws CoderException on JSON encoding/decoding errors
146 public static void beforeFillDatabase() throws PfModelException, CoderException {
147 for (String policyTypeDataString : EXAMPLE_POLICY_TYPES) {
148 String[] policyTypeDataArray = policyTypeDataString.split("#");
149 String policyTypeYamlDefinition = ResourceUtils.getResourceAsString(policyTypeDataArray[2]);
151 Object yamlObject = new Yaml().load(policyTypeYamlDefinition);
152 String policyTypeJsonDefinition = coder.encode(yamlObject);
154 ToscaServiceTemplate serviceTemplate = coder.decode(policyTypeJsonDefinition, ToscaServiceTemplate.class);
155 policyTypeMap.put(new ToscaEntityKey(policyTypeDataArray[0], policyTypeDataArray[1]), serviceTemplate);
156 new AuthorativeToscaProvider().createPolicyTypes(pfDao, serviceTemplate);
161 public static void afterTeardown() {
166 public void testPolicyTypeRead() throws PfModelException, CoderException {
167 for (Entry<ToscaEntityKey, ToscaServiceTemplate> policyTypeMapEntry : policyTypeMap.entrySet()) {
168 ToscaServiceTemplate serviceTemplate = new AuthorativeToscaProvider().getPolicyTypes(pfDao,
169 policyTypeMapEntry.getKey().getName(), policyTypeMapEntry.getKey().getVersion());
171 assertEquals(1, serviceTemplate.getPolicyTypes().size());
173 String originalJson = coder.encode(policyTypeMapEntry.getValue());
174 String databaseJson = coder.encode(serviceTemplate);
176 // TODO: This test has no chance of passing yet but must eventually pass to prove that the policy types
177 // TODO: that were retrieved are the same as the policy types that were stored
178 // assertEquals(originalJson, databaseJson);