2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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 * ============LICENSE_END=========================================================
19 package org.onap.policy.models.tosca.simple.serialization;
21 import static org.junit.Assert.assertEquals;
22 import static org.junit.Assert.assertNotNull;
23 import static org.junit.Assert.assertNull;
24 import static org.junit.Assert.assertTrue;
26 import java.util.List;
28 import org.junit.Before;
29 import org.junit.Test;
30 import org.onap.policy.common.utils.coder.CoderException;
31 import org.onap.policy.common.utils.coder.StandardCoder;
32 import org.onap.policy.common.utils.resources.ResourceUtils;
33 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
34 import org.onap.policy.models.tosca.simple.concepts.JpaToscaConstraint;
35 import org.onap.policy.models.tosca.simple.concepts.JpaToscaConstraintValidValues;
36 import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicyType;
37 import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicyTypes;
38 import org.onap.policy.models.tosca.simple.concepts.JpaToscaProperty;
39 import org.onap.policy.models.tosca.simple.concepts.JpaToscaServiceTemplate;
40 import org.yaml.snakeyaml.Yaml;
42 public class OptimizationPolicyTypeSerializationTest {
44 private static final String TYPE_ROOT = "tosca.policies.Root";
45 private static final String VERSION = "1.0.0";
47 private static final String INPUT_OPTIMIZATION_YAML = "policytypes/onap.policies.Optimization.yaml";
48 private static final String INPUT_OPTIMIZATION_RESOURCE_YAML =
49 "policytypes/onap.policies.optimization.Resource.yaml";
50 private static final String INPUT_OPTIMIZATION_SERVICE_YAML = "policytypes/onap.policies.optimization.Service.yaml";
52 private StandardCoder coder;
56 coder = new StandardCoder();
60 public void testOptimization() throws CoderException {
61 JpaToscaServiceTemplate svctmpl = loadYaml(INPUT_OPTIMIZATION_YAML);
62 validate("initial object", svctmpl, TYPE_ROOT, "onap.policies.Optimization", false, false);
64 String ser = serialize(svctmpl);
65 JpaToscaServiceTemplate svctmpl2 = deserialize(ser);
66 validate("copy", svctmpl2, TYPE_ROOT, "onap.policies.Optimization", false, false);
68 assertEquals(svctmpl, svctmpl2);
72 public void testOptimizationResource() throws CoderException {
73 JpaToscaServiceTemplate svctmpl = loadYaml(INPUT_OPTIMIZATION_RESOURCE_YAML);
74 validate("initial object", svctmpl, "onap.policies.Optimization", "onap.policies.optimization.Resource", true,
77 String ser = serialize(svctmpl);
78 JpaToscaServiceTemplate svctmpl2 = deserialize(ser);
79 validate("copy", svctmpl2, "onap.policies.Optimization", "onap.policies.optimization.Resource", true, true);
81 assertEquals(svctmpl, svctmpl2);
85 public void testOptimizationService() throws CoderException {
86 JpaToscaServiceTemplate svctmpl = loadYaml(INPUT_OPTIMIZATION_SERVICE_YAML);
87 validate("initial object", svctmpl, "onap.policies.Optimization", "onap.policies.optimization.Service", false,
90 String ser = serialize(svctmpl);
91 JpaToscaServiceTemplate svctmpl2 = deserialize(ser);
92 validate("copy", svctmpl2, "onap.policies.Optimization", "onap.policies.optimization.Service", false, true);
94 assertEquals(svctmpl, svctmpl2);
97 private JpaToscaServiceTemplate loadYaml(String yamlFileName) throws CoderException {
98 Yaml yaml = new Yaml();
99 String policyTypeYaml = ResourceUtils.getResourceAsString(yamlFileName);
100 Object yamlObject = yaml.load(policyTypeYaml);
101 String yamlAsJsonString = coder.encode(yamlObject);
102 return deserialize(yamlAsJsonString);
105 private JpaToscaServiceTemplate deserialize(String json) throws CoderException {
106 ToscaServiceTemplate auth = coder.decode(json, ToscaServiceTemplate.class);
108 JpaToscaServiceTemplate svctmpl = new JpaToscaServiceTemplate();
109 svctmpl.fromAuthorative(auth);
113 private String serialize(JpaToscaServiceTemplate svctmpl) throws CoderException {
114 ToscaServiceTemplate auth = svctmpl.toAuthorative();
115 return coder.encode(auth);
118 private void validate(String testnm, JpaToscaServiceTemplate svctmpl, String derivedFrom,
119 String typeName, boolean checkResource, boolean checkService) {
120 JpaToscaPolicyTypes policyTypes = svctmpl.getPolicyTypes();
122 assertEquals(testnm + " type count", 1, policyTypes.getConceptMap().size());
123 JpaToscaPolicyType policyType = policyTypes.getConceptMap().values().iterator().next();
125 assertEquals(testnm + " name", typeName, policyType.getName());
126 assertEquals(testnm + " version", VERSION, policyType.getVersion());
128 assertNotNull(testnm + " derived from", policyType.getDerivedFrom());
129 assertEquals(testnm + " derived from name", derivedFrom, policyType.getDerivedFrom().getName());
131 Map<String, JpaToscaProperty> props = policyType.getProperties();
132 assertNotNull(testnm + " properties", props);
134 if (checkResource && checkService) {
135 validateResources(testnm, props.get("resources"));
136 validateServices(testnm, props.get("services"));
137 } else if (checkService && !checkResource) {
138 validateServices(testnm, props.get("services"));
140 validateScope(testnm, props.get("scope"));
141 validateGeography(testnm, props.get("geography"));
142 validateIdentity(testnm, props.get("identity"));
146 // only need to validate deep match of one of these; geography is the most interesting
148 private void validateScope(String testName, JpaToscaProperty prop) {
149 String testnm = testName + " scope";
151 assertNotNull(testnm, prop);
152 validateMatchable(testnm, prop.getMetadata());
155 private void validateServices(String testName, JpaToscaProperty prop) {
156 String testnm = testName + " services";
158 assertNotNull(testnm, prop);
159 validateMatchable(testnm, prop.getMetadata());
162 private void validateResources(String testName, JpaToscaProperty prop) {
163 String testnm = testName + " resources";
165 assertNotNull(testnm, prop);
166 validateMatchable(testnm, prop.getMetadata());
169 private void validateGeography(String testName, JpaToscaProperty prop) {
170 String testnm = testName + " geography";
172 assertNotNull(testnm, prop);
174 // this line results in a stack overflow
175 // assertEquals(testnm + " name", "geography", prop.getName());
177 assertEquals(testnm + " description", "One or more geographic regions", prop.getDescription());
178 assertEquals(testnm + " type", "list", prop.getType().getName());
179 validateMatchable(testnm, prop.getMetadata());
180 assertTrue(testnm + " required", prop.isRequired());
181 assertEquals(testnm + " entry_schema", "string", prop.getEntrySchema().getType().getName());
183 List<JpaToscaConstraint> constraints = prop.getEntrySchema().getConstraints();
184 assertNotNull(testnm + " constraints", constraints);
186 assertEquals(testnm + " constraint size", 1, constraints.size());
187 assertTrue(testnm + " constraint type", constraints.get(0) instanceof JpaToscaConstraintValidValues);
188 JpaToscaConstraintValidValues constraint = (JpaToscaConstraintValidValues) constraints.get(0);
190 assertEquals(testnm + " valid values", "[US, International]", constraint.getValidValues().toString());
193 private void validateIdentity(String testName, JpaToscaProperty prop) {
194 String testnm = testName + " identity";
196 assertNotNull(testnm, prop);
197 assertNull(testnm + " metadata", prop.getMetadata());
200 private void validateMatchable(String testName, Map<String, String> metadata) {
201 String testnm = testName + " matchable";
203 assertNotNull(testnm + " metadata", metadata);
204 assertEquals(testnm + " value", "true", metadata.get("matchable"));