2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
4 * Modifications Copyright (C) 2020 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.Assert.assertEquals;
23 import static org.junit.Assert.assertNotNull;
24 import static org.junit.Assert.assertTrue;
26 import java.util.List;
29 import org.junit.Before;
30 import org.junit.Test;
31 import org.onap.policy.common.utils.coder.CoderException;
32 import org.onap.policy.common.utils.coder.StandardCoder;
33 import org.onap.policy.common.utils.resources.ResourceUtils;
34 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
35 import org.onap.policy.models.tosca.simple.concepts.JpaToscaConstraint;
36 import org.onap.policy.models.tosca.simple.concepts.JpaToscaConstraintValidValues;
37 import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicyType;
38 import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicyTypes;
39 import org.onap.policy.models.tosca.simple.concepts.JpaToscaProperty;
40 import org.onap.policy.models.tosca.simple.concepts.JpaToscaServiceTemplate;
41 import org.yaml.snakeyaml.Yaml;
43 public class OptimizationPolicyTypeSerializationTest {
45 private static final String TYPE_ROOT = "tosca.policies.Root";
46 private static final String VERSION = "1.0.0";
48 private static final String INPUT_OPTIMIZATION_YAML = "policytypes/onap.policies.Optimization.yaml";
49 private static final String INPUT_OPTIMIZATION_RESOURCE_YAML =
50 "policytypes/onap.policies.optimization.Resource.yaml";
51 private static final String INPUT_OPTIMIZATION_SERVICE_YAML = "policytypes/onap.policies.optimization.Service.yaml";
53 private StandardCoder coder;
57 coder = new StandardCoder();
61 public void testOptimization() throws CoderException {
62 JpaToscaServiceTemplate svctmpl = loadYaml(INPUT_OPTIMIZATION_YAML);
63 validate("initial object", svctmpl, TYPE_ROOT, "onap.policies.Optimization", false, false);
65 String ser = serialize(svctmpl);
66 JpaToscaServiceTemplate svctmpl2 = deserialize(ser);
67 validate("copy", svctmpl2, TYPE_ROOT, "onap.policies.Optimization", false, false);
69 assertEquals(svctmpl, svctmpl2);
73 public void testOptimizationResource() throws CoderException {
74 JpaToscaServiceTemplate svctmpl = loadYaml(INPUT_OPTIMIZATION_RESOURCE_YAML);
75 validate("initial object", svctmpl, "onap.policies.Optimization", "onap.policies.optimization.Resource", true,
78 String ser = serialize(svctmpl);
79 JpaToscaServiceTemplate svctmpl2 = deserialize(ser);
80 validate("copy", svctmpl2, "onap.policies.Optimization", "onap.policies.optimization.Resource", true, true);
82 assertEquals(svctmpl, svctmpl2);
86 public void testOptimizationService() throws CoderException {
87 JpaToscaServiceTemplate svctmpl = loadYaml(INPUT_OPTIMIZATION_SERVICE_YAML);
88 validate("initial object", svctmpl, "onap.policies.Optimization", "onap.policies.optimization.Service", false,
91 String ser = serialize(svctmpl);
92 JpaToscaServiceTemplate svctmpl2 = deserialize(ser);
93 validate("copy", svctmpl2, "onap.policies.Optimization", "onap.policies.optimization.Service", false, true);
95 assertEquals(svctmpl, svctmpl2);
98 private JpaToscaServiceTemplate loadYaml(String yamlFileName) throws CoderException {
99 Yaml yaml = new Yaml();
100 String policyTypeYaml = ResourceUtils.getResourceAsString(yamlFileName);
101 Object yamlObject = yaml.load(policyTypeYaml);
102 String yamlAsJsonString = coder.encode(yamlObject);
103 return deserialize(yamlAsJsonString);
106 private JpaToscaServiceTemplate deserialize(String json) throws CoderException {
107 ToscaServiceTemplate auth = coder.decode(json, ToscaServiceTemplate.class);
109 JpaToscaServiceTemplate svctmpl = new JpaToscaServiceTemplate();
110 svctmpl.fromAuthorative(auth);
114 private String serialize(JpaToscaServiceTemplate svctmpl) throws CoderException {
115 ToscaServiceTemplate auth = svctmpl.toAuthorative();
116 return coder.encode(auth);
119 private void validate(String testnm, JpaToscaServiceTemplate svctmpl, String derivedFrom, String typeName,
120 boolean checkResource, boolean checkService) {
121 JpaToscaPolicyTypes policyTypes = svctmpl.getPolicyTypes();
123 assertEquals(testnm + " type count", 1, policyTypes.getConceptMap().size());
124 JpaToscaPolicyType policyType = policyTypes.getConceptMap().values().iterator().next();
126 assertEquals(testnm + " name", typeName, policyType.getName());
127 assertEquals(testnm + " version", VERSION, policyType.getVersion());
129 assertNotNull(testnm + " derived from", policyType.getDerivedFrom());
130 assertEquals(testnm + " derived from name", derivedFrom, policyType.getDerivedFrom().getName());
132 Map<String, JpaToscaProperty> props = policyType.getProperties();
133 assertNotNull(testnm + " properties", props);
135 if (checkResource && checkService) {
136 validateResources(testnm, props.get("resources"));
137 validateServices(testnm, props.get("services"));
138 } else if (checkService && !checkResource) {
139 validateServices(testnm, props.get("services"));
141 validateScope(testnm, props.get("scope"));
142 validateGeography(testnm, props.get("geography"));
143 validateIdentity(testnm, props.get("identity"));
147 // only need to validate deep match of one of these; geography is the most interesting
149 private void validateScope(String testName, JpaToscaProperty prop) {
150 String testnm = testName + " scope";
152 assertNotNull(testnm, prop);
153 validateMatchable(testnm, prop.getMetadata());
156 private void validateServices(String testName, JpaToscaProperty prop) {
157 String testnm = testName + " services";
159 assertNotNull(testnm, prop);
160 validateMatchable(testnm, prop.getMetadata());
163 private void validateResources(String testName, JpaToscaProperty prop) {
164 String testnm = testName + " resources";
166 assertNotNull(testnm, prop);
167 validateMatchable(testnm, prop.getMetadata());
170 private void validateGeography(String testName, JpaToscaProperty prop) {
171 String testnm = testName + " geography";
173 assertNotNull(testnm, prop);
175 // this line results in a stack overflow
176 // assertEquals(testnm + " name", "geography", prop.getName());
178 assertEquals(testnm + " description", "One or more geographic regions", prop.getDescription());
179 assertEquals(testnm + " type", "list", prop.getType().getName());
180 validateMatchable(testnm, prop.getMetadata());
181 assertTrue(testnm + " required", prop.isRequired());
182 assertEquals(testnm + " entry_schema", "string", prop.getEntrySchema().getType().getName());
184 List<JpaToscaConstraint> constraints = prop.getEntrySchema().getConstraints();
185 assertNotNull(testnm + " constraints", constraints);
187 assertEquals(testnm + " constraint size", 1, constraints.size());
188 assertTrue(testnm + " constraint type", constraints.get(0) instanceof JpaToscaConstraintValidValues);
189 JpaToscaConstraintValidValues constraint = (JpaToscaConstraintValidValues) constraints.get(0);
191 assertEquals(testnm + " valid values", "[US, International]", constraint.getValidValues().toString());
194 private void validateIdentity(String testName, JpaToscaProperty prop) {
195 String testnm = testName + " identity";
197 assertNotNull(testnm, prop);
198 assertEquals(testnm + " metadata", 0, prop.getMetadata().size());
201 private void validateMatchable(String testName, Map<String, String> metadata) {
202 String testnm = testName + " matchable";
204 assertNotNull(testnm + " metadata", metadata);
205 assertEquals(testnm + " value", "true", metadata.get("matchable"));