2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019 Nordix Foundation.
4 * Modifications Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.models.tosca.legacy.mapping;
24 import static org.assertj.core.api.Assertions.assertThatThrownBy;
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertNotNull;
27 import static org.junit.Assert.assertTrue;
29 import java.util.LinkedHashMap;
31 import org.junit.Before;
32 import org.junit.Test;
33 import org.onap.policy.common.utils.coder.StandardCoder;
34 import org.onap.policy.common.utils.resources.ResourceUtils;
35 import org.onap.policy.models.base.PfConceptKey;
36 import org.onap.policy.models.base.PfValidationResult;
37 import org.onap.policy.models.tosca.legacy.concepts.LegacyOperationalPolicy;
38 import org.onap.policy.models.tosca.legacy.mapping.LegacyOperationalPolicyMapper;
39 import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicies;
40 import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicy;
41 import org.onap.policy.models.tosca.simple.concepts.JpaToscaServiceTemplate;
42 import org.onap.policy.models.tosca.simple.concepts.JpaToscaTopologyTemplate;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
47 * Test serialization of monitoring policies.
49 * @author Liam Fallon (liam.fallon@est.tech)
51 public class LegacyOperationalPolicyMapperTest {
52 // Logger for this class
53 private static final Logger LOGGER = LoggerFactory.getLogger(LegacyOperationalPolicyMapperTest.class);
55 private StandardCoder standardCoder;
59 standardCoder = new StandardCoder();
63 public void testJsonDeserialization() throws Exception {
64 String vcpePolicyJson = ResourceUtils.getResourceAsString("policies/vCPE.policy.operational.input.json");
66 LegacyOperationalPolicy legacyOperationalPolicy =
67 standardCoder.decode(vcpePolicyJson, LegacyOperationalPolicy.class);
69 JpaToscaServiceTemplate serviceTemplate =
70 new LegacyOperationalPolicyMapper().toToscaServiceTemplate(legacyOperationalPolicy);
72 assertNotNull(serviceTemplate);
73 LOGGER.info(serviceTemplate.validate(new PfValidationResult()).toString());
74 assertTrue(serviceTemplate.validate(new PfValidationResult()).isValid());
76 assertEquals("operational.restart:1.0.0",
77 serviceTemplate.getTopologyTemplate().getPolicies().get("operational.restart").getId());
81 public void testOperationalPolicyMapper() {
82 JpaToscaServiceTemplate serviceTemplate = new JpaToscaServiceTemplate();
83 serviceTemplate.setTopologyTemplate(new JpaToscaTopologyTemplate());
84 serviceTemplate.getTopologyTemplate().setPolicies(new JpaToscaPolicies());
86 JpaToscaPolicy policy0 = new JpaToscaPolicy(new PfConceptKey("PolicyName0", "0.0.1"));
87 serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().put(policy0.getKey(), policy0);
88 JpaToscaPolicy policy1 = new JpaToscaPolicy(new PfConceptKey("PolicyName1", "0.0.1"));
89 serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().put(policy1.getKey(), policy1);
91 assertThatThrownBy(() -> {
92 new LegacyOperationalPolicyMapper().fromToscaServiceTemplate(serviceTemplate);
93 }).hasMessage("more than one policy found in service template");
95 serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().remove(policy1.getKey());
97 assertThatThrownBy(() -> {
98 new LegacyOperationalPolicyMapper().fromToscaServiceTemplate(serviceTemplate);
99 }).hasMessage("no properties defined on TOSCA policy");
101 policy0.setProperties(new LinkedHashMap<>());
102 assertThatThrownBy(() -> {
103 new LegacyOperationalPolicyMapper().fromToscaServiceTemplate(serviceTemplate);
104 }).hasMessage("property \"content\" not defined on TOSCA policy");