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 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;
38 * This class performs unit test of {@link PlainToscaServiceTemplateMapper}}.
40 * @author Chenfei Gao (cgao@research.att.com)
42 public class PlainToscaServiceTemplateMapperTest {
44 private StandardCoder standardCoder;
45 private PlainToscaServiceTemplateMapper mapper;
49 standardCoder = new StandardCoder();
50 mapper = new PlainToscaServiceTemplateMapper();
54 public void testPlainToscaPolicies() throws Exception {
56 String inputJson = ResourceUtils.getResourceAsString("policies/vCPE.policy.monitoring.input.tosca.json");
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));
64 } catch (Exception e) {
65 fail("no exception should be thrown");
70 public void testPlainToscaPolicyTypes() throws Exception {
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);
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);
87 } catch (Exception e) {
88 fail("no exception should be thrown");