2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019 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 @NonNull 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.getCanonicalName());
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 }).hasMessage(DAO_IS_NULL);
113 assertThatThrownBy(() -> {
114 new AuthorativeToscaProvider().getPolicyList(null, null, null);
115 }).hasMessage(DAO_IS_NULL);
119 ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode(
120 ResourceUtils.getResourceAsString(VCPE_JSON),
121 ToscaServiceTemplate.class);
123 assertNotNull(toscaServiceTemplate);
124 ToscaServiceTemplate createdServiceTemplate =
125 new AuthorativeToscaProvider().createPolicies(pfDao, toscaServiceTemplate);
127 PfConceptKey policyKey = new PfConceptKey(POLICY_AND_VERSION);
129 ToscaPolicy beforePolicy =
130 toscaServiceTemplate.getToscaTopologyTemplate().getPolicies().get(0).get(policyKey.getName());
131 ToscaPolicy createdPolicy =
132 createdServiceTemplate.getToscaTopologyTemplate().getPolicies().get(0).get(policyKey.getName());
133 assertEquals(0, beforePolicy.compareNameVersion(beforePolicy, createdPolicy));
134 assertTrue(beforePolicy.getType().equals(createdPolicy.getType()));
136 ToscaServiceTemplate gotServiceTemplate =
137 new AuthorativeToscaProvider().getPolicies(pfDao, policyKey.getName(), policyKey.getVersion());
139 ToscaPolicy gotPolicy =
140 gotServiceTemplate.getToscaTopologyTemplate().getPolicies().get(0).get(policyKey.getName());
141 assertEquals(0, beforePolicy.compareNameVersion(beforePolicy, gotPolicy));
142 assertTrue(beforePolicy.getType().equals(gotPolicy.getType()));
144 List<ToscaPolicy> gotPolicyList =
145 new AuthorativeToscaProvider().getPolicyList(pfDao, POLICY1, VERSION_100);
146 assertEquals(1, gotPolicyList.size());
147 assertEquals(0, beforePolicy.compareNameVersion(beforePolicy, gotPolicyList.get(0)));
149 gotPolicyList = new AuthorativeToscaProvider().getPolicyList(pfDao, POLICY1, null);
150 assertEquals(1, gotPolicyList.size());
151 assertEquals(0, beforePolicy.compareNameVersion(beforePolicy, gotPolicyList.get(0)));
153 gotPolicyList = new AuthorativeToscaProvider().getPolicyList(pfDao, null, null);
154 assertEquals(1, gotPolicyList.size());
155 assertEquals(0, beforePolicy.compareNameVersion(beforePolicy, gotPolicyList.get(0)));
157 gotPolicyList = new AuthorativeToscaProvider().getPolicyList(pfDao, null, VERSION_100);
158 assertEquals(1, gotPolicyList.size());
159 assertEquals(0, beforePolicy.compareNameVersion(beforePolicy, gotPolicyList.get(0)));
161 gotPolicyList = new AuthorativeToscaProvider().getPolicyList(pfDao, "Nonexistant", VERSION_100);
162 assertEquals(0, gotPolicyList.size());
166 public void testPoliciesGetFiltered() throws Exception {
167 assertThatThrownBy(() -> {
168 new AuthorativeToscaProvider().getFilteredPolicies(null, null);
169 }).hasMessage(DAO_IS_NULL);
171 assertThatThrownBy(() -> {
172 new AuthorativeToscaProvider().getFilteredPolicies(null, ToscaPolicyFilter.builder().build());
173 }).hasMessage(DAO_IS_NULL);
175 assertThatThrownBy(() -> {
176 new AuthorativeToscaProvider().getFilteredPolicies(pfDao, null);
177 }).hasMessage("filter is marked @NonNull but is null");
179 assertThatThrownBy(() -> {
180 new AuthorativeToscaProvider().getFilteredPolicyList(null, null);
181 }).hasMessage(DAO_IS_NULL);
183 assertThatThrownBy(() -> {
184 new AuthorativeToscaProvider().getFilteredPolicyList(null, ToscaPolicyFilter.builder().build());
185 }).hasMessage(DAO_IS_NULL);
187 assertThatThrownBy(() -> {
188 new AuthorativeToscaProvider().getFilteredPolicyList(pfDao, null);
189 }).hasMessage("filter is marked @NonNull but is null");
193 ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode(
194 ResourceUtils.getResourceAsString(VCPE_JSON),
195 ToscaServiceTemplate.class);
197 assertNotNull(toscaServiceTemplate);
198 ToscaServiceTemplate createdServiceTemplate =
199 new AuthorativeToscaProvider().createPolicies(pfDao, toscaServiceTemplate);
201 PfConceptKey policyKey = new PfConceptKey(POLICY_AND_VERSION);
203 ToscaPolicy beforePolicy =
204 toscaServiceTemplate.getToscaTopologyTemplate().getPolicies().get(0).get(policyKey.getName());
205 ToscaPolicy createdPolicy =
206 createdServiceTemplate.getToscaTopologyTemplate().getPolicies().get(0).get(policyKey.getName());
207 assertEquals(0, beforePolicy.compareNameVersion(beforePolicy, createdPolicy));
208 assertTrue(beforePolicy.getType().equals(createdPolicy.getType()));
210 ToscaServiceTemplate gotServiceTemplate =
211 new AuthorativeToscaProvider().getFilteredPolicies(pfDao, ToscaPolicyFilter.builder().build());
213 ToscaPolicy gotPolicy =
214 gotServiceTemplate.getToscaTopologyTemplate().getPolicies().get(0).get(policyKey.getName());
215 assertEquals(0, beforePolicy.compareNameVersion(beforePolicy, gotPolicy));
216 assertTrue(beforePolicy.getType().equals(gotPolicy.getType()));
218 gotServiceTemplate = new AuthorativeToscaProvider().getFilteredPolicies(pfDao,
219 ToscaPolicyFilter.builder().name(policyKey.getName()).build());
221 gotPolicy = gotServiceTemplate.getToscaTopologyTemplate().getPolicies().get(0).get(policyKey.getName());
222 assertEquals(0, beforePolicy.compareNameVersion(beforePolicy, gotPolicy));
223 assertTrue(beforePolicy.getType().equals(gotPolicy.getType()));
225 gotServiceTemplate = new AuthorativeToscaProvider().getFilteredPolicies(pfDao,
226 ToscaPolicyFilter.builder().name(policyKey.getName()).version(VERSION_100).build());
228 gotPolicy = gotServiceTemplate.getToscaTopologyTemplate().getPolicies().get(0).get(policyKey.getName());
229 assertEquals(0, beforePolicy.compareNameVersion(beforePolicy, gotPolicy));
230 assertTrue(beforePolicy.getType().equals(gotPolicy.getType()));
232 List<ToscaPolicy> gotPolicyList =
233 new AuthorativeToscaProvider().getPolicyList(pfDao, POLICY1, VERSION_100);
234 assertEquals(1, gotPolicyList.size());
235 assertEquals(0, beforePolicy.compareNameVersion(beforePolicy, gotPolicyList.get(0)));
238 new AuthorativeToscaProvider().getFilteredPolicyList(pfDao, ToscaPolicyFilter.builder().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()).build());
244 assertEquals(1, gotPolicyList.size());
245 assertEquals(0, beforePolicy.compareNameVersion(beforePolicy, gotPolicyList.get(0)));
247 gotPolicyList = new AuthorativeToscaProvider().getFilteredPolicyList(pfDao,
248 ToscaPolicyFilter.builder().name(policyKey.getName()).version(VERSION_100).build());
249 assertEquals(1, gotPolicyList.size());
250 assertEquals(0, beforePolicy.compareNameVersion(beforePolicy, gotPolicyList.get(0)));
254 public void testPolicyCreate() throws Exception {
255 assertThatThrownBy(() -> {
256 new AuthorativeToscaProvider().createPolicies(null, null);
257 }).hasMessage(DAO_IS_NULL);
259 assertThatThrownBy(() -> {
260 new AuthorativeToscaProvider().createPolicies(null, new ToscaServiceTemplate());
261 }).hasMessage(DAO_IS_NULL);
263 assertThatThrownBy(() -> {
264 new AuthorativeToscaProvider().createPolicies(pfDao, null);
265 }).hasMessage("serviceTemplate is marked @NonNull but is null");
269 ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode(
270 ResourceUtils.getResourceAsString(VCPE_JSON),
271 ToscaServiceTemplate.class);
273 assertNotNull(toscaServiceTemplate);
274 ToscaServiceTemplate createdServiceTemplate =
275 new AuthorativeToscaProvider().createPolicies(pfDao, toscaServiceTemplate);
277 PfConceptKey policyKey = new PfConceptKey(POLICY_AND_VERSION);
279 ToscaPolicy beforePolicy =
280 toscaServiceTemplate.getToscaTopologyTemplate().getPolicies().get(0).get(policyKey.getName());
281 ToscaPolicy createdPolicy =
282 createdServiceTemplate.getToscaTopologyTemplate().getPolicies().get(0).get(policyKey.getName());
283 assertEquals(0, beforePolicy.compareNameVersion(beforePolicy, createdPolicy));
284 assertTrue(beforePolicy.getType().equals(createdPolicy.getType()));
288 public void testPolicyUpdate() throws Exception {
289 assertThatThrownBy(() -> {
290 new AuthorativeToscaProvider().createPolicies(null, null);
291 }).hasMessage(DAO_IS_NULL);
293 assertThatThrownBy(() -> {
294 new AuthorativeToscaProvider().updatePolicies(null, null);
295 }).hasMessage(DAO_IS_NULL);
297 assertThatThrownBy(() -> {
298 new AuthorativeToscaProvider().updatePolicies(null, new ToscaServiceTemplate());
299 }).hasMessage(DAO_IS_NULL);
301 assertThatThrownBy(() -> {
302 new AuthorativeToscaProvider().updatePolicies(pfDao, null);
303 }).hasMessage("serviceTemplate is marked @NonNull but is null");
307 ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode(
308 ResourceUtils.getResourceAsString(VCPE_JSON),
309 ToscaServiceTemplate.class);
311 assertNotNull(toscaServiceTemplate);
312 ToscaServiceTemplate createdServiceTemplate =
313 new AuthorativeToscaProvider().createPolicies(pfDao, toscaServiceTemplate);
315 PfConceptKey policyKey = new PfConceptKey(POLICY_AND_VERSION);
317 ToscaPolicy beforePolicy =
318 toscaServiceTemplate.getToscaTopologyTemplate().getPolicies().get(0).get(policyKey.getName());
319 ToscaPolicy createdPolicy =
320 createdServiceTemplate.getToscaTopologyTemplate().getPolicies().get(0).get(policyKey.getName());
321 assertEquals(0, beforePolicy.compareNameVersion(beforePolicy, createdPolicy));
322 assertTrue(beforePolicy.getType().equals(createdPolicy.getType()));
324 ToscaServiceTemplate updatedServiceTemplate =
325 new AuthorativeToscaProvider().updatePolicies(pfDao, toscaServiceTemplate);
327 ToscaPolicy updatedPolicy =
328 updatedServiceTemplate.getToscaTopologyTemplate().getPolicies().get(0).get(policyKey.getName());
329 assertEquals(0, beforePolicy.compareNameVersion(beforePolicy, updatedPolicy));
330 assertTrue(beforePolicy.getType().equals(updatedPolicy.getType()));
334 public void testPoliciesDelete() throws Exception {
335 assertThatThrownBy(() -> {
336 new AuthorativeToscaProvider().deletePolicy(null, null, null);
337 }).hasMessage(DAO_IS_NULL);
339 assertThatThrownBy(() -> {
340 new AuthorativeToscaProvider().deletePolicy(null, null, VERSION);
341 }).hasMessage(DAO_IS_NULL);
343 assertThatThrownBy(() -> {
344 new AuthorativeToscaProvider().deletePolicy(null, "name", null);
345 }).hasMessage(DAO_IS_NULL);
347 assertThatThrownBy(() -> {
348 new AuthorativeToscaProvider().deletePolicy(null, "name", VERSION);
349 }).hasMessage(DAO_IS_NULL);
351 assertThatThrownBy(() -> {
352 new AuthorativeToscaProvider().deletePolicy(pfDao, null, null);
353 }).hasMessage("name is marked @NonNull but is null");
355 assertThatThrownBy(() -> {
356 new AuthorativeToscaProvider().deletePolicy(pfDao, null, VERSION);
357 }).hasMessage("name is marked @NonNull but is null");
359 assertThatThrownBy(() -> {
360 new AuthorativeToscaProvider().deletePolicy(pfDao, "name", null);
361 }).hasMessage("version is marked @NonNull but is null");
365 ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode(
366 ResourceUtils.getResourceAsString(VCPE_JSON),
367 ToscaServiceTemplate.class);
369 assertNotNull(toscaServiceTemplate);
370 ToscaServiceTemplate createdServiceTemplate =
371 new AuthorativeToscaProvider().createPolicies(pfDao, toscaServiceTemplate);
373 PfConceptKey policyKey = new PfConceptKey(POLICY_AND_VERSION);
375 ToscaPolicy beforePolicy =
376 toscaServiceTemplate.getToscaTopologyTemplate().getPolicies().get(0).get(policyKey.getName());
377 ToscaPolicy createdPolicy =
378 createdServiceTemplate.getToscaTopologyTemplate().getPolicies().get(0).get(policyKey.getName());
379 assertEquals(0, beforePolicy.compareNameVersion(beforePolicy, createdPolicy));
380 assertTrue(beforePolicy.getType().equals(createdPolicy.getType()));
382 ToscaServiceTemplate deletedServiceTemplate =
383 new AuthorativeToscaProvider().deletePolicy(pfDao, policyKey.getName(), policyKey.getVersion());
385 ToscaPolicy deletedPolicy =
386 deletedServiceTemplate.getToscaTopologyTemplate().getPolicies().get(0).get(policyKey.getName());
387 assertEquals(0, beforePolicy.compareNameVersion(beforePolicy, createdPolicy));
388 assertTrue(beforePolicy.getType().equals(deletedPolicy.getType()));
390 ToscaServiceTemplate gotServiceTemplate =
391 new AuthorativeToscaProvider().getPolicies(pfDao, policyKey.getName(), policyKey.getVersion());
393 assertTrue(gotServiceTemplate.getToscaTopologyTemplate().getPolicies().isEmpty());
397 public void testAssertPoliciesExist() {
398 ToscaServiceTemplate testServiceTemplate = new ToscaServiceTemplate();
400 assertThatThrownBy(() -> {
401 new AuthorativeToscaProvider().deletePolicy(pfDao, "name", null);
402 }).hasMessage("version is marked @NonNull but is null");
404 assertThatThrownBy(() -> {
405 new AuthorativeToscaProvider().createPolicies(pfDao, testServiceTemplate);
406 }).hasMessage("topology template not specified on service template");
408 testServiceTemplate.setToscaTopologyTemplate(new ToscaTopologyTemplate());
409 assertThatThrownBy(() -> {
410 new AuthorativeToscaProvider().createPolicies(pfDao, testServiceTemplate);
411 }).hasMessage("no policies specified on topology template of service template");
413 testServiceTemplate.getToscaTopologyTemplate().setPolicies(new ArrayList<>());
414 assertThatThrownBy(() -> {
415 new AuthorativeToscaProvider().createPolicies(pfDao, testServiceTemplate);
416 }).hasMessage("An incoming list of concepts must have at least one entry");
420 public void testEntityMaps() throws CoderException, PfModelException {
421 Object yamlObject = new Yaml().load(
422 ResourceUtils.getResourceAsString("policytypes/onap.policies.monitoring.cdap.tca.hi.lo.app.yaml"));
423 String yamlAsJsonString = new StandardCoder().encode(yamlObject);
425 ToscaServiceTemplate toscaServiceTemplatePolicyType =
426 standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class);
428 assertNotNull(toscaServiceTemplatePolicyType);
429 new AuthorativeToscaProvider().createPolicyTypes(pfDao, toscaServiceTemplatePolicyType);
431 assertEquals(3, toscaServiceTemplatePolicyType.getDataTypesAsMap().size());
432 assertEquals(2, toscaServiceTemplatePolicyType.getPolicyTypesAsMap().size());
434 ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode(
435 ResourceUtils.getResourceAsString("policies/vCPE.policy.monitoring.input.tosca.json"),
436 ToscaServiceTemplate.class);
438 assertNotNull(toscaServiceTemplate);
439 ToscaServiceTemplate createdServiceTemplate =
440 new AuthorativeToscaProvider().createPolicies(pfDao, toscaServiceTemplate);
442 PfConceptKey policyKey = new PfConceptKey("onap.restart.tca:1.0.0");
444 ToscaPolicy beforePolicy =
445 toscaServiceTemplate.getToscaTopologyTemplate().getPolicies().get(0).get(policyKey.getName());
446 ToscaPolicy createdPolicy =
447 createdServiceTemplate.getToscaTopologyTemplate().getPolicies().get(0).get(policyKey.getName());
448 assertEquals(0, beforePolicy.compareNameVersion(beforePolicy, createdPolicy));
449 assertTrue(beforePolicy.getType().equals(createdPolicy.getType()));
451 assertEquals(1, toscaServiceTemplate.getToscaTopologyTemplate().getPoliciesAsMap().size());
452 assertEquals(1, createdServiceTemplate.getToscaTopologyTemplate().getPoliciesAsMap().size());
454 Map<String, ToscaPolicy> policyMapItem = createdServiceTemplate.getToscaTopologyTemplate().getPolicies().get(0);
455 createdServiceTemplate.getToscaTopologyTemplate().getPolicies().add(policyMapItem);
457 assertThatThrownBy(() -> {
458 createdServiceTemplate.getToscaTopologyTemplate().getPoliciesAsMap();
459 }).hasMessageContaining("list of map of entities contains more than one entity with key");
462 private void createPolicyTypes() throws CoderException, PfModelException {
463 Object yamlObject = new Yaml().load(
464 ResourceUtils.getResourceAsString("policytypes/onap.policies.monitoring.cdap.tca.hi.lo.app.yaml"));
465 String yamlAsJsonString = new StandardCoder().encode(yamlObject);
467 ToscaServiceTemplate toscaServiceTemplatePolicyType =
468 standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class);
470 assertNotNull(toscaServiceTemplatePolicyType);
471 new AuthorativeToscaProvider().createPolicyTypes(pfDao, toscaServiceTemplatePolicyType);