fb258a215c2ffc2b31838e32b19d88431dd9f988
[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  * 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
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
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=========================================================
18  */
19
20 package org.onap.policy.models.tosca.simple.serialization;
21
22 import static org.junit.Assert.assertEquals;
23 import static org.junit.Assert.assertNotNull;
24 import static org.junit.Assert.assertTrue;
25
26 import java.util.List;
27 import java.util.Map;
28
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;
42
43 public class OptimizationPolicyTypeSerializationTest {
44
45     private static final String TYPE_ROOT = "tosca.policies.Root";
46     private static final String VERSION = "1.0.0";
47
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";
52
53     private StandardCoder coder;
54
55     @Before
56     public void setUp() {
57         coder = new StandardCoder();
58     }
59
60     @Test
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);
64
65         String ser = serialize(svctmpl);
66         JpaToscaServiceTemplate svctmpl2 = deserialize(ser);
67         validate("copy", svctmpl2, TYPE_ROOT, "onap.policies.Optimization", false, false);
68
69         assertEquals(svctmpl, svctmpl2);
70     }
71
72     @Test
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,
76                 true);
77
78         String ser = serialize(svctmpl);
79         JpaToscaServiceTemplate svctmpl2 = deserialize(ser);
80         validate("copy", svctmpl2, "onap.policies.Optimization", "onap.policies.optimization.Resource", true, true);
81
82         assertEquals(svctmpl, svctmpl2);
83     }
84
85     @Test
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,
89                 true);
90
91         String ser = serialize(svctmpl);
92         JpaToscaServiceTemplate svctmpl2 = deserialize(ser);
93         validate("copy", svctmpl2, "onap.policies.Optimization", "onap.policies.optimization.Service", false, true);
94
95         assertEquals(svctmpl, svctmpl2);
96     }
97
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);
104     }
105
106     private JpaToscaServiceTemplate deserialize(String json) throws CoderException {
107         ToscaServiceTemplate auth = coder.decode(json, ToscaServiceTemplate.class);
108
109         JpaToscaServiceTemplate svctmpl = new JpaToscaServiceTemplate();
110         svctmpl.fromAuthorative(auth);
111         return svctmpl;
112     }
113
114     private String serialize(JpaToscaServiceTemplate svctmpl) throws CoderException {
115         ToscaServiceTemplate auth = svctmpl.toAuthorative();
116         return coder.encode(auth);
117     }
118
119     private void validate(String testnm, JpaToscaServiceTemplate svctmpl, String derivedFrom, String typeName,
120             boolean checkResource, boolean checkService) {
121         JpaToscaPolicyTypes policyTypes = svctmpl.getPolicyTypes();
122
123         assertEquals(testnm + " type count", 1, policyTypes.getConceptMap().size());
124         JpaToscaPolicyType policyType = policyTypes.getConceptMap().values().iterator().next();
125
126         assertEquals(testnm + " name", typeName, policyType.getName());
127         assertEquals(testnm + " version", VERSION, policyType.getVersion());
128
129         assertNotNull(testnm + " derived from", policyType.getDerivedFrom());
130         assertEquals(testnm + " derived from name", derivedFrom, policyType.getDerivedFrom().getName());
131
132         Map<String, JpaToscaProperty> props = policyType.getProperties();
133         assertNotNull(testnm + " properties", props);
134
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"));
140         } else {
141             validateScope(testnm, props.get("scope"));
142             validateGeography(testnm, props.get("geography"));
143             validateIdentity(testnm, props.get("identity"));
144         }
145     }
146
147     // only need to validate deep match of one of these; geography is the most interesting
148
149     private void validateScope(String testName, JpaToscaProperty prop) {
150         String testnm = testName + " scope";
151
152         assertNotNull(testnm, prop);
153         validateMatchable(testnm, prop.getMetadata());
154     }
155
156     private void validateServices(String testName, JpaToscaProperty prop) {
157         String testnm = testName + " services";
158
159         assertNotNull(testnm, prop);
160         validateMatchable(testnm, prop.getMetadata());
161     }
162
163     private void validateResources(String testName, JpaToscaProperty prop) {
164         String testnm = testName + " resources";
165
166         assertNotNull(testnm, prop);
167         validateMatchable(testnm, prop.getMetadata());
168     }
169
170     private void validateGeography(String testName, JpaToscaProperty prop) {
171         String testnm = testName + " geography";
172
173         assertNotNull(testnm, prop);
174
175         // this line results in a stack overflow
176         // assertEquals(testnm + " name", "geography", prop.getName());
177
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());
183
184         List<JpaToscaConstraint> constraints = prop.getEntrySchema().getConstraints();
185         assertNotNull(testnm + " constraints", constraints);
186
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);
190
191         assertEquals(testnm + " valid values", "[US, International]", constraint.getValidValues().toString());
192     }
193
194     private void validateIdentity(String testName, JpaToscaProperty prop) {
195         String testnm = testName + " identity";
196
197         assertNotNull(testnm, prop);
198         assertEquals(testnm + " metadata", 0, prop.getMetadata().size());
199     }
200
201     private void validateMatchable(String testName, Map<String, String> metadata) {
202         String testnm = testName + " matchable";
203
204         assertNotNull(testnm + " metadata", metadata);
205         assertEquals(testnm + " value", "true", metadata.get("matchable"));
206     }
207 }