2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019 Nordix Foundation.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.models.tosca.simple.serialization;
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertNotNull;
25 import static org.junit.Assert.assertTrue;
27 import com.google.gson.Gson;
28 import com.google.gson.GsonBuilder;
29 import com.google.gson.JsonSyntaxException;
31 import java.io.IOException;
33 import org.junit.Before;
34 import org.junit.Test;
35 import org.onap.policy.common.utils.resources.ResourceUtils;
36 import org.onap.policy.models.base.PfValidationResult;
37 import org.onap.policy.models.tosca.simple.concepts.ToscaServiceTemplate;
38 import org.onap.policy.models.tosca.simple.serialization.ToscaServiceTemplateMessageBodyHandler;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41 import org.yaml.snakeyaml.Yaml;
44 * Test serialization of monitoring policies.
46 * @author Liam Fallon (liam.fallon@est.tech)
48 public class MonitoringPolicySerializationTest {
49 // Logger for this class
50 private static final Logger LOGGER = LoggerFactory.getLogger(MonitoringPolicySerializationTest.class);
56 gson = new ToscaServiceTemplateMessageBodyHandler().getGson();
60 public void testJsonDeserialization() throws JsonSyntaxException, IOException {
61 String vcpePolicyJson = ResourceUtils.getResourceAsString("policies/vCPE.policy.monitoring.input.tosca.json");
63 ToscaServiceTemplate serviceTemplate = gson.fromJson(vcpePolicyJson, ToscaServiceTemplate.class);
64 assertNotNull(serviceTemplate);
65 LOGGER.info(serviceTemplate.validate(new PfValidationResult()).toString());
66 assertTrue(serviceTemplate.validate(new PfValidationResult()).isValid());
68 assertEquals("onap.restart.tca:1.0.0",
69 serviceTemplate.getTopologyTemplate().getPolicies().get("onap.restart.tca").getId());
71 String reserializedString = gson.toJson(serviceTemplate, ToscaServiceTemplate.class);
72 assertEquals(vcpePolicyJson.replaceAll("\\s+", ""), reserializedString.replaceAll("\\s+", ""));
74 ToscaServiceTemplate serviceTemplate2 = gson.fromJson(reserializedString, ToscaServiceTemplate.class);
75 assertNotNull(serviceTemplate2);
76 assertEquals(serviceTemplate, serviceTemplate2);
80 public void testYamlDeserialization() throws JsonSyntaxException, IOException {
81 Yaml yaml = new Yaml();
83 String vcpePolicyYaml = ResourceUtils.getResourceAsString("policies/vCPE.policy.monitoring.input.tosca.yaml");
84 Object yamlObject = yaml.load(vcpePolicyYaml);
86 String yamlAsJsonString = new GsonBuilder().setPrettyPrinting().create().toJson(yamlObject);
88 ToscaServiceTemplate serviceTemplate = gson.fromJson(yamlAsJsonString, ToscaServiceTemplate.class);
90 assertNotNull(serviceTemplate);
91 LOGGER.info(serviceTemplate.validate(new PfValidationResult()).toString());
92 assertTrue(serviceTemplate.validate(new PfValidationResult()).isValid());
94 assertEquals("onap.restart.tca:1.0.0",
95 serviceTemplate.getTopologyTemplate().getPolicies().get("onap.restart.tca").getId());
97 String reserializedString = gson.toJson(serviceTemplate, ToscaServiceTemplate.class);
98 assertEquals(yamlAsJsonString.replaceAll("\\s+", ""), reserializedString.replaceAll("\\s+", ""));
100 ToscaServiceTemplate serviceTemplate2 = gson.fromJson(reserializedString, ToscaServiceTemplate.class);
101 assertNotNull(serviceTemplate2);
102 assertEquals(serviceTemplate, serviceTemplate2);