2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2019-2020 Nordix Foundation.
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
20 * SPDX-License-Identifier: Apache-2.0
21 * ============LICENSE_END=========================================================
24 package org.onap.policy.models.tosca.authorative.provider;
26 import static org.junit.Assert.assertEquals;
27 import static org.junit.Assert.assertTrue;
29 import org.junit.Before;
30 import org.junit.Test;
31 import org.onap.policy.common.utils.coder.StandardCoder;
32 import org.onap.policy.common.utils.coder.YamlJsonTranslator;
33 import org.onap.policy.common.utils.resources.ResourceUtils;
34 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
35 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
36 import org.onap.policy.models.tosca.simple.concepts.JpaToscaServiceTemplate;
37 import org.onap.policy.models.tosca.utils.ToscaServiceTemplateUtils;
38 import org.yaml.snakeyaml.Yaml;
41 * This class performs unit test of {@link PlainToscaServiceTemplateMapper}}.
43 * @author Chenfei Gao (cgao@research.att.com)
45 public class ToscaServiceTemplateMappingTest {
47 private StandardCoder standardCoder;
48 private YamlJsonTranslator yamlJsonTranslator = new YamlJsonTranslator();
52 standardCoder = new StandardCoder();
56 public void testPlainToscaPolicies() throws Exception {
57 String policyTypeInputJson =
58 ResourceUtils.getResourceAsString("policytypes/onap.policies.monitoring.tcagen2.yaml");
59 ToscaServiceTemplate plainPolicyTypes =
60 yamlJsonTranslator.fromYaml(policyTypeInputJson, ToscaServiceTemplate.class);
62 String policyInputJson = ResourceUtils.getResourceAsString("policies/vCPE.policy.monitoring.input.tosca.json");
63 ToscaServiceTemplate plainPolicies = standardCoder.decode(policyInputJson, ToscaServiceTemplate.class);
65 JpaToscaServiceTemplate policyTypeServiceTemplate = new JpaToscaServiceTemplate();
66 policyTypeServiceTemplate.fromAuthorative(plainPolicyTypes);
68 JpaToscaServiceTemplate policyFragmentServiceTemplate = new JpaToscaServiceTemplate();
69 policyFragmentServiceTemplate.fromAuthorative(plainPolicies);
71 JpaToscaServiceTemplate internalServiceTemplate =
72 ToscaServiceTemplateUtils.addFragment(policyTypeServiceTemplate, policyFragmentServiceTemplate);
74 assertTrue(internalServiceTemplate.validate("").isValid());
75 ToscaServiceTemplate plainPolicies2 = internalServiceTemplate.toAuthorative();
77 ToscaPolicy pp1 = plainPolicies.getToscaTopologyTemplate().getPolicies().get(0).values().iterator().next();
78 ToscaPolicy pp2 = plainPolicies2.getToscaTopologyTemplate().getPolicies().get(0).values().iterator().next();
80 assertEquals(pp1.getProperties().keySet(), pp2.getProperties().keySet());
84 public void testPlainToscaPolicyTypes() throws Exception {
85 Yaml yaml = new Yaml();
87 ResourceUtils.getResourceAsString("policytypes/onap.policies.monitoring.tcagen2.yaml");
88 Object yamlObject = yaml.load(inputYaml);
89 String yamlAsJsonString = standardCoder.encode(yamlObject);
91 ToscaServiceTemplate plainPolicyTypes = standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class);
92 JpaToscaServiceTemplate internalPolicyTypes = new JpaToscaServiceTemplate();
93 internalPolicyTypes.fromAuthorative(plainPolicyTypes);
94 assertTrue(internalPolicyTypes.validate("").isValid());
95 ToscaServiceTemplate plainPolicyTypes2 = internalPolicyTypes.toAuthorative();
96 JpaToscaServiceTemplate internalPolicyTypes2 = new JpaToscaServiceTemplate();
97 internalPolicyTypes2.fromAuthorative(plainPolicyTypes2);
98 assertTrue(internalPolicyTypes2.validate("").isValid());
99 assertEquals(0, internalPolicyTypes.compareTo(internalPolicyTypes2));