bd6b26bb9925e7889be21e754409cc5b77d2ec8a
[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 com.google.gson.Gson;
29 import com.google.gson.JsonSyntaxException;
30 import java.io.IOException;
31 import org.junit.Before;
32 import org.junit.Test;
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.PlainToscaServiceTemplate;
36 import org.onap.policy.models.tosca.simple.concepts.ToscaServiceTemplate;
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 PlainToscaServiceTemplateMapperTest {
45
46     private Gson defaultGson;
47     private PlainToscaServiceTemplateMapper mapper;
48
49     @Before
50     public void setUp() {
51         defaultGson = new Gson();
52         mapper = new PlainToscaServiceTemplateMapper();
53     }
54
55     @Test
56     public void testPlainToscaPolicies() throws JsonSyntaxException, IOException {
57         try {
58             String inputJson = ResourceUtils.getResourceAsString("policies/vCPE.policy.monitoring.input.tosca.json");
59
60             PlainToscaServiceTemplate plainPolicies = defaultGson.fromJson(inputJson, PlainToscaServiceTemplate.class);
61             ToscaServiceTemplate internalPolicies = mapper.toToscaServiceTemplate(plainPolicies);
62             assertTrue(internalPolicies.validate(new PfValidationResult()).isValid());
63             PlainToscaServiceTemplate plainPolicies2 = mapper.fromToscaServiceTemplate(internalPolicies);
64             assertTrue(plainPolicies.equals(plainPolicies2));
65
66         } catch (Exception e) {
67             fail("no exception should be thrown");
68         }
69     }
70
71     @Test
72     public void testPlainToscaPolicyTypes() throws JsonSyntaxException, IOException {
73         try {
74             Yaml yaml = new Yaml();
75             String inputYaml = ResourceUtils.getResourceAsString(
76                     "policytypes/onap.policy.monitoring.cdap.tca.hi.lo.app.yaml");
77             Object yamlObject = yaml.load(inputYaml);
78             String yamlAsJsonString = defaultGson.toJson(yamlObject);
79
80             PlainToscaServiceTemplate plainPolicyTypes = defaultGson.fromJson(yamlAsJsonString,
81                     PlainToscaServiceTemplate.class);
82             ToscaServiceTemplate internalPolicyTypes = mapper.toToscaServiceTemplate(plainPolicyTypes);
83             assertTrue(internalPolicyTypes.validate(new PfValidationResult()).isValid());
84             PlainToscaServiceTemplate plainPolicyTypes2 = mapper.fromToscaServiceTemplate(internalPolicyTypes);
85             ToscaServiceTemplate internalPolicyTypes2 = mapper.toToscaServiceTemplate(plainPolicyTypes2);
86             assertTrue(internalPolicyTypes2.validate(new PfValidationResult()).isValid());
87             assertTrue(internalPolicyTypes.compareTo(internalPolicyTypes2) == 0);
88
89         } catch (Exception e) {
90             fail("no exception should be thrown");
91         }
92
93     }
94 }