Implement persistence test for policies
[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  * ================================================================================
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.models.tosca.simple.serialization;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertNotNull;
25 import static org.junit.Assert.assertTrue;
26
27 import com.google.gson.Gson;
28 import com.google.gson.GsonBuilder;
29 import com.google.gson.JsonSyntaxException;
30
31 import java.io.IOException;
32
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;
42
43 /**
44  * Test serialization of monitoring policies.
45  *
46  * @author Liam Fallon (liam.fallon@est.tech)
47  */
48 public class MonitoringPolicySerializationTest {
49     // Logger for this class
50     private static final Logger LOGGER = LoggerFactory.getLogger(MonitoringPolicySerializationTest.class);
51
52     private Gson gson;
53
54     @Before
55     public void setUp() {
56         gson = new ToscaServiceTemplateMessageBodyHandler().getGson();
57     }
58
59     @Test
60     public void testJsonDeserialization() throws JsonSyntaxException, IOException {
61         String vcpePolicyJson = ResourceUtils.getResourceAsString("policies/vCPE.policy.monitoring.input.tosca.json");
62
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());
67
68         assertEquals("onap.restart.tca:1.0.0",
69                 serviceTemplate.getTopologyTemplate().getPolicies().get("onap.restart.tca").getId());
70
71         String reserializedString = gson.toJson(serviceTemplate, ToscaServiceTemplate.class);
72         assertEquals(vcpePolicyJson.replaceAll("\\s+", ""), reserializedString.replaceAll("\\s+", ""));
73
74         ToscaServiceTemplate serviceTemplate2 = gson.fromJson(reserializedString, ToscaServiceTemplate.class);
75         assertNotNull(serviceTemplate2);
76         assertEquals(serviceTemplate, serviceTemplate2);
77     }
78
79     @Test
80     public void testYamlDeserialization() throws JsonSyntaxException, IOException {
81         Yaml yaml = new Yaml();
82
83         String vcpePolicyYaml = ResourceUtils.getResourceAsString("policies/vCPE.policy.monitoring.input.tosca.yaml");
84         Object yamlObject = yaml.load(vcpePolicyYaml);
85
86         String yamlAsJsonString = new GsonBuilder().setPrettyPrinting().create().toJson(yamlObject);
87
88         ToscaServiceTemplate serviceTemplate = gson.fromJson(yamlAsJsonString, ToscaServiceTemplate.class);
89
90         assertNotNull(serviceTemplate);
91         LOGGER.info(serviceTemplate.validate(new PfValidationResult()).toString());
92         assertTrue(serviceTemplate.validate(new PfValidationResult()).isValid());
93
94         assertEquals("onap.restart.tca:1.0.0",
95                 serviceTemplate.getTopologyTemplate().getPolicies().get("onap.restart.tca").getId());
96
97         String reserializedString = gson.toJson(serviceTemplate, ToscaServiceTemplate.class);
98         assertEquals(yamlAsJsonString.replaceAll("\\s+", ""), reserializedString.replaceAll("\\s+", ""));
99
100         ToscaServiceTemplate serviceTemplate2 = gson.fromJson(reserializedString, ToscaServiceTemplate.class);
101         assertNotNull(serviceTemplate2);
102         assertEquals(serviceTemplate, serviceTemplate2);
103     }
104 }