2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019-2021, 2023 Nordix Foundation.
4 * Modifications Copyright (C) 2019-2021 AT&T Intellectual Property. All rights reserved.
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.models.tosca.simple.concepts;
24 import static org.assertj.core.api.Assertions.assertThat;
25 import static org.assertj.core.api.Assertions.assertThatThrownBy;
26 import static org.junit.Assert.assertEquals;
27 import static org.junit.Assert.assertFalse;
28 import static org.junit.Assert.assertNotEquals;
29 import static org.junit.Assert.assertNotNull;
30 import static org.junit.Assert.assertTrue;
32 import java.util.LinkedHashMap;
34 import java.util.TreeMap;
35 import org.junit.Test;
36 import org.onap.policy.common.parameters.BeanValidationResult;
37 import org.onap.policy.models.base.PfConceptKey;
38 import org.onap.policy.models.base.PfReferenceKey;
39 import org.onap.policy.models.base.Validated;
42 * DAO test for ToscaDatatype.
44 * @author Liam Fallon (liam.fallon@est.tech)
46 public class JpaToscaServiceTemplateTest {
48 private static final String KEY_IS_NULL = "key is marked .*on.*ull but is null";
49 private static final String VERSION_001 = "0.0.1";
52 public void testServiceTemplateNull() {
53 assertNotNull(new JpaToscaServiceTemplate());
54 assertNotNull(new JpaToscaServiceTemplate(new PfConceptKey()));
55 assertNotNull(new JpaToscaServiceTemplate(new PfConceptKey(), ""));
56 assertNotNull(new JpaToscaServiceTemplate(new JpaToscaServiceTemplate()));
58 assertThatThrownBy(() -> new JpaToscaServiceTemplate((PfConceptKey) null)).hasMessageMatching(KEY_IS_NULL);
60 assertThatThrownBy(() -> new JpaToscaServiceTemplate(null, null)).hasMessageMatching(KEY_IS_NULL);
62 assertThatThrownBy(() -> new JpaToscaServiceTemplate(null, "")).hasMessageMatching(KEY_IS_NULL);
64 assertThatThrownBy(() -> new JpaToscaServiceTemplate(new PfConceptKey(), null))
65 .hasMessageMatching("toscaDefinitionsVersion is marked .*on.*ull but is null");
67 assertThatThrownBy(() -> new JpaToscaServiceTemplate((JpaToscaServiceTemplate) null))
68 .isInstanceOf(NullPointerException.class);
72 public void testServiceTemplatePojo() {
73 PfConceptKey tstKey = new PfConceptKey("tst", VERSION_001);
74 JpaToscaServiceTemplate tst = new JpaToscaServiceTemplate(tstKey, "Tosca Version");
76 PfConceptKey dataTypeKey = new PfConceptKey("DataType", VERSION_001);
77 JpaToscaDataType dataType0 = new JpaToscaDataType(dataTypeKey);
78 PfConceptKey dtsKey = new PfConceptKey("dts", VERSION_001);
79 Map<PfConceptKey, JpaToscaDataType> dataTypeMap = new TreeMap<>();
80 dataTypeMap.put(dataTypeKey, dataType0);
81 JpaToscaDataTypes dataTypes = new JpaToscaDataTypes(dtsKey, dataTypeMap);
82 tst.setDataTypes(dataTypes);
83 assertEquals(dataTypes, tst.getDataTypes());
85 PfConceptKey policyTypeKey = new PfConceptKey("DataType", VERSION_001);
86 JpaToscaPolicyType policyType0 = new JpaToscaPolicyType(policyTypeKey);
87 PfConceptKey ptsKey = new PfConceptKey("dts", VERSION_001);
88 Map<PfConceptKey, JpaToscaPolicyType> policyTypeMap = new TreeMap<>();
89 policyTypeMap.put(policyTypeKey, policyType0);
90 JpaToscaPolicyTypes policyTypes = new JpaToscaPolicyTypes(ptsKey, policyTypeMap);
91 tst.setPolicyTypes(policyTypes);
92 assertEquals(policyTypes, tst.getPolicyTypes());
94 PfReferenceKey tttKey = new PfReferenceKey(tstKey, "TopologyTemplate");
95 JpaToscaTopologyTemplate ttt = new JpaToscaTopologyTemplate(tttKey);
96 tst.setTopologyTemplate(ttt);
97 assertEquals(ttt, tst.getTopologyTemplate());
99 assertCloneAndCopies(tstKey, tst, dataTypes, policyTypes, ttt);
101 assertTrue(new JpaToscaServiceTemplate().validate("").isValid());
102 assertTrue(tst.validate("").isValid());
104 tst.setDescription(null);
105 assertTrue(tst.validate("").isValid());
106 tst.setDescription("");
107 assertFalse(tst.validate("").isValid());
108 tst.setDescription("A Description");
109 assertTrue(tst.validate("").isValid());
111 assertThatThrownBy(() -> tst.validate(null)).hasMessageMatching("fieldName is marked .*on.*ull but is null");
113 tst.setToscaDefinitionsVersion(null);
114 BeanValidationResult result = tst.validate("");
115 assertThat(result.getResult()).contains("tosca_definitions_version").contains(Validated.IS_NULL);
117 tst.setToscaDefinitionsVersion(JpaToscaServiceTemplate.DEFAULT_TOSCA_DEFINITIONS_VERSION);
118 tst.setDataTypes(null);
119 result = tst.validate("");
120 assertTrue(result.isValid());
122 JpaToscaPolicyType pt0 = new JpaToscaPolicyType(new PfConceptKey("pt0:0.0.1"));
123 tst.getPolicyTypes().getConceptMap().put(pt0.getKey(), pt0);
124 result = tst.validate("");
125 assertTrue(result.isValid());
127 JpaToscaDataType dt0 = new JpaToscaDataType(new PfConceptKey("dt0:0.0.1"));
128 JpaToscaProperty prop0 = new JpaToscaProperty(new PfReferenceKey(pt0.getKey(), "prop0"));
129 prop0.setType(dt0.getKey());
131 assertDataTypesAndToscaTopology(tst, pt0, dt0, prop0);
133 tst.setPolicyTypes(null);
134 result = tst.validate("");
135 assertTrue(result.isValid());
137 assertNoPolicyType(tst, policyTypes, pt0, dt0);
141 private static void assertDataTypesAndToscaTopology(JpaToscaServiceTemplate tst, JpaToscaPolicyType pt0,
142 JpaToscaDataType dt0, JpaToscaProperty prop0) {
143 BeanValidationResult result;
144 pt0.setProperties(new LinkedHashMap<>());
145 pt0.getProperties().put(prop0.getKey().getLocalName(), prop0);
146 result = tst.validate("");
147 assertFalse(result.isValid());
148 assertThat(result.getResult()).contains("data type").contains("dt0:0.0.1").contains(Validated.NOT_FOUND);
150 tst.setDataTypes(null);
151 result = tst.validate("");
152 assertFalse(result.isValid());
153 assertThat(result.getResult()).contains("data type").contains("dt0:0.0.1").contains(Validated.NOT_FOUND);
155 tst.setDataTypes(new JpaToscaDataTypes());
156 result = tst.validate("");
157 assertFalse(result.isValid());
158 assertThat(result.getResult()).contains("data type").contains("dt0:0.0.1").contains(Validated.NOT_FOUND);
160 tst.getDataTypes().getConceptMap().put(dt0.getKey(), dt0);
161 result = tst.validate("");
162 assertTrue(result.isValid());
164 tst.setTopologyTemplate(null);
165 result = tst.validate("");
166 assertTrue(result.isValid());
168 tst.setTopologyTemplate(new JpaToscaTopologyTemplate());
169 result = tst.validate("");
170 assertTrue(result.isValid());
172 tst.getTopologyTemplate().setPolicies(new JpaToscaPolicies());
173 result = tst.validate("");
174 assertTrue(result.isValid());
177 private static void assertCloneAndCopies(PfConceptKey tstKey, JpaToscaServiceTemplate tst,
178 JpaToscaDataTypes dataTypes, JpaToscaPolicyTypes policyTypes,
179 JpaToscaTopologyTemplate ttt) {
180 JpaToscaServiceTemplate tttClone0 = new JpaToscaServiceTemplate(tst);
181 assertEquals(tst, tttClone0);
182 assertEquals(0, tst.compareTo(tttClone0));
184 JpaToscaServiceTemplate tttClone1 = new JpaToscaServiceTemplate(tst);
185 assertEquals(tst, tttClone1);
186 assertEquals(0, tst.compareTo(tttClone1));
188 assertEquals(-1, tst.compareTo(null));
189 assertEquals(0, tst.compareTo(tst));
190 assertNotEquals(0, tst.compareTo(tst.getKey()));
192 PfConceptKey otherDtKey = new PfConceptKey("otherDt", VERSION_001);
193 JpaToscaServiceTemplate otherDt = new JpaToscaServiceTemplate(otherDtKey);
195 assertNotEquals(0, tst.compareTo(otherDt));
196 otherDt.setKey(tstKey);
197 assertNotEquals(0, tst.compareTo(otherDt));
198 otherDt.setToscaDefinitionsVersion("Tosca Version");
199 assertNotEquals(0, tst.compareTo(otherDt));
200 otherDt.setDataTypes(dataTypes);
201 assertNotEquals(0, tst.compareTo(otherDt));
202 otherDt.setPolicyTypes(policyTypes);
203 assertNotEquals(0, tst.compareTo(otherDt));
204 otherDt.setTopologyTemplate(ttt);
205 assertEquals(0, tst.compareTo(otherDt));
207 assertEquals(6, tst.getKeys().size());
208 assertEquals(1, new JpaToscaServiceTemplate().getKeys().size());
210 new JpaToscaServiceTemplate().clean();
212 assertEquals(tttClone0, tst);
215 private static void assertNoPolicyType(JpaToscaServiceTemplate tst, JpaToscaPolicyTypes policyTypes,
216 JpaToscaPolicyType pt0, JpaToscaDataType dt0) {
217 BeanValidationResult result;
218 JpaToscaPolicy pol0 = new JpaToscaPolicy(new PfConceptKey("pol0:0.0.1"));
219 tst.getTopologyTemplate().getPolicies().getConceptMap().put(pol0.getKey(), pol0);
220 result = tst.validate("");
221 assertFalse(result.isValid());
222 assertThat(result.getResult()).contains("type").contains(Validated.IS_A_NULL_KEY);
224 pol0.setType(new PfConceptKey("i.dont.Exist:0.0.1"));
225 result = tst.validate("");
226 assertFalse(result.isValid());
227 assertThat(result.getResult()).contains(
228 "no policy types are defined on the service template for the policies in the topology template");
230 tst.setPolicyTypes(policyTypes);
231 result = tst.validate("");
232 assertFalse(result.isValid());
233 assertThat(result.getResult()).contains("policy type").contains("i.dont.Exist:0.0.1")
234 .contains(Validated.NOT_FOUND);
236 pol0.setType(dt0.getKey());
237 result = tst.validate("");
238 assertFalse(result.isValid());
239 assertThat(result.getResult()).contains("policy type").contains("dt0:0.0.1").contains(Validated.NOT_FOUND);
241 pol0.setType(pt0.getKey());
242 result = tst.validate("");
243 assertTrue(result.isValid());
245 tst.setPolicyTypes(null);
246 result = tst.validate("");
247 assertFalse(result.isValid());
248 assertThat(result.getResult()).contains(
249 "no policy types are defined on the service template for the policies in the topology template");
251 tst.setPolicyTypes(policyTypes);
252 pol0.setType(pt0.getKey());
253 result = tst.validate("");
254 assertTrue(result.isValid());
256 tst.setPolicyTypes(new JpaToscaPolicyTypes());
257 result = tst.validate("");
258 assertFalse(result.isValid());
259 assertThat(result.getResult()).contains(
260 "no policy types are defined on the service template for the policies in the topology template");