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
10 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
20 package org.onap.policy.models.tosca.simple.serialization;
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;
27 import java.util.List;
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;
44 class OptimizationPolicyTypeSerializationTest {
46 private static final String TYPE_ROOT = "tosca.policies.Root";
47 private static final String VERSION = "1.0.0";
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";
54 private StandardCoder coder;
58 coder = new StandardCoder();
62 void testOptimization() throws CoderException {
63 JpaToscaServiceTemplate svctmpl = loadYaml(INPUT_OPTIMIZATION_YAML);
64 validate("initial object", svctmpl, TYPE_ROOT, "onap.policies.Optimization", false, false);
66 String ser = serialize(svctmpl);
67 JpaToscaServiceTemplate svctmpl2 = deserialize(ser);
68 validate("copy", svctmpl2, TYPE_ROOT, "onap.policies.Optimization", false, false);
70 assertEquals(svctmpl, svctmpl2);
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,
79 String ser = serialize(svctmpl);
80 JpaToscaServiceTemplate svctmpl2 = deserialize(ser);
81 validate("copy", svctmpl2, "onap.policies.Optimization", "onap.policies.optimization.Resource", true, true);
83 assertEquals(svctmpl, svctmpl2);
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,
92 String ser = serialize(svctmpl);
93 JpaToscaServiceTemplate svctmpl2 = deserialize(ser);
94 validate("copy", svctmpl2, "onap.policies.Optimization", "onap.policies.optimization.Service", false, true);
96 assertEquals(svctmpl, svctmpl2);
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);
107 private JpaToscaServiceTemplate deserialize(String json) throws CoderException {
108 ToscaServiceTemplate auth = coder.decode(json, ToscaServiceTemplate.class);
110 JpaToscaServiceTemplate svctmpl = new JpaToscaServiceTemplate();
111 svctmpl.fromAuthorative(auth);
115 private String serialize(JpaToscaServiceTemplate svctmpl) throws CoderException {
116 ToscaServiceTemplate auth = svctmpl.toAuthorative();
117 return coder.encode(auth);
120 private void validate(String testnm, JpaToscaServiceTemplate svctmpl, String derivedFrom, String typeName,
121 boolean checkResource, boolean checkService) {
122 JpaToscaPolicyTypes policyTypes = svctmpl.getPolicyTypes();
124 assertEquals(1, policyTypes.getConceptMap().size(), testnm + " type count");
125 JpaToscaPolicyType policyType = policyTypes.getConceptMap().values().iterator().next();
127 assertEquals(typeName, policyType.getName(), testnm + " name");
128 assertEquals(VERSION, policyType.getVersion(), testnm + " version");
130 assertNotNull(String.valueOf(policyType.getDerivedFrom()), testnm + " derived from");
131 assertEquals(derivedFrom, policyType.getDerivedFrom().getName(), testnm + " derived from name");
133 Map<String, JpaToscaProperty> props = policyType.getProperties();
134 assertNotNull(props.toString(), testnm + " properties");
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"));
142 validateScope(testnm, props.get("scope"));
143 validateGeography(testnm, props.get("geography"));
144 validateIdentity(testnm, props.get("identity"));
148 // only need to validate deep match of one of these; geography is the most interesting
150 private void validateScope(String testName, JpaToscaProperty prop) {
151 String testnm = testName + " scope";
153 assertNotNull(prop, testnm);
154 validateMatchable(testnm, prop.getMetadata());
157 private void validateServices(String testName, JpaToscaProperty prop) {
158 String testnm = testName + " services";
160 assertNotNull(prop, testnm);
161 validateMatchable(testnm, prop.getMetadata());
164 private void validateResources(String testName, JpaToscaProperty prop) {
165 String testnm = testName + " resources";
167 assertNotNull(prop, testnm);
168 validateMatchable(testnm, prop.getMetadata());
171 private void validateGeography(String testName, JpaToscaProperty prop) {
172 String testnm = testName + " geography";
174 assertNotNull(prop, testnm);
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");
182 List<JpaToscaConstraint> constraints = prop.getEntrySchema().getConstraints();
183 assertNotNull(constraints, testnm + " constraints");
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);
189 assertEquals("[US, International]", constraint.getValidValues().toString(), testnm + " valid values");
192 private void validateIdentity(String testName, JpaToscaProperty prop) {
193 String testnm = testName + " identity";
195 assertNotNull(prop, testnm);
196 assertTrue(MapUtils.isEmpty(prop.getMetadata()), testnm + " metadata");
199 private void validateMatchable(String testName, Map<String, String> metadata) {
200 String testnm = testName + " matchable";
202 assertNotNull(metadata, testnm + " metadata");
203 assertEquals("true", metadata.get("matchable"), testnm + " value");