Update policy committers in policy/models
[policy/models.git] / models-tosca / src / test / java / org / onap / policy / models / tosca / simple / serialization / OptimizationPolicyTypeSerializationTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
4  * Modifications Copyright (C) 2020, 2024 Nordix Foundation.
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  * ============LICENSE_END=========================================================
18  */
19
20 package org.onap.policy.models.tosca.simple.serialization;
21
22 import static org.junit.jupiter.api.Assertions.assertEquals;
23 import static org.junit.jupiter.api.Assertions.assertInstanceOf;
24 import static org.junit.jupiter.api.Assertions.assertNotNull;
25 import static org.junit.jupiter.api.Assertions.assertTrue;
26
27 import java.util.List;
28 import java.util.Map;
29 import org.apache.commons.collections4.MapUtils;
30 import org.junit.jupiter.api.BeforeEach;
31 import org.junit.jupiter.api.Test;
32 import org.onap.policy.common.utils.coder.CoderException;
33 import org.onap.policy.common.utils.coder.StandardCoder;
34 import org.onap.policy.common.utils.resources.ResourceUtils;
35 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
36 import org.onap.policy.models.tosca.simple.concepts.JpaToscaConstraint;
37 import org.onap.policy.models.tosca.simple.concepts.JpaToscaConstraintValidValues;
38 import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicyType;
39 import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicyTypes;
40 import org.onap.policy.models.tosca.simple.concepts.JpaToscaProperty;
41 import org.onap.policy.models.tosca.simple.concepts.JpaToscaServiceTemplate;
42 import org.yaml.snakeyaml.Yaml;
43
44 class OptimizationPolicyTypeSerializationTest {
45
46     private static final String TYPE_ROOT = "tosca.policies.Root";
47     private static final String VERSION = "1.0.0";
48
49     private static final String INPUT_OPTIMIZATION_YAML = "policytypes/onap.policies.Optimization.yaml";
50     private static final String INPUT_OPTIMIZATION_RESOURCE_YAML =
51             "policytypes/onap.policies.optimization.Resource.yaml";
52     private static final String INPUT_OPTIMIZATION_SERVICE_YAML = "policytypes/onap.policies.optimization.Service.yaml";
53
54     private StandardCoder coder;
55
56     @BeforeEach
57     void setUp() {
58         coder = new StandardCoder();
59     }
60
61     @Test
62     void testOptimization() throws CoderException {
63         JpaToscaServiceTemplate svctmpl = loadYaml(INPUT_OPTIMIZATION_YAML);
64         validate("initial object", svctmpl, TYPE_ROOT, "onap.policies.Optimization", false, false);
65
66         String ser = serialize(svctmpl);
67         JpaToscaServiceTemplate svctmpl2 = deserialize(ser);
68         validate("copy", svctmpl2, TYPE_ROOT, "onap.policies.Optimization", false, false);
69
70         assertEquals(svctmpl, svctmpl2);
71     }
72
73     @Test
74     void testOptimizationResource() throws CoderException {
75         JpaToscaServiceTemplate svctmpl = loadYaml(INPUT_OPTIMIZATION_RESOURCE_YAML);
76         validate("initial object", svctmpl, "onap.policies.Optimization", "onap.policies.optimization.Resource", true,
77                 true);
78
79         String ser = serialize(svctmpl);
80         JpaToscaServiceTemplate svctmpl2 = deserialize(ser);
81         validate("copy", svctmpl2, "onap.policies.Optimization", "onap.policies.optimization.Resource", true, true);
82
83         assertEquals(svctmpl, svctmpl2);
84     }
85
86     @Test
87     void testOptimizationService() throws CoderException {
88         JpaToscaServiceTemplate svctmpl = loadYaml(INPUT_OPTIMIZATION_SERVICE_YAML);
89         validate("initial object", svctmpl, "onap.policies.Optimization", "onap.policies.optimization.Service", false,
90                 true);
91
92         String ser = serialize(svctmpl);
93         JpaToscaServiceTemplate svctmpl2 = deserialize(ser);
94         validate("copy", svctmpl2, "onap.policies.Optimization", "onap.policies.optimization.Service", false, true);
95
96         assertEquals(svctmpl, svctmpl2);
97     }
98
99     private JpaToscaServiceTemplate loadYaml(String yamlFileName) throws CoderException {
100         Yaml yaml = new Yaml();
101         String policyTypeYaml = ResourceUtils.getResourceAsString(yamlFileName);
102         Object yamlObject = yaml.load(policyTypeYaml);
103         String yamlAsJsonString = coder.encode(yamlObject);
104         return deserialize(yamlAsJsonString);
105     }
106
107     private JpaToscaServiceTemplate deserialize(String json) throws CoderException {
108         ToscaServiceTemplate auth = coder.decode(json, ToscaServiceTemplate.class);
109
110         JpaToscaServiceTemplate svctmpl = new JpaToscaServiceTemplate();
111         svctmpl.fromAuthorative(auth);
112         return svctmpl;
113     }
114
115     private String serialize(JpaToscaServiceTemplate svctmpl) throws CoderException {
116         ToscaServiceTemplate auth = svctmpl.toAuthorative();
117         return coder.encode(auth);
118     }
119
120     private void validate(String testnm, JpaToscaServiceTemplate svctmpl, String derivedFrom, String typeName,
121             boolean checkResource, boolean checkService) {
122         JpaToscaPolicyTypes policyTypes = svctmpl.getPolicyTypes();
123
124         assertEquals(1, policyTypes.getConceptMap().size(), testnm + " type count");
125         JpaToscaPolicyType policyType = policyTypes.getConceptMap().values().iterator().next();
126
127         assertEquals(typeName, policyType.getName(), testnm + " name");
128         assertEquals(VERSION, policyType.getVersion(), testnm + " version");
129
130         assertNotNull(String.valueOf(policyType.getDerivedFrom()), testnm + " derived from");
131         assertEquals(derivedFrom, policyType.getDerivedFrom().getName(), testnm + " derived from name");
132
133         Map<String, JpaToscaProperty> props = policyType.getProperties();
134         assertNotNull(props.toString(), testnm + " properties");
135
136         if (checkResource && checkService) {
137             validateResources(testnm, props.get("resources"));
138             validateServices(testnm, props.get("services"));
139         } else if (checkService && !checkResource) {
140             validateServices(testnm, props.get("services"));
141         } else {
142             validateScope(testnm, props.get("scope"));
143             validateGeography(testnm, props.get("geography"));
144             validateIdentity(testnm, props.get("identity"));
145         }
146     }
147
148     // only need to validate deep match of one of these; geography is the most interesting
149
150     private void validateScope(String testName, JpaToscaProperty prop) {
151         String testnm = testName + " scope";
152
153         assertNotNull(prop, testnm);
154         validateMatchable(testnm, prop.getMetadata());
155     }
156
157     private void validateServices(String testName, JpaToscaProperty prop) {
158         String testnm = testName + " services";
159
160         assertNotNull(prop, testnm);
161         validateMatchable(testnm, prop.getMetadata());
162     }
163
164     private void validateResources(String testName, JpaToscaProperty prop) {
165         String testnm = testName + " resources";
166
167         assertNotNull(prop, testnm);
168         validateMatchable(testnm, prop.getMetadata());
169     }
170
171     private void validateGeography(String testName, JpaToscaProperty prop) {
172         String testnm = testName + " geography";
173
174         assertNotNull(prop, testnm);
175
176         assertEquals("One or more geographic regions", prop.getDescription(), testnm + " description");
177         assertEquals("list", prop.getType().getName(), testnm + " description");
178         validateMatchable(testnm, prop.getMetadata());
179         assertTrue(prop.isRequired(), testnm + " required");
180         assertEquals("string", prop.getEntrySchema().getType().getName(), testnm + " description");
181
182         List<JpaToscaConstraint> constraints = prop.getEntrySchema().getConstraints();
183         assertNotNull(constraints, testnm + " constraints");
184
185         assertEquals(1, constraints.size(), testnm + " constraint size");
186         assertInstanceOf(JpaToscaConstraintValidValues.class, constraints.get(0), testnm + " constraint type");
187         JpaToscaConstraintValidValues constraint = (JpaToscaConstraintValidValues) constraints.get(0);
188
189         assertEquals("[US, International]", constraint.getValidValues().toString(), testnm + " valid values");
190     }
191
192     private void validateIdentity(String testName, JpaToscaProperty prop) {
193         String testnm = testName + " identity";
194
195         assertNotNull(prop, testnm);
196         assertTrue(MapUtils.isEmpty(prop.getMetadata()), testnm + " metadata");
197     }
198
199     private void validateMatchable(String testName, Map<String, String> metadata) {
200         String testnm = testName + " matchable";
201
202         assertNotNull(metadata, testnm + " metadata");
203         assertEquals("true", metadata.get("matchable"), testnm + " value");
204     }
205 }