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