2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 * SPDX-License-Identifier: Apache-2.0
20 * ============LICENSE_END=========================================================
23 package org.onap.policy.models.tosca.authorative.mapping;
25 import static org.junit.Assert.assertTrue;
26 import static org.junit.Assert.fail;
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;
40 * This class performs unit test of {@link PlainToscaServiceTemplateMapper}}
42 * @author Chenfei Gao (cgao@research.att.com)
44 public class PlainToscaServiceTemplateMapperTest {
46 private Gson defaultGson;
47 private PlainToscaServiceTemplateMapper mapper;
51 defaultGson = new Gson();
52 mapper = new PlainToscaServiceTemplateMapper();
56 public void testPlainToscaPolicies() throws JsonSyntaxException, IOException {
58 String inputJson = ResourceUtils.getResourceAsString("policies/vCPE.policy.monitoring.input.tosca.json");
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));
66 } catch (Exception e) {
67 fail("no exception should be thrown");
72 public void testPlainToscaPolicyTypes() throws JsonSyntaxException, IOException {
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);
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);
89 } catch (Exception e) {
90 fail("no exception should be thrown");