2 * ============LICENSE_START=======================================================
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 * SPDX-License-Identifier: Apache-2.0
21 * ============LICENSE_END=========================================================
24 package org.onap.policy.models.tosca.authorative.mapping;
26 import static org.junit.Assert.assertEquals;
27 import static org.junit.Assert.assertTrue;
28 import static org.junit.Assert.fail;
30 import org.junit.Before;
31 import org.junit.Test;
32 import org.onap.policy.common.utils.coder.StandardCoder;
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.ToscaPolicy;
36 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
37 import org.onap.policy.models.tosca.simple.concepts.JpaToscaServiceTemplate;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40 import org.yaml.snakeyaml.Yaml;
43 * This class performs unit test of {@link PlainToscaServiceTemplateMapper}}.
45 * @author Chenfei Gao (cgao@research.att.com)
47 public class ToscaServiceTemplateMappingTest {
48 private static final Logger LOGGER = LoggerFactory.getLogger(ToscaServiceTemplateMappingTest.class);
50 private StandardCoder standardCoder;
54 standardCoder = new StandardCoder();
58 public void testPlainToscaPolicies() throws Exception {
60 String inputJson = ResourceUtils.getResourceAsString("policies/vCPE.policy.monitoring.input.tosca.json");
62 ToscaServiceTemplate plainPolicies = standardCoder.decode(inputJson, ToscaServiceTemplate.class);
63 JpaToscaServiceTemplate internalPolicies = new JpaToscaServiceTemplate();
64 internalPolicies.fromAuthorative(plainPolicies);
66 assertTrue(internalPolicies.validate(new PfValidationResult()).isValid());
67 ToscaServiceTemplate plainPolicies2 = internalPolicies.toAuthorative();
69 ToscaPolicy pp1 = plainPolicies.getToscaTopologyTemplate().getPolicies().get(0).values().iterator().next();
70 ToscaPolicy pp2 = plainPolicies2.getToscaTopologyTemplate().getPolicies().get(0).values().iterator().next();
72 assertEquals(pp1.getProperties().keySet(), pp2.getProperties().keySet());
74 } catch (Exception e) {
75 LOGGER.warn("no exception should be thrown", e);
76 fail("no exception should be thrown");
81 public void testPlainToscaPolicyTypes() throws Exception {
83 Yaml yaml = new Yaml();
84 String inputYaml = ResourceUtils.getResourceAsString(
85 "policytypes/onap.policy.monitoring.cdap.tca.hi.lo.app.yaml");
86 Object yamlObject = yaml.load(inputYaml);
87 String yamlAsJsonString = standardCoder.encode(yamlObject);
89 ToscaServiceTemplate plainPolicyTypes = standardCoder.decode(yamlAsJsonString,
90 ToscaServiceTemplate.class);
91 JpaToscaServiceTemplate internalPolicyTypes = new JpaToscaServiceTemplate();
92 internalPolicyTypes.fromAuthorative(plainPolicyTypes);
93 assertTrue(internalPolicyTypes.validate(new PfValidationResult()).isValid());
94 ToscaServiceTemplate plainPolicyTypes2 = internalPolicyTypes.toAuthorative();
95 JpaToscaServiceTemplate internalPolicyTypes2 = new JpaToscaServiceTemplate();
96 internalPolicyTypes2.fromAuthorative(plainPolicyTypes2);
97 assertTrue(internalPolicyTypes2.validate(new PfValidationResult()).isValid());
98 assertTrue(internalPolicyTypes.compareTo(internalPolicyTypes2) == 0);
100 } catch (Exception e) {
101 LOGGER.warn("no exception should be thrown", e);
102 fail("no exception should be thrown");