cd35f710a9709f3480778ee7322c8197c4b22816
[policy/models.git] / models-tosca / src / test / java / org / onap / policy / models / tosca / authorative / provider / ToscaServiceTemplateMappingTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP Policy Model
4  * ================================================================================
5  * Copyright (C) 2019 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
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
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.
19  *
20  * SPDX-License-Identifier: Apache-2.0
21  * ============LICENSE_END=========================================================
22  */
23
24 package org.onap.policy.models.tosca.authorative.provider;
25
26 import static org.junit.Assert.assertEquals;
27 import static org.junit.Assert.assertTrue;
28
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.base.PfValidationResult;
35 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
36 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
37 import org.onap.policy.models.tosca.simple.concepts.JpaToscaServiceTemplate;
38 import org.onap.policy.models.tosca.utils.ToscaServiceTemplateUtils;
39 import org.yaml.snakeyaml.Yaml;
40
41 /**
42  * This class performs unit test of {@link PlainToscaServiceTemplateMapper}}.
43  *
44  * @author Chenfei Gao (cgao@research.att.com)
45  */
46 public class ToscaServiceTemplateMappingTest {
47
48     private StandardCoder standardCoder;
49     private YamlJsonTranslator yamlJsonTranslator = new YamlJsonTranslator();
50
51     @Before
52     public void setUp() {
53         standardCoder = new StandardCoder();
54     }
55
56     @Test
57     public void testPlainToscaPolicies() throws Exception {
58         String policyTypeInputJson =
59                 ResourceUtils.getResourceAsString("policytypes/onap.policies.monitoring.tcagen2.yaml");
60         ToscaServiceTemplate plainPolicyTypes =
61                 yamlJsonTranslator.fromYaml(policyTypeInputJson, ToscaServiceTemplate.class);
62
63         String policyInputJson = ResourceUtils.getResourceAsString("policies/vCPE.policy.monitoring.input.tosca.json");
64         ToscaServiceTemplate plainPolicies = standardCoder.decode(policyInputJson, ToscaServiceTemplate.class);
65
66         JpaToscaServiceTemplate policyTypeServiceTemplate = new JpaToscaServiceTemplate();
67         policyTypeServiceTemplate.fromAuthorative(plainPolicyTypes);
68
69         JpaToscaServiceTemplate policyFragmentServiceTemplate = new JpaToscaServiceTemplate();
70         policyFragmentServiceTemplate.fromAuthorative(plainPolicies);
71
72         JpaToscaServiceTemplate internalServiceTemplate =
73                 ToscaServiceTemplateUtils.addFragment(policyTypeServiceTemplate, policyFragmentServiceTemplate);
74
75         assertTrue(internalServiceTemplate.validate(new PfValidationResult()).isValid());
76         ToscaServiceTemplate plainPolicies2 = internalServiceTemplate.toAuthorative();
77
78         ToscaPolicy pp1 = plainPolicies.getToscaTopologyTemplate().getPolicies().get(0).values().iterator().next();
79         ToscaPolicy pp2 = plainPolicies2.getToscaTopologyTemplate().getPolicies().get(0).values().iterator().next();
80
81         assertEquals(pp1.getProperties().keySet(), pp2.getProperties().keySet());
82     }
83
84     @Test
85     public void testPlainToscaPolicyTypes() throws Exception {
86         Yaml yaml = new Yaml();
87         String inputYaml =
88                 ResourceUtils.getResourceAsString("policytypes/onap.policies.monitoring.tcagen2.yaml");
89         Object yamlObject = yaml.load(inputYaml);
90         String yamlAsJsonString = standardCoder.encode(yamlObject);
91
92         ToscaServiceTemplate plainPolicyTypes = standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class);
93         JpaToscaServiceTemplate internalPolicyTypes = new JpaToscaServiceTemplate();
94         internalPolicyTypes.fromAuthorative(plainPolicyTypes);
95         assertTrue(internalPolicyTypes.validate(new PfValidationResult()).isValid());
96         ToscaServiceTemplate plainPolicyTypes2 = internalPolicyTypes.toAuthorative();
97         JpaToscaServiceTemplate internalPolicyTypes2 = new JpaToscaServiceTemplate();
98         internalPolicyTypes2.fromAuthorative(plainPolicyTypes2);
99         assertTrue(internalPolicyTypes2.validate(new PfValidationResult()).isValid());
100         assertTrue(internalPolicyTypes.compareTo(internalPolicyTypes2) == 0);
101     }
102 }