Changes for Checkstyle 8.32
[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 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_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";
51
52     private StandardCoder coder;
53
54     @Before
55     public void setUp() {
56         coder = new StandardCoder();
57     }
58
59     @Test
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);
63
64         String ser = serialize(svctmpl);
65         JpaToscaServiceTemplate svctmpl2 = deserialize(ser);
66         validate("copy", svctmpl2, TYPE_ROOT, "onap.policies.Optimization", false, false);
67
68         assertEquals(svctmpl, svctmpl2);
69     }
70
71     @Test
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,
75                 true);
76
77         String ser = serialize(svctmpl);
78         JpaToscaServiceTemplate svctmpl2 = deserialize(ser);
79         validate("copy", svctmpl2, "onap.policies.Optimization", "onap.policies.optimization.Resource", true, true);
80
81         assertEquals(svctmpl, svctmpl2);
82     }
83
84     @Test
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,
88                 true);
89
90         String ser = serialize(svctmpl);
91         JpaToscaServiceTemplate svctmpl2 = deserialize(ser);
92         validate("copy", svctmpl2, "onap.policies.Optimization", "onap.policies.optimization.Service", false, true);
93
94         assertEquals(svctmpl, svctmpl2);
95     }
96
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);
103     }
104
105     private JpaToscaServiceTemplate deserialize(String json) throws CoderException {
106         ToscaServiceTemplate auth = coder.decode(json, ToscaServiceTemplate.class);
107
108         JpaToscaServiceTemplate svctmpl = new JpaToscaServiceTemplate();
109         svctmpl.fromAuthorative(auth);
110         return svctmpl;
111     }
112
113     private String serialize(JpaToscaServiceTemplate svctmpl) throws CoderException {
114         ToscaServiceTemplate auth = svctmpl.toAuthorative();
115         return coder.encode(auth);
116     }
117
118     private void validate(String testnm, JpaToscaServiceTemplate svctmpl, String derivedFrom, String typeName,
119             boolean checkResource, boolean checkService) {
120         JpaToscaPolicyTypes policyTypes = svctmpl.getPolicyTypes();
121
122         assertEquals(testnm + " type count", 1, policyTypes.getConceptMap().size());
123         JpaToscaPolicyType policyType = policyTypes.getConceptMap().values().iterator().next();
124
125         assertEquals(testnm + " name", typeName, policyType.getName());
126         assertEquals(testnm + " version", VERSION, policyType.getVersion());
127
128         assertNotNull(testnm + " derived from", policyType.getDerivedFrom());
129         assertEquals(testnm + " derived from name", derivedFrom, policyType.getDerivedFrom().getName());
130
131         Map<String, JpaToscaProperty> props = policyType.getProperties();
132         assertNotNull(testnm + " properties", props);
133
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"));
139         } else {
140             validateScope(testnm, props.get("scope"));
141             validateGeography(testnm, props.get("geography"));
142             validateIdentity(testnm, props.get("identity"));
143         }
144     }
145
146     // only need to validate deep match of one of these; geography is the most interesting
147
148     private void validateScope(String testName, JpaToscaProperty prop) {
149         String testnm = testName + " scope";
150
151         assertNotNull(testnm, prop);
152         validateMatchable(testnm, prop.getMetadata());
153     }
154
155     private void validateServices(String testName, JpaToscaProperty prop) {
156         String testnm = testName + " services";
157
158         assertNotNull(testnm, prop);
159         validateMatchable(testnm, prop.getMetadata());
160     }
161
162     private void validateResources(String testName, JpaToscaProperty prop) {
163         String testnm = testName + " resources";
164
165         assertNotNull(testnm, prop);
166         validateMatchable(testnm, prop.getMetadata());
167     }
168
169     private void validateGeography(String testName, JpaToscaProperty prop) {
170         String testnm = testName + " geography";
171
172         assertNotNull(testnm, prop);
173
174         // this line results in a stack overflow
175         // assertEquals(testnm + " name", "geography", prop.getName());
176
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());
182
183         List<JpaToscaConstraint> constraints = prop.getEntrySchema().getConstraints();
184         assertNotNull(testnm + " constraints", constraints);
185
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);
189
190         assertEquals(testnm + " valid values", "[US, International]", constraint.getValidValues().toString());
191     }
192
193     private void validateIdentity(String testName, JpaToscaProperty prop) {
194         String testnm = testName + " identity";
195
196         assertNotNull(testnm, prop);
197         assertEquals(testnm + " metadata", 0, prop.getMetadata().size());
198     }
199
200     private void validateMatchable(String testName, Map<String, String> metadata) {
201         String testnm = testName + " matchable";
202
203         assertNotNull(testnm + " metadata", metadata);
204         assertEquals(testnm + " value", "true", metadata.get("matchable"));
205     }
206 }