Add metadata to properties
[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  * ================================================================================
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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=========================================================
17  */
18
19 package org.onap.policy.models.tosca.simple.serialization;
20
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;
25
26 import java.util.List;
27 import java.util.Map;
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;
41
42 public class OptimizationPolicyTypeSerializationTest {
43
44     private static final String TYPE_ROOT = "tosca.policies.Root";
45     private static final String VERSION = "1.0.0";
46
47     private static final String INPUT_YAML = "policytypes/onap.policies.Optimization.yaml";
48
49     private StandardCoder coder;
50
51     @Before
52     public void setUp() {
53         coder = new StandardCoder();
54     }
55
56     @Test
57     public void test() throws CoderException {
58         JpaToscaServiceTemplate svctmpl = loadYaml(INPUT_YAML);
59         validate("initial object", svctmpl);
60
61         String ser = serialize(svctmpl);
62         JpaToscaServiceTemplate svctmpl2 = deserialize(ser);
63         validate("copy", svctmpl2);
64
65         assertEquals(svctmpl, svctmpl2);
66     }
67
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);
74     }
75
76     private JpaToscaServiceTemplate deserialize(String json) throws CoderException {
77         ToscaServiceTemplate auth = coder.decode(json, ToscaServiceTemplate.class);
78
79         JpaToscaServiceTemplate svctmpl = new JpaToscaServiceTemplate();
80         svctmpl.fromAuthorative(auth);
81         return svctmpl;
82     }
83
84     private String serialize(JpaToscaServiceTemplate svctmpl) throws CoderException {
85         ToscaServiceTemplate auth = svctmpl.toAuthorative();
86         return coder.encode(auth);
87     }
88
89     private void validate(String testnm, JpaToscaServiceTemplate svctmpl) {
90         JpaToscaPolicyTypes policyTypes = svctmpl.getPolicyTypes();
91
92         assertEquals(testnm + " type count", 1, policyTypes.getConceptMap().size());
93         JpaToscaPolicyType policyType = policyTypes.getConceptMap().values().iterator().next();
94
95         assertEquals(testnm + " name", "onap.policies.Optimization", policyType.getName());
96         assertEquals(testnm + " version", VERSION, policyType.getVersion());
97
98         assertNotNull(testnm + " derived from", policyType.getDerivedFrom());
99         assertEquals(testnm + " derived from name", TYPE_ROOT, policyType.getDerivedFrom().getName());
100
101         assertEquals(testnm + " description", "The base policy type for all policies that govern optimization",
102                         policyType.getDescription());
103
104         Map<String, JpaToscaProperty> props = policyType.getProperties();
105         assertNotNull(testnm + " properties", props);
106
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"));
112     }
113
114     // only need to validate deep match of one of these; geography is the most interesting
115
116     private void validateScope(String testName, JpaToscaProperty prop) {
117         String testnm = testName + " scope";
118
119         assertNotNull(testnm, prop);
120         validateMatchable(testnm, prop.getMetadata());
121     }
122
123     private void validateServices(String testName, JpaToscaProperty prop) {
124         String testnm = testName + " services";
125
126         assertNotNull(testnm, prop);
127         validateMatchable(testnm, prop.getMetadata());
128     }
129
130     private void validateResources(String testName, JpaToscaProperty prop) {
131         String testnm = testName + " resources";
132
133         assertNotNull(testnm, prop);
134         validateMatchable(testnm, prop.getMetadata());
135     }
136
137     private void validateGeography(String testName, JpaToscaProperty prop) {
138         String testnm = testName + " geography";
139
140         assertNotNull(testnm, prop);
141
142         // this line results in a stack overflow
143         // assertEquals(testnm + " name", "geography", prop.getName());
144
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());
150
151         List<JpaToscaConstraint> constraints = prop.getEntrySchema().getConstraints();
152         assertNotNull(testnm + " constraints", constraints);
153
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);
157
158         assertEquals(testnm + " valid values", "[US, International]", constraint.getValidValues().toString());
159     }
160
161     private void validateIdentity(String testName, JpaToscaProperty prop) {
162         String testnm = testName + " identity";
163
164         assertNotNull(testnm, prop);
165         assertNull(testnm + " metadata", prop.getMetadata());
166     }
167
168     private void validateMatchable(String testName, Map<String, String> metadata) {
169         String testnm = testName + " matchable";
170
171         assertNotNull(testnm + " metadata", metadata);
172         assertEquals(testnm + " value", "true", metadata.get("matchable"));
173     }
174 }