Changes for Checkstyle 8.32
[policy/models.git] / models-tosca / src / test / java / org / onap / policy / models / tosca / legacy / mapping / LegacyOperationalPolicyMapperTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019-2020 Nordix Foundation.
4  *  Modifications Copyright (C) 2019-2020 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
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.models.tosca.legacy.mapping;
23
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;
28
29 import java.util.LinkedHashMap;
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.coder.YamlJsonTranslator;
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.authorative.concepts.ToscaServiceTemplate;
38 import org.onap.policy.models.tosca.legacy.concepts.LegacyOperationalPolicy;
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.onap.policy.models.tosca.utils.ToscaServiceTemplateUtils;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
46
47 /**
48  * Test serialization of monitoring policies.
49  *
50  * @author Liam Fallon (liam.fallon@est.tech)
51  */
52 public class LegacyOperationalPolicyMapperTest {
53     // Logger for this class
54     private static final Logger LOGGER = LoggerFactory.getLogger(LegacyOperationalPolicyMapperTest.class);
55
56     private StandardCoder standardCoder;
57     private YamlJsonTranslator yamlJsonTranslator = new YamlJsonTranslator();
58
59     @Before
60     public void setUp() {
61         standardCoder = new StandardCoder();
62     }
63
64     @Test
65     public void testJsonDeserialization() throws Exception {
66         String policyTypeInputJson =
67                 ResourceUtils.getResourceAsString("policytypes/onap.policies.controlloop.Operational.yaml");
68         ToscaServiceTemplate policyTypes = yamlJsonTranslator.fromYaml(policyTypeInputJson, ToscaServiceTemplate.class);
69
70         JpaToscaServiceTemplate policyTypeServiceTemplate = new JpaToscaServiceTemplate();
71         policyTypeServiceTemplate.fromAuthorative(policyTypes);
72
73         String vcpePolicyJson = ResourceUtils.getResourceAsString("policies/vCPE.policy.operational.legacy.input.json");
74         LegacyOperationalPolicy legacyOperationalPolicy =
75                 standardCoder.decode(vcpePolicyJson, LegacyOperationalPolicy.class);
76
77         JpaToscaServiceTemplate legacyPolicyFragmentServiceTemplate =
78                 new LegacyOperationalPolicyMapper().toToscaServiceTemplate(legacyOperationalPolicy);
79
80         JpaToscaServiceTemplate serviceTemplate =
81                 ToscaServiceTemplateUtils.addFragment(policyTypeServiceTemplate, legacyPolicyFragmentServiceTemplate);
82
83         assertNotNull(serviceTemplate);
84         LOGGER.info(serviceTemplate.validate(new PfValidationResult()).toString());
85         assertTrue(serviceTemplate.validate(new PfValidationResult()).isValid());
86
87         assertEquals("operational.restart:1.0.0",
88                 serviceTemplate.getTopologyTemplate().getPolicies().get("operational.restart").getId());
89     }
90
91     @Test
92     public void testOperationalPolicyMapper() {
93         JpaToscaServiceTemplate serviceTemplate = new JpaToscaServiceTemplate();
94         serviceTemplate.setTopologyTemplate(new JpaToscaTopologyTemplate());
95         serviceTemplate.getTopologyTemplate().setPolicies(new JpaToscaPolicies());
96
97         JpaToscaPolicy policy0 = new JpaToscaPolicy(new PfConceptKey("PolicyName0", "0.0.1"));
98         serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().put(policy0.getKey(), policy0);
99         JpaToscaPolicy policy1 = new JpaToscaPolicy(new PfConceptKey("PolicyName1", "0.0.1"));
100         serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().put(policy1.getKey(), policy1);
101
102         assertThatThrownBy(() -> {
103             new LegacyOperationalPolicyMapper().fromToscaServiceTemplate(serviceTemplate);
104         }).hasMessage("more than one policy found in service template");
105
106         serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().remove(policy1.getKey());
107
108         policy0.setProperties(null);
109         assertThatThrownBy(() -> {
110             new LegacyOperationalPolicyMapper().fromToscaServiceTemplate(serviceTemplate);
111         }).hasMessage("no properties defined on TOSCA policy");
112
113         policy0.setProperties(new LinkedHashMap<>());
114         assertThatThrownBy(() -> {
115             new LegacyOperationalPolicyMapper().fromToscaServiceTemplate(serviceTemplate);
116         }).hasMessage("property \"content\" not defined on TOSCA policy");
117     }
118 }