Restructure for authorative models
[policy/models.git] / models-tosca / src / test / java / org / onap / policy / models / tosca / authorative / mapping / PlainToscaServiceTemplateMapperTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP Policy Model
4  * ================================================================================
5  * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  * SPDX-License-Identifier: Apache-2.0
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.policy.models.tosca.authorative.mapping;
24
25 import static org.junit.Assert.assertTrue;
26 import static org.junit.Assert.fail;
27
28 import org.junit.Before;
29 import org.junit.Test;
30 import org.onap.policy.common.utils.coder.StandardCoder;
31 import org.onap.policy.common.utils.resources.ResourceUtils;
32 import org.onap.policy.models.base.PfValidationResult;
33 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
34 import org.onap.policy.models.tosca.simple.concepts.JpaToscaServiceTemplate;
35 import org.yaml.snakeyaml.Yaml;
36
37 /**
38  * This class performs unit test of {@link PlainToscaServiceTemplateMapper}}.
39  *
40  * @author Chenfei Gao (cgao@research.att.com)
41  */
42 public class PlainToscaServiceTemplateMapperTest {
43
44     private StandardCoder standardCoder;
45     private PlainToscaServiceTemplateMapper mapper;
46
47     @Before
48     public void setUp() {
49         standardCoder = new StandardCoder();
50         mapper = new PlainToscaServiceTemplateMapper();
51     }
52
53     @Test
54     public void testPlainToscaPolicies() throws Exception {
55         try {
56             String inputJson = ResourceUtils.getResourceAsString("policies/vCPE.policy.monitoring.input.tosca.json");
57
58             ToscaServiceTemplate plainPolicies = standardCoder.decode(inputJson, ToscaServiceTemplate.class);
59             JpaToscaServiceTemplate internalPolicies = mapper.toToscaServiceTemplate(plainPolicies);
60             assertTrue(internalPolicies.validate(new PfValidationResult()).isValid());
61             ToscaServiceTemplate plainPolicies2 = mapper.fromToscaServiceTemplate(internalPolicies);
62             assertTrue(plainPolicies.equals(plainPolicies2));
63
64         } catch (Exception e) {
65             fail("no exception should be thrown");
66         }
67     }
68
69     @Test
70     public void testPlainToscaPolicyTypes() throws Exception {
71         try {
72             Yaml yaml = new Yaml();
73             String inputYaml = ResourceUtils.getResourceAsString(
74                     "policytypes/onap.policy.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 = mapper.toToscaServiceTemplate(plainPolicyTypes);
81             assertTrue(internalPolicyTypes.validate(new PfValidationResult()).isValid());
82             ToscaServiceTemplate plainPolicyTypes2 = mapper.fromToscaServiceTemplate(internalPolicyTypes);
83             JpaToscaServiceTemplate internalPolicyTypes2 = mapper.toToscaServiceTemplate(plainPolicyTypes2);
84             assertTrue(internalPolicyTypes2.validate(new PfValidationResult()).isValid());
85             assertTrue(internalPolicyTypes.compareTo(internalPolicyTypes2) == 0);
86
87         } catch (Exception e) {
88             fail("no exception should be thrown");
89         }
90
91     }
92 }