Merge "Fix the bug of cannot return multiple versions of particular tosca policy...
[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 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.resources.ResourceUtils;
33 import org.onap.policy.models.base.PfValidationResult;
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.yaml.snakeyaml.Yaml;
38
39 /**
40  * This class performs unit test of {@link PlainToscaServiceTemplateMapper}}.
41  *
42  * @author Chenfei Gao (cgao@research.att.com)
43  */
44 public class ToscaServiceTemplateMappingTest {
45
46     private StandardCoder standardCoder;
47
48     @Before
49     public void setUp() {
50         standardCoder = new StandardCoder();
51     }
52
53     @Test
54     public void testPlainToscaPolicies() throws Exception {
55         String inputJson = ResourceUtils.getResourceAsString("policies/vCPE.policy.monitoring.input.tosca.json");
56
57         ToscaServiceTemplate plainPolicies = standardCoder.decode(inputJson, ToscaServiceTemplate.class);
58         JpaToscaServiceTemplate internalPolicies = new JpaToscaServiceTemplate();
59         internalPolicies.fromAuthorative(plainPolicies);
60
61         assertTrue(internalPolicies.validate(new PfValidationResult()).isValid());
62         ToscaServiceTemplate plainPolicies2 = internalPolicies.toAuthorative();
63
64         ToscaPolicy pp1 = plainPolicies.getToscaTopologyTemplate().getPolicies().get(0).values().iterator().next();
65         ToscaPolicy pp2 = plainPolicies2.getToscaTopologyTemplate().getPolicies().get(0).values().iterator().next();
66
67         assertEquals(pp1.getProperties().keySet(), pp2.getProperties().keySet());
68     }
69
70     @Test
71     public void testPlainToscaPolicyTypes() throws Exception {
72         Yaml yaml = new Yaml();
73         String inputYaml = ResourceUtils.getResourceAsString(
74                 "policytypes/onap.policies.monitoring.cdap.tca.hi.lo.app.yaml");
75         Object yamlObject = yaml.load(inputYaml);
76         String yamlAsJsonString = standardCoder.encode(yamlObject);
77
78         ToscaServiceTemplate plainPolicyTypes = standardCoder.decode(yamlAsJsonString,
79                 ToscaServiceTemplate.class);
80         JpaToscaServiceTemplate internalPolicyTypes = new JpaToscaServiceTemplate();
81         internalPolicyTypes.fromAuthorative(plainPolicyTypes);
82         assertTrue(internalPolicyTypes.validate(new PfValidationResult()).isValid());
83         ToscaServiceTemplate plainPolicyTypes2 = internalPolicyTypes.toAuthorative();
84         JpaToscaServiceTemplate internalPolicyTypes2 = new JpaToscaServiceTemplate();
85         internalPolicyTypes2.fromAuthorative(plainPolicyTypes2);
86         assertTrue(internalPolicyTypes2.validate(new PfValidationResult()).isValid());
87         assertTrue(internalPolicyTypes.compareTo(internalPolicyTypes2) == 0);
88     }
89 }