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_YAML = "policytypes/onap.policies.Optimization.yaml";
49 private StandardCoder coder;
53 coder = new StandardCoder();
57 public void test() throws CoderException {
58 JpaToscaServiceTemplate svctmpl = loadYaml(INPUT_YAML);
59 validate("initial object", svctmpl);
61 String ser = serialize(svctmpl);
62 JpaToscaServiceTemplate svctmpl2 = deserialize(ser);
63 validate("copy", svctmpl2);
65 assertEquals(svctmpl, svctmpl2);
68 private JpaToscaServiceTemplate loadYaml(String yamlFileName) throws CoderException {
69 Yaml yaml = new Yaml();
70 String policyTypeYaml = ResourceUtils.getResourceAsString(yamlFileName);
71 Object yamlObject = yaml.load(policyTypeYaml);
72 String yamlAsJsonString = coder.encode(yamlObject);
73 return deserialize(yamlAsJsonString);
76 private JpaToscaServiceTemplate deserialize(String json) throws CoderException {
77 ToscaServiceTemplate auth = coder.decode(json, ToscaServiceTemplate.class);
79 JpaToscaServiceTemplate svctmpl = new JpaToscaServiceTemplate();
80 svctmpl.fromAuthorative(auth);
84 private String serialize(JpaToscaServiceTemplate svctmpl) throws CoderException {
85 ToscaServiceTemplate auth = svctmpl.toAuthorative();
86 return coder.encode(auth);
89 private void validate(String testnm, JpaToscaServiceTemplate svctmpl) {
90 JpaToscaPolicyTypes policyTypes = svctmpl.getPolicyTypes();
92 assertEquals(testnm + " type count", 1, policyTypes.getConceptMap().size());
93 JpaToscaPolicyType policyType = policyTypes.getConceptMap().values().iterator().next();
95 assertEquals(testnm + " name", "onap.policies.Optimization", policyType.getName());
96 assertEquals(testnm + " version", VERSION, policyType.getVersion());
98 assertNotNull(testnm + " derived from", policyType.getDerivedFrom());
99 assertEquals(testnm + " derived from name", TYPE_ROOT, policyType.getDerivedFrom().getName());
101 assertEquals(testnm + " description", "The base policy type for all policies that govern optimization",
102 policyType.getDescription());
104 Map<String, JpaToscaProperty> props = policyType.getProperties();
105 assertNotNull(testnm + " properties", props);
107 validateScope(testnm, props.get("scope"));
108 validateServices(testnm, props.get("services"));
109 validateResources(testnm, props.get("resources"));
110 validateGeography(testnm, props.get("geography"));
111 validateIdentity(testnm, props.get("identity"));
114 // only need to validate deep match of one of these; geography is the most interesting
116 private void validateScope(String testName, JpaToscaProperty prop) {
117 String testnm = testName + " scope";
119 assertNotNull(testnm, prop);
120 validateMatchable(testnm, prop.getMetadata());
123 private void validateServices(String testName, JpaToscaProperty prop) {
124 String testnm = testName + " services";
126 assertNotNull(testnm, prop);
127 validateMatchable(testnm, prop.getMetadata());
130 private void validateResources(String testName, JpaToscaProperty prop) {
131 String testnm = testName + " resources";
133 assertNotNull(testnm, prop);
134 validateMatchable(testnm, prop.getMetadata());
137 private void validateGeography(String testName, JpaToscaProperty prop) {
138 String testnm = testName + " geography";
140 assertNotNull(testnm, prop);
142 // this line results in a stack overflow
143 // assertEquals(testnm + " name", "geography", prop.getName());
145 assertEquals(testnm + " description", "One or more geographic regions", prop.getDescription());
146 assertEquals(testnm + " type", "list", prop.getType().getName());
147 validateMatchable(testnm, prop.getMetadata());
148 assertTrue(testnm + " required", prop.isRequired());
149 assertEquals(testnm + " entry_schema", "string", prop.getEntrySchema().getType().getName());
151 List<JpaToscaConstraint> constraints = prop.getEntrySchema().getConstraints();
152 assertNotNull(testnm + " constraints", constraints);
154 assertEquals(testnm + " constraint size", 1, constraints.size());
155 assertTrue(testnm + " constraint type", constraints.get(0) instanceof JpaToscaConstraintValidValues);
156 JpaToscaConstraintValidValues constraint = (JpaToscaConstraintValidValues) constraints.get(0);
158 assertEquals(testnm + " valid values", "[US, International]", constraint.getValidValues().toString());
161 private void validateIdentity(String testName, JpaToscaProperty prop) {
162 String testnm = testName + " identity";
164 assertNotNull(testnm, prop);
165 assertNull(testnm + " metadata", prop.getMetadata());
168 private void validateMatchable(String testName, Map<String, String> metadata) {
169 String testnm = testName + " matchable";
171 assertNotNull(testnm + " metadata", metadata);
172 assertEquals(testnm + " value", "true", metadata.get("matchable"));