Merge "Fix simple sonar issues in models: errors to sim-pdp"
[policy/models.git] / models-tosca / src / test / java / org / onap / policy / models / tosca / simple / serialization / MonitoringPolicySerializationTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019 Nordix Foundation.
4  *  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
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.simple.serialization;
23
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertNotNull;
26 import static org.junit.Assert.assertTrue;
27 import static org.junit.Assert.fail;
28
29 import com.google.gson.JsonArray;
30 import com.google.gson.JsonObject;
31 import com.google.gson.JsonParser;
32
33 import java.util.Map;
34 import org.junit.Before;
35 import org.junit.Test;
36 import org.onap.policy.common.utils.coder.CoderException;
37 import org.onap.policy.common.utils.coder.StandardCoder;
38 import org.onap.policy.common.utils.resources.ResourceUtils;
39 import org.onap.policy.models.base.PfConceptKey;
40 import org.onap.policy.models.base.PfValidationResult;
41 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
42 import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicy;
43 import org.onap.policy.models.tosca.simple.concepts.JpaToscaServiceTemplate;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
46 import org.yaml.snakeyaml.Yaml;
47
48 /**
49  * Test serialization of monitoring policies.
50  *
51  * @author Liam Fallon (liam.fallon@est.tech)
52  * @author Chenfei Gao (cgao@research.att.com)
53  */
54 public class MonitoringPolicySerializationTest {
55
56     private static final String VERSION = "version";
57
58     private static final String YAML_VERSION = "tosca_simple_yaml_1_0_0";
59
60     private static final String DEFINITION_VERSION = "tosca_definitions_version";
61
62     private static final String TOPOLOGY_TEMPLATE = "topology_template";
63
64     private static final String TCA_POLICY = "tca_policy";
65
66     private static final String PROPERTIES2 = "properties";
67
68     private static final String POLICY_ID = "policy-id";
69
70     private static final String POLICIES = "policies";
71
72     private static final String POLICY3 = "onap.vfirewall.tca";
73
74     private static final String POLICY2 = "onap.scaleout.tca";
75
76     private static final String POLICY1 = "onap.restart.tca";
77
78     private static final String TYPE1 = "onap.policies.monitoring.cdap.tca.hi.lo.app";
79
80     private static final String METADATA = "metadata";
81
82     private static final String VERSION_100 = "1.0.0";
83
84     private static final Logger LOGGER = LoggerFactory.getLogger(MonitoringPolicySerializationTest.class);
85
86     private static final String VCPE_MON_INPUT_JSON = "policies/vCPE.policy.monitoring.input.tosca.json";
87     private static final String VCPE_MON_INPUT_YAML = "policies/vCPE.policy.monitoring.input.tosca.yaml";
88     private static final String VDNS_MON_INPUT_JSON = "policies/vDNS.policy.monitoring.input.tosca.json";
89     private static final String VDNS_MON_INPUT_YAML = "policies/vDNS.policy.monitoring.input.tosca.yaml";
90     private static final String VFW_MON_INPUT_JSON = "policies/vFirewall.policy.monitoring.input.tosca.json";
91     private static final String VFW_MON_INPUT_YAML = "policies/vFirewall.policy.monitoring.input.tosca.yaml";
92
93     private StandardCoder standardCoder;
94
95     @Before
96     public void setUp() {
97         standardCoder = new StandardCoder();
98     }
99
100     @Test
101     public void testDeserialization() throws Exception {
102         // vCPE
103         JpaToscaServiceTemplate serviceTemplateFromJson = deserializeMonitoringInputJson(VCPE_MON_INPUT_JSON);
104         verifyVcpeMonitoringInputDeserialization(serviceTemplateFromJson);
105         JpaToscaServiceTemplate serviceTemplateFromYaml = deserializeMonitoringInputYaml(VCPE_MON_INPUT_YAML);
106         assertTrue(serviceTemplateFromJson.compareTo(serviceTemplateFromYaml) == 0);
107
108         // vDNS
109         serviceTemplateFromJson = deserializeMonitoringInputJson(VDNS_MON_INPUT_JSON);
110         verifyVdnsMonitoringInputDeserialization(serviceTemplateFromJson);
111         serviceTemplateFromYaml = deserializeMonitoringInputYaml(VDNS_MON_INPUT_YAML);
112         assertTrue(serviceTemplateFromJson.compareTo(serviceTemplateFromYaml) == 0);
113
114         // vFirewall
115         serviceTemplateFromJson = deserializeMonitoringInputJson(VFW_MON_INPUT_JSON);
116         verifyVfwMonitoringInputDeserialization(serviceTemplateFromJson);
117         serviceTemplateFromYaml = deserializeMonitoringInputYaml(VFW_MON_INPUT_YAML);
118         assertTrue(serviceTemplateFromJson.compareTo(serviceTemplateFromYaml) == 0);
119     }
120
121     @Test
122     public void testSerialization() {
123         try {
124             // vCPE
125             JpaToscaServiceTemplate serviceTemplate = deserializeMonitoringInputJson(VCPE_MON_INPUT_JSON);
126             String serializedServiceTemplate = serializeMonitoringServiceTemplate(serviceTemplate);
127             verifyVcpeMonitoringOutputserialization(serializedServiceTemplate);
128
129             // vDNS
130             serviceTemplate = deserializeMonitoringInputJson(VDNS_MON_INPUT_JSON);
131             serializedServiceTemplate = serializeMonitoringServiceTemplate(serviceTemplate);
132             verifyVdnsMonitoringOutputserialization(serializedServiceTemplate);
133
134             // vFirewall
135             serviceTemplate = deserializeMonitoringInputJson(VFW_MON_INPUT_JSON);
136             serializedServiceTemplate = serializeMonitoringServiceTemplate(serviceTemplate);
137             verifyVfwMonitoringOutputserialization(serializedServiceTemplate);
138
139         } catch (Exception e) {
140             LOGGER.warn("No exception should be thrown", e);
141             fail("No exception should be thrown");
142         }
143     }
144
145     private JpaToscaServiceTemplate deserializeMonitoringInputJson(String resourcePath)
146             throws Exception {
147
148         String policyJson = ResourceUtils.getResourceAsString(resourcePath);
149         ToscaServiceTemplate serviceTemplate = standardCoder.decode(policyJson, ToscaServiceTemplate.class);
150         JpaToscaServiceTemplate jpaToscaServiceTemplate = new JpaToscaServiceTemplate();
151         jpaToscaServiceTemplate.fromAuthorative(serviceTemplate);
152         return jpaToscaServiceTemplate;
153     }
154
155     private JpaToscaServiceTemplate deserializeMonitoringInputYaml(String resourcePath)
156             throws Exception {
157
158         Yaml yaml = new Yaml();
159         String policyYaml = ResourceUtils.getResourceAsString(resourcePath);
160         Object yamlObject = yaml.load(policyYaml);
161         String yamlAsJsonString = new StandardCoder().encode(yamlObject);
162         ToscaServiceTemplate serviceTemplate = standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class);
163
164         JpaToscaServiceTemplate jpaToscaServiceTemplate = new JpaToscaServiceTemplate();
165         jpaToscaServiceTemplate.fromAuthorative(serviceTemplate);
166         return jpaToscaServiceTemplate;
167     }
168
169     private String serializeMonitoringServiceTemplate(JpaToscaServiceTemplate serviceTemplate) throws CoderException {
170         return standardCoder.encode(serviceTemplate.toAuthorative());
171     }
172
173     private void verifyVcpeMonitoringInputDeserialization(JpaToscaServiceTemplate serviceTemplate) {
174
175         // Sanity check the entire structure
176         assertNotNull(serviceTemplate);
177         LOGGER.info(serviceTemplate.validate(new PfValidationResult()).toString());
178         assertTrue(serviceTemplate.validate(new PfValidationResult()).isValid());
179
180         // Check tosca_definitions_version
181         assertEquals(YAML_VERSION,
182                 serviceTemplate.getToscaDefinitionsVersion());
183
184         Map<PfConceptKey, JpaToscaPolicy> policiesConceptMap = serviceTemplate.getTopologyTemplate()
185                 .getPolicies().getConceptMap();
186
187         // Check policies
188         assertTrue(policiesConceptMap.size() == 1);
189         assertEquals(POLICY1, policiesConceptMap.keySet().iterator().next().getName());
190         assertEquals("onap.restart.tca:1.0.0",
191                 serviceTemplate.getTopologyTemplate().getPolicies().get(POLICY1).getId());
192
193         JpaToscaPolicy policyVal = policiesConceptMap.values().iterator().next();
194
195         // Check metadata
196         assertTrue(policyVal.getMetadata().size() == 2);
197         assertEquals(POLICY_ID, policyVal.getMetadata().entrySet().iterator().next().getKey());
198         assertEquals(POLICY1, policyVal.getMetadata().entrySet().iterator().next().getValue());
199
200         // Check properties
201         assertTrue(policiesConceptMap.values().iterator().next().getProperties().size() == 1);
202         assertEquals(TCA_POLICY, policyVal.getProperties().keySet().iterator().next());
203         assertNotNull(policyVal.getProperties().values().iterator().next());
204     }
205
206     private void verifyVdnsMonitoringInputDeserialization(JpaToscaServiceTemplate serviceTemplate) {
207
208         // Sanity check the entire structure
209         assertNotNull(serviceTemplate);
210         LOGGER.info(serviceTemplate.validate(new PfValidationResult()).toString());
211         assertTrue(serviceTemplate.validate(new PfValidationResult()).isValid());
212
213         // Check tosca_definitions_version
214         assertEquals(YAML_VERSION,
215                 serviceTemplate.getToscaDefinitionsVersion());
216
217         Map<PfConceptKey, JpaToscaPolicy> policiesConceptMap = serviceTemplate.getTopologyTemplate()
218                 .getPolicies().getConceptMap();
219
220         // Check policies
221         assertTrue(policiesConceptMap.size() == 1);
222         assertEquals(POLICY2, policiesConceptMap.keySet().iterator().next().getName());
223         assertEquals("onap.scaleout.tca:1.0.0",
224                 serviceTemplate.getTopologyTemplate().getPolicies().get(POLICY2).getId());
225
226         JpaToscaPolicy policyVal = policiesConceptMap.values().iterator().next();
227
228         // Check metadata
229         assertTrue(policyVal.getMetadata().size() == 2);
230         assertEquals(POLICY_ID, policyVal.getMetadata().entrySet().iterator().next().getKey());
231         assertEquals(POLICY2, policyVal.getMetadata().entrySet().iterator().next().getValue());
232
233         // Check properties
234         assertTrue(policiesConceptMap.values().iterator().next().getProperties().size() == 1);
235         assertEquals(TCA_POLICY, policyVal.getProperties().keySet().iterator().next());
236         assertNotNull(policyVal.getProperties().values().iterator().next());
237     }
238
239     private void verifyVfwMonitoringInputDeserialization(JpaToscaServiceTemplate serviceTemplate) {
240
241         // Sanity check the entire structure
242         assertNotNull(serviceTemplate);
243         LOGGER.info(serviceTemplate.validate(new PfValidationResult()).toString());
244         assertTrue(serviceTemplate.validate(new PfValidationResult()).isValid());
245
246         // Check tosca_definitions_version
247         assertEquals(YAML_VERSION,
248                 serviceTemplate.getToscaDefinitionsVersion());
249
250         Map<PfConceptKey, JpaToscaPolicy> policiesConceptMap = serviceTemplate.getTopologyTemplate()
251                 .getPolicies().getConceptMap();
252
253         // Check policies
254         assertTrue(policiesConceptMap.size() == 1);
255         assertEquals(POLICY3, policiesConceptMap.keySet().iterator().next().getName());
256         assertEquals("onap.vfirewall.tca:1.0.0",
257                 serviceTemplate.getTopologyTemplate().getPolicies().get(POLICY3).getId());
258
259         JpaToscaPolicy policyVal = policiesConceptMap.values().iterator().next();
260
261         // Check metadata
262         assertTrue(policyVal.getMetadata().size() == 2);
263         assertEquals(POLICY_ID, policyVal.getMetadata().entrySet().iterator().next().getKey());
264         assertEquals(POLICY3, policyVal.getMetadata().entrySet().iterator().next().getValue());
265
266         // Check properties
267         assertTrue(policiesConceptMap.values().iterator().next().getProperties().size() == 1);
268         assertEquals(TCA_POLICY, policyVal.getProperties().keySet().iterator().next());
269         assertNotNull(policyVal.getProperties().values().iterator().next());
270     }
271
272     private void verifyVcpeMonitoringOutputserialization(String serializedServiceTemplate) {
273
274         JsonObject serviceTemplateJsonObject = new JsonParser().parse(serializedServiceTemplate).getAsJsonObject();
275         assertEquals(YAML_VERSION, serviceTemplateJsonObject.get(DEFINITION_VERSION)
276                 .getAsString());
277         JsonObject topologyTemplateJsonObject = serviceTemplateJsonObject.get(TOPOLOGY_TEMPLATE)
278                 .getAsJsonObject();
279         JsonArray policiesJsonArray = topologyTemplateJsonObject.get(POLICIES).getAsJsonArray();
280         assertTrue(policiesJsonArray.size() == 1);
281         JsonObject policy = policiesJsonArray.iterator().next().getAsJsonObject();
282         assertNotNull(policy.get(POLICY1));
283         JsonObject policyVal = policy.get(POLICY1).getAsJsonObject();
284         assertEquals(TYPE1, policyVal.get("type").getAsString());
285         assertEquals(VERSION_100, policyVal.get(VERSION).getAsString());
286         assertEquals(POLICY1, policyVal.get(METADATA).getAsJsonObject().get(POLICY_ID)
287                 .getAsString());
288         JsonObject properties = policyVal.get(PROPERTIES2).getAsJsonObject();
289         assertNotNull(properties.get(TCA_POLICY));
290     }
291
292     private void verifyVdnsMonitoringOutputserialization(String serializedServiceTemplate) {
293
294         JsonObject serviceTemplateJsonObject = new JsonParser().parse(serializedServiceTemplate).getAsJsonObject();
295         assertEquals(YAML_VERSION, serviceTemplateJsonObject.get(DEFINITION_VERSION)
296                 .getAsString());
297         JsonObject topologyTemplateJsonObject = serviceTemplateJsonObject.get(TOPOLOGY_TEMPLATE).getAsJsonObject();
298         JsonArray policiesJsonArray = topologyTemplateJsonObject.get(POLICIES).getAsJsonArray();
299         assertTrue(policiesJsonArray.size() == 1);
300         JsonObject policy = policiesJsonArray.iterator().next().getAsJsonObject();
301         assertNotNull(policy.get(POLICY2));
302         JsonObject policyVal = policy.get(POLICY2).getAsJsonObject();
303         assertEquals(TYPE1, policyVal.get("type").getAsString());
304         assertEquals(VERSION_100, policyVal.get(VERSION).getAsString());
305         assertEquals(POLICY2, policyVal.get(METADATA).getAsJsonObject().get(POLICY_ID)
306                 .getAsString());
307         JsonObject properties = policyVal.get(PROPERTIES2).getAsJsonObject();
308         assertNotNull(properties.get(TCA_POLICY));
309     }
310
311     private void verifyVfwMonitoringOutputserialization(String serializedServiceTemplate) {
312
313         JsonObject serviceTemplateJsonObject = new JsonParser().parse(serializedServiceTemplate).getAsJsonObject();
314         assertEquals(YAML_VERSION, serviceTemplateJsonObject.get(DEFINITION_VERSION)
315                 .getAsString());
316         JsonObject topologyTemplateJsonObject = serviceTemplateJsonObject.get(TOPOLOGY_TEMPLATE).getAsJsonObject();
317         JsonArray policiesJsonArray = topologyTemplateJsonObject.get(POLICIES).getAsJsonArray();
318         assertTrue(policiesJsonArray.size() == 1);
319         JsonObject policy = policiesJsonArray.iterator().next().getAsJsonObject();
320         assertNotNull(policy.get(POLICY3));
321         JsonObject policyVal = policy.get(POLICY3).getAsJsonObject();
322         assertEquals(TYPE1, policyVal.get("type").getAsString());
323         assertEquals(VERSION_100, policyVal.get(VERSION).getAsString());
324         assertEquals(POLICY3, policyVal.get(METADATA).getAsJsonObject().get(POLICY_ID)
325                 .getAsString());
326         JsonObject properties = policyVal.get(PROPERTIES2).getAsJsonObject();
327         assertNotNull(properties.get(TCA_POLICY));
328     }
329 }